| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <errno.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/stat.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <libgen.h> |
| 15 |
#ifdef GF_LINUX_HOST_OS |
| 16 |
#include <alloca.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include "posix-handle.h" |
| 20 |
#include "posix.h" |
| 21 |
#include <glusterfs/syscall.h> |
| 22 |
#include "posix-messages.h" |
| 23 |
#include "posix-metadata.h" |
| 24 |
|
| 25 |
#include <glusterfs/compat-errno.h> |
| 26 |
|
| 27 |
static int |
| 28 |
posix_handle_mkdir_hashes(xlator_t *this, int dfd, uuid_t gfid); |
| 29 |
|
| 30 |
inode_t * |
| 31 |
posix_resolve(xlator_t *this, inode_table_t *itable, inode_t *parent, |
| 32 |
char *bname, struct iatt *iabuf) |
| 33 |
{ |
| 34 |
inode_t *inode = NULL; |
| 35 |
int ret = -1; |
| 36 |
|
| 37 |
ret = posix_istat(this, NULL, parent->gfid, bname, iabuf, _gf_false); |
| 38 |
if (ret < 0) { |
| 39 |
gf_log(this->name, GF_LOG_WARNING, |
| 40 |
"gfid: %s, bname: %s " |
| 41 |
"failed", |
| 42 |
uuid_utoa(parent->gfid), bname); |
| 43 |
goto out; |
| 44 |
} |
| 45 |
|
| 46 |
if (__is_root_gfid(iabuf->ia_gfid) && !strcmp(bname, "/")) { |
| 47 |
inode = itable->root; |
| 48 |
} else { |
| 49 |
inode = inode_find(itable, iabuf->ia_gfid); |
| 50 |
if (inode == NULL) { |
| 51 |
inode = inode_new(itable); |
| 52 |
gf_uuid_copy(inode->gfid, iabuf->ia_gfid); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
ret = posix_get_mdata_xattr(this, NULL, -1, inode, iabuf); |
| 61 |
if (ret) { |
| 62 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_GETMDATA_FAILED, |
| 63 |
"posix get mdata failed on gfid:%s", uuid_utoa(inode->gfid)); |
| 64 |
goto out; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
out: |
| 82 |
return inode; |
| 83 |
} |
| 84 |
|
| 85 |
static int |
| 86 |
posix_make_ancestral_node(const char *priv_base_path, char *path, int pathsize, |
| 87 |
gf_dirent_t *head, char *dir_name, struct iatt *iabuf, |
| 88 |
inode_t *inode, int type, dict_t *xdata) |
| 89 |
{ |
| 90 |
gf_dirent_t *entry = NULL; |
| 91 |
char real_path[PATH_MAX + 1] = |
| 92 |
{ |
| 93 |
0, |
| 94 |
}, |
| 95 |
len = 0; |
| 96 |
loc_t loc = { |
| 97 |
0, |
| 98 |
}; |
| 99 |
int ret = -1; |
| 100 |
const size_t dir_name_len = strlen(dir_name); |
| 101 |
|
| 102 |
len = strlen(path) + dir_name_len + 1; |
| 103 |
if (len > pathsize) { |
| 104 |
goto out; |
| 105 |
} |
| 106 |
|
| 107 |
strcat(path, dir_name); |
| 108 |
if (*dir_name != '/') |
| 109 |
strcat(path, "/"); |
| 110 |
|
| 111 |
if (type & POSIX_ANCESTRY_DENTRY) { |
| 112 |
entry = gf_dirent_for_name2(dir_name, dir_name_len, -1, 0, 0, iabuf); |
| 113 |
if (!entry) |
| 114 |
goto out; |
| 115 |
|
| 116 |
entry->inode = inode_ref(inode); |
| 117 |
|
| 118 |
list_add_tail(&entry->list, &head->list); |
| 119 |
snprintf(real_path, sizeof(real_path), "%s/%s", priv_base_path, path); |
| 120 |
loc.inode = inode_ref(inode); |
| 121 |
gf_uuid_copy(loc.gfid, inode->gfid); |
| 122 |
|
| 123 |
entry->dict = posix_xattr_fill(THIS, real_path, &loc, NULL, -1, xdata, |
| 124 |
iabuf); |
| 125 |
loc_wipe(&loc); |
| 126 |
} |
| 127 |
|
| 128 |
ret = 0; |
| 129 |
|
| 130 |
out: |
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
int |
| 135 |
posix_make_ancestryfromgfid(xlator_t *this, char *path, int pathsize, |
| 136 |
gf_dirent_t *head, int type, uuid_t gfid, |
| 137 |
const size_t handle_size, |
| 138 |
const char *priv_base_path, inode_table_t *itable, |
| 139 |
inode_t **parent, dict_t *xdata, int32_t *op_errno) |
| 140 |
{ |
| 141 |
char *linkname = NULL; |
| 142 |
|
| 143 |
char *dir_handle = NULL; |
| 144 |
char *pgfidstr = NULL; |
| 145 |
char *saveptr = NULL; |
| 146 |
ssize_t len = 0; |
| 147 |
inode_t *inode = NULL; |
| 148 |
struct iatt iabuf = { |
| 149 |
0, |
| 150 |
}; |
| 151 |
int ret = -1; |
| 152 |
uuid_t tmp_gfid = { |
| 153 |
0, |
| 154 |
}; |
| 155 |
char *dir_stack[PATH_MAX / 2 + 1]; |
| 156 |
|
| 157 |
|
| 158 |
uuid_t gfid_stack[PATH_MAX / 2 + 1]; |
| 159 |
|
| 160 |
char *dir_name = NULL; |
| 161 |
char *saved_dir = NULL; |
| 162 |
int top = -1; |
| 163 |
|
| 164 |
if (!path || !parent || !priv_base_path || gf_uuid_is_null(gfid)) { |
| 165 |
*op_errno = EINVAL; |
| 166 |
goto out; |
| 167 |
} |
| 168 |
|
| 169 |
dir_handle = alloca(handle_size); |
| 170 |
linkname = alloca(PATH_MAX); |
| 171 |
gf_uuid_copy(tmp_gfid, gfid); |
| 172 |
|
| 173 |
while (top < PATH_MAX / 2) { |
| 174 |
gf_uuid_copy(gfid_stack[++top], tmp_gfid); |
| 175 |
if (__is_root_gfid(tmp_gfid)) { |
| 176 |
*parent = inode_ref(itable->root); |
| 177 |
|
| 178 |
saved_dir = alloca(sizeof("/")); |
| 179 |
strcpy(saved_dir, "/"); |
| 180 |
dir_stack[top] = saved_dir; |
| 181 |
break; |
| 182 |
} else { |
| 183 |
snprintf(dir_handle, handle_size, "%s/%s/%02x/%02x/%s", |
| 184 |
priv_base_path, GF_HIDDEN_PATH, tmp_gfid[0], tmp_gfid[1], |
| 185 |
uuid_utoa(tmp_gfid)); |
| 186 |
|
| 187 |
len = sys_readlink(dir_handle, linkname, PATH_MAX); |
| 188 |
if (len < 0) { |
| 189 |
*op_errno = errno; |
| 190 |
gf_msg(this->name, |
| 191 |
(errno == ENOENT || errno == ESTALE) ? GF_LOG_DEBUG |
| 192 |
: GF_LOG_ERROR, |
| 193 |
errno, P_MSG_READLINK_FAILED, |
| 194 |
"could not read" |
| 195 |
" the link from the gfid handle %s ", |
| 196 |
dir_handle); |
| 197 |
ret = -1; |
| 198 |
goto out; |
| 199 |
} |
| 200 |
|
| 201 |
linkname[len] = '\0'; |
| 202 |
|
| 203 |
pgfidstr = strtok_r(linkname + SLEN("../../00/00/"), "/", &saveptr); |
| 204 |
dir_name = strtok_r(NULL, "/", &saveptr); |
| 205 |
saved_dir = alloca(strlen(dir_name) + 1); |
| 206 |
gf_uuid_parse(pgfidstr, tmp_gfid); |
| 207 |
strcpy(saved_dir, dir_name); |
| 208 |
dir_stack[top] = saved_dir; |
| 209 |
} |
| 210 |
} |
| 211 |
if (top == PATH_MAX / 2) { |
| 212 |
gf_msg(this->name, GF_LOG_ERROR, P_MSG_ANCESTORY_FAILED, 0, |
| 213 |
"build ancestry failed due to " |
| 214 |
"deep directory hierarchy, depth: %d.", |
| 215 |
top); |
| 216 |
*op_errno = EINVAL; |
| 217 |
ret = -1; |
| 218 |
goto out; |
| 219 |
} |
| 220 |
|
| 221 |
while (top >= 0) { |
| 222 |
if (!*parent) { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
gf_msg(this->name, GF_LOG_WARNING, P_MSG_INODE_RESOLVE_FAILED, 0, |
| 228 |
"OOPS: *parent is null (path: %s), bailing!", path); |
| 229 |
goto out; |
| 230 |
} |
| 231 |
|
| 232 |
memset(&iabuf, 0, sizeof(iabuf)); |
| 233 |
inode = posix_resolve(this, itable, *parent, dir_stack[top], &iabuf); |
| 234 |
if (inode == NULL) { |
| 235 |
gf_msg(this->name, GF_LOG_ERROR, P_MSG_INODE_RESOLVE_FAILED, 0, |
| 236 |
"posix resolve on the inode %s failed", |
| 237 |
uuid_utoa(gfid_stack[top])); |
| 238 |
*op_errno = ESTALE; |
| 239 |
ret = -1; |
| 240 |
goto out; |
| 241 |
} |
| 242 |
|
| 243 |
ret = posix_make_ancestral_node(priv_base_path, path, pathsize, head, |
| 244 |
dir_stack[top], &iabuf, inode, type, |
| 245 |
xdata); |
| 246 |
if (ret < 0) { |
| 247 |
*op_errno = ENOMEM; |
| 248 |
goto out; |
| 249 |
} |
| 250 |
|
| 251 |
inode_unref(*parent); |
| 252 |
*parent = inode; |
| 253 |
top--; |
| 254 |
} |
| 255 |
out: |
| 256 |
return ret; |
| 257 |
} |
| 258 |
|
| 259 |
int |
| 260 |
posix_handle_relpath(xlator_t *this, uuid_t gfid, const char *basename, |
| 261 |
char *buf, size_t buflen) |
| 262 |
{ |
| 263 |
char *uuid_str = NULL; |
| 264 |
int len = 0; |
| 265 |
|
| 266 |
len = POSIX_GFID_HANDLE_RELSIZE; |
| 267 |
|
| 268 |
if (basename) { |
| 269 |
len += (strlen(basename) + 1); |
| 270 |
} |
| 271 |
|
| 272 |
if (buflen < len || !buf) |
| 273 |
return len; |
| 274 |
|
| 275 |
uuid_str = uuid_utoa(gfid); |
| 276 |
|
| 277 |
if (basename) { |
| 278 |
len = snprintf(buf, buflen, "../../%02x/%02x/%s/%s", gfid[0], gfid[1], |
| 279 |
uuid_str, basename); |
| 280 |
} else { |
| 281 |
len = snprintf(buf, buflen, "../../%02x/%02x/%s", gfid[0], gfid[1], |
| 282 |
uuid_str); |
| 283 |
} |
| 284 |
|
| 285 |
return len; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static gf_boolean_t |
| 292 |
posix_is_malformed_link(xlator_t *this, char *base_str, char *linkname, |
| 293 |
size_t len) |
| 294 |
{ |
| 295 |
if ((len == 8) && strcmp(linkname, "../../..")) |
| 296 |
goto err; |
| 297 |
|
| 298 |
if (len < 50 || len >= 512) |
| 299 |
goto err; |
| 300 |
|
| 301 |
if (memcmp(linkname, "../../", 6) != 0) |
| 302 |
goto err; |
| 303 |
|
| 304 |
if ((linkname[2] != '/') || (linkname[5] != '/') || (linkname[8] != '/') || |
| 305 |
(linkname[11] != '/') || (linkname[48] != '/')) { |
| 306 |
goto err; |
| 307 |
} |
| 308 |
|
| 309 |
if ((linkname[20] != '-') || (linkname[25] != '-') || |
| 310 |
(linkname[30] != '-') || (linkname[35] != '-')) { |
| 311 |
goto err; |
| 312 |
} |
| 313 |
|
| 314 |
return _gf_false; |
| 315 |
|
| 316 |
err: |
| 317 |
gf_log_callingfn(this->name, GF_LOG_ERROR, |
| 318 |
"malformed internal link " |
| 319 |
"%s for %s", |
| 320 |
linkname, base_str); |
| 321 |
return _gf_true; |
| 322 |
} |
| 323 |
|
| 324 |
static int |
| 325 |
posix_handle_pump(xlator_t *this, char *buf, int len, int maxlen, |
| 326 |
char *base_str, int base_len, int pfx_len) |
| 327 |
{ |
| 328 |
char linkname[512] = { |
| 329 |
0, |
| 330 |
}; |
| 331 |
int ret = 0; |
| 332 |
int blen = 0; |
| 333 |
int link_len = 0; |
| 334 |
char tmpstr[POSIX_GFID_HASH2_LEN] = { |
| 335 |
0, |
| 336 |
}; |
| 337 |
char d2[3] = { |
| 338 |
0, |
| 339 |
}; |
| 340 |
int index = 0; |
| 341 |
int dirfd = 0; |
| 342 |
struct posix_private *priv = this->private; |
| 343 |
|
| 344 |
strncpy(tmpstr, (base_str + pfx_len + 3), 40); |
| 345 |
strncpy(d2, (base_str + pfx_len), 2); |
| 346 |
index = strtoul(d2, NULL, 16); |
| 347 |
dirfd = priv->arrdfd[index]; |
| 348 |
|
| 349 |
|
| 350 |
ret = readlinkat(dirfd, tmpstr, linkname, 512); |
| 351 |
if (ret == -1) { |
| 352 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_READLINK_FAILED, |
| 353 |
"internal readlink failed on %s ", base_str); |
| 354 |
goto err; |
| 355 |
} |
| 356 |
|
| 357 |
if (ret < 512) |
| 358 |
linkname[ret] = 0; |
| 359 |
|
| 360 |
link_len = ret; |
| 361 |
|
| 362 |
if ((ret == 8) && memcmp(linkname, "../../..", 8) == 0) { |
| 363 |
if (strcmp(base_str, buf) == 0) { |
| 364 |
strcpy(buf + pfx_len, ".."); |
| 365 |
} |
| 366 |
goto out; |
| 367 |
} |
| 368 |
|
| 369 |
if (posix_is_malformed_link(this, base_str, linkname, ret)) |
| 370 |
goto err; |
| 371 |
|
| 372 |
blen = link_len - 48; |
| 373 |
|
| 374 |
if (len + blen >= maxlen) { |
| 375 |
gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_HANDLEPATH_FAILED, |
| 376 |
"Unable to form handle path for %s (maxlen = %d)", buf, maxlen); |
| 377 |
goto err; |
| 378 |
} |
| 379 |
|
| 380 |
memmove(buf + base_len + blen, buf + base_len, |
| 381 |
(strlen(buf) - base_len) + 1); |
| 382 |
|
| 383 |
memcpy(base_str + pfx_len, linkname + 6, 42); |
| 384 |
|
| 385 |
memcpy(buf + pfx_len, linkname + 6, link_len - 6); |
| 386 |
out: |
| 387 |
return len + blen; |
| 388 |
err: |
| 389 |
return -1; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
int |
| 402 |
posix_handle_path(xlator_t *this, uuid_t gfid, const char *basename, char *buf, |
| 403 |
size_t maxlen) |
| 404 |
{ |
| 405 |
struct posix_private *priv = NULL; |
| 406 |
char *uuid_str = NULL; |
| 407 |
int len = 0; |
| 408 |
int ret = -1; |
| 409 |
struct stat stat; |
| 410 |
char *base_str = NULL; |
| 411 |
int base_len = 0; |
| 412 |
int pfx_len; |
| 413 |
int index = 0; |
| 414 |
int dfd = 0; |
| 415 |
char newstr[POSIX_GFID_HASH2_LEN] = { |
| 416 |
0, |
| 417 |
}; |
| 418 |
|
| 419 |
priv = this->private; |
| 420 |
|
| 421 |
uuid_str = uuid_utoa(gfid); |
| 422 |
|
| 423 |
index = gfid[0]; |
| 424 |
dfd = priv->arrdfd[index]; |
| 425 |
|
| 426 |
base_len = (priv->base_path_length + SLEN(GF_HIDDEN_PATH) + 45); |
| 427 |
base_str = alloca(base_len + 1); |
| 428 |
base_len = snprintf(base_str, base_len + 1, "%s/%s/%02x/%02x/%s", |
| 429 |
priv->base_path, GF_HIDDEN_PATH, gfid[0], gfid[1], |
| 430 |
uuid_str); |
| 431 |
pfx_len = priv->base_path_length + 1 + SLEN(GF_HIDDEN_PATH) + 1; |
| 432 |
|
| 433 |
if (basename) { |
| 434 |
len = snprintf(buf, maxlen, "%s/%s", base_str, basename); |
| 435 |
} else { |
| 436 |
len = snprintf(buf, maxlen, "%s", base_str); |
| 437 |
} |
| 438 |
|
| 439 |
snprintf(newstr, sizeof(newstr), "%02x/%s", gfid[1], uuid_str); |
| 440 |
ret = sys_fstatat(dfd, newstr, &stat, AT_SYMLINK_NOFOLLOW); |
| 441 |
|
| 442 |
if (!(ret == 0 && S_ISLNK(stat.st_mode) && stat.st_nlink == 1)) |
| 443 |
goto out; |
| 444 |
|
| 445 |
do { |
| 446 |
errno = 0; |
| 447 |
ret = posix_handle_pump(this, buf, len, maxlen, base_str, base_len, |
| 448 |
pfx_len); |
| 449 |
len = ret; |
| 450 |
|
| 451 |
if (ret == -1) |
| 452 |
break; |
| 453 |
ret = sys_lstat(buf, &stat); |
| 454 |
} while ((ret == -1) && errno == ELOOP); |
| 455 |
|
| 456 |
out: |
| 457 |
return len + 1; |
| 458 |
} |
| 459 |
|
| 460 |
int |
| 461 |
posix_handle_gfid_path(xlator_t *this, uuid_t gfid, char *buf, size_t buflen) |
| 462 |
{ |
| 463 |
struct posix_private *priv = NULL; |
| 464 |
char *uuid_str = NULL; |
| 465 |
int len = 0; |
| 466 |
|
| 467 |
priv = this->private; |
| 468 |
|
| 469 |
len = POSIX_GFID_HANDLE_SIZE(priv->base_path_length); |
| 470 |
|
| 471 |
len += 256; |
| 472 |
|
| 473 |
if ((buflen < len) || !buf) |
| 474 |
return len; |
| 475 |
|
| 476 |
uuid_str = uuid_utoa(gfid); |
| 477 |
|
| 478 |
if (__is_root_gfid(gfid)) { |
| 479 |
len = snprintf(buf, buflen, "%s", priv->base_path); |
| 480 |
} else { |
| 481 |
len = snprintf(buf, buflen, "%s/%s/%02x/%02x/%s", priv->base_path, |
| 482 |
GF_HIDDEN_PATH, gfid[0], gfid[1], uuid_str); |
| 483 |
} |
| 484 |
|
| 485 |
return len; |
| 486 |
} |
| 487 |
|
| 488 |
int |
| 489 |
posix_handle_init(xlator_t *this) |
| 490 |
{ |
| 491 |
struct posix_private *priv = NULL; |
| 492 |
char *handle_pfx = NULL; |
| 493 |
int ret = 0; |
| 494 |
struct stat stbuf; |
| 495 |
struct stat rootbuf; |
| 496 |
struct stat exportbuf; |
| 497 |
char *rootstr = NULL; |
| 498 |
static uuid_t gfid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; |
| 499 |
int dfd = 0; |
| 500 |
struct stat handledir; |
| 501 |
|
| 502 |
priv = this->private; |
| 503 |
|
| 504 |
ret = sys_stat(priv->base_path, &exportbuf); |
| 505 |
if (ret || !S_ISDIR(exportbuf.st_mode)) { |
| 506 |
gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_HANDLE_CREATE, |
| 507 |
"Not a directory: %s", priv->base_path); |
| 508 |
return -1; |
| 509 |
} |
| 510 |
|
| 511 |
handle_pfx = alloca(priv->base_path_length + 1 + SLEN(GF_HIDDEN_PATH) + 1); |
| 512 |
|
| 513 |
sprintf(handle_pfx, "%s/%s", priv->base_path, GF_HIDDEN_PATH); |
| 514 |
|
| 515 |
ret = sys_stat(handle_pfx, &stbuf); |
| 516 |
switch (ret) { |
| 517 |
case -1: |
| 518 |
if (errno == ENOENT) { |
| 519 |
ret = sys_mkdir(handle_pfx, 0600); |
| 520 |
if (ret != 0) { |
| 521 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 522 |
"Creating directory %s failed", handle_pfx); |
| 523 |
return -1; |
| 524 |
} |
| 525 |
} else { |
| 526 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 527 |
"Checking for %s failed", handle_pfx); |
| 528 |
return -1; |
| 529 |
} |
| 530 |
break; |
| 531 |
case 0: |
| 532 |
if (!S_ISDIR(stbuf.st_mode)) { |
| 533 |
gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_HANDLE_CREATE, |
| 534 |
"Not a directory: %s", handle_pfx); |
| 535 |
return -1; |
| 536 |
} |
| 537 |
break; |
| 538 |
default: |
| 539 |
break; |
| 540 |
} |
| 541 |
|
| 542 |
ret = sys_stat(handle_pfx, &handledir); |
| 543 |
|
| 544 |
if (ret) { |
| 545 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 546 |
"stat for %s failed", handle_pfx); |
| 547 |
return -1; |
| 548 |
} |
| 549 |
|
| 550 |
priv->handledir_st_ino = handledir.st_ino; |
| 551 |
priv->handledir_st_dev = handledir.st_dev; |
| 552 |
|
| 553 |
MAKE_HANDLE_ABSPATH_FD(rootstr, this, gfid, dfd); |
| 554 |
ret = sys_fstatat(dfd, rootstr, &rootbuf, 0); |
| 555 |
switch (ret) { |
| 556 |
case -1: |
| 557 |
if (errno != ENOENT) { |
| 558 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 559 |
"%s", priv->base_path); |
| 560 |
return -1; |
| 561 |
} |
| 562 |
ret = posix_handle_mkdir_hashes(this, dfd, gfid); |
| 563 |
if (ret) { |
| 564 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 565 |
"mkdir %s failed", rootstr); |
| 566 |
return -1; |
| 567 |
} |
| 568 |
|
| 569 |
ret = sys_symlinkat("../../..", dfd, rootstr); |
| 570 |
if (ret) { |
| 571 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 572 |
"symlink %s creation failed", rootstr); |
| 573 |
return -1; |
| 574 |
} |
| 575 |
break; |
| 576 |
case 0: |
| 577 |
if ((exportbuf.st_ino == rootbuf.st_ino) && |
| 578 |
(exportbuf.st_dev == rootbuf.st_dev)) |
| 579 |
return 0; |
| 580 |
|
| 581 |
gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_HANDLE_CREATE, |
| 582 |
"Different dirs %s (%lld/%lld) != %s (%lld/%lld)", |
| 583 |
priv->base_path, (long long)exportbuf.st_ino, |
| 584 |
(long long)exportbuf.st_dev, rootstr, |
| 585 |
(long long)rootbuf.st_ino, (long long)rootbuf.st_dev); |
| 586 |
return -1; |
| 587 |
|
| 588 |
break; |
| 589 |
} |
| 590 |
|
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
static gf_boolean_t |
| 595 |
posix_does_old_trash_exists(char *old_trash) |
| 596 |
{ |
| 597 |
uuid_t gfid = {0}; |
| 598 |
gf_boolean_t exists = _gf_false; |
| 599 |
struct stat stbuf = {0}; |
| 600 |
int ret = 0; |
| 601 |
|
| 602 |
ret = sys_lstat(old_trash, &stbuf); |
| 603 |
if ((ret == 0) && S_ISDIR(stbuf.st_mode)) { |
| 604 |
ret = sys_lgetxattr(old_trash, "trusted.gfid", gfid, 16); |
| 605 |
if ((ret < 0) && (errno == ENODATA || errno == ENOATTR)) |
| 606 |
exists = _gf_true; |
| 607 |
} |
| 608 |
return exists; |
| 609 |
} |
| 610 |
|
| 611 |
static int |
| 612 |
posix_handle_new_trash_init(xlator_t *this, char *trash) |
| 613 |
{ |
| 614 |
int ret = 0; |
| 615 |
struct stat stbuf = {0}; |
| 616 |
|
| 617 |
ret = sys_lstat(trash, &stbuf); |
| 618 |
switch (ret) { |
| 619 |
case -1: |
| 620 |
if (errno == ENOENT) { |
| 621 |
ret = sys_mkdir(trash, 0755); |
| 622 |
if (ret != 0) { |
| 623 |
gf_msg(this->name, GF_LOG_ERROR, errno, |
| 624 |
P_MSG_HANDLE_TRASH_CREATE, |
| 625 |
"Creating directory %s failed", trash); |
| 626 |
} |
| 627 |
} else { |
| 628 |
gf_msg(this->name, GF_LOG_ERROR, errno, |
| 629 |
P_MSG_HANDLE_TRASH_CREATE, "Checking for %s failed", |
| 630 |
trash); |
| 631 |
} |
| 632 |
break; |
| 633 |
case 0: |
| 634 |
if (!S_ISDIR(stbuf.st_mode)) { |
| 635 |
gf_msg(this->name, GF_LOG_ERROR, errno, |
| 636 |
P_MSG_HANDLE_TRASH_CREATE, "Not a directory: %s", trash); |
| 637 |
ret = -1; |
| 638 |
} |
| 639 |
break; |
| 640 |
default: |
| 641 |
break; |
| 642 |
} |
| 643 |
return ret; |
| 644 |
} |
| 645 |
|
| 646 |
static int |
| 647 |
posix_mv_old_trash_into_new_trash(xlator_t *this, char *old, char *new) |
| 648 |
{ |
| 649 |
char dest_old[PATH_MAX] = {0}; |
| 650 |
int ret = 0; |
| 651 |
uuid_t dest_name = {0}; |
| 652 |
|
| 653 |
if (!posix_does_old_trash_exists(old)) |
| 654 |
goto out; |
| 655 |
gf_uuid_generate(dest_name); |
| 656 |
snprintf(dest_old, sizeof(dest_old), "%s/%s", new, uuid_utoa(dest_name)); |
| 657 |
ret = sys_rename(old, dest_old); |
| 658 |
if (ret < 0) { |
| 659 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_TRASH_CREATE, |
| 660 |
"Not able to move %s -> %s ", old, dest_old); |
| 661 |
} |
| 662 |
out: |
| 663 |
return ret; |
| 664 |
} |
| 665 |
|
| 666 |
int |
| 667 |
posix_handle_trash_init(xlator_t *this) |
| 668 |
{ |
| 669 |
int ret = -1; |
| 670 |
struct posix_private *priv = NULL; |
| 671 |
char old_trash[PATH_MAX] = {0}; |
| 672 |
|
| 673 |
priv = this->private; |
| 674 |
|
| 675 |
priv->trash_path = GF_MALLOC(priv->base_path_length + SLEN("/") + |
| 676 |
SLEN(GF_HIDDEN_PATH) + SLEN("/") + |
| 677 |
SLEN(TRASH_DIR) + 1, |
| 678 |
gf_posix_mt_trash_path); |
| 679 |
|
| 680 |
if (!priv->trash_path) |
| 681 |
goto out; |
| 682 |
|
| 683 |
snprintf( |
| 684 |
priv->trash_path, |
| 685 |
priv->base_path_length + SLEN(GF_HIDDEN_PATH) + SLEN(TRASH_DIR) + 3, |
| 686 |
"%s/%s/%s", priv->base_path, GF_HIDDEN_PATH, TRASH_DIR); |
| 687 |
|
| 688 |
ret = posix_handle_new_trash_init(this, priv->trash_path); |
| 689 |
if (ret) |
| 690 |
goto out; |
| 691 |
snprintf(old_trash, sizeof(old_trash), "%s/.landfill", priv->base_path); |
| 692 |
ret = posix_mv_old_trash_into_new_trash(this, old_trash, priv->trash_path); |
| 693 |
out: |
| 694 |
return ret; |
| 695 |
} |
| 696 |
|
| 697 |
static int |
| 698 |
posix_handle_mkdir_hashes(xlator_t *this, int dirfd, uuid_t gfid) |
| 699 |
{ |
| 700 |
int ret = -1; |
| 701 |
char d2[3] = { |
| 702 |
0, |
| 703 |
}; |
| 704 |
|
| 705 |
snprintf(d2, sizeof(d2), "%02x", gfid[1]); |
| 706 |
ret = sys_mkdirat(dirfd, d2, 0700); |
| 707 |
if (ret == -1 && errno != EEXIST) { |
| 708 |
gf_msg(this->name, GF_LOG_ERROR, errno, P_MSG_HANDLE_CREATE, |
| 709 |
"error mkdir hash-2 %s ", uuid_utoa(gfid)); |
| 710 |
return -1; |
| 711 |
} |
| 712 |
|
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
int |
| 717 |
posix_handle_hard(xlator_t *this, const char *oldpath, uuid_t gfid, |
| 718 |
struct stat *oldbuf) |
| 719 |
{ |
| 720 |
struct stat newbuf; |
| 721 |
struct stat hashbuf; |
| 722 |
int ret = -1; |
| 723 |
gf_boolean_t link_exists = _gf_false; |
| 724 |
char d2[3] = { |
| 725 |
0, |
| 726 |
}; |
| 727 |
int dfd = -1; |
| 728 |
char *newstr = NULL; |
| 729 |
|
| 730 |
MAKE_HANDLE_ABSPATH_FD(newstr, this, gfid, dfd); |
| 731 |
ret = sys_fstatat(dfd, newstr, &newbuf, AT_SYMLINK_NOFOLLOW); |
| 732 |
|
| 733 |
if (ret == -1 && errno != ENOENT) { |
| 734 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, "%s", |
| 735 |
uuid_utoa(gfid)); |
| 736 |
return -1; |
| 737 |
} |
| 738 |
|
| 739 |
if (ret == -1 && errno == ENOENT) { |
| 740 |
snprintf(d2, sizeof(d2), "%02x", gfid[1]); |
| 741 |
ret = sys_fstatat(dfd, d2, &hashbuf, 0); |
| 742 |
if (ret) { |
| 743 |
ret = posix_handle_mkdir_hashes(this, dfd, gfid); |
| 744 |
if (ret) { |
| 745 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 746 |
"mkdir %s failed ", uuid_utoa(gfid)); |
| 747 |
return -1; |
| 748 |
} |
| 749 |
} |
| 750 |
ret = sys_linkat(AT_FDCWD, oldpath, dfd, newstr); |
| 751 |
|
| 752 |
if (ret) { |
| 753 |
if (errno != EEXIST) { |
| 754 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 755 |
"link %s -> %s" |
| 756 |
"failed ", |
| 757 |
oldpath, newstr); |
| 758 |
return -1; |
| 759 |
} else { |
| 760 |
link_exists = _gf_true; |
| 761 |
} |
| 762 |
} |
| 763 |
ret = sys_fstatat(dfd, newstr, &newbuf, AT_SYMLINK_NOFOLLOW); |
| 764 |
|
| 765 |
if (ret) { |
| 766 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 767 |
"lstat on %s failed", uuid_utoa(gfid)); |
| 768 |
return -1; |
| 769 |
} |
| 770 |
if ((link_exists) && (!S_ISREG(newbuf.st_mode))) { |
| 771 |
gf_msg(this->name, GF_LOG_ERROR, EINVAL, P_MSG_HANDLE_CREATE, |
| 772 |
"%s - Expected regular file", uuid_utoa(gfid)); |
| 773 |
return -1; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
if (newbuf.st_ino != oldbuf->st_ino || newbuf.st_dev != oldbuf->st_dev) { |
| 778 |
gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_HANDLE_CREATE, |
| 779 |
"mismatching ino/dev between file %s (%lld/%lld) " |
| 780 |
"and handle %s (%lld/%lld)", |
| 781 |
oldpath, (long long)oldbuf->st_ino, (long long)oldbuf->st_dev, |
| 782 |
uuid_utoa(gfid), (long long)newbuf.st_ino, |
| 783 |
(long long)newbuf.st_dev); |
| 784 |
ret = -1; |
| 785 |
} |
| 786 |
|
| 787 |
return ret; |
| 788 |
} |
| 789 |
|
| 790 |
int |
| 791 |
posix_handle_soft(xlator_t *this, const char *real_path, loc_t *loc, |
| 792 |
uuid_t gfid, struct stat *oldbuf) |
| 793 |
{ |
| 794 |
char *oldpath = NULL; |
| 795 |
char *newpath = NULL; |
| 796 |
struct stat newbuf; |
| 797 |
struct stat hashbuf; |
| 798 |
int ret = -1; |
| 799 |
char d2[3] = { |
| 800 |
0, |
| 801 |
}; |
| 802 |
int dfd = -1; |
| 803 |
char *newstr = NULL; |
| 804 |
|
| 805 |
MAKE_HANDLE_ABSPATH(newpath, this, gfid); |
| 806 |
MAKE_HANDLE_ABSPATH_FD(newstr, this, gfid, dfd); |
| 807 |
MAKE_HANDLE_RELPATH(oldpath, this, loc->pargfid, loc->name); |
| 808 |
|
| 809 |
ret = sys_fstatat(dfd, newstr, &newbuf, AT_SYMLINK_NOFOLLOW); |
| 810 |
|
| 811 |
if (ret == -1 && errno != ENOENT) { |
| 812 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, "%s", |
| 813 |
newstr); |
| 814 |
return -1; |
| 815 |
} |
| 816 |
|
| 817 |
if (ret == -1 && errno == ENOENT) { |
| 818 |
if (posix_is_malformed_link(this, newpath, oldpath, strlen(oldpath))) { |
| 819 |
GF_ASSERT(!"Malformed link"); |
| 820 |
errno = EINVAL; |
| 821 |
return -1; |
| 822 |
} |
| 823 |
|
| 824 |
snprintf(d2, sizeof(d2), "%02x", gfid[1]); |
| 825 |
ret = sys_fstatat(dfd, d2, &hashbuf, 0); |
| 826 |
|
| 827 |
if (ret) { |
| 828 |
ret = posix_handle_mkdir_hashes(this, dfd, gfid); |
| 829 |
if (ret) { |
| 830 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 831 |
"mkdir %s failed ", newstr); |
| 832 |
return -1; |
| 833 |
} |
| 834 |
} |
| 835 |
ret = sys_symlinkat(oldpath, dfd, newstr); |
| 836 |
if (ret) { |
| 837 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 838 |
"symlink %s -> %s failed", oldpath, newstr); |
| 839 |
return -1; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
return ret; |
| 844 |
} |
| 845 |
|
| 846 |
int |
| 847 |
posix_handle_unset_gfid(xlator_t *this, uuid_t gfid) |
| 848 |
{ |
| 849 |
int ret = 0; |
| 850 |
int index = 0; |
| 851 |
int dfd = 0; |
| 852 |
char newstr[POSIX_GFID_HASH2_LEN] = { |
| 853 |
0, |
| 854 |
}; |
| 855 |
struct posix_private *priv = this->private; |
| 856 |
|
| 857 |
index = gfid[0]; |
| 858 |
dfd = priv->arrdfd[index]; |
| 859 |
|
| 860 |
snprintf(newstr, sizeof(newstr), "%02x/%s", gfid[1], uuid_utoa(gfid)); |
| 861 |
ret = sys_unlinkat(dfd, newstr); |
| 862 |
if (ret && (errno != ENOENT)) { |
| 863 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_DELETE, |
| 864 |
"unlink %s failed", newstr); |
| 865 |
} |
| 866 |
|
| 867 |
return ret; |
| 868 |
} |
| 869 |
|
| 870 |
int |
| 871 |
posix_create_link_if_gfid_exists(xlator_t *this, uuid_t gfid, char *real_path, |
| 872 |
inode_table_t *itable) |
| 873 |
{ |
| 874 |
int ret = -1; |
| 875 |
char *newpath = NULL; |
| 876 |
char *unlink_path = NULL; |
| 877 |
uint64_t ctx_int = 0; |
| 878 |
inode_t *inode = NULL; |
| 879 |
struct stat stbuf = { |
| 880 |
0, |
| 881 |
}; |
| 882 |
struct posix_private *priv = NULL; |
| 883 |
posix_inode_ctx_t *ctx = NULL; |
| 884 |
|
| 885 |
priv = this->private; |
| 886 |
|
| 887 |
MAKE_HANDLE_PATH(newpath, this, gfid, NULL); |
| 888 |
if (!newpath) { |
| 889 |
gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_HANDLE_CREATE, |
| 890 |
"Failed to create handle path (%s)", uuid_utoa(gfid)); |
| 891 |
return ret; |
| 892 |
} |
| 893 |
|
| 894 |
ret = sys_lstat(newpath, &stbuf); |
| 895 |
if (!ret) { |
| 896 |
ret = sys_link(newpath, real_path); |
| 897 |
} else { |
| 898 |
inode = inode_find(itable, gfid); |
| 899 |
if (!inode) |
| 900 |
return -1; |
| 901 |
|
| 902 |
LOCK(&inode->lock); |
| 903 |
{ |
| 904 |
ret = __posix_inode_ctx_get_all(inode, this, &ctx); |
| 905 |
if (ret) |
| 906 |
goto unlock; |
| 907 |
|
| 908 |
if (ctx->unlink_flag != GF_UNLINK_TRUE) { |
| 909 |
ret = -1; |
| 910 |
goto unlock; |
| 911 |
} |
| 912 |
|
| 913 |
POSIX_GET_FILE_UNLINK_PATH(priv->base_path, gfid, unlink_path); |
| 914 |
ret = sys_link(unlink_path, real_path); |
| 915 |
if (ret) { |
| 916 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 917 |
"Failed to link " |
| 918 |
"%s with %s", |
| 919 |
real_path, unlink_path); |
| 920 |
goto unlock; |
| 921 |
} |
| 922 |
ret = sys_rename(unlink_path, newpath); |
| 923 |
if (ret) { |
| 924 |
gf_msg(this->name, GF_LOG_WARNING, errno, P_MSG_HANDLE_CREATE, |
| 925 |
"Failed to link " |
| 926 |
"%s with %s", |
| 927 |
real_path, unlink_path); |
| 928 |
goto unlock; |
| 929 |
} |
| 930 |
ctx_int = GF_UNLINK_FALSE; |
| 931 |
ret = __posix_inode_ctx_set_unlink_flag(inode, this, ctx_int); |
| 932 |
} |
| 933 |
unlock: |
| 934 |
UNLOCK(&inode->lock); |
| 935 |
|
| 936 |
inode_unref(inode); |
| 937 |
} |
| 938 |
|
| 939 |
return ret; |
| 940 |
} |