0f15b44903594693877f5ed5c3e6f32259c98360
[xfstests-dev.git] / src / godown.c
1 /*
2  * Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include "global.h"
34
35 /* These should be in libxfs.h */
36 #ifndef XFS_IOC_GOINGDOWN
37 #define XFS_IOC_GOINGDOWN           _IOR ('X', 125, __uint32_t)
38 #endif
39 #ifndef XFS_FSOP_GOING_FLAGS_DEFAULT
40 #define XFS_FSOP_GOING_FLAGS_DEFAULT    0x0     /* going down */
41 #endif
42 #ifndef XFS_FSOP_GOING_FLAGS_LOGFLUSH
43 #define XFS_FSOP_GOING_FLAGS_LOGFLUSH    0x1     /* flush log */
44 #endif
45 #ifndef XFS_FSOP_GOING_FLAGS_NOLOGFLUSH
46 #define XFS_FSOP_GOING_FLAGS_NOLOGFLUSH  0x2     /* don't flush log */
47 #endif
48
49 static char *progname;
50
51
52 static void
53 usage(void)
54 {
55         fprintf(stderr, "usage: %s [-f] [-v] mnt-dir\n", progname);
56 }
57
58 int
59 main(int argc, char *argv[])
60 {
61         int c;
62         int flag;
63         int flushlog_opt = 0;
64         int verbose_opt = 0;
65         struct stat st;
66         char *mnt_dir;
67         int fd;
68
69         progname = argv[0];
70
71         while ((c = getopt(argc, argv, "fv")) != -1) {
72                 switch (c) {
73                 case 'f':
74                         flushlog_opt = 1;
75                         break;
76                 case 'v':
77                         verbose_opt = 1;
78                         break;
79                 case '?':
80                         usage();
81                         return 1;
82                 }
83         }
84
85         /* process required cmd argument */
86         if (optind == argc-1) {
87                 mnt_dir = argv[optind];
88         }
89         else {
90                 usage();
91                 return 1;
92         }
93
94         if ((stat(mnt_dir, &st)) == -1) {
95                 fprintf(stderr, "%s: error on stat \"%s\": %s\n",
96                         progname, mnt_dir, strerror(errno));
97                 return 1;
98         }
99
100         if (!S_ISDIR(st.st_mode)) {
101                 fprintf(stderr, "%s: argument \"%s\" is not a directory\n",
102                         progname, mnt_dir);
103                 return 1;
104         }
105
106         
107 #if 0
108         {
109                 struct statvfs stvfs;
110                 if ((statvfs(mnt_dir, &stvfs)) == -1) {
111                         fprintf(stderr, "%s: error on statfs \"%s\": %s\n",
112                                 progname, mnt_dir, strerror(errno));
113                         return 1;
114                 }
115
116                 if (strcmp(stvfs.f_basetype, "xfs") != 0) {
117                         fprintf(stderr, "%s: filesys for \"%s\" is not XFS:\"%s\"\n",
118                                 progname, mnt_dir, stvfs.f_basetype);
119                         return 1;
120                 }
121         }
122 #endif
123
124
125         flag = (flushlog_opt ? XFS_FSOP_GOING_FLAGS_LOGFLUSH 
126                             : XFS_FSOP_GOING_FLAGS_NOLOGFLUSH);
127
128         if (verbose_opt) {
129                 printf("Opening \"%s\"\n", mnt_dir);
130         }
131         if ((fd = open(mnt_dir, O_RDONLY)) == -1) {
132                 fprintf(stderr, "%s: error on open of \"%s\": %s\n",
133                         progname, mnt_dir, strerror(errno));
134                 return 1;
135         }
136
137         if (verbose_opt) {
138                 printf("Calling XFS_IOC_GOINGDOWN\n");
139         }
140         if ((xfsctl(mnt_dir, fd, XFS_IOC_GOINGDOWN, &flag)) == -1) {
141                 fprintf(stderr, "%s: error on xfsctl(GOINGDOWN) of \"%s\": %s\n",
142                         progname, mnt_dir, strerror(errno));
143                 return 1;
144         }
145
146         close(fd);
147
148         return 0;
149 }