]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/clri/clri.c
Merge lldb trunk r338150, and resolve conflicts.
[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 <errno.h>
58 #include <fcntl.h>
59 #include <libufs.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdio.h>
63 #include <unistd.h>
64
65 static void
66 usage(void)
67 {
68         (void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
69         exit(1);
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75         struct fs *fs;
76         struct ufs1_dinode *dp1;
77         struct ufs2_dinode *dp2;
78         char *ibuf[MAXBSIZE];
79         long generation, bsize;
80         off_t offset;
81         int fd, ret, inonum;
82         char *fsname;
83         void *v = ibuf;
84
85         if (argc < 3)
86                 usage();
87
88         fsname = *++argv;
89
90         /* get the superblock. */
91         if ((fd = open(fsname, O_RDWR, 0)) < 0)
92                 err(1, "%s", fsname);
93         if ((ret = sbget(fd, &fs, -1)) != 0) {
94                 switch (ret) {
95                 case ENOENT:
96                         warn("Cannot find file system superblock");
97                         return (1);
98                 default:
99                         warn("Unable to read file system superblock");
100                         return (1);
101                 }
102         }
103         bsize = fs->fs_bsize;
104
105         /* remaining arguments are inode numbers. */
106         while (*++argv) {
107                 /* get the inode number. */
108                 if ((inonum = atoi(*argv)) <= 0)
109                         errx(1, "%s is not a valid inode number", *argv);
110                 (void)printf("clearing %d\n", inonum);
111
112                 /* read in the appropriate block. */
113                 offset = ino_to_fsba(fs, inonum);       /* inode to fs blk */
114                 offset = fsbtodb(fs, offset);           /* fs blk disk blk */
115                 offset *= DEV_BSIZE;                    /* disk blk to bytes */
116
117                 /* seek and read the block */
118                 if (lseek(fd, offset, SEEK_SET) < 0)
119                         err(1, "%s", fsname);
120                 if (read(fd, ibuf, bsize) != bsize)
121                         err(1, "%s", fsname);
122
123                 if (fs->fs_magic == FS_UFS2_MAGIC) {
124                         /* get the inode within the block. */
125                         dp2 = &(((struct ufs2_dinode *)v)
126                             [ino_to_fsbo(fs, inonum)]);
127
128                         /* clear the inode, and bump the generation count. */
129                         generation = dp2->di_gen + 1;
130                         memset(dp2, 0, sizeof(*dp2));
131                         dp2->di_gen = generation;
132                 } else {
133                         /* get the inode within the block. */
134                         dp1 = &(((struct ufs1_dinode *)v)
135                             [ino_to_fsbo(fs, inonum)]);
136
137                         /* clear the inode, and bump the generation count. */
138                         generation = dp1->di_gen + 1;
139                         memset(dp1, 0, sizeof(*dp1));
140                         dp1->di_gen = generation;
141                 }
142
143                 /* backup and write the block */
144                 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
145                         err(1, "%s", fsname);
146                 if (write(fd, ibuf, bsize) != bsize)
147                         err(1, "%s", fsname);
148                 (void)fsync(fd);
149         }
150         (void)close(fd);
151         exit(0);
152 }