1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
/*
Copyright (C) 2008- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#include "chirp_alloc.h"
#include "chirp_filesystem.h"
#include "chirp_protocol.h"
#include "debug.h"
#include "hash_table.h"
#include "int_sizes.h"
#include "itable.h"
#include "macros.h"
#include "path.h"
#include "stringtools.h"
#include "xxmalloc.h"
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* On why HDFS can't do quotas (allocations) [1]:
*
* In the current implementation, quotas (allocations) do not work
* with HDFS b/c the chirp_alloc module stores the allocation
* information in the Unix filesystem and relies upon file locking
* and signals to ensure mutual exclusion. Modifying the code to
* store it in cfs instead of Unix would be easy, but hdfs still
* doesn't support file locking. (Nor does any other distributed
* file system.)
*
* An alternative approach would be to store the allocation data in
* a database alongside the filesystem. This has some pros and cons
* to be worked out.
*
* Addendum: flock is now part of CFS, but HDFS has a stub for it.
*
* [1] https://github.com/batrick/cctools/commit/377377f54e7660c8571d3088487b00c8ad2d2d7d#commitcomment-4265178
*/
static int alloc_enabled = 0;
static struct hash_table *alloc_table = 0;
static time_t last_flush_time = 0;
static int recovery_in_progress = 0;
static struct hash_table *root_table = 0;
struct alloc_state {
int fd;
INT64_T size;
INT64_T inuse;
INT64_T avail;
INT64_T dirty;
};
/*
Note that the space consumed by a file is not the same
as the filesize. This function computes the space consumed
by a file of a given size. Currently, it rounds up to
the next blocksize. A more exact function might take into
account indirect blocks allocated within the filesystem.
*/
static INT64_T space_consumed(INT64_T filesize)
{
INT64_T block_size = 4096;
INT64_T blocks = filesize / block_size;
if(filesize % block_size)
blocks++;
return blocks * block_size;
}
static void alloc_state_update(struct alloc_state *a, INT64_T change)
{
if(change != 0) {
a->inuse += change;
if(a->inuse < 0)
a->inuse = 0;
a->avail = a->size - a->inuse;
a->dirty = 1;
}
}
static struct alloc_state *alloc_state_load(const char *path)
{
struct alloc_state *s = xxmalloc(sizeof(*s));
char statename[CHIRP_PATH_MAX];
char buffer[4096]; /* any .__alloc file is smaller than this */
debug(D_ALLOC, "locking %s", path);
string_nformat(statename, sizeof(statename), "%s/.__alloc", path);
s->fd = cfs->open(statename, O_RDWR, S_IRUSR|S_IWUSR);
if(s->fd == -1) {
free(s);
return 0;
}
if(cfs->lockf(s->fd, F_TLOCK, 0)) {
debug(D_ALLOC, "lock of %s blocked; flushing outstanding locks", path);
chirp_alloc_flush();
debug(D_ALLOC, "locking %s (retry)", path);
if(cfs->lockf(s->fd, F_LOCK, 0)) {
debug(D_ALLOC, "lock of %s failed: %s", path, strerror(errno));
cfs->close(s->fd);
free(s);
return 0;
}
}
memset(buffer, 0, sizeof(buffer));
INT64_T result = cfs->pread(s->fd, buffer, sizeof(buffer), 0);
assert(0 < result && result < (INT64_T)sizeof(buffer));
result = sscanf(buffer, "%" SCNd64 " %" SCNd64, &s->size, &s->inuse);
assert(result == 2);
s->dirty = 0;
if(recovery_in_progress) {
s->inuse = 0;
s->dirty = 1;
}
s->avail = s->size - s->inuse;
return s;
}
static void alloc_state_save(const char *path, struct alloc_state *s)
{
if(s->dirty) {
debug(D_ALLOC, "storing %s", path);
} else {
debug(D_ALLOC, "freeing %s", path);
}
if(s->dirty) {
char buffer[4096];
cfs->ftruncate(s->fd, 0);
string_nformat(buffer, sizeof(buffer), "%" PRId64 "\n%" PRId64 "\n", s->size, s->inuse);
int64_t result = cfs->pwrite(s->fd, buffer, strlen(buffer), 0);
assert(result == (int64_t) strlen(buffer));
}
cfs->close(s->fd);
free(s);
}
static int alloc_state_create(const char *path, INT64_T size)
{
char statepath[CHIRP_PATH_MAX];
int fd;
string_nformat(statepath, sizeof(statepath), "%s/.__alloc", path);
fd = cfs->open(statepath, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if(fd >= 0) {
char buffer[4096];
string_nformat(buffer, sizeof(buffer), "%" PRId64 " 0\n", size);
int64_t result = cfs->pwrite(fd, buffer, strlen(buffer), 0);
assert(result == (int64_t) strlen(buffer));
cfs->close(fd);
return 1;
} else {
return 0;
}
}
static char *alloc_state_root(const char *path)
{
char dirname[CHIRP_PATH_MAX];
string_nformat(dirname, sizeof(dirname), "%s", path);
while(1) {
char statename[CHIRP_PATH_MAX];
string_nformat(statename, sizeof(statename), "%s/.__alloc", dirname);
if(cfs_file_size(statename) >= 0) {
if (dirname[0])
return xxstrdup(dirname);
else
return xxstrdup("/");
}
char *s = strrchr(dirname, '/');
if(!s)
return 0;
*s = 0;
}
return 0;
}
static char *alloc_state_root_cached(const char *path)
{
char *result;
result = hash_table_lookup(root_table, path);
if(result)
return result;
result = alloc_state_root(path);
if(!result)
return 0;
hash_table_insert(root_table, path, result);
return result;
}
static struct alloc_state *alloc_state_cache_exact(const char *path)
{
struct alloc_state *a;
char *d;
char dirname[CHIRP_PATH_MAX];
d = alloc_state_root_cached(path);
if(!d)
return 0;
/*
Save a copy of dirname, because the following
alloc_table_load_cached may result in a flush of the alloc table root.
*/
strcpy(dirname, d);
a = hash_table_lookup(alloc_table, dirname);
if(a)
return a;
a = alloc_state_load(dirname);
if(!a)
return a;
hash_table_insert(alloc_table, dirname, a);
return a;
}
static struct alloc_state *alloc_state_cache(const char *path)
{
char dirname[CHIRP_PATH_MAX];
path_dirname(path, dirname);
return alloc_state_cache_exact(dirname);
}
static void recover(const char *path)
{
char newpath[CHIRP_PATH_MAX];
struct alloc_state *a, *b;
struct chirp_dir *dir;
struct chirp_dirent *d;
a = alloc_state_cache_exact(path);
if(!a)
fatal("couldn't open alloc state in %s: %s", path, strerror(errno));
dir = cfs->opendir(path);
if(!dir)
fatal("couldn't open %s: %s\n", path, strerror(errno));
while((d = cfs->readdir(dir))) {
if(!strcmp(d->name, "."))
continue;
if(!strcmp(d->name, ".."))
continue;
if(!strncmp(d->name, ".__", 3))
continue;
string_nformat(newpath, sizeof(newpath), "%s/%s", path, d->name);
if(S_ISDIR(d->info.cst_mode)) {
recover(newpath);
b = alloc_state_cache_exact(newpath);
if(a != b)
alloc_state_update(a, b->size);
} else if(S_ISREG(d->info.cst_mode)) {
alloc_state_update(a, space_consumed(d->info.cst_size));
} else {
debug(D_ALLOC, "warning: unknown file type: %s\n", newpath);
}
}
cfs->closedir(dir);
debug(D_ALLOC, "%s (%sB)", path, string_metric(a->inuse, -1, 0));
}
int chirp_alloc_init(INT64_T size)
{
struct alloc_state *a;
time_t start, stop;
INT64_T inuse, avail;
alloc_enabled = 0;
if(size == 0) {
return 0;
} else if (cfs->lockf(-1, F_TEST, 0) == -1 && errno == ENOSYS) {
return -1;
}
alloc_enabled = 1;
recovery_in_progress = 1;
assert(alloc_table == NULL);
alloc_table = hash_table_create(0, 0);
assert(root_table == NULL);
root_table = hash_table_create(0, 0);
debug(D_ALLOC, "### begin allocation recovery scan ###");
if(!alloc_state_create("/", size)) {
debug(D_ALLOC, "couldn't create allocation in `/': %s\n", strerror(errno));
return -1;
}
a = alloc_state_cache_exact("/");
if(!a) {
debug(D_ALLOC, "couldn't find allocation in `/': %s\n", strerror(errno));
return -1;
}
start = time(0);
recover("/");
size = a->size;
inuse = a->inuse;
avail = a->avail;
chirp_alloc_flush();
stop = time(0);
debug(D_ALLOC, "### allocation recovery took %d seconds ###", (int) (stop-start) );
debug(D_ALLOC, "%sB total", string_metric(size, -1, 0));
debug(D_ALLOC, "%sB in use", string_metric(inuse, -1, 0));
debug(D_ALLOC, "%sB available", string_metric(avail, -1, 0));
recovery_in_progress = 0;
return 0;
}
void chirp_alloc_flush()
{
char *path, *root;
struct alloc_state *s;
if(!alloc_enabled)
return;
debug(D_ALLOC, "flushing allocation states...");
hash_table_firstkey(alloc_table);
while(hash_table_nextkey(alloc_table, &path, (void **) &s)) {
alloc_state_save(path, s);
hash_table_remove(alloc_table, path);
}
hash_table_firstkey(root_table);
while(hash_table_nextkey(root_table, &path, (void **) &root)) {
free(root);
hash_table_remove(root_table, path);
}
last_flush_time = time(0);
}
int chirp_alloc_flush_needed()
{
if(!alloc_enabled)
return 0;
return hash_table_size(alloc_table);
}
time_t chirp_alloc_last_flush_time()
{
return last_flush_time;
}
INT64_T chirp_alloc_realloc (const char *path, INT64_T change, INT64_T *current)
{
struct alloc_state *a;
int result;
INT64_T dummy;
if (current == NULL)
current = &dummy;
if(!alloc_enabled) {
*current = 0;
return 0;
}
debug(D_ALLOC, "path `%s' change = %" PRId64, path, change);
a = alloc_state_cache(path);
if(a) {
/* FIXME this won't work with symlinks, problem existed before probably */
*current = cfs_file_size(path);
if(*current == -1 && errno == ENOENT)
*current = 0;
if(*current >= 0) {
if(change == *current) {
/* NOP */
result = 0;
} else {
INT64_T alloc_change = space_consumed(change) - space_consumed(*current);
debug(D_ALLOC, "path `%s' actual change = %" PRId64 " from current = %" PRId64, path, alloc_change, *current);
if(a->avail >= alloc_change) {
alloc_state_update(a, alloc_change);
result = 0;
} else {
errno = ENOSPC;
result = -1;
}
}
} else {
result = -1;
}
} else {
result = -1;
}
return result;
}
INT64_T chirp_alloc_frealloc (int fd, INT64_T change, INT64_T *current)
{
char path[CHIRP_PATH_MAX];
if (cfs->fname(fd, path) == -1) return -1;
return chirp_alloc_realloc(path, change, current);
}
INT64_T chirp_alloc_statfs(const char *path, struct chirp_statfs * info)
{
struct alloc_state *a;
int result;
if(!alloc_enabled)
return cfs->statfs(path, info);
a = alloc_state_cache(path);
if(a) {
result = cfs->statfs(path, info);
if(result == 0) {
info->f_blocks = a->size / info->f_bsize;
info->f_bavail = a->avail / info->f_bsize;
info->f_bfree = a->avail / info->f_bsize;
if(a->avail < 0) {
info->f_bavail = 0;
info->f_bfree = 0;
}
}
} else {
result = -1;
}
return result;
}
INT64_T chirp_alloc_fstatfs(int fd, struct chirp_statfs *buf)
{
char path[CHIRP_PATH_MAX];
if (cfs->fname(fd, path) == -1) return -1;
return chirp_alloc_statfs(path, buf);
}
INT64_T chirp_alloc_lsalloc(const char *path, char *alloc_path, INT64_T * total, INT64_T * inuse)
{
int result = -1;
if(!alloc_enabled) {
errno = ENOSYS;
return -1;
}
char *name = alloc_state_root_cached(path);
if(name) {
struct alloc_state *a = alloc_state_cache_exact(name);
if(a) {
strcpy(alloc_path, name);
*total = a->size;
*inuse = a->inuse;
result = 0;
} else {
result = -1;
}
} else {
result = -1;
}
return result;
}
INT64_T chirp_alloc_mkalloc(const char *path, INT64_T size, INT64_T mode)
{
struct alloc_state *a;
int result = -1;
if(!alloc_enabled) {
errno = ENOSYS;
return -1;
}
a = alloc_state_cache(path);
if(a) {
if(a->avail > size) {
result = cfs->mkdir(path, mode);
if(result == 0) {
if(alloc_state_create(path, size)) {
alloc_state_update(a, size);
debug(D_ALLOC, "mkalloc %s %"PRId64, path, size);
chirp_alloc_flush();
} else {
result = -1;
}
}
} else {
errno = ENOSPC;
return -1;
}
} else {
return -1;
}
return result;
}
/* vim: set noexpandtab tabstop=4: */
|