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); }
uint32_t flock_list(uint32_t inode,uint8_t *buff) { inodelocks *il; lock *l; uint32_t h; uint32_t ret=0; if (inode==0) { for (h=0 ; h<FLOCK_INODE_HASHSIZE ; h++) { for (il = inodehash[h] ; il ; il=il->next) { for (l=il->active ; l ; l=l->next) { if (buff==NULL) { ret+=37; } else { put32bit(&buff,il->inode); put32bit(&buff,l->sessionid); put64bit(&buff,l->owner); memset(buff,0,20); // pid,start,end buff+=20; switch (l->ltype) { case LTYPE_READER: put8bit(&buff,1); break; case LTYPE_WRITER: put8bit(&buff,2); break; default: put8bit(&buff,0); } } } } } } else { il = flock_inode_find(inode); if (il!=NULL) { for (l=il->active ; l ; l=l->next) { if (buff==NULL) { ret+=33; } else { put32bit(&buff,l->sessionid); put64bit(&buff,l->owner); memset(buff,0,20); // pid,start,end buff+=20; switch (l->ltype) { case LTYPE_READER: put8bit(&buff,1); break; case LTYPE_WRITER: put8bit(&buff,2); break; default: put8bit(&buff,0); } } } } } return ret; }
void masterconn_sendregister(masterconn *eptr) { uint8_t *buff; uint32_t myip; uint16_t myport; uint64_t usedspace,totalspace; uint64_t tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; myip = csserv_getlistenip(); myport = csserv_getlistenport(); if (eptr->new_register_mode) { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 6 - init + space info"); #endif hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); if (eptr->gotrndblob && AuthCode) { md5ctx md5c; buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+16+4+4+2+2+2+8+8+4+8+8+4); put8bit(&buff,60); md5_init(&md5c); md5_update(&md5c,eptr->rndblob,16); md5_update(&md5c,(const uint8_t *)AuthCode,strlen(AuthCode)); md5_update(&md5c,eptr->rndblob+16,16); md5_final(buff,&md5c); buff+=16; } else { buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+4+4+2+2+2+8+8+4+8+8+4); put8bit(&buff,60); } put32bit(&buff,VERSHEX); put32bit(&buff,myip); put16bit(&buff,myport); put16bit(&buff,Timeout); put16bit(&buff,masterconn_getcsid()); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); } else { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 5 - init"); #endif buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+4+4+2+2); put8bit(&buff,50); put32bit(&buff,VERSHEX); put32bit(&buff,myip); put16bit(&buff,myport); if (Timeout>0) { put16bit(&buff,Timeout); } else { put16bit(&buff,10); } } }
void csserv_idlejob_finished(uint8_t status,void *ijp) { idlejob *ij = (idlejob*)ijp; csserventry *eptr = ij->eptr; uint8_t *ptr; if (eptr) { switch (ij->op) { case IJ_GET_CHUNK_BLOCKS: ptr = csserv_create_packet(eptr,CSTOAN_CHUNK_BLOCKS,8+4+2+1); put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); if (status==MFS_STATUS_OK) { memcpy(ptr,ij->buff,2); ptr+=2; } else { put16bit(&ptr,0); } put8bit(&ptr,status); break; case IJ_GET_CHUNK_CHECKSUM: if (status!=MFS_STATUS_OK) { ptr = csserv_create_packet(eptr,CSTOAN_CHUNK_CHECKSUM,8+4+1); } else { ptr = csserv_create_packet(eptr,CSTOAN_CHUNK_CHECKSUM,8+4+4); } put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); if (status!=MFS_STATUS_OK) { put8bit(&ptr,status); } else { memcpy(ptr,ij->buff,4); } break; case IJ_GET_CHUNK_CHECKSUM_TAB: if (status!=MFS_STATUS_OK) { ptr = csserv_create_packet(eptr,CSTOAN_CHUNK_CHECKSUM_TAB,8+4+1); } else { ptr = csserv_create_packet(eptr,CSTOAN_CHUNK_CHECKSUM_TAB,8+4+4096); } put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); if (status!=MFS_STATUS_OK) { put8bit(&ptr,status); } else { memcpy(ptr,ij->buff,4096); } break; } *(ij->prev) = ij->next; if (ij->next) { ij->next->prev = ij->prev; } } free(ij); }
//读取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_idlejob_finished(uint8_t status,void *ijp) { idlejob *ij = (idlejob*)ijp; masterconn *eptr = masterconnsingleton; uint8_t *ptr; if (eptr && eptr->mode == DATA && ij->valid) { switch (ij->op) { case IJ_GET_CHUNK_BLOCKS: ptr = masterconn_create_attached_packet(eptr,CSTOAN_CHUNK_BLOCKS,8+4+2+1); put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); memcpy(ptr,ij->buff,2); ptr+=2; put8bit(&ptr,status); break; case IJ_GET_CHUNK_CHECKSUM: if (status!=MFS_STATUS_OK) { ptr = masterconn_create_attached_packet(eptr,CSTOAN_CHUNK_CHECKSUM,8+4+1); } else { ptr = masterconn_create_attached_packet(eptr,CSTOAN_CHUNK_CHECKSUM,8+4+4); } put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); if (status!=MFS_STATUS_OK) { put8bit(&ptr,status); } else { memcpy(ptr,ij->buff,4); } break; case IJ_GET_CHUNK_CHECKSUM_TAB: if (status!=MFS_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); } put64bit(&ptr,ij->chunkid); put32bit(&ptr,ij->version); if (status!=MFS_STATUS_OK) { put8bit(&ptr,status); } else { memcpy(ptr,ij->buff,4096); } break; } } if (ij->valid) { *(ij->prev) = ij->next; if (ij->next) { ij->next->prev = ij->prev; } } free(ij); }
void masterconn_check_hdd_reports() { masterconn *eptr = masterconnsingleton; uint32_t errorcounter; uint32_t chunkcounter; uint8_t *buff; if (eptr->mode==DATA || eptr->mode==HEADER) { if (hdd_spacechanged()) { uint64_t usedspace,totalspace,tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; buff = masterconn_create_attached_packet(eptr,CSTOMA_SPACE,8+8+4+8+8+4); if (buff) { hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); } } errorcounter = hdd_errorcounter(); while (errorcounter) { masterconn_create_attached_packet(eptr,CSTOMA_ERROR_OCCURRED,0); errorcounter--; } chunkcounter = hdd_get_damaged_chunk_count(); // lock if (chunkcounter) { buff = masterconn_create_attached_packet(eptr,CSTOMA_CHUNK_DAMAGED,8*chunkcounter); if (buff) { hdd_get_damaged_chunk_data(buff); // unlock } else { hdd_get_damaged_chunk_data(NULL); // unlock } } else { hdd_get_damaged_chunk_data(NULL); } chunkcounter = hdd_get_lost_chunk_count(); // lock if (chunkcounter) { buff = masterconn_create_attached_packet(eptr,CSTOMA_CHUNK_LOST,8*chunkcounter); if (buff) { hdd_get_lost_chunk_data(buff); // unlock } else { hdd_get_lost_chunk_data(NULL); // unlock } } else { hdd_get_lost_chunk_data(NULL); } } }
//读取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 charts_makedata(uint8_t *buff,uint32_t number) { uint32_t i,j,ts,pointer,chtype,chrange; uint64_t *tab; chtype = number / 10; chrange = number % 10; if (chrange<RANGES && CHARTS_IS_DIRECT_STAT(chtype)) { tab = series[chtype][chrange]; pointer = pointers[chrange]; ts = timepoint[chrange]; switch (chrange) { case SHORTRANGE: ts *= 60; break; case MEDIUMRANGE: ts *= 60*6; break; case LONGRANGE: ts *= 60*30; break; case VERYLONGRANGE: ts *= 60*60*24; break; } put32bit(&buff,ts); for (i=0 ; i<LENG ; i++) { j = (LENG+1+pointer+i)%LENG; put64bit(&buff,tab[j]); } } }
/*---------------------------------------------------------------------*/ 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; }
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); } }
int client_unlink(uint64_t parent, uint32_t nleng, const uint8_t* name, uint32_t uid, uint32_t gid) { int iSendLeng = 8 + 20 + nleng ; uint8_t *szCommand = (uint8_t *)def_calloc(iSendLeng, sizeof(uint8_t)); char *curr = (char *)szCommand; put32bit(&szCommand, CLTODI_UNLINK); put32bit(&szCommand, 20 + nleng); put64bit(&szCommand, parent); put32bit(&szCommand, uid); put32bit(&szCommand, gid); put32bit(&szCommand, nleng); memcpy(szCommand, name, nleng); 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); int ret2 = rpc_result_check(ret, szOutBuf, iOutLength); return ret2; }
void masterconn_download_next(masterconn *eptr) { uint8_t *ptr; uint8_t filenum; int64_t dltime; if (eptr->dloffset>=eptr->filesize) { // end of file filenum = eptr->downloading; if (masterconn_download_end(eptr)<0) { return; } dltime = main_utime()-eptr->dlstartuts; if (dltime<=0) { dltime=1; } syslog(LOG_NOTICE,"%s downloaded %"PRIu64"B/%"PRIu64".%06"PRIu32"s (%.3lf MB/s)",(filenum==1)?"metadata":(filenum==2)?"sessions":(filenum==11)?"changelog_0":(filenum==12)?"changelog_1":"???",eptr->filesize,dltime/1000000,(uint32_t)(dltime%1000000),(double)(eptr->filesize)/(double)(dltime)); if (filenum==1) { if (masterconn_metadata_check("metadata_ml.tmp")==0) { if (BackMetaCopies>0) { char metaname1[100],metaname2[100]; int i; for (i=BackMetaCopies-1 ; i>0 ; i--) { snprintf(metaname1,100,"metadata_ml.mfs.back.%"PRIu32,i+1); snprintf(metaname2,100,"metadata_ml.mfs.back.%"PRIu32,i); rename(metaname2,metaname1); } rename("metadata_ml.mfs.back","metadata_ml.mfs.back.1"); } if (rename("metadata_ml.tmp","metadata_ml.mfs.back")<0) { syslog(LOG_NOTICE,"can't rename downloaded metadata - do it manually before next download"); } } if (eptr->oldmode==0) { masterconn_download_init(eptr,11); } else { masterconn_download_init(eptr,2); } } else if (filenum==11) { if (rename("changelog_ml.tmp","changelog_ml_back.0.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded changelog - do it manually before next download"); } masterconn_download_init(eptr,12); } else if (filenum==12) { if (rename("changelog_ml.tmp","changelog_ml_back.1.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded changelog - do it manually before next download"); } masterconn_download_init(eptr,2); } else if (filenum==2) { if (rename("sessions_ml.tmp","sessions_ml.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded sessions - do it manually before next download"); } } } else { // send request for next data packet ptr = masterconn_createpacket(eptr,MLTOMA_DOWNLOAD_DATA,12); put64bit(&ptr,eptr->dloffset); if (eptr->filesize-eptr->dloffset>META_DL_BLOCK) { put32bit(&ptr,META_DL_BLOCK); } else { put32bit(&ptr,eptr->filesize-eptr->dloffset); } } }
void masterconn_check_hdd_space() { masterconn *eptr = masterconnsingleton; uint8_t *buff; if ((eptr->registerstate==REGISTERED || eptr->registerstate==INPROGRESS) && eptr->mode==DATA) { if (hdd_spacechanged()) { uint64_t usedspace,totalspace,tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; buff = masterconn_create_attached_packet(eptr,CSTOMA_SPACE,8+8+4+8+8+4); hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); } } }
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; }
void masterconn_sendchunksinfo(masterconn *eptr) { uint8_t *buff; uint32_t chunks; uint64_t usedspace,totalspace; uint64_t tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. %u - chunks info",(eptr->new_register_mode)?6:5); #endif hdd_get_chunks_begin(0); while ((chunks = hdd_get_chunks_next_list_count())) { buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+chunks*(8+4)); if (eptr->new_register_mode) { put8bit(&buff,61); } else { put8bit(&buff,51); } hdd_get_chunks_next_list_data(buff); } hdd_get_chunks_end(); if (eptr->new_register_mode) { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 6 - end"); #endif buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1); put8bit(&buff,62); } else { #ifdef MFSDEBUG syslog(LOG_NOTICE,"register ver. 5 - end + space info"); #endif hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+8+8+4+8+8+4); put8bit(&buff,52); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); } }
void masterconn_download_next(serventry *eptr) { uint8_t *ptr; uint8_t filenum; uint64_t dltime; uint64_t starttime; if (eptr->dloffset>=eptr->filesize) { // end of file filenum = eptr->downloading; dltime = main_utime()-eptr->dlstartuts; MFSLOG(LOG_NOTICE,"%s downloaded %"PRIu64"B/%"PRIu64".%06"PRIu32"s (%.3lf MB/s)",(filenum==1)?"metadata":(filenum==2)?"changelog.0":"???",eptr->filesize,dltime/1000000,(uint32_t)(dltime%1000000),(double)(eptr->filesize)/(double)(dltime)); if (filenum==1) { if (rename("metadata.tmp","metadata.mfs")<0) { MFSLOG(LOG_NOTICE,"can't rename downloaded metadata - do it manually before next download"); } else { MFSLOG(LOG_NOTICE,"loading metadata ..."); fs_strinit(); chunk_strinit(); starttime = get_current_time(); if (fs_loadall(NULL)<0) { return; MFSLOG(LOG_NOTICE,"fs init failed"); } MFSLOG(LOG_NOTICE,"metadata file has been loaded"); } } else if (filenum==2) { if (rename("changelog.0.tmp","changelog.0.mfs")<0) { MFSLOG(LOG_NOTICE,"can't rename downloaded changelog.0.mfs - do it manually before next download"); } else { if (restore() < 0) { MFSLOG(LOG_ERR,"restore failed"); } else { meta_ready = 0; } } } //filenum:1,metadata finish ;2,changelog.0 finish if (masterconn_download_end(eptr,filenum)<0) { return; } } else { // send request for next data packet ptr = masterconn_createpacket(eptr,SLATOMA_DOWNLOAD_DATA,12); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,eptr->dloffset); if (eptr->filesize-eptr->dloffset>META_DL_BLOCK) { put32bit(&ptr,META_DL_BLOCK); } else { put32bit(&ptr,eptr->filesize-eptr->dloffset); } } }
void matomlserv_broadcast_logstring(uint64_t version,uint8_t *logstr,uint32_t logstrsize) { matomlserventry *eptr; uint8_t *data; for (eptr = matomlservhead ; eptr ; eptr=eptr->next) { if (eptr->version>0) { data = matomlserv_createpacket(eptr,MATOML_METACHANGES_LOG,9+logstrsize); put8bit(&data,0xFF); put64bit(&data,version); memcpy(data,logstr,logstrsize); } } }
//封装向Master发送注册信息的packet,存入服务队列 //调用:masterconn_connected() void masterconn_sendregister(masterconn *eptr) { uint8_t *buff; uint32_t chunks,myip; uint16_t myport; uint64_t usedspace,totalspace; uint64_t tdusedspace,tdtotalspace; uint32_t chunkcount,tdchunkcount; myip = csserv_getlistenip(); myport = csserv_getlistenport(); hdd_get_space(&usedspace,&totalspace,&chunkcount,&tdusedspace,&tdtotalspace,&tdchunkcount); chunks = hdd_get_chunks_count(); buff = masterconn_create_attached_packet(eptr,CSTOMA_REGISTER,1+4+4+2+2+8+8+4+8+8+4+chunks*(8+4)); if (buff==NULL) { eptr->mode=KILL; hdd_get_chunks_data(NULL); // unlock return; } put8bit(&buff,4); /* put32bit(&buff,VERSION): */ put16bit(&buff,VERSMAJ); put8bit(&buff,VERSMID); put8bit(&buff,VERSMIN); put32bit(&buff,myip); put16bit(&buff,myport); put16bit(&buff,Timeout); put64bit(&buff,usedspace); put64bit(&buff,totalspace); put32bit(&buff,chunkcount); put64bit(&buff,tdusedspace); put64bit(&buff,tdtotalspace); put32bit(&buff,tdchunkcount); if (chunks>0) { hdd_get_chunks_data(buff); } else { hdd_get_chunks_data(NULL); // unlock } }
//读取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 */ }
void matomlserv_send_archived_changes(matomlserventry *eptr) { char buff[4096],logname[30],*ptr; uint8_t *data; uint64_t nextid; int i,len; for (i=0; i<50000 && eptr->logf; i++) { if (fgets(buff,4096,eptr->logf)!=NULL) { len=strlen(buff); if (len==0 || buff[len-1]!='\n') { syslog(LOG_WARNING,"meta logger broken changelog: len=%d", len); fclose(eptr->logf); eptr->logf=NULL; return; } nextid=strtoull(buff,&ptr,10); if (nextid<eptr->currentversion) continue; ptr += 2; // ": " len=strlen(ptr); ptr[len-1]='\0'; // drop \n data = matomlserv_createpacket(eptr,MATOML_METACHANGES_LOG,9+len); put8bit(&data,0xFF); put64bit(&data,nextid); memcpy(data,ptr,len); eptr->currentversion++; if (old_changes_head && eptr->currentversion==old_changes_head->minversion) { fclose(eptr->logf); eptr->logf=NULL; } } else { // end of file fclose(eptr->logf); eptr->logf=NULL; eptr->logindex--; if (eptr->logindex>=0) { data = matomlserv_createpacket(eptr,MATOML_METACHANGES_LOG,1); put8bit(&data,0x55); sprintf(logname,"changelog.%d.mfs",eptr->logindex); if ((eptr->logf=fopen(logname,"r"))==NULL) { syslog(LOG_WARNING,"meta logger open %s failed", logname); } } } } }
void masterconn_delete(masterconn *eptr,const uint8_t *data,uint32_t length) { uint64_t chunkid; uint32_t version; uint8_t *ptr; void *packet; if (length!=8+4) { syslog(LOG_NOTICE,"MATOCS_DELETE - wrong size (%"PRIu32"/12)",length); eptr->mode = KILL; return; } chunkid = get64bit(&data); version = get32bit(&data); packet = masterconn_create_detached_packet(eptr,CSTOMA_DELETE,8+1); ptr = masterconn_get_packet_data(packet); put64bit(&ptr,chunkid); job_delete(masterconn_jobfinished,packet,chunkid,version); }
void csserv_module_info(csserventry *eptr,const uint8_t *data,uint32_t length) { uint8_t *ptr; if (length!=0) { syslog(LOG_NOTICE,"CLTOAN_MODULE_INFO - wrong size (%"PRIu32"/0)",length); eptr->state = CLOSE; return; } (void)data; ptr = csserv_create_packet(eptr,ANTOCL_MODULE_INFO,21); put8bit(&ptr,MODULE_TYPE_CHUNKSERVER); put16bit(&ptr,VERSMAJ); put8bit(&ptr,VERSMID); put8bit(&ptr,VERSMIN); put16bit(&ptr,masterconn_getcsid()); put64bit(&ptr,masterconn_getmetaid()); put32bit(&ptr,masterconn_getmasterip()); put16bit(&ptr,masterconn_getmasterport()); }
void matoslaserv_download_start(serventry *eptr,const uint8_t *data,uint32_t length) { uint8_t filenum; uint64_t size; uint8_t *ptr; if (length!=1) { MFSLOG(LOG_NOTICE,"slaTOMA_DOWNLOAD_START - wrong size (%"PRIu32"/1)",length); eptr->mode=KILL; return; } if (eptr->metafd>0) { close(eptr->metafd); eptr->metafd=-1; } filenum = get8bit(&data); if (filenum==1) { eptr->metafd = open("metadata.mfs.back",O_RDONLY); } else if (filenum==2) { eptr->metafd = open("changelog.0.mfs",O_RDONLY); } else { eptr->mode=KILL; return; } if (eptr->metafd<0) { ptr = matoslaserv_createpacket(eptr,MATOSLA_DOWNLOAD_START,1); if (ptr==NULL) { eptr->mode=KILL; return; } put8bit(&ptr,0xff); // error return; } size = lseek(eptr->metafd,0,SEEK_END); if (filenum==2) { eptr->changelog_offset = size; // syslog(LOG_NOTICE,"changelog_offset is %d",changelog_offset); } ptr = matoslaserv_createpacket(eptr,MATOSLA_DOWNLOAD_START,8); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,size); // ok }
uint32_t missing_log_getdata(uint8_t *buff,uint8_t mode) { uint32_t i,j; if (buff==NULL) { return mloghashprevelements*((mode==0)?16:17); } else { j = 0; for (i=0 ; i<mloghashprevsize && j<mloghashprevelements ; i++) { if (mloghashprev[i].chunkid!=0) { put64bit(&buff,mloghashprev[i].chunkid); put32bit(&buff,mloghashprev[i].inode); put32bit(&buff,mloghashprev[i].indx); if (mode) { put8bit(&buff,mloghashprev[i].type); } j++; } } return 0; } }
uint32_t job_replicate_simple(void (*callback)(uint8_t status,void *extra),void *extra,uint64_t chunkid,uint32_t version,uint32_t ip,uint16_t port) { jobpool* jp = globalpool; chunk_rp_args *args; uint8_t *ptr; ptr = malloc(sizeof(chunk_rp_args)+18); passert(ptr); args = (chunk_rp_args*)ptr; ptr += sizeof(chunk_rp_args); args->chunkid = chunkid; args->version = version; args->srccnt = 1; args->xormasks[0] = UINT32_C(0x88888888); args->xormasks[1] = UINT32_C(0x44444444); args->xormasks[2] = UINT32_C(0x22222222); args->xormasks[3] = UINT32_C(0x11111111); put64bit(&ptr,chunkid); put32bit(&ptr,version); put32bit(&ptr,ip); put16bit(&ptr,port); return job_new(jp,OP_REPLICATE,args,callback,extra); }
// 获取dslist 列表 int client_get_dslist(char szmac[12], uint64_t inode, uint8_t **szOutBuf, uint32_t *iOutLength) { int iSendLeng = 8 + 12 + 8 ; uint8_t *szCommand = (uint8_t *)def_calloc(iSendLeng, sizeof(char)); char *curr = (char *)szCommand; put32bit(&szCommand, CLTODI_GET_DSLIST); put32bit(&szCommand, 12 + 8 ); memcpy(szCommand, szmac, 12); szCommand += 12; put64bit(&szCommand, inode); 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, (char **)&szOutBuf, &iOutLength); def_free(curr); return ret; }
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); }