summaryrefslogtreecommitdiff
path: root/src/xfsctl.c
blob: 4c0b098800a3f680020e863a5f8cdac3accf40ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include <xfs/xfs.h>
#include <xfs/jdm.h>

/* simple test program to try out a bunch of xfsctls:
 *     XFS_IOC_FSCOUNTS
 *     XFS_IOC_GET_RESBLKS
 *     XFS_IOC_SET_RESBLKS
 *     XFS_IOC_PATH_TO_FSHANDLE
 *     XFS_IOC_PATH_TO_HANDLE
 *     XFS_IOC_FD_TO_HANDLE
 *     XFS_IOC_OPEN_BY_HANDLE
 *     XFS_IOC_READLINK_BY_HANDLE
*/

    
void fscounts(char *fname, int fsfd)
{
    xfs_fsop_counts_t   counts;
    int                 ret;
    
    ret=xfsctl(fname, fsfd, XFS_IOC_FSCOUNTS, &counts);
    if (ret) {
        perror("xfsctl(XFS_IOC_FSCOUNTS)");
        exit(1);
    }

    printf("XFS_IOC_FSCOUNTS-\n    freedata: %lld freertx: %lld freeino: %lld allocino: %lld\n",
            (long long)counts.freedata, (long long)counts.freertx,
	   (long long)counts.freeino, (long long)counts.allocino);
}
    
__u64 getresblks(char *fname, int fsfd)
{
    xfs_fsop_resblks_t  res;
    int                 ret;
    
    ret=xfsctl(fname, fsfd, XFS_IOC_GET_RESBLKS, &res);
    if (ret) {
        perror("xfsctl(XFS_IOC_GET_RESBLKS)");
        exit(1);
    }
    
    printf("XFS_IOC_GET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
            (long long)res.resblks, (long long)res.resblks_avail);
    
    return res.resblks;
}
    
__u64 setresblks(char *fname, int fsfd, __u64 blks)
{
    xfs_fsop_resblks_t  res;
    int                 ret;
    
    res.resblks=blks;
    ret=xfsctl(fname, fsfd, XFS_IOC_SET_RESBLKS, &res);
    if (ret) {
        perror("xfsctl(XFS_IOC_SET_RESBLKS)");
        exit(1);
    }
    
    printf("XFS_IOC_SET_RESBLKS-\n    resblks: %lld blksavail: %lld\n",
            (long long)res.resblks, (long long)res.resblks_avail);
    
    return res.resblks;
}

void handle_print(void *handle, int handlen)
{
    char *p=handle;
    if (!handle||!handlen) {
        printf("%s",handle?"<zero len>":"<NULL>");
        return;
    };
    
    printf("#");
    while (handlen--)
        printf("%02x", *p++);   
}

void stat_print(int fd)
{
    struct stat buf;
    
    if (fstat(fd, &buf)) {
        perror("stat");
        exit(1);
    }
    printf("dev: %llu ino: %llu mode: %o\n",
            (unsigned long long)buf.st_dev, (unsigned long long)buf.st_ino, buf.st_mode);
}
    

void handle(char *fname, int fsfd, char *path)
{
    xfs_fsop_handlereq_t    handle;
    char                    buffer[1024];
    char                    link[1024];
    int                     ret;
    __u32                   len;
    __u32                   linklen;
    int                     fd;
    
    handle.path=path;
    handle.ohandle=buffer;
    handle.ohandlen=&len;
    ret=xfsctl(fname, fsfd, XFS_IOC_PATH_TO_FSHANDLE, &handle);
    if (ret) {
        perror("xfsctl(XFS_IOC_PATH_TO_FSHANDLE)");
        exit(1);
    }
    printf("XFS_IOC_PATH_TO_FSHANDLE-\n    handle: ");
    handle_print(handle.ohandle, *handle.ohandlen);
    printf("\n");
    
    fd=open(path,O_RDONLY);
    if (fd<0) {
        perror("open");
        exit(1);
    }
    handle.path=NULL;
    handle.fd=fd;
    handle.ohandle=buffer;
    handle.ohandlen=&len;
    ret=xfsctl(fname, fsfd, XFS_IOC_FD_TO_HANDLE, &handle);
    if (ret) {
        perror("ioctl(XFS_IOC_FD_TO_HANDLE)");
        exit(1);
    }
    
    printf("XFS_IOC_FD_TO_HANDLE-\n    path: %s\n    handle: ", path);
    handle_print(handle.ohandle, *handle.ohandlen);
    printf("\n");
    
    close(fd);
    
    handle.path=NULL;
    handle.fd=-1;
    handle.ihandle=buffer;
    handle.ihandlen=len;
    handle.ohandle=NULL;
    handle.ohandlen=NULL;
    ret=xfsctl(fname, fsfd, XFS_IOC_OPEN_BY_HANDLE, &handle);
    if (ret<0) {
        perror("xfsctl(XFS_IOC_OPEN_BY_HANDLE)");
        exit(1);
    }
    printf("XFS_IOC_OPEN_BY_HANDLE-\n    handle: ");
    handle_print(handle.ihandle, handle.ihandlen);
    printf("\n    fd: %d\n    stat- ", ret);
    stat_print(ret);
    close(ret);
    
    handle.path=path;
    handle.ohandle=buffer;
    handle.ohandlen=&len;
    ret=xfsctl(fname, fsfd, XFS_IOC_PATH_TO_HANDLE, &handle);
    if (ret) {
        perror("xfsctl(XFS_IOC_PATH_TO_HANDLE)");
        exit(1);
    }
    printf("XFS_IOC_PATH_TO_HANDLE-\n    path: %s\n    handle: ", path);
    handle_print(handle.ohandle, *handle.ohandlen);
    printf("\n");

    handle.path=NULL;
    handle.fd=-1;
    handle.ihandle=buffer;
    handle.ihandlen=len;
    handle.ohandle=link;
    linklen=sizeof(link);
    handle.ohandlen=&linklen;
    ret=xfsctl(fname, fsfd, XFS_IOC_READLINK_BY_HANDLE, &handle);
    if (ret<0) {
        perror("xfsctl(XFS_IOC_READLINK_BY_HANDLE)");
        fprintf(stderr,"ERROR IGNORED\n");
    } else {
        printf("XFS_IOC_READLINK_BY_HANDLE-\n    handle: ");
        handle_print(handle.ihandle, handle.ihandlen);
        printf("\n    link=\"%*.*s\"\n", 
                ret, ret, (char*)handle.ohandle);
    }
}

int
main(int argc, char **argv)
{
    int                 fsfd;
    
    if (argc != 3) {
        fprintf(stderr,"usage: %s <filesystem> <file/link in FS>\n",
                argv[0]);
        exit(0);
    }

    fsfd = open(argv[1], O_RDONLY);
    if (fsfd < 0) {
	perror("open");
	exit(1);
    }
    
    /* XFS_IOC_FSCOUNTS */
    fscounts(argv[0], fsfd);
    /* XFS_IOC_GET_RESBLKS & XFS_IOC_SET_RESBLKS */
    getresblks(argv[0], fsfd);
    setresblks(argv[0], fsfd, 1000);
    getresblks(argv[0], fsfd);
    setresblks(argv[0], fsfd, 0);
    /* TODO - XFS_IOC_FSINUMBERS */

    /* XFS_IOC_PATH_TO_FSHANDLE */
    /* XFS_IOC_PATH_TO_HANDLE */
    /* XFS_IOC_FD_TO_HANDLE */
    /* XFS_IOC_OPEN_BY_HANDLE */
    /* XFS_IOC_READLINK_BY_HANDLE */
    handle(argv[0], fsfd, argv[2]);
    
    return 0;
}