2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 * Block I/O parameterization. A basic block (BB) is the lowest size of
37 * filesystem allocation, and must equal 512. Length units given to bio
38 * routines are in BB's.
41 /* Assume that if we have BTOBB, then we have the rest */
44 #define BBSIZE (1<<BBSHIFT)
45 #define BBMASK (BBSIZE-1)
46 #define BTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
47 #define BTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
48 #define BBTOB(bbs) ((bbs) << BBSHIFT)
49 #define OFFTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
51 #define SEEKLIMIT32 0x7fffffff
52 #define BBSEEKLIMIT32 BTOBBT(SEEKLIMIT32)
53 #define SEEKLIMIT 0x7fffffffffffffffLL
54 #define BBSEEKLIMIT OFFTOBBT(SEEKLIMIT)
58 #define OFFTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
59 #define BBTOOFF(bbs) ((__u64)(bbs) << BBSHIFT)
62 #define FSBTOBB(f) (OFFTOBBT(FSBTOOFF(f)))
63 #define BBTOFSB(b) (OFFTOFSB(BBTOOFF(b)))
64 #define OFFTOFSB(o) ((o) / blocksize)
65 #define FSBTOOFF(f) ((f) * blocksize)
69 printf("usage: alloc [-b blocksize] [-d dir] [-f file] [-n] [-r] [-t]\n"
71 " -n - non-interractive mode\n"
72 " -r - real time file\n"
73 " -t - truncate on open\n"
76 " r [offset] [length] - reserve\n"
77 " u [offset] [length] - unreserve\n"
78 " a [offset] [length] - alloc *** identical to free\n"
79 " f [offset] [length] - free *** identical to alloc\n"
80 " m/p [offset] [length] - print map\n"
82 " t [offset] - truncate\n"
84 " h/? - this help\n");
92 /* params are in bytes */
93 void map(off64_t off, off64_t len)
97 bzero(bm, sizeof(bm));
100 bm[0].bmv_offset = OFFTOBB(off);
101 if (len==(off64_t)-1) { /* unsigned... */
102 bm[0].bmv_length = -1;
103 printf(" MAP off=%lld, len=%lld [%lld-]\n",
104 (long long)off, (long long)len,
105 (long long)BBTOFSB(bm[0].bmv_offset));
107 bm[0].bmv_length = OFFTOBB(len);
108 printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
109 (long long)off, (long long)len,
110 (long long)BBTOFSB(bm[0].bmv_offset),
111 (long long)BBTOFSB(bm[0].bmv_length));
114 printf(" [ofs,count]: start..end\n");
116 #ifdef XFS_IOC_GETBMAP
117 if (xfsctl(filename, fd, XFS_IOC_GETBMAP, bm) < 0) {
120 if (fcntl(fd, F_GETBMAP, bm) < 0) {
129 if (bm[0].bmv_entries == 0)
132 printf(" [%lld,%lld]: ",
133 (long long)BBTOFSB(bm[1].bmv_offset),
134 (long long)BBTOFSB(bm[1].bmv_length));
136 if (bm[1].bmv_block == -1)
140 (long long)BBTOFSB(bm[1].bmv_block),
141 (long long)BBTOFSB(bm[1].bmv_block +
142 bm[1].bmv_length - 1));
148 main(int argc, char **argv)
151 char *dirname = NULL;
159 static char *opnames[] = { "freesp",
165 /* Assume that if we have FREESP64 then we have the rest */
166 #ifdef XFS_IOC_FREESP64
168 static int optab[] = { XFS_IOC_FREESP64,
175 static int optab[] = { F_FREESP64,
184 struct statvfs64 svfs;
190 while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
193 blocksize = atoi(optarg);
197 printf("can't specify both -d and -f\n");
204 printf("can't specify both -d and -f\n");
219 printf("unknown option\n");
224 if (!dirname && !filename)
227 static char tmpfile[] = "allocXXXXXX";
230 filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
231 sprintf(filename, "%s/%s", dirname, tmpfile);
234 oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
235 fd = open(filename, oflags, 0666);
238 printf(" filename %s\n", filename);
245 if (fstatvfs64(fd, &svfs) < 0) {
250 blocksize = (int)svfs.f_bsize;
253 fprintf(stderr,"illegal blocksize %d\n", blocksize);
257 printf(" blocksize %d\n", blocksize);
261 #ifdef XFS_IOC_FSGETXATTR
262 if (xfsctl(filename, fd, XFS_IOC_FSGETXATTR, &a) < 0) {
263 perror("XFS_IOC_FSGETXATTR");
269 if (fcntl(fd, F_FSGETXATTR, &a) < 0) {
270 perror("F_FSGETXATTR");
279 a.fsx_xflags |= XFS_XFLAG_REALTIME;
281 #ifdef XFS_IOC_FSSETXATTR
282 if (xfsctl(filename, fd, XFS_IOC_FSSETXATTR, &a) < 0) {
283 perror("XFS_IOC_FSSETXATTR");
289 if (fcntl(fd, F_FSSETXATTR, &a) < 0) {
290 perror("F_FSSETXATTR");
302 if (!nflag) printf("alloc> ");
304 if (!fgets(line, 1024, stdin)) break;
307 if (p!=line&&p[-1]=='\n') p[-1]=0;
318 v = strtoll(&line[2], &p, 0);
324 f.l_whence = SEEK_SET;
329 v = strtoll(p, &p, 0);
336 printf(" CMD %s, off=%lld, len=%lld\n",
337 opnames[opno], (long long)off, (long long)len);
341 c = xfsctl(filename, fd, optab[opno], &f);
344 c = fcntl(fd, optab[opno], &f);
350 perror(opnames[opno]);
359 v = strtoll(p, &p, 0);
368 v = strtoll(p, &p, 0);
378 v = strtoll(p, &p, 0);
383 printf(" TRUNCATE off=%lld\n", (long long)off);
384 if (ftruncate64(fd, off) < 0) {
402 printf("unknown command '%s'\n", line);
406 if (!nflag) printf("\n");