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