1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
10 * Block I/O parameterization. A basic block (BB) is the lowest size of
11 * filesystem allocation, and must equal 512. Length units given to bio
12 * routines are in BB's.
15 /* Assume that if we have BTOBB, then we have the rest */
18 #define BBSIZE (1<<BBSHIFT)
19 #define BBMASK (BBSIZE-1)
20 #define BTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
21 #define BTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
22 #define BBTOB(bbs) ((bbs) << BBSHIFT)
23 #define OFFTOBBT(bytes) ((__u64)(bytes) >> BBSHIFT)
25 #define SEEKLIMIT32 0x7fffffff
26 #define BBSEEKLIMIT32 BTOBBT(SEEKLIMIT32)
27 #define SEEKLIMIT 0x7fffffffffffffffLL
28 #define BBSEEKLIMIT OFFTOBBT(SEEKLIMIT)
32 #define OFFTOBB(bytes) (((__u64)(bytes) + BBSIZE - 1) >> BBSHIFT)
33 #define BBTOOFF(bbs) ((__u64)(bbs) << BBSHIFT)
36 #define FSBTOBB(f) (OFFTOBBT(FSBTOOFF(f)))
37 #define BBTOFSB(b) (OFFTOFSB(BBTOOFF(b)))
38 #define OFFTOFSB(o) ((o) / blocksize)
39 #define FSBTOOFF(f) ((f) * blocksize)
43 printf("usage: alloc [-b blocksize] [-d dir] [-f file] [-n] [-r] [-t]\n"
45 " -n - non-interractive mode\n"
46 " -r - real time file\n"
47 " -t - truncate on open\n"
50 " r [offset] [length] - reserve\n"
51 " u [offset] [length] - unreserve\n"
52 " a [offset] [length] - alloc *** identical to free\n"
53 " f [offset] [length] - free *** identical to alloc\n"
54 " m/p [offset] [length] - print map\n"
56 " t [offset] - truncate\n"
58 " h/? - this help\n");
66 /* params are in bytes */
67 void map(off64_t off, off64_t len)
71 bzero(bm, sizeof(bm));
74 bm[0].bmv_offset = OFFTOBB(off);
75 if (len==(off64_t)-1) { /* unsigned... */
76 bm[0].bmv_length = -1;
77 printf(" MAP off=%lld, len=%lld [%lld-]\n",
78 (long long)off, (long long)len,
79 (long long)BBTOFSB(bm[0].bmv_offset));
81 bm[0].bmv_length = OFFTOBB(len);
82 printf(" MAP off=%lld, len=%lld [%lld,%lld]\n",
83 (long long)off, (long long)len,
84 (long long)BBTOFSB(bm[0].bmv_offset),
85 (long long)BBTOFSB(bm[0].bmv_length));
88 printf(" [ofs,count]: start..end\n");
90 #ifdef XFS_IOC_GETBMAP
91 if (xfsctl(filename, fd, XFS_IOC_GETBMAP, bm) < 0) {
94 if (fcntl(fd, F_GETBMAP, bm) < 0) {
103 if (bm[0].bmv_entries == 0)
106 printf(" [%lld,%lld]: ",
107 (long long)BBTOFSB(bm[1].bmv_offset),
108 (long long)BBTOFSB(bm[1].bmv_length));
110 if (bm[1].bmv_block == -1)
114 (long long)BBTOFSB(bm[1].bmv_block),
115 (long long)BBTOFSB(bm[1].bmv_block +
116 bm[1].bmv_length - 1));
122 main(int argc, char **argv)
125 char *dirname = NULL;
133 static char *opnames[] = { "freesp",
139 /* Assume that if we have FREESP64 then we have the rest */
140 #ifdef XFS_IOC_FREESP64
142 static int optab[] = { XFS_IOC_FREESP64,
149 static int optab[] = { F_FREESP64,
158 struct statvfs64 svfs;
164 while ((c = getopt(argc, argv, "b:d:f:rtn")) != -1) {
167 blocksize = atoi(optarg);
171 printf("can't specify both -d and -f\n");
178 printf("can't specify both -d and -f\n");
193 printf("unknown option\n");
198 if (!dirname && !filename)
201 static char tmpfile[] = "allocXXXXXX";
204 filename = malloc(strlen(tmpfile) + strlen(dirname) + 2);
205 sprintf(filename, "%s/%s", dirname, tmpfile);
208 oflags = O_RDWR | O_CREAT | (tflag ? O_TRUNC : 0);
209 fd = open(filename, oflags, 0666);
212 printf(" filename %s\n", filename);
219 if (fstatvfs64(fd, &svfs) < 0) {
224 blocksize = (int)svfs.f_bsize;
227 fprintf(stderr,"illegal blocksize %d\n", blocksize);
231 printf(" blocksize %d\n", blocksize);
235 #ifdef XFS_IOC_FSGETXATTR
236 if (xfsctl(filename, fd, XFS_IOC_FSGETXATTR, &a) < 0) {
237 perror("XFS_IOC_FSGETXATTR");
243 if (fcntl(fd, F_FSGETXATTR, &a) < 0) {
244 perror("F_FSGETXATTR");
253 a.fsx_xflags |= XFS_XFLAG_REALTIME;
255 #ifdef XFS_IOC_FSSETXATTR
256 if (xfsctl(filename, fd, XFS_IOC_FSSETXATTR, &a) < 0) {
257 perror("XFS_IOC_FSSETXATTR");
263 if (fcntl(fd, F_FSSETXATTR, &a) < 0) {
264 perror("F_FSSETXATTR");
276 if (!nflag) printf("alloc> ");
278 if (!fgets(line, 1024, stdin)) break;
281 if (p!=line&&p[-1]=='\n') p[-1]=0;
292 v = strtoll(&line[2], &p, 0);
298 f.l_whence = SEEK_SET;
303 v = strtoll(p, &p, 0);
310 printf(" CMD %s, off=%lld, len=%lld\n",
311 opnames[opno], (long long)off, (long long)len);
315 c = xfsctl(filename, fd, optab[opno], &f);
318 c = fcntl(fd, optab[opno], &f);
324 perror(opnames[opno]);
333 v = strtoll(p, &p, 0);
342 v = strtoll(p, &p, 0);
352 v = strtoll(p, &p, 0);
357 printf(" TRUNCATE off=%lld\n", (long long)off);
358 if (ftruncate64(fd, off) < 0) {
376 printf("unknown command '%s'\n", line);
380 if (!nflag) printf("\n");