2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
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.
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.
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
22 * Block I/O parameterization. A basic block (BB) is the lowest size of
23 * filesystem allocation, and must equal 512. Length units given to bio
24 * routines are in BB's.
27 /* Assume that if we have BTOBB, then we have the rest */
30 #define BBSIZE (1<<BBSHIFT)
31 #define BBMASK (BBSIZE-1)
32 #define BTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
33 #define BTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
34 #define BBTOB(bbs) ((bbs) << BBSHIFT)
35 #define OFFTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
37 #define SEEKLIMIT32 0x7fffffff
38 #define BBSEEKLIMIT32 BTOBBT(SEEKLIMIT32)
39 #define SEEKLIMIT 0x7fffffffffffffffLL
40 #define BBSEEKLIMIT OFFTOBBT(SEEKLIMIT)
44 #define OFFTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
45 #define BBTOOFF(bbs) ((__u64)(bbs) << BBSHIFT)
48 #define FSBTOBB(f) (OFFTOBBT(FSBTOOFF(f)))
49 #define BBTOFSB(b) (OFFTOFSB(BBTOOFF(b)))
50 #define OFFTOFSB(o) ((o) / blocksize)
51 #define FSBTOOFF(f) ((f) * blocksize)
55 printf("usage: alloc [-b blocksize] [-d dir] [-f file] [-n] [-r] [-t]\n"
57 " -n - non-interractive mode\n"
58 " -r - real time file\n"
59 " -t - truncate on open\n"
62 " r [offset] [length] - reserve\n"
63 " u [offset] [length] - unreserve\n"
64 " a [offset] [length] - alloc *** identical to free\n"
65 " f [offset] [length] - free *** identical to alloc\n"
66 " m/p [offset] [length] - print map\n"
68 " t [offset] - truncate\n"
70 " h/? - this help\n");
78 /* params are in bytes */
79 void map(off64_t off, off64_t len)
83 bzero(bm, sizeof(bm));
86 bm[0].bmv_offset = OFFTOBB(off);
87 if (len==(off64_t)-1) { /* unsigned... */
88 bm[0].bmv_length = -1;
89 printf(" MAP off=%lld, len=%lld [%lld-]\n",
90 (long long)off, (long long)len,
91 (long long)BBTOFSB(bm[0].bmv_offset));
93 bm[0].bmv_length = OFFTOBB(len);
94 printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
95 (long long)off, (long long)len,
96 (long long)BBTOFSB(bm[0].bmv_offset),
97 (long long)BBTOFSB(bm[0].bmv_length));
100 printf(" [ofs,count]: start..end\n");
102 #ifdef XFS_IOC_GETBMAP
103 if (xfsctl(filename, fd, XFS_IOC_GETBMAP, bm) < 0) {
106 if (fcntl(fd, F_GETBMAP, bm) < 0) {
115 if (bm[0].bmv_entries == 0)
118 printf(" [%lld,%lld]: ",
119 (long long)BBTOFSB(bm[1].bmv_offset),
120 (long long)BBTOFSB(bm[1].bmv_length));
122 if (bm[1].bmv_block == -1)
126 (long long)BBTOFSB(bm[1].bmv_block),
127 (long long)BBTOFSB(bm[1].bmv_block +
128 bm[1].bmv_length - 1));
134 main(int argc, char **argv)
137 char *dirname = NULL;
145 static char *opnames[] = { "freesp",
151 /* Assume that if we have FREESP64 then we have the rest */
152 #ifdef XFS_IOC_FREESP64
154 static int optab[] = { XFS_IOC_FREESP64,
161 static int optab[] = { F_FREESP64,
170 struct statvfs64 svfs;
176 while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
179 blocksize = atoi(optarg);
183 printf("can't specify both -d and -f\n");
190 printf("can't specify both -d and -f\n");
205 printf("unknown option\n");
210 if (!dirname && !filename)
213 static char tmpfile[] = "allocXXXXXX";
216 filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
217 sprintf(filename, "%s/%s", dirname, tmpfile);
220 oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
221 fd = open(filename, oflags, 0666);
224 printf(" filename %s\n", filename);
231 if (fstatvfs64(fd, &svfs) < 0) {
236 blocksize = (int)svfs.f_bsize;
239 fprintf(stderr,"illegal blocksize %d\n", blocksize);
243 printf(" blocksize %d\n", blocksize);
247 #ifdef XFS_IOC_FSGETXATTR
248 if (xfsctl(filename, fd, XFS_IOC_FSGETXATTR, &a) < 0) {
249 perror("XFS_IOC_FSGETXATTR");
255 if (fcntl(fd, F_FSGETXATTR, &a) < 0) {
256 perror("F_FSGETXATTR");
265 a.fsx_xflags |= XFS_XFLAG_REALTIME;
267 #ifdef XFS_IOC_FSSETXATTR
268 if (xfsctl(filename, fd, XFS_IOC_FSSETXATTR, &a) < 0) {
269 perror("XFS_IOC_FSSETXATTR");
275 if (fcntl(fd, F_FSSETXATTR, &a) < 0) {
276 perror("F_FSSETXATTR");
288 if (!nflag) printf("alloc> ");
290 if (!fgets(line, 1024, stdin)) break;
293 if (p!=line&&p[-1]=='\n') p[-1]=0;
304 v = strtoll(&line[2], &p, 0);
310 f.l_whence = SEEK_SET;
315 v = strtoll(p, &p, 0);
322 printf(" CMD %s, off=%lld, len=%lld\n",
323 opnames[opno], (long long)off, (long long)len);
327 c = xfsctl(filename, fd, optab[opno], &f);
330 c = fcntl(fd, optab[opno], &f);
336 perror(opnames[opno]);
345 v = strtoll(p, &p, 0);
354 v = strtoll(p, &p, 0);
364 v = strtoll(p, &p, 0);
369 printf(" TRUNCATE off=%lld\n", (long long)off);
370 if (ftruncate64(fd, off) < 0) {
388 printf("unknown command '%s'\n", line);
392 if (!nflag) printf("\n");