employ gnu99 style of GCC

This patch lets sheepdog employ gnu99 (C99 + gnu extensions) style of
GCC. The main benefit of gnu99 is allowing interleaved statements and
declarations. sheepdog source tree has many functions with lots of
local variable declarations on their head part. They are harmful from
the perspective of readability. Basically, variable declarations
should be delayed until they are actually required. With gnu99, we can
write interleaved statements and declarations, so this can improve the
readability of sheepdog.

The RB_ROOT constant is not friendly with gnu99, so this patch also
add a minor change to it. In addition, RB_ROOT cannot be used in
statements (not declarations). This patch also prepares a new macro
INIT_RB_ROOT() for such usecases.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
sheep: implement a correct mutex of a base directory

Current lockf() usage in lock_base_dir() is invalid bacause sheep
calls daemon(3) after the lockf(). daemon(3) forks internally and a
parent process exits immediately. In a case of sheep, daemon() must be
called after locking base dir so the lock owner, parent process, exits
and the lock will be released even though the child process is
running. This is the reason current lock_base_dir() doesn't work
well. And it causes writing logs to sheep.log from multiple
sheeps. This phenomenon is very confusing and should be avoided.

This patch implement custom function for daemonizing,
lock_and_daemon(). And mutex of base directory is done in
it. lock_and_daemon() does mutex between fork() and closing fd 0, 1,
and 2, so it can report the failure of mutexing base directory to
stderr.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
Revert "test: collect logs of sheeps after tests"

This reverts commit 26cdf0dee923ad16989ed22436692272af1a071a.

I iterate the check script many times everyday and can pass all the
test cases in most cases.  However, this patch generates a log.*
directory for each time even if there is no errors, and a large number
of log directories have been annoying me.  In addition, this collects
log files only of the last test, and does nothing against the other
ones.

Rather than improving this half baked patch, let's revert it for now
and reconsider the best way to collect logs.  IMHO collecting log
files should be done outside of the test scripts, though.

Cc: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: use $* instead of $@

The behavior of $@ in double quotations depends on the environment.
Actually, './check -valgrind' doesn't work on Fedora 16.  It is safe
to use $* instead of it.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: silence valgrind output

Valgrind prints only error messages with this patch.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: check root privilege

This prevents non-root users from running tests which need root
privilege.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: wait for sheep to be stopped

This patch ensures that the next sheep can start successfully by
checking that its port is not in LISTEN state.  We can use this, e.g.,
after 'collie cluster shutdown'.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: wait for sheep to be started

This adds _wait_for_sheep where it is necessary to avoid a false
alarm.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: refine start up of valgrind sheep

This removes a lock file before starting sheep because we wait for
sheep's start-up by checking the lock file existence.  This also
checks the sheep process to avoid infinite loop.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: declare variables as local

This narrows the scopes of variables and makes the scripts much easier
to read and safer.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: use different directory for each test case

With this patch, log files aren't removed by the succeeding tests, and
it makes it easier for us to look into the cause of errors.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: normalize store directory name

Currently, some test cases assume that we use /tmp/sheepdog for sheep
store.  This patch normalizes their outputs and allows us to run tests
on another directory.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests: wait format at most 60 seconds

036 and 038 cannot stop when 'collie cluster format' never succeeds.
This patch fixes it.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests/001: sleep before starting next sheep

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests/017: remove the current state after rolling back

This addresses the recent change in QEMU, "sheepdog: fix loadvm
operation".

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
tests/057: fix output based on the current store directory

The result of tests/057 changes based on the store directory.  We
moved the default store from from /tmp/sheepdog to /tmp/sheepdog/057,
so this patch also modifies the 057.out.

When we set the store directory manually, this patch skips 057.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
remove sd_*_req and sd_*_rsp

This patch removes all the sd_*_req and sd_*_rsp in internal_proto.h,
and adds the necessary fields to sd_req and sd_rsp.  It is not good to
include internal protocols in sheepdog_proto.h, but the change can
decrease many casting, improve type safety, and simplify codes a lot.

This also checks the size of sd_req and sd_rsp so that we don't break
them.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
don't see errno when read/2 returns zero

read/2 sets errno only when it returns -1.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
store: return SD_RES_NETWORK_ERROR when error is retryable

We have to avoid SD_RES_EIO as far as we can.  When the backend store
returns n retryable error, it should return SD_RES_NETWORK_ERROR for
gateway to try the request again.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
move vdi_is_snapshot() to sheepdog_proto.h

This allows us to use the macro outside of sheep/vdi.c.

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>