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