commit 7ab0655151664f60377c4077e26ac4c99150683f
parent e926da88667b3230a846ec7178168d5b07343a44
Author: cowmonk <cowmonk@based.pt>
Date: Sun, 29 Nov 2026 22:02:32 -0700
fsck: port NetBSD's fsck_ext2fs and add fsck.vfat
for NetBSD's fsck_ext2fs: kept all five passes for aruu ext4.
count the internal journal's blocks and check read-only. stop
if replay is needed or the volume uses extents. Since these
aren't supported and I'm too lazy to make my own implementation
unfortunately lwext4's implementation for these were under GPLv2.
(at least for xattrs and extent)
+ some custom glue for musl, device I/O, 32-byte group descriptors,
64-bit offsets & Linux fsck exit codes. need more testing for larger
sizes.
alias fsck.vfat as fsck.fat. install the symlink and give aruu-box
the same alias. rebuild dispatch when genbox.sh changes.
Diffstat:
27 files changed, 6665 insertions(+), 36 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -70,4 +70,5 @@ based on code written by Rob Landley
mkfs.fat is a port of OpenBSD's newfs_msdos (BSD 2-Clause).
mkfs.ext4 is based on lwext4's formatter (BSD 3-Clause).
fsck.fat is from Android's BSD fsck_msdos. See cmd/extra/fsck.fat/NOTICE for its license notices.
+fsck.ext4 adapts checks from NetBSD's fsck_ext2fs. See cmd/extra/fsck.ext4/NOTICE.
The rest are based on SBASE/UBASE source code
diff --git a/README b/README
@@ -152,8 +152,8 @@ xsi:
standalone and installed with setuid
extra:
- b3sum, blkid, dc, fdisk, fsck.fat, kmap, lsblk, mkfs.ext4, mkfs.fat,
- partplan, pgrep, signify, sync, yap
+ b3sum, blkid, dc, fdisk, fsck.ext4, fsck.fat, kmap, lsblk, mkfs.ext4,
+ mkfs.fat, partplan, pgrep, signify, sync, yap
TODOs / PENDING RELEASE
=======================
diff --git a/cmd/dev/ninqu/ninqu b/cmd/dev/ninqu/ninqu
Binary files differ.
diff --git a/cmd/extra/fsck.ext4/NOTICE b/cmd/extra/fsck.ext4/NOTICE
@@ -0,0 +1,484 @@
+NetBSD fsck_ext2fs and supporting files
+https://github.com/NetBSD/src/tree/7f97e06ed25899bf3fcb577111b0caf5b4d8f69b/sbin/fsck_ext2fs
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)fs.h 8.10 (Berkeley) 10/27/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)fs.h 8.10 (Berkeley) 10/27/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/*
+ * Copyright (c) 1982, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)dinode.h 8.6 (Berkeley) 9/13/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)dinode.h 8.6 (Berkeley) 9/13/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1982, 1986, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)dir.h 8.4 (Berkeley) 8/10/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)dir.h 8.4 (Berkeley) 8/10/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1996 Christos Zoulas. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ * Copyright (c) 1994 James A. Jegers
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)fsck.h 8.1 (Berkeley) 6/5/93
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)fsck.h 8.1 (Berkeley) 6/5/93
+ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
diff --git a/cmd/extra/fsck.ext4/compat.h b/cmd/extra/fsck.ext4/compat.h
@@ -0,0 +1,57 @@
+/* aruu replacements for NetBSD userspace interfaces. */
+#include "util.h"
+
+#include <dirent.h>
+#include <stdint.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#define daddr_t int64_t
+#define IFMT S_IFMT
+#define IFIFO S_IFIFO
+#define IFCHR S_IFCHR
+#define IFDIR S_IFDIR
+#define IFBLK S_IFBLK
+#define IFREG S_IFREG
+#define IFLNK S_IFLNK
+#define IFSOCK S_IFSOCK
+
+#ifndef __RCSID
+#define __RCSID(s) typedef char rcsid[sizeof(s)]
+#endif
+#ifndef __KERNEL_RCSID
+#define __KERNEL_RCSID(n, s) __RCSID(s)
+#endif
+#ifndef __dead
+#define __dead __attribute__((__noreturn__))
+#endif
+#ifndef __unused
+#define __unused __attribute__((__unused__))
+#endif
+#ifndef __printflike
+#define __printflike(f, a) __attribute__((__format__(__printf__, f, a)))
+#endif
+#ifndef __UNCONST
+#define __UNCONST(p) ((void *)(uintptr_t)(const void *)(p))
+#endif
+
+#define getprogname() argv0
+
+#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define fsck_little_endian 1
+#else
+#define bswap16 __builtin_bswap16
+#define bswap32 __builtin_bswap32
+#define bswap64 __builtin_bswap64
+#endif
+
+#ifndef __COPYRIGHT
+#define __COPYRIGHT(s) typedef char upstream_copyright[sizeof(s)]
+#endif
+#ifndef roundup2
+#define roundup2(x, y) (((x) + (y) - 1) & ~((y) - 1))
+#endif
+#ifndef btodb
+#define btodb(bytes) ((bytes) / DEV_BSIZE)
+#endif
+#define UFS_DIRBLKSIZ 512
diff --git a/cmd/extra/fsck.ext4/dir.c b/cmd/extra/fsck.ext4/dir.c
@@ -0,0 +1,720 @@
+/* $NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 dholland Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)dir.c 8.5 (Berkeley) 12/8/94";
+#else
+__RCSID("$NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 dholland Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs.h>
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+
+#include "fsck.h"
+#include "fsutil.h"
+#include "extern.h"
+
+const char *lfname = "lost+found";
+int lfmode = 01700;
+struct ext2fs_dirtemplate emptydir = {
+ .dot_ino = 0,
+ .dot_reclen = UFS_DIRBLKSIZ,
+};
+struct ext2fs_dirtemplate dirhead = {
+ .dot_ino = 0,
+ .dot_reclen = 12,
+ .dot_namlen = 1,
+ .dot_type = EXT2_FT_DIR,
+ .dot_name = ".",
+ .dotdot_ino = 0,
+ .dotdot_reclen = UFS_DIRBLKSIZ - 12,
+ .dotdot_namlen = 2,
+ .dotdot_type = EXT2_FT_DIR,
+ .dotdot_name = "..",
+};
+#undef UFS_DIRBLKSIZ
+
+static int expanddir(struct ext2fs_dinode *, char *);
+static void freedir(ino_t, ino_t);
+static struct ext2fs_direct *fsck_readdir(struct inodesc *);
+static struct bufarea *getdirblk(daddr_t, long);
+static int lftempname(char *, ino_t);
+static int mkentry(struct inodesc *);
+static int chgino(struct inodesc *);
+
+/*
+ * Propagate connected state through the tree.
+ */
+void
+propagate(void)
+{
+ struct inoinfo **inpp, *inp, *pinp;
+ struct inoinfo **inpend;
+
+ /*
+ * Create a list of children for each directory.
+ */
+ inpend = &inpsort[inplast];
+ for (inpp = inpsort; inpp < inpend; inpp++) {
+ inp = *inpp;
+ if (inp->i_parent == 0 ||
+ inp->i_number == EXT2_ROOTINO)
+ continue;
+ pinp = getinoinfo(inp->i_parent);
+ inp->i_parentp = pinp;
+ inp->i_sibling = pinp->i_child;
+ pinp->i_child = inp;
+ }
+ inp = getinoinfo(EXT2_ROOTINO);
+ while (inp) {
+ statemap[inp->i_number] = DFOUND;
+ if (inp->i_child &&
+ statemap[inp->i_child->i_number] == DSTATE)
+ inp = inp->i_child;
+ else if (inp->i_sibling)
+ inp = inp->i_sibling;
+ else
+ inp = inp->i_parentp;
+ }
+}
+
+/*
+ * Scan each entry in a directory block.
+ */
+int
+dirscan(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dp;
+ struct bufarea *bp;
+ int dsize, n;
+ long blksiz;
+ char *dbuf = NULL;
+
+ if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL)
+ err(8, "Can't allocate directory block");
+
+ if (idesc->id_type != DATA)
+ errexit("wrong type to dirscan %d", idesc->id_type);
+ if (idesc->id_entryno == 0 &&
+ (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0)
+ idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize);
+ blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
+ if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
+ idesc->id_filesize -= blksiz;
+ free(dbuf);
+ return (SKIP);
+ }
+ idesc->id_loc = 0;
+ for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
+ dsize = fs2h16(dp->e2d_reclen);
+ memcpy(dbuf, dp, (size_t)dsize);
+ idesc->id_dirp = (struct ext2fs_direct *)dbuf;
+ if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
+ bp = getdirblk(idesc->id_blkno, blksiz);
+ memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
+ (size_t)dsize);
+ dirty(bp);
+ sbdirty();
+ }
+ if (n & STOP) {
+ free(dbuf);
+ return (n);
+ }
+ }
+ free(dbuf);
+ return (idesc->id_filesize > 0 ? KEEPON : STOP);
+}
+
+/*
+ * get next entry in a directory.
+ */
+static struct ext2fs_direct *
+fsck_readdir(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dp, *ndp;
+ struct bufarea *bp;
+ long size, blksiz, fix, dploc;
+
+ blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
+ bp = getdirblk(idesc->id_blkno, blksiz);
+ if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 &&
+ idesc->id_loc < blksiz) {
+ dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
+ if (dircheck(idesc, dp))
+ goto dpok;
+ if (idesc->id_fix == IGNORE)
+ return (0);
+ fix = dofix(idesc, "DIRECTORY CORRUPTED");
+ bp = getdirblk(idesc->id_blkno, blksiz);
+ dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
+ dp->e2d_reclen = h2fs16(sblock.e2fs_bsize);
+ dp->e2d_ino = 0;
+ dp->e2d_namlen = 0;
+ dp->e2d_type = 0;
+ dp->e2d_name[0] = '\0';
+ if (fix)
+ dirty(bp);
+ idesc->id_loc += sblock.e2fs_bsize;
+ idesc->id_filesize -= sblock.e2fs_bsize;
+ return (dp);
+ }
+dpok:
+ if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
+ return NULL;
+ dploc = idesc->id_loc;
+ dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
+ idesc->id_loc += fs2h16(dp->e2d_reclen);
+ idesc->id_filesize -= fs2h16(dp->e2d_reclen);
+ if ((idesc->id_loc % sblock.e2fs_bsize) == 0)
+ return (dp);
+ ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
+ if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
+ dircheck(idesc, ndp) == 0) {
+ size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
+ idesc->id_loc += size;
+ idesc->id_filesize -= size;
+ if (idesc->id_fix == IGNORE)
+ return (0);
+ fix = dofix(idesc, "DIRECTORY CORRUPTED");
+ bp = getdirblk(idesc->id_blkno, blksiz);
+ dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
+ dp->e2d_reclen = h2fs16(fs2h16(dp->e2d_reclen) + size);
+ if (fix)
+ dirty(bp);
+ }
+ return (dp);
+}
+
+/*
+ * Verify that a directory entry is valid.
+ * This is a superset of the checks made in the kernel.
+ */
+int
+dircheck(struct inodesc *idesc, struct ext2fs_direct *dp)
+{
+ int size;
+ char *cp;
+ int spaceleft;
+ u_int16_t reclen = fs2h16(dp->e2d_reclen);
+
+ spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
+ if (fs2h32(dp->e2d_ino) > maxino ||
+ reclen == 0 ||
+ reclen > spaceleft ||
+ (reclen & 0x3) != 0)
+ return (0);
+ if (dp->e2d_ino == 0)
+ return (1);
+ if (sblock.e2fs.e2fs_rev < E2FS_REV1 ||
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0)
+ if (dp->e2d_type != 0)
+ return (1);
+ size = EXT2FS_DIRSIZ(dp->e2d_namlen);
+ if (reclen < size ||
+ idesc->id_filesize < size /* ||
+ dp->e2d_namlen > EXT2FS_MAXNAMLEN */)
+ return (0);
+ for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++)
+ if (*cp == '\0' || (*cp++ == '/'))
+ return (0);
+ return (1);
+}
+
+void
+direrror(ino_t ino, const char *errmesg)
+{
+
+ fileerror(ino, ino, errmesg);
+}
+
+void
+fileerror(ino_t cwd, ino_t ino, const char *errmesg)
+{
+ struct ext2fs_dinode *dp;
+ char pathbuf[MAXPATHLEN + 1];
+
+ pwarn("%s ", errmesg);
+ pinode(ino);
+ printf("\n");
+ getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
+ if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) {
+ pfatal("NAME=%s\n", pathbuf);
+ return;
+ }
+ dp = ginode(ino);
+ if (ftypeok(dp))
+ pfatal("%s=%s\n",
+ (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
+ else
+ pfatal("NAME=%s\n", pathbuf);
+}
+
+void
+adjust(struct inodesc *idesc, short lcnt)
+{
+ struct ext2fs_dinode *dp;
+
+ dp = ginode(idesc->id_number);
+ if (fs2h16(dp->e2di_nlink) == lcnt) {
+ if (linkup(idesc->id_number, (ino_t)0) == 0)
+ clri(idesc, "UNREF", 0);
+ } else {
+ pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
+ ((fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
+ pinode(idesc->id_number);
+ printf(" COUNT %d SHOULD BE %d",
+ fs2h16(dp->e2di_nlink), fs2h16(dp->e2di_nlink) - lcnt);
+ if (preen) {
+ if (lcnt < 0) {
+ printf("\n");
+ pfatal("LINK COUNT INCREASING");
+ }
+ printf(" (ADJUSTED)\n");
+ }
+ if (preen || reply("ADJUST") == 1) {
+ dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - lcnt);
+ inodirty();
+ }
+ }
+}
+
+static int
+mkentry(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dirp = idesc->id_dirp;
+ struct ext2fs_direct newent;
+ int newlen, oldlen;
+
+ newent.e2d_type = 0; /* XXX gcc */
+ newent.e2d_namlen = strlen(idesc->id_name);
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
+ newlen = EXT2FS_DIRSIZ(newent.e2d_namlen);
+ if (dirp->e2d_ino != 0)
+ oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen);
+ else
+ oldlen = 0;
+ if (fs2h16(dirp->e2d_reclen) - oldlen < newlen)
+ return (KEEPON);
+ newent.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - oldlen);
+ dirp->e2d_reclen = h2fs16(oldlen);
+ dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen);
+ dirp->e2d_ino = h2fs32(idesc->id_parent); /* ino to be entered is in id_parent */
+ dirp->e2d_reclen = newent.e2d_reclen;
+ dirp->e2d_namlen = newent.e2d_namlen;
+ dirp->e2d_type = newent.e2d_type;
+ memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen));
+ return (ALTERED|STOP);
+}
+
+static int
+chgino(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dirp = idesc->id_dirp;
+ u_int16_t namlen = dirp->e2d_namlen;
+
+ if (strlen(idesc->id_name) != namlen ||
+ strncmp(dirp->e2d_name, idesc->id_name, (int)namlen))
+ return (KEEPON);
+ dirp->e2d_ino = h2fs32(idesc->id_parent);
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
+ else
+ dirp->e2d_type = 0;
+ return (ALTERED|STOP);
+}
+
+int
+linkup(ino_t orphan, ino_t parentdir)
+{
+ struct ext2fs_dinode *dp;
+ int lostdir;
+ ino_t oldlfdir;
+ struct inodesc idesc;
+ char tempname[BUFSIZ];
+
+ memset(&idesc, 0, sizeof(struct inodesc));
+ dp = ginode(orphan);
+ lostdir = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR;
+ pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
+ pinode(orphan);
+ if (preen && inosize(dp) == 0)
+ return (0);
+ if (preen)
+ printf(" (RECONNECTED)\n");
+ else
+ if (reply("RECONNECT") == 0)
+ return (0);
+ if (lfdir == 0) {
+ dp = ginode(EXT2_ROOTINO);
+ idesc.id_name = lfname;
+ idesc.id_type = DATA;
+ idesc.id_func = findino;
+ idesc.id_number = EXT2_ROOTINO;
+ if ((ckinode(dp, &idesc) & FOUND) != 0) {
+ lfdir = idesc.id_parent;
+ } else {
+ pwarn("NO lost+found DIRECTORY");
+ if (preen || reply("CREATE")) {
+ lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode);
+ if (lfdir != 0) {
+ if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) {
+ if (preen)
+ printf(" (CREATED)\n");
+ } else {
+ freedir(lfdir, EXT2_ROOTINO);
+ lfdir = 0;
+ if (preen)
+ printf("\n");
+ }
+ }
+ }
+ }
+ if (lfdir == 0) {
+ pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
+ printf("\n\n");
+ return (0);
+ }
+ }
+ dp = ginode(lfdir);
+ if ((fs2h16(dp->e2di_mode) & IFMT) != IFDIR) {
+ pfatal("lost+found IS NOT A DIRECTORY");
+ if (reply("REALLOCATE") == 0)
+ return (0);
+ oldlfdir = lfdir;
+ if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) {
+ pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
+ return (0);
+ }
+ if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
+ pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
+ return (0);
+ }
+ inodirty();
+ idesc.id_type = ADDR;
+ idesc.id_func = pass4check;
+ idesc.id_number = oldlfdir;
+ adjust(&idesc, lncntp[oldlfdir] + 1);
+ lncntp[oldlfdir] = 0;
+ dp = ginode(lfdir);
+ }
+ if (statemap[lfdir] != DFOUND) {
+ pfatal("SORRY. NO lost+found DIRECTORY\n\n");
+ return (0);
+ }
+ (void)lftempname(tempname, orphan);
+ if (makeentry(lfdir, orphan, tempname) == 0) {
+ pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
+ printf("\n\n");
+ return (0);
+ }
+ lncntp[orphan]--;
+ if (lostdir) {
+ if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
+ parentdir != (ino_t)-1)
+ (void)makeentry(orphan, lfdir, "..");
+ dp = ginode(lfdir);
+ dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) +1);
+ inodirty();
+ lncntp[lfdir]++;
+ pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
+ if (parentdir != (ino_t)-1)
+ printf("PARENT WAS I=%llu\n",
+ (unsigned long long)parentdir);
+ if (preen == 0)
+ printf("\n");
+ }
+ return (1);
+}
+
+/*
+ * fix an entry in a directory.
+ */
+int
+changeino(ino_t dir, const char *name, ino_t newnum)
+{
+ struct inodesc idesc;
+
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = DATA;
+ idesc.id_func = chgino;
+ idesc.id_number = dir;
+ idesc.id_fix = DONTKNOW;
+ idesc.id_name = name;
+ idesc.id_parent = newnum; /* new value for name */
+ return (ckinode(ginode(dir), &idesc));
+}
+
+/*
+ * make an entry in a directory
+ */
+int
+makeentry(ino_t parent, ino_t ino, const char *name)
+{
+ struct ext2fs_dinode *dp;
+ struct inodesc idesc;
+ char pathbuf[MAXPATHLEN + 1];
+
+ if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO)
+ || parent >= maxino ||
+ (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino)
+ return (0);
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = DATA;
+ idesc.id_func = mkentry;
+ idesc.id_number = parent;
+ idesc.id_parent = ino; /* this is the inode to enter */
+ idesc.id_fix = DONTKNOW;
+ idesc.id_name = name;
+ dp = ginode(parent);
+ if (inosize(dp) % sblock.e2fs_bsize) {
+ inossize(dp, roundup(inosize(dp), sblock.e2fs_bsize));
+ inodirty();
+ }
+ if ((ckinode(dp, &idesc) & ALTERED) != 0)
+ return (1);
+ getpathname(pathbuf, sizeof(pathbuf), parent, parent);
+ dp = ginode(parent);
+ if (expanddir(dp, pathbuf) == 0)
+ return (0);
+ return (ckinode(dp, &idesc) & ALTERED);
+}
+
+/*
+ * Attempt to expand the size of a directory
+ */
+static int
+expanddir(struct ext2fs_dinode *dp, char *name)
+{
+ daddr_t lastbn, newblk;
+ struct bufarea *bp;
+ char *firstblk;
+
+ lastbn = ext2_lblkno(&sblock, inosize(dp));
+ if (lastbn >= EXT2FS_NDADDR - 1 || fs2h32(dp->e2di_blocks[lastbn]) == 0 ||
+ inosize(dp) == 0) {
+ return (0);
+ }
+ if ((newblk = allocblk()) == 0) {
+ return (0);
+ }
+ dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn];
+ dp->e2di_blocks[lastbn] = h2fs32(newblk);
+ inossize(dp, inosize(dp) + sblock.e2fs_bsize);
+ inosnblock(dp, inonblock(dp) + btodb(sblock.e2fs_bsize));
+ bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
+ sblock.e2fs_bsize);
+ if (bp->b_errs)
+ goto bad;
+ if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL)
+ err(8, "cannot allocate first block");
+ memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize);
+ bp = getdirblk(newblk, sblock.e2fs_bsize);
+ if (bp->b_errs) {
+ free(firstblk);
+ goto bad;
+ }
+ memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize);
+ free(firstblk);
+ dirty(bp);
+ bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
+ sblock.e2fs_bsize);
+ if (bp->b_errs)
+ goto bad;
+ emptydir.dot_reclen = h2fs16(sblock.e2fs_bsize);
+ memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
+ pwarn("NO SPACE LEFT IN %s", name);
+ if (preen)
+ printf(" (EXPANDED)\n");
+ else if (reply("EXPAND") == 0)
+ goto bad;
+ dirty(bp);
+ inodirty();
+ return (1);
+bad:
+ dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1];
+ dp->e2di_blocks[lastbn + 1] = 0;
+ inossize(dp, inosize(dp) - sblock.e2fs_bsize);
+ inosnblock(dp, inonblock(dp) - btodb(sblock.e2fs_bsize));
+ freeblk(newblk);
+ return (0);
+}
+
+/*
+ * allocate a new directory
+ */
+int
+allocdir(ino_t parent, ino_t request, int mode)
+{
+ ino_t ino;
+ struct ext2fs_dinode *dp;
+ struct bufarea *bp;
+ struct ext2fs_dirtemplate *dirp;
+
+ ino = allocino(request, IFDIR|mode);
+ dirhead.dot_reclen = h2fs16(12); /* XXX */
+ dirhead.dotdot_reclen = h2fs16(sblock.e2fs_bsize - 12); /* XXX */
+ dirhead.dot_namlen = 1;
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ dirhead.dot_type = EXT2_FT_DIR;
+ else
+ dirhead.dot_type = 0;
+ dirhead.dotdot_namlen = 2;
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ dirhead.dotdot_type = EXT2_FT_DIR;
+ else
+ dirhead.dotdot_type = 0;
+ dirp = &dirhead;
+ dirp->dot_ino = h2fs32(ino);
+ dirp->dotdot_ino = h2fs32(parent);
+ dp = ginode(ino);
+ bp = getdirblk(fs2h32(dp->e2di_blocks[0]), sblock.e2fs_bsize);
+ if (bp->b_errs) {
+ freeino(ino);
+ return (0);
+ }
+ memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate));
+ dirty(bp);
+ dp->e2di_nlink = h2fs16(2);
+ inodirty();
+ if (ino == EXT2_ROOTINO) {
+ lncntp[ino] = fs2h16(dp->e2di_nlink);
+ cacheino(dp, ino);
+ return(ino);
+ }
+ if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
+ freeino(ino);
+ return (0);
+ }
+ cacheino(dp, ino);
+ statemap[ino] = statemap[parent];
+ if (statemap[ino] == DSTATE) {
+ lncntp[ino] = fs2h16(dp->e2di_nlink);
+ lncntp[parent]++;
+ }
+ dp = ginode(parent);
+ dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1);
+ inodirty();
+ return (ino);
+}
+
+/*
+ * free a directory inode
+ */
+static void
+freedir(ino_t ino, ino_t parent)
+{
+ struct ext2fs_dinode *dp;
+
+ if (ino != parent) {
+ dp = ginode(parent);
+ dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - 1);
+ inodirty();
+ }
+ freeino(ino);
+}
+
+/*
+ * generate a temporary name for the lost+found directory.
+ */
+static int
+lftempname(char *bufp, ino_t ino)
+{
+ ino_t in;
+ char *cp;
+ int namlen;
+
+ cp = bufp + 2;
+ for (in = maxino; in > 0; in /= 10)
+ cp++;
+ *--cp = 0;
+ namlen = cp - bufp;
+ in = ino;
+ while (cp > bufp) {
+ *--cp = (in % 10) + '0';
+ in /= 10;
+ }
+ *cp = '#';
+ return (namlen);
+}
+
+/*
+ * Get a directory block.
+ * Insure that it is held until another is requested.
+ */
+static struct bufarea *
+getdirblk(daddr_t blkno, long size)
+{
+
+ if (pdirbp != 0)
+ pdirbp->b_flags &= ~B_INUSE;
+ pdirbp = getdatablk(blkno, size);
+ return (pdirbp);
+}
diff --git a/cmd/extra/fsck.ext4/exitvalues.h b/cmd/extra/fsck.ext4/exitvalues.h
@@ -0,0 +1,35 @@
+/* $NetBSD: exitvalues.h,v 1.3 2015/06/21 03:33:22 dholland Exp $ */
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define FSCK_EXIT_OK 0
+#define FSCK_EXIT_UNRESOLVED 4
+#define FSCK_EXIT_CHECK_FAILED 8
+#define FSCK_EXIT_USAGE 16
+#define FSCK_EXIT_SIGNALLED 32
diff --git a/cmd/extra/fsck.ext4/ext2fs_bswap.c b/cmd/extra/fsck.ext4/ext2fs_bswap.c
@@ -0,0 +1,138 @@
+/* $NetBSD: ext2fs_bswap.c,v 1.26 2025/12/24 15:06:59 nia Exp $ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_bswap.c,v 1.26 2025/12/24 15:06:59 nia Exp $");
+
+#include <sys/types.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+
+#if defined(_KERNEL)
+#include <sys/systm.h>
+#else
+#include <string.h>
+#endif
+
+/* These functions are only needed if native byte order is not little endian */
+#if BYTE_ORDER == BIG_ENDIAN
+void
+e2fs_sb_bswap(struct ext2fs *old, struct ext2fs *new)
+{
+
+ /* preserve unused fields */
+ memcpy(new, old, sizeof(struct ext2fs));
+ new->e2fs_icount = bswap32(old->e2fs_icount);
+ new->e2fs_bcount = bswap32(old->e2fs_bcount);
+ new->e2fs_rbcount = bswap32(old->e2fs_rbcount);
+ new->e2fs_fbcount = bswap32(old->e2fs_fbcount);
+ new->e2fs_ficount = bswap32(old->e2fs_ficount);
+ new->e2fs_first_dblock = bswap32(old->e2fs_first_dblock);
+ new->e2fs_log_bsize = bswap32(old->e2fs_log_bsize);
+ new->e2fs_fsize = bswap32(old->e2fs_fsize);
+ new->e2fs_bpg = bswap32(old->e2fs_bpg);
+ new->e2fs_fpg = bswap32(old->e2fs_fpg);
+ new->e2fs_ipg = bswap32(old->e2fs_ipg);
+ new->e2fs_mtime = bswap32(old->e2fs_mtime);
+ new->e2fs_wtime = bswap32(old->e2fs_wtime);
+ new->e2fs_mnt_count = bswap16(old->e2fs_mnt_count);
+ new->e2fs_max_mnt_count = bswap16(old->e2fs_max_mnt_count);
+ new->e2fs_magic = bswap16(old->e2fs_magic);
+ new->e2fs_state = bswap16(old->e2fs_state);
+ new->e2fs_beh = bswap16(old->e2fs_beh);
+ new->e2fs_minrev = bswap16(old->e2fs_minrev);
+ new->e2fs_lastfsck = bswap32(old->e2fs_lastfsck);
+ new->e2fs_fsckintv = bswap32(old->e2fs_fsckintv);
+ new->e2fs_creator = bswap32(old->e2fs_creator);
+ new->e2fs_rev = bswap32(old->e2fs_rev);
+ new->e2fs_ruid = bswap16(old->e2fs_ruid);
+ new->e2fs_rgid = bswap16(old->e2fs_rgid);
+ new->e2fs_first_ino = bswap32(old->e2fs_first_ino);
+ new->e2fs_inode_size = bswap16(old->e2fs_inode_size);
+ new->e2fs_block_group_nr = bswap16(old->e2fs_block_group_nr);
+ new->e2fs_features_compat = bswap32(old->e2fs_features_compat);
+ new->e2fs_features_incompat = bswap32(old->e2fs_features_incompat);
+ new->e2fs_features_rocompat = bswap32(old->e2fs_features_rocompat);
+ new->e2fs_algo = bswap32(old->e2fs_algo);
+ new->e2fs_reserved_ngdb = bswap16(old->e2fs_reserved_ngdb);
+ new->e4fs_want_extra_isize = bswap16(old->e4fs_want_extra_isize);
+}
+
+void
+e2fs_i_bswap(struct ext2fs_dinode *old, struct ext2fs_dinode *new, size_t isize)
+{
+ /* preserve non-swapped and unused fields */
+ memcpy(new, old, isize);
+
+ /* swap what needs to be swapped */
+ new->e2di_mode = bswap16(old->e2di_mode);
+ new->e2di_uid = bswap16(old->e2di_uid);
+ new->e2di_gid = bswap16(old->e2di_gid);
+ new->e2di_nlink = bswap16(old->e2di_nlink);
+ new->e2di_size = bswap32(old->e2di_size);
+ new->e2di_atime = bswap32(old->e2di_atime);
+ new->e2di_ctime = bswap32(old->e2di_ctime);
+ new->e2di_mtime = bswap32(old->e2di_mtime);
+ new->e2di_dtime = bswap32(old->e2di_dtime);
+ new->e2di_nblock = bswap32(old->e2di_nblock);
+ new->e2di_flags = bswap32(old->e2di_flags);
+ new->e2di_version = bswap32(old->e2di_version);
+ new->e2di_gen = bswap32(old->e2di_gen);
+ new->e2di_facl = bswap32(old->e2di_facl);
+ new->e2di_size_high = bswap32(old->e2di_size_high);
+ new->e2di_nblock_high = bswap16(old->e2di_nblock_high);
+ new->e2di_facl_high = bswap16(old->e2di_facl_high);
+ new->e2di_uid_high = bswap16(old->e2di_uid_high);
+ new->e2di_gid_high = bswap16(old->e2di_gid_high);
+ new->e2di_checksum_low = bswap16(old->e2di_checksum_low);
+
+ /*
+ * Following fields are only supported for inode sizes bigger
+ * than the old ext2 one
+ */
+ if (isize == EXT2_REV0_DINODE_SIZE)
+ return;
+
+ new->e2di_extra_isize = bswap16(old->e2di_extra_isize);
+ new->e2di_checksum_high = bswap16(old->e2di_checksum_high);
+
+ /* Following fields are ext4, might not be actually present */
+ if (EXT2_DINODE_FITS(new, e2di_ctime_extra, isize))
+ new->e2di_ctime_extra = bswap32(old->e2di_ctime_extra);
+ if (EXT2_DINODE_FITS(new, e2di_mtime_extra, isize))
+ new->e2di_mtime_extra = bswap32(old->e2di_mtime_extra);
+ if (EXT2_DINODE_FITS(new, e2di_atime_extra, isize))
+ new->e2di_atime_extra = bswap32(old->e2di_atime_extra);
+ if (EXT2_DINODE_FITS(new, e2di_crtime, isize))
+ new->e2di_crtime = bswap32(old->e2di_crtime);
+ if (EXT2_DINODE_FITS(new, e2di_crtime_extra, isize))
+ new->e2di_crtime_extra = bswap32(old->e2di_crtime_extra);
+ if (EXT2_DINODE_FITS(new, e2di_version_high, isize))
+ new->e2di_version_high = bswap32(old->e2di_version_high);
+ if (EXT2_DINODE_FITS(new, e2di_projid, isize))
+ new->e2di_projid = bswap32(old->e2di_projid);
+}
+#endif
diff --git a/cmd/extra/fsck.ext4/extern.h b/cmd/extra/fsck.ext4/extern.h
@@ -0,0 +1,75 @@
+/* $NetBSD: extern.h,v 1.8 2012/11/25 19:42:14 jakllsch Exp $ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ * Copyright (c) 1994 James A. Jegers
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+void adjust(struct inodesc *, short);
+int allocblk(void);
+int allocdir(ino_t, ino_t, int);
+void blkerror(ino_t, const char *, daddr_t);
+int bread(int, char *, daddr_t, long);
+void bufinit(void);
+void bwrite(int, char *, daddr_t, long);
+void cacheino(struct ext2fs_dinode *, ino_t);
+int changeino(ino_t, const char *, ino_t);
+int chkrange(daddr_t, int);
+void ckfini(int);
+int ckinode(struct ext2fs_dinode *, struct inodesc *);
+void clri(struct inodesc *, const char *, int);
+int dircheck(struct inodesc *, struct ext2fs_direct *);
+void direrror(ino_t, const char *);
+int dirscan(struct inodesc *);
+int dofix(struct inodesc *, const char *);
+void fileerror(ino_t, ino_t, const char *);
+int findino(struct inodesc *);
+int findname(struct inodesc *);
+void flush(int, struct bufarea *);
+void freeblk(daddr_t);
+void freeino(ino_t);
+void freeinodebuf(void);
+int ftypeok(struct ext2fs_dinode *);
+void getpathname(char *, size_t, ino_t, ino_t);
+void inocleanup(void);
+void inodirty(void);
+u_int64_t inosize(struct ext2fs_dinode *);
+void inossize(struct ext2fs_dinode *, u_int64_t);
+int linkup(ino_t, ino_t);
+int makeentry(ino_t, ino_t, const char *);
+void pass1(void);
+void pass1b(void);
+void pass2(void);
+void pass3(void);
+void pass4(void);
+int pass1check(struct inodesc *);
+int pass4check(struct inodesc *);
+void pass5(void);
+void pinode(ino_t);
+void propagate(void);
+int reply(const char *);
+void resetinodebuf(void);
+int setup(const char *);
+struct ext2fs_dinode * getnextinode(ino_t);
+uint64_t inonblock(struct ext2fs_dinode *);
+void inosnblock(struct ext2fs_dinode *, uint64_t);
diff --git a/cmd/extra/fsck.ext4/fsck.h b/cmd/extra/fsck.ext4/fsck.h
@@ -0,0 +1,249 @@
+/* $NetBSD: fsck.h,v 1.17 2020/04/05 15:25:39 joerg Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)fsck.h 8.1 (Berkeley) 6/5/93
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)fsck.h 8.1 (Berkeley) 6/5/93
+ */
+
+#define MAXDUP 10 /* limit on dup blks (per inode) */
+#define MAXBAD 10 /* limit on bad blks (per inode) */
+#define MAXBUFSPACE 80*1024 /* maximum space to allocate to buffers */
+#define INOBUFSIZE 128*1024 /* size of buffer to read inodes in pass1 */
+
+#ifndef BUFSIZ
+#define BUFSIZ 1024
+#endif
+
+#define USTATE 01 /* inode not allocated */
+#define FSTATE 02 /* inode is file */
+#define DSTATE 03 /* inode is directory */
+#define DFOUND 04 /* directory found during descent */
+#define DCLEAR 05 /* directory is to be cleared */
+#define FCLEAR 06 /* file is to be cleared */
+
+/*
+ * buffer cache structure.
+ */
+struct bufarea {
+ struct bufarea *b_next; /* free list queue */
+ struct bufarea *b_prev; /* free list queue */
+ daddr_t b_bno;
+ int b_size;
+ int b_errs;
+ int b_flags;
+ union {
+ char *b_buf; /* buffer space */
+ /* XXX ondisk32 */
+ int32_t *b_indir; /* indirect block */
+ struct ext2fs *b_fs; /* super block */
+ struct ext2_gd *b_cgd; /* cylinder group descriptor */
+ struct ext2fs_dinode *b_dinode; /* inode block */
+ } b_un;
+ char b_dirty;
+};
+
+#define B_INUSE 1
+
+#define MINBUFS 5 /* minimum number of buffers required */
+extern struct bufarea bufhead; /* head of list of other blks in filesys */
+extern struct bufarea sblk; /* file system superblock */
+extern struct bufarea asblk; /* first alternate superblock */
+extern struct bufarea *pdirbp; /* current directory contents */
+extern struct bufarea *pbp; /* current inode block */
+extern struct bufarea *getdatablk(daddr_t, long);
+extern struct m_ext2fs sblock;
+
+#define dirty(bp) (bp)->b_dirty = 1
+#define initbarea(bp) \
+ (bp)->b_dirty = 0; \
+ (bp)->b_bno = (daddr_t)-1; \
+ (bp)->b_flags = 0;
+
+#define sbdirty() copyback_sb(&sblk); sblk.b_dirty = 1
+
+enum fixstate {DONTKNOW, NOFIX, FIX, IGNORE};
+
+struct inodesc {
+ enum fixstate id_fix; /* policy on fixing errors */
+ int (*id_func) /* function to be applied to blocks of inode */
+ (struct inodesc *);
+ ino_t id_number; /* inode number described */
+ ino_t id_parent; /* for DATA nodes, their parent */
+ daddr_t id_blkno; /* current block number being examined */
+ int id_numfrags; /* number of frags contained in block */
+ quad_t id_filesize; /* for DATA nodes, the size of the directory */
+ int id_loc; /* for DATA nodes, current location in dir */
+ int id_entryno; /* for DATA nodes, current entry number */
+ struct ext2fs_direct *id_dirp; /* for DATA nodes, ptr to current entry */
+ const char *id_name; /* for DATA nodes, name to find or enter */
+ char id_type; /* type of descriptor, DATA or ADDR */
+};
+/* file types */
+#define DATA 1
+#define ADDR 2
+
+/*
+ * Linked list of duplicate blocks.
+ *
+ * The list is composed of two parts. The first part of the
+ * list (from duplist through the node pointed to by muldup)
+ * contains a single copy of each duplicate block that has been
+ * found. The second part of the list (from muldup to the end)
+ * contains duplicate blocks that have been found more than once.
+ * To check if a block has been found as a duplicate it is only
+ * necessary to search from duplist through muldup. To find the
+ * total number of times that a block has been found as a duplicate
+ * the entire list must be searched for occurrences of the block
+ * in question. The following diagram shows a sample list where
+ * w (found twice), x (found once), y (found three times), and z
+ * (found once) are duplicate block numbers:
+ *
+ * w -> y -> x -> z -> y -> w -> y
+ * ^ ^
+ * | |
+ * duplist muldup
+ */
+struct dups {
+ struct dups *next;
+ daddr_t dup;
+};
+extern struct dups *duplist; /* head of dup list */
+extern struct dups *muldup; /* end of unique duplicate dup block numbers */
+
+/*
+ * Linked list of inodes with zero link counts.
+ */
+struct zlncnt {
+ struct zlncnt *next;
+ ino_t zlncnt;
+};
+extern struct zlncnt *zlnhead; /* head of zero link count list */
+
+/*
+ * Inode cache data structures.
+ */
+extern struct inoinfo {
+ struct inoinfo *i_nexthash; /* next entry in hash chain */
+ struct inoinfo *i_child, *i_sibling, *i_parentp;
+ ino_t i_number; /* inode number of this entry */
+ ino_t i_parent; /* inode number of parent */
+ ino_t i_dotdot; /* inode number of `..' */
+ u_int64_t i_isize; /* size of inode */
+ u_int i_numblks; /* size of block array in bytes */
+ /* XXX ondisk32 */
+ int32_t i_blks[1]; /* actually longer */
+} **inphead, **inpsort;
+extern long numdirs, listmax, inplast;
+
+extern long dev_bsize; /* computed value of DEV_BSIZE */
+extern long secsize; /* actual disk sector size */
+extern char nflag; /* assume a no response */
+extern char yflag; /* assume a yes response */
+extern int bflag; /* location of alternate super block */
+extern int Uflag; /* resolve user names */
+extern int debug; /* output debugging info */
+extern int preen; /* just fix normal inconsistencies */
+extern char havesb; /* superblock has been read */
+extern char skipclean; /* skip clean file systems if preening */
+extern int fsmodified; /* 1 => write done to file system */
+extern int fsreadfd; /* file descriptor for reading file system */
+extern int fswritefd; /* file descriptor for writing file system */
+extern int rerun; /* rerun fsck. Only used in non-preen mode */
+
+extern daddr_t maxfsblock; /* number of blocks in the file system */
+extern char *blockmap; /* ptr to primary blk allocation map */
+extern ino_t maxino; /* number of inodes in file system */
+extern ino_t lastino; /* last inode in use */
+extern char *statemap; /* ptr to inode state table */
+extern u_char *typemap; /* ptr to inode type table */
+extern int16_t *lncntp; /* ptr to link count table */
+
+extern ino_t lfdir; /* lost & found directory inode number */
+extern const char *lfname; /* lost & found directory name */
+extern int lfmode; /* lost & found directory creation mode */
+
+extern daddr_t n_blks; /* number of blocks in use */
+extern daddr_t n_files; /* number of files in use */
+
+#define clearinode(dp) (*(dp) = zino)
+extern struct ext2fs_dinode zino;
+
+#define setbmap(blkno) setbit(blockmap, blkno)
+#define testbmap(blkno) isset(blockmap, blkno)
+#define clrbmap(blkno) clrbit(blockmap, blkno)
+
+#define STOP 0x01
+#define SKIP 0x02
+#define KEEPON 0x04
+#define ALTERED 0x08
+#define FOUND 0x10
+
+/*
+ * Kernel support for features doesn't imply fsck support
+ */
+#define EXT2F_COMPAT_SUPP_FSCK (EXT2F_COMPAT_HASJOURNAL | EXT2F_COMPAT_EXTATTR)
+#define EXT2F_ROCOMPAT_SUPP_FSCK (EXT2F_ROCOMPAT_SPARSESUPER \
+ | EXT2F_ROCOMPAT_LARGEFILE \
+ | EXT2F_ROCOMPAT_EXTRA_ISIZE | EXT2F_ROCOMPAT_HUGE_FILE \
+ )
+#define EXT2F_INCOMPAT_SUPP_FSCK (EXT2F_INCOMPAT_FTYPE \
+ )
+
+struct ext2fs_dinode *ginode(ino_t);
+struct inoinfo *getinoinfo(ino_t);
+void getblk(struct bufarea *, daddr_t, long);
+ino_t allocino(ino_t, int);
+void copyback_sb(struct bufarea*);
+daddr_t cgoverhead(int); /* overhead per cg */
diff --git a/cmd/extra/fsck.ext4/fsutil.c b/cmd/extra/fsck.ext4/fsutil.c
@@ -0,0 +1,237 @@
+/* $NetBSD: fsutil.c,v 1.27 2020/04/03 19:36:32 joerg Exp $ */
+
+/*
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+__RCSID("$NetBSD: fsutil.c,v 1.27 2020/04/03 19:36:32 joerg Exp $");
+#endif /* not lint */
+
+/*
+ * used by sbin/fsck
+ * used by sbin/fsck_ext2fs
+ * used by sbin/fsck_ffs
+ * used by sbin/fsck_lfs
+ * used by sbin/fsck_msdos
+ * used by sbin/fsck_v7fs
+ * used by sbin/fsdb
+ * used by usr.sbin/quotacheck
+ */
+
+#include <sys/param.h>
+
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <err.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "fsutil.h"
+#include "exitvalues.h"
+
+extern int unresolved;
+
+volatile sig_atomic_t returntosingle;
+
+void (*ckfinish)(int);
+
+static const char *dev = NULL;
+static int hot = 0;
+static int preen = 0;
+int quiet; /* don't report clean filesystems */
+#define F_ERROR 0x80000000
+
+void
+setcdevname(const char *cd, int pr)
+{
+
+ dev = cd;
+ preen = pr;
+}
+
+const char *
+cdevname(void)
+{
+
+ return dev;
+}
+
+int
+hotroot(void)
+{
+
+ return hot;
+}
+
+/*VARARGS*/
+void
+errexit(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ (void) vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ (void)fprintf(stderr, "\n");
+ exit(FSCK_EXIT_CHECK_FAILED);
+}
+
+void
+vmsg(int fatal, const char *fmt, va_list ap)
+{
+ int serr = fatal & F_ERROR;
+ int serrno = errno;
+ fatal &= ~F_ERROR;
+
+ if (!fatal && preen)
+ (void)printf("%s: ", dev);
+ if (quiet && !preen) {
+ (void)printf("** %s (vmsg)\n", dev);
+ quiet = 0;
+ }
+
+ (void) vprintf(fmt, ap);
+ if (serr)
+ printf(" (%s)", strerror(serrno));
+
+ if (fatal && preen)
+ (void) printf("\n");
+
+ if (fatal && preen) {
+ (void) printf(
+ "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
+ dev, getprogname());
+ exit(FSCK_EXIT_CHECK_FAILED);
+ }
+}
+
+/*VARARGS*/
+void
+pfatal(const char *fmt, ...)
+{
+ va_list ap;
+
+ unresolved = 1;
+
+ va_start(ap, fmt);
+ vmsg(1, fmt, ap);
+ va_end(ap);
+}
+
+/*VARARGS*/
+void
+pwarn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(0, fmt, ap);
+ va_end(ap);
+}
+
+void
+perr(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(1 | F_ERROR, fmt, ap);
+ va_end(ap);
+}
+
+void
+panic(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(1, fmt, ap);
+ va_end(ap);
+ exit(FSCK_EXIT_CHECK_FAILED);
+}
+
+const char *
+print_mtime(time_t t)
+{
+ static char b[128];
+ char *p = ctime(&t);
+ if (p != NULL)
+ (void)snprintf(b, sizeof(b), "%12.12s %4.4s ", &p[4], &p[20]);
+ else
+ (void)snprintf(b, sizeof(b), "%lld ", (long long)t);
+ return b;
+}
+
+
+void
+catch(int n)
+{
+ if (ckfinish) (*ckfinish)(0);
+ _exit(FSCK_EXIT_SIGNALLED);
+}
+
+/*
+ * When preening, allow a single quit to signal
+ * a special exit after filesystem checks complete
+ * so that reboot sequence may be interrupted.
+ */
+void
+catchquit(int n)
+{
+ static const char msg[] =
+ "returning to single-user after filesystem check\n";
+ int serrno = errno;
+
+ (void)write(STDOUT_FILENO, msg, sizeof(msg) - 1);
+ returntosingle = 1;
+ (void)signal(SIGQUIT, SIG_DFL);
+ errno = serrno;
+}
+
+/*
+ * Ignore a single quit signal; wait and flush just in case.
+ * Used by child processes in preen.
+ */
+void
+voidquit(int n)
+{
+ int serrno = errno;
+
+ sleep(1);
+ (void)signal(SIGQUIT, SIG_IGN);
+ (void)signal(SIGQUIT, SIG_DFL);
+ errno = serrno;
+}
diff --git a/cmd/extra/fsck.ext4/fsutil.h b/cmd/extra/fsck.ext4/fsutil.h
@@ -0,0 +1,59 @@
+/* $NetBSD: fsutil.h,v 1.21 2020/04/03 19:36:32 joerg Exp $ */
+
+/*
+ * Copyright (c) 1996 Christos Zoulas. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdarg.h>
+#include <signal.h>
+
+void errexit(const char *, ...) __printflike(1, 2) __dead;
+void pfatal(const char *, ...) __printflike(1, 2);
+void pwarn(const char *, ...) __printflike(1, 2);
+void perr(const char *, ...) __printflike(1, 2);
+void panic(const char *, ...) __printflike(1, 2) __dead;
+void vmsg(int, const char *, va_list) __printflike(2, 0);
+const char *blockcheck(const char *);
+const char *cdevname(void);
+void setcdevname(const char *, int);
+int hotroot(void);
+const char *print_mtime(time_t);
+
+#define CHECK_PREEN 1
+#define CHECK_VERBOSE 2
+#define CHECK_DEBUG 4
+#define CHECK_FORCE 8
+#define CHECK_PROGRESS 16
+#define CHECK_NOFIX 32
+
+struct fstab;
+int checkfstab(int, int, void *(*)(struct fstab *),
+ int (*) (const char *, const char *, const char *, void *, pid_t *));
+
+extern void (*ckfinish)(int);
+extern volatile sig_atomic_t returntosingle;
+void catch(int) __dead;
+void catchquit(int);
+void voidquit(int);
+
+extern int quiet;
diff --git a/cmd/extra/fsck.ext4/inode.c b/cmd/extra/fsck.ext4/inode.c
@@ -0,0 +1,810 @@
+/* $NetBSD: inode.c,v 1.37 2016/08/04 17:43:47 jdolecek Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95";
+#else
+__RCSID("$NetBSD: inode.c,v 1.37 2016/08/04 17:43:47 jdolecek Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs.h>
+
+#ifndef SMALL
+#include <pwd.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "fsck.h"
+#include "fsutil.h"
+#include "extern.h"
+
+/*
+ * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
+ * here.
+ */
+
+#define fsck_ino_to_fsba(fs, x) \
+ (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
+ (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
+
+
+static ino_t startinum;
+
+static int iblock(struct inodesc *, long, u_int64_t);
+
+static int setlarge(void);
+
+static int sethuge(void);
+
+static int
+setlarge(void)
+{
+ if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
+ pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
+ return 0;
+ }
+ if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
+ if (preen)
+ pwarn("SETTING LARGE FILE INDICATOR\n");
+ else if (!reply("SET LARGE FILE INDICATOR"))
+ return 0;
+ sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
+ sbdirty();
+ }
+ return 1;
+}
+
+static int
+sethuge(void)
+{
+ if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
+ pfatal("HUGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
+ return 0;
+ }
+ if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
+ if (preen)
+ pwarn("SETTING HUGE FILE FEATURE\n");
+ else if (!reply("SET HUGE FILE FEATURE"))
+ return 0;
+ sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_HUGE_FILE;
+ sbdirty();
+ }
+ return 1;
+}
+
+u_int64_t
+inosize(struct ext2fs_dinode *dp)
+{
+ u_int64_t size = fs2h32(dp->e2di_size);
+
+ if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
+ size |= (u_int64_t)fs2h32(dp->e2di_size_high) << 32;
+ if (size > INT32_MAX)
+ (void)setlarge();
+ return size;
+}
+
+void
+inossize(struct ext2fs_dinode *dp, u_int64_t size)
+{
+ if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
+ dp->e2di_size_high = h2fs32(size >> 32);
+ if (size > INT32_MAX)
+ if (!setlarge())
+ return;
+ } else if (size > INT32_MAX) {
+ pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
+ (unsigned long long)size, fs2h16(dp->e2di_mode) & IFMT);
+ return;
+ }
+ dp->e2di_size = h2fs32(size);
+}
+
+int
+ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
+{
+ u_int32_t *ap;
+ long ret, n, ndb;
+ struct ext2fs_dinode dino;
+ u_int64_t remsize, sizepb;
+ mode_t mode;
+ char pathbuf[MAXPATHLEN + 1];
+
+ if (idesc->id_fix != IGNORE)
+ idesc->id_fix = DONTKNOW;
+ idesc->id_entryno = 0;
+ idesc->id_filesize = inosize(dp);
+ mode = fs2h16(dp->e2di_mode) & IFMT;
+ if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
+ (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
+ return (KEEPON);
+ dino = *dp;
+ ndb = howmany(inosize(&dino), sblock.e2fs_bsize);
+ for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[EXT2FS_NDADDR];
+ ap++,ndb--) {
+ idesc->id_numfrags = 1;
+ if (*ap == 0) {
+ if (idesc->id_type == DATA && ndb > 0) {
+ /* An empty block in a directory XXX */
+ getpathname(pathbuf, sizeof(pathbuf),
+ idesc->id_number, idesc->id_number);
+ pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
+ pathbuf);
+ if (reply("ADJUST LENGTH") == 1) {
+ dp = ginode(idesc->id_number);
+ inossize(dp,
+ (ap - &dino.e2di_blocks[0]) *
+ sblock.e2fs_bsize);
+ printf(
+ "YOU MUST RERUN FSCK AFTERWARDS\n");
+ rerun = 1;
+ inodirty();
+ }
+ }
+ continue;
+ }
+ idesc->id_blkno = fs2h32(*ap);
+ if (idesc->id_type == ADDR)
+ ret = (*idesc->id_func)(idesc);
+ else
+ ret = dirscan(idesc);
+ if (ret & STOP)
+ return (ret);
+ }
+ idesc->id_numfrags = 1;
+ remsize = inosize(&dino) - sblock.e2fs_bsize * EXT2FS_NDADDR;
+ sizepb = sblock.e2fs_bsize;
+ for (ap = &dino.e2di_blocks[EXT2FS_NDADDR], n = 1; n <= EXT2FS_NIADDR; ap++, n++) {
+ if (*ap) {
+ idesc->id_blkno = fs2h32(*ap);
+ ret = iblock(idesc, n, remsize);
+ if (ret & STOP)
+ return (ret);
+ } else {
+ if (idesc->id_type == DATA && remsize > 0) {
+ /* An empty block in a directory XXX */
+ getpathname(pathbuf, sizeof(pathbuf),
+ idesc->id_number, idesc->id_number);
+ pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
+ pathbuf);
+ if (reply("ADJUST LENGTH") == 1) {
+ dp = ginode(idesc->id_number);
+ inossize(dp, inosize(dp) - remsize);
+ remsize = 0;
+ printf(
+ "YOU MUST RERUN FSCK AFTERWARDS\n");
+ rerun = 1;
+ inodirty();
+ break;
+ }
+ }
+ }
+ sizepb *= EXT2_NINDIR(&sblock);
+ remsize -= sizepb;
+ }
+ return (KEEPON);
+}
+
+static int
+iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
+{
+ /* XXX ondisk32 */
+ int32_t *ap;
+ int32_t *aplim;
+ struct bufarea *bp;
+ int i, n, (*func)(struct inodesc *);
+ size_t nif;
+ u_int64_t sizepb;
+ char buf[BUFSIZ];
+ char pathbuf[MAXPATHLEN + 1];
+ struct ext2fs_dinode *dp;
+
+ if (idesc->id_type == ADDR) {
+ func = idesc->id_func;
+ if (((n = (*func)(idesc)) & KEEPON) == 0)
+ return (n);
+ } else
+ func = dirscan;
+ if (chkrange(idesc->id_blkno, idesc->id_numfrags))
+ return (SKIP);
+ bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
+ ilevel--;
+ for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
+ sizepb *= EXT2_NINDIR(&sblock);
+ if (isize > sizepb * EXT2_NINDIR(&sblock))
+ nif = EXT2_NINDIR(&sblock);
+ else
+ nif = howmany(isize, sizepb);
+ if (idesc->id_func == pass1check &&
+ nif < EXT2_NINDIR(&sblock)) {
+ aplim = &bp->b_un.b_indir[EXT2_NINDIR(&sblock)];
+ for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
+ if (*ap == 0)
+ continue;
+ (void)snprintf(buf, sizeof(buf),
+ "PARTIALLY TRUNCATED INODE I=%llu",
+ (unsigned long long)idesc->id_number);
+ if (dofix(idesc, buf)) {
+ *ap = 0;
+ dirty(bp);
+ }
+ }
+ flush(fswritefd, bp);
+ }
+ aplim = &bp->b_un.b_indir[nif];
+ for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
+ if (*ap) {
+ idesc->id_blkno = fs2h32(*ap);
+ if (ilevel == 0)
+ n = (*func)(idesc);
+ else
+ n = iblock(idesc, ilevel, isize);
+ if (n & STOP) {
+ bp->b_flags &= ~B_INUSE;
+ return (n);
+ }
+ } else {
+ if (idesc->id_type == DATA && isize > 0) {
+ /* An empty block in a directory XXX */
+ getpathname(pathbuf, sizeof(pathbuf),
+ idesc->id_number, idesc->id_number);
+ pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
+ pathbuf);
+ if (reply("ADJUST LENGTH") == 1) {
+ dp = ginode(idesc->id_number);
+ inossize(dp, inosize(dp) - isize);
+ isize = 0;
+ printf(
+ "YOU MUST RERUN FSCK AFTERWARDS\n");
+ rerun = 1;
+ inodirty();
+ bp->b_flags &= ~B_INUSE;
+ return(STOP);
+ }
+ }
+ }
+ isize -= sizepb;
+ }
+ bp->b_flags &= ~B_INUSE;
+ return (KEEPON);
+}
+
+/*
+ * Check that a block in a legal block number.
+ * Return 0 if in range, 1 if out of range.
+ */
+int
+chkrange(daddr_t blk, int cnt)
+{
+ int c, overh;
+
+ if ((unsigned int)(blk + cnt) > maxfsblock)
+ return (1);
+ c = dtog(&sblock, blk);
+ overh = cgoverhead(c);
+ if (blk < sblock.e2fs.e2fs_bpg * c + overh +
+ sblock.e2fs.e2fs_first_dblock) {
+ if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
+ sblock.e2fs.e2fs_first_dblock) {
+ if (debug) {
+ printf("blk %lld < cgdmin %d;",
+ (long long)blk,
+ sblock.e2fs.e2fs_bpg * c + overh +
+ sblock.e2fs.e2fs_first_dblock);
+ printf(" blk + cnt %lld > cgsbase %d\n",
+ (long long)(blk + cnt),
+ sblock.e2fs.e2fs_bpg * c +
+ overh + sblock.e2fs.e2fs_first_dblock);
+ }
+ return (1);
+ }
+ } else {
+ if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
+ sblock.e2fs.e2fs_first_dblock) {
+ if (debug) {
+ printf("blk %lld >= cgdmin %d;",
+ (long long)blk,
+ sblock.e2fs.e2fs_bpg * c + overh +
+ sblock.e2fs.e2fs_first_dblock);
+ printf(" blk + cnt %lld > cgdmax %d\n",
+ (long long)(blk+cnt),
+ sblock.e2fs.e2fs_bpg * (c + 1) +
+ overh + sblock.e2fs.e2fs_first_dblock);
+ }
+ return (1);
+ }
+ }
+ return (0);
+}
+
+/*
+ * General purpose interface for reading inodes.
+ */
+struct ext2fs_dinode *
+ginode(ino_t inumber)
+{
+ daddr_t iblk;
+ struct ext2fs_dinode *dp;
+
+ if ((inumber < EXT2_FIRSTINO &&
+ inumber != EXT2_ROOTINO &&
+ !(inumber == EXT2_RESIZEINO &&
+ (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
+ || inumber > maxino)
+ errexit("bad inode number %llu to ginode",
+ (unsigned long long)inumber);
+ if (startinum == 0 ||
+ inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
+ iblk = fsck_ino_to_fsba(&sblock, inumber);
+ if (pbp != 0)
+ pbp->b_flags &= ~B_INUSE;
+ pbp = getdatablk(iblk, sblock.e2fs_bsize);
+ startinum =
+ ((inumber - 1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
+ }
+ dp = (struct ext2fs_dinode *)(pbp->b_un.b_buf +
+ EXT2_DINODE_SIZE(&sblock) * ino_to_fsbo(&sblock, inumber));
+
+ return dp;
+}
+
+/*
+ * Special purpose version of ginode used to optimize first pass
+ * over all the inodes in numerical order.
+ */
+ino_t nextino, lastinum;
+long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
+char *inodebuf;
+
+struct ext2fs_dinode *
+getnextinode(ino_t inumber)
+{
+ long size;
+ daddr_t dblk;
+ struct ext2fs_dinode *dp;
+ static char *bp;
+
+ if (inumber != nextino++ || inumber > maxino)
+ errexit("bad inode number %llu to nextinode",
+ (unsigned long long)inumber);
+ if (inumber >= lastinum) {
+ readcnt++;
+ dblk = EXT2_FSBTODB(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
+ if (readcnt % readpercg == 0) {
+ size = partialsize;
+ lastinum += partialcnt;
+ } else {
+ size = inobufsize;
+ lastinum += fullcnt;
+ }
+ (void)bread(fsreadfd, inodebuf, dblk, size);
+ bp = inodebuf;
+ }
+ dp = (struct ext2fs_dinode *)bp;
+ bp += EXT2_DINODE_SIZE(&sblock);
+
+ return dp;
+}
+
+void
+resetinodebuf(void)
+{
+
+ startinum = 0;
+ nextino = 1;
+ lastinum = 1;
+ readcnt = 0;
+ inobufsize = ext2_blkroundup(&sblock, INOBUFSIZE);
+ fullcnt = inobufsize / EXT2_DINODE_SIZE(&sblock);
+ readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
+ partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
+ partialsize = partialcnt * EXT2_DINODE_SIZE(&sblock);
+ if (partialcnt != 0) {
+ readpercg++;
+ } else {
+ partialcnt = fullcnt;
+ partialsize = inobufsize;
+ }
+ if (inodebuf == NULL &&
+ (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
+ errexit("Cannot allocate space for inode buffer");
+ while (nextino < EXT2_ROOTINO)
+ (void)getnextinode(nextino);
+}
+
+void
+freeinodebuf(void)
+{
+
+ if (inodebuf != NULL)
+ free(inodebuf);
+ inodebuf = NULL;
+}
+
+/*
+ * Routines to maintain information about directory inodes.
+ * This is built during the first pass and used during the
+ * second and third passes.
+ *
+ * Enter inodes into the cache.
+ */
+void
+cacheino(struct ext2fs_dinode *dp, ino_t inumber)
+{
+ struct inoinfo *inp;
+ struct inoinfo **inpp;
+ unsigned int blks;
+
+ blks = howmany(inosize(dp), sblock.e2fs_bsize);
+ if (blks > EXT2FS_NDADDR)
+ blks = EXT2FS_NDADDR + EXT2FS_NIADDR;
+ /* XXX ondisk32 */
+ inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
+ if (inp == NULL)
+ return;
+ inpp = &inphead[inumber % numdirs];
+ inp->i_nexthash = *inpp;
+ *inpp = inp;
+ inp->i_child = inp->i_sibling = inp->i_parentp = 0;
+ if (inumber == EXT2_ROOTINO)
+ inp->i_parent = EXT2_ROOTINO;
+ else
+ inp->i_parent = (ino_t)0;
+ inp->i_dotdot = (ino_t)0;
+ inp->i_number = inumber;
+ inp->i_isize = inosize(dp);
+ /* XXX ondisk32 */
+ inp->i_numblks = blks * sizeof(int32_t);
+ memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
+ if (inplast == listmax) {
+ listmax += 100;
+ inpsort = (struct inoinfo **)realloc((char *)inpsort,
+ (unsigned int)listmax * sizeof(struct inoinfo *));
+ if (inpsort == NULL)
+ errexit("cannot increase directory list");
+ }
+ inpsort[inplast++] = inp;
+}
+
+/*
+ * Look up an inode cache structure.
+ */
+struct inoinfo *
+getinoinfo(ino_t inumber)
+{
+ struct inoinfo *inp;
+
+ for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
+ if (inp->i_number != inumber)
+ continue;
+ return (inp);
+ }
+ errexit("cannot find inode %llu", (unsigned long long)inumber);
+ return ((struct inoinfo *)0);
+}
+
+/*
+ * Clean up all the inode cache structure.
+ */
+void
+inocleanup(void)
+{
+ struct inoinfo **inpp;
+
+ if (inphead == NULL)
+ return;
+ for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
+ free(*inpp);
+ free(inphead);
+ free(inpsort);
+ inphead = inpsort = NULL;
+}
+
+void
+inodirty(void)
+{
+
+ dirty(pbp);
+}
+
+void
+clri(struct inodesc *idesc, const char *type, int flag)
+{
+ struct ext2fs_dinode *dp;
+
+ dp = ginode(idesc->id_number);
+ if (flag == 1) {
+ pwarn("%s %s", type,
+ (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
+ pinode(idesc->id_number);
+ }
+ if (preen || reply("CLEAR") == 1) {
+ if (preen)
+ printf(" (CLEARED)\n");
+ n_files--;
+ (void)ckinode(dp, idesc);
+ clearinode(dp);
+ statemap[idesc->id_number] = USTATE;
+ inodirty();
+ }
+}
+
+int
+findname(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dirp = idesc->id_dirp;
+ u_int16_t namlen = dirp->e2d_namlen;
+ /* from utilities.c namebuf[] variable */
+ char *buf = __UNCONST(idesc->id_name);
+ if (namlen > MAXPATHLEN) {
+ /* XXX: Prevent overflow but don't fix */
+ namlen = MAXPATHLEN;
+ }
+
+ if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
+ return (KEEPON);
+ (void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
+ buf[namlen] = '\0';
+ return (STOP|FOUND);
+}
+
+int
+findino(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dirp = idesc->id_dirp;
+ u_int32_t ino = fs2h32(dirp->e2d_ino);
+
+ if (ino == 0)
+ return (KEEPON);
+ if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
+ (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
+ && ino <= maxino) {
+ idesc->id_parent = ino;
+ return (STOP|FOUND);
+ }
+ return (KEEPON);
+}
+
+void
+pinode(ino_t ino)
+{
+ struct ext2fs_dinode *dp;
+ struct passwd *pw;
+ uid_t uid;
+
+ printf(" I=%llu ", (unsigned long long)ino);
+ if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
+ return;
+ dp = ginode(ino);
+ uid = fs2h16(dp->e2di_uid);
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0)
+ uid |= fs2h16(dp->e2di_uid_high) << 16;
+ printf(" OWNER=");
+#ifndef SMALL
+ if (Uflag && (pw = getpwuid(uid)) != 0)
+ printf("%s ", pw->pw_name);
+ else
+#endif
+ printf("%u ", (unsigned int)uid);
+ printf("MODE=%o\n", fs2h16(dp->e2di_mode));
+ if (preen)
+ printf("%s: ", cdevname());
+ printf("SIZE=%llu ", (long long)inosize(dp));
+ printf("MTIME=%s ", print_mtime(fs2h32(dp->e2di_mtime)));
+}
+
+void
+blkerror(ino_t ino, const char *type, daddr_t blk)
+{
+
+ pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
+ printf("\n");
+ switch (statemap[ino]) {
+
+ case FSTATE:
+ statemap[ino] = FCLEAR;
+ return;
+
+ case DSTATE:
+ statemap[ino] = DCLEAR;
+ return;
+
+ case FCLEAR:
+ case DCLEAR:
+ return;
+
+ default:
+ errexit("BAD STATE %d TO BLKERR", statemap[ino]);
+ /* NOTREACHED */
+ }
+}
+
+/*
+ * allocate an unused inode
+ */
+ino_t
+allocino(ino_t request, int type)
+{
+ ino_t ino;
+ struct ext2fs_dinode *dp;
+ time_t t;
+
+ if (request == 0)
+ request = EXT2_ROOTINO;
+ else if (statemap[request] != USTATE)
+ return (0);
+ for (ino = request; ino < maxino; ino++) {
+ if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
+ continue;
+ if (statemap[ino] == USTATE)
+ break;
+ }
+ if (ino == maxino)
+ return (0);
+ switch (type & IFMT) {
+ case IFDIR:
+ statemap[ino] = DSTATE;
+ break;
+ case IFREG:
+ case IFLNK:
+ statemap[ino] = FSTATE;
+ break;
+ default:
+ return (0);
+ }
+ dp = ginode(ino);
+ dp->e2di_blocks[0] = h2fs32(allocblk());
+ if (dp->e2di_blocks[0] == 0) {
+ statemap[ino] = USTATE;
+ return (0);
+ }
+ dp->e2di_mode = h2fs16(type);
+ (void)time(&t);
+ dp->e2di_atime = h2fs32(t);
+ dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
+ dp->e2di_dtime = 0;
+ inossize(dp, sblock.e2fs_bsize);
+ inosnblock(dp, btodb(sblock.e2fs_bsize));
+ n_files++;
+ inodirty();
+ typemap[ino] = E2IFTODT(type);
+ return (ino);
+}
+
+/*
+ * deallocate an inode
+ */
+void
+freeino(ino_t ino)
+{
+ struct inodesc idesc;
+ struct ext2fs_dinode *dp;
+
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = ADDR;
+ idesc.id_func = pass4check;
+ idesc.id_number = ino;
+ dp = ginode(ino);
+ (void)ckinode(dp, &idesc);
+ clearinode(dp);
+ inodirty();
+ statemap[ino] = USTATE;
+ n_files--;
+}
+
+uint64_t
+inonblock(struct ext2fs_dinode *dp)
+{
+ uint64_t nblock;
+
+ /* XXX check for EXT2_HUGE_FILE without EXT2F_ROCOMPAT_HUGE_FILE? */
+
+ nblock = fs2h32(dp->e2di_nblock);
+
+ if ((sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
+ nblock |= (uint64_t)fs2h16(dp->e2di_nblock_high) << 32;
+ if (fs2h32(dp->e2di_flags) & EXT2_HUGE_FILE) {
+ nblock = EXT2_FSBTODB(&sblock, nblock);
+ }
+ }
+
+ return nblock;
+}
+
+void
+inosnblock(struct ext2fs_dinode *dp, uint64_t nblock)
+{
+ uint32_t flags;
+
+ flags = fs2h32(dp->e2di_flags);
+
+ if (nblock <= 0xffffffffULL) {
+ flags &= ~EXT2_HUGE_FILE;
+ dp->e2di_flags = h2fs32(flags);
+ dp->e2di_nblock = h2fs32(nblock);
+ return;
+ }
+
+ sethuge();
+
+ if (nblock <= 0xffffffffffffULL) {
+ flags &= ~EXT2_HUGE_FILE;
+ dp->e2di_flags = h2fs32(flags);
+ dp->e2di_nblock = h2fs32(nblock);
+ dp->e2di_nblock_high = h2fs16((nblock >> 32));
+ return;
+ }
+
+ if (EXT2_DBTOFSB(&sblock, nblock) <= 0xffffffffffffULL) {
+ flags |= EXT2_HUGE_FILE;
+ dp->e2di_flags = h2fs32(flags);
+ dp->e2di_nblock = h2fs32(EXT2_DBTOFSB(&sblock, nblock));
+ dp->e2di_nblock_high = h2fs16((EXT2_DBTOFSB(&sblock, nblock) >> 32));
+ return;
+ }
+
+ pfatal("trying to set nblocks higher than representable");
+
+ return;
+}
diff --git a/cmd/extra/fsck.ext4/main.c b/cmd/extra/fsck.ext4/main.c
@@ -0,0 +1,284 @@
+/* $NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+__COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
+ The Regents of the University of California. All rights reserved.");
+#endif /* not lint */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/23/94";
+#else
+__RCSID("$NetBSD: main.c,v 1.41 2020/04/05 15:25:39 joerg Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include "fsck.h"
+#include "extern.h"
+#include "fsutil.h"
+#include "exitvalues.h"
+
+struct bufarea bufhead;
+struct bufarea sblk;
+struct bufarea asblk;
+struct bufarea *pdirbp;
+struct bufarea *pbp;
+struct bufarea *getdatablk(daddr_t, long);
+struct m_ext2fs sblock;
+struct dups *duplist;
+struct dups *muldup;
+struct zlncnt *zlnhead;
+struct inoinfo **inphead, **inpsort;
+long numdirs, listmax, inplast;
+long dev_bsize;
+long secsize;
+char nflag;
+char yflag;
+int bflag;
+int Uflag;
+int debug;
+int preen;
+char havesb;
+char skipclean;
+int fsmodified;
+int fsreadfd;
+int fswritefd;
+int rerun;
+daddr_t maxfsblock;
+char *blockmap;
+ino_t maxino;
+ino_t lastino;
+char *statemap;
+u_char *typemap;
+int16_t *lncntp;
+ino_t lfdir;
+daddr_t n_blks;
+daddr_t n_files;
+struct ext2fs_dinode zino;
+
+static int checkfilesys(const char *, char *, long, int);
+static void usage(void) __dead;
+int unresolved;
+
+int
+main(int argc, char *argv[])
+{
+ int result;
+
+ ARGBEGIN {
+ case 'n':
+ case 'f':
+ break;
+ default:
+ usage();
+ } ARGEND
+ if (argc != 1)
+ usage();
+ nflag = 1;
+ result = checkfilesys(argv[0], NULL, 0, 0);
+ return fshut(stdout, "<stdout>") ? FSCK_EXIT_CHECK_FAILED : result;
+}
+
+/*
+ * Check the specified filesystem.
+ */
+/* ARGSUSED */
+static int
+checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
+{
+ daddr_t n_bfree;
+ struct dups *dp;
+ struct zlncnt *zlnp;
+ int i;
+
+ if (preen && child)
+ (void)signal(SIGQUIT, voidquit);
+ setcdevname(filesys, preen);
+ if (debug && preen)
+ pwarn("starting\n");
+ switch (setup(filesys)) {
+ case 0:
+ return FSCK_EXIT_CHECK_FAILED;
+ case -1:
+ return FSCK_EXIT_OK;
+ }
+ /*
+ * 1: scan inodes tallying blocks used
+ */
+ if (preen == 0) {
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
+ printf("** Last Mounted on %.64s\n",
+ sblock.e2fs.e2fs_fsmnt);
+ }
+ printf("** Phase 1 - Check Blocks and Sizes\n");
+ }
+ pass1();
+
+ /*
+ * 1b: locate first references to duplicates, if any
+ */
+ if (duplist) {
+ if (preen)
+ pfatal("INTERNAL ERROR: dups with -p");
+ printf("** Phase 1b - Rescan For More DUPS\n");
+ pass1b();
+ }
+
+ /*
+ * 2: traverse directories from root to mark all connected directories
+ */
+ if (preen == 0)
+ printf("** Phase 2 - Check Pathnames\n");
+ pass2();
+
+ /*
+ * 3: scan inodes looking for disconnected directories
+ */
+ if (preen == 0)
+ printf("** Phase 3 - Check Connectivity\n");
+ pass3();
+
+ /*
+ * 4: scan inodes looking for disconnected files; check reference counts
+ */
+ if (preen == 0)
+ printf("** Phase 4 - Check Reference Counts\n");
+ pass4();
+
+ /*
+ * 5: check and repair resource counts in cylinder groups
+ */
+ if (preen == 0)
+ printf("** Phase 5 - Check Cyl groups\n");
+ pass5();
+
+ /*
+ * print out summary statistics
+ */
+ n_bfree = sblock.e2fs.e2fs_fbcount;
+
+ pwarn("%lld files, %lld used, %lld free\n",
+ (long long)n_files, (long long)n_blks, (long long)n_bfree);
+ if (debug &&
+ /* 9 reserved and unused inodes in FS */
+ (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
+ printf("%lld files missing\n", (long long)n_files);
+ if (debug) {
+ for (i = 0; i < sblock.e2fs_ncg; i++)
+ n_blks += cgoverhead(i);
+ n_blks += sblock.e2fs.e2fs_first_dblock;
+ if (n_blks -= maxfsblock - n_bfree)
+ printf("%lld blocks missing\n", (long long)n_blks);
+ if (duplist != NULL) {
+ printf("The following duplicate blocks remain:");
+ for (dp = duplist; dp; dp = dp->next)
+ printf(" %lld,", (long long)dp->dup);
+ printf("\n");
+ }
+ if (zlnhead != NULL) {
+ printf("The following zero link count inodes remain:");
+ for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
+ printf(" %llu,",
+ (unsigned long long)zlnp->zlncnt);
+ printf("\n");
+ }
+ }
+ zlnhead = (struct zlncnt *)0;
+ duplist = (struct dups *)0;
+ muldup = (struct dups *)0;
+ inocleanup();
+ if (fsmodified) {
+ time_t t;
+ (void)time(&t);
+ sblock.e2fs.e2fs_wtime = t;
+ sblock.e2fs.e2fs_lastfsck = t;
+ sbdirty();
+ }
+ ckfini(1);
+ free(blockmap);
+ free(statemap);
+ free((char *)lncntp);
+ if (!fsmodified)
+ return unresolved ? FSCK_EXIT_UNRESOLVED : FSCK_EXIT_OK;
+ if (!preen)
+ printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
+ if (rerun)
+ printf("\n***** PLEASE RERUN FSCK *****\n");
+ return unresolved ? FSCK_EXIT_UNRESOLVED : FSCK_EXIT_OK;
+}
+
+static void
+usage(void)
+{
+
+ (void) fprintf(stderr,
+ "usage: %s [-fn] filesystem\n",
+ getprogname());
+ exit(FSCK_EXIT_USAGE);
+}
+
diff --git a/cmd/extra/fsck.ext4/pass1.c b/cmd/extra/fsck.ext4/pass1.c
@@ -0,0 +1,405 @@
+/* $NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93";
+#else
+__RCSID("$NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs.h>
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "fsck.h"
+#include "extern.h"
+#include "fsutil.h"
+#include "exitvalues.h"
+
+static daddr_t badblk;
+static daddr_t dupblk;
+static void checkinode(ino_t, struct inodesc *);
+
+void
+pass1(void)
+{
+ ino_t inumber;
+ int c, i;
+ size_t j;
+ daddr_t dbase;
+ struct inodesc idesc;
+
+ /*
+ * Set file system reserved blocks in used block map.
+ */
+ for (c = 0; c < sblock.e2fs_ncg; c++) {
+ dbase = c * sblock.e2fs.e2fs_bpg +
+ sblock.e2fs.e2fs_first_dblock;
+ /* Mark the blocks used for the inode table */
+ if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
+ for (i = 0; i < sblock.e2fs_itpg; i++)
+ setbmap(
+ fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables)
+ + i);
+ }
+ /* Mark the blocks used for the block bitmap */
+ if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
+ setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
+ /* Mark the blocks used for the inode bitmap */
+ if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
+ setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
+
+ if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
+ (sblock.e2fs.e2fs_features_rocompat &
+ EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
+ cg_has_sb(c)) {
+ /* Mark copuy of SB and descriptors */
+ setbmap(dbase);
+ for (i = 1; i <= sblock.e2fs_ngdb; i++)
+ setbmap(dbase+i);
+ }
+
+
+ if (c == 0) {
+ for(i = 0; i < dbase; i++)
+ setbmap(i);
+ }
+ }
+
+ /*
+ * Find all allocated blocks.
+ */
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = ADDR;
+ idesc.id_func = pass1check;
+ inumber = 1;
+ n_files = n_blks = 0;
+ resetinodebuf();
+ for (c = 0; c < sblock.e2fs_ncg; c++) {
+ for (j = 0;
+ j < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
+ j++, inumber++) {
+ if (inumber < EXT2_ROOTINO)
+ continue;
+ checkinode(inumber, &idesc);
+ }
+ }
+ freeinodebuf();
+}
+
+static void
+checkinode(ino_t inumber, struct inodesc *idesc)
+{
+ struct ext2fs_dinode *dp;
+ struct zlncnt *zlnp;
+ int ndb, j;
+ mode_t mode;
+
+ dp = getnextinode(inumber);
+ if (inumber == 8 &&
+ (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_HASJOURNAL)) {
+ if ((fs2h16(dp->e2di_mode) & IFMT) != IFREG)
+ errexit("invalid journal inode");
+ idesc->id_number = inumber;
+ ckinode(dp, idesc);
+ return;
+ }
+ if (inumber < EXT2_FIRSTINO &&
+ inumber != EXT2_ROOTINO &&
+ !(inumber == EXT2_RESIZEINO &&
+ (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
+ return;
+
+ if (dp->e2di_mode && (dp->e2di_facl || dp->e2di_facl_high))
+ errexit("inode %llu has an external attribute block",
+ (unsigned long long)inumber);
+
+ mode = fs2h16(dp->e2di_mode) & IFMT;
+ if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
+ if (mode == 0 && (
+ memcmp(dp->e2di_blocks, zino.e2di_blocks,
+ (EXT2FS_NDADDR + EXT2FS_NIADDR) * sizeof(u_int32_t)) ||
+ dp->e2di_mode || inosize(dp))) {
+ pfatal("PARTIALLY ALLOCATED INODE I=%llu",
+ (unsigned long long)inumber);
+ if (reply("CLEAR") == 1) {
+ dp = ginode(inumber);
+ clearinode(dp);
+ inodirty();
+ }
+ }
+#ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
+ if (dp->e2di_dtime == 0) {
+ pwarn("DELETED INODE I=%llu HAS A NULL DTIME",
+ (unsigned long long)inumber);
+ if (preen) {
+ printf(" (CORRECTED)\n");
+ }
+ if (preen || reply("CORRECT")) {
+ time_t t;
+ time(&t);
+ dp->e2di_dtime = h2fs32(t);
+ dp = ginode(inumber);
+ inodirty();
+ }
+ }
+#endif
+ statemap[inumber] = USTATE;
+ return;
+ }
+ lastino = inumber;
+ if (dp->e2di_dtime != 0) {
+ pwarn("INODE I=%llu HAS DTIME=%s",
+ (unsigned long long)inumber,
+ print_mtime(fs2h32(dp->e2di_dtime)));
+ if (preen) {
+ printf(" (CORRECTED)\n");
+ }
+ if (preen || reply("CORRECT")) {
+ dp = ginode(inumber);
+ dp->e2di_dtime = 0;
+ inodirty();
+ }
+ }
+ if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
+ if (debug)
+ printf("bad size %llu:", (unsigned long long)inosize(dp));
+ goto unknown;
+ }
+ if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
+ dp = ginode(inumber);
+ dp->e2di_mode = h2fs16(IFREG|0600);
+ inossize(dp, sblock.e2fs_bsize);
+ inodirty();
+ }
+ ndb = howmany(inosize(dp), sblock.e2fs_bsize);
+ if (ndb < 0) {
+ if (debug)
+ printf("bad size %llu ndb %d:",
+ (unsigned long long)inosize(dp), ndb);
+ goto unknown;
+ }
+ if (mode == IFBLK || mode == IFCHR)
+ ndb++;
+ if (mode == IFLNK) {
+ /*
+ * Fake ndb value so direct/indirect block checks below
+ * will detect any garbage after symlink string.
+ */
+ if (inosize(dp) < EXT2_MAXSYMLINKLEN ||
+ (EXT2_MAXSYMLINKLEN == 0 && inonblock(dp) == 0)) {
+ ndb = howmany(inosize(dp), sizeof(u_int32_t));
+ if (ndb > EXT2FS_NDADDR) {
+ j = ndb - EXT2FS_NDADDR;
+ for (ndb = 1; j > 1; j--)
+ ndb *= EXT2_NINDIR(&sblock);
+ ndb += EXT2FS_NDADDR;
+ }
+ }
+ }
+ /* Linux puts things in blocks for FIFO, so skip this check */
+ if (mode != IFIFO) {
+ for (j = ndb; j < EXT2FS_NDADDR; j++)
+ if (dp->e2di_blocks[j] != 0) {
+ if (debug)
+ printf("bad direct addr: %d\n",
+ fs2h32(dp->e2di_blocks[j]));
+ goto unknown;
+ }
+ for (j = 0, ndb -= EXT2FS_NDADDR; ndb > 0; j++)
+ ndb /= EXT2_NINDIR(&sblock);
+ for (; j < EXT2FS_NIADDR; j++) {
+ if (dp->e2di_blocks[j+EXT2FS_NDADDR] != 0) {
+ if (debug)
+ printf("bad indirect addr: %d\n",
+ fs2h32(dp->e2di_blocks[j+EXT2FS_NDADDR]));
+ goto unknown;
+ }
+ }
+ }
+ if (ftypeok(dp) == 0)
+ goto unknown;
+ if (inumber >= EXT2_FIRSTINO || inumber == EXT2_ROOTINO) {
+ /* Don't count reserved inodes except root */
+ n_files++;
+ }
+ lncntp[inumber] = fs2h16(dp->e2di_nlink);
+ if (dp->e2di_nlink == 0) {
+ zlnp = malloc(sizeof *zlnp);
+ if (zlnp == NULL) {
+ pfatal("LINK COUNT TABLE OVERFLOW");
+ if (reply("CONTINUE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ } else {
+ zlnp->zlncnt = inumber;
+ zlnp->next = zlnhead;
+ zlnhead = zlnp;
+ }
+ }
+ if (mode == IFDIR) {
+ if (inosize(dp) == 0)
+ statemap[inumber] = DCLEAR;
+ else
+ statemap[inumber] = DSTATE;
+ cacheino(dp, inumber);
+ } else {
+ statemap[inumber] = FSTATE;
+ }
+ typemap[inumber] = E2IFTODT(mode);
+ badblk = dupblk = 0;
+ idesc->id_number = inumber;
+ (void)ckinode(dp, idesc);
+ idesc->id_entryno *= btodb(sblock.e2fs_bsize);
+ if (inonblock(dp) != (uint32_t)idesc->id_entryno) {
+ pwarn("INCORRECT BLOCK COUNT I=%llu (%llu should be %d)",
+ (unsigned long long)inumber,
+ (unsigned long long)inonblock(dp),
+ idesc->id_entryno);
+ if (preen)
+ printf(" (CORRECTED)\n");
+ else if (reply("CORRECT") == 0)
+ return;
+ dp = ginode(inumber);
+ inosnblock(dp, idesc->id_entryno);
+ inodirty();
+ }
+ return;
+unknown:
+ pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
+ statemap[inumber] = FCLEAR;
+ if (reply("CLEAR") == 1) {
+ statemap[inumber] = USTATE;
+ dp = ginode(inumber);
+ clearinode(dp);
+ inodirty();
+ }
+}
+
+int
+pass1check(struct inodesc *idesc)
+{
+ int res = KEEPON;
+ int anyout, nfrags;
+ daddr_t blkno = idesc->id_blkno;
+ struct dups *dlp;
+ struct dups *new;
+
+ if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
+ blkerror(idesc->id_number, "BAD", blkno);
+ if (badblk++ >= MAXBAD) {
+ pwarn("EXCESSIVE BAD BLKS I=%llu",
+ (unsigned long long)idesc->id_number);
+ if (preen)
+ printf(" (SKIPPING)\n");
+ else if (reply("CONTINUE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ return (STOP);
+ }
+ }
+ for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
+ if (anyout && chkrange(blkno, 1)) {
+ res = SKIP;
+ } else if (!testbmap(blkno)) {
+ n_blks++;
+ setbmap(blkno);
+ } else {
+ blkerror(idesc->id_number, "DUP", blkno);
+ if (dupblk++ >= MAXDUP) {
+ pwarn("EXCESSIVE DUP BLKS I=%llu",
+ (unsigned long long)idesc->id_number);
+ if (preen)
+ printf(" (SKIPPING)\n");
+ else if (reply("CONTINUE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ return (STOP);
+ }
+ new = malloc(sizeof(struct dups));
+ if (new == NULL) {
+ pfatal("DUP TABLE OVERFLOW.");
+ if (reply("CONTINUE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ return (STOP);
+ }
+ new->dup = blkno;
+ if (muldup == 0) {
+ duplist = muldup = new;
+ new->next = 0;
+ } else {
+ new->next = muldup->next;
+ muldup->next = new;
+ }
+ for (dlp = duplist; dlp != muldup; dlp = dlp->next)
+ if (dlp->dup == blkno)
+ break;
+ if (dlp == muldup && dlp->dup != blkno)
+ muldup = new;
+ }
+ /*
+ * count the number of blocks found in id_entryno
+ */
+ idesc->id_entryno++;
+ }
+ return (res);
+}
diff --git a/cmd/extra/fsck.ext4/pass1b.c b/cmd/extra/fsck.ext4/pass1b.c
@@ -0,0 +1,129 @@
+/* $NetBSD: pass1b.c,v 1.8 2009/10/19 18:41:08 bouyer Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass1b.c 8.1 (Berkeley) 6/5/93";
+#else
+__RCSID("$NetBSD: pass1b.c,v 1.8 2009/10/19 18:41:08 bouyer Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+
+#include <string.h>
+#include "fsck.h"
+#include "extern.h"
+
+static int pass1bcheck(struct inodesc *);
+static struct dups *duphead;
+
+void
+pass1b(void)
+{
+ int c;
+ uint32_t i;
+ struct ext2fs_dinode *dp;
+ struct inodesc idesc;
+ ino_t inumber;
+
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = ADDR;
+ idesc.id_func = pass1bcheck;
+ duphead = duplist;
+ inumber = 0;
+ for (c = 0; c < sblock.e2fs_ncg; c++) {
+ for (i = 0; i < sblock.e2fs.e2fs_ipg; i++, inumber++) {
+ if ((inumber < EXT2_FIRSTINO) && (inumber != EXT2_ROOTINO))
+ continue;
+ dp = ginode(inumber);
+ if (dp == NULL)
+ continue;
+ idesc.id_number = inumber;
+ if (statemap[inumber] != USTATE &&
+ (ckinode(dp, &idesc) & STOP))
+ return;
+ }
+ }
+}
+
+static int
+pass1bcheck(struct inodesc *idesc)
+{
+ struct dups *dlp;
+ int nfrags, res = KEEPON;
+ daddr_t blkno = idesc->id_blkno;
+
+ for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
+ if (chkrange(blkno, 1))
+ res = SKIP;
+ for (dlp = duphead; dlp; dlp = dlp->next) {
+ if (dlp->dup == blkno) {
+ blkerror(idesc->id_number, "DUP", blkno);
+ dlp->dup = duphead->dup;
+ duphead->dup = blkno;
+ duphead = duphead->next;
+ }
+ if (dlp == muldup)
+ break;
+ }
+ if (muldup == 0 || duphead == muldup->next)
+ return (STOP);
+ }
+ return (res);
+}
diff --git a/cmd/extra/fsck.ext4/pass2.c b/cmd/extra/fsck.ext4/pass2.c
@@ -0,0 +1,468 @@
+/* $NetBSD: pass2.c,v 1.15 2009/10/19 18:41:08 bouyer Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass2.c 8.6 (Berkeley) 10/27/94";
+#else
+__RCSID("$NetBSD: pass2.c,v 1.15 2009/10/19 18:41:08 bouyer Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs.h>
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fsck.h"
+#include "fsutil.h"
+#include "extern.h"
+#include "exitvalues.h"
+
+#define MINDIRSIZE (sizeof (struct ext2fs_dirtemplate))
+
+static int pass2check(struct inodesc *);
+static int blksort(const void *, const void *);
+
+void
+pass2(void)
+{
+ struct ext2fs_dinode *dp;
+ struct inoinfo **inpp, *inp;
+ struct inoinfo **inpend;
+ struct inodesc curino;
+ struct ext2fs_dinode dino;
+ char pathbuf[MAXPATHLEN + 1];
+
+ switch (statemap[EXT2_ROOTINO]) {
+
+ case USTATE:
+ pfatal("ROOT INODE UNALLOCATED");
+ if (reply("ALLOCATE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
+ errexit("CANNOT ALLOCATE ROOT INODE");
+ break;
+
+ case DCLEAR:
+ pfatal("DUPS/BAD IN ROOT INODE");
+ if (reply("REALLOCATE")) {
+ freeino(EXT2_ROOTINO);
+ if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
+ errexit("CANNOT ALLOCATE ROOT INODE");
+ break;
+ }
+ if (reply("CONTINUE") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ break;
+
+ case FSTATE:
+ case FCLEAR:
+ pfatal("ROOT INODE NOT DIRECTORY");
+ if (reply("REALLOCATE")) {
+ freeino(EXT2_ROOTINO);
+ if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
+ errexit("CANNOT ALLOCATE ROOT INODE");
+ break;
+ }
+ if (reply("FIX") == 0)
+ exit(FSCK_EXIT_CHECK_FAILED);
+ dp = ginode(EXT2_ROOTINO);
+ dp->e2di_mode = h2fs16((fs2h16(dp->e2di_mode) & ~IFMT) | IFDIR);
+ inodirty();
+ break;
+
+ case DSTATE:
+ break;
+
+ default:
+ errexit("BAD STATE %d FOR ROOT INODE", statemap[EXT2_ROOTINO]);
+ }
+
+ /*
+ * Sort the directory list into disk block order.
+ */
+ qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
+ /*
+ * Check the integrity of each directory.
+ */
+ memset(&curino, 0, sizeof(struct inodesc));
+ curino.id_type = DATA;
+ curino.id_func = pass2check;
+ inpend = &inpsort[inplast];
+ for (inpp = inpsort; inpp < inpend; inpp++) {
+ inp = *inpp;
+ if (inp->i_isize == 0)
+ continue;
+ if (inp->i_isize < MINDIRSIZE) {
+ direrror(inp->i_number, "DIRECTORY TOO SHORT");
+ inp->i_isize = roundup(MINDIRSIZE, sblock.e2fs_bsize);
+ if (reply("FIX") == 1) {
+ dp = ginode(inp->i_number);
+ inossize(dp, inp->i_isize);
+ inodirty();
+ }
+ } else if ((inp->i_isize & (sblock.e2fs_bsize - 1)) != 0) {
+ getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
+ inp->i_number);
+ pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
+ pathbuf, (u_long)inp->i_isize, sblock.e2fs_bsize);
+ if (preen)
+ printf(" (ADJUSTED)\n");
+ inp->i_isize = roundup(inp->i_isize, sblock.e2fs_bsize);
+ if (preen || reply("ADJUST") == 1) {
+ dp = ginode(inp->i_number);
+ inossize(dp, inp->i_isize);
+ inodirty();
+ }
+ }
+ memset(&dino, 0, sizeof(struct ext2fs_dinode));
+ dino.e2di_mode = h2fs16(IFDIR);
+ inossize(&dino, inp->i_isize);
+ memcpy(&dino.e2di_blocks[0], &inp->i_blks[0], (size_t)inp->i_numblks);
+ curino.id_number = inp->i_number;
+ curino.id_parent = inp->i_parent;
+ (void)ckinode(&dino, &curino);
+ }
+ /*
+ * Now that the parents of all directories have been found,
+ * make another pass to verify the value of `..'
+ */
+ for (inpp = inpsort; inpp < inpend; inpp++) {
+ inp = *inpp;
+ if (inp->i_parent == 0 || inp->i_isize == 0)
+ continue;
+ if (inp->i_dotdot == inp->i_parent ||
+ inp->i_dotdot == (ino_t)-1)
+ continue;
+ if (inp->i_dotdot == 0) {
+ inp->i_dotdot = inp->i_parent;
+ fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
+ if (reply("FIX") == 0)
+ continue;
+ (void)makeentry(inp->i_number, inp->i_parent, "..");
+ lncntp[inp->i_parent]--;
+ continue;
+ }
+ fileerror(inp->i_parent, inp->i_number,
+ "BAD INODE NUMBER FOR '..'");
+ if (reply("FIX") == 0)
+ continue;
+ lncntp[inp->i_dotdot]++;
+ lncntp[inp->i_parent]--;
+ inp->i_dotdot = inp->i_parent;
+ (void)changeino(inp->i_number, "..", inp->i_parent);
+ }
+ /*
+ * Mark all the directories that can be found from the root.
+ */
+ propagate();
+}
+
+static int
+pass2check(struct inodesc *idesc)
+{
+ struct ext2fs_direct *dirp = idesc->id_dirp;
+ struct inoinfo *inp;
+ int n, entrysize, ret = 0;
+ struct ext2fs_dinode *dp;
+ const char *errmsg;
+ struct ext2fs_direct proto;
+ char namebuf[MAXPATHLEN + 1];
+ char pathbuf[MAXPATHLEN + 1];
+
+ /*
+ * check for "."
+ */
+ if (idesc->id_entryno != 0)
+ goto chk1;
+ if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 &&
+ dirp->e2d_name[0] == '.') {
+ if (fs2h32(dirp->e2d_ino) != idesc->id_number) {
+ direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
+ dirp->e2d_ino = h2fs32(idesc->id_number);
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
+ && (dirp->e2d_type != EXT2_FT_DIR)) {
+ direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
+ dirp->e2d_type = EXT2_FT_DIR;
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+ goto chk1;
+ }
+ direrror(idesc->id_number, "MISSING '.'");
+ proto.e2d_ino = h2fs32(idesc->id_number);
+ proto.e2d_namlen = 1;
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ proto.e2d_type = EXT2_FT_DIR;
+ else
+ proto.e2d_type = 0;
+ (void)strlcpy(proto.e2d_name, ".", sizeof(proto.e2d_name));
+ entrysize = EXT2FS_DIRSIZ(proto.e2d_namlen);
+ if (fs2h32(dirp->e2d_ino) != 0 && strcmp(dirp->e2d_name, "..") != 0) {
+ pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
+ dirp->e2d_name);
+ } else if (fs2h16(dirp->e2d_reclen) < entrysize) {
+ pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
+ } else if (fs2h16(dirp->e2d_reclen) < 2 * entrysize) {
+ proto.e2d_reclen = dirp->e2d_reclen;
+ memcpy(dirp, &proto, (size_t)entrysize);
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ } else {
+ n = fs2h16(dirp->e2d_reclen) - entrysize;
+ proto.e2d_reclen = h2fs16(entrysize);
+ memcpy(dirp, &proto, (size_t)entrysize);
+ idesc->id_entryno++;
+ lncntp[fs2h32(dirp->e2d_ino)]--;
+ dirp = (struct ext2fs_direct *)((char *)(dirp) + entrysize);
+ memset(dirp, 0, (size_t)n);
+ dirp->e2d_reclen = h2fs16(n);
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+chk1:
+ if (idesc->id_entryno > 1)
+ goto chk2;
+ inp = getinoinfo(idesc->id_number);
+ proto.e2d_ino = h2fs32(inp->i_parent);
+ proto.e2d_namlen = 2;
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
+ proto.e2d_type = EXT2_FT_DIR;
+ else
+ proto.e2d_type = 0;
+ (void)strlcpy(proto.e2d_name, "..", sizeof(proto.e2d_name));
+ entrysize = EXT2FS_DIRSIZ(2);
+ if (idesc->id_entryno == 0) {
+ n = EXT2FS_DIRSIZ(dirp->e2d_namlen);
+ if (fs2h16(dirp->e2d_reclen) < n + entrysize)
+ goto chk2;
+ proto.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - n);
+ dirp->e2d_reclen = h2fs16(n);
+ idesc->id_entryno++;
+ lncntp[fs2h32(dirp->e2d_ino)]--;
+ dirp = (struct ext2fs_direct *)((char *)(dirp) + n);
+ memset(dirp, 0, (size_t)fs2h16(proto.e2d_reclen));
+ dirp->e2d_reclen = proto.e2d_reclen;
+ }
+ if (fs2h32(dirp->e2d_ino) != 0 &&
+ dirp->e2d_namlen == 2 &&
+ strncmp(dirp->e2d_name, "..", 2) == 0) {
+ inp->i_dotdot = fs2h32(dirp->e2d_ino);
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
+ && dirp->e2d_type != EXT2_FT_DIR) {
+ direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
+ dirp->e2d_type = EXT2_FT_DIR;
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+ goto chk2;
+ }
+ if (fs2h32(dirp->e2d_ino) != 0 &&
+ dirp->e2d_namlen == 1 &&
+ strncmp(dirp->e2d_name, ".", 1) != 0) {
+ fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
+ pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
+ dirp->e2d_name);
+ inp->i_dotdot = (ino_t)-1;
+ } else if (fs2h16(dirp->e2d_reclen) < entrysize) {
+ fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
+ pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
+ inp->i_dotdot = (ino_t)-1;
+ } else if (inp->i_parent != 0) {
+ /*
+ * We know the parent, so fix now.
+ */
+ inp->i_dotdot = inp->i_parent;
+ fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
+ proto.e2d_reclen = dirp->e2d_reclen;
+ memcpy(dirp, &proto, (size_t)entrysize);
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+ idesc->id_entryno++;
+ if (fs2h32(dirp->e2d_ino) != 0)
+ lncntp[fs2h32(dirp->e2d_ino)]--;
+ return (ret|KEEPON);
+chk2:
+ if (fs2h32(dirp->e2d_ino) == 0)
+ return (ret|KEEPON);
+ if (dirp->e2d_namlen <= 2 &&
+ dirp->e2d_name[0] == '.' &&
+ idesc->id_entryno >= 2) {
+ if (dirp->e2d_namlen == 1) {
+ direrror(idesc->id_number, "EXTRA '.' ENTRY");
+ dirp->e2d_ino = 0;
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ return (KEEPON | ret);
+ }
+ if (dirp->e2d_name[1] == '.') {
+ direrror(idesc->id_number, "EXTRA '..' ENTRY");
+ dirp->e2d_ino = 0;
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ return (KEEPON | ret);
+ }
+ }
+ idesc->id_entryno++;
+ n = 0;
+ if (fs2h32(dirp->e2d_ino) > maxino ||
+ (fs2h32(dirp->e2d_ino) < EXT2_FIRSTINO &&
+ fs2h32(dirp->e2d_ino) != EXT2_ROOTINO)) {
+ fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "I OUT OF RANGE");
+ n = reply("REMOVE");
+ } else {
+again:
+ switch (statemap[fs2h32(dirp->e2d_ino)]) {
+ case USTATE:
+ if (idesc->id_entryno <= 2)
+ break;
+ fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "UNALLOCATED");
+ n = reply("REMOVE");
+ break;
+
+ case DCLEAR:
+ case FCLEAR:
+ if (idesc->id_entryno <= 2)
+ break;
+ if (statemap[fs2h32(dirp->e2d_ino)] == FCLEAR)
+ errmsg = "DUP/BAD";
+ else if (!preen)
+ errmsg = "ZERO LENGTH DIRECTORY";
+ else {
+ n = 1;
+ break;
+ }
+ fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), errmsg);
+ if ((n = reply("REMOVE")) == 1)
+ break;
+ dp = ginode(fs2h32(dirp->e2d_ino));
+ statemap[fs2h32(dirp->e2d_ino)] =
+ (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
+ lncntp[fs2h32(dirp->e2d_ino)] = fs2h16(dp->e2di_nlink);
+ goto again;
+
+ case DSTATE:
+ case DFOUND:
+ inp = getinoinfo(fs2h32(dirp->e2d_ino));
+ if (inp->i_parent != 0 && idesc->id_entryno > 2) {
+ getpathname(pathbuf, sizeof(pathbuf),
+ idesc->id_number, idesc->id_number);
+ getpathname(namebuf, sizeof(namebuf),
+ fs2h32(dirp->e2d_ino),
+ fs2h32(dirp->e2d_ino));
+ pwarn("%s %s %s\n", pathbuf,
+ "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
+ namebuf);
+ if (preen)
+ printf(" (IGNORED)\n");
+ else if ((n = reply("REMOVE")) == 1)
+ break;
+ }
+ if (idesc->id_entryno > 2)
+ inp->i_parent = idesc->id_number;
+ /* fall through */
+
+ case FSTATE:
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (sblock.e2fs.e2fs_features_incompat &
+ EXT2F_INCOMPAT_FTYPE) &&
+ dirp->e2d_type !=
+ inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)])) {
+ dirp->e2d_type =
+ inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)]);
+ fileerror(idesc->id_number,
+ fs2h32(dirp->e2d_ino),
+ "BAD TYPE VALUE");
+ if (reply("FIX") == 1)
+ ret |= ALTERED;
+ }
+ lncntp[fs2h32(dirp->e2d_ino)]--;
+ break;
+
+ default:
+ errexit("BAD STATE %d FOR INODE I=%d",
+ statemap[fs2h32(dirp->e2d_ino)], fs2h32(dirp->e2d_ino));
+ }
+ }
+ if (n == 0)
+ return (ret|KEEPON);
+ dirp->e2d_ino = 0;
+ return (ret|KEEPON|ALTERED);
+}
+
+/*
+ * Routine to sort disk blocks.
+ */
+static int
+blksort(const void *inpp1, const void *inpp2)
+{
+ return ((* (const struct inoinfo * const*) inpp1)->i_blks[0] -
+ (* (const struct inoinfo * const*) inpp2)->i_blks[0]);
+}
diff --git a/cmd/extra/fsck.ext4/pass3.c b/cmd/extra/fsck.ext4/pass3.c
@@ -0,0 +1,99 @@
+/* $NetBSD: pass3.c,v 1.7 2009/10/19 18:41:08 bouyer Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass3.c 8.1 (Berkeley) 6/5/93";
+#else
+__RCSID("$NetBSD: pass3.c,v 1.7 2009/10/19 18:41:08 bouyer Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include "fsck.h"
+#include "extern.h"
+
+void
+pass3(void)
+{
+ struct inoinfo **inpp, *inp;
+ ino_t orphan;
+ int loopcnt;
+
+ for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
+ inp = *inpp;
+ if (inp->i_number == EXT2_ROOTINO ||
+ !(inp->i_parent == 0 || statemap[inp->i_number] == DSTATE))
+ continue;
+ if (statemap[inp->i_number] == DCLEAR)
+ continue;
+ for (loopcnt = 0; ; loopcnt++) {
+ orphan = inp->i_number;
+ if (inp->i_parent == 0 ||
+ statemap[inp->i_parent] != DSTATE ||
+ loopcnt > numdirs)
+ break;
+ inp = getinoinfo(inp->i_parent);
+ }
+ (void)linkup(orphan, inp->i_dotdot);
+ inp->i_parent = inp->i_dotdot = lfdir;
+ lncntp[lfdir]--;
+ statemap[orphan] = DFOUND;
+ propagate();
+ }
+}
diff --git a/cmd/extra/fsck.ext4/pass4.c b/cmd/extra/fsck.ext4/pass4.c
@@ -0,0 +1,163 @@
+/* $NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass4.c 8.1 (Berkeley) 6/5/93";
+#else
+__RCSID("$NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fsutil.h"
+#include "fsck.h"
+#include "extern.h"
+
+void
+pass4(void)
+{
+ ino_t inumber;
+ struct zlncnt *zlnp;
+ struct ext2fs_dinode *dp;
+ struct inodesc idesc;
+ int n;
+
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = ADDR;
+ idesc.id_func = pass4check;
+ for (inumber = EXT2_ROOTINO; inumber <= lastino; inumber++) {
+ if (inumber < EXT2_FIRSTINO && inumber > EXT2_ROOTINO)
+ continue;
+ idesc.id_number = inumber;
+ switch (statemap[inumber]) {
+
+ case FSTATE:
+ case DFOUND:
+ n = lncntp[inumber];
+ if (n)
+ adjust(&idesc, (short)n);
+ else {
+ for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
+ if (zlnp->zlncnt == inumber) {
+ zlnp->zlncnt = zlnhead->zlncnt;
+ zlnp = zlnhead;
+ zlnhead = zlnhead->next;
+ free((char *)zlnp);
+ clri(&idesc, "UNREF", 1);
+ break;
+ }
+ }
+ break;
+
+ case DSTATE:
+ clri(&idesc, "UNREF", 1);
+ break;
+
+ case DCLEAR:
+ dp = ginode(inumber);
+ if (inosize(dp) == 0) {
+ clri(&idesc, "ZERO LENGTH", 1);
+ break;
+ }
+ /* fall through */
+ case FCLEAR:
+ clri(&idesc, "BAD/DUP", 1);
+ break;
+
+ case USTATE:
+ break;
+
+ default:
+ errexit("BAD STATE %d FOR INODE I=%llu",
+ statemap[inumber], (unsigned long long)inumber);
+ }
+ }
+}
+
+int
+pass4check(struct inodesc *idesc)
+{
+ struct dups *dlp;
+ int nfrags, res = KEEPON;
+ daddr_t blkno = idesc->id_blkno;
+
+ for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
+ if (chkrange(blkno, 1)) {
+ res = SKIP;
+ } else if (testbmap(blkno)) {
+ for (dlp = duplist; dlp; dlp = dlp->next) {
+ if (dlp->dup != blkno)
+ continue;
+ dlp->dup = duplist->dup;
+ dlp = duplist;
+ duplist = duplist->next;
+ free((char *)dlp);
+ break;
+ }
+ if (dlp == 0) {
+ clrbmap(blkno);
+ n_blks--;
+ }
+ }
+ }
+ return (res);
+}
diff --git a/cmd/extra/fsck.ext4/pass5.c b/cmd/extra/fsck.ext4/pass5.c
@@ -0,0 +1,275 @@
+/* $NetBSD: pass5.c,v 1.20 2012/08/26 09:33:18 dholland Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)pass5.c 8.6 (Berkeley) 11/30/94";
+#else
+__RCSID("$NetBSD: pass5.c,v 1.20 2012/08/26 09:33:18 dholland Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "fsutil.h"
+#include "fsck.h"
+#include "extern.h"
+
+
+static void print_bmap(char *, uint32_t);
+
+void
+pass5(void)
+{
+ int c;
+ struct m_ext2fs *fs = &sblock;
+ daddr_t dbase, dmax;
+ daddr_t d;
+ uint32_t i, j;
+ struct inodesc idesc[3];
+ struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
+ char *ibmap, *bbmap;
+ u_int32_t cs_ndir, cs_nbfree, cs_nifree;
+ char msg[255];
+
+ cs_ndir = 0;
+ cs_nbfree = 0;
+ cs_nifree = 0;
+
+ ibmap = malloc(fs->e2fs_bsize);
+ bbmap = malloc(fs->e2fs_bsize);
+ if (ibmap == NULL || bbmap == NULL) {
+ errexit("out of memory");
+ }
+
+ for (c = 0; c < fs->e2fs_ncg; c++) {
+ u_int32_t nbfree = 0;
+ u_int32_t nifree = 0;
+ u_int32_t ndirs = 0;
+
+ nbfree = 0;
+ nifree = fs->e2fs.e2fs_ipg;
+ ndirs = 0;
+
+ if (blk_bitmap == NULL) {
+ blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
+ fs->e2fs_bsize);
+ } else {
+ getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
+ fs->e2fs_bsize);
+ }
+ if (ino_bitmap == NULL) {
+ ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
+ fs->e2fs_bsize);
+ } else {
+ getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
+ fs->e2fs_bsize);
+ }
+ memset(bbmap, 0, fs->e2fs_bsize);
+ memset(ibmap, 0, fs->e2fs_bsize);
+ memset(&idesc[0], 0, sizeof idesc);
+ for (i = 0; i < 3; i++) {
+ idesc[i].id_type = ADDR;
+ }
+
+ j = fs->e2fs.e2fs_ipg * c + 1;
+
+ for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
+ if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
+ setbit(ibmap, i);
+ nifree--;
+ continue;
+ }
+ if (j > fs->e2fs.e2fs_icount) {
+ setbit(ibmap, i);
+ continue;
+ }
+ switch (statemap[j]) {
+
+ case USTATE:
+ break;
+
+ case DSTATE:
+ case DCLEAR:
+ case DFOUND:
+ ndirs++;
+ /* fall through */
+
+ case FSTATE:
+ case FCLEAR:
+ nifree--;
+ setbit(ibmap, i);
+ break;
+
+ default:
+ errexit("BAD STATE %d FOR INODE I=%"PRIu32,
+ statemap[j], j);
+ }
+ }
+
+ /* fill in unused par of the inode map */
+ for (i = fs->e2fs.e2fs_ipg / NBBY; i < (uint32_t)fs->e2fs_bsize; i++)
+ ibmap[i] = 0xff;
+
+ dbase = c * sblock.e2fs.e2fs_bpg +
+ sblock.e2fs.e2fs_first_dblock;
+ dmax = (daddr_t)(c+1) * sblock.e2fs.e2fs_bpg +
+ sblock.e2fs.e2fs_first_dblock;
+
+ for (i = 0, d = dbase;
+ d < dmax;
+ d ++, i ++) {
+ if (d >= sblock.e2fs.e2fs_bcount || testbmap(d)) {
+ setbit(bbmap, i);
+ continue;
+ } else {
+ nbfree++;
+ }
+
+ }
+ cs_nbfree += nbfree;
+ cs_nifree += nifree;
+ cs_ndir += ndirs;
+
+ if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
+ fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
+ fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
+ printf("summary info for cg %d is %d, %d, %d,"
+ "should be %d, %d, %d\n", c,
+ fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
+ fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
+ fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
+ nbfree,
+ nifree,
+ ndirs);
+ }
+ (void)snprintf(msg, sizeof(msg),
+ "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
+ if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
+ fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
+ fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
+ dofix(&idesc[0], msg)) {
+ fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
+ fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
+ fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
+ sbdirty();
+ }
+
+ if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
+ printf("blk_bitmap:\n");
+ print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
+ printf("bbmap:\n");
+ print_bmap(bbmap, fs->e2fs_bsize);
+ }
+
+ (void)snprintf(msg, sizeof(msg),
+ "BLK(S) MISSING IN BIT MAPS #%d", c);
+ if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
+ dofix(&idesc[1], msg)) {
+ memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
+ dirty(blk_bitmap);
+ }
+ if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
+ printf("ino_bitmap:\n");
+ print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
+ printf("ibmap:\n");
+ print_bmap(ibmap, fs->e2fs_bsize);
+ }
+ (void)snprintf(msg, sizeof(msg),
+ "INODE(S) MISSING IN BIT MAPS #%d", c);
+ if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
+ dofix(&idesc[1], msg)) {
+ memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
+ dirty(ino_bitmap);
+ }
+
+ }
+ if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
+ fs->e2fs.e2fs_ficount != cs_nifree)) {
+ printf("summary info bad in superblock: %d, %d should be %d, %d\n",
+ fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
+ cs_nbfree, cs_nifree);
+ }
+ if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
+ fs->e2fs.e2fs_ficount != cs_nifree)
+ && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
+ fs->e2fs.e2fs_fbcount = cs_nbfree;
+ fs->e2fs.e2fs_ficount = cs_nifree;
+ sbdirty();
+ }
+ free(ibmap);
+ free(bbmap);
+}
+
+static void
+print_bmap(char *map, uint32_t size)
+{
+ uint32_t i, j;
+
+ i = 0;
+ while (i < size) {
+ printf("%04x: ",i);
+ for (j = 0; j < 16; j++, i++)
+ printf("%02x ", (unsigned char)map[i] & 0xff);
+ printf("\n");
+ }
+}
diff --git a/cmd/extra/fsck.ext4/setup.c b/cmd/extra/fsck.ext4/setup.c
@@ -0,0 +1,390 @@
+/* $NetBSD: setup.c,v 1.39 2019/03/31 13:16:52 mlelstv Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)setup.c 8.5 (Berkeley) 11/23/94";
+#else
+__RCSID("$NetBSD: setup.c,v 1.39 2019/03/31 13:16:52 mlelstv Exp $");
+#endif
+#endif /* not lint */
+
+#define FSTYPENAMES
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/file.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+
+#include "fsck.h"
+#include "extern.h"
+#include "fsutil.h"
+#include "diskutil.h"
+#include "exitvalues.h"
+
+void badsb(int, const char *);
+static int readsb(int);
+
+static uint64_t volume_bytes;
+int
+setup(const char *dev)
+{
+ long cg, i;
+ long bmapsize;
+ struct stat statb;
+ struct BlockDev target = {0};
+ off_t sizepb;
+ uint64_t maxfilesize;
+ int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY;
+
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+ flags |= O_EXCL;
+#endif
+ havesb = 0;
+ fswritefd = -1;
+ if ((fsreadfd = open(dev, flags)) < 0 || fstat(fsreadfd, &statb) < 0)
+ enprintf(FSCK_EXIT_CHECK_FAILED, "%s:", dev);
+ if (S_ISREG(statb.st_mode))
+ volume_bytes = statb.st_size;
+ else if (S_ISBLK(statb.st_mode) || S_ISCHR(statb.st_mode)) {
+ target.fd = fsreadfd;
+ blockdev_sys_size(&target);
+ volume_bytes = target.size;
+ }
+ if (!volume_bytes)
+ errexit("%s: cannot determine volume size", dev);
+ printf("** %s (NO WRITE)\n", dev);
+ fsmodified = 0;
+ lfdir = 0;
+ initbarea(&sblk);
+ initbarea(&asblk);
+ sblk.b_un.b_buf = malloc(SBSIZE);
+ asblk.b_un.b_buf = malloc(SBSIZE);
+ if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
+ errexit("cannot allocate space for superblock");
+ dev_bsize = secsize = DEV_BSIZE;
+ if (!readsb(1))
+ return 0;
+ maxfsblock = sblock.e2fs.e2fs_bcount;
+ maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
+ sizepb = sblock.e2fs_bsize;
+ maxfilesize = sblock.e2fs_bsize * EXT2FS_NDADDR - 1;
+ for (i = 0; i < EXT2FS_NIADDR; i++) {
+ sizepb *= EXT2_NINDIR(&sblock);
+ maxfilesize += sizepb;
+ }
+ /*
+ * Check and potentially fix certain fields in the super block.
+ */
+ if (/* (sblock.e2fs.e2fs_rbcount < 0) || */
+ (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
+ pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
+ sblock.e2fs.e2fs_rbcount);
+ if (reply("SET TO DEFAULT") == 1) {
+ sblock.e2fs.e2fs_rbcount =
+ sblock.e2fs.e2fs_bcount * MINFREE / 100;
+ sbdirty();
+ dirty(&asblk);
+ }
+ }
+ if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
+ pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
+ sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
+ return 0;
+ }
+ if (asblk.b_dirty && !bflag) {
+ copyback_sb(&asblk);
+ flush(fswritefd, &asblk);
+ }
+ /*
+ * read in the summary info.
+ */
+
+ sblock.e2fs_gd = ecalloc(sblock.e2fs_ncg, sizeof(*sblock.e2fs_gd));
+ for (i = 0; i < sblock.e2fs_ngdb; i++) {
+ char buf[4096];
+ int j;
+ int perblock = sblock.e2fs_bsize / E2FS_REV0_GD_SIZE;
+
+ bread(fsreadfd, buf,
+ EXT2_FSBTODB(&sblock, sblock.e2fs.e2fs_first_dblock + i + 1),
+ sblock.e2fs_bsize);
+ for (j = 0; j < perblock && i * perblock + j < sblock.e2fs_ncg; j++)
+ memcpy(&sblock.e2fs_gd[i * perblock + j],
+ buf + j * E2FS_REV0_GD_SIZE, E2FS_REV0_GD_SIZE);
+ }
+ for (cg = 0; cg < sblock.e2fs_ncg; cg++) {
+ const struct ext2_gd *gd = &sblock.e2fs_gd[cg];
+ uint64_t first = (uint64_t)cg * sblock.e2fs.e2fs_bpg +
+ sblock.e2fs.e2fs_first_dblock;
+ uint64_t end = MIN(first + sblock.e2fs.e2fs_bpg, maxfsblock);
+ uint32_t blockmap = fs2h32(gd->ext2bgd_b_bitmap);
+ uint32_t inodemap = fs2h32(gd->ext2bgd_i_bitmap);
+ uint32_t table = fs2h32(gd->ext2bgd_i_tables);
+
+ if (cgoverhead(cg) > end - first ||
+ (fs2h16(gd->ext2bgd_flags) & ~E2FS_BG_INODE_ZEROED) ||
+ blockmap < first || blockmap >= end ||
+ inodemap < first || inodemap >= end ||
+ table < first || table >= end ||
+ sblock.e2fs_itpg > end - table)
+ errexit("invalid or unsupported group %ld", cg);
+ }
+ /*
+ * allocate and initialize the necessary maps
+ */
+ bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
+ blockmap = calloc((size_t)bmapsize, sizeof(char));
+ if (blockmap == NULL) {
+ printf("cannot alloc %u bytes for blockmap\n",
+ (unsigned int)bmapsize);
+ goto badsblabel;
+ }
+ statemap = calloc((size_t)(maxino + 2), sizeof(char));
+ if (statemap == NULL) {
+ printf("cannot alloc %u bytes for statemap\n",
+ (unsigned int)(maxino + 1));
+ goto badsblabel;
+ }
+ typemap = calloc((size_t)(maxino + 1), sizeof(char));
+ if (typemap == NULL) {
+ printf("cannot alloc %u bytes for typemap\n",
+ (unsigned int)(maxino + 1));
+ goto badsblabel;
+ }
+ lncntp = calloc((size_t)(maxino + 1), sizeof(int16_t));
+ if (lncntp == NULL) {
+ printf("cannot alloc %u bytes for lncntp\n",
+ (unsigned int)((maxino + 1) * sizeof(int16_t)));
+ goto badsblabel;
+ }
+ for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
+ numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
+ }
+ inplast = 0;
+ numdirs = MAX(numdirs, 1);
+ listmax = numdirs + 10;
+ inpsort = calloc((unsigned int)listmax, sizeof(struct inoinfo *));
+ inphead = calloc((unsigned int)numdirs, sizeof(struct inoinfo *));
+ if (inpsort == NULL || inphead == NULL) {
+ printf("cannot alloc %u bytes for inphead\n",
+ (unsigned int)(numdirs * sizeof(struct inoinfo *)));
+ goto badsblabel;
+ }
+ bufinit();
+ return 1;
+
+badsblabel:
+ ckfini(0);
+ return 0;
+}
+
+/*
+ * Read in the super block and its summary info, convert to host byte order.
+ */
+static int
+readsb(int listerr)
+{
+ daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
+
+ if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
+ return 0;
+ sblk.b_bno = super;
+ sblk.b_size = SBSIZE;
+
+ /* Copy the superblock in memory */
+ e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
+
+ /*
+ * run a few consistency checks of the super block
+ */
+ if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
+ badsb(listerr, "MAGIC NUMBER WRONG");
+ return 0;
+ }
+ if (sblock.e2fs.e2fs_log_bsize > 2) {
+ badsb(listerr, "BAD LOG_BSIZE");
+ return 0;
+ }
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ (!powerof2(sblock.e2fs.e2fs_inode_size) ||
+ sblock.e2fs.e2fs_inode_size < EXT2_REV0_DINODE_SIZE ||
+ sblock.e2fs.e2fs_inode_size >
+ (1024 << sblock.e2fs.e2fs_log_bsize))) {
+ badsb(listerr, "BAD INODE_SIZE");
+ return 0;
+ }
+
+ if (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_REPLAY_JOURNAL)
+ errexit("journal replay required");
+ if ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP_FSCK) ||
+ (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP_FSCK) ||
+ (sblock.e2fs.e2fs_features_compat & ~EXT2F_COMPAT_SUPP_FSCK))
+ errexit("unsupported ext layout");
+ if ((sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_HASJOURNAL) &&
+ (fs2h32(sblk.b_un.b_fs->e3fs_journal_inum) != 8 ||
+ sblk.b_un.b_fs->e3fs_journal_dev != 0))
+ errexit("unsupported journal layout");
+ if (sblk.b_un.b_fs->e3fs_last_orphan ||
+ sblock.e2fs.e2fs_state != E2FS_ISCLEAN)
+ errexit("filesystem requires recovery");
+ if (sblock.e2fs.e2fs_rev > E2FS_REV1 ||
+ sblock.e2fs.e2fs_first_dblock != (sblock.e2fs.e2fs_log_bsize == 0) ||
+ sblock.e2fs.e2fs_bcount <= sblock.e2fs.e2fs_first_dblock ||
+ !sblock.e2fs.e2fs_bpg || !sblock.e2fs.e2fs_ipg ||
+ sblock.e2fs.e2fs_bpg > (8192U << sblock.e2fs.e2fs_log_bsize) ||
+ sblock.e2fs.e2fs_ipg > (8192U << sblock.e2fs.e2fs_log_bsize) ||
+ sblock.e2fs.e2fs_ipg % 8 ||
+ (uint64_t)sblock.e2fs.e2fs_bcount * (1024U << sblock.e2fs.e2fs_log_bsize) >
+ volume_bytes)
+ errexit("invalid ext geometry");
+ sblock.e2fs_ncg =
+ howmany((uint64_t)sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
+ sblock.e2fs.e2fs_bpg);
+ sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
+ sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
+ sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
+ sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
+ sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
+ sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
+ sblock.e2fs_bsize / E2FS_REV0_GD_SIZE);
+ sblock.e2fs_ipb = sblock.e2fs_bsize / EXT2_DINODE_SIZE(&sblock);
+ sblock.e2fs_itpg = howmany(sblock.e2fs.e2fs_ipg, sblock.e2fs_ipb);
+
+ if ((uint64_t)sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg !=
+ sblock.e2fs.e2fs_icount || sblock.e2fs.e2fs_icount < EXT2_FIRSTINO ||
+ sblock.e2fs.e2fs_icount >= UINT32_MAX - 2 ||
+ 1 + sblock.e2fs_ngdb > sblock.e2fs.e2fs_bpg)
+ errexit("invalid ext group or inode count");
+ havesb = 1;
+ return 1;
+}
+
+void
+copyback_sb(struct bufarea *bp)
+{
+ /* Copy the in-memory superblock back to buffer */
+ bp->b_un.b_fs->e2fs_icount = h2fs32(sblock.e2fs.e2fs_icount);
+ bp->b_un.b_fs->e2fs_bcount = h2fs32(sblock.e2fs.e2fs_bcount);
+ bp->b_un.b_fs->e2fs_rbcount = h2fs32(sblock.e2fs.e2fs_rbcount);
+ bp->b_un.b_fs->e2fs_fbcount = h2fs32(sblock.e2fs.e2fs_fbcount);
+ bp->b_un.b_fs->e2fs_ficount = h2fs32(sblock.e2fs.e2fs_ficount);
+ bp->b_un.b_fs->e2fs_first_dblock =
+ h2fs32(sblock.e2fs.e2fs_first_dblock);
+ bp->b_un.b_fs->e2fs_log_bsize = h2fs32(sblock.e2fs.e2fs_log_bsize);
+ bp->b_un.b_fs->e2fs_fsize = h2fs32(sblock.e2fs.e2fs_fsize);
+ bp->b_un.b_fs->e2fs_bpg = h2fs32(sblock.e2fs.e2fs_bpg);
+ bp->b_un.b_fs->e2fs_fpg = h2fs32(sblock.e2fs.e2fs_fpg);
+ bp->b_un.b_fs->e2fs_ipg = h2fs32(sblock.e2fs.e2fs_ipg);
+ bp->b_un.b_fs->e2fs_mtime = h2fs32(sblock.e2fs.e2fs_mtime);
+ bp->b_un.b_fs->e2fs_wtime = h2fs32(sblock.e2fs.e2fs_wtime);
+ bp->b_un.b_fs->e2fs_lastfsck = h2fs32(sblock.e2fs.e2fs_lastfsck);
+ bp->b_un.b_fs->e2fs_fsckintv = h2fs32(sblock.e2fs.e2fs_fsckintv);
+ bp->b_un.b_fs->e2fs_creator = h2fs32(sblock.e2fs.e2fs_creator);
+ bp->b_un.b_fs->e2fs_rev = h2fs32(sblock.e2fs.e2fs_rev);
+ bp->b_un.b_fs->e2fs_mnt_count = h2fs16(sblock.e2fs.e2fs_mnt_count);
+ bp->b_un.b_fs->e2fs_max_mnt_count =
+ h2fs16(sblock.e2fs.e2fs_max_mnt_count);
+ bp->b_un.b_fs->e2fs_magic = h2fs16(sblock.e2fs.e2fs_magic);
+ bp->b_un.b_fs->e2fs_state = h2fs16(sblock.e2fs.e2fs_state);
+ bp->b_un.b_fs->e2fs_beh = h2fs16(sblock.e2fs.e2fs_beh);
+ bp->b_un.b_fs->e2fs_ruid = h2fs16(sblock.e2fs.e2fs_ruid);
+ bp->b_un.b_fs->e2fs_rgid = h2fs16(sblock.e2fs.e2fs_rgid);
+}
+
+void
+badsb(int listerr, const char *s)
+{
+
+ if (!listerr)
+ return;
+ if (preen)
+ printf("%s: ", cdevname());
+ pfatal("BAD SUPER BLOCK: %s\n", s);
+}
+
+/*
+ * Calculate a prototype superblock based on information in the disk label.
+ * When done the cgsblock macro can be calculated and the fs_ncg field
+ * can be used. Do NOT attempt to use other macros without verifying that
+ * their needed information is available!
+ */
+
+daddr_t
+cgoverhead(int c)
+{
+ int overh;
+ overh =
+ 1 /* block bitmap */ +
+ 1 /* inode bitmap */ +
+ sblock.e2fs_itpg;
+ if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
+ sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
+ if (cg_has_sb(c) == 0)
+ return overh;
+ }
+ overh += 1 /* superblock */ + sblock.e2fs_ngdb;
+ return overh;
+}
diff --git a/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs.h b/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs.h
@@ -0,0 +1,568 @@
+/* $NetBSD: ext2fs.h,v 1.53 2025/05/04 20:26:56 rillig Exp $ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)fs.h 8.10 (Berkeley) 10/27/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)fs.h 8.10 (Berkeley) 10/27/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+#ifndef _UFS_EXT2FS_EXT2FS_H_
+#define _UFS_EXT2FS_EXT2FS_H_
+
+#include <stdint.h>
+
+/*
+ * Each disk drive contains some number of file systems.
+ * A file system consists of a number of cylinder groups.
+ * Each cylinder group has inodes and data.
+ *
+ * A file system is described by its super-block, which in turn
+ * describes the cylinder groups. The super-block is critical
+ * data and is replicated in each cylinder group to protect against
+ * catastrophic loss. This is done at `newfs' time and the critical
+ * super-block data does not change, so the copies need not be
+ * referenced further unless disaster strikes.
+ *
+ * The first boot and super blocks are given in absolute disk addresses.
+ * The byte-offset forms are preferred, as they don't imply a sector size.
+ */
+#define BBSIZE 1024
+#define SBSIZE 1024
+#define BBOFF ((off_t)(0))
+#define SBOFF ((off_t)(BBOFF + BBSIZE))
+#define BBLOCK ((daddr_t)(0))
+#define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
+
+#define fsbtodb(fs, b) ((daddr_t)(b) << (fs)->e2fs_fsbtodb)
+/* calculates (loc / fs->fs_bsize) */
+#define lblkno(fs, loc) ((loc) >> (fs->e2fs_bshift))
+#define blksize(fs, ip, lbn) ((fs)->e2fs_bsize)
+
+/*
+ * Addresses stored in inodes are capable of addressing blocks
+ * XXX
+ */
+
+/*
+ * MINBSIZE is the smallest allowable block size.
+ * MINBSIZE must be big enough to hold a cylinder group block,
+ * thus changes to (struct cg) must keep its size within MINBSIZE.
+ * Note that super blocks are always of size SBSIZE,
+ * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
+ */
+#define LOG_MINBSIZE 10
+#define MINBSIZE (1 << LOG_MINBSIZE)
+
+/*
+ * The path name on which the file system is mounted is maintained
+ * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
+ * the super block for this name.
+ */
+#define MAXMNTLEN 512
+
+/*
+ * MINFREE gives the minimum acceptable percentage of file system
+ * blocks which may be free. If the freelist drops below this level
+ * only the superuser may continue to allocate blocks. This may
+ * be set to 0 if no reserve of free blocks is deemed necessary,
+ * however throughput drops by fifty percent if the file system
+ * is run at between 95% and 100% full; thus the minimum default
+ * value of fs_minfree is 5%. However, to get good clustering
+ * performance, 10% is a better choice. hence we use 10% as our
+ * default value. With 10% free space, fragmentation is not a
+ * problem, so we choose to optimize for time.
+ */
+#define MINFREE 5
+
+/*
+ * This is maximum amount of links allowed for files. For directories,
+ * going over this means setting DIR_NLINK feature.
+ */
+#define EXT2FS_LINK_MAX 65000
+#define EXT2FS_LINK_INF 1 /* link count unknown */
+
+/*
+ * Super block for an ext2fs file system.
+ */
+struct ext2fs {
+ uint32_t e2fs_icount; /* Inode count */
+ uint32_t e2fs_bcount; /* blocks count */
+ uint32_t e2fs_rbcount; /* reserved blocks count */
+ uint32_t e2fs_fbcount; /* free blocks count */
+ uint32_t e2fs_ficount; /* free inodes count */
+ uint32_t e2fs_first_dblock; /* first data block */
+ uint32_t e2fs_log_bsize; /* bsize = 1024*(2^e2fs_log_bsize) */
+ uint32_t e2fs_fsize; /* fragment size */
+ uint32_t e2fs_bpg; /* blocks per group */
+ uint32_t e2fs_fpg; /* frags per group */
+ uint32_t e2fs_ipg; /* inodes per group */
+ uint32_t e2fs_mtime; /* mount time */
+ uint32_t e2fs_wtime; /* write time */
+ uint16_t e2fs_mnt_count; /* mount count */
+ uint16_t e2fs_max_mnt_count; /* max mount count */
+ uint16_t e2fs_magic; /* magic number */
+ uint16_t e2fs_state; /* file system state */
+ uint16_t e2fs_beh; /* behavior on errors */
+ uint16_t e2fs_minrev; /* minor revision level */
+ uint32_t e2fs_lastfsck; /* time of last fsck */
+ uint32_t e2fs_fsckintv; /* max time between fscks */
+ uint32_t e2fs_creator; /* creator OS */
+ uint32_t e2fs_rev; /* revision level */
+ uint16_t e2fs_ruid; /* default uid for reserved blocks */
+ uint16_t e2fs_rgid; /* default gid for reserved blocks */
+ /* EXT2_DYNAMIC_REV superblocks */
+ uint32_t e2fs_first_ino; /* first non-reserved inode */
+ uint16_t e2fs_inode_size; /* size of inode structure */
+ uint16_t e2fs_block_group_nr; /* block grp number of this sblk*/
+ uint32_t e2fs_features_compat; /* compatible feature set */
+ uint32_t e2fs_features_incompat; /* incompatible feature set */
+ uint32_t e2fs_features_rocompat; /* RO-compatible feature set */
+ uint8_t e2fs_uuid[16]; /* 128-bit uuid for volume */
+ char e2fs_vname[16]; /* volume name */
+ char e2fs_fsmnt[64]; /* name mounted on */
+ uint32_t e2fs_algo; /* For compression */
+ uint8_t e2fs_prealloc; /* # of blocks to preallocate */
+ uint8_t e2fs_dir_prealloc; /* # of blocks to preallocate for dir */
+ uint16_t e2fs_reserved_ngdb; /* # of reserved gd blocks for resize */
+
+ /* Additional fields */
+ char e3fs_journal_uuid[16];/* uuid of journal superblock */
+ uint32_t e3fs_journal_inum; /* inode number of journal file */
+ uint32_t e3fs_journal_dev; /* device number of journal file */
+ uint32_t e3fs_last_orphan; /* start of list of inodes to delete */
+ uint32_t e3fs_hash_seed[4]; /* HTREE hash seed */
+ char e3fs_def_hash_version;/* Default hash version to use */
+ char e3fs_jnl_backup_type;
+ uint16_t e3fs_desc_size; /* size of group descriptor */
+ uint32_t e3fs_default_mount_opts;
+ uint32_t e3fs_first_meta_bg; /* First metablock block group */
+ uint32_t e3fs_mkfs_time; /* when the fs was created */
+ uint32_t e3fs_jnl_blks[17]; /* backup of the journal inode */
+ uint32_t e4fs_bcount_hi; /* high bits of blocks count */
+ uint32_t e4fs_rbcount_hi; /* high bits of reserved blocks count */
+ uint32_t e4fs_fbcount_hi; /* high bits of free blocks count */
+ uint16_t e4fs_min_extra_isize; /* all inodes have some bytes */
+ uint16_t e4fs_want_extra_isize;/* inodes must reserve some bytes */
+ uint32_t e4fs_flags; /* miscellaneous flags */
+ uint16_t e4fs_raid_stride; /* RAID stride */
+ uint16_t e4fs_mmpintv; /* seconds to wait in MMP checking */
+ uint64_t e4fs_mmpblk; /* block for multi-mount protection */
+ uint32_t e4fs_raid_stripe_wid; /* blocks on data disks (N * stride) */
+ uint8_t e4fs_log_gpf; /* FLEX_BG group size */
+ uint8_t e4fs_chksum_type; /* metadata checksum algorithm used */
+ uint8_t e4fs_encrypt; /* versioning level for encryption */
+ uint8_t e4fs_reserved_pad;
+ uint64_t e4fs_kbytes_written; /* number of lifetime kilobytes */
+ uint32_t e4fs_snapinum; /* inode number of active snapshot */
+ uint32_t e4fs_snapid; /* sequential ID of active snapshot */
+ uint64_t e4fs_snaprbcount; /* rsvd blocks for active snapshot */
+ uint32_t e4fs_snaplist; /* inode number for on-disk snapshot */
+ uint32_t e4fs_errcount; /* number of file system errors */
+ uint32_t e4fs_first_errtime; /* first time an error happened */
+ uint32_t e4fs_first_errino; /* inode involved in first error */
+ uint64_t e4fs_first_errblk; /* block involved of first error */
+ uint8_t e4fs_first_errfunc[32];/* function where error happened */
+ uint32_t e4fs_first_errline; /* line number where error happened */
+ uint32_t e4fs_last_errtime; /* most recent time of an error */
+ uint32_t e4fs_last_errino; /* inode involved in last error */
+ uint32_t e4fs_last_errline; /* line number where error happened */
+ uint64_t e4fs_last_errblk; /* block involved of last error */
+ uint8_t e4fs_last_errfunc[32];/* function where error happened */
+ uint8_t e4fs_mount_opts[64];
+ uint32_t e4fs_usrquota_inum; /* inode for tracking user quota */
+ uint32_t e4fs_grpquota_inum; /* inode for tracking group quota */
+ uint32_t e4fs_overhead_clusters;/* overhead blocks/clusters */
+ uint32_t e4fs_backup_bgs[2]; /* groups with sparse_super2 SBs */
+ uint8_t e4fs_encrypt_algos[4];/* encryption algorithms in use */
+ uint8_t e4fs_encrypt_pw_salt[16];/* salt used for string2key */
+ uint32_t e4fs_lpf_ino; /* location of the lost+found inode */
+ uint32_t e4fs_proj_quota_inum; /* inode for tracking project quota */
+ uint32_t e4fs_chksum_seed; /* checksum seed */
+ uint32_t e4fs_reserved[98]; /* padding to the end of the block */
+ uint32_t e4fs_sbchksum; /* superblock checksum */
+};
+
+
+/* in-memory data for ext2fs */
+struct m_ext2fs {
+ struct ext2fs e2fs;
+ u_char e2fs_fsmnt[MAXMNTLEN]; /* name mounted on */
+ int8_t e2fs_ronly; /* mounted read-only flag */
+ int8_t e2fs_fmod; /* super block modified flag */
+ int8_t e2fs_uhash; /* 3 if hash should be signed, 0 if not */
+ int32_t e2fs_bsize; /* block size */
+ int32_t e2fs_bshift; /* ``lblkno'' calc of logical blkno */
+ int32_t e2fs_bmask; /* ``blkoff'' calc of blk offsets */
+ int64_t e2fs_qbmask; /* ~fs_bmask - for use with quad size */
+ int32_t e2fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
+ int32_t e2fs_ncg; /* number of cylinder groups */
+ int32_t e2fs_ngdb; /* number of group descriptor blocks */
+ int32_t e2fs_ipb; /* number of inodes per block */
+ int32_t e2fs_itpg; /* number of inode table blocks per group */
+ uint8_t e2fs_group_desc_shift; /* binary log group desc size */
+ struct ext2_gd *e2fs_gd; /* group descriptors (data not byteswapped) */
+};
+
+
+
+/*
+ * Filesystem identification
+ */
+#define E2FS_MAGIC 0xef53 /* the ext2fs magic number */
+#define E2FS_REV0 0 /* GOOD_OLD revision */
+#define E2FS_REV1 1 /* Support compat/incompat features */
+
+/* compatible/incompatible features */
+#define EXT2F_COMPAT_PREALLOC 0x0001
+#define EXT2F_COMPAT_AFS 0x0002
+#define EXT2F_COMPAT_HASJOURNAL 0x0004
+#define EXT2F_COMPAT_EXTATTR 0x0008
+#define EXT2F_COMPAT_RESIZE 0x0010
+#define EXT2F_COMPAT_DIRHASHINDEX 0x0020
+#define EXT2F_COMPAT_SPARSESUPER2 0x0200
+#define EXT2F_COMPAT_BITS \
+ "\20" \
+ "\12COMPAT_SPARSESUPER2" \
+ "\11" \
+ "\10" \
+ "\07" \
+ "\06COMPAT_DIRHASHINDEX" \
+ "\05COMPAT_RESIZE" \
+ "\04COMPAT_EXTATTR" \
+ "\03COMPAT_HASJOURNAL" \
+ "\02COMPAT_AFS" \
+ "\01COMPAT_PREALLOC"
+
+#define EXT2F_ROCOMPAT_SPARSESUPER 0x0001
+#define EXT2F_ROCOMPAT_LARGEFILE 0x0002
+#define EXT2F_ROCOMPAT_BTREE_DIR 0x0004
+#define EXT2F_ROCOMPAT_HUGE_FILE 0x0008
+#define EXT2F_ROCOMPAT_GDT_CSUM 0x0010
+#define EXT2F_ROCOMPAT_DIR_NLINK 0x0020
+#define EXT2F_ROCOMPAT_EXTRA_ISIZE 0x0040
+#define EXT2F_ROCOMPAT_QUOTA 0x0100
+#define EXT2F_ROCOMPAT_BIGALLOC 0x0200
+#define EXT2F_ROCOMPAT_METADATA_CKSUM 0x0400
+#define EXT2F_ROCOMPAT_READONLY 0x1000
+#define EXT2F_ROCOMPAT_PROJECT 0x2000
+#define EXT2F_ROCOMPAT_BITS \
+ "\20" \
+ "\16ROCOMPAT_PROJECT" \
+ "\15ROCOMPAT_READONLY" \
+ "\14?" \
+ "\13ROCOMPAT_METADATA_CKSUM" \
+ "\12ROCOMPAT_BIGALLOC" \
+ "\11ROCOMPAT_QUOTA" \
+ "\10?" \
+ "\07ROCOMPAT_EXTRA_ISIZE" \
+ "\06ROCOMPAT_DIR_NLINK" \
+ "\05ROCOMPAT_GDT_CSUM" \
+ "\04ROCOMPAT_HUGE_FILE" \
+ "\03ROCOMPAT_BTREE_DIR" \
+ "\02ROCOMPAT_LARGEFILE" \
+ "\01ROCOMPAT_SPARSESUPER"
+
+#define EXT2F_INCOMPAT_COMP 0x0001
+#define EXT2F_INCOMPAT_FTYPE 0x0002
+#define EXT2F_INCOMPAT_REPLAY_JOURNAL 0x0004
+#define EXT2F_INCOMPAT_USES_JOURNAL 0x0008
+#define EXT2F_INCOMPAT_META_BG 0x0010
+#define EXT2F_INCOMPAT_EXTENTS 0x0040
+#define EXT2F_INCOMPAT_64BIT 0x0080
+#define EXT2F_INCOMPAT_MMP 0x0100
+#define EXT2F_INCOMPAT_FLEX_BG 0x0200
+#define EXT2F_INCOMPAT_EA_INODE 0x0400
+#define EXT2F_INCOMPAT_DIRDATA 0x1000
+#define EXT2F_INCOMPAT_CSUM_SEED 0x2000
+#define EXT2F_INCOMPAT_LARGEDIR 0x4000
+#define EXT2F_INCOMPAT_INLINE_DATA 0x8000
+#define EXT2F_INCOMPAT_ENCRYPT 0x10000
+#define EXT2F_INCOMPAT_BITS \
+ "\20" \
+ "\021INCOMPAT_ENCRYPT" \
+ "\020INCOMPAT_INLINE_DATA" \
+ "\017INCOMPAT_LARGEDIR" \
+ "\016INCOMPAT_CSUM_SEED" \
+ "\015INCOMPAT_DIRDATA" \
+ "\014?" \
+ "\013INCOMPAT_EA_INODE" \
+ "\012INCOMPAT_FLEX_BG" \
+ "\011INCOMPAT_MMP" \
+ "\010INCOMPAT_64BIT" \
+ "\07INCOMPAT_EXTENTS" \
+ "\05INCOMPAT_META_BG" \
+ "\04INCOMPAT_USES_JOURNAL" \
+ "\03INCOMPAT_REPLAY_JOURNAL" \
+ "\02INCOMPAT_FTYPE" \
+ "\01INCOMPAT_COMP"
+
+/*
+ * Features supported in this implementation
+ *
+ * We support the following REV1 features:
+ * - EXT2F_ROCOMPAT_SPARSESUPER
+ * superblock backups stored only in cg_has_sb(bno) groups
+ * - EXT2F_ROCOMPAT_LARGEFILE
+ * use e2di_size_high in struct ext2fs_dinode to store
+ * upper 32bit of size for >2GB files
+ * - EXT2F_INCOMPAT_FTYPE
+ * store file type to e2d_type in struct ext2fs_direct
+ * (on REV0 e2d_namlen is uint16_t and no e2d_type, like ffs)
+ */
+#define EXT2F_COMPAT_SUPP 0x0000
+#define EXT2F_ROCOMPAT_SUPP (EXT2F_ROCOMPAT_SPARSESUPER \
+ | EXT2F_ROCOMPAT_LARGEFILE \
+ | EXT2F_ROCOMPAT_HUGE_FILE \
+ | EXT2F_ROCOMPAT_EXTRA_ISIZE \
+ | EXT2F_ROCOMPAT_DIR_NLINK \
+ | EXT2F_ROCOMPAT_GDT_CSUM)
+#define EXT2F_INCOMPAT_SUPP (EXT2F_INCOMPAT_FTYPE \
+ | EXT2F_INCOMPAT_EXTENTS \
+ | EXT2F_INCOMPAT_FLEX_BG \
+ | EXT2F_INCOMPAT_64BIT)
+
+/*
+ * Feature set definitions
+ */
+#define EXT2F_HAS_COMPAT_FEATURE(fs, feature) \
+ ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && \
+ ((fs)->e2fs.e2fs_features_compat & (feature)) != 0)
+
+#define EXT2F_HAS_ROCOMPAT_FEATURE(fs, feature) \
+ ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && \
+ ((fs)->e2fs.e2fs_features_rocompat & (feature)) != 0)
+
+#define EXT2F_HAS_INCOMPAT_FEATURE(fs, feature) \
+ ((fs)->e2fs.e2fs_rev >= E2FS_REV1 && \
+ ((fs)->e2fs.e2fs_features_incompat & (feature)) != 0)
+
+
+/*
+ * Definitions of behavior on errors
+ */
+#define E2FS_BEH_CONTINUE 1 /* continue operation */
+#define E2FS_BEH_READONLY 2 /* remount fs read only */
+#define E2FS_BEH_PANIC 3 /* cause panic */
+#define E2FS_BEH_DEFAULT E2FS_BEH_CONTINUE
+
+/*
+ * OS identification
+ */
+#define E2FS_OS_LINUX 0
+#define E2FS_OS_HURD 1
+#define E2FS_OS_MASIX 2
+#define E2FS_OS_FREEBSD 3
+#define E2FS_OS_LITES 4
+
+/*
+ * Filesystem clean flags
+ */
+#define E2FS_ISCLEAN 0x01
+#define E2FS_ERRORS 0x02
+
+/* ext2 file system block group descriptor */
+
+struct ext2_gd {
+ uint32_t ext2bgd_b_bitmap; /* blocks bitmap block */
+ uint32_t ext2bgd_i_bitmap; /* inodes bitmap block */
+ uint32_t ext2bgd_i_tables; /* first inodes table block */
+ uint16_t ext2bgd_nbfree; /* number of free blocks */
+ uint16_t ext2bgd_nifree; /* number of free inodes */
+ uint16_t ext2bgd_ndirs; /* number of directories */
+
+ /*
+ * Following only valid when either GDT_CSUM (AKA uninit_bg)
+ * or METADATA_CKSUM feature is on
+ */
+ uint16_t ext2bgd_flags; /* ext4 bg flags (INODE_UNINIT, ...)*/
+ uint32_t ext2bgd_exclude_bitmap_lo; /* snapshot exclude bitmap */
+ uint16_t ext2bgd_block_bitmap_csum_lo; /* Low block bitmap checksum */
+ uint16_t ext2bgd_inode_bitmap_csum_lo; /* Low inode bitmap checksum */
+ uint16_t ext2bgd_itable_unused_lo; /* Low unused inode offset */
+ uint16_t ext2bgd_checksum; /* Group desc checksum */
+
+ uint32_t ext2bgd_b_bitmap_hi; /* blocks bitmap block (high bits) */
+ uint32_t ext2bgd_i_bitmap_hi; /* inodes bitmap block (high bits) */
+ uint32_t ext2bgd_i_tables_hi; /* inodes table block (high bits) */
+ uint16_t ext2bgd_nbfree_hi; /* number of free blocks (high bits) */
+ uint16_t ext2bgd_nifree_hi; /* number of free inodes (high bits) */
+ uint16_t ext2bgd_ndirs_hi; /* number of directories (high bits) */
+ uint16_t reserved_hi;
+ uint32_t reserved2_hi[3];
+};
+
+#define E2FS_REV0_GD_SIZE (sizeof(struct ext2_gd) / 2) /* 32 */
+#define E2FS_BG_INODE_UNINIT 0x0001 /* Inode bitmap not used/initialized */
+#define E2FS_BG_BLOCK_UNINIT 0x0002 /* Block bitmap not used/initialized */
+#define E2FS_BG_INODE_ZEROED 0x0004 /* On-disk inode table initialized */
+
+#define E2FS_HAS_GD_CSUM(fs) \
+ EXT2F_HAS_ROCOMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM|EXT2F_ROCOMPAT_METADATA_CKSUM) != 0
+
+/*
+ * If the EXT2F_ROCOMPAT_SPARSESUPER flag is set, the cylinder group has a
+ * copy of the super and cylinder group descriptors blocks only if it's
+ * 1, a power of 3, 5 or 7
+ */
+
+static __inline int cg_has_sb(int) __unused;
+static __inline int
+cg_has_sb(int i)
+{
+ int a3, a5, a7;
+
+ if (i == 0 || i == 1)
+ return 1;
+ for (a3 = 3, a5 = 5, a7 = 7;
+ a3 <= i || a5 <= i || a7 <= i;
+ a3 *= 3, a5 *= 5, a7 *= 7)
+ if (i == a3 || i == a5 || i == a7)
+ return 1;
+ return 0;
+}
+
+/* EXT2FS metadatas are stored in little-endian byte order. These macros
+ * helps reading theses metadatas
+ */
+
+#ifdef fsck_little_endian
+# define h2fs16(x) (x)
+# define h2fs32(x) (x)
+# define h2fs64(x) (x)
+# define fs2h16(x) (x)
+# define fs2h32(x) (x)
+# define fs2h64(x) (x)
+# define e2fs_sbload(old, new) memcpy((new), (old), SBSIZE)
+# define e2fs_sbsave(old, new) memcpy((new), (old), SBSIZE)
+#else
+void e2fs_sb_bswap(struct ext2fs *, struct ext2fs *);
+# define h2fs16(x) bswap16(x)
+# define h2fs32(x) bswap32(x)
+# define h2fs64(x) bswap64(x)
+# define fs2h16(x) bswap16(x)
+# define fs2h32(x) bswap32(x)
+# define fs2h64(x) bswap64(x)
+# define e2fs_sbload(old, new) e2fs_sb_bswap((old), (new))
+# define e2fs_sbsave(old, new) e2fs_sb_bswap((old), (new))
+#endif
+
+#ifndef _KERNEL /* XXX */
+/* Group descriptors are not byte swapped */
+#define e2fs_cgload(old, new, size) memcpy((new), (old), (size))
+#define e2fs_cgsave(old, new, size) memcpy((new), (old), (size))
+#endif
+
+/*
+ * Turn file system block numbers into disk block addresses.
+ * This maps file system blocks to device size blocks.
+ */
+#define EXT2_FSBTODB(fs, b) ((int64_t)(b) << (fs)->e2fs_fsbtodb)
+#define EXT2_FSBTODB64(fs, b, b_hi) \
+ (((((uint64_t)(b_hi)) << 32) | (b)) << (fs)->e2fs_fsbtodb)
+#define EXT2_FSBTODB64OFF(fs, b, b_hi, off) \
+ ((((((uint64_t)(b_hi)) << 32) | (b)) + (off)) << (fs)->e2fs_fsbtodb)
+#define EXT2_DBTOFSB(fs, b) ((b) >> (fs)->e2fs_fsbtodb)
+
+/*
+ * Macros for handling inode numbers:
+ * inode number to file system block offset.
+ * inode number to cylinder group number.
+ * inode number to file system block address.
+ */
+#define ino_to_cg(fs, x) (((x) - 1) / (fs)->e2fs.e2fs_ipg)
+#define _e2fs_gd(fs, x) (fs)->e2fs_gd[ino_to_cg((fs), (x))]
+#define ino_to_fsba(fs, x) \
+ (fs2h32(_e2fs_gd(fs, x).ext2bgd_i_tables) + \
+ (((uint64_t)fs2h32(_e2fs_gd(fs, x).ext2bgd_i_tables_hi)) << 32) + \
+ (((x) - 1) % (fs)->e2fs.e2fs_ipg) / (fs)->e2fs_ipb)
+#define ino_to_fsbo(fs, x) (((x) - 1) % (fs)->e2fs_ipb)
+
+/*
+ * Give cylinder group number for a file system block.
+ * Give cylinder group block number for a file system block.
+ */
+#define dtog(fs, d) (((d) - (fs)->e2fs.e2fs_first_dblock) / (fs)->e2fs.e2fs_fpg)
+#define dtogd(fs, d) \
+ (((d) - (fs)->e2fs.e2fs_first_dblock) % (fs)->e2fs.e2fs_fpg)
+
+/*
+ * The following macros optimize certain frequently calculated
+ * quantities by using shifts and masks in place of divisions
+ * modulos and multiplications.
+ */
+#define ext2_blkoff(fs, loc) /* calculates (loc % fs->e2fs_bsize) */ \
+ ((loc) & (fs)->e2fs_qbmask)
+#define ext2_lblktosize(fs, blk) /* calculates (blk * fs->e2fs_bsize) */ \
+ ((blk) << (fs)->e2fs_bshift)
+#define ext2_lblkno(fs, loc) /* calculates (loc / fs->e2fs_bsize) */ \
+ ((loc) >> (fs)->e2fs_bshift)
+#define ext2_blkroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
+ (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
+#define ext2_fragroundup(fs, size) /* calculates roundup(size, fs->e2fs_bsize) */ \
+ (((size) + (fs)->e2fs_qbmask) & (fs)->e2fs_bmask)
+/*
+ * Determine the number of available frags given a
+ * percentage to hold in reserve.
+ */
+#define freespace(fs) \
+ ((fs)->e2fs.e2fs_fbcount - (fs)->e2fs.e2fs_rbcount)
+
+/*
+ * Number of indirects in a file system block.
+ */
+#define EXT2_NINDIR(fs) ((fs)->e2fs_bsize / sizeof(uint32_t))
+
+#endif /* !_UFS_EXT2FS_EXT2FS_H_ */
diff --git a/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs_dinode.h b/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs_dinode.h
@@ -0,0 +1,286 @@
+/* $NetBSD: ext2fs_dinode.h,v 1.37 2017/01/13 18:04:36 christos Exp $ */
+
+/*
+ * Copyright (c) 1982, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)dinode.h 8.6 (Berkeley) 9/13/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)dinode.h 8.6 (Berkeley) 9/13/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+#ifndef _UFS_EXT2FS_EXT2FS_DINODE_H_
+#define _UFS_EXT2FS_EXT2FS_DINODE_H_
+
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#include <stddef.h> /* for offsetof */
+#endif
+
+#include <sys/stat.h>
+
+/*
+ * The root inode is the root of the file system. Inode 0 can't be used for
+ * normal purposes and bad blocks are normally linked to inode 1, thus
+ * the root inode is 2.
+ * Inode 3 to 10 are reserved in ext2fs.
+ */
+#define EXT2_BADBLKINO ((ino_t)1)
+#define EXT2_ROOTINO ((ino_t)2)
+#define EXT2_ACLIDXINO ((ino_t)3)
+#define EXT2_ACLDATAINO ((ino_t)4)
+#define EXT2_BOOTLOADERINO ((ino_t)5)
+#define EXT2_UNDELDIRINO ((ino_t)6)
+#define EXT2_RESIZEINO ((ino_t)7)
+#define EXT2_JOURNALINO ((ino_t)8)
+#define EXT2_FIRSTINO ((ino_t)11)
+
+/*
+ * A dinode contains all the meta-data associated with a UFS file.
+ * This structure defines the on-disk format of a dinode. Since
+ * this structure describes an on-disk structure, all its fields
+ * are defined by types with precise widths.
+ */
+
+/*
+ * XXX these are the same values as UFS_NDADDR/UFS_NIADDR and it is
+ * far from clear that there isn't code that relies on them being the
+ * same.
+ */
+#define EXT2FS_NDADDR 12 /* Direct addresses in inode. */
+#define EXT2FS_NIADDR 3 /* Indirect addresses in inode. */
+
+#define EXT2_MAXSYMLINKLEN ((EXT2FS_NDADDR+EXT2FS_NIADDR) * sizeof (uint32_t))
+#define E2MAXSYMLINKLEN EXT2_MAXSYMLINKLEN
+
+struct ext2fs_dinode {
+ uint16_t e2di_mode; /* 0: IFMT, permissions; see below. */
+ uint16_t e2di_uid; /* 2: Owner UID */
+ uint32_t e2di_size; /* 4: Size (in bytes) */
+ uint32_t e2di_atime; /* 8: Access time */
+ uint32_t e2di_ctime; /* 12: Create time */
+ uint32_t e2di_mtime; /* 16: Modification time */
+ uint32_t e2di_dtime; /* 20: Deletion time */
+ uint16_t e2di_gid; /* 24: Owner GID */
+ uint16_t e2di_nlink; /* 26: File link count */
+ uint32_t e2di_nblock; /* 28: Blocks count */
+ uint32_t e2di_flags; /* 32: Status flags (chflags) */
+ uint32_t e2di_version; /* 36: was reserved1 */
+ uint32_t e2di_blocks[EXT2FS_NDADDR+EXT2FS_NIADDR];
+ /* 40: disk blocks */
+ uint32_t e2di_gen; /* 100: generation number */
+ uint32_t e2di_facl; /* 104: file ACL (ext3) */
+ uint32_t e2di_size_high; /* 108: Size (in bytes) high */
+ uint32_t e2di_obso_faddr;/* 112: obsolete fragment address (ext2) */
+ uint16_t e2di_nblock_high; /* 116: Blocks count bits 47:32 (ext4) */
+ uint16_t e2di_facl_high; /* 118: file ACL bits 47:32 (ext4/64bit) */
+ uint16_t e2di_uid_high; /* 120: Owner UID top 16 bits (ext4) */
+ uint16_t e2di_gid_high; /* 122: Owner GID top 16 bits (ext4) */
+ uint16_t e2di_checksum_low; /* 124: crc LE (not implemented) (ext4) */
+ uint16_t e2di_reserved; /* 126: reserved */
+ uint16_t e2di_extra_isize; /* 128: inode extra size (over 128) actually used (ext4) */
+ uint16_t e2di_checksum_high; /* 130: crc BE (not implemented) (ext4) */
+ uint32_t e2di_ctime_extra; /* 132: ctime (nsec << 2 | high epoch) (ext4) */
+ uint32_t e2di_mtime_extra; /* 136: mtime (nsec << 2 | high epoch) (ext4) */
+ uint32_t e2di_atime_extra; /* 140: atime (nsec << 2 | high epoch) (ext4) */
+ uint32_t e2di_crtime; /* 144: creation time (epoch) (ext4) */
+ uint32_t e2di_crtime_extra; /* 148: creation time (nsec << 2 | high epoch) (ext4) */
+ uint32_t e2di_version_high; /* 152: version high (ext4) */
+ uint32_t e2di_projid; /* 156: project id (not implemented) (ext4) */
+};
+
+#define i_e2fs_mode i_din.e2fs_din->e2di_mode
+#define i_e2fs_uid i_din.e2fs_din->e2di_uid
+#define i_e2fs_size i_din.e2fs_din->e2di_size
+#define i_e2fs_atime i_din.e2fs_din->e2di_atime
+#define i_e2fs_ctime i_din.e2fs_din->e2di_ctime
+#define i_e2fs_mtime i_din.e2fs_din->e2di_mtime
+#define i_e2fs_dtime i_din.e2fs_din->e2di_dtime
+#define i_e2fs_gid i_din.e2fs_din->e2di_gid
+#define i_e2fs_nlink i_din.e2fs_din->e2di_nlink
+#define i_e2fs_nblock i_din.e2fs_din->e2di_nblock
+#define i_e2fs_flags i_din.e2fs_din->e2di_flags
+#define i_e2fs_version i_din.e2fs_din->e2di_version
+#define i_e2fs_blocks i_din.e2fs_din->e2di_blocks
+#define i_e2fs_rdev i_din.e2fs_din->e2di_rdev
+#define i_e2fs_gen i_din.e2fs_din->e2di_gen
+#define i_e2fs_facl i_din.e2fs_din->e2di_facl
+#define i_e2fs_nblock_high i_din.e2fs_din->e2di_nblock_high
+#define i_e2fs_facl_high i_din.e2fs_din->e2di_facl_high
+#define i_e2fs_uid_high i_din.e2fs_din->e2di_uid_high
+#define i_e2fs_gid_high i_din.e2fs_din->e2di_gid_high
+
+/* File permissions. */
+#define EXT2_IEXEC 0000100 /* Executable. */
+#define EXT2_IWRITE 0000200 /* Writable. */
+#define EXT2_IREAD 0000400 /* Readable. */
+#define EXT2_ISVTX 0001000 /* Sticky bit. */
+#define EXT2_ISGID 0002000 /* Set-gid. */
+#define EXT2_ISUID 0004000 /* Set-uid. */
+
+/* File types. */
+#define EXT2_IFMT 0170000 /* Mask of file type. */
+#define EXT2_IFIFO 0010000 /* Named pipe (fifo). */
+#define EXT2_IFCHR 0020000 /* Character device. */
+#define EXT2_IFDIR 0040000 /* Directory file. */
+#define EXT2_IFBLK 0060000 /* Block device. */
+#define EXT2_IFREG 0100000 /* Regular file. */
+#define EXT2_IFLNK 0120000 /* Symbolic link. */
+#define EXT2_IFSOCK 0140000 /* UNIX domain socket. */
+
+/* file flags */
+#define EXT2_SECRM 0x00000001 /* Secure deletion */
+#define EXT2_UNRM 0x00000002 /* Undelete */
+#define EXT2_COMPR 0x00000004 /* Compress file */
+#define EXT2_SYNC 0x00000008 /* Synchronous updates */
+#define EXT2_IMMUTABLE 0x00000010 /* Immutable file */
+#define EXT2_APPEND 0x00000020 /* writes to file may only append */
+#define EXT2_NODUMP 0x00000040 /* do not dump file */
+#define EXT2_NOATIME 0x00000080 /* do not update atime */
+#define EXT2_INDEX 0x00001000 /* hash-indexed directory */
+#define EXT2_IMAGIC 0x00002000 /* AFS directory */
+#define EXT2_JOURNAL_DATA 0x00004000 /* file data should be journaled */
+#define EXT2_NOTAIL 0x00008000 /* file tail should not be merged */
+#define EXT2_DIRSYNC 0x00010000 /* dirsync behaviour */
+#define EXT2_TOPDIR 0x00020000 /* Top of directory hierarchies*/
+#define EXT2_HUGE_FILE 0x00040000 /* Set to each huge file */
+#define EXT2_EXTENTS 0x00080000 /* Inode uses extents */
+#define EXT2_EA_INODE 0x00200000 /* Inode used for large EA */
+#define EXT2_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */
+#define EXT2_INLINE_DATA 0x10000000 /* Inode has inline data */
+#define EXT2_PROJINHERIT 0x20000000 /* Children inherit project ID */
+
+/* Size of on-disk inode. */
+#define EXT2_REV0_DINODE_SIZE 128U
+#define EXT2_DINODE_SIZE(fs) ((fs)->e2fs.e2fs_rev > E2FS_REV0 ? \
+ (fs)->e2fs.e2fs_inode_size : \
+ EXT2_REV0_DINODE_SIZE)
+#define EXT2_DINODE_FITS(dinode, field, isize) (\
+ (isize > EXT2_REV0_DINODE_SIZE) \
+ && ((EXT2_REV0_DINODE_SIZE + (dinode)->e2di_extra_isize) >= offsetof(struct ext2fs_dinode, field) + sizeof((dinode)->field)) \
+ )
+
+/*
+ * Time encoding
+ * Lower two bits of extra field are extra high bits for epoch; unfortunately still, Linux kernels treat 11 there as 00 for compatibility
+ * Rest of extra fields are nanoseconds
+ */
+static __inline void
+ext2fs_dinode_time_get(struct timespec *ts, uint32_t epoch, uint32_t extra)
+{
+ ts->tv_sec = (signed) epoch;
+
+ if (extra) {
+ uint64_t epoch_bits = extra & 0x3;
+ /* XXX compatibility with linux kernel < 4.20 */
+ if (epoch_bits == 3 && ts->tv_sec < 0)
+ epoch_bits = 0;
+
+ ts->tv_sec |= epoch_bits << 32;
+
+ ts->tv_nsec = extra >> 2;
+ } else {
+ ts->tv_nsec = 0;
+ }
+}
+#define EXT2_DINODE_TIME_GET(ts, dinode, field, isize) \
+ ext2fs_dinode_time_get(ts, (dinode)->field, \
+ EXT2_DINODE_FITS(dinode, field ## _extra, isize) \
+ ? (dinode)->field ## _extra : 0 \
+ )
+
+static __inline void
+ext2fs_dinode_time_set(const struct timespec *ts, uint32_t *epoch, uint32_t *extra)
+{
+ *epoch = (int32_t) ts->tv_sec;
+
+ if (extra) {
+ uint32_t epoch_bits = (ts->tv_sec >> 32) & 0x3;
+
+ *extra = (ts->tv_nsec << 2) | epoch_bits;
+ }
+}
+#define EXT2_DINODE_TIME_SET(ts, dinode, field, isize) \
+ ext2fs_dinode_time_set(ts, &(dinode)->field, \
+ EXT2_DINODE_FITS(dinode, field ## _extra, isize) \
+ ? &(dinode)->field ## _extra : NULL \
+ )
+
+/*
+ * The e2di_blocks fields may be overlaid with other information for
+ * file types that do not have associated disk storage. Block
+ * and character devices overlay the first data block with their
+ * dev_t value. Short symbolic links place their path in the
+ * di_db area.
+ */
+
+#define e2di_rdev e2di_blocks[0]
+#define e2di_shortlink e2di_blocks
+
+/* e2fs needs byte swapping on big-endian systems */
+#if BYTE_ORDER == LITTLE_ENDIAN
+# define e2fs_iload(old, new, isize) \
+ memcpy((new),(old),(isize))
+# define e2fs_isave(old, new, isize) \
+ memcpy((new),(old),(isize))
+#else
+void e2fs_i_bswap(struct ext2fs_dinode *, struct ext2fs_dinode *, size_t);
+# define e2fs_iload(old, new, isize) e2fs_i_bswap((old), (new), (isize))
+# define e2fs_isave(old, new, isize) e2fs_i_bswap((old), (new), (isize))
+#endif
+
+#endif /* !_UFS_EXT2FS_EXT2FS_DINODE_H_ */
diff --git a/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs_dir.h b/cmd/extra/fsck.ext4/ufs/ext2fs/ext2fs_dir.h
@@ -0,0 +1,231 @@
+/* $NetBSD: ext2fs_dir.h,v 1.23 2024/03/10 17:36:33 christos Exp $ */
+
+/*
+ * Copyright (c) 1982, 1986, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)dir.h 8.4 (Berkeley) 8/10/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @(#)dir.h 8.4 (Berkeley) 8/10/94
+ * Modified for ext2fs by Manuel Bouyer.
+ */
+
+#ifndef _UFS_EXT2FS_EXT2FS_DIR_H_
+#define _UFS_EXT2FS_EXT2FS_DIR_H_
+
+#include <dirent.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+
+/*
+ * Theoretically, directories can be more than 2Gb in length, however, in
+ * practice this seems unlikely. So, we define the type doff_t as a 32-bit
+ * quantity to keep down the cost of doing lookup on a 32-bit machine.
+ */
+#define doff_t int32_t
+#define EXT2FS_MAXDIRSIZE INT32_MAX
+
+/*
+ * A directory consists of some number of blocks of e2fs_bsize bytes.
+ *
+ * Each block contains some number of directory entry
+ * structures, which are of variable length. Each directory entry has
+ * a struct direct at the front of it, containing its inode number,
+ * the length of the entry, and the length of the name contained in
+ * the entry. These are followed by the name padded to a 4 byte boundary
+ * with null bytes. All names are guaranteed null terminated.
+ * The maximum length of a name in a directory is EXT2FS_MAXNAMLEN.
+ *
+ * The macro EXT2FS_DIRSIZ(fmt, dp) gives the amount of space required to
+ * represent a directory entry. Free space in a directory is represented by
+ * entries which have dp->e2d_reclen > DIRSIZ(fmt, dp). All d2fs_bsize bytes
+ * in a directory block are claimed by the directory entries. This
+ * usually results in the last entry in a directory having a large
+ * dp->e2d_reclen. When entries are deleted from a directory, the
+ * space is returned to the previous entry in the same directory
+ * block by increasing its dp->e2d_reclen. If the first entry of
+ * a directory block is free, then its dp->e2d_ino is set to 0.
+ * Entries other than the first in a directory do not normally have
+ * dp->e2d_ino set to 0.
+ * Ext2 rev 0 has a 16 bits e2d_namlen. For Ext2 vev 1 this has been split
+ * into a 8 bits e2d_namlen and 8 bits e2d_type (looks like ffs, isnt't it ? :)
+ * It's safe to use this for rev 0 as well because all ext2 are little-endian.
+ */
+
+#define EXT2FS_MAXNAMLEN 255
+
+struct ext2fs_direct {
+ uint32_t e2d_ino; /* inode number of entry */
+ uint16_t e2d_reclen; /* length of this record */
+ uint8_t e2d_namlen; /* length of string in d_name */
+ uint8_t e2d_type; /* file type */
+ char e2d_name[EXT2FS_MAXNAMLEN];/* name with length<=EXT2FS_MAXNAMLEN */
+};
+
+enum ext2fs_slotstatus {
+ NONE,
+ COMPACT,
+ FOUND
+};
+
+struct ext2fs_searchslot {
+ enum ext2fs_slotstatus slotstatus;
+ doff_t slotoffset; /* offset of area with free space */
+ int slotsize; /* size of area at slotoffset */
+ int slotfreespace; /* amount of space free in slot */
+ int slotneeded; /* sizeof the entry we are seeking */
+};
+
+/* Ext2 directory file types (not the same as FFS. Sigh.) */
+#define EXT2_FT_UNKNOWN 0
+#define EXT2_FT_REG_FILE 1
+#define EXT2_FT_DIR 2
+#define EXT2_FT_CHRDEV 3
+#define EXT2_FT_BLKDEV 4
+#define EXT2_FT_FIFO 5
+#define EXT2_FT_SOCK 6
+#define EXT2_FT_SYMLINK 7
+
+#define EXT2_FT_MAX 8
+
+#define E2IFTODT(mode) (((mode) & 0170000) >> 12)
+
+static __inline uint8_t inot2ext2dt(uint16_t) __unused;
+static __inline uint8_t
+inot2ext2dt(uint16_t type)
+{
+
+ switch (type) {
+ case E2IFTODT(EXT2_IFIFO):
+ return EXT2_FT_FIFO;
+ case E2IFTODT(EXT2_IFCHR):
+ return EXT2_FT_CHRDEV;
+ case E2IFTODT(EXT2_IFDIR):
+ return EXT2_FT_DIR;
+ case E2IFTODT(EXT2_IFBLK):
+ return EXT2_FT_BLKDEV;
+ case E2IFTODT(EXT2_IFREG):
+ return EXT2_FT_REG_FILE;
+ case E2IFTODT(EXT2_IFLNK):
+ return EXT2_FT_SYMLINK;
+ case E2IFTODT(EXT2_IFSOCK):
+ return EXT2_FT_SOCK;
+ default:
+ return 0;
+ }
+}
+
+static __inline uint8_t ext2dt2dt(uint8_t) __unused;
+static __inline uint8_t
+ext2dt2dt(uint8_t type)
+{
+ switch (type) {
+ case EXT2_FT_REG_FILE:
+ return DT_REG;
+ case EXT2_FT_DIR:
+ return DT_DIR;
+ case EXT2_FT_CHRDEV:
+ return DT_CHR;
+ case EXT2_FT_BLKDEV:
+ return DT_BLK;
+ case EXT2_FT_FIFO:
+ return DT_FIFO;
+ case EXT2_FT_SOCK:
+ return DT_SOCK;
+ case EXT2_FT_SYMLINK:
+ return DT_LNK;
+ default:
+ return DT_UNKNOWN;
+ }
+}
+
+/*
+ * The EXT2FS_DIRSIZ macro gives the minimum record length which will hold
+ * the directory entryfor a name len "len" (without the terminating null byte).
+ * This requires the amount of space in struct direct
+ * without the d_name field, plus enough space for the name without a
+ * terminating null byte, rounded up to a 4 byte boundary.
+ */
+#define EXT2FS_DIRSIZ(len) roundup2(8 + len, 4)
+
+/*
+ * Template for manipulating directories. Should use struct direct's,
+ * but the name field is EXT2FS_MAXNAMLEN - 1, and this just won't do.
+ */
+struct ext2fs_dirtemplate {
+ uint32_t dot_ino;
+ int16_t dot_reclen;
+ uint8_t dot_namlen;
+ uint8_t dot_type;
+ char dot_name[4]; /* must be multiple of 4 */
+ uint32_t dotdot_ino;
+ int16_t dotdot_reclen;
+ uint8_t dotdot_namlen;
+ uint8_t dotdot_type;
+ char dotdot_name[4]; /* ditto */
+};
+
+/*
+ * EXT2_DIR_PAD defines the directory entries boundaries
+ *
+ * NOTE: It must be a multiple of 4
+ */
+#define EXT2_DIR_PAD 4
+#define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
+#define EXT2_DIR_REC_LEN(namelen) \
+ (((namelen) + 8 + EXT2_DIR_ROUND) & ~EXT2_DIR_ROUND)
+
+#endif /* !_UFS_EXT2FS_EXT2FS_DIR_H_ */
diff --git a/cmd/extra/fsck.ext4/utilities.c b/cmd/extra/fsck.ext4/utilities.c
@@ -0,0 +1,444 @@
+/* $NetBSD: utilities.c,v 1.23 2013/06/23 02:06:04 dholland Exp $ */
+
+/*
+ * Copyright (c) 1980, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1997 Manuel Bouyer.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef lint
+#if 0
+static char sccsid[] = "@(#)utilities.c 8.1 (Berkeley) 6/5/93";
+#else
+__RCSID("$NetBSD: utilities.c,v 1.23 2013/06/23 02:06:04 dholland Exp $");
+#endif
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <ufs/ext2fs/ext2fs_dinode.h>
+#include <ufs/ext2fs/ext2fs_dir.h>
+#include <ufs/ext2fs/ext2fs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include "fsutil.h"
+#include "fsck.h"
+#include "extern.h"
+#include "exitvalues.h"
+
+extern int unresolved;
+
+long diskreads, totalreads; /* Disk cache statistics */
+
+
+int
+ftypeok(struct ext2fs_dinode *dp)
+{
+ switch (fs2h16(dp->e2di_mode) & IFMT) {
+
+ case IFDIR:
+ case IFREG:
+ case IFBLK:
+ case IFCHR:
+ case IFLNK:
+ case IFSOCK:
+ case IFIFO:
+ return (1);
+
+ default:
+ if (debug)
+ printf("bad file type 0%o\n", fs2h16(dp->e2di_mode));
+ return (0);
+ }
+}
+
+int
+reply(const char *question)
+{
+ int persevere;
+ char c;
+
+ if (preen)
+ pfatal("INTERNAL ERROR: GOT TO reply()");
+ persevere = !strcmp(question, "CONTINUE");
+ printf("\n");
+ if (!persevere && (nflag || fswritefd < 0)) {
+ unresolved = 1;
+ printf("%s? no\n\n", question);
+ return (0);
+ }
+ if (yflag || (persevere && nflag)) {
+ printf("%s? yes\n\n", question);
+ return (1);
+ }
+ do {
+ printf("%s? [yn] ", question);
+ (void) fflush(stdout);
+ c = getc(stdin);
+ while (c != '\n' && getc(stdin) != '\n')
+ if (feof(stdin))
+ return (0);
+ } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
+ printf("\n");
+ if (c == 'y' || c == 'Y')
+ return (1);
+ return (0);
+}
+
+/*
+ * Malloc buffers and set up cache.
+ */
+void
+bufinit(void)
+{
+ struct bufarea *bp;
+ long bufcnt, i;
+ char *bufp;
+
+ diskreads = totalreads = 0;
+ pbp = pdirbp = (struct bufarea *)0;
+ bufhead.b_next = bufhead.b_prev = &bufhead;
+ bufcnt = MAXBUFSPACE / sblock.e2fs_bsize;
+ if (bufcnt < MINBUFS)
+ bufcnt = MINBUFS;
+ for (i = 0; i < bufcnt; i++) {
+ bp = malloc(sizeof(struct bufarea));
+ bufp = malloc((size_t)sblock.e2fs_bsize);
+ if (bp == NULL || bufp == NULL) {
+ free(bp);
+ free(bufp);
+ if (i >= MINBUFS)
+ break;
+ errexit("cannot allocate buffer pool");
+ }
+ bp->b_un.b_buf = bufp;
+ bp->b_prev = &bufhead;
+ bp->b_next = bufhead.b_next;
+ bufhead.b_next->b_prev = bp;
+ bufhead.b_next = bp;
+ initbarea(bp);
+ }
+ bufhead.b_size = i; /* save number of buffers */
+}
+
+/*
+ * Manage a cache of directory blocks.
+ */
+struct bufarea *
+getdatablk(daddr_t blkno, long size)
+{
+ struct bufarea *bp;
+
+ for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
+ if (bp->b_bno == EXT2_FSBTODB(&sblock, blkno))
+ goto foundit;
+ for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
+ if ((bp->b_flags & B_INUSE) == 0)
+ break;
+ if (bp == &bufhead)
+ errexit("deadlocked buffer pool");
+ getblk(bp, blkno, size);
+ diskreads++;
+ /* fall through */
+foundit:
+ totalreads++;
+ bp->b_prev->b_next = bp->b_next;
+ bp->b_next->b_prev = bp->b_prev;
+ bp->b_prev = &bufhead;
+ bp->b_next = bufhead.b_next;
+ bufhead.b_next->b_prev = bp;
+ bufhead.b_next = bp;
+ bp->b_flags |= B_INUSE;
+ return (bp);
+}
+
+void
+getblk(struct bufarea *bp, daddr_t blk, long size)
+{
+ daddr_t dblk;
+
+ dblk = EXT2_FSBTODB(&sblock, blk);
+ if (bp->b_bno != dblk) {
+ flush(fswritefd, bp);
+ bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
+ bp->b_bno = dblk;
+ bp->b_size = size;
+ }
+}
+
+void
+flush(int fd, struct bufarea *bp)
+{
+ int i;
+
+ if (!bp->b_dirty)
+ return;
+ if (bp->b_errs != 0)
+ pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
+ (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
+ (long long)bp->b_bno);
+ bp->b_dirty = 0;
+ bp->b_errs = 0;
+ bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
+ if (bp != &sblk)
+ return;
+ for (i = 0; i < sblock.e2fs_ngdb; i++) {
+ bwrite(fswritefd, (char *)
+ &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
+ EXT2_FSBTODB(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
+ sblock.e2fs_bsize);
+ }
+}
+
+void
+ckfini(int markclean)
+{
+ struct bufarea *bp, *nbp;
+ int cnt = 0;
+
+ if (fswritefd < 0) {
+ (void)close(fsreadfd);
+ return;
+ }
+ flush(fswritefd, &sblk);
+ if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
+ !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
+ sblk.b_bno = SBOFF / dev_bsize;
+ sbdirty();
+ flush(fswritefd, &sblk);
+ copyback_sb(&asblk);
+ asblk.b_dirty = 1;
+ flush(fswritefd, &asblk);
+ }
+ for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
+ cnt++;
+ flush(fswritefd, bp);
+ nbp = bp->b_prev;
+ free(bp->b_un.b_buf);
+ free(bp);
+ }
+ if (bufhead.b_size != cnt)
+ errexit("Panic: lost %d buffers", bufhead.b_size - cnt);
+ pbp = pdirbp = (struct bufarea *)0;
+ if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) {
+ /*
+ * Mark the file system as clean, and sync the superblock.
+ */
+ if (preen)
+ pwarn("MARKING FILE SYSTEM CLEAN\n");
+ else if (!reply("MARK FILE SYSTEM CLEAN"))
+ markclean = 0;
+ if (markclean) {
+ sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
+ sbdirty();
+ flush(fswritefd, &sblk);
+ }
+ }
+ if (debug)
+ printf("cache missed %ld of %ld (%d%%)\n", diskreads,
+ totalreads, (int)(diskreads * 100 / totalreads));
+ (void)close(fsreadfd);
+ (void)close(fswritefd);
+}
+
+int
+bread(int fd, char *buf, daddr_t blk, long size)
+{
+ off_t offset;
+
+ offset = blk;
+ offset *= dev_bsize;
+ if (blk < 0 || pread(fd, buf, size, offset) != size)
+ errexit("cannot read block %lld", (long long)blk);
+ return 0;
+}
+
+void
+bwrite(int fd, char *buf, daddr_t blk, long size)
+{
+ (void)fd;
+ (void)buf;
+ (void)blk;
+ (void)size;
+ errexit("write requested in read-only checker");
+}
+
+/*
+ * allocate a data block
+ */
+int
+allocblk(void)
+{
+ int i;
+
+ for (i = 0; i < maxfsblock - 1; i++) {
+ if (testbmap(i))
+ continue;
+ setbmap(i);
+ n_blks++;
+ return (i);
+ }
+ return (0);
+}
+
+/*
+ * Free a previously allocated block
+ */
+void
+freeblk(daddr_t blkno)
+{
+ struct inodesc idesc;
+
+ idesc.id_blkno = blkno;
+ idesc.id_numfrags = 1;
+ (void)pass4check(&idesc);
+}
+
+/*
+ * Find a pathname
+ */
+void
+getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
+{
+ int len;
+ char *cp;
+ struct inodesc idesc;
+ static int busy = 0;
+
+ if (curdir == ino && ino == EXT2_ROOTINO) {
+ (void)strlcpy(namebuf, "/", namebuflen);
+ return;
+ }
+ if (busy ||
+ (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
+ (void)strlcpy(namebuf, "?", namebuflen);
+ return;
+ }
+ busy = 1;
+ memset(&idesc, 0, sizeof(struct inodesc));
+ idesc.id_type = DATA;
+ idesc.id_fix = IGNORE;
+ cp = &namebuf[MAXPATHLEN - 1];
+ *cp = '\0';
+ if (curdir != ino) {
+ idesc.id_parent = curdir;
+ goto namelookup;
+ }
+ while (ino != EXT2_ROOTINO) {
+ idesc.id_number = ino;
+ idesc.id_func = findino;
+ idesc.id_name = "..";
+ if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
+ break;
+ namelookup:
+ idesc.id_number = idesc.id_parent;
+ idesc.id_parent = ino;
+ idesc.id_func = findname;
+ idesc.id_name = namebuf;
+ if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
+ break;
+ len = strlen(namebuf);
+ cp -= len;
+ memcpy(cp, namebuf, (size_t)len);
+ *--cp = '/';
+ if (cp < &namebuf[EXT2FS_MAXNAMLEN])
+ break;
+ ino = idesc.id_number;
+ }
+ busy = 0;
+ if (ino != EXT2_ROOTINO)
+ *--cp = '?';
+ memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
+}
+
+/*
+ * determine whether an inode should be fixed.
+ */
+int
+dofix(struct inodesc *idesc, const char *msg)
+{
+
+ switch (idesc->id_fix) {
+
+ case DONTKNOW:
+ if (idesc->id_type == DATA)
+ direrror(idesc->id_number, msg);
+ else
+ pwarn("%s", msg);
+ if (preen) {
+ printf(" (SALVAGED)\n");
+ idesc->id_fix = FIX;
+ return (ALTERED);
+ }
+ if (reply("SALVAGE") == 0) {
+ idesc->id_fix = NOFIX;
+ return (0);
+ }
+ idesc->id_fix = FIX;
+ return (ALTERED);
+
+ case FIX:
+ return (ALTERED);
+
+ case NOFIX:
+ case IGNORE:
+ return (0);
+
+ default:
+ errexit("UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
+ }
+ /* NOTREACHED */
+}
diff --git a/ninqu.rules b/ninqu.rules
@@ -180,7 +180,7 @@
(group net lib NET_BIN)
(group xsi lib XSI_BIN)
(group pseudo lib PSEUDO_BIN)
- (group extra lib EXTRA_BIN EXTRA_YAP EXTRA_FSCK_FAT)
+ (group extra lib EXTRA_BIN EXTRA_YAP EXTRA_FSCK_FAT EXTRA_FSCK_EXT4)
; codegen: generate sources consumed by the build rules below. ungated
; ones always run. conditional ones use (req) so they only run when
@@ -299,19 +299,30 @@
(out "cmd/extra/yap/yap")
(exec $(CC) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) -ltinfo))
- (rule EXTRA_FSCK_FAT_OBJ
- (glob "cmd/extra/fsck.fat/*.c")
- (gate build_extra_fsck_fat)
- (dep $(MATCH) FEATURES_H "glob:cmd/extra/fsck.fat/*.h" "shared/queue.h")
- (out "$(STEM).o")
- (exec $(CC) $(CPPFLAGS) -U_POSIX_C_SOURCE -D_DEFAULT_SOURCE $(CFLAGS) -c -o $(OUT) $(MATCH)))
-
- (rule EXTRA_FSCK_FAT
- (gate build_extra_fsck_fat)
- (member extra fsck.fat)
- (dep EXTRA_FSCK_FAT_OBJ LIBUTIL)
- (out "cmd/extra/fsck.fat/fsck.fat")
- (exec $(CC) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS)))
+ (template FSCK (RULE TOOL KEY FLAGS HEADERS)
+ (rule $(RULE)_OBJ
+ (glob "cmd/extra/$(TOOL)/*.c")
+ (gate build_extra_$(KEY))
+ (dep $(MATCH) FEATURES_H "glob:cmd/extra/$(TOOL)/**/*.h" $(HEADERS))
+ (out "$(STEM).o")
+ (exec $(CC) $(CPPFLAGS) -U_POSIX_C_SOURCE -D_DEFAULT_SOURCE -Icmd/extra/$(TOOL) $(FLAGS) $(CFLAGS) -c -o $(OUT) $(MATCH)))
+
+ (rule $(RULE)
+ (gate build_extra_$(KEY))
+ (member extra $(TOOL))
+ (dep $(RULE)_OBJ LIBUTIL)
+ (out "cmd/extra/$(TOOL)/$(TOOL)")
+ (exec $(CC) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS)))
+
+ (rule $(RULE)_BOX
+ (gate build_extra_$(KEY))
+ (dep $(RULE)_OBJ)
+ (out ".box/cmd/extra/$(TOOL).o")
+ (shell "$(LD) -r -o $(OUT) $(IN) && $(OBJCOPY) --redefine-sym main=$(KEY)_main $(OUT) && $(OBJCOPY) --keep-global-symbol=$(KEY)_main $(OUT)")
+ (group box)))
+
+ (use FSCK EXTRA_FSCK_FAT fsck.fat fsck_fat "-includecmd/extra/fsck.fat/freebsd-compat.h" "shared/queue.h")
+ (use FSCK EXTRA_FSCK_EXT4 fsck.ext4 fsck_ext4 "-includecmd/extra/fsck.ext4/compat.h" "shared/diskutil.h")
; (member GROUP ALIAS) gives a rule a qualified user-facing name
; (e.g. posix/bc) while keeping its internal name (POSIX_BC) for deps,
@@ -533,7 +544,7 @@
find shared -name '*.a' -exec rm -f {} +
printf ' CLEAN compiled binaries\n'
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 -exec rm -f {} +
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' -o -name 'fsck.ext4' ')' -perm -100 -exec rm -f {} +
sh scripts/genconfig.sh clean
rm -f scripts/mk/config.mk scripts/mk/config.kv scripts/mk/rules.mk shared/libutil/nofork_list.h
@@ -571,7 +582,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' -o -name 'fsck.ext4' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -594,9 +605,14 @@
cp -R share/kmap \"$d/share/kmap\"
if [ -x cmd/extra/fsck.fat/fsck.fat ]; then
- mkdir -p \"$d/share/licenses/aruu\"
- cp cmd/extra/fsck.fat/NOTICE \"$d/share/licenses/aruu/fsck.fat\"
+ ln -sf fsck.fat \"$d/bin/fsck.vfat\"
fi
+ for name in fsck.fat fsck.ext4; do
+ if [ -x \"cmd/extra/$name/$name\" ]; then
+ mkdir -p \"$d/share/licenses/aruu\"
+ cp \"cmd/extra/$name/NOTICE\" \"$d/share/licenses/aruu/$name\"
+ fi
+ done
printf ' INSTALL %s/lib/libaruu.a\n' '$(PREFIX)'
cp shared/libutil/libutil.a \"$d/lib/libaruu.a\"
@@ -617,7 +633,7 @@
(rule INSTALL_BASE
(phony)
(after POSIX_BIN LINUX_BIN NET_BIN XSI_BIN PSEUDO_BIN EXTRA_BIN
- POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT
+ POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT EXTRA_FSCK_EXT4
LIBUTIL MAN_MDOC MAN_TXT)
(shell "
[ -n \"${DESTDIR:-}\" ] || { printf 'install-base requires DESTDIR\n' >&2; exit 1; }
@@ -627,7 +643,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' -o -name 'fsck.ext4' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -650,9 +666,14 @@
cp -R share/kmap \"$d/share/kmap\"
if [ -x cmd/extra/fsck.fat/fsck.fat ]; then
- mkdir -p \"$d/share/licenses/aruu\"
- cp cmd/extra/fsck.fat/NOTICE \"$d/share/licenses/aruu/fsck.fat\"
+ ln -sf fsck.fat \"$d/bin/fsck.vfat\"
fi
+ for name in fsck.fat fsck.ext4; do
+ if [ -x \"cmd/extra/$name/$name\" ]; then
+ mkdir -p \"$d/share/licenses/aruu\"
+ cp \"cmd/extra/$name/NOTICE\" \"$d/share/licenses/aruu/$name\"
+ fi
+ done
printf ' INSTALL %s/lib/libaruu.a\n' '$(PREFIX)'
cp shared/libutil/libutil.a \"$d/lib/libaruu.a\"
@@ -676,7 +697,7 @@
case \"$DESTDIR\" in /usr|/usr/*) printf 'ARUU cannot remove from usr\n' >&2; exit 1 ;; esac
d=\"$DESTDIR$(PREFIX)\"
m=\"$DESTDIR$(MANPREFIX)\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' -o -name 'fsck.ext4' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' REMOVE %s/bin/%s\n' '$(PREFIX)' \"$name\"
rm -f \"$d/bin/$name\"
@@ -689,7 +710,11 @@
rm -f \"$m/$sec/$name\"
done
+ if [ \"`readlink \"$d/bin/fsck.vfat\"`\" = fsck.fat ]; then
+ rm -f \"$d/bin/fsck.vfat\"
+ fi
rm -f \"$d/share/licenses/aruu/fsck.fat\"
+ rm -f \"$d/share/licenses/aruu/fsck.ext4\"
printf ' REMOVE %s/lib/libaruu.a\n' '$(PREFIX)'
rm -f \"$d/lib/libaruu.a\"
@@ -713,6 +738,7 @@
! -path 'cmd/posix/make/*' \
! -path 'cmd/extra/yap/*' \
! -path 'cmd/extra/fsck.fat/*' \
+ ! -path 'cmd/extra/fsck.ext4/*' \
! -path 'cmd/posix/bc.c' \
-print | xargs -r clang-format -i
"))
@@ -868,13 +894,6 @@
(shell "$(LD) -r -o $(OUT) $(IN) && $(OBJCOPY) --redefine-sym main=yap_main $(OUT) && $(OBJCOPY) --keep-global-symbol=yap_main $(OUT)")
(group box))
- (rule EXTRA_FSCK_FAT_BOX
- (gate build_extra_fsck_fat)
- (dep EXTRA_FSCK_FAT_OBJ)
- (out ".box/cmd/extra/fsck.fat.o")
- (shell "$(LD) -r -o $(OUT) $(IN) && $(OBJCOPY) --redefine-sym main=fsck_fat_main $(OUT) && $(OBJCOPY) --keep-global-symbol=fsck_fat_main $(OUT)")
- (group box))
-
; genbox.sh relies on arguments passed by ninqu, where each argument matches
; an output from BOX_OBJ or a custom box rule. the script derives categories
; and tool names directly from these paths and outputs the dispatch code
@@ -895,10 +914,10 @@
(group box))
(rule BOX_DISPATCH
- (dep BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX NINQU_BOX)
+ (dep "scripts/genbox.sh" BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX EXTRA_FSCK_EXT4_BOX NINQU_BOX)
(out ".box/aruu-box.c")
(description "dispatch table and main() for aruu-box, generated from the enabled tool list")
- (exec scripts/genbox.sh $(IN))
+ (exec sh $(IN))
(redirect)
(group box))
@@ -915,7 +934,7 @@
; instead, same as LIBUTIL/LIBUTF/LIBREDLINE
;
(rule BOX
- (dep BOX_DISPATCH BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX NINQU_BOX LIBDISK LIBUTIL LIBUTF LIBREDLINE LIBBLAKE3 LIBMAGIC)
+ (dep BOX_DISPATCH BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX EXTRA_FSCK_EXT4_BOX NINQU_BOX LIBDISK LIBUTIL LIBUTF LIBREDLINE LIBBLAKE3 LIBMAGIC)
(out "aruu-box")
(description "single multi-call binary, one dispatch table for every enabled tool")
(shell "$(CC) $(CPPFLAGS) $(CFLAGS) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) $(LDLIBS_TLS) $(BOX_EXTRA_LIBS)")
@@ -936,7 +955,7 @@
(after LIBUTIL LIBUTF LIBREDLINE
POSIX_BIN LINUX_BIN NET_BIN XSI_BIN
PSEUDO_BIN EXTRA_BIN
- POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT
+ POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT EXTRA_FSCK_EXT4
AR XELF AS LD CC1 CPP CCDRV)
(exec true))
diff --git a/scripts/genbox.sh b/scripts/genbox.sh
@@ -78,6 +78,9 @@ for category in $categories; do
if [ "$tool" = "pgrep" ]; then
printf '\t{ "pkill", %s, "%s" },\n' "$sym" "$category"
fi
+ if [ "$tool" = "fsck.fat" ]; then
+ printf '\t{ "fsck.vfat", %s, "%s" },\n' "$sym" "$category"
+ fi
if [ "$tool" = "grep" ]; then
printf '\t{ "egrep", %s, "%s" },\n' "$sym" "$category"
printf '\t{ "fgrep", %s, "%s" },\n' "$sym" "$category"