int storagedb_txt_sync(void) { FILE *fp; int lock; if( !storage_db ) return 1; if( (fp = lock_fopen(storage_txt, &lock)) == NULL ) { printf("storagedb_txt_sync: can't write [%s] !!! data is lost !!!\n", storage_txt); return 1; } numdb_foreach(storage_db, storagedb_txt_sync_sub, fp); lock_fclose(fp, storage_txt, &lock); #ifdef TXT_JOURNAL if( storage_journal_enable ) { // コミットしたのでジャーナルを新規作成する journal_final( &storage_journal ); journal_create( &storage_journal, sizeof(struct storage), storage_journal_cache, storage_journal_file ); } #endif return 0; }
int mercdb_txt_sync(void) { FILE *fp; int lock; if( !merc_db ) return 1; if( (fp = lock_fopen(merc_txt, &lock)) == NULL ) { printf("mercdb_txt_sync: can't write [%s] !!! data is lost !!!\n", merc_txt); return 1; } numdb_foreach(merc_db, mercdb_txt_sync_sub, fp); lock_fclose(fp, merc_txt, &lock); #ifdef TXT_JOURNAL if( merc_journal_enable ) { // コミットしたのでジャーナルを新規作成する journal_final( &merc_journal ); journal_create( &merc_journal, sizeof(struct mmo_mercstatus), merc_journal_cache, merc_journal_file ); } #endif return 0; }
/*========================================== * 傭兵データファイルの読み込み *------------------------------------------ */ static bool mercdb_txt_read(void) { FILE *fp; bool ret = true; merc_db = numdb_init(); if((fp = fopen(merc_txt, "r")) == NULL) { printf("mercdb_txt_read: open [%s] failed !\n", merc_txt); ret = false; } else { int count = 0; char line[8192]; while(fgets(line, sizeof(line), fp)) { struct mmo_mercstatus *m = (struct mmo_mercstatus *)aCalloc(1, sizeof(struct mmo_mercstatus)); if(merc_fromstr(line, m) == 0 && m->merc_id > 0) { if(m->merc_id >= merc_newid) merc_newid = m->merc_id + 1; numdb_insert(merc_db, m->merc_id, m); } else { printf("int_merc: broken data [%s] line %d\n", merc_txt, count); aFree(m); } count++; } fclose(fp); } #ifdef TXT_JOURNAL if( merc_journal_enable ) { // ジャーナルデータのロールフォワード if( journal_load( &merc_journal, sizeof(struct mmo_mercstatus), merc_journal_file ) ) { int c = journal_rollforward( &merc_journal, merc_journal_rollforward ); printf("int_merc: journal: roll-forward (%d)\n", c ); // ロールフォワードしたので、txt データを保存する ( journal も新規作成される) mercdb_txt_sync(); } else { // ジャーナルを新規作成する journal_final( &merc_journal ); journal_create( &merc_journal, sizeof(struct mmo_mercstatus), merc_journal_cache, merc_journal_file ); } } #endif return ret; }
/*========================================== * アカウント変数の読み込み *------------------------------------------ */ static bool accregdb_txt_read(void) { FILE *fp; bool ret = true; accreg_db = numdb_init(); if((fp = fopen(accreg_txt, "r")) == NULL) { printf("accregdb_txt_read: open [%s] failed !\n", accreg_txt); ret = false; } else { int count = 0; char line[8192]; while(fgets(line, sizeof(line), fp)) { struct accreg *reg = (struct accreg *)aCalloc(1, sizeof(struct accreg)); if(accregdb_fromstr(line, reg) == 0 && reg->account_id > 0) { numdb_insert(accreg_db, reg->account_id, reg); } else { printf("inter: accreg: broken data [%s] line %d\n", accreg_txt, count); aFree(reg); } count++; } fclose(fp); } #ifdef TXT_JOURNAL if( accreg_journal_enable ) { // ジャーナルデータのロールフォワード if( journal_load( &accreg_journal, sizeof(struct accreg), accreg_journal_file ) ) { int c = journal_rollforward( &accreg_journal, accregdb_journal_rollforward ); printf("inter: accreg_journal: roll-forward (%d)\n", c ); // ロールフォワードしたので、txt データを保存する ( journal も新規作成される) accregdb_txt_sync(); } else { // ジャーナルを新規作成する journal_final( &accreg_journal ); journal_create( &accreg_journal, sizeof(struct accreg), accreg_journal_cache, accreg_journal_file ); } } #endif return ret; }
static int ext3fs_mount(void *dev, void *dir) { struct fs_driver *drv; struct ext2fs_dinode *dip = sysmalloc(sizeof(struct ext2fs_dinode)); char buf[SECTOR_SIZE * 2]; struct ext2_fs_info *fsi; int inode_sector, ret, rsize; struct node *dev_node = dev; struct nas *dir_nas = ((struct node *)dir)->nas; journal_t *jp = NULL; ext3_journal_specific_t *ext3_spec; journal_fs_specific_t spec = { .bmap = ext3_journal_bmap, .commit = ext3_journal_commit, .update = ext3_journal_update, .trans_freespace = ext3_journal_trans_freespace }; if (NULL == (drv = fs_driver_find_drv(EXT2_NAME))) { return -1; } if ((ret = drv->fsop->mount(dev, dir)) < 0) { return ret; } if (NULL == (ext3_spec = objalloc(&ext3_journal_cache))) { return -1; } spec.data = ext3_spec; if (NULL == (jp = journal_create(&spec))) { objfree(&ext3_journal_cache, ext3_spec); return -1; } /* Getting first block for inode number EXT3_JOURNAL_SUPERBLOCK_INODE */ dir_nas = ((struct node *)dir)->nas; fsi = dir_nas->fs->fsi; inode_sector = ino_to_fsba(fsi, EXT3_JOURNAL_SUPERBLOCK_INODE); rsize = ext2_read_sector(dir_nas, buf, 1, inode_sector); if (rsize * fsi->s_block_size != fsi->s_block_size) { return -EIO; } /* set pointer to inode struct in read buffer */ memcpy(dip, (buf + EXT2_DINODE_SIZE(fsi) * ino_to_fsbo(fsi, EXT3_JOURNAL_SUPERBLOCK_INODE)), sizeof(struct ext2fs_dinode)); /* XXX Hack to use ext2 functions */ dir_nas->fs->drv = &ext3fs_driver; ext3_spec->ext3_journal_inode = dip; if (0 > ext3_journal_load(jp, (struct block_dev *) dev_node->nas->fi->privdata, fsbtodb(fsi, dip->i_block[0]))) { return -EIO; } /* * FIXME Now journal supports block size only equal to filesystem block size * It is not critical but not flexible enough */ assert(jp->j_blocksize == fsi->s_block_size); fsi->journal = jp; return 0; }
journal_t* journal_open(const char *fn, size_t fslimit, int mode, uint16_t bflags) { /*! \todo Memory mapping may be faster than stdio? (issue #964) */ if (fn == NULL) { return NULL; } /* Check for lazy mode. */ if (mode & JOURNAL_LAZY) { dbg_journal("journal: opening journal %s lazily\n", fn); journal_t *j = malloc(sizeof(journal_t)); if (j != NULL) { memset(j, 0, sizeof(journal_t)); j->fd = -1; j->path = strdup(fn); j->fslimit = fslimit; j->bflags = bflags; j->refs = 1; } return j; } /* Open journal file for r/w (returns error if not exists). */ int fd = open(fn, O_RDWR); if (fd < 0) { if (errno == ENOENT) { if(journal_create(fn, JOURNAL_NCOUNT) == KNOT_EOK) { return journal_open(fn, fslimit, mode, bflags); } } return NULL; } /* File lock. */ struct flock fl; memset(&fl, 0, sizeof(struct flock)); fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 0; fl.l_pid = getpid(); /* Attempt to lock. */ dbg_journal_verb("journal: locking journal %s\n", fn); int ret = fcntl(fd, F_SETLK, &fl); /* Lock. */ if (ret < 0) { struct flock efl; memcpy(&efl, &fl, sizeof(struct flock)); fcntl(fd, F_GETLK, &efl); log_server_warning("Journal file '%s' is locked by process " "PID=%d, waiting for process to " "release lock.\n", fn, efl.l_pid); ret = fcntl(fd, F_SETLKW, &fl); } fl.l_type = F_UNLCK; dbg_journal("journal: locked journal %s (returned %d)\n", fn, ret); /* Read magic bytes. */ dbg_journal("journal: reading magic bytes\n"); const char magic_req[MAGIC_LENGTH] = JOURNAL_MAGIC; char magic[MAGIC_LENGTH]; if (!sfread(magic, MAGIC_LENGTH, fd)) { dbg_journal_detail("journal: cannot read magic bytes\n"); fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } if (memcmp(magic, magic_req, MAGIC_LENGTH) != 0) { log_server_warning("Journal file '%s' version is too old, " "it will be flushed.\n", fn); fcntl(fd, F_SETLK, &fl); close(fd); if (journal_create(fn, JOURNAL_NCOUNT) == KNOT_EOK) { return journal_open(fn, fslimit, mode, bflags); } return NULL; } crc_t crc = 0; if (!sfread(&crc, sizeof(crc_t), fd)) { dbg_journal_detail("journal: cannot read CRC\n"); fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } /* Recalculate CRC. */ char buf[4096]; ssize_t rb = 0; crc_t crc_calc = crc_init(); while((rb = read(fd, buf, sizeof(buf))) > 0) { crc_calc = crc_update(crc_calc, (const unsigned char *)buf, rb); } /* Compare */ if (crc == crc_calc) { /* Rewind. */ if (lseek(fd, MAGIC_LENGTH + sizeof(crc_t), SEEK_SET) < 0) { fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } } else { log_server_warning("Journal file '%s' CRC error, " "it will be flushed.\n", fn); fcntl(fd, F_SETLK, &fl); close(fd); if (journal_create(fn, JOURNAL_NCOUNT) == KNOT_EOK) { return journal_open(fn, fslimit, mode, bflags); } return NULL; } /* Read maximum number of entries. */ uint16_t max_nodes = 512; if (!sfread(&max_nodes, sizeof(uint16_t), fd)) { dbg_journal_detail("journal: cannot read max_nodes\n"); fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } /* Check max_nodes, but this is riddiculous. */ if (max_nodes == 0) { dbg_journal_detail("journal: max_nodes is invalid\n"); fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } /* Allocate journal structure. */ const size_t node_len = sizeof(journal_node_t); journal_t *j = malloc(sizeof(journal_t) + max_nodes * node_len); if (j == NULL) { dbg_journal_detail("journal: cannot allocate journal\n"); fcntl(fd, F_SETLK, &fl); close(fd); return NULL; } memset(j, 0, sizeof(journal_t) + max_nodes * node_len); j->qhead = j->qtail = 0; j->fd = fd; j->max_nodes = max_nodes; j->bflags = bflags; j->refs = 1; /* Load node queue state. */ if (!sfread(&j->qhead, sizeof(uint16_t), fd)) { dbg_journal_detail("journal: cannot read qhead\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Load queue tail. */ if (!sfread(&j->qtail, sizeof(uint16_t), fd)) { dbg_journal_detail("journal: cannot read qtail\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Check head + tail */ if (j->qtail > max_nodes || j->qhead > max_nodes) { dbg_journal_detail("journal: queue pointers corrupted\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Load empty segment descriptor. */ if (!sfread(&j->free, node_len, fd)) { dbg_journal_detail("journal: cannot read free segment ptr\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Read journal descriptors table. */ if (!sfread(&j->nodes, max_nodes * node_len, fd)) { dbg_journal_detail("journal: cannot read node table\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Get journal file size. */ struct stat st; if (stat(fn, &st) < 0) { dbg_journal_detail("journal: cannot get journal fsize\n"); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } /* Set file size. */ j->fsize = st.st_size; if (fslimit == 0) { j->fslimit = FSLIMIT_INF; } else { j->fslimit = (size_t)fslimit; } dbg_journal("journal: opened journal size=%u, queue=<%u, %u>, fd=%d\n", max_nodes, j->qhead, j->qtail, j->fd); /* Check node queue. */ unsigned qtail_free = (jnode_flags(j, j->qtail) <= JOURNAL_FREE); unsigned qhead_free = j->max_nodes - 1; /* Left of qhead must be free.*/ if (j->qhead > 0) { qhead_free = (j->qhead - 1); } qhead_free = (jnode_flags(j, qhead_free) <= JOURNAL_FREE); if ((j->qhead != j->qtail) && (!qtail_free || !qhead_free)) { log_server_warning("Recovering journal '%s' metadata " "after crash.\n", fn); ret = journal_recover(j); if (ret != KNOT_EOK) { log_server_error("Journal file '%s' is unrecoverable, " "metadata corrupted - %s\n", fn, knot_strerror(ret)); fcntl(fd, F_SETLK, &fl); close(fd); free(j); return NULL; } } /* Save file lock. */ fl.l_type = F_WRLCK; memcpy(&j->fl, &fl, sizeof(struct flock)); return j; }
/*! \brief Open journal file for r/w (returns error if not exists). */ static int journal_open_file(journal_t *j) { assert(j != NULL); int ret = KNOT_EOK; j->fd = open(j->path, O_RDWR); dbg_journal_verb("journal: open_file '%s'\n", j->path); if (j->fd < 0) { if (errno != ENOENT) { return knot_map_errno(errno); } /* Create new journal file and open if not exists. */ ret = journal_create(j->path, JOURNAL_NCOUNT); if(ret == KNOT_EOK) { return journal_open_file(j); } return ret; } /* File lock. */ memset(&j->fl, 0, sizeof(struct flock)); j->fl.l_type = F_WRLCK; j->fl.l_whence = SEEK_SET; j->fl.l_start = 0; j->fl.l_len = 0; j->fl.l_pid = getpid(); /* Attempt to lock. */ dbg_journal_verb("journal: locking journal %s\n", j->path); ret = fcntl(j->fd, F_SETLK, &j->fl); /* Lock. */ if (ret < 0) { struct flock efl = {0}; memcpy(&efl, &j->fl, sizeof(struct flock)); (void) fcntl(j->fd, F_GETLK, &efl); log_server_warning("Journal file '%s' is locked by process " "PID=%d, waiting for process to " "release lock.\n", j->path, efl.l_pid); ret = fcntl(j->fd, F_SETLKW, &j->fl); } UNUSED(ret); dbg_journal("journal: locked journal %s (returned %d)\n", j->path, ret); /* Read magic bytes. */ dbg_journal("journal: reading magic bytes\n"); const char magic_req[MAGIC_LENGTH] = JOURNAL_MAGIC; char magic[MAGIC_LENGTH]; if (!sfread(magic, MAGIC_LENGTH, j->fd)) { dbg_journal_verb("journal: cannot read magic bytes\n"); goto open_file_error; } if (memcmp(magic, magic_req, MAGIC_LENGTH) != 0) { log_server_warning("Journal file '%s' version is too old, " "it will be purged.\n", j->path); close(j->fd); j->fd = -1; ret = journal_create(j->path, JOURNAL_NCOUNT); if(ret == KNOT_EOK) { return journal_open_file(j); } return ret; } crc_t crc = 0; if (!sfread(&crc, sizeof(crc_t), j->fd)) { dbg_journal_verb("journal: cannot read CRC\n"); goto open_file_error; } /* Recalculate CRC. */ char buf[4096]; ssize_t rb = 0; crc_t crc_calc = crc_init(); while((rb = read(j->fd, buf, sizeof(buf))) > 0) { crc_calc = crc_update(crc_calc, (const unsigned char *)buf, rb); } /* Compare */ if (crc == crc_calc) { /* Rewind. */ if (lseek(j->fd, MAGIC_LENGTH + sizeof(crc_t), SEEK_SET) < 0) { goto open_file_error; } } else { log_server_warning("Journal file '%s' CRC error, " "it will be purged.\n", j->path); close(j->fd); j->fd = -1; ret = journal_create(j->path, JOURNAL_NCOUNT); if(ret == KNOT_EOK) { return journal_open_file(j); } return ret; } /* Get journal file size. */ struct stat st; if (stat(j->path, &st) < 0) { dbg_journal_verb("journal: cannot get journal fsize\n"); goto open_file_error; } /* Set file size. */ j->fsize = st.st_size; /* Read maximum number of entries. */ if (!sfread(&j->max_nodes, sizeof(uint16_t), j->fd)) { dbg_journal_verb("journal: cannot read max_nodes\n"); goto open_file_error; } /* Check max_nodes, but this is riddiculous. */ if (j->max_nodes == 0) { dbg_journal_verb("journal: invalid max_nodes\n"); goto open_file_error; } /* Allocate nodes. */ const size_t node_len = sizeof(journal_node_t); j->nodes = malloc(j->max_nodes * node_len); if (j->nodes == NULL) { dbg_journal_verb("journal: can't allocate nodes\n"); goto open_file_error; } else { memset(j->nodes, 0, j->max_nodes * node_len); } /* Load node queue state. */ j->qhead = j->qtail = 0; if (!sfread(&j->qhead, sizeof(uint16_t), j->fd)) { dbg_journal_verb("journal: cannot read qhead\n"); goto open_file_error; } /* Load queue tail. */ if (!sfread(&j->qtail, sizeof(uint16_t), j->fd)) { dbg_journal_verb("journal: cannot read qtail\n"); goto open_file_error; } /* Check head + tail */ if (j->qtail >= j->max_nodes || j->qhead >= j->max_nodes) { dbg_journal_verb("journal: queue pointers corrupted\n"); goto open_file_error; } /* Load empty segment descriptor. */ if (!sfread(&j->free, node_len, j->fd)) { dbg_journal_verb("journal: cannot read free segment ptr\n"); goto open_file_error; } /* Read journal descriptors table. */ if (!sfread(j->nodes, j->max_nodes * node_len, j->fd)) { dbg_journal_verb("journal: cannot read node table\n"); goto open_file_error; } dbg_journal("journal: opened journal size=%u, queue=<%u, %u>, fd=%d\n", j->max_nodes, j->qhead, j->qtail, j->fd); /* Check node queue. */ unsigned qtail_free = (jnode_flags(j, j->qtail) <= JOURNAL_FREE); unsigned qhead_free = j->max_nodes - 1; /* Left of qhead must be free.*/ if (j->qhead > 0) { qhead_free = (j->qhead - 1); } qhead_free = (jnode_flags(j, qhead_free) <= JOURNAL_FREE); if ((j->qhead != j->qtail) && (!qtail_free || !qhead_free)) { log_server_warning("Recovering journal '%s' metadata " "after crash.\n", j->path); ret = journal_recover(j); if (ret != KNOT_EOK) { log_server_error("Journal file '%s' is unrecoverable, " "metadata corrupted - %s\n", j->path, knot_strerror(ret)); goto open_file_error; } } /* Save file lock and return. */ return KNOT_EOK; /* Unlock and close file and return error. */ open_file_error: free(j->nodes); j->nodes = NULL; close(j->fd); j->fd = -1; return KNOT_ERROR; }