]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/clri/clri.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ release_60 r321788,
[FreeBSD/FreeBSD.git] / sbin / clri / clri.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rich $alz of BBN Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #if 0
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1990, 1993\n\
39         The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 static char sccsid[] = "@(#)clri.c      8.2 (Berkeley) 9/23/93";
44 #endif /* not lint */
45 #endif
46
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
52
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
55
56 #include <err.h>
57 #include <fcntl.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdio.h>
61 #include <unistd.h>
62
63 /*
64  * Possible superblock locations ordered from most to least likely.
65  */
66 static int sblock_try[] = SBLOCKSEARCH;
67
68 static void
69 usage(void)
70 {
71         (void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
72         exit(1);
73 }
74
75 int
76 main(int argc, char *argv[])
77 {
78         struct fs *sbp;
79         struct ufs1_dinode *dp1;
80         struct ufs2_dinode *dp2;
81         char *ibuf[MAXBSIZE];
82         long generation, bsize;
83         off_t offset;
84         int i, fd, inonum;
85         char *fs, sblock[SBLOCKSIZE];
86         void *v = ibuf;
87
88         if (argc < 3)
89                 usage();
90
91         fs = *++argv;
92         sbp = NULL;
93
94         /* get the superblock. */
95         if ((fd = open(fs, O_RDWR, 0)) < 0)
96                 err(1, "%s", fs);
97         for (i = 0; sblock_try[i] != -1; i++) {
98                 if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
99                         err(1, "%s", fs);
100                 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
101                         errx(1, "%s: can't read superblock", fs);
102                 sbp = (struct fs *)sblock;
103                 if ((sbp->fs_magic == FS_UFS1_MAGIC ||
104                      (sbp->fs_magic == FS_UFS2_MAGIC &&
105                       sbp->fs_sblockloc == sblock_try[i])) &&
106                     sbp->fs_bsize <= MAXBSIZE &&
107                     sbp->fs_bsize >= (int)sizeof(struct fs))
108                         break;
109         }
110         if (sblock_try[i] == -1)
111                 errx(2, "cannot find file system superblock");
112         bsize = sbp->fs_bsize;
113
114         /* remaining arguments are inode numbers. */
115         while (*++argv) {
116                 /* get the inode number. */
117                 if ((inonum = atoi(*argv)) <= 0)
118                         errx(1, "%s is not a valid inode number", *argv);
119                 (void)printf("clearing %d\n", inonum);
120
121                 /* read in the appropriate block. */
122                 offset = ino_to_fsba(sbp, inonum);      /* inode to fs blk */
123                 offset = fsbtodb(sbp, offset);          /* fs blk disk blk */
124                 offset *= DEV_BSIZE;                    /* disk blk to bytes */
125
126                 /* seek and read the block */
127                 if (lseek(fd, offset, SEEK_SET) < 0)
128                         err(1, "%s", fs);
129                 if (read(fd, ibuf, bsize) != bsize)
130                         err(1, "%s", fs);
131
132                 if (sbp->fs_magic == FS_UFS2_MAGIC) {
133                         /* get the inode within the block. */
134                         dp2 = &(((struct ufs2_dinode *)v)
135                             [ino_to_fsbo(sbp, inonum)]);
136
137                         /* clear the inode, and bump the generation count. */
138                         generation = dp2->di_gen + 1;
139                         memset(dp2, 0, sizeof(*dp2));
140                         dp2->di_gen = generation;
141                 } else {
142                         /* get the inode within the block. */
143                         dp1 = &(((struct ufs1_dinode *)v)
144                             [ino_to_fsbo(sbp, inonum)]);
145
146                         /* clear the inode, and bump the generation count. */
147                         generation = dp1->di_gen + 1;
148                         memset(dp1, 0, sizeof(*dp1));
149                         dp1->di_gen = generation;
150                 }
151
152                 /* backup and write the block */
153                 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
154                         err(1, "%s", fs);
155                 if (write(fd, ibuf, bsize) != bsize)
156                         err(1, "%s", fs);
157                 (void)fsync(fd);
158         }
159         (void)close(fd);
160         exit(0);
161 }