2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <sys/statvfs.h>
26 #include "elliptics/interface.h"
28 static void ids_usage(char *p
)
30 fprintf(stderr
, "Usage: %s <options>\n"
31 " -i file - ids file\n"
32 " -r file - random file\n"
33 " -d dir - storage dir path\n"
34 " -t - use total size of the storage device instead of free size\n"
40 static int ids_append(char *random_file
, int fd
, unsigned long long diff
)
42 unsigned long long sz
= 1024 * 1024;
46 rnd
= open(random_file
, O_RDONLY
);
49 fprintf(stderr
, "Failed to open random file '%s': %s [%d]\n",
50 random_file
, strerror(errno
), errno
);
64 err
= read(rnd
, buf
, sz
);
66 fprintf(stderr
, "Failed to read from random file '%s': %s [%d]\n",
67 random_file
, strerror(errno
), errno
);
71 err
= write(fd
, buf
, err
);
73 fprintf(stderr
, "Failed to write into ids file: %s [%d]\n",
74 strerror(errno
), errno
);
90 static int ids_update(char *ids_file
, char *random_file
, unsigned long long new_size
)
94 unsigned long long new_num
, old_num
;
96 fd
= open(ids_file
, O_RDWR
| O_CREAT
| O_APPEND
, 0644);
99 fprintf(stderr
, "Faield to open ids file '%s': %s [%d]\n",
100 ids_file
, strerror(errno
), errno
);
104 err
= fstat(fd
, &st
);
107 fprintf(stderr
, "Faield to stat ids file '%s': %s [%d]\n",
108 ids_file
, strerror(errno
), errno
);
112 old_num
= st
.st_size
/ DNET_ID_SIZE
;
113 new_num
= new_size
/ (100 * 1024 * 1024 * 1024ULL) + 1;
115 if (new_num
< old_num
) {
116 err
= ftruncate(fd
, new_num
* DNET_ID_SIZE
);
119 fprintf(stderr
, "Faield to truncate ids file '%s': %llu -> %llu (in IDs, not bytes): %s [%d]\n",
120 ids_file
, old_num
, new_num
, strerror(errno
), errno
);
124 err
= ids_append(random_file
, fd
, (new_num
- old_num
) * DNET_ID_SIZE
);
130 fprintf(stderr
, "Updated '%s': %llu -> %llu (in IDs, not bytes)\n",
131 ids_file
, old_num
, new_num
);
139 int main(int argc
, char *argv
[])
143 char *random
= "/dev/urandom";
147 unsigned long long storage_size
;
149 while ((ch
= getopt(argc
, argv
, "i:r:d:th")) != -1) {
171 fprintf(stderr
, "Both ids and dir options must be specified\n");
176 err
= statvfs(dir
, &s
);
179 fprintf(stderr
, "Failed to get VFS statistics of '%s': %s [%d].\n",
180 dir
, strerror(errno
), errno
);
185 storage_size
= s
.f_frsize
* s
.f_blocks
;
187 storage_size
= s
.f_bsize
* s
.f_bavail
;
189 return ids_update(ids
, random
, storage_size
);