btrfs: test fstrim after doing a device replace
[xfstests-dev.git] / src / xfsctl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13
14 #include <xfs/xfs.h>
15 #include <xfs/jdm.h>
16
17 /* simple test program to try out a bunch of xfsctls:
18  *     XFS_IOC_FSCOUNTS
19  *     XFS_IOC_GET_RESBLKS
20  *     XFS_IOC_SET_RESBLKS
21  *     XFS_IOC_PATH_TO_FSHANDLE
22  *     XFS_IOC_PATH_TO_HANDLE
23  *     XFS_IOC_FD_TO_HANDLE
24  *     XFS_IOC_OPEN_BY_HANDLE
25  *     XFS_IOC_READLINK_BY_HANDLE
26 */
27
28     
29 void fscounts(char *fname, int fsfd)
30 {
31     xfs_fsop_counts_t   counts;
32     int                 ret;
33     
34     ret=xfsctl(fname, fsfd, XFS_IOC_FSCOUNTS, &counts);
35     if (ret) {
36         perror("xfsctl(XFS_IOC_FSCOUNTS)");
37         exit(1);
38     }
39
40     printf("XFS_IOC_FSCOUNTS-\n    freedata: %lld freertx: %lld freeino: %lld allocino: %lld\n",
41             (long long)counts.freedata, (long long)counts.freertx,
42            (long long)counts.freeino, (long long)counts.allocino);
43 }
44     
45 __u64 getresblks(char *fname, int fsfd)
46 {
47     xfs_fsop_resblks_t  res;
48     int                 ret;
49     
50     ret=xfsctl(fname, fsfd, XFS_IOC_GET_RESBLKS, &res);
51     if (ret) {
52         perror("xfsctl(XFS_IOC_GET_RESBLKS)");
53         exit(1);
54     }
55     
56     printf("XFS_IOC_GET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
57             (long long)res.resblks, (long long)res.resblks_avail);
58     
59     return res.resblks;
60 }
61     
62 __u64 setresblks(char *fname, int fsfd, __u64 blks)
63 {
64     xfs_fsop_resblks_t  res;
65     int                 ret;
66     
67     res.resblks=blks;
68     ret=xfsctl(fname, fsfd, XFS_IOC_SET_RESBLKS, &res);
69     if (ret) {
70         perror("xfsctl(XFS_IOC_SET_RESBLKS)");
71         exit(1);
72     }
73     
74     printf("XFS_IOC_SET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
75             (long long)res.resblks, (long long)res.resblks_avail);
76     
77     return res.resblks;
78 }
79
80 void handle_print(void *handle, int handlen)
81 {
82     char *p=handle;
83     if (!handle||!handlen) {
84         printf("%s",handle?"<zero len>":"<NULL>");
85         return;
86     };
87     
88     printf("#");
89     while (handlen--)
90         printf("%02x", *p++);   
91 }
92
93 void stat_print(int fd)
94 {
95     struct stat buf;
96     
97     if (fstat(fd, &buf)) {
98         perror("stat");
99         exit(1);
100     }
101     printf("dev: %llu ino: %llu mode: %o\n",
102             (unsigned long long)buf.st_dev, (unsigned long long)buf.st_ino, buf.st_mode);
103 }
104     
105
106 void handle(char *fname, int fsfd, char *path)
107 {
108     xfs_fsop_handlereq_t    handle;
109     char                    buffer[1024];
110     char                    link[1024];
111     int                     ret;
112     __u32                   len;
113     __u32                   linklen;
114     int                     fd;
115     
116     handle.path=path;
117     handle.ohandle=buffer;
118     handle.ohandlen=&len;
119     ret=xfsctl(fname, fsfd, XFS_IOC_PATH_TO_FSHANDLE, &handle);
120     if (ret) {
121         perror("xfsctl(XFS_IOC_PATH_TO_FSHANDLE)");
122         exit(1);
123     }
124     printf("XFS_IOC_PATH_TO_FSHANDLE-\n    handle: ");
125     handle_print(handle.ohandle, *handle.ohandlen);
126     printf("\n");
127     
128     fd=open(path,O_RDONLY);
129     if (fd<0) {
130         perror("open");
131         exit(1);
132     }
133     handle.path=NULL;
134     handle.fd=fd;
135     handle.ohandle=buffer;
136     handle.ohandlen=&len;
137     ret=xfsctl(fname, fsfd, XFS_IOC_FD_TO_HANDLE, &handle);
138     if (ret) {
139         perror("ioctl(XFS_IOC_FD_TO_HANDLE)");
140         exit(1);
141     }
142     
143     printf("XFS_IOC_FD_TO_HANDLE-\n    path: %s\n    handle: ", path);
144     handle_print(handle.ohandle, *handle.ohandlen);
145     printf("\n");
146     
147     close(fd);
148     
149     handle.path=NULL;
150     handle.fd=-1;
151     handle.ihandle=buffer;
152     handle.ihandlen=len;
153     handle.ohandle=NULL;
154     handle.ohandlen=NULL;
155     ret=xfsctl(fname, fsfd, XFS_IOC_OPEN_BY_HANDLE, &handle);
156     if (ret<0) {
157         perror("xfsctl(XFS_IOC_OPEN_BY_HANDLE)");
158         exit(1);
159     }
160     printf("XFS_IOC_OPEN_BY_HANDLE-\n    handle: ");
161     handle_print(handle.ihandle, handle.ihandlen);
162     printf("\n    fd: %d\n    stat- ", ret);
163     stat_print(ret);
164     close(ret);
165     
166     handle.path=path;
167     handle.ohandle=buffer;
168     handle.ohandlen=&len;
169     ret=xfsctl(fname, fsfd, XFS_IOC_PATH_TO_HANDLE, &handle);
170     if (ret) {
171         perror("xfsctl(XFS_IOC_PATH_TO_HANDLE)");
172         exit(1);
173     }
174     printf("XFS_IOC_PATH_TO_HANDLE-\n    path: %s\n    handle: ", path);
175     handle_print(handle.ohandle, *handle.ohandlen);
176     printf("\n");
177
178     handle.path=NULL;
179     handle.fd=-1;
180     handle.ihandle=buffer;
181     handle.ihandlen=len;
182     handle.ohandle=link;
183     linklen=sizeof(link);
184     handle.ohandlen=&linklen;
185     ret=xfsctl(fname, fsfd, XFS_IOC_READLINK_BY_HANDLE, &handle);
186     if (ret<0) {
187         perror("xfsctl(XFS_IOC_READLINK_BY_HANDLE)");
188         fprintf(stderr,"ERROR IGNORED\n");
189     } else {
190         printf("XFS_IOC_READLINK_BY_HANDLE-\n    handle: ");
191         handle_print(handle.ihandle, handle.ihandlen);
192         printf("\n    link=\"%*.*s\"\n", 
193                 ret, ret, (char*)handle.ohandle);
194     }
195 }
196
197 int
198 main(int argc, char **argv)
199 {
200     int                 fsfd;
201     
202     if (argc != 3) {
203         fprintf(stderr,"usage: %s <filesystem> <file/link in FS>\n",
204                 argv[0]);
205         exit(0);
206     }
207
208     fsfd = open(argv[1], O_RDONLY);
209     if (fsfd < 0) {
210         perror("open");
211         exit(1);
212     }
213     
214     /* XFS_IOC_FSCOUNTS */
215     fscounts(argv[0], fsfd);
216     /* XFS_IOC_GET_RESBLKS & XFS_IOC_SET_RESBLKS */
217     getresblks(argv[0], fsfd);
218     setresblks(argv[0], fsfd, 1000);
219     getresblks(argv[0], fsfd);
220     setresblks(argv[0], fsfd, 0);
221     /* TODO - XFS_IOC_FSINUMBERS */
222
223     /* XFS_IOC_PATH_TO_FSHANDLE */
224     /* XFS_IOC_PATH_TO_HANDLE */
225     /* XFS_IOC_FD_TO_HANDLE */
226     /* XFS_IOC_OPEN_BY_HANDLE */
227     /* XFS_IOC_READLINK_BY_HANDLE */
228     handle(argv[0], fsfd, argv[2]);
229     
230     return 0;
231 }