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