fsstress: translate flags in fiemap_f
[xfstests-dev.git] / src / godown.c
1 /*
2  * Copyright (c) 2004 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <syslog.h>
20 #include "global.h"
21
22 static char *xprogname;
23
24
25 static void
26 usage(void)
27 {
28         fprintf(stderr, "usage: %s [-f] [-v] mnt-dir\n", xprogname);
29 }
30
31 int
32 main(int argc, char *argv[])
33 {
34         int c;
35         int flag;
36         int flushlog_opt = 0;
37         int verbose_opt = 0;
38         struct stat st;
39         char *mnt_dir;
40         int fd;
41
42         xprogname = argv[0];
43
44         while ((c = getopt(argc, argv, "fv")) != -1) {
45                 switch (c) {
46                 case 'f':
47                         flushlog_opt = 1;
48                         break;
49                 case 'v':
50                         verbose_opt = 1;
51                         break;
52                 case '?':
53                         usage();
54                         return 1;
55                 }
56         }
57
58         /* process required cmd argument */
59         if (optind == argc-1) {
60                 mnt_dir = argv[optind];
61         }
62         else {
63                 usage();
64                 return 1;
65         }
66
67         if ((stat(mnt_dir, &st)) == -1) {
68                 fprintf(stderr, "%s: error on stat \"%s\": %s\n",
69                         xprogname, mnt_dir, strerror(errno));
70                 return 1;
71         }
72
73         if (!S_ISDIR(st.st_mode)) {
74                 fprintf(stderr, "%s: argument \"%s\" is not a directory\n",
75                         xprogname, mnt_dir);
76                 return 1;
77         }
78
79         
80 #if 0
81         {
82                 struct statvfs stvfs;
83                 if ((statvfs(mnt_dir, &stvfs)) == -1) {
84                         fprintf(stderr, "%s: error on statfs \"%s\": %s\n",
85                                 xprogname, mnt_dir, strerror(errno));
86                         return 1;
87                 }
88
89                 if (strcmp(stvfs.f_basetype, "xfs") != 0) {
90                         fprintf(stderr, "%s: filesys for \"%s\" is not XFS:\"%s\"\n",
91                                 xprogname, mnt_dir, stvfs.f_basetype);
92                         return 1;
93                 }
94         }
95 #endif
96
97
98         flag = (flushlog_opt ? XFS_FSOP_GOING_FLAGS_LOGFLUSH 
99                             : XFS_FSOP_GOING_FLAGS_NOLOGFLUSH);
100
101         if (verbose_opt) {
102                 printf("Opening \"%s\"\n", mnt_dir);
103         }
104         if ((fd = open(mnt_dir, O_RDONLY)) == -1) {
105                 fprintf(stderr, "%s: error on open of \"%s\": %s\n",
106                         xprogname, mnt_dir, strerror(errno));
107                 return 1;
108         }
109
110         if (verbose_opt) {
111                 printf("Calling XFS_IOC_GOINGDOWN\n");
112         }
113         syslog(LOG_WARNING, "xfstests-induced forced shutdown of %s:\n",
114                 mnt_dir);
115         if ((xfsctl(mnt_dir, fd, XFS_IOC_GOINGDOWN, &flag)) == -1) {
116                 fprintf(stderr, "%s: error on xfsctl(GOINGDOWN) of \"%s\": %s\n",
117                         xprogname, mnt_dir, strerror(errno));
118                 return 1;
119         }
120
121         close(fd);
122
123         return 0;
124 }