Skip to content

Commit

Permalink
Merge branch 'lab6' into lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
remysys committed Aug 2, 2015
2 parents aa105bd + af347b7 commit 6375d10
Show file tree
Hide file tree
Showing 32 changed files with 3,298 additions and 97 deletions.
3 changes: 3 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ endif
ifeq ($(LAB4GE),1)
yfs_client += lock_client_cache.cc
endif
ifeq ($(LAB5GE),1)
yfs_client += extent_client_cache.cc
endif
yfs_client : $(patsubst %.cc,%.o,$(yfs_client)) rpc/librpc.a

extent_server=extent_server.cc extent_smain.cc
Expand Down
1,021 changes: 1,021 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added core
Binary file not shown.
30 changes: 26 additions & 4 deletions extent_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,40 @@

class extent_client {
private:
protected:
rpcc *cl;

public:
extent_client(std::string dst);

extent_protocol::status get(extent_protocol::extentid_t eid,
virtual extent_protocol::status get(extent_protocol::extentid_t eid,
std::string &buf);
extent_protocol::status getattr(extent_protocol::extentid_t eid,
virtual 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);
virtual extent_protocol::status put(extent_protocol::extentid_t eid, std::string buf);
virtual extent_protocol::status remove(extent_protocol::extentid_t eid);
};

class extent_client_cache : public extent_client {
enum file_state {NONE,UPDATED, MODIFIED, REMOVED};
struct extent {
std::string data;
file_state status;
extent_protocol::attr attr;
extent():status(NONE) {}
};
public:
extent_client_cache(std::string dst);
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);
extent_protocol::status flush(extent_protocol::extentid_t eid);
private:
pthread_mutex_t extent_mutex;
std::map <extent_protocol::extentid_t, extent>file_cached;
};
#endif

196 changes: 196 additions & 0 deletions extent_client_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@

// 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>

// The calls assume that the caller holds a lock on the extent
extent_client_cache::extent_client_cache(std::string dst)
:extent_client(dst)
{
VERIFY(pthread_mutex_init(&extent_mutex, NULL) == 0);
}

extent_protocol::status
extent_client_cache::get(extent_protocol::extentid_t eid, std::string &buf)
{
extent_protocol::status ret = extent_protocol::OK;
ScopedLock _m(&extent_mutex);
bool flag = file_cached.count(eid);
if (flag) {
switch (file_cached[eid].status) {
case UPDATED:
case MODIFIED:
buf = file_cached[eid].data;
file_cached[eid].attr.atime = time(NULL);
break;
case NONE:
ret = cl->call(extent_protocol::get, eid, buf);
if (ret == extent_protocol::OK) {
file_cached[eid].data = buf;
file_cached[eid].status = UPDATED;
file_cached[eid].attr.atime = time(NULL);
file_cached[eid].attr.size = buf.size();
}
break;
case REMOVED:
default:
ret = extent_protocol::NOENT;
break;
}
} else {
ret = cl->call(extent_protocol::get, eid, buf);
if (ret == extent_protocol::OK) {
file_cached[eid].data = buf;
file_cached[eid].status = UPDATED;
file_cached[eid].attr.atime = time(NULL);
file_cached[eid].attr.size = buf.size();
file_cached[eid].attr.ctime = 0;
file_cached[eid].attr.mtime = 0;
}
}
buf = file_cached[eid].data;
return ret;
}

extent_protocol::status
extent_client_cache::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
extent_protocol::status ret = extent_protocol::OK;
extent_protocol::attr *a, temp;
ScopedLock _m(&extent_mutex);
bool flag = file_cached.count(eid);
if (flag) {
switch (file_cached[eid].status) {
case UPDATED:
case MODIFIED:
case NONE:
a = &file_cached[eid].attr;
if (!a->atime || !a->ctime || !a->mtime) {
ret = cl->call(extent_protocol::getattr, eid, temp);
if (ret == extent_protocol::OK) {
if (!a->atime)
a->atime = temp.atime;
if (!a->ctime)
a->ctime = temp.ctime;
if (!a->mtime)
a->mtime = temp.mtime;
if (file_cached[eid].status == NONE)
a->size = temp.size;
else
a->size = file_cached[eid].attr.size;
}
}
break;
case REMOVED:
default:
ret = extent_protocol::NOENT;
break;
}
} else {
file_cached[eid].status = NONE;
a = &file_cached[eid].attr;
a->atime = a->mtime = a->ctime = 0;
a->size = 0;
ret = cl->call(extent_protocol::getattr, eid, temp);
if (ret == extent_protocol::OK) {
a->atime = temp.atime;
a->ctime = temp.ctime;
a->mtime = temp.mtime;
a->size = temp.size;
}
}
attr = *a;
return ret;
}

extent_protocol::status
extent_client_cache::put(extent_protocol::extentid_t eid, std::string buf)
{
extent_protocol::status ret = extent_protocol::OK;
int r;
ScopedLock _m(&extent_mutex);
bool flag = file_cached.count(eid);

if (flag) {
switch (file_cached[eid].status) {
case NONE:
case UPDATED:
case MODIFIED:
file_cached[eid].data = buf;
file_cached[eid].status = MODIFIED;
file_cached[eid].attr.mtime = time(NULL);
file_cached[eid].attr.ctime = time(NULL);
file_cached[eid].attr.size = buf.size();
break;
case REMOVED:
ret = extent_protocol::NOENT;
break;
}
} else {
file_cached[eid].data = buf;
file_cached[eid].status = MODIFIED;
file_cached[eid].attr.atime = time(NULL);
file_cached[eid].attr.mtime = time(NULL);
file_cached[eid].attr.ctime = time(NULL);
file_cached[eid].attr.size = buf.size();
}
return ret;
}

extent_protocol::status
extent_client_cache::remove(extent_protocol::extentid_t eid)
{
extent_protocol::status ret = extent_protocol::OK;
int r;
ScopedLock _m(&extent_mutex);
bool flag = file_cached.count(eid);
if (flag) {
switch (file_cached[eid].status) {
case NONE:
case UPDATED:
case MODIFIED:
file_cached[eid].status = REMOVED;
break;
case REMOVED:
ret = extent_protocol::NOENT;
break;
}
} else {
file_cached[eid].status = REMOVED;
// ret = extent_protocol::NOENT;
}
return ret;
}

extent_protocol::status
extent_client_cache::flush(extent_protocol::extentid_t eid)
{
extent_protocol::status ret = extent_protocol::OK;
int r;
ScopedLock _m(&extent_mutex);
bool flag = file_cached.count(eid);
if (flag) {
switch(file_cached[eid].status) {
case MODIFIED:
ret = cl->call(extent_protocol::put, eid, file_cached[eid].data, r);
break;
case REMOVED:
ret = cl->call(extent_protocol::remove, eid);
break;
case NONE:
case UPDATED:
default:
break;
}
file_cached.erase(eid);
} else {
ret = extent_protocol::NOENT;
}
return ret;
}
49 changes: 42 additions & 7 deletions extent_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@
#include <sys/stat.h>
#include <fcntl.h>

extent_server::extent_server() {}
extent_server::extent_server() {
int ret;
pthread_mutex_init(&mutex, NULL);
//root i-number is 1
put(1, "", ret);
}


int extent_server::put(extent_protocol::extentid_t id, std::string buf, int &)
{
// You fill this in for Lab 2.
return extent_protocol::IOERR;
ScopedLock _l(&mutex);
extent_protocol::attr attr;
attr.atime = attr.mtime = attr.ctime = time(NULL);
if (file_map.find(id) != file_map.end())
attr.atime = file_map[id].attr.atime;
attr.size = buf.size();
file_map[id].data = buf;
file_map[id].attr = attr;
return extent_protocol::OK;
// return extent_protocol::IOERR;
}

int extent_server::get(extent_protocol::extentid_t id, std::string &buf)
{
// You fill this in for Lab 2.
return extent_protocol::IOERR;
ScopedLock _l(&mutex);
if (file_map.find(id) != file_map.end()) {
file_map[id].attr.atime = time(NULL);
buf = file_map[id].data;
return extent_protocol::OK;
}
return extent_protocol::NOENT;
// return extent_protocol::IOERR;
}

int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a)
Expand All @@ -29,16 +50,30 @@ int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr
// You replace this with a real implementation. We send a phony response
// for now because it's difficult to get FUSE to do anything (including
// unmount) if getattr fails.
a.size = 0;
/* a.size = 0;
a.atime = 0;
a.mtime = 0;
a.ctime = 0;
return extent_protocol::OK;
a.ctime = 0;*/
ScopedLock _l(&mutex);
if (file_map.find(id) != file_map.end()) {
a = file_map[id].attr;
return extent_protocol::OK;
}
return extent_protocol::NOENT;
// return extent_protocol::OK;
}

int extent_server::remove(extent_protocol::extentid_t id, int &)
{
// You fill this in for Lab 2.
return extent_protocol::IOERR;
std::map<extent_protocol::extentid_t, extent>::iterator iter;
iter = file_map.find(id);
if (iter != file_map.end()) {
file_map.erase(iter);
return extent_protocol::OK;
} else {
return extent_protocol::NOENT;
}
// return extent_protocol::IOERR;
}

8 changes: 7 additions & 1 deletion extent_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
#include "extent_protocol.h"

class extent_server {

struct extent {
std::string data;
extent_protocol::attr attr;
};
public:
extent_server();

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 &);
private:
pthread_mutex_t mutex;
std::map<extent_protocol::extentid_t, extent> file_map;
};

#endif
Expand Down
1 change: 1 addition & 0 deletions extent_smain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "extent_server.h"
#include <unistd.h>

// Main loop of extent server

Expand Down
Loading

0 comments on commit 6375d10

Please sign in to comment.