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 #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");
85 /* params are in bytes */
86 void map(off64_t off, off64_t len)
88 struct getbmap bm[2]={{0}};
91 bm[0].bmv_offset = OFFTOBB(off);
92 if (len==(off64_t)-1) { /* unsigned... */
93 bm[0].bmv_length = -1;
94 printf(" MAP off=%lld, len=%lld [%lld-]\n",
95 (__s64)off, (__s64)len,
96 (__s64)BBTOFSB(bm[0].bmv_offset));
98 bm[0].bmv_length = OFFTOBB(len);
99 printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
100 (__s64)off, (__s64)len,
101 (__s64)BBTOFSB(bm[0].bmv_offset),
102 (__s64)BBTOFSB(bm[0].bmv_length));
105 printf(" [ofs,count]: start..end\n");
107 if (xfsctl(filename, fd, XFS_IOC_GETBMAP, bm) < 0) {
111 if (bm[0].bmv_entries == 0)
113 printf(" [%lld,%lld]: ",
114 (__s64)BBTOFSB(bm[1].bmv_offset),
115 (__s64)BBTOFSB(bm[1].bmv_length));
116 if (bm[1].bmv_block == -1)
120 (__s64)BBTOFSB(bm[1].bmv_block),
121 (__s64)BBTOFSB(bm[1].bmv_block +
122 bm[1].bmv_length - 1));
128 main(int argc, char **argv)
131 char *dirname = NULL;
139 static char *opnames[] =
140 { "freesp", "allocsp", "unresvsp", "resvsp" };
143 { XFS_IOC_FREESP64, XFS_IOC_ALLOCSP64, XFS_IOC_UNRESVSP64, XFS_IOC_RESVSP64 };
145 struct statvfs64 svfs;
151 while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
154 blocksize = atoi(optarg);
158 printf("can't specify both -d and -f\n");
165 printf("can't specify both -d and -f\n");
180 printf("unknown option\n");
185 if (!dirname && !filename)
188 static char tmpfile[] = "allocXXXXXX";
191 filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
192 sprintf(filename, "%s/%s", dirname, tmpfile);
195 oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
196 fd = open(filename, oflags, 0666);
199 printf(" filename %s\n", filename);
206 if (fstatvfs64(fd, &svfs) < 0) {
211 blocksize = (int)svfs.f_bsize;
214 fprintf(stderr,"illegal blocksize %d\n", blocksize);
218 printf(" blocksize %d\n", blocksize);
222 if (xfsctl(filename, fd, XFS_IOC_FSGETXATTR, &a) < 0) {
223 perror("XFS_IOC_FSGETXATTR");
227 a.fsx_xflags |= XFS_XFLAG_REALTIME;
228 if (xfsctl(filename, fd, XFS_IOC_FSSETXATTR, &a) < 0) {
229 perror("XFS_IOC_FSSETXATTR");
237 if (!nflag) printf("alloc> ");
239 if (!fgets(line, 1024, stdin)) break;
242 if (p!=line&&p[-1]=='\n') p[-1]=0;
253 v = strtoll(&line[2], &p, 0);
259 f.l_whence = SEEK_SET;
264 v = strtoll(p, &p, 0);
271 printf(" CMD %s, off=%lld, len=%lld\n",
272 opnames[opno], (__s64)off, (__s64)len);
275 c = xfsctl(filename, fd, optab[opno], &f);
277 perror(opnames[opno]);
286 v = strtoll(p, &p, 0);
295 v = strtoll(p, &p, 0);
305 v = strtoll(p, &p, 0);
310 printf(" TRUNCATE off=%lld\n", (__s64)off);
311 if (ftruncate64(fd, off) < 0) {
329 printf("unknown command '%s'\n", line);
333 if (!nflag) printf("\n");