-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathalloc.c
388 lines (359 loc) · 8.55 KB
/
alloc.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2000-2003 Silicon Graphics, Inc.
* All Rights Reserved.
*/
#include "global.h"
/*
* Block I/O parameterization. A basic block (BB) is the lowest size of
* filesystem allocation, and must equal 512. Length units given to bio
* routines are in BB's.
*/
/* Assume that if we have BTOBB, then we have the rest */
#ifndef BTOBB
#define BBSHIFT 9
#define BBSIZE (1<<BBSHIFT)
#define BBMASK (BBSIZE-1)
#define BTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
#define BTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
#define BBTOB(bbs) ((bbs) << BBSHIFT)
#define OFFTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
#define SEEKLIMIT32 0x7fffffff
#define BBSEEKLIMIT32 BTOBBT(SEEKLIMIT32)
#define SEEKLIMIT 0x7fffffffffffffffLL
#define BBSEEKLIMIT OFFTOBBT(SEEKLIMIT)
#endif
#ifndef OFFTOBB
#define OFFTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
#define BBTOOFF(bbs) ((__u64)(bbs) << BBSHIFT)
#endif
#define FSBTOBB(f) (OFFTOBBT(FSBTOOFF(f)))
#define BBTOFSB(b) (OFFTOFSB(BBTOOFF(b)))
#define OFFTOFSB(o) ((o) / blocksize)
#define FSBTOOFF(f) ((f) * blocksize)
void usage(void)
{
printf("usage: alloc [-b blocksize] [-d dir] [-f file] [-n] [-r] [-t]\n"
"flags:\n"
" -n - non-interractive mode\n"
" -r - real time file\n"
" -t - truncate on open\n"
"\n"
"commands:\n"
" r [offset] [length] - reserve\n"
" u [offset] [length] - unreserve\n"
" a [offset] [length] - alloc *** identical to free\n"
" f [offset] [length] - free *** identical to alloc\n"
" m/p [offset] [length] - print map\n"
" s - sync file\n"
" t [offset] - truncate\n"
" q - quit\n"
" h/? - this help\n");
}
int fd = -1;
int blocksize;
char *filename;
/* params are in bytes */
void map(off64_t off, off64_t len)
{
struct getbmap bm[2];
bzero(bm, sizeof(bm));
bm[0].bmv_count = 2;
bm[0].bmv_offset = OFFTOBB(off);
if (len==(off64_t)-1) { /* unsigned... */
bm[0].bmv_length = -1;
printf(" MAP off=%lld, len=%lld [%lld-]\n",
(long long)off, (long long)len,
(long long)BBTOFSB(bm[0].bmv_offset));
} else {
bm[0].bmv_length = OFFTOBB(len);
printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
(long long)off, (long long)len,
(long long)BBTOFSB(bm[0].bmv_offset),
(long long)BBTOFSB(bm[0].bmv_length));
}
printf(" [ofs,count]: start..end\n");
for (;;) {
#ifdef XFS_IOC_GETBMAP
if (xfsctl(filename, fd, XFS_IOC_GETBMAP, bm) < 0) {
#else
#ifdef F_GETBMAP
if (fcntl(fd, F_GETBMAP, bm) < 0) {
#else
bozo!
#endif
#endif
perror("getbmap");
break;
}
if (bm[0].bmv_entries == 0)
break;
printf(" [%lld,%lld]: ",
(long long)BBTOFSB(bm[1].bmv_offset),
(long long)BBTOFSB(bm[1].bmv_length));
if (bm[1].bmv_block == -1)
printf("hole");
else
printf("%lld..%lld",
(long long)BBTOFSB(bm[1].bmv_block),
(long long)BBTOFSB(bm[1].bmv_block +
bm[1].bmv_length - 1));
printf("\n");
}
}
int
main(int argc, char **argv)
{
int c;
char *dirname = NULL;
int done = 0;
int status = 0;
struct flock64 f;
off64_t len;
char line[1024];
off64_t off;
int oflags;
static char *opnames[] = { "freesp",
"allocsp",
"unresvsp",
"resvsp" };
int opno;
/* Assume that if we have FREESP64 then we have the rest */
#ifdef XFS_IOC_FREESP64
#define USE_XFSCTL
static int optab[] = { XFS_IOC_FREESP64,
XFS_IOC_ALLOCSP64,
XFS_IOC_UNRESVSP64,
XFS_IOC_RESVSP64 };
#else
#ifdef F_FREESP64
#define USE_FCNTL
static int optab[] = { F_FREESP64,
F_ALLOCSP64,
F_UNRESVSP64,
F_RESVSP64 };
#else
bozo!
#endif
#endif
int rflag = 0;
struct statvfs64 svfs;
int tflag = 0;
int nflag = 0;
int unlinkit = 0;
int64_t v;
while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
switch (c) {
case 'b':
blocksize = atoi(optarg);
break;
case 'd':
if (filename) {
printf("can't specify both -d and -f\n");
exit(1);
}
dirname = optarg;
break;
case 'f':
if (dirname) {
printf("can't specify both -d and -f\n");
exit(1);
}
filename = optarg;
break;
case 'r':
rflag = 1;
break;
case 't':
tflag = 1;
break;
case 'n':
nflag++;
break;
default:
printf("unknown option\n");
usage();
exit(1);
}
}
if (!dirname && !filename)
dirname = ".";
if (!filename) {
static char tmpfile[] = "allocXXXXXX";
mkstemp(tmpfile);
filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
sprintf(filename, "%s/%s", dirname, tmpfile);
unlinkit = 1;
}
oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
fd = open(filename, oflags, 0666);
if (!nflag) {
printf("alloc:\n");
printf(" filename %s\n", filename);
}
if (fd < 0) {
perror(filename);
exit(1);
}
if (!blocksize) {
if (fstatvfs64(fd, &svfs) < 0) {
perror(filename);
status = 1;
goto done;
}
blocksize = (int)svfs.f_bsize;
}
if (blocksize<0) {
fprintf(stderr,"illegal blocksize %d\n", blocksize);
status = 1;
goto done;
}
printf(" blocksize %d\n", blocksize);
if (rflag) {
struct fsxattr a;
#ifdef XFS_IOC_FSGETXATTR
if (xfsctl(filename, fd, XFS_IOC_FSGETXATTR, &a) < 0) {
perror("XFS_IOC_FSGETXATTR");
status = 1;
goto done;
}
#else
#ifdef F_FSGETXATTR
if (fcntl(fd, F_FSGETXATTR, &a) < 0) {
perror("F_FSGETXATTR");
status = 1;
goto done;
}
#else
bozo!
#endif
#endif
a.fsx_xflags |= XFS_XFLAG_REALTIME;
#ifdef XFS_IOC_FSSETXATTR
if (xfsctl(filename, fd, XFS_IOC_FSSETXATTR, &a) < 0) {
perror("XFS_IOC_FSSETXATTR");
status = 1;
goto done;
}
#else
#ifdef F_FSSETXATTR
if (fcntl(fd, F_FSSETXATTR, &a) < 0) {
perror("F_FSSETXATTR");
status = 1;
goto done;
}
#else
bozo!
#endif
#endif
}
while (!done) {
char *p;
if (!nflag) printf("alloc> ");
fflush(stdout);
if (!fgets(line, 1024, stdin)) break;
p=line+strlen(line);
if (p!=line&&p[-1]=='\n') p[-1]=0;
opno = 0;
switch (line[0]) {
case 'r':
opno++;
case 'u':
opno++;
case 'a':
opno++;
case 'f':
v = strtoll(&line[2], &p, 0);
if (*p == 'b') {
off = FSBTOOFF(v);
p++;
} else
off = v;
f.l_whence = SEEK_SET;
f.l_start = off;
if (*p == '\0')
v = -1;
else
v = strtoll(p, &p, 0);
if (*p == 'b') {
len = FSBTOOFF(v);
p++;
} else
len = v;
printf(" CMD %s, off=%lld, len=%lld\n",
opnames[opno], (long long)off, (long long)len);
f.l_len = len;
#ifdef USE_XFSCTL
c = xfsctl(filename, fd, optab[opno], &f);
#else
#ifdef USE_FCNTL
c = fcntl(fd, optab[opno], &f);
#else
bozo!
#endif
#endif
if (c < 0) {
perror(opnames[opno]);
break;
}
map(off,len);
break;
case 'p':
case 'm':
p = &line[1];
v = strtoll(p, &p, 0);
if (*p == 'b') {
off = FSBTOOFF(v);
p++;
} else
off = v;
if (*p == '\0')
len = -1;
else {
v = strtoll(p, &p, 0);
if (*p == 'b')
len = FSBTOOFF(v);
else
len = v;
}
map(off,len);
break;
case 't':
p = &line[1];
v = strtoll(p, &p, 0);
if (*p == 'b')
off = FSBTOOFF(v);
else
off = v;
printf(" TRUNCATE off=%lld\n", (long long)off);
if (ftruncate64(fd, off) < 0) {
perror("ftruncate");
break;
}
break;
case 's':
printf(" SYNC\n");
fsync(fd);
break;
case 'q':
printf(" QUIT\n");
done = 1;
break;
case '?':
case 'h':
usage();
break;
default:
printf("unknown command '%s'\n", line);
break;
}
}
if (!nflag) printf("\n");
done:
if (fd != -1)
close(fd);
if (unlinkit)
unlink(filename);
exit(status);
/* NOTREACHED */
}