Skip to content

Commit

Permalink
solution to lab5
Browse files Browse the repository at this point in the history
  • Loading branch information
mlnotes committed Dec 19, 2012
1 parent 3a78f05 commit 0fefc98
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 15 deletions.
69 changes: 69 additions & 0 deletions extent_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,25 @@ extent_protocol::status
extent_client::get(extent_protocol::extentid_t eid, std::string &buf)
{
extent_protocol::status ret = extent_protocol::OK;
if(cache.find(eid) != cache.end()){
if(cache[eid].removed)
return extent_protocol::NOENT;
else if(cache[eid].content_cached){
buf = cache[eid].content;
cache[eid].a.atime = time(0);
printf("buffered content eid=>%16llu, buf=>%s\n", eid, buf.c_str());
return ret;
}
}

ret = cl->call(extent_protocol::get, eid, buf);
printf("get conent from server buf=>%s\n", buf.c_str());
if(ret == extent_protocol::OK){
printf("cache content in the get eid=>%16llu\n", eid);
cache[eid].content = buf;
cache[eid].content_cached = true;
cache[eid].a.atime = time(0);
}
return ret;
}

Expand All @@ -32,26 +50,77 @@ extent_client::getattr(extent_protocol::extentid_t eid,
extent_protocol::attr &attr)
{
extent_protocol::status ret = extent_protocol::OK;
if(cache.find(eid) != cache.end()){
if(cache[eid].removed)
return extent_protocol::NOENT;
attr = cache[eid].a;
return ret;
}

ret = cl->call(extent_protocol::getattr, eid, attr);
if(ret == extent_protocol::OK){
printf("cache attr in the getattr eid=>%16llu\n", eid);
cache[eid].a = attr;
}

return ret;
}

extent_protocol::status
extent_client::put(extent_protocol::extentid_t eid, std::string buf)
{
extent_protocol::status ret = extent_protocol::OK;
cache[eid].removed = false;
cache[eid].dirty = true;
cache[eid].content_cached = true;
cache[eid].content = buf;

cache[eid].a.size = buf.size();
cache[eid].a.ctime = cache[eid].a.mtime = time(0);
printf("put content into buffer eid=>%16llu, buf=>%s\n", eid, buf.c_str());


// TODO change attr?
return ret;

/*
int r;
ret = cl->call(extent_protocol::put, eid, buf, r);
return ret;
*/
}

extent_protocol::status
extent_client::remove(extent_protocol::extentid_t eid)
{
extent_protocol::status ret = extent_protocol::OK;
cache[eid].removed = true;
return ret;

/*
int r;
ret = cl->call(extent_protocol::remove, eid, r);
return ret;
*/
}

void
extent_client::flush(extent_protocol::extentid_t eid){
printf("extent client flush eid=>%16llu\n", eid);

if(cache.find(eid) == cache.end())
return;

if(cache[eid].removed){
int r;
printf("extent client flush removed eid=>%16llu\n", eid);
cl->call(extent_protocol::remove, eid, r);
}else if(cache[eid].dirty){
int r;
printf("extent client flush buf=>%s\n", cache[eid].content.c_str());
extent_protocol::status ret = cl->call(extent_protocol::put, eid, cache[eid].content, r);
printf("extent client flush return ret=>%d\n", ret);
}

cache.erase(eid);
}
35 changes: 35 additions & 0 deletions extent_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@

#include <string>
#include "extent_protocol.h"
#include "lock_client_cache.h"
#include "rpc.h"
#include <map>

struct extent_cache {
extent_cache(){
dirty = false;
removed = false;
content_cached = false;
}

bool dirty;
bool removed;

bool content_cached;
std::string content;
extent_protocol::attr a;
};


class extent_client {
private:
rpcc *cl;
std::map<extent_protocol::extentid_t, extent_cache> cache;

public:
extent_client(std::string dst);
Expand All @@ -20,6 +39,22 @@ class extent_client {
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);

void flush(extent_protocol::extentid_t eid);
};

class lock_release_cache:public lock_release_user{
public:
lock_release_cache(extent_client *ec){
this->ec = ec;
}

void dorelease(lock_protocol::lockid_t lid){
if(ec != 0)
ec->flush(lid);
}
private:
extent_client *ec;
};

#endif
Expand Down
2 changes: 2 additions & 0 deletions extent_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int extent_server::put(extent_protocol::extentid_t id, std::string buf, int &)
a.size = buf.size();
a.ctime = a.mtime = time(0);
attrs[id] = a;

printf("put id=>%16llu, buf=>%s\n", id, buf.c_str());
return extent_protocol::OK;
}

Expand Down
2 changes: 1 addition & 1 deletion fuse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fuseserver_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
{
printf("fuseserver_setattr 0x%x\n", to_set);
if (FUSE_SET_ATTR_SIZE & to_set) {
printf(" fuseserver_setattr set size to %zu\n", attr->st_size);
printf(" fuseserver_setattr set size to %016llx\n", attr->st_size);
struct stat st;
// You fill this in for Lab 2
#if 1
Expand Down
6 changes: 6 additions & 0 deletions lock_client_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ lock_client_cache::release(lock_protocol::lockid_t lid)
if(revokes.find(lid) != revokes.end()){ // release to the server
tprintf("[RELEASE TO SERVER]: lid=>%llu id=>%s\n", lid, this->id.c_str());
locks[lid].state = lock_client_info::RELEASING;
if(lu != 0)
lu->dorelease(lid);

pthread_mutex_unlock(&lock_mutex);

int r;
Expand Down Expand Up @@ -144,6 +147,9 @@ lock_client_cache::revoke_handler(lock_protocol::lockid_t lid,
pthread_mutex_lock(&lock_mutex);
if(locks[lid].state == lock_client_info::FREE){
locks[lid].state = lock_client_info::RELEASING;
if(lu != 0)
lu->dorelease(lid);

pthread_mutex_unlock(&lock_mutex);
tprintf("[RLEASE TO SERVER]: lid=>%llu id=>%s\n", lid, id.c_str());

Expand Down
47 changes: 34 additions & 13 deletions yfs_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
yfs_client::yfs_client(std::string extent_dst, std::string lock_dst)
{
ec = new extent_client(extent_dst);
lc = new lock_client_cache(lock_dst);
lrc = new lock_release_cache(ec);
lc = new lock_client_cache(lock_dst, lrc);
}

yfs_client::inum
Expand Down Expand Up @@ -70,7 +71,7 @@ std::string
yfs_client::dir2str(std::vector<dirent> &ents)
{
std::string str = "";
for(int i = 0; i < ents.size(); ++i)
for(unsigned int i = 0; i < ents.size(); ++i)
str += ents[i].name + ":" + filename(ents[i].inum) + ";";

return str;
Expand Down Expand Up @@ -98,6 +99,8 @@ yfs_client::getfile(inum inum, fileinfo &fin)
// - hold and release the file lock

printf("getfile %016llx\n", inum);

lc->acquire(inum);
extent_protocol::attr a;
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
Expand All @@ -111,7 +114,7 @@ yfs_client::getfile(inum inum, fileinfo &fin)
printf("getfile %016llx -> sz %llu\n", inum, fin.size);

release:

lc->release(inum);
return r;
}

Expand All @@ -124,6 +127,8 @@ yfs_client::getdir(inum inum, dirinfo &din)

printf("getdir %016llx\n", inum);
extent_protocol::attr a;

lc->acquire(inum);
if (ec->getattr(inum, a) != extent_protocol::OK) {
r = IOERR;
goto release;
Expand All @@ -133,6 +138,7 @@ yfs_client::getdir(inum inum, dirinfo &din)
din.ctime = a.ctime;

release:
lc->release(inum);
return r;
}

Expand All @@ -142,13 +148,18 @@ yfs_client::readdir(inum inum, std::vector<dirent> &ents)
int r = OK;

printf("readdir %016llx\n", inum);

lc->acquire(inum);
std::string str;
r = ec->get(inum, str);
if(r != extent_protocol::OK)
return r;
goto release;

str2dir(str, ents);
return r;

release:
lc->release(inum);
return r;
}

int
Expand All @@ -163,7 +174,7 @@ yfs_client::lookup(inum parent, std::string child, inum &inum)
return r;

r = extent_protocol::NOENT;
for(int i = 0; i < ents.size(); ++i){
for(unsigned int i = 0; i < ents.size(); ++i){
if(child == ents[i].name){
inum = ents[i].inum;
r = extent_protocol::OK;
Expand All @@ -181,13 +192,17 @@ yfs_client::create(inum parent, inum inum, std::string name)

printf("create name -> %s inum -> %016llx\n", name.c_str(), inum);

lc->acquire(parent);
std::string parent_str;
std::vector<dirent> ents;
r = readdir(parent, ents);

lc->acquire(parent);
r = ec->get(parent, parent_str);
if(r != extent_protocol::OK)
goto release;

for(int i = 0; i < ents.size(); ++i){
str2dir(parent_str, ents);

for(unsigned int i = 0; i < ents.size(); ++i){
if(name == ents[i].name){
r = yfs_client::EXIST;
goto release;
Expand Down Expand Up @@ -239,17 +254,20 @@ yfs_client::read(inum inum, unsigned long long offset, unsigned long long size,
int r = OK;
printf("yfs_client read inum-> %016llx offset -> %016llx size -> %016llx\n", inum, offset, size);

lc->acquire(inum);
std::string str;
r = ec->get(inum, str);
if(r != extent_protocol::OK)
return r;
goto release;

if(offset >= str.size())
buf = "";
else
buf = str.substr(offset, size);

return r;

release:
lc->release(inum);
return r;
}

int
Expand Down Expand Up @@ -284,13 +302,16 @@ yfs_client::unlink(inum parent, std::string name){

printf("unlink name -> %s parent -> %016llx\n", name.c_str(), parent);

std::string parent_str;
std::vector<dirent> ents;
std::vector<dirent>::iterator iter;

lc->acquire(parent);
r = readdir(parent, ents);
r = ec->get(parent, parent_str);
if(r != extent_protocol::OK)
goto release;

str2dir(parent_str, ents);

iter = ents.begin();
while(iter != ents.end()){
Expand Down
3 changes: 2 additions & 1 deletion yfs_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

class yfs_client {
extent_client *ec;
lock_client_cache *lc;
lock_client_cache *lc;
lock_release_cache *lrc;

public:

Expand Down

0 comments on commit 0fefc98

Please sign in to comment.