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
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2017 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <ntapi/ntapi.h>
#include <ntapi/nt_fsctl.h>
#include <ntapi/nt_mount.h>
#include <ntapi/nt_statfs.h>
#include "ntapi_impl.h"
int32_t __stdcall __ntapi_tt_statfs(
__in void * hfile,
__out nt_statfs * statfs,
__in uint32_t infolen,
__out uintptr_t * buffer,
__in uint32_t buffer_size,
__in uint32_t flags)
{
int32_t status;
nt_iosb iosb;
nt_unicode_string * sdev;
wchar16_t * wch;
wchar16_t * wch_mark;
uint32_t offset;
uint32_t hash;
void * mnt_points_buffer;
nt_mount_points * mnt_points;
nt_fsai * fsai;
nt_fsfsi * fsfsi;
uint32_t * fsid;
uint64_t * pguid;
uint32_t written;
/* validation */
if (infolen < sizeof(*statfs))
return NT_STATUS_INFO_LENGTH_MISMATCH;
/* init */
__ntapi->tt_aligned_block_memset(
statfs,0,sizeof(*statfs));
statfs->hfile = hfile;
statfs->dev_name_maxlen = (uint16_t)infolen - sizeof(*statfs);
/* sigh */
if (buffer_size >= 65536)
buffer_size = 65535;
/* maximum component length, file system type */
if ((status = __ntapi->zw_query_volume_information_file(
hfile,
&iosb,
buffer,
buffer_size,
NT_FILE_FS_ATTRIBUTE_INFORMATION)))
return status;
fsai = (nt_fsai *)buffer;
statfs->f_type = 0;
statfs->f_namelen = fsai->maximum_component_name_length;
statfs->nt_fstype_hash = __ntapi->tt_buffer_crc32(
0,&fsai->file_system_name,
fsai->file_system_name_length);
/* max files per volume */
switch (statfs->nt_fstype_hash) {
case NT_FS_TYPE_HPFS_NAME_HASH:
case NT_FS_TYPE_NTFS_NAME_HASH:
case NT_FS_TYPE_SMB_NAME_HASH:
case NT_FS_TYPE_UDF_NAME_HASH:
statfs->f_files = 0xFFFFFFFF;
break;
case NT_FS_TYPE_FAT16_NAME_HASH:
statfs->f_files = 0x10000;
break;
case NT_FS_TYPE_FAT32_NAME_HASH:
statfs->f_files = 0x400000;
break;
default:
/* pretend there is no limitation */
statfs->f_files = (uint64_t)(-1);
break;
}
/* number of free file records on volume */
/* (skip, yet indicate that the volume is not empty) */
statfs->f_ffree = (size_t)statfs->f_files >> 4 << 3;
/* file system size information */
if ((status = __ntapi->zw_query_volume_information_file(
hfile,
&iosb,
buffer,
buffer_size,
NT_FILE_FS_FULL_SIZE_INFORMATION)))
return status;
fsfsi = (nt_fsfsi *)buffer;
statfs->f_blocks = fsfsi->total_allocation_units.quad;
statfs->f_bfree = fsfsi->actual_available_allocation_units.quad;
statfs->f_bavail = fsfsi->caller_available_allocation_units.quad;
statfs->f_bsize = fsfsi->sectors_per_allocation_unit * fsfsi->bytes_per_sector;
statfs->f_frsize = fsfsi->bytes_per_sector;
/* system-unique device name */
if ((status = __ntapi->zw_query_object(
hfile,
NT_OBJECT_NAME_INFORMATION,
buffer,
buffer_size,
&written)))
return status;
sdev = (nt_unicode_string *)buffer;
wch = sdev->buffer;
if (sdev->strlen < __DEVICE_PATH_PREFIX_LEN)
return NT_STATUS_NOT_SUPPORTED;
if ((wch[0] != '\\')
|| (wch[1] != 'D')
|| (wch[2] != 'e')
|| (wch[3] != 'v')
|| (wch[4] != 'i')
|| (wch[5] != 'c')
|| (wch[6] != 'e')
|| (wch[7] != '\\'))
return NT_STATUS_NOT_SUPPORTED;
if (sdev->strlen < __DEVICE_MUP_PREFIX_LEN)
return NT_STATUS_INVALID_HANDLE;
if ((wch[8]=='M')
&& (wch[9]=='u')
&& (wch[10]=='p')
&& (wch[11]=='\\')) {
statfs->nt_drive_letter = 0;
hash = __DEVICE_MUP_PREFIX_HASH;
flags &= ~NT_STATFS_VOLUME_GUID;
statfs->flags |= NT_STATFS_MUP_DEVICE;
wch_mark = &wch[12];
wch = wch_mark;
for (; *wch!='\\'; )
wch++;
wch++;
} else {
wch_mark = &wch[8];
wch = wch_mark;
hash = __DEVICE_PATH_PREFIX_HASH;
}
for (; *wch!='\\'; )
wch++;
statfs->obj_name_strlen = sdev->strlen;
statfs->obj_name_maxlen = statfs->dev_name_maxlen;
statfs->dev_name_strlen = sizeof(uint16_t) * (uint16_t)(wch - sdev->buffer);
statfs->dev_name_hash = __ntapi->tt_buffer_crc32(
hash,wch_mark,
sizeof(wchar16_t) * (wch - wch_mark));
/* device name */
if (!(flags & NT_STATFS_DEV_NAME_COPY))
(void)0;
else if (statfs->dev_name_maxlen < sdev->strlen)
return NT_STATUS_BUFFER_TOO_SMALL;
else
__ntapi->tt_memcpy_utf16(
statfs->dev_name,
sdev->buffer,
sdev->strlen);
/* f_fsid: hash of the system-unique device name */
/* (never use the volume serial number) */
fsid = (uint32_t *)&(statfs->f_fsid);
fsid[0] = statfs->dev_name_hash;
fsid[1] = 0;
/* f_flags, nt_attr */
statfs->f_flags = 0;
statfs->nt_attr = 0;
statfs->nt_padding = 0;
if (!(flags & NT_STATFS_VOLUME_GUID)) {
statfs->nt_drive_letter = 0;
pguid = (uint64_t *)&(statfs->nt_volume_guid);
pguid[0] = 0;
pguid[1] = 0;
return NT_STATUS_SUCCESS;
}
/* dos device letter and volume guid */
wch = (wchar16_t *)sdev->buffer;
mnt_points_buffer = (void *)((uintptr_t)wch + statfs->dev_name_strlen);
*(--wch) = statfs->dev_name_strlen;
offset = sizeof(nt_unicode_string) + statfs->dev_name_strlen;
if ((status = __ntapi->tt_get_dos_drive_mount_points(
0,0,
(nt_mount_dev_name *)wch,
mnt_points_buffer,
buffer_size - offset)))
return status;
offset = ((nt_mount_point_param *)mnt_points_buffer)->mount_points_offset;
mnt_points = (nt_mount_points *)((uintptr_t)mnt_points_buffer + offset);
status = __ntapi->tt_dev_mount_points_to_statfs(
mnt_points,statfs);
return status;
}
|