tfs.c 13.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16 17
#define _DEFAULT_SOURCE
#include "tfsInt.h"
H
Hongze Cheng 已提交
18

S
Shengliang Guan 已提交
19 20 21 22 23 24 25 26 27 28
static int32_t   tfsMount(STfs *pTfs, SDiskCfg *pCfg);
static int32_t   tfsCheck(STfs *pTfs);
static int32_t   tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg);
static int32_t   tfsFormatDir(char *idir, char *odir);
static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir);
static int32_t   tfsOpendirImpl(STfs *pTfs, STfsDir *pDir);
static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter);

STfs *tfsOpen(SDiskCfg *pCfg, int32_t ndisk) {
  if (ndisk < 0 || ndisk > TFS_MAX_DISKS) {
29
    terrno = TSDB_CODE_INVALID_PARA;
S
Shengliang Guan 已提交
30
    return NULL;
31
  }
H
Hongze Cheng 已提交
32

S
Shengliang Guan 已提交
33 34 35 36 37
  STfs *pTfs = calloc(1, sizeof(STfs));
  if (pTfs == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
H
Hongze Cheng 已提交
38

S
Shengliang Guan 已提交
39 40 41 42 43
  if (pthread_spin_init(&pTfs->lock, 0) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    tfsClose(pTfs);
    return NULL;
  }
H
Hongze Cheng 已提交
44

S
Shengliang Guan 已提交
45 46 47 48 49
  for (int32_t level = 0; level < TFS_MAX_TIERS; level++) {
    STfsTier *pTier = &pTfs->tiers[level];
    if (tfsInitTier(pTier, level) < 0) {
      tfsClose(pTfs);
      return NULL;
H
Hongze Cheng 已提交
50
    }
H
Hongze Cheng 已提交
51 52
  }

S
Shengliang Guan 已提交
53 54
  pTfs->hash = taosHashInit(TFS_MAX_DISKS * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (pTfs->hash == NULL) {
S
Shengliang Guan 已提交
55
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
56 57
    tfsClose(pTfs);
    return NULL;
H
Hongze Cheng 已提交
58 59
  }

S
Shengliang Guan 已提交
60
  for (int32_t idisk = 0; idisk < ndisk; idisk++) {
S
Shengliang Guan 已提交
61 62 63
    if (tfsMount(pTfs, &pCfg[idisk]) < 0) {
      tfsClose(pTfs);
      return NULL;
H
Hongze Cheng 已提交
64 65 66
    }
  }

S
Shengliang Guan 已提交
67 68 69
  if (tfsCheck(pTfs) < 0) {
    tfsClose(pTfs);
    return NULL;
H
Hongze Cheng 已提交
70 71
  }

S
Shengliang Guan 已提交
72 73 74
  tfsUpdateSize(pTfs);
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    tfsPosNextId(&pTfs->tiers[level]);
H
Hongze Cheng 已提交
75
  }
H
Hongze Cheng 已提交
76

S
Shengliang Guan 已提交
77
  return pTfs;
H
Hongze Cheng 已提交
78 79
}

S
Shengliang Guan 已提交
80 81
void tfsClose(STfs *pTfs) {
  if (pTfs == NULL) return;
H
Hongze Cheng 已提交
82

S
Shengliang Guan 已提交
83 84
  for (int32_t level = 0; level < TFS_MAX_LEVEL; level++) {
    tfsDestroyTier(&pTfs->tiers[level]);
H
Hongze Cheng 已提交
85
  }
S
Shengliang Guan 已提交
86 87 88 89

  taosHashCleanup(pTfs->hash);
  pthread_spin_destroy(&pTfs->lock);
  free(pTfs);
H
Hongze Cheng 已提交
90 91
}

S
Shengliang Guan 已提交
92
void tfsUpdateSize(STfs *pTfs) {
93
  SDiskSize size = {0};
H
Hongze Cheng 已提交
94

S
Shengliang Guan 已提交
95 96
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    STfsTier *pTier = &pTfs->tiers[level];
97
    tfsUpdateTierSize(pTier);
S
Shengliang Guan 已提交
98 99 100
    size.total += pTier->size.total;
    size.avail += pTier->size.avail;
    size.used += pTier->size.used;
H
Hongze Cheng 已提交
101 102
  }

S
Shengliang Guan 已提交
103 104 105
  tfsLock(pTfs);
  pTfs->size = size;
  tfsUnLock(pTfs);
H
Hongze Cheng 已提交
106 107
}

S
Shengliang Guan 已提交
108 109 110 111
SDiskSize tfsGetSize(STfs *pTfs) {
  tfsLock(pTfs);
  SDiskSize size = pTfs->size;
  tfsUnLock(pTfs);
H
Hongze Cheng 已提交
112

S
Shengliang Guan 已提交
113 114 115 116 117 118
  return size;
}

int32_t tfsAllocDisk(STfs *pTfs, int32_t expLevel, SDiskID *pDiskId) {
  pDiskId->level = expLevel;
  pDiskId->id = -1;
H
Hongze Cheng 已提交
119

S
Shengliang Guan 已提交
120 121
  if (pDiskId->level >= pTfs->nlevel) {
    pDiskId->level--;
H
Hongze Cheng 已提交
122 123
  }

S
Shengliang Guan 已提交
124 125 126 127
  while (pDiskId->level >= 0) {
    pDiskId->id = tfsAllocDiskOnTier(&pTfs->tiers[pDiskId->level]);
    if (pDiskId->id < 0) {
      pDiskId->level--;
H
Hongze Cheng 已提交
128 129 130
      continue;
    }

S
Shengliang Guan 已提交
131
    return 0;
H
Hongze Cheng 已提交
132 133
  }

S
Shengliang Guan 已提交
134 135
  terrno = TSDB_CODE_FS_NO_VALID_DISK;
  return -1;
H
Hongze Cheng 已提交
136 137
}

S
Shengliang Guan 已提交
138
const char *tfsGetPrimaryPath(STfs *pTfs) { return TFS_PRIMARY_DISK(pTfs)->path; }
H
Hongze Cheng 已提交
139

S
Shengliang Guan 已提交
140
const char *tfsGetDiskPath(STfs *pTfs, SDiskID diskId) { return TFS_DISK_AT(pTfs, diskId)->path; }
H
Hongze Cheng 已提交
141

S
Shengliang Guan 已提交
142 143
void tfsInitFile(STfs *pTfs, STfsFile *pFile, SDiskID diskId, const char *rname) {
  STfsDisk *pDisk = TFS_DISK_AT(pTfs, diskId);
H
Hongze Cheng 已提交
144

S
Shengliang Guan 已提交
145 146
  pFile->did = diskId;
  tstrncpy(pFile->rname, rname, TSDB_FILENAME_LEN);
S
TD-1207  
Shengliang Guan 已提交
147 148

  char tmpName[TMPNAME_LEN] = {0};
S
Shengliang Guan 已提交
149
  snprintf(tmpName, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname);
S
Shengliang Guan 已提交
150 151
  tstrncpy(pFile->aname, tmpName, TSDB_FILENAME_LEN);
  pFile->pTfs = pTfs;
H
Hongze Cheng 已提交
152 153
}

S
Shengliang Guan 已提交
154 155 156 157 158
bool tfsIsSameFile(const STfsFile *pFile1, const STfsFile *pFile2) {
  if (pFile1 == NULL || pFile2 == NULL || pFile1->pTfs != pFile2->pTfs) return false;
  if (pFile1->did.level != pFile2->did.level) return false;
  if (pFile1->did.id != pFile2->did.id) return false;
  if (strncmp(pFile1->rname, pFile2->rname, TSDB_FILENAME_LEN) != 0) return false;
H
Hongze Cheng 已提交
159 160 161
  return true;
}

S
Shengliang Guan 已提交
162
int32_t tfsEncodeFile(void **buf, STfsFile *pFile) {
S
Shengliang Guan 已提交
163
  int32_t tlen = 0;
H
Hongze Cheng 已提交
164

S
Shengliang Guan 已提交
165 166 167
  tlen += taosEncodeVariantI32(buf, pFile->did.level);
  tlen += taosEncodeVariantI32(buf, pFile->did.id);
  tlen += taosEncodeString(buf, pFile->rname);
H
Hongze Cheng 已提交
168 169 170 171

  return tlen;
}

S
Shengliang Guan 已提交
172 173 174
void *tfsDecodeFile(STfs *pTfs, void *buf, STfsFile *pFile) {
  SDiskID diskId = {0};
  char   *rname = NULL;
H
Hongze Cheng 已提交
175

S
Shengliang Guan 已提交
176 177
  buf = taosDecodeVariantI32(buf, &diskId.level);
  buf = taosDecodeVariantI32(buf, &diskId.id);
H
Hongze Cheng 已提交
178 179
  buf = taosDecodeString(buf, &rname);

S
Shengliang Guan 已提交
180
  tfsInitFile(pTfs, pFile, diskId, rname);
H
Hongze Cheng 已提交
181 182 183 184 185

  tfree(rname);
  return buf;
}

S
Shengliang Guan 已提交
186
void tfsBasename(const STfsFile *pFile, char *dest) {
H
Hongze Cheng 已提交
187 188
  char tname[TSDB_FILENAME_LEN] = "\0";

S
Shengliang Guan 已提交
189
  tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN);
190
  tstrncpy(dest, basename(tname), TSDB_FILENAME_LEN);
H
Hongze Cheng 已提交
191 192
}

S
Shengliang Guan 已提交
193
void tfsDirname(const STfsFile *pFile, char *dest) {
H
Hongze Cheng 已提交
194 195
  char tname[TSDB_FILENAME_LEN] = "\0";

S
Shengliang Guan 已提交
196
  tstrncpy(tname, pFile->aname, TSDB_FILENAME_LEN);
197
  tstrncpy(dest, dirname(tname), TSDB_FILENAME_LEN);
H
Hongze Cheng 已提交
198 199
}

S
Shengliang Guan 已提交
200 201 202 203 204 205 206 207
int32_t tfsRemoveFile(const STfsFile *pFile) {
  return remove(pFile->aname);
}

int32_t tfsCopyFile(const STfsFile *pFile1, const STfsFile *pFile2) {
  return taosCopyFile(pFile1->aname, pFile2->aname);
}

S
Shengliang Guan 已提交
208 209 210
int32_t tfsMkdirAt(STfs *pTfs, const char *rname, SDiskID diskId) {
  STfsDisk *pDisk = TFS_DISK_AT(pTfs, diskId);
  char      aname[TMPNAME_LEN];
H
Hongze Cheng 已提交
211

S
Shengliang Guan 已提交
212
  snprintf(aname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname);
213
  if (taosMkDir(aname) != 0) {
H
Hongze Cheng 已提交
214 215 216 217 218 219 220
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
221 222
int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) {
  if (tfsMkdirAt(pTfs, rname, diskId) < 0) {
H
Hongze Cheng 已提交
223 224 225 226
    if (errno == ENOENT) {
      // Try to create upper
      char *s = strdup(rname);

J
Jun Li 已提交
227 228 229 230
      // Make a copy of dirname(s) because the implementation of 'dirname' differs on different platforms.
      // Some platform may modify the contents of the string passed into dirname(). Others may return a pointer to
      // internal static storage space that will be overwritten by next call. For case like that, we should not use
      // the pointer directly in this recursion.
H
Hongze Cheng 已提交
231 232
      // See
      // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html
J
Jun Li 已提交
233 234
      char *dir = strdup(dirname(s));

S
Shengliang Guan 已提交
235
      if (tfsMkdirRecurAt(pTfs, dir, diskId) < 0) {
J
Jun Li 已提交
236 237
        free(s);
        free(dir);
H
Hongze Cheng 已提交
238 239
        return -1;
      }
J
Jun Li 已提交
240 241
      free(s);
      free(dir);
H
Hongze Cheng 已提交
242

S
Shengliang Guan 已提交
243
      if (tfsMkdirAt(pTfs, rname, diskId) < 0) {
H
Hongze Cheng 已提交
244 245 246 247 248 249 250 251 252 253
        return -1;
      }
    } else {
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
254 255 256 257 258 259
int32_t tfsMkdir(STfs *pTfs, const char *rname) {
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    STfsTier *pTier = TFS_TIER_AT(pTfs, level);
    for (int32_t id = 0; id < pTier->ndisk; id++) {
      SDiskID did = {.id = id, .level = level};
      if (tfsMkdirAt(pTfs, rname, did) < 0) {
H
Hongze Cheng 已提交
260 261 262 263 264 265 266 267
        return -1;
      }
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
268
int32_t tfsRmdir(STfs *pTfs, const char *rname) {
S
TD-1207  
Shengliang Guan 已提交
269
  char aname[TMPNAME_LEN] = "\0";
H
Hongze Cheng 已提交
270

S
Shengliang Guan 已提交
271 272 273 274 275
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    STfsTier *pTier = TFS_TIER_AT(pTfs, level);
    for (int32_t id = 0; id < pTier->ndisk; id++) {
      STfsDisk *pDisk = pTier->disks[id];
      snprintf(aname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname);
H
Hongze Cheng 已提交
276
      taosRemoveDir(aname);
H
Hongze Cheng 已提交
277 278 279 280 281 282
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
283
int32_t tfsRename(STfs *pTfs, char *orname, char *nrname) {
S
TD-1207  
Shengliang Guan 已提交
284 285
  char oaname[TMPNAME_LEN] = "\0";
  char naname[TMPNAME_LEN] = "\0";
H
Hongze Cheng 已提交
286

S
Shengliang Guan 已提交
287 288 289 290
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    STfsTier *pTier = TFS_TIER_AT(pTfs, level);
    for (int32_t id = 0; id < pTier->ndisk; id++) {
      STfsDisk *pDisk = pTier->disks[id];
S
Shengliang Guan 已提交
291 292
      snprintf(oaname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, orname);
      snprintf(naname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, nrname);
S
Shengliang Guan 已提交
293 294 295
      if (taosRenameFile(oaname, naname) != 0) {
        return -1;
      }
H
Hongze Cheng 已提交
296 297 298 299 300
    }
  }

  return 0;
}
S
Shengliang Guan 已提交
301 302 303 304

STfsDir *tfsOpendir(STfs *pTfs, const char *rname) {
  STfsDir *pDir = calloc(1, sizeof(STfsDir));
  if (pDir == NULL) {
305
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
306 307 308
    return NULL;
  }

S
Shengliang Guan 已提交
309
  SDiskID diskId = {.id = 0, .level = 0};
S
Shengliang Guan 已提交
310
  pDir->iter.pDisk = TFS_DISK_AT(pTfs, diskId);
S
Shengliang Guan 已提交
311 312
  pDir->pTfs = pTfs;
  tstrncpy(pDir->dirname, rname, TSDB_FILENAME_LEN);
H
Hongze Cheng 已提交
313

S
Shengliang Guan 已提交
314 315
  if (tfsOpendirImpl(pTfs, pDir) < 0) {
    free(pDir);
H
Hongze Cheng 已提交
316 317 318
    return NULL;
  }

S
Shengliang Guan 已提交
319
  return pDir;
H
Hongze Cheng 已提交
320 321
}

S
Shengliang Guan 已提交
322 323
const STfsFile *tfsReaddir(STfsDir *pDir) {
  if (pDir == NULL || pDir->dir == NULL) return NULL;
S
TD-1207  
Shengliang Guan 已提交
324
  char bname[TMPNAME_LEN * 2] = "\0";
H
Hongze Cheng 已提交
325 326 327

  while (true) {
    struct dirent *dp = NULL;
S
Shengliang Guan 已提交
328
    dp = readdir(pDir->dir);
H
Hongze Cheng 已提交
329
    if (dp != NULL) {
330 331 332
      // Skip . and ..
      if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue;

S
Shengliang Guan 已提交
333
      snprintf(bname, TMPNAME_LEN * 2, "%s%s%s", pDir->dirname, TD_DIRSEP, dp->d_name);
S
Shengliang Guan 已提交
334 335
      tfsInitFile(pDir->pTfs, &pDir->tfile, pDir->did, bname);
      return &pDir->tfile;
H
Hongze Cheng 已提交
336 337
    }

S
Shengliang Guan 已提交
338
    if (tfsOpendirImpl(pDir->pTfs, pDir) < 0) {
H
Hongze Cheng 已提交
339 340 341
      return NULL;
    }

S
Shengliang Guan 已提交
342
    if (pDir->dir == NULL) {
H
Hongze Cheng 已提交
343 344 345 346 347 348
      terrno = TSDB_CODE_SUCCESS;
      return NULL;
    }
  }
}

S
Shengliang Guan 已提交
349 350 351 352 353
void tfsClosedir(STfsDir *pDir) {
  if (pDir) {
    if (pDir->dir != NULL) {
      closedir(pDir->dir);
      pDir->dir = NULL;
H
Hongze Cheng 已提交
354
    }
S
Shengliang Guan 已提交
355
    free(pDir);
H
Hongze Cheng 已提交
356 357 358
  }
}

S
Shengliang Guan 已提交
359 360 361 362
static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg) {
  if (tfsCheckAndFormatCfg(pTfs, pCfg) < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
363

S
Shengliang Guan 已提交
364 365
  SDiskID   did = {.level = pCfg->level};
  STfsDisk *pDisk = tfsMountDiskToTier(TFS_TIER_AT(pTfs, did.level), pCfg);
H
Hongze Cheng 已提交
366
  if (pDisk == NULL) {
S
Shengliang Guan 已提交
367
    fError("failed to mount disk %s to level %d since %s", pCfg->dir, pCfg->level, terrstr());
H
refact  
Hongze Cheng 已提交
368
    return -1;
H
Hongze Cheng 已提交
369
  }
370
  did.id = pDisk->id;
H
Hongze Cheng 已提交
371

S
Shengliang Guan 已提交
372 373 374 375
  taosHashPut(pTfs->hash, (void *)(pCfg->dir), strnlen(pCfg->dir, TSDB_FILENAME_LEN), (void *)(&did), sizeof(did));
  if (pTfs->nlevel < pCfg->level + 1) {
    pTfs->nlevel = pCfg->level + 1;
  }
H
Hongze Cheng 已提交
376 377 378 379

  return 0;
}

S
Shengliang Guan 已提交
380
static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) {
H
Hongze Cheng 已提交
381 382 383
  char        dirName[TSDB_FILENAME_LEN] = "\0";
  struct stat pstat;

S
Shengliang Guan 已提交
384
  if (pCfg->level < 0 || pCfg->level >= TFS_MAX_TIERS) {
H
Hongze Cheng 已提交
385
    fError("failed to mount %s to FS since invalid level %d", pCfg->dir, pCfg->level);
H
Hongze Cheng 已提交
386 387 388 389
    terrno = TSDB_CODE_FS_INVLD_CFG;
    return -1;
  }

H
refact  
Hongze Cheng 已提交
390 391
  if (pCfg->primary) {
    if (pCfg->level != 0) {
H
Hongze Cheng 已提交
392
      fError("failed to mount %s to FS since disk is primary but level %d not 0", pCfg->dir, pCfg->level);
H
refact  
Hongze Cheng 已提交
393 394 395
      terrno = TSDB_CODE_FS_INVLD_CFG;
      return -1;
    }
H
Hongze Cheng 已提交
396

S
Shengliang Guan 已提交
397
    if (TFS_PRIMARY_DISK(pTfs) != NULL) {
H
Hongze Cheng 已提交
398
      fError("failed to mount %s to FS since duplicate primary mount", pCfg->dir);
H
refact  
Hongze Cheng 已提交
399 400 401 402
      terrno = TSDB_CODE_FS_DUP_PRIMARY;
      return -1;
    }
  }
H
Hongze Cheng 已提交
403

H
Hongze Cheng 已提交
404
  if (tfsFormatDir(pCfg->dir, dirName) < 0) {
H
Hongze Cheng 已提交
405
    fError("failed to mount %s to FS since invalid dir format", pCfg->dir);
H
Hongze Cheng 已提交
406 407 408 409
    terrno = TSDB_CODE_FS_INVLD_CFG;
    return -1;
  }

S
Shengliang Guan 已提交
410
  if (tfsGetDiskByName(pTfs, dirName) != NULL) {
H
Hongze Cheng 已提交
411
    fError("failed to mount %s to FS since duplicate mount", pCfg->dir);
H
Hongze Cheng 已提交
412 413 414 415 416
    terrno = TSDB_CODE_FS_INVLD_CFG;
    return -1;
  }

  if (access(dirName, W_OK | R_OK | F_OK) != 0) {
H
Hongze Cheng 已提交
417
    fError("failed to mount %s to FS since no R/W access rights", pCfg->dir);
H
Hongze Cheng 已提交
418 419 420 421 422
    terrno = TSDB_CODE_FS_INVLD_CFG;
    return -1;
  }

  if (stat(dirName, &pstat) < 0) {
H
Hongze Cheng 已提交
423
    fError("failed to mount %s to FS since %s", pCfg->dir, strerror(errno));
H
Hongze Cheng 已提交
424 425 426 427 428
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  if (!S_ISDIR(pstat.st_mode)) {
H
Hongze Cheng 已提交
429
    fError("failed to mount %s to FS since not a directory", pCfg->dir);
H
Hongze Cheng 已提交
430 431 432 433 434 435 436 437 438
    terrno = TSDB_CODE_FS_INVLD_CFG;
    return -1;
  }

  strncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN);

  return 0;
}

S
Shengliang Guan 已提交
439
static int32_t tfsFormatDir(char *idir, char *odir) {
H
Hongze Cheng 已提交
440 441
  wordexp_t wep = {0};

S
Shengliang Guan 已提交
442
  int32_t code = wordexp(idir, &wep, 0);
H
Hongze Cheng 已提交
443 444 445 446 447
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }

448 449
  char tmp[PATH_MAX] = {0};
  if (realpath(wep.we_wordv[0], tmp) == NULL) {
H
Hongze Cheng 已提交
450 451 452 453
    terrno = TAOS_SYSTEM_ERROR(errno);
    wordfree(&wep);
    return -1;
  }
454
  strcpy(odir, tmp);
H
Hongze Cheng 已提交
455 456 457 458 459

  wordfree(&wep);
  return 0;
}

S
Shengliang Guan 已提交
460 461
static int32_t tfsCheck(STfs *pTfs) {
  if (TFS_PRIMARY_DISK(pTfs) == NULL) {
H
Hongze Cheng 已提交
462 463 464 465 466
    fError("no primary disk is set");
    terrno = TSDB_CODE_FS_NO_PRIMARY_DISK;
    return -1;
  }

S
Shengliang Guan 已提交
467 468
  for (int32_t level = 0; level < pTfs->nlevel; level++) {
    if (TFS_TIER_AT(pTfs, level)->ndisk == 0) {
H
Hongze Cheng 已提交
469
      fError("no disk at level %d", level);
H
refact  
Hongze Cheng 已提交
470
      terrno = TSDB_CODE_FS_NO_MOUNT_AT_TIER;
H
Hongze Cheng 已提交
471 472
      return -1;
    }
H
Hongze Cheng 已提交
473
  }
H
Hongze Cheng 已提交
474

H
Hongze Cheng 已提交
475 476 477
  return 0;
}

S
Shengliang Guan 已提交
478 479
static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir) {
  void *pr = taosHashGet(pTfs->hash, (void *)dir, strnlen(dir, TSDB_FILENAME_LEN));
H
Hongze Cheng 已提交
480 481
  if (pr == NULL) return NULL;

S
Shengliang Guan 已提交
482 483
  SDiskID   did = *(SDiskID *)pr;
  STfsDisk *pDisk = TFS_DISK_AT(pTfs, did);
H
refact  
Hongze Cheng 已提交
484

H
Hongze Cheng 已提交
485
  return pDisk;
H
Hongze Cheng 已提交
486 487
}

S
Shengliang Guan 已提交
488 489 490
static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pDir) {
  STfsDisk *pDisk = NULL;
  char      adir[TMPNAME_LEN * 2] = "\0";
H
Hongze Cheng 已提交
491

S
Shengliang Guan 已提交
492 493 494
  if (pDir->dir != NULL) {
    closedir(pDir->dir);
    pDir->dir = NULL;
H
Hongze Cheng 已提交
495 496 497
  }

  while (true) {
S
Shengliang Guan 已提交
498
    pDisk = tfsNextDisk(pTfs, &pDir->iter);
H
Hongze Cheng 已提交
499 500
    if (pDisk == NULL) return 0;

S
Shengliang Guan 已提交
501 502
    pDir->did.level = pDisk->level;
    pDir->did.id = pDisk->id;
H
Hongze Cheng 已提交
503

S
Shengliang Guan 已提交
504 505 506
    snprintf(adir, TMPNAME_LEN * 2, "%s%s%s", pDisk->path, TD_DIRSEP, pDir->dirname);
    pDir->dir = opendir(adir);
    if (pDir->dir != NULL) break;
H
Hongze Cheng 已提交
507 508 509 510 511
  }

  return 0;
}

S
Shengliang Guan 已提交
512 513
static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter) {
  if (pIter == NULL) return NULL;
H
Hongze Cheng 已提交
514

S
Shengliang Guan 已提交
515
  STfsDisk *pDisk = pIter->pDisk;
S
Shengliang Guan 已提交
516 517 518
  if (pDisk == NULL) return NULL;

  SDiskID did = {.level = pDisk->level, .id = pDisk->id + 1};
H
Hongze Cheng 已提交
519

S
Shengliang Guan 已提交
520 521
  if (did.id < TFS_TIER_AT(pTfs, did.level)->ndisk) {
    pIter->pDisk = TFS_DISK_AT(pTfs, did);
H
Hongze Cheng 已提交
522
  } else {
S
Shengliang Guan 已提交
523 524 525 526
    did.level++;
    did.id = 0;
    if (did.level < pTfs->nlevel) {
      pIter->pDisk = TFS_DISK_AT(pTfs, did);
H
Hongze Cheng 已提交
527 528 529 530 531 532 533
    } else {
      pIter->pDisk = NULL;
    }
  }

  return pDisk;
}