2 * Copyright (c) 2000-2001 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 #define BBSIZE (1<<BBSHIFT)
42 #define BBMASK (BBSIZE-1)
43 #define BTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
44 #define BTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
45 #define BBTOB(bbs) ((bbs) << BBSHIFT)
46 #define OFFTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
47 #define OFFTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
48 #define BBTOOFF(bbs) ((__u64)(bbs) << BBSHIFT)
50 #define SEEKLIMIT32 0x7fffffff
51 #define BBSEEKLIMIT32 BTOBBT(SEEKLIMIT32)
52 #define SEEKLIMIT 0x7fffffffffffffffLL
53 #define BBSEEKLIMIT OFFTOBBT(SEEKLIMIT)
55 #define FSBTOBB(f) (OFFTOBBT(FSBTOOFF(f)))
56 #define BBTOFSB(b) (OFFTOFSB(BBTOOFF(b)))
57 #define OFFTOFSB(o) ((o) / blocksize)
58 #define FSBTOOFF(f) ((f) * blocksize)
62 printf("usage: alloc [-b blocksize] [-d dir] [-f file] [-n] [-r] [-t]\n"
64 " -n - non-interractive mode\n"
65 " -r - real time file\n"
66 " -t - truncate on open\n"
69 " r [offset] [length] - reserve\n"
70 " u [offset] [length] - unreserve\n"
71 " a [offset] [length] - alloc *** identical to free\n"
72 " f [offset] [length] - free *** identical to alloc\n"
73 " m/p [offset] [length] - print map\n"
75 " t [offset] - truncate\n"
77 " h/? - this help\n");
84 /* params are in bytes */
85 void map(off64_t off, off64_t len)
87 struct getbmap bm[2]={{0}};
90 bm[0].bmv_offset = OFFTOBB(off);
91 if (len==(off64_t)-1) { /* unsigned... */
92 bm[0].bmv_length = -1;
93 printf(" MAP off=%lld, len=%lld [%lld-]\n",
94 (__s64)off, (__s64)len,
95 (__s64)BBTOFSB(bm[0].bmv_offset));
97 bm[0].bmv_length = OFFTOBB(len);
98 printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
99 (__s64)off, (__s64)len,
100 (__s64)BBTOFSB(bm[0].bmv_offset),
101 (__s64)BBTOFSB(bm[0].bmv_length));
104 printf(" [ofs,count]: start..end\n");
106 if (ioctl(fd, XFS_IOC_GETBMAP, bm) < 0) {
110 if (bm[0].bmv_entries == 0)
112 printf(" [%lld,%lld]: ",
113 (__s64)BBTOFSB(bm[1].bmv_offset),
114 (__s64)BBTOFSB(bm[1].bmv_length));
115 if (bm[1].bmv_block == -1)
119 (__s64)BBTOFSB(bm[1].bmv_block),
120 (__s64)BBTOFSB(bm[1].bmv_block +
121 bm[1].bmv_length - 1));
127 main(int argc, char **argv)
130 char *dirname = NULL;
133 char *filename = NULL;
138 static char *opnames[] =
139 { "freesp", "allocsp", "unresvsp", "resvsp" };
142 { XFS_IOC_FREESP64, XFS_IOC_ALLOCSP64, XFS_IOC_UNRESVSP64, XFS_IOC_RESVSP64 };
144 struct statvfs64 svfs;
150 while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
153 blocksize = atoi(optarg);
157 printf("can't specify both -d and -f\n");
164 printf("can't specify both -d and -f\n");
179 printf("unknown option\n");
184 if (!dirname && !filename)
187 static char tmpfile[] = "allocXXXXXX";
190 filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
191 sprintf(filename, "%s/%s", dirname, tmpfile);
194 oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
195 fd = open(filename, oflags, 0666);
198 printf(" filename %s\n", filename);
207 if (fstatvfs64(fd, &svfs) < 0) {
211 blocksize = (int)svfs.f_bsize;
214 fprintf(stderr,"illegal blocksize %d\n", blocksize);
217 printf(" blocksize %d\n", blocksize);
221 if (ioctl(fd, XFS_IOC_FSGETXATTR, &a) < 0) {
222 perror("XFS_IOC_FSGETXATTR");
225 a.fsx_xflags |= XFS_XFLAG_REALTIME;
226 if (ioctl(fd, XFS_IOC_FSSETXATTR, &a) < 0) {
227 perror("XFS_IOC_FSSETXATTR");
234 if (!nflag) printf("alloc> ");
236 if (!fgets(line, 1024, stdin)) break;
239 if (p!=line&&p[-1]=='\n') p[-1]=0;
250 v = strtoll(&line[2], &p, 0);
256 f.l_whence = SEEK_SET;
261 v = strtoll(p, &p, 0);
268 printf(" CMD %s, off=%lld, len=%lld\n",
269 opnames[opno], (__s64)off, (__s64)len);
272 c = ioctl(fd, optab[opno], &f);
274 perror(opnames[opno]);
283 v = strtoll(p, &p, 0);
292 v = strtoll(p, &p, 0);
302 v = strtoll(p, &p, 0);
307 printf(" TRUNCATE off=%lld\n", (__s64)off);
308 if (ftruncate64(fd, off) < 0) {
326 printf("unknown command '%s'\n", line);
330 if (!nflag) printf("\n");