Skip to content

Commit

Permalink
Merge branch 'lab4' into lab5
Browse files Browse the repository at this point in the history
  • Loading branch information
mlnotes committed Dec 18, 2012
2 parents 6535bc5 + da10282 commit 3a78f05
Show file tree
Hide file tree
Showing 16 changed files with 790 additions and 50 deletions.
8 changes: 4 additions & 4 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
CXXFLAGS = -g -MMD -Wall -I. -I$(RPC) -DLAB=$(LAB) -DSOL=$(SOL) -D_FILE_OFFSET_BITS=64 -DZDEBUG
FUSEFLAGS= -D_FILE_OFFSET_BITS=64 -DFUSE_USE_VERSION=25 -I/usr/local/include/fuse -I/usr/include/fuse
ifeq ($(shell uname -s),Darwin)
MACFLAGS= -D__FreeBSD__=10
Expand Down Expand Up @@ -116,7 +116,7 @@ rsm_tester: $(patsubst %.cc,%.o,$(rsm_tester)) rpc/librpc.a
fuse.o: fuse.cc
$(CXX) -c $(CXXFLAGS) $(FUSEFLAGS) $(MACFLAGS) $<

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

-include *.d
Expand All @@ -128,10 +128,10 @@ clean:
rm $(clean_files) -rf

handin_ignore=$(clean_files) core* *log
handin_file=$(shell whoami)-lab$(LAB).tgz
handin_file=$(shell whoami)-lab$(LAB)-112037xxxx.tgz
labdir=$(shell basename $(PWD))
handin:
@if test -f stop.sh; then ./stop.sh > /dev/null 2>&1 | echo ""; fi
@bash -c "cd ../; tar -X <(tr ' ' '\n' < <(echo '$(handin_ignore)')) -czvf $(handin_file) $(labdir); mv $(handin_file) $(labdir); cd $(labdir)"
@echo Please email $(handin_file) to [email protected]
@echo Please change the name of $(handin_file) and email it to [email protected]
@echo Thanks!
54 changes: 44 additions & 10 deletions extent_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,45 @@
#include <sys/stat.h>
#include <fcntl.h>

extent_server::extent_server() {}
extent_server::extent_server()
{
VERIFY(pthread_mutex_init(&mutex, NULL) == 0);
int r;
VERIFY(put(0x00000001, "", r) == extent_protocol::OK);
}

extent_server::~extent_server()
{
VERIFY(pthread_mutex_destroy(&mutex) == 0);
}

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 lock(&mutex);
fs[id] = buf;
extent_protocol::attr a = attrs[id];
a.size = buf.size();
a.ctime = a.mtime = time(0);
attrs[id] = a;
return extent_protocol::OK;
}

int extent_server::get(extent_protocol::extentid_t id, std::string &buf)
{
// You fill this in for Lab 2.
return extent_protocol::IOERR;
#ifdef ZDEBUG
printf("get %016llx\n", id);
#endif

ScopedLock lock(&mutex);
if(fs.find(id) == fs.end()){
return extent_protocol::NOENT;
}else{
buf = fs[id];
attrs[id].atime = time(0);
}
return extent_protocol::OK;
}

int extent_server::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a)
Expand All @@ -29,16 +55,24 @@ 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.atime = 0;
a.mtime = 0;
a.ctime = 0;
return extent_protocol::OK;
ScopedLock lock(&mutex);
if(attrs.find(id) == attrs.end()){
return extent_protocol::NOENT;
}else{
a = attrs[id];
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;
ScopedLock lock(&mutex);
if(fs.find(id) == fs.end()){
return extent_protocol::NOENT;
}else{
fs.erase(id);
attrs.erase(id);
return extent_protocol::OK;
}
}

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

class extent_server {

public:
extent_server();
~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, std::string> fs;
std::map<extent_protocol::extentid_t, extent_protocol::attr> attrs;
};

#endif
Expand Down
129 changes: 122 additions & 7 deletions fuse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,28 @@ fuseserver_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
printf(" fuseserver_setattr set size to %zu\n", attr->st_size);
struct stat st;
// You fill this in for Lab 2
#if 0
#if 1
// Change the above line to "#if 1", and your code goes here
// Note: fill st using getattr before fuse_reply_attr
yfs_client::inum inum = ino;
yfs_client::status ret = yfs->setsize(inum, attr->st_size);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}

ret = getattr(inum, st);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}
printf("fuseserver_setattr success size -> %016llx\n", attr->st_size);
fuse_reply_attr(req, &st, 0);
#else
fuse_reply_err(req, ENOSYS);
#endif
} else {
printf("fuseserver_setattr but not set size!\n");
fuse_reply_err(req, ENOSYS);
}
}
Expand All @@ -155,9 +169,15 @@ fuseserver_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
// You fill this in for Lab 2
#if 0
#if 1
std::string buf;
// Change the above "#if 0" to "#if 1", and your code goes here
yfs_client::inum inum = ino;
yfs_client::status ret = yfs->read(inum, off, size, buf);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}
fuse_reply_buf(req, buf.data(), buf.size());
#else
fuse_reply_err(req, ENOSYS);
Expand Down Expand Up @@ -185,8 +205,16 @@ fuseserver_write(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi)
{
// You fill this in for Lab 2
#if 0
#if 1
// Change the above line to "#if 1", and your code goes here
yfs_client::inum inum = ino;
std::string str(buf, size);

yfs_client::status ret = yfs->write(inum, off, str);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}
fuse_reply_write(req, size);
#else
fuse_reply_err(req, ENOSYS);
Expand Down Expand Up @@ -220,16 +248,39 @@ fuseserver_createhelper(fuse_ino_t parent, const char *name,
e->entry_timeout = 0.0;
e->generation = 0;
// You fill this in for Lab 2
return yfs_client::NOENT;
// pick a inum
// yfs_client::inum inum = time(0) | 0x80000000;
// srand((unsigned)time(0));
yfs_client::inum inum = rand() | 0x80000000;
yfs_client::inum iparent = parent;

#ifdef ZDEBUG
printf("createhelper name -> %s inum -> %016llx\n", name, inum);
#endif

yfs_client::status ret = yfs->create(iparent, inum, name);
if(ret != yfs_client::OK)
return ret;

e->ino = inum;
ret = getattr(inum, e->attr);
return ret;
}

void
fuseserver_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_file_info *fi)
{
#ifdef ZDEBUG
printf("fuseserver_create name -> %s\n", name);
#endif

struct fuse_entry_param e;
yfs_client::status ret;
if( (ret = fuseserver_createhelper( parent, name, mode, &e )) == yfs_client::OK ) {
#ifdef ZDEBUG
printf("fuseserver_create success name -> %s\n", name);
#endif
fuse_reply_create(req, &e, fi);
} else {
if (ret == yfs_client::EXIST) {
Expand All @@ -242,6 +293,10 @@ fuseserver_create(fuse_req_t req, fuse_ino_t parent, const char *name,

void fuseserver_mknod( fuse_req_t req, fuse_ino_t parent,
const char *name, mode_t mode, dev_t rdev ) {
#ifdef ZDEBUG
printf("fuseserver_create name -> %s\n", name);
#endif

struct fuse_entry_param e;
yfs_client::status ret;
if( (ret = fuseserver_createhelper( parent, name, mode, &e )) == yfs_client::OK ) {
Expand Down Expand Up @@ -271,8 +326,27 @@ fuseserver_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
bool found = false;

// You fill this in for Lab 2
if (found)
#ifdef ZDEBUG
printf("fuseserver_lookup name -> %s\n", name);
#endif

yfs_client::inum iparent = parent;
yfs_client::inum ichild;

found = (yfs->lookup(iparent, name, ichild) == yfs_client::OK);
if (found){
e.ino = ichild;

struct stat st;
yfs_client::status ret;
ret = getattr(ichild, st);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}
e.attr = st;
fuse_reply_entry(req, &e);
}
else
fuse_reply_err(req, ENOENT);
}
Expand Down Expand Up @@ -332,7 +406,16 @@ fuseserver_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,


// You fill this in for Lab 2
std::vector<yfs_client::dirent> ents;
yfs_client::status ret = yfs->readdir(inum, ents);
if(ret != yfs_client::OK){
fuse_reply_err(req, ENOENT);
return;
}

for(unsigned int i = 0; i < ents.size(); ++i){
dirbuf_add(&b, ents[i].name.c_str(), ents[i].inum);
}

reply_buf_limited(req, b.p, b.size, off, size);
free(b.p);
Expand Down Expand Up @@ -369,7 +452,27 @@ fuseserver_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
(void) e;

// You fill this in for Lab 3
#if 0

yfs_client::inum inum = rand() & ~0x80000000;
yfs_client::inum iparent = parent;

#ifdef ZDEBUG
printf("createhelper name -> %s inum -> %016llx\n", name, inum);
#endif

yfs_client::status ret = yfs->create(iparent, inum, name);
if(ret == yfs_client::OK){
e.ino = inum;
getattr(inum, e.attr);
} else {
if (ret == yfs_client::EXIST) {
fuse_reply_err(req, EEXIST);
}else{
fuse_reply_err(req, ENOENT);
}
}

#if 1
fuse_reply_entry(req, &e);
#else
fuse_reply_err(req, ENOSYS);
Expand All @@ -390,7 +493,17 @@ fuseserver_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
// You fill this in for Lab 3
// Success: fuse_reply_err(req, 0);
// Not found: fuse_reply_err(req, ENOENT);
fuse_reply_err(req, ENOSYS);

yfs_client::inum iparent = parent;

if(yfs->unlink(iparent, name) == yfs_client::OK){
fuse_reply_err(req, 0);
}else{
fuse_reply_err(req, ENOENT);
}


// fuse_reply_err(req, ENOSYS);
}

void
Expand All @@ -417,6 +530,8 @@ main(int argc, char *argv[])
int err = -1;
int fd;

srand((unsigned)time(0));

setvbuf(stdout, NULL, _IONBF, 0);

if(argc != 4){
Expand Down
8 changes: 8 additions & 0 deletions lock_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ lock_client::stat(lock_protocol::lockid_t lid)
lock_protocol::status
lock_client::acquire(lock_protocol::lockid_t lid)
{
int r;
lock_protocol::status ret = cl->call(lock_protocol::acquire, cl->id(), lid, r);
VERIFY (ret == lock_protocol::OK);
return r;
}

lock_protocol::status
lock_client::release(lock_protocol::lockid_t lid)
{
int r;
lock_protocol::status ret = cl->call(lock_protocol::release, cl->id(), lid, r);
VERIFY (ret == lock_protocol::OK);
return r;
}

Loading

0 comments on commit 3a78f05

Please sign in to comment.