At Sat, 19 May 2012 23:11:22 +0800, Liu Yuan wrote: > > From: Liu Yuan <tailai.ly at taobao.com> > > refactor strcut siocb, this makes the code more clean. > > - let sd_store->read/write() use open() internally. > - remove sd_store->open()/close() > - add sd_store->exist() > > But now we don't actually have concurrent requests to the same object, because > upper layer have exclude concurrent access by check_request(). > > We'll remove this constaint by later patch set. > > Signed-off-by: Liu Yuan <tailai.ly at taobao.com> > --- > include/util.h | 5 +- > sheep/farm/farm.c | 190 ++++++++++++++++++++++++++++---------------------- > sheep/object_cache.c | 5 -- > sheep/ops.c | 37 ++-------- > sheep/recovery.c | 16 +---- > sheep/sheep_priv.h | 6 +- > 6 files changed, 121 insertions(+), 138 deletions(-) > > diff --git a/include/util.h b/include/util.h > index 1c32954..10b30d7 100644 > --- a/include/util.h > +++ b/include/util.h > @@ -8,6 +8,7 @@ > > #include "bitops.h" > #include "list.h" > +#include "logger.h" > > #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) > #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) > @@ -61,8 +62,10 @@ static inline void *zalloc(size_t size) > > static inline int xlockf(int fd, int cmd, off_t offset, off_t len) > { > - if (lseek(fd, offset, SEEK_SET) < 0) > + if (lseek(fd, offset, SEEK_SET) < 0) { > + eprintf("%m\n"); > return -1; > + } > > return lockf(fd, cmd, len); > } > diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c > index 7912cb7..b5bfa19 100644 > --- a/sheep/farm/farm.c > +++ b/sheep/farm/farm.c > @@ -13,9 +13,12 @@ > > #include <dirent.h> > #include <pthread.h> > +#include <linux/limits.h> > > #include "farm.h" > #include "sheep_priv.h" > +#include "sheepdog_proto.h" > +#include "sheep.h" > > char farm_obj_dir[PATH_MAX]; > char farm_dir[PATH_MAX]; > @@ -67,15 +70,77 @@ err: > return ret; > } > > -static int farm_write(uint64_t oid, struct siocb *iocb) > +static int farm_exist(uint64_t oid) > { > - ssize_t size = xpwrite(iocb->fd, iocb->buf, iocb->length, iocb->offset); > + char path[PATH_MAX]; > + int flags = def_open_flags, fd; > > - if (size != iocb->length) > - return SD_RES_EIO; > + sprintf(path, "%s%016"PRIx64, obj_path, oid); > + fd = open(path, flags); > + if (fd < 0) { > + if (errno != ENOENT) > + eprintf("%m\n"); > + return 0; > + } > + > + close(fd); > + return 1; > +} Is it better to use stat() instead of open() and close()? > + > +static int err_to_sderr(uint64_t oid, int err) > +{ > + int ret; > + if (err == ENOENT) { > + struct stat s; > + > + if (stat(obj_path, &s) < 0) { > + eprintf("corrupted\n"); > + ret = SD_RES_EIO; > + } else { > + dprintf("object %016" PRIx64 " not found locally\n", oid); > + ret = SD_RES_NO_OBJ; > + } > + } else { > + eprintf("%m\n"); > + ret = SD_RES_UNKNOWN; > + } > + return ret; > +} > + > +static int farm_write(uint64_t oid, struct siocb *iocb, int create) > +{ > + int flags = def_open_flags, fd, ret; ret must be initialized with SD_RES_SUCCESS. Other parts of this series look good to me. Thanks, Kazutaka |