fix problems with includes now that our previous linux/acl.h has gone away.
[xfstests-dev.git] / src / ioctl.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <libxfs.h>
34 #include <jdm.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37
38 /* simple test program to try out  a bunch of ioctls:
39  *     XFS_IOC_FSCOUNTS
40  *     XFS_IOC_GET_RESBLKS
41  *     XFS_IOC_SET_RESBLKS
42  *     XFS_IOC_PATH_TO_FSHANDLE
43  *     XFS_IOC_PATH_TO_HANDLE
44  *     XFS_IOC_FD_TO_HANDLE
45  *     XFS_IOC_OPEN_BY_HANDLE
46  *     XFS_IOC_READLINK_BY_HANDLE
47 */
48
49     
50 void fscounts(int fsfd)
51 {
52     xfs_fsop_counts_t   counts;
53     int                 ret;
54     
55     ret=ioctl(fsfd, XFS_IOC_FSCOUNTS, &counts);
56     if (ret) {
57         perror("ioctl(XFS_IOC_FSCOUNTS)");
58         exit(1);
59     }
60
61     printf("XFS_IOC_FSCOUNTS-\n    freedata: %lld freertx: %lld freeino: %lld allocino: %lld\n",
62             counts.freedata, counts.freertx, counts.freeino, counts.allocino);
63 }
64     
65 __u64 getresblks(int fsfd)
66 {
67     xfs_fsop_resblks_t  res;
68     int                 ret;
69     
70     ret=ioctl(fsfd, XFS_IOC_GET_RESBLKS, &res);
71     if (ret) {
72         perror("ioctl(XFS_IOC_GET_RESBLKS)");
73         exit(1);
74     }
75     
76     printf("XFS_IOC_GET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
77             res.resblks, res.resblks_avail);
78     
79     return res.resblks;
80 }
81     
82 __u64 setresblks(int fsfd, __u64 blks)
83 {
84     xfs_fsop_resblks_t  res;
85     int                 ret;
86     
87     res.resblks=blks;
88     ret=ioctl(fsfd, XFS_IOC_SET_RESBLKS, &res);
89     if (ret) {
90         perror("ioctl(XFS_IOC_SET_RESBLKS)");
91         exit(1);
92     }
93     
94     printf("XFS_IOC_SET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
95             res.resblks, res.resblks_avail);
96     
97     return res.resblks;
98 }
99
100 void handle_print(void *handle, int handlen)
101 {
102     char *p=handle;
103     if (!handle||!handlen) {
104         printf("%s",handle?"<zero len>":"<NULL>");
105         return;
106     };
107     
108     printf("#");
109     while (handlen--)
110         printf("%02x", *p++);   
111 }
112
113 void stat_print(int fd)
114 {
115     struct stat buf;
116     
117     if (fstat(fd, &buf)) {
118         perror("stat");
119         exit(1);
120     }
121     printf("dev: %llu ino: %llu mode: %o\n",
122             (__u64)buf.st_dev, (__u64)buf.st_ino, buf.st_mode);
123 }
124     
125
126 void handle(int fsfd, char *path)
127 {
128     xfs_fsop_handlereq_t    handle;
129     char                    buffer[1024];
130     char                    link[1024];
131     int                     ret;
132     __u32                   len;
133     __u32                   linklen;
134     int                     fd;
135     
136     handle.path=path;
137     handle.ohandle=buffer;
138     handle.ohandlen=&len;
139     ret=ioctl(fsfd, XFS_IOC_PATH_TO_FSHANDLE, &handle);
140     if (ret) {
141         perror("ioctl(XFS_IOC_PATH_TO_FSHANDLE)");
142         exit(1);
143     }
144     printf("XFS_IOC_PATH_TO_FSHANDLE-\n    handle: ");
145     handle_print(handle.ohandle, *handle.ohandlen);
146     printf("\n");
147     
148     fd=open(path,O_RDONLY);
149     if (fd<0) {
150         perror("open");
151         exit(1);
152     }
153     handle.path=NULL;
154     handle.fd=fd;
155     handle.ohandle=buffer;
156     handle.ohandlen=&len;
157     ret=ioctl(fsfd, XFS_IOC_FD_TO_HANDLE, &handle);
158     if (ret) {
159         perror("ioctl(XFS_IOC_FD_TO_HANDLE)");
160         exit(1);
161     }
162     
163     printf("XFS_IOC_FD_TO_HANDLE-\n    path: %s\n    handle: ", path);
164     handle_print(handle.ohandle, *handle.ohandlen);
165     printf("\n");
166     
167     close(fd);
168     
169     handle.path=NULL;
170     handle.fd=-1;
171     handle.ihandle=buffer;
172     handle.ihandlen=len;
173     handle.ohandle=NULL;
174     handle.ohandlen=NULL;
175     ret=ioctl(fsfd, XFS_IOC_OPEN_BY_HANDLE, &handle);
176     if (ret<0) {
177         perror("ioctl(XFS_IOC_OPEN_BY_HANDLE)");
178         exit(1);
179     }
180     printf("XFS_IOC_OPEN_BY_HANDLE-\n    handle: ");
181     handle_print(handle.ihandle, handle.ihandlen);
182     printf("\n    fd: %d\n    stat- ", ret);
183     stat_print(ret);
184     close(ret);
185     
186     handle.path=path;
187     handle.ohandle=buffer;
188     handle.ohandlen=&len;
189     ret=ioctl(fsfd, XFS_IOC_PATH_TO_HANDLE, &handle);
190     if (ret) {
191         perror("ioctl(XFS_IOC_PATH_TO_HANDLE)");
192         exit(1);
193     }
194     printf("XFS_IOC_PATH_TO_HANDLE-\n    path: %s\n    handle: ", path);
195     handle_print(handle.ohandle, *handle.ohandlen);
196     printf("\n");
197
198     handle.path=NULL;
199     handle.fd=-1;
200     handle.ihandle=buffer;
201     handle.ihandlen=len;
202     handle.ohandle=link;
203     linklen=sizeof(link);
204     handle.ohandlen=&linklen;
205     ret=ioctl(fsfd, XFS_IOC_READLINK_BY_HANDLE, &handle);
206     if (ret<0) {
207         perror("ioctl(XFS_IOC_READLINK_BY_HANDLE)");
208         fprintf(stderr,"ERROR IGNORED\n");
209     } else {
210         printf("XFS_IOC_READLINK_BY_HANDLE-\n    handle: ");
211         handle_print(handle.ihandle, handle.ihandlen);
212         printf("\n    link=\"%*.*s\"\n", 
213                 ret, ret, (char*)handle.ohandle);
214     }
215 }
216
217 int
218 main(int argc, char **argv)
219 {
220     int                 fsfd;
221     
222     if (argc != 3) {
223         fprintf(stderr,"usage: %s <filesystem> <file/link in FS>\n",
224                 argv[0]);
225         exit(0);
226     }
227
228     fsfd = open(argv[1], O_RDONLY);
229     if (fsfd < 0) {
230         perror("open");
231         exit(1);
232     }
233     
234     /* XFS_IOC_FSCOUNTS */
235     fscounts(fsfd);
236     /* XFS_IOC_GET_RESBLKS & XFS_IOC_SET_RESBLKS */
237     getresblks(fsfd);
238     setresblks(fsfd, 1000);
239     getresblks(fsfd);
240     setresblks(fsfd, 0);
241     /* XFS_IOC_FSINUMBERS */
242     
243         /* NYI in kernel */
244     
245     /* XFS_IOC_PATH_TO_FSHANDLE */
246     /* XFS_IOC_PATH_TO_HANDLE */
247     /* XFS_IOC_FD_TO_HANDLE */
248     /* XFS_IOC_OPEN_BY_HANDLE */
249     /* XFS_IOC_READLINK_BY_HANDLE */
250     handle(fsfd, argv[2]);
251     
252     return 0;
253 }