Skip to content

Commit

Permalink
lab1 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ytliu committed Oct 15, 2013
0 parents commit 5d806e9
Show file tree
Hide file tree
Showing 30 changed files with 4,996 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.d
lab1_tester
129 changes: 129 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
LAB=1
SOL=0
RPC=./rpc
LAB1GE=$(shell expr $(LAB) \>\= 1)
LAB2GE=$(shell expr $(LAB) \>\= 2)
LAB3GE=$(shell expr $(LAB) \>\= 3)
LAB4GE=$(shell expr $(LAB) \>\= 4)
LAB5GE=$(shell expr $(LAB) \>\= 5)
LAB6GE=$(shell expr $(LAB) \>\= 6)
LAB7GE=$(shell expr $(LAB) \>\= 7)
CXXFLAGS = -g -MMD -Wall -I. -I$(RPC) -DLAB=$(LAB) -DSOL=$(SOL) -D_FILE_OFFSET_BITS=64

ifeq ($(shell uname -s),Darwin)
MACFLAGS= -D__FreeBSD__=10
else
MACFLAGS=
endif
LDFLAGS = -L. -L/usr/local/lib
LDLIBS = -lpthread

LDLIBS += $(shell test -f `gcc -print-file-name=librt.so` && echo -lrt)
LDLIBS += $(shell test -f `gcc -print-file-name=libdl.so` && echo -ldl)
CC = g++
CXX = g++

lab: lab$(LAB)
lab1: lab1_tester
lab2: yfs_client
lab3: yfs_client extent_server lock_server test-lab-3-b test-lab-3-c
lab4: yfs_client extent_server lock_server lock_tester test-lab-3-b\
test-lab-3-c
lab5: yfs_client extent_server lock_server test-lab-3-b test-lab-3-c
lab6: lock_server rsm_tester
lab7: lock_tester lock_server rsm_tester

hfiles1=rpc/fifo.h rpc/connection.h rpc/rpc.h rpc/marshall.h rpc/method_thread.h\
rpc/thr_pool.h rpc/pollmgr.h rpc/jsl_log.h rpc/slock.h rpc/rpctest.cc\
lock_protocol.h lock_server.h lock_client.h gettime.h gettime.cc lang/verify.h \
lang/algorithm.h
hfiles2=yfs_client.h extent_client.h extent_protocol.h extent_server.h
hfiles3=lock_client_cache.h lock_server_cache.h handle.h tprintf.h
hfiles4=log.h rsm.h rsm_protocol.h config.h paxos.h paxos_protocol.h rsm_state_transfer.h rsmtest_client.h tprintf.h
hfiles5=rsm_state_transfer.h rsm_client.h
rsm_files = rsm.cc paxos.cc config.cc log.cc handle.cc

rpclib=rpc/rpc.cc rpc/connection.cc rpc/pollmgr.cc rpc/thr_pool.cc rpc/jsl_log.cc gettime.cc
rpc/librpc.a: $(patsubst %.cc,%.o,$(rpclib))
rm -f $@
ar cq $@ $^
ranlib rpc/librpc.a

rpc/rpctest=rpc/rpctest.cc
rpc/rpctest: $(patsubst %.cc,%.o,$(rpctest)) rpc/librpc.a

lock_demo=lock_demo.cc lock_client.cc
lock_demo : $(patsubst %.cc,%.o,$(lock_demo)) rpc/librpc.a

lock_tester=lock_tester.cc lock_client.cc
ifeq ($(LAB4GE),1)
lock_tester += lock_client_cache.cc
endif
ifeq ($(LAB7GE),1)
lock_tester+=rsm_client.cc handle.cc lock_client_cache_rsm.cc
endif
lock_tester : $(patsubst %.cc,%.o,$(lock_tester)) rpc/librpc.a

lock_server=lock_server.cc lock_smain.cc
ifeq ($(LAB4GE),1)
lock_server+=lock_server_cache.cc handle.cc
endif
ifeq ($(LAB6GE),1)
lock_server+= $(rsm_files)
endif
ifeq ($(LAB7GE),1)
lock_server+= lock_server_cache_rsm.cc
endif

lock_server : $(patsubst %.cc,%.o,$(lock_server)) rpc/librpc.a

lab1_tester=lab1_tester.cc extent_client.cc extent_server.cc inode_manager.cc
lab1_tester : $(patsubst %.cc,%.o,$(lab1_tester))
yfs_client=yfs_client.cc extent_client.cc fuse.cc extent_server.cc inode_manager.cc
ifeq ($(LAB3GE),1)
yfs_client += lock_client.cc
endif
ifeq ($(LAB7GE),1)
yfs_client += rsm_client.cc lock_client_cache_rsm.cc
endif
ifeq ($(LAB4GE),1)
yfs_client += lock_client_cache.cc
endif
yfs_client : $(patsubst %.cc,%.o,$(yfs_client)) rpc/librpc.a

extent_server=extent_server.cc extent_smain.cc
extent_server : $(patsubst %.cc,%.o,$(extent_server)) rpc/librpc.a

test-lab-3-b=test-lab-3-b.c
test-lab-3-b: $(patsubst %.c,%.o,$(test_lab_4-b)) rpc/librpc.a

test-lab-3-c=test-lab-3-c.c
test-lab-4-c: $(patsubst %.c,%.o,$(test_lab_4-c)) rpc/librpc.a

rsm_tester=rsm_tester.cc rsmtest_client.cc
rsm_tester: $(patsubst %.cc,%.o,$(rsm_tester)) rpc/librpc.a

%.o: %.cc
$(CXX) $(CXXFLAGS) -c $< -o $@

fuse.o: fuse.cc
$(CXX) -c $(CXXFLAGS) $(FUSEFLAGS) $(MACFLAGS) $<

# mklab.inc is needed by 6.824 staff only. Just ignore it.
-include mklab.inc

-include *.d
-include rpc/*.d

clean_files=rpc/rpctest rpc/*.o rpc/*.d rpc/librpc.a *.o *.d yfs_client extent_server lock_server lock_tester lock_demo rpctest test-lab-3-b test-lab-3-c rsm_tester lab1_tester
.PHONY: clean handin
clean:
rm $(clean_files) -rf

handin_ignore=$(clean_files) core* *log
handin_file=lab$(LAB).tgz
labdir=$(shell basename $(PWD))
handin:
@bash -c "cd ../; tar -X <(tr ' ' '\n' < <(echo '$(handin_ignore)')) -czvf $(handin_file) $(labdir); mv $(handin_file) $(labdir); cd $(labdir)"
@echo Please modify lab1.tgz to lab1_[your student id].tgz and upload it to ftp://ytliu.cc:public@public.sjtu.edu.cn/upload/
@echo Thanks!
58 changes: 58 additions & 0 deletions extent_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RPC stubs for clients to talk to extent_server

#include "extent_client.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <time.h>

extent_client::extent_client()
{
es = new extent_server();
}

extent_protocol::status
extent_client::create(uint32_t type, extent_protocol::extentid_t &id)
{
extent_protocol::status ret = extent_protocol::OK;
ret = es->create(type, id);
return ret;
}

extent_protocol::status
extent_client::get(extent_protocol::extentid_t eid, std::string &buf)
{
extent_protocol::status ret = extent_protocol::OK;
ret = es->get(eid, buf);
return ret;
}

extent_protocol::status
extent_client::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
extent_protocol::status ret = extent_protocol::OK;
ret = es->getattr(eid, attr);
return ret;
}

extent_protocol::status
extent_client::put(extent_protocol::extentid_t eid, std::string buf)
{
extent_protocol::status ret = extent_protocol::OK;
int r;
ret = es->put(eid, buf, r);
return ret;
}

extent_protocol::status
extent_client::remove(extent_protocol::extentid_t eid)
{
extent_protocol::status ret = extent_protocol::OK;
int r;
ret = es->remove(eid, r);
return ret;
}


27 changes: 27 additions & 0 deletions extent_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// extent client interface.

#ifndef extent_client_h
#define extent_client_h

#include <string>
#include "extent_protocol.h"
#include "extent_server.h"

class extent_client {
private:
extent_server *es;

public:
extent_client();

extent_protocol::status create(uint32_t type, extent_protocol::extentid_t &eid);
extent_protocol::status get(extent_protocol::extentid_t eid,
std::string &buf);
extent_protocol::status getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &a);
extent_protocol::status put(extent_protocol::extentid_t eid, std::string buf);
extent_protocol::status remove(extent_protocol::extentid_t eid);
};

#endif

56 changes: 56 additions & 0 deletions extent_protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// extent wire protocol

#ifndef extent_protocol_h
#define extent_protocol_h

#include "rpc.h"

class extent_protocol {
public:
typedef int status;
typedef unsigned long long extentid_t;
enum xxstatus { OK, RPCERR, NOENT, IOERR };
enum rpc_numbers {
put = 0x6001,
get,
getattr,
remove
};

enum types {
T_DIR = 1,
T_FILE
};

struct attr {
uint32_t type;
unsigned int atime;
unsigned int mtime;
unsigned int ctime;
unsigned int size;
};
};

inline unmarshall &
operator>>(unmarshall &u, extent_protocol::attr &a)
{
u >> a.type;
u >> a.atime;
u >> a.mtime;
u >> a.ctime;
u >> a.size;
return u;
}

inline marshall &
operator<<(marshall &m, extent_protocol::attr a)
{
m << a.type;
m << a.atime;
m << a.mtime;
m << a.ctime;
m << a.size;
return m;
}

#endif
80 changes: 80 additions & 0 deletions extent_server.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// the extent server implementation

#include "extent_server.h"
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

extent_server::extent_server()
{
im = new inode_manager();
}

int extent_server::create(uint32_t type, extent_protocol::extentid_t &id)
{
// alloc a new inode and return inum
printf("extent_server: create inode\n");
id = im->alloc_inode(type);

return extent_protocol::OK;
}

int extent_server::put(extent_protocol::extentid_t id, std::string buf, int &)
{
id &= 0x7fffffff;

const char * cbuf = buf.c_str();
int size = buf.size();
im->write_file(id, cbuf, size);

return extent_protocol::OK;
}

int extent_server::get(extent_protocol::extentid_t id, std::string &buf)
{
printf("extent_server: get %lld\n", id);

id &= 0x7fffffff;

int size = 0;
char *cbuf = NULL;

im->read_file(id, &cbuf, &size);
if (size == 0)
buf = "";
else {
buf.assign(cbuf, size);
free(cbuf);
}

return extent_protocol::OK;
}

int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a)
{
printf("extent_server: getattr %lld\n", id);

id &= 0x7fffffff;

extent_protocol::attr attr;
memset(&attr, 0, sizeof(attr));
im->getattr(id, attr);
a = attr;

return extent_protocol::OK;
}

int extent_server::remove(extent_protocol::extentid_t id, int &)
{
printf("extent_server: write %lld\n", id);

id &= 0x7fffffff;
im->remove_file(id);

return extent_protocol::OK;
}

39 changes: 39 additions & 0 deletions extent_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// this is the extent server

#ifndef extent_server_h
#define extent_server_h

#include <string>
#include <map>
#include "extent_protocol.h"
#include "inode_manager.h"

class extent_server {
protected:
#if 0
typedef struct extent {
std::string data;
struct extent_protocol::attr attr;
} extent_t;
std::map <extent_protocol::extentid_t, extent_t> extents;
#endif
inode_manager *im;

public:
extent_server();

int create(uint32_t type, extent_protocol::extentid_t &id);
int put(extent_protocol::extentid_t id, std::string, int &);
int get(extent_protocol::extentid_t id, std::string &);
int getattr(extent_protocol::extentid_t id, extent_protocol::attr &);
int remove(extent_protocol::extentid_t id, int &);
};

#endif







Loading

0 comments on commit 5d806e9

Please # to comment.