All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Fam Zheng <famz@redhat.com>
Cc: qemu-devel@nongnu.org, eblake@redhat.com,
	Max Reitz <mreitz@redhat.com>,
	qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v14 19/20] file-posix: Add image locking in perm operations
Date: Mon, 24 Apr 2017 15:39:33 +0200	[thread overview]
Message-ID: <20170424133933.GF4739@noname.redhat.com> (raw)
In-Reply-To: <20170421035606.448-20-famz@redhat.com>

Am 21.04.2017 um 05:56 hat Fam Zheng geschrieben:
> virtlockd in libvirt locks the first byte, so we start looking at the
> file bytes from 0x10.
> 
> The complication is in the transactional interface.  To make the reopen
> logic managable, and allow better reuse, the code is internally
> organized with a table from old mode to the new one.
> 
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/file-posix.c | 739 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 736 insertions(+), 3 deletions(-)
> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index 2114720..c307493 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -129,8 +129,54 @@ do { \
>  
>  #define MAX_BLOCKSIZE	4096
>  
> +/* Posix file locking bytes. Libvirt takes byte 0, we start from byte 0x10,
> + * leaving a few more bytes for its future use. */
> +#define RAW_LOCK_BYTE_MIN             0x10
> +#define RAW_LOCK_BYTE_NO_OTHER_WRITER 0x10
> +#define RAW_LOCK_BYTE_WRITE           0x11
> +#ifdef F_OFD_SETLK
> +#define RAW_LOCK_SUPPORTED 1
> +#else
> +#define RAW_LOCK_SUPPORTED 0
> +#endif
> +
> +/*
> + ** reader that can tolerate writers: Don't do anything
> + *
> + ** reader that can't tolerate writers: Take shared lock on byte 0x10. Test
> + *  byte 0x11 is unlocked.
> + *
> + ** shared writer: Take shared lock on byte 0x11. Test byte 0x10 is unlocked.
> + *
> + ** exclusive writer: Take exclusive locks on both bytes.
> + */

There are few things that made this patch basically degenerate into a
ton of special cases, the most important of them being that we thought
that we'd need acquire exclusive locks to avoid races and that read-only
file descriptors can't take an exclusive lock and therefore can't test
whether anyone else is holding a shared lock on the file descriptor.

After looking a bit more into this and discussing it on IRC with Fam who
found the last missing puzzle piece, it turned out that while it's true
that F_OFD_SETLK for an exclusive lock doesn't work for read-only FDs,
in fact F_OFD_GETLK (which tests if a lock could be set) does work, so
we can use it to check whether anyone else is holding a shared lock.

We also never need to actually acquire any exclusive locks, just
acquiring shared locks everywhere and testing whether we could set
exclusive locks is enough for our needs. Both of these operations work
fine with read-only FDs.

With these observations, it should be easy to even map _all_ 64 bits of
perm and ~shared to file locks. I think the algorithm looks roughly like
this:

1. Acquire shared @perm locks for (old_perm | new_perm)
2. Acquire shared @~shared locks for (~old_shared | ~new_shared)
3. Test that nobody else holds @perm locks for ~new_shared
4. Test that nobody else holds @~shared locks for new_perm
5. Drop @perm locks for (old_perm & ~new_perm)
6. Drop @~shared locks for (~old_shared & new_shared)

Having one universal algorithm for all state transitions allows us to
greatly simplify the code in this patch.

Kevin

  reply	other threads:[~2017-04-24 13:39 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-21  3:55 [Qemu-devel] [PATCH v14 00/20] block: Image locking series Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 01/20] block: Add, parse and store "force-shared-write" option Fam Zheng
2017-04-21  8:34   ` Kevin Wolf
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 02/20] qapi: Add 'force-shared-write' to blockdev-add arguments Fam Zheng
2017-04-21  8:35   ` Kevin Wolf
2017-04-21  8:42     ` Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 03/20] block: Respect "force-shared-write" in perm propagating Fam Zheng
2017-04-21  8:38   ` Kevin Wolf
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 04/20] qemu-img: Add --share-rw option to subcommands Fam Zheng
2017-04-21 13:25   ` Kevin Wolf
2017-04-21 15:35     ` Eric Blake
2017-04-24  6:10     ` Fam Zheng
2017-04-24 10:13       ` Kevin Wolf
2017-04-24 11:28         ` Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 05/20] qemu-img: Update documentation for --share-rw Fam Zheng
2017-04-21 15:37   ` Eric Blake
2017-04-24  5:44     ` Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 06/20] qemu-io: Add --share-rw option Fam Zheng
2017-04-21 13:45   ` Kevin Wolf
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 07/20] iotests: 030: Prepare for image locking Fam Zheng
2017-04-21 13:51   ` Kevin Wolf
2017-04-24  6:15     ` Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 08/20] iotests: 046: " Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 09/20] iotests: 055: Don't attach the target image already for drive-backup Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 10/20] iotests: 085: Avoid image locking conflict Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 11/20] iotests: 087: Don't attach test image twice Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 12/20] iotests: 091: Quit QEMU before checking image Fam Zheng
2017-04-21  3:55 ` [Qemu-devel] [PATCH v14 13/20] iotests: 172: Use separate images for multiple devices Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 14/20] tests: Use null-co:// instead of /dev/null as the dummy image Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 15/20] file-posix: Add 'locking' option Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 16/20] tests: Disable image lock in test-replication Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 17/20] block: Reuse bs as backing hd for drive-backup sync=none Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 18/20] osdep: Add qemu_lock_fd and qemu_unlock_fd Fam Zheng
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 19/20] file-posix: Add image locking in perm operations Fam Zheng
2017-04-24 13:39   ` Kevin Wolf [this message]
2017-04-21  3:56 ` [Qemu-devel] [PATCH v14 20/20] qemu-iotests: Add test case 153 for image locking Fam Zheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170424133933.GF4739@noname.redhat.com \
    --to=kwolf@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.