xfstests: fix build errors and warnings
[xfstests-dev.git] / dmapi / src / suite1 / cmd / probe_punch_xfsctl_hole.c
1 /*
2  * Copyright (c) 2000-2001 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 <lib/hsm.h>
20
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <string.h>
24 #include <fcntl.h>
25
26
27
28 /*---------------------------------------------------------------------------
29
30 Test program used to test the DMAPI function dm_probe_hole().  The
31 command line is:
32
33         probe_hole [-o offset] [-l length] [-s sid] pathname
34
35 where pathname is the name of a file, offset is the offset of the start of
36 the proposed punch, and length is the length of the punch.  sid is the
37 session ID whose events you you are interested in.
38
39 ----------------------------------------------------------------------------*/
40
41 #ifndef linux
42 extern  char    *sys_errlist[];
43 #endif
44 extern  int     optind;
45 extern  char    *optarg;
46
47
48 char    *Progname;
49
50 #define METHOD_DMAPI_PROBE      0
51 #define METHOD_DMAPI_PUNCH      1
52 #define METHOD_XFSCTL           2
53
54 char *methodmap[] = {"DMAPI probe hole",
55                      "DMAPI punch hole",
56                      "Punch hole with xfsctl(XFS_IOC_FREESP64)"};
57
58 static void
59 usage(void)
60 {
61         fprintf(stderr, "usage:\t%s [-x|-p] [-o offset] [-l length] "
62                 "[-s sid] pathname\n", Progname);
63         exit(1);
64 }
65
66 int
67 xfsctl_punch_hole(
68                   char          *path,
69                   dm_off_t      offset,
70                   dm_size_t     length)
71 {
72         xfs_flock64_t   flock;
73         int             fd;
74         
75         if ((fd = open(path, O_RDWR|O_CREAT, 0600)) < 0) {
76                 perror(path);
77                 exit(errno);
78         }
79
80         flock.l_whence = 0;
81         flock.l_start = offset;
82         flock.l_len = length; 
83
84         if (xfsctl(path, fd, XFS_IOC_FREESP64, &flock)) {
85                 fprintf(stderr, "can't XFS_IOC_FREESP64 %s: %s\n",
86                         path, strerror(errno));
87                 return errno;
88         }
89         
90         close(fd);
91
92         printf("ok.\n");
93         return 0;
94 }
95
96 int
97 main(
98         int     argc, 
99         char    **argv)
100 {
101         dm_sessid_t     sid = DM_NO_SESSION;
102         char            *pathname = NULL;
103         dm_off_t        offset = 0;
104         dm_size_t       length = 0;
105         dm_off_t        roffp;
106         dm_size_t       rlenp;
107         void            *hanp;
108         size_t          hlen;
109         char            *name;
110         char            *p;
111         int             opt;
112         int             method = METHOD_DMAPI_PROBE;
113
114         Progname = strrchr(argv[0], '/');
115         if (Progname) {
116                 Progname++;
117         } else {
118                 Progname = argv[0];
119         }
120
121         /* Crack and validate the command line options. */
122
123         while ((opt = getopt(argc, argv, "o:l:s:xp")) != EOF) {
124                 switch (opt) {
125                 case 'o':
126                         offset = strtoll(optarg, &p, 10);
127                         if (p && (*p == 'k' || *p == 'K'))
128                                 offset *= 1024;
129                         break;
130                 case 'l':
131                         length = strtoll(optarg, &p, 10);
132                         if (p && (*p == 'k' || *p == 'K'))
133                                 length *= 1024;
134                         break;
135                 case 's':
136                         sid = atol(optarg);
137                         break;
138                 case 'x':
139                         method = METHOD_XFSCTL;
140                         break;
141                 case 'p':
142                         method = METHOD_DMAPI_PUNCH;
143                         break;
144                 case '?':
145                         usage();
146                 }
147         }
148         if (optind + 1 != argc)
149                 usage();
150         pathname = argv[optind];
151
152         if (!pathname)
153                 usage();
154
155         printf("Running %s on %s with settings:\n", methodmap[method], pathname);
156         printf("  offset = '%lld', length = '%lld', sid = '%lld'\n",
157                 (long long) offset, (unsigned long long) length, (long long) sid);
158         
159         if (method ==  METHOD_XFSCTL) 
160                 return xfsctl_punch_hole(pathname, offset, length);
161         
162         if (dm_init_service(&name) == -1)  {
163                 fprintf(stderr, "Can't initialize the DMAPI\n");
164                 exit(1);
165         }
166         if (sid == DM_NO_SESSION)
167                 find_test_session(&sid);
168         
169         /* Get the file's handle. */
170         
171         if (dm_path_to_handle(pathname, &hanp, &hlen)) {
172                 fprintf(stderr, "can't get handle for file %s\n", pathname);
173                 exit(1);
174         }
175         
176         switch(method) {
177         case METHOD_DMAPI_PROBE:
178                 if (dm_probe_hole(sid, hanp, hlen, DM_NO_TOKEN, offset, length,
179                                   &roffp, &rlenp)) {
180                         fprintf(stderr, "dm_probe_hole failed, %s\n",
181                                 strerror(errno));
182                         exit(1);
183                 }
184                 fprintf(stdout, "roffp is %lld, rlenp is %llu\n",
185                         (long long) roffp, (unsigned long long) rlenp);
186                 break;
187         case METHOD_DMAPI_PUNCH:
188                 if (dm_punch_hole(sid, hanp, hlen, DM_NO_TOKEN, offset, length)) {
189                         fprintf(stderr, "dm_punch_hole failed, %s\n",
190                                 strerror(errno));
191                         exit(1);
192                 }
193                 break;                  
194         }
195         dm_handle_free(hanp, hlen);
196         
197         return 0;
198 }