int chunk_load(FILE *fd) { uint8_t hdr[8]; uint8_t loadbuff[16]; const uint8_t *ptr; int32_t r; uint64_t chunkid,nextchunkid; uint32_t version,lockedto; if (fread(hdr,1,8,fd)!=8) { return -1; } ptr = hdr; nextchunkid = get64bit(&ptr); printf("# nextchunkid: %016"PRIX64"\n",nextchunkid); for (;;) { r = fread(loadbuff,1,16,fd); (void)r; ptr = loadbuff; chunkid = get64bit(&ptr); version = get32bit(&ptr); lockedto = get32bit(&ptr); if (chunkid==0 && version==0 && lockedto==0) { return 0; } printf("*|i:%016"PRIX64"|v:%08"PRIX32"|t:%10"PRIu32"\n",chunkid,version,lockedto); } }
void masterconn_chunkop(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version,newversion; uint64_t copychunkid; uint32_t copyversion; uint32_t leng; uint8_t *ptr; void *packet; if (length!=8+4+8+4+4+4) { syslog(LOG_NOTICE,"MATOCS_CHUNKOP - wrong size (%"PRIu32"/32)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); newversion = get32bit(&data); copychunkid = get64bit(&data); copyversion = get32bit(&data); leng = get32bit(&data); packet = masterconn_create_detached_packet(eptr,CSTOMA_CHUNKOP,8+4+4+8+4+4+1); ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); put32bit(&ptr,version); put32bit(&ptr,newversion); put64bit(&ptr,copychunkid); put32bit(&ptr,copyversion); put32bit(&ptr,leng); job_chunkop(masterconn_chunkopfinished,packet,chunkid,version,newversion,copychunkid,copyversion,leng); }
int masterconn_metadata_check(char *name) { int fd; char chkbuff[16]; char eofmark[16]; const uint8_t *rptr; uint64_t metaversion,metaid; fd = open(name,O_RDONLY); if (fd<0) { syslog(LOG_WARNING,"can't open downloaded metadata"); return -1; } if (read(fd,chkbuff,8)!=8) { syslog(LOG_WARNING,"can't read downloaded metadata"); close(fd); return -1; } if (memcmp(chkbuff,"MFSM NEW",8)==0) { // silently ignore "new file" close(fd); return -1; } if (memcmp(chkbuff,MFSSIGNATURE "M ",5)==0 && chkbuff[5]>='1' && chkbuff[5]<='9' && chkbuff[6]=='.' && chkbuff[7]>='0' && chkbuff[7]<='9') { uint8_t fver = ((chkbuff[5]-'0')<<4)+(chkbuff[7]-'0'); if (fver<0x17) { memset(eofmark,0,16); } else { memcpy(eofmark,"[MFS EOF MARKER]",16); if (fver>=0x20) { if (read(fd,chkbuff,16)!=16) { syslog(LOG_WARNING,"can't read downloaded metadata"); close(fd); return -1; } rptr = (uint8_t*)chkbuff; metaversion = get64bit(&rptr); metaid = get64bit(&rptr); syslog(LOG_NOTICE,"meta data version: %"PRIu64", meta data id: 0x%016"PRIX64,metaversion,metaid); } } } else { syslog(LOG_WARNING,"bad metadata file format"); close(fd); return -1; } lseek(fd,-16,SEEK_END); if (read(fd,chkbuff,16)!=16) { syslog(LOG_WARNING,"can't read downloaded metadata"); close(fd); return -1; } close(fd); if (memcmp(chkbuff,eofmark,16)!=0) { syslog(LOG_WARNING,"truncated metadata file !!!"); return -1; } return 0; }
//读取Master的packet,怀疑是复制chunk操作,待确定 //调用:masterconn_gotpacket() void masterconn_chunkop(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version,newversion; uint64_t copychunkid; uint32_t copyversion; uint32_t leng; uint8_t *ptr; #ifdef BGJOBS void *packet; #else /* BGJOBS */ uint8_t status; #endif /* BGJOBS */ if (length!=8+4+8+4+4+4) { syslog(LOG_NOTICE,"MATOCS_CHUNKOP - wrong size (%"PRIu32"/32)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); newversion = get32bit(&data); copychunkid = get64bit(&data); copyversion = get32bit(&data); leng = get32bit(&data); #ifdef BGJOBS packet = masterconn_create_detached_packet(CSTOMA_CHUNKOP,8+4+4+8+4+4+1); if (packet==NULL) { eptr->mode=KILL; return; } ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); put32bit(&ptr,version); put32bit(&ptr,newversion); put64bit(&ptr,copychunkid); put32bit(&ptr,copyversion); put32bit(&ptr,leng); job_chunkop(eptr->jpool,masterconn_chunkopfinished,packet,chunkid,version,newversion,copychunkid,copyversion,leng); #else /* BGJOBS */ status = hdd_chunkop(chunkid,version,newversion,copychunkid,copyversion,leng); ptr = masterconn_create_attached_packet(eptr,CSTOMA_CHUNKOP,8+4+4+8+4+4+1); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,chunkid); put32bit(&ptr,version); put32bit(&ptr,newversion); put64bit(&ptr,copychunkid); put32bit(&ptr,copyversion); put32bit(&ptr,leng); put8bit(&ptr,status); #endif /* BGJOBS */ }
void masterconn_download_info(masterconn *eptr,const uint8_t *data,uint32_t length) { if (length!=1 && length!=8) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_INFO - wrong size (%"PRIu32"/1|8)",length); eptr->mode = KILL; return; } passert(data); if (length==1) { eptr->downloading = 0; syslog(LOG_NOTICE,"download start error"); return; } eptr->filesize = get64bit(&data); eptr->dloffset = 0; eptr->downloadretrycnt = 0; eptr->dlstartuts = monotonic_useconds(); if (eptr->downloading==1) { eptr->metafd = open("metadata_ml.tmp",O_WRONLY | O_TRUNC | O_CREAT,0666); } else if (eptr->downloading==11 || eptr->downloading==12) { eptr->metafd = open("changelog_ml.tmp",O_WRONLY | O_TRUNC | O_CREAT,0666); } else { syslog(LOG_NOTICE,"unexpected MATOAN_DOWNLOAD_INFO packet"); eptr->mode = KILL; return; } if (eptr->metafd<0) { mfs_errlog_silent(LOG_NOTICE,"error opening metafile"); masterconn_download_end(eptr); return; } masterconn_download_next(eptr); }
//读取Master的packet,读取chunk校验和列表(不确定用途) //调用:masterconn_gotpacket() void masterconn_chunk_checksum_tab(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint8_t *ptr; uint8_t status; uint8_t crctab[4096]; if (length!=8+4) { syslog(LOG_NOTICE,"ANTOCS_CHUNK_CHECKSUM_TAB - wrong size (%"PRIu32"/12)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); status = hdd_get_checksum_tab(chunkid,version,crctab); if (status!=STATUS_OK) { ptr = masterconn_create_attached_packet(eptr,CSTOAN_CHUNK_CHECKSUM_TAB,8+4+1); } else { ptr = masterconn_create_attached_packet(eptr,CSTOAN_CHUNK_CHECKSUM_TAB,8+4+4096); } if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,chunkid); put32bit(&ptr,version); if (status!=STATUS_OK) { put8bit(&ptr,status); } else { memcpy(ptr,crctab,4096); } }
//读取Master的packet,备份chunk //调用:masterconn_gotpacket() void masterconn_replicate(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; // uint32_t ip; // uint16_t port; uint8_t *ptr; syslog(LOG_WARNING,"This version of chunkserver can perform replication only in background, but was compiled without bgjobs"); if (length!=8+4+4+2) { syslog(LOG_NOTICE,"MATOCS_REPLICATE - wrong size (%"PRIu32"/18)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); // ip = get32bit(&data); // port = get16bit(&data); ptr = masterconn_create_attached_packet(eptr,CSTOMA_REPLICATE,8+4+1); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,chunkid); put32bit(&ptr,version); put8bit(&ptr,ERROR_CANTCONNECT); // any error }
void masterconn_replicate(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint32_t ip; uint16_t port; uint8_t *ptr; void *packet; if (length!=8+4+4+2 && (length<12+18 || length>12+18*100 || (length-12)%18!=0)) { syslog(LOG_NOTICE,"MATOCS_REPLICATE - wrong size (%"PRIu32"/18|12+n*18[n:1..100])",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); packet = masterconn_create_detached_packet(CSTOMA_REPLICATE,8+4+1); if (packet==NULL) { eptr->mode=KILL; return; } ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); put32bit(&ptr,version); if (length==8+4+4+2) { ip = get32bit(&data); port = get16bit(&data); // syslog(LOG_NOTICE,"start job replication (%08"PRIX64":%04"PRIX32":%04"PRIX32":%02"PRIX16")",chunkid,version,ip,port); job_replicate_simple(eptr->jpool,masterconn_replicationfinished,packet,chunkid,version,ip,port); } else { job_replicate(eptr->jpool,masterconn_replicationfinished,packet,chunkid,version,(length-12)/18,data); } }
void masterconn_replicate(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint32_t ip; uint16_t port; uint32_t xormasks[4]; uint8_t *ptr; void *packet; if (!(length==18 || (length>=28+18 && length<=28+8*18 && (length-28)%18==0))) { syslog(LOG_NOTICE,"MATOCS_REPLICATE - wrong size (%"PRIu32"/18|12+n*18[n:1..100]|28+n*18[n:1..8])",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); packet = masterconn_create_detached_packet(eptr,CSTOMA_REPLICATE,8+4+1); ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); put32bit(&ptr,version); if (length==8+4+4+2) { ip = get32bit(&data); port = get16bit(&data); // syslog(LOG_NOTICE,"start job replication (%08"PRIX64":%04"PRIX32":%04"PRIX32":%02"PRIX16")",chunkid,version,ip,port); job_replicate_simple(masterconn_replicationfinished,packet,chunkid,version,ip,port); } else { xormasks[0] = get32bit(&data); xormasks[1] = get32bit(&data); xormasks[2] = get32bit(&data); xormasks[3] = get32bit(&data); job_replicate_raid(masterconn_replicationfinished,packet,chunkid,version,(length-28)/18,xormasks,data); } }
static void mfs_attr_to_stat(uint32_t inode,uint8_t attr[35], struct stat *stbuf) { uint16_t attrmode; uint8_t attrtype; uint32_t attruid,attrgid,attratime,attrmtime,attrctime,attrnlink; uint64_t attrlength; const uint8_t *ptr; ptr = attr; attrtype = get8bit(&ptr); attrmode = get16bit(&ptr); attruid = get32bit(&ptr); attrgid = get32bit(&ptr); attratime = get32bit(&ptr); attrmtime = get32bit(&ptr); attrctime = get32bit(&ptr); attrnlink = get32bit(&ptr); attrlength = get64bit(&ptr); stbuf->st_ino = inode; if (attrtype==TYPE_FILE || attrtype==TYPE_TRASH || attrtype==TYPE_RESERVED) { stbuf->st_mode = S_IFREG | ( attrmode & 07777); } else { stbuf->st_mode = 0; } stbuf->st_size = attrlength; stbuf->st_blocks = (attrlength+511)/512; stbuf->st_uid = attruid; stbuf->st_gid = attrgid; stbuf->st_atime = attratime; stbuf->st_mtime = attrmtime; stbuf->st_ctime = attrctime; stbuf->st_nlink = attrnlink; }
void masterconn_download_start(serventry *eptr,const uint8_t *data,uint32_t length) { if (length!=1 && length!=8) { MFSLOG(LOG_NOTICE,"MATOSLA_DOWNLOAD_START - wrong size (%"PRIu32"/1|8)",length); eptr->mode = KILL; return; } if (length==1) { MFSLOG(LOG_NOTICE,"download start error"); return; } eptr->filesize = get64bit(&data); eptr->dloffset = 0; eptr->retrycnt = 0; eptr->dlstartuts = main_utime(); if (eptr->downloading==1) { eptr->metafd = open("metadata.tmp",O_WRONLY | O_TRUNC | O_CREAT,0666); } else if (eptr->downloading==2) { eptr->metafd = open("changelog.0.tmp",O_WRONLY | O_TRUNC | O_CREAT,0666); } else { MFSLOG(LOG_NOTICE,"unexpected MATOSLA_DOWNLOAD_START packet"); eptr->mode = KILL; return; } if (eptr->metafd<0) { MFSLOG(LOG_NOTICE,"error opening metafile: %m"); masterconn_download_end(eptr,4); return; } masterconn_download_next(eptr); }
/*---------------------------------------------------------------------*/ int client_mknod(char szmac[12], uint64_t parent, uint8_t nleng, const uint8_t *name, uint8_t type, uint32_t mode, uint32_t uid, uint32_t gid, uint32_t rdev, uint64_t *inode, uint8_t attr[FILE_ATTR_LEN], uint8_t **dslist, uint32_t *dslistlen) { int iSendLeng = 8 + 38 + nleng; uint8_t *szCommand = (uint8_t *)def_calloc(iSendLeng, sizeof(char)); char *curr = (char *)szCommand; put32bit(&szCommand, CLTODI_FUSE_MKNOD); put32bit(&szCommand, 38 + nleng); memcpy(szCommand, szmac, 12); szCommand += 12; put64bit(&szCommand, parent); put8bit(&szCommand, nleng); memcpy(szCommand, name, nleng); szCommand += nleng; put8bit(&szCommand, type); put32bit(&szCommand, mode); put32bit(&szCommand, uid); put32bit(&szCommand, gid); put32bit(&szCommand, rdev); char *szOutBuf = NULL; int iOutLength = 0; int ret = net_client_rpc(p_cn_client->p_client, p_cn_client->namenode_info[0].namenode_ip, p_cn_client->namenode_info[0].namenode_port, curr, iSendLeng, &szOutBuf, &iOutLength); def_free(curr); if (ret != 0) { if(szOutBuf) def_free(szOutBuf); ret = EIO; } else { if (iOutLength == 4) { const uint8_t * result = (const uint8_t *) szOutBuf; ret = get32bit(&result); } else { uint8_t *out = (uint8_t *) szOutBuf; uint64_t t64 = get64bit((const uint8_t **)&out); *inode = t64; memcpy(attr, out, FILE_ATTR_LEN); out += FILE_ATTR_LEN; *dslistlen = iOutLength - 8 - FILE_ATTR_LEN; *dslist = def_calloc(*dslistlen, sizeof(char)); memcpy(*dslist, out, *dslistlen); ret = STATUS_OK; } def_free(szOutBuf); } return ret; }
int client_lookup(uint64_t parent, uint8_t nleng, const uint8_t *name, uint32_t uid, uint32_t gid, uint64_t *inode, uint8_t attr[FILE_ATTR_LEN]) { int iSendLeng = 8 + 20 + nleng; uint8_t *szCommand = (uint8_t *)def_calloc(iSendLeng, sizeof(char)); char *curr = (char *)szCommand; put32bit(&szCommand, CLTODI_FUSE_LOOKUP); put32bit(&szCommand, 20 + nleng); put64bit(&szCommand, parent); put32bit(&szCommand, nleng); memcpy(szCommand, name, nleng); szCommand += nleng; put32bit(&szCommand, uid); put32bit(&szCommand, gid); char *szOutBuf = NULL; int iOutLength = 0; int ret = net_client_rpc(p_cn_client->p_client, p_cn_client->namenode_info[0].namenode_ip, p_cn_client->namenode_info[0].namenode_port, curr, iSendLeng, &szOutBuf, &iOutLength); def_free(curr); if (ret != 0) { nb_log("client lookup", NB_LOG_ERROR, "nn --->cli rpc is error."); if(szOutBuf) def_free(szOutBuf); ret = EIO; } else { if(iOutLength == 4) { const uint8_t * result = (const uint8_t *) szOutBuf; ret = get32bit(&result); } else if (iOutLength != FILE_ATTR_LEN + 8) { ret = EIO; } else { const uint8_t *out = (const uint8_t *) szOutBuf; *inode = get64bit(&out); memcpy(attr, out, FILE_ATTR_LEN); ret = STATUS_OK; } def_free(szOutBuf); } return ret; }
// 字符串转换为statvfs 结构 void buf_to_statvfs(const uint8_t *buf, int buflen, struct statvfs *statfs) { if(buflen != 80) return; const uint8_t * ptr = (uint8_t *)buf; statfs->f_bsize = get64bit(&ptr); // file system block size statfs->f_frsize = get64bit(&ptr); // fragment size statfs->f_blocks = get64bit(&ptr); // size of fs in f_frsize units statfs->f_bfree = get64bit(&ptr); // free blocks statfs->f_bavail = get64bit(&ptr); // free blocks for non-root statfs->f_files = get64bit(&ptr); // inodes statfs->f_ffree = get64bit(&ptr); // free inodes statfs->f_favail = get64bit(&ptr); // free inodes for non-root statfs->f_fsid = get64bit(&ptr); // file system ID statfs->f_flag = get64bit(&ptr); // mount flags statfs->f_namemax = FS_NAME_MAX; }
void masterconn_metachanges_log(masterconn *eptr,const uint8_t *data,uint32_t length) { char line[1024]; uint64_t version; if (length==1 && data[0]==0x55) { fs_storeall(1); return; } if (length<10) { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - wrong size (%"PRIu32"/9+data)",length); eptr->mode = KILL; return; } if (data[0]!=0xFF) { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - wrong packet"); eptr->mode = KILL; return; } if (data[length-1]!='\0') { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - invalid string"); eptr->mode = KILL; return; } data++; version = get64bit(&data); if (version<currentlogversion) { syslog(LOG_WARNING, "get old change: %"PRIu64":%"PRIu64", ignore",currentlogversion,version-1); return; } if (currentlogversion>0 && version>currentlogversion) { syslog(LOG_WARNING, "some changes lost: [%"PRIu64"-%"PRIu64"], download metadata again",currentlogversion,version-1); masterconn_metadownloadinit(); return; } if (version == fs_getversion()) { changelog(version, "%s", (const char*)data); currentlogversion = version+1; sprintf(line, ": %s\n", data); if (restore_line("(live changelog)", version, line)!=STATUS_OK) { syslog(LOG_WARNING, "replay change log failed: version=%"PRIu64", download metadata again",version); masterconn_metadownloadinit(); } else if (fs_getversion() != version+1) { syslog(LOG_WARNING, "restored version not match: %"PRIu64"!=%"PRIu64", download metadata again",fs_getversion(),version+1); masterconn_metadownloadinit(); } } else { syslog(LOG_WARNING, "version not match: %"PRIu64"!=%"PRIu64", download metadata again",fs_getversion(),version); masterconn_metadownloadinit(); } }
void masterconn_duplicate(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint64_t copychunkid; uint32_t copyversion; uint8_t *ptr; void *packet; if (length!=8+4+8+4) { syslog(LOG_NOTICE,"MATOCS_DUPLICATE - wrong size (%"PRIu32"/24)",length); eptr->mode = KILL; return; } copychunkid = get64bit(&data); copyversion = get32bit(&data); chunkid = get64bit(&data); version = get32bit(&data); packet = masterconn_create_detached_packet(eptr,CSTOMA_DUPLICATE,8+1); ptr = masterconn_get_packet_data(packet); put64bit(&ptr,copychunkid); job_duplicate(masterconn_jobfinished,packet,chunkid,version,version,copychunkid,copyversion); }
void masterconn_metachanges_log(masterconn *eptr,const uint8_t *data,uint32_t length) { char logname1[100],logname2[100]; uint32_t i; uint64_t version; if (length==1 && data[0]==0x55) { if (eptr->logfd!=NULL) { fclose(eptr->logfd); eptr->logfd=NULL; } if (BackLogsNumber>0) { for (i=BackLogsNumber ; i>0 ; i--) { snprintf(logname1,100,"changelog_ml.%"PRIu32".mfs",i); snprintf(logname2,100,"changelog_ml.%"PRIu32".mfs",i-1); rename(logname2,logname1); } } else { unlink("changelog_ml.0.mfs"); } return; } if (length<10) { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - wrong size (%"PRIu32"/9+data)",length); eptr->mode = KILL; return; } if (data[0]!=0xFF) { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - wrong packet"); eptr->mode = KILL; return; } if (data[length-1]!='\0') { syslog(LOG_NOTICE,"MATOML_METACHANGES_LOG - invalid string"); eptr->mode = KILL; return; } if (eptr->logfd==NULL) { eptr->logfd = fopen("changelog_ml.0.mfs","a"); } data++; version = get64bit(&data); if (eptr->logfd) { fprintf(eptr->logfd,"%"PRIu64": %s\n",version,data); } else { syslog(LOG_NOTICE,"lost MFS change %"PRIu64": %s",version,data); } }
void matomlserv_register(matomlserventry *eptr,const uint8_t *data,uint32_t length) { uint8_t rversion; uint64_t minversion; if (eptr->version>0) { syslog(LOG_WARNING,"got register message from registered metalogger !!!"); eptr->mode=KILL; return; } if (length<1) { syslog(LOG_NOTICE,"MLTOMA_REGISTER - wrong size (%"PRIu32")",length); eptr->mode=KILL; return; } else { rversion = get8bit(&data); if (rversion==1) { if (length!=7) { syslog(LOG_NOTICE,"MLTOMA_REGISTER (ver 1) - wrong size (%"PRIu32"/7)",length); eptr->mode=KILL; return; } eptr->version = get32bit(&data); eptr->timeout = get16bit(&data); } else if (rversion==2) { if (length!=7+8) { syslog(LOG_NOTICE,"MLTOMA_REGISTER (ver 2) - wrong size (%"PRIu32"/15)",length); eptr->mode=KILL; return; } eptr->version = get32bit(&data); eptr->timeout = get16bit(&data); minversion = get64bit(&data); matomlserv_send_old_changes(eptr,minversion); } else { syslog(LOG_NOTICE,"MLTOMA_REGISTER - wrong version (%"PRIu8"/1)",rversion); eptr->mode=KILL; return; } if (eptr->timeout<10) { syslog(LOG_NOTICE,"MLTOMA_REGISTER communication timeout too small (%"PRIu16" seconds - should be at least 10 seconds)",eptr->timeout); if (eptr->timeout<3) { eptr->timeout=3; } // eptr->mode=KILL; return; } } }
//rewrite metachanges_log func void masterconn_metachanges_log(serventry *eptr,const uint8_t *data,uint32_t length) { char log_data[1000]; char log_str[1000]; uint64_t version; uint32_t size; //FILE *new_fd; uint8_t log_count = 0; uint8_t log_limit; int ret = 0; NOT_USED(length); log_limit = get8bit(&data); if (eptr->logfd == NULL) { eptr->logfd = fopen("changelog.0.mfs","a"); } while(log_count < log_limit) { version = get64bit(&data); size = get32bit(&data); memcpy(log_str,data,size); data = data + size; if (log_str[size - 1] != '\0') { MFSLOG(LOG_NOTICE,"MATOSLA_CHANGELOG - invalide string the last is %c",log_str[size - 1]); } snprintf(log_data,sizeof(log_data),"%"PRIu64": %s\n",version,log_str); if (eptr->logfd) { ret = replay(log_data); if (ret != 0) { MFSLOG(LOG_ERR,"changelog replay failed"); break; //more complicated method to ensure consistency } fprintf(eptr->logfd,"%"PRIu64": %s",version,log_str); fflush(eptr->logfd); log_count++; } else { MFSLOG(LOG_NOTICE,"lost MFS change %"PRIu64": %s",version,log_str); ret = -1; break; } } if (ret == 0) { masterconn_ack_changelog(eptr,0); } else { masterconn_ack_changelog(eptr,1); } }
void csserv_get_chunk_checksum_tab(csserventry *eptr,const uint8_t *data,uint32_t length) { idlejob *ij; if (length!=8+4) { syslog(LOG_NOTICE,"ANTOCS_GET_CHUNK_CHECKSUM_TAB - wrong size (%"PRIu32"/12)",length); eptr->state = CLOSE; return; } ij = malloc(offsetof(idlejob,buff)+4096); ij->op = IJ_GET_CHUNK_CHECKSUM_TAB; ij->chunkid = get64bit(&data); ij->version = get32bit(&data); ij->eptr = eptr; ij->next = eptr->idlejobs; ij->prev = &(eptr->idlejobs); eptr->idlejobs = ij; ij->jobid = job_get_chunk_checksum_tab(csserv_idlejob_finished,ij,ij->chunkid,ij->version,ij->buff); }
void masterconn_get_chunk_checksum_tab(masterconn *eptr,const uint8_t *data,uint32_t length) { idlejob *ij; if (length!=8+4) { syslog(LOG_NOTICE,"ANTOCS_GET_CHUNK_CHECKSUM_TAB - wrong size (%"PRIu32"/12)",length); eptr->mode = KILL; return; } ij = malloc(offsetof(idlejob,buff)+4096); ij->op = IJ_GET_CHUNK_CHECKSUM_TAB; ij->chunkid = get64bit(&data); ij->version = get32bit(&data); ij->valid = 1; ij->next = idlejobs; ij->prev = &(idlejobs); idlejobs = ij; ij->jobid = job_get_chunk_checksum_tab(masterconn_idlejob_finished,ij,ij->chunkid,ij->version,ij->buff); }
void masterconn_setversion(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint32_t newversion; uint8_t *ptr; void *packet; if (length!=8+4+4) { syslog(LOG_NOTICE,"MATOCS_SET_VERSION - wrong size (%"PRIu32"/16)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); newversion = get32bit(&data); version = get32bit(&data); packet = masterconn_create_detached_packet(eptr,CSTOMA_SET_VERSION,8+1); ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); job_version(masterconn_jobfinished,packet,chunkid,version,newversion); }
//读取Master的packet,输出日志信息 //调用:masterconn_gotpacket() void masterconn_structure_log(masterconn *eptr,const uint8_t *data,uint32_t length) { if (length<5) { syslog(LOG_NOTICE,"MATOCS_STRUCTURE_LOG - wrong size (%"PRIu32"/4+data)",length); eptr->mode = KILL; return; } if (data[0]==0xFF && length<10) { syslog(LOG_NOTICE,"MATOCS_STRUCTURE_LOG - wrong size (%"PRIu32"/9+data)",length); eptr->mode = KILL; return; } if (data[length-1]!='\0') { syslog(LOG_NOTICE,"MATOCS_STRUCTURE_LOG - invalid string"); eptr->mode = KILL; return; } if (logfd==NULL) { logfd = fopen("changelog_csback.0.mfs","a"); } if (data[0]==0xFF) { // new version uint64_t version; data++; version = get64bit(&data); if (logfd) { fprintf(logfd,"%"PRIu64": %s\n",version,data); } else { syslog(LOG_NOTICE,"lost MFS change %"PRIu64": %s",version,data); } } else { // old version uint32_t version; version = get32bit(&data); if (logfd) { fprintf(logfd,"%"PRIu32": %s\n",version,data); } else { syslog(LOG_NOTICE,"lost MFS change %"PRIu32": %s",version,data); } } }
void matoslaserv_download_data(serventry *eptr,const uint8_t *data,uint32_t length) { uint8_t *ptr; uint64_t offset; uint32_t leng; uint32_t crc; ssize_t ret; if (length!=12) { MFSLOG(LOG_NOTICE,"slaTOMA_DOWNLOAD_DATA - wrong size (%"PRIu32"/12)",length); eptr->mode=KILL; return; } if (eptr->metafd<0) { MFSLOG(LOG_NOTICE,"slaTOMA_DOWNLOAD_DATA - file not opened"); eptr->mode=KILL; return; } offset = get64bit(&data); leng = get32bit(&data); ptr = matoslaserv_createpacket(eptr,MATOSLA_DOWNLOAD_DATA,16+leng); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,offset); put32bit(&ptr,leng); #ifdef HAVE_PREAD ret = pread(eptr->metafd,ptr+4,leng,offset); #else /* HAVE_PWRITE */ lseek(eptr->metafd,offset,SEEK_SET); ret = read(eptr->metafd,ptr+4,leng); #endif /* HAVE_PWRITE */ if (ret!=(ssize_t)leng) { MFSLOG(LOG_NOTICE,"error reading metafile: %m"); eptr->mode=KILL; return; } crc = mycrc32(0,ptr+4,leng); put32bit(&ptr,crc); }
//读取Master的packet,删除chunk //调用:masterconn_gotpacket() void masterconn_delete(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint8_t *ptr; #ifdef BGJOBS void *packet; #else /* BGJOBS */ uint8_t status; #endif /* BGJOBS */ if (length!=8+4) { syslog(LOG_NOTICE,"MATOCS_DELETE - wrong size (%"PRIu32"/12)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); #ifdef BGJOBS packet = masterconn_create_detached_packet(CSTOMA_DELETE,8+1); if (packet==NULL) { eptr->mode=KILL; return; } ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); job_delete(eptr->jpool,masterconn_jobfinished,packet,chunkid,version); #else /* BGJOBS */ status = hdd_delete(chunkid,version); ptr = masterconn_create_attached_packet(eptr,CSTOMA_DELETE,8+1); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,chunkid); put8bit(&ptr,status); #endif /* BGJOBS */ }
static inline void masterconn_initcsid(void) { int fd; uint8_t buff[10]; const uint8_t *rptr; ssize_t ret; if (csidvalid) { return; } ChunkServerID = 0; MetaID = 0; csidvalid = 1; fd = open("chunkserverid.mfs",O_RDWR); if (fd>=0) { ret = read(fd,buff,10); rptr = buff; if (ret>=2) { ChunkServerID = get16bit(&rptr); } if (ret>=10) { MetaID = get64bit(&rptr); } close(fd); } }
void masterconn_download_data(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t offset; uint32_t leng; uint32_t crc; ssize_t ret; if (eptr->metafd<0) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_DATA - file not opened"); eptr->mode = KILL; return; } if (length<16) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_DATA - wrong size (%"PRIu32"/16+data)",length); eptr->mode = KILL; return; } passert(data); offset = get64bit(&data); leng = get32bit(&data); crc = get32bit(&data); if (leng+16!=length) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_DATA - wrong size (%"PRIu32"/16+%"PRIu32")",length,leng); eptr->mode = KILL; return; } if (offset!=eptr->dloffset) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_DATA - unexpected file offset (%"PRIu64"/%"PRIu64")",offset,eptr->dloffset); eptr->mode = KILL; return; } if (offset+leng>eptr->filesize) { syslog(LOG_NOTICE,"MATOAN_DOWNLOAD_DATA - unexpected file size (%"PRIu64"/%"PRIu64")",offset+leng,eptr->filesize); eptr->mode = KILL; return; } #ifdef HAVE_PWRITE ret = pwrite(eptr->metafd,data,leng,offset); #else /* HAVE_PWRITE */ lseek(eptr->metafd,offset,SEEK_SET); ret = write(eptr->metafd,data,leng); #endif /* HAVE_PWRITE */ if (ret!=(ssize_t)leng) { mfs_errlog_silent(LOG_NOTICE,"error writing metafile"); if (eptr->downloadretrycnt>=5) { masterconn_download_end(eptr); } else { eptr->downloadretrycnt++; masterconn_download_next(eptr); } return; } if (crc!=mycrc32(0,data,leng)) { syslog(LOG_NOTICE,"metafile data crc error"); if (eptr->downloadretrycnt>=5) { masterconn_download_end(eptr); } else { eptr->downloadretrycnt++; masterconn_download_next(eptr); } return; } if (fsync(eptr->metafd)<0) { mfs_errlog_silent(LOG_NOTICE,"error syncing metafile"); if (eptr->downloadretrycnt>=5) { masterconn_download_end(eptr); } else { eptr->downloadretrycnt++; masterconn_download_next(eptr); } return; } eptr->dloffset+=leng; eptr->downloadretrycnt=0; masterconn_download_next(eptr); }
int main(void) { uint64_t buff[2]; uint8_t *wp; const uint8_t *rp; uint32_t i; mfstest_init(); wp = (uint8_t*)buff; for (i=0 ; i<16 ; i++) { wp[i] = ((15-i)*0x10)+i; } mfstest_start(getbit_uneven); rp = (uint8_t*)buff; mfstest_assert_uint8_eq(get8bit(&rp),0xF0); mfstest_assert_uint16_eq(get16bit(&rp),0xE1D2); mfstest_assert_uint32_eq(get32bit(&rp),0xC3B4A596); mfstest_assert_uint64_eq(get64bit(&rp),0x8778695A4B3C2D1E); mfstest_end(); mfstest_start(getbit_even); rp = (uint8_t*)buff; mfstest_assert_uint64_eq(get64bit(&rp),0xF0E1D2C3B4A59687); mfstest_assert_uint32_eq(get32bit(&rp),0x78695A4B); mfstest_assert_uint16_eq(get16bit(&rp),0x3C2D); mfstest_assert_uint8_eq(get8bit(&rp),0x1E); mfstest_end(); wp = (uint8_t*)buff; for (i=0; i<16 ; i++) { wp[i] = 0; } put8bit(&wp,0xF0); put16bit(&wp,0xE1D2); put32bit(&wp,0xC3B4A596); put64bit(&wp,0x8778695A4B3C2D1E); put8bit(&wp,0x0F); mfstest_start(putbit_uneven); rp = (uint8_t*)buff; for (i=0 ; i<16 ; i++) { mfstest_assert_uint8_eq(rp[i],((15-i)*0x10)+i); } mfstest_end(); wp = (uint8_t*)buff; for (i=0; i<16 ; i++) { wp[i] = 0; } put64bit(&wp,0xF0E1D2C3B4A59687); put32bit(&wp,0x78695A4B); put16bit(&wp,0x3C2D); put8bit(&wp,0x1E); put8bit(&wp,0x0F); mfstest_start(putbit_even); rp = (uint8_t*)buff; for (i=0 ; i<16 ; i++) { mfstest_assert_uint8_eq(rp[i],((15-i)*0x10)+i); } mfstest_end(); mfstest_return(); }
int ppfs_write (const char *path, const char *buf, size_t st, off_t off, struct fuse_file_info *fi){ fprintf(stderr,"\n\n\nppfs_write:%s,size:%d,offset:%d\n\n\n",path,st,off); int nwrite = 0; int ost,ooff; ost = st; ooff = off; ppacket* p = createpacket_s(4+strlen(path),CLTOMD_READ_CHUNK_INFO,-1); uint8_t* ptr = p->startptr + HEADER_LEN; int plen = strlen(path); uint32_t ip; uint64_t* chunklist = NULL; int clen,calloc; const char* wbuf = buf; put32bit(&ptr,plen); memcpy(ptr,path,plen); ptr += plen; fprintf(stderr,"just to be clear\n"); const uint8_t* tmpptr = p->startptr + HEADER_LEN; int i; for(i=0;i<p->size;i+=1){ int x = get8bit(&tmpptr); fprintf(stderr,"%X\t",x); } fprintf(stderr,"\n"); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr,"status:%d\n",status); if(status == 0){ ip = get32bit(&ptr2); if(ip == -1){ fprintf(stderr,"local mds\n"); } else { fprintf(stderr,"remote mds:%X\n",ip); } int chunks = get32bit(&ptr2); fprintf(stderr,"chunks=%d\n",chunks); int i; chunklist = (uint64_t*)malloc(sizeof(uint64_t)*(chunks+20)); clen = 0; calloc = chunks+20; for(i=0;i<chunks;i++){ uint64_t chunkid = get64bit(&ptr2); fprintf(stderr,"(%d):id=%lld\n",i,chunkid); chunklist[clen++] = chunkid; } ppfs_conn_entry* e = NULL; if(ip != -1){ if(remote_mds.sockfd != -1 && remote_mds.peerip != ip){ tcpclose(remote_mds.sockfd); remote_mds.sockfd = -1; } if(remote_mds.sockfd == -1){ if(serv_connect(&remote_mds,ip,MDS_PORT) < 0){ return -1; } } e = &remote_mds; } else { e = &local_mds; } fprintf(stderr,"connected\n"); if(chunks * CHUNKSIZE <= off + st){ fprintf(stderr,"clearing cache\n"); chunk_cache* cc; attr_cache* ac; if(lookup_chunk_cache(path,&cc) == 0){ remove_chunk_cache(cc); free_chunk_cache(cc); } if(lookup_attr_cache(path,&ac) == 0){ remove_attr_cache(ac); free_attr_cache(ac); } fprintf(stderr,"appending chunk\n"); while(chunks * CHUNKSIZE <= off + st){ ppacket* p = createpacket_s(4+plen,CLTOMD_APPEND_CHUNK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put32bit(&ptr,plen); memcpy(ptr,path,plen); sendpacket(e->sockfd,p); free(p); ppacket* rp = receivepacket(e->sockfd); const uint8_t* ptr2 = rp->startptr; int status = get32bit(&ptr2); printf("status:%d\n",status); if(status == 0){ uint64_t chunkid = get64bit(&ptr2); printf("chunkid=%lld\n",chunkid); if(clen < calloc){ chunklist[clen++] = chunkid; } else { chunklist = (uint64_t*)realloc(chunklist,calloc<<1); chunklist[clen++] = chunkid; } } else { free(rp); return status; } free(rp); chunks++; } } fprintf(stderr,"chunklist now:\n"); for(i=0;i<clen;i++){ fprintf(stderr,"\t(%d):%lld\n",i,chunklist[i]); } ppacket* p = createpacket_s(4+plen+4+4,CLTOMD_WRITE,-1); ptr = p->startptr + HEADER_LEN; put32bit(&ptr,plen); memcpy(ptr,path,plen); ptr += plen; put32bit(&ptr,ooff); put32bit(&ptr,ost); sendpacket(e->sockfd,p); free(p); int starti = off/CHUNKSIZE; int buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); ppfs_conn_entry cs; cs.sockfd = -1; fprintf(stderr,"off=%d,st=%lld\n",off,st); while(st > 0){ uint64_t chunkid = chunklist[starti]; ppacket* p = createpacket_s(8,CLTOMD_LOOKUP_CHUNK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); sendpacket(e->sockfd,p); free(p); p = receivepacket(e->sockfd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); printf("status:%d\n",status); if(status == 0){ int csip = get32bit(&ptr2); printf("cid:%lld,csip:%X\n",chunkid,csip); if(cs.sockfd != -1 && cs.peerip != csip){ tcpclose(cs.sockfd); } if(cs.sockfd == -1){ if(serv_connect(&cs,csip,CS_PORT) < 0){ return -1; } } } else { return -1; } p = createpacket_s(8+4+4+buflen,CLTOCS_WRITE_CHUNK,-1); ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); put32bit(&ptr,off % CHUNKSIZE); put32bit(&ptr,buflen); memcpy(ptr,wbuf,buflen); fprintf(stderr,"starti=%d,chunkid=%lld,off=%d,buflen=%d\n",starti,chunkid,off % CHUNKSIZE,buflen); sendpacket(cs.sockfd,p); free(p); p = receivepacket(cs.sockfd); ptr2 = p->startptr; status = get32bit(&ptr2); printf("status=%d\n",status); if(status == 0){ int wlen = get32bit(&ptr2); nwrite += wlen; printf("wlen=%d,nwrite=%d\n",wlen,nwrite); wbuf += wlen; } st -= buflen; off += buflen; starti = off/CHUNKSIZE; buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); } } else { return status; } fprintf(stderr,"off=%d,st=%d,nwrite=%d\n",ooff,ost,nwrite); return nwrite; }
int flock_load(bio *fd,uint8_t mver,uint8_t ignoreflag) { uint8_t loadbuff[FLOCK_REC_SIZE]; const uint8_t *ptr; int32_t r; uint32_t inode,sessionid; uint64_t owner; uint8_t ltype; inodelocks *il; lock *l; if (mver!=0x10) { return -1; } for (;;) { r = bio_read(fd,loadbuff,FLOCK_REC_SIZE); if (r!=FLOCK_REC_SIZE) { return -1; } ptr = loadbuff; inode = get32bit(&ptr); owner = get64bit(&ptr); sessionid = get32bit(&ptr); ltype = get8bit(&ptr); if (inode==0 && owner==0 && sessionid==0) { return 0; } if (of_checknode(sessionid,inode)==0) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!! (ignoring)"); continue; } else { mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!!"); return -1; } } // add lock il = flock_inode_find(inode); if (il==NULL) { il = flock_inode_new(inode); } if (il->active!=NULL && (il->active->ltype==LTYPE_WRITER || ltype==LTYPE_WRITER)) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!! (ignoring)"); continue; } else { mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!!"); return -1; } } l = malloc(sizeof(lock)); l->owner = owner; l->sessionid = sessionid; l->state = STATE_ACTIVE; l->ltype = ltype; l->lock_instances = NULL; l->parent = il; l->next = NULL; l->prev = NULL; flock_do_lock_inode_attach(l); } return 0; // unreachable }