2 * Copyright (C) 2010 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
26 #include <sys/types.h>
34 #include "kerncompat.h"
36 #include "transaction.h"
40 static u64
parse_size(char *s
)
46 if (!isdigit(s
[len
- 1])) {
47 c
= tolower(s
[len
- 1]);
58 fprintf(stderr
, "Unknown size descriptor %c\n", c
);
63 return atoll(s
) * mult
;
66 static void print_usage(void)
68 printf("usage: btrfs-defrag [-c] [-f] [-s start] [-l len] "
69 "[-t threshold] file ...\n");
73 int main(int ac
, char **av
)
85 struct btrfs_ioctl_defrag_range_args range
;
88 int c
= getopt(ac
, av
, "vcfs:l:t:");
102 start
= parse_size(optarg
);
105 len
= parse_size(optarg
);
108 thresh
= parse_size(optarg
);
115 if (ac
- optind
== 0)
118 memset(&range
, 0, sizeof(range
));
121 range
.extent_thresh
= thresh
;
123 range
.flags
|= BTRFS_DEFRAG_RANGE_COMPRESS
;
125 range
.flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
127 for (i
= optind
; i
< ac
; i
++) {
128 fd
= open(av
[i
], O_RDWR
);
130 fprintf(stderr
, "failed to open %s\n", av
[i
]);
135 ret
= ioctl(fd
, BTRFS_IOC_DEFRAG_RANGE
, &range
);
137 fprintf(stderr
, "ioctl failed on %s ret %d\n",
144 printf("%s\n", BTRFS_BUILD_VERSION
);
146 fprintf(stderr
, "total %d failures\n", errors
);