public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Timo Sirainen <tss@iki.fi>
To: Jamie Lokier <jamie@shareable.org>
Cc: root@chaos.analogic.com,
	Martin Konold <martin.konold@erfrakon.de>,
	linux-kernel@vger.kernel.org
Subject: Re: Lockless file reading
Date: Thu, 28 Aug 2003 03:52:22 +0300	[thread overview]
Message-ID: <1062031942.1454.147.camel@hurina> (raw)
In-Reply-To: <20030827233903.GB3759@mail.jlokier.co.uk>

On Thu, 2003-08-28 at 02:39, Jamie Lokier wrote:
> Timo Sirainen wrote:
> > Maybe it would be possible to use some kind of error detection 
> > checksums which would guarantee that the data either is valid or isn't, 
> > regardless of the order in which it is written. I don't really know how 
> > that could be done though.
> 
> You can use a strong checksum like MD5, if that is really faster than
> locking.  (Over NFS it probably is faster than fcntl()-locking, for
> small data blocks).

While MD5 is probably good enough, it doesn't _guarantee_ the
consistency. I just thought of a simple algorithm that I think would.
I'll go and use that unless someone proves it wrong :)

Except of course if there's 256 writes and the last one is non-ordered
and it all happens while a read() is executing.. Less than unlikely I'd
say.

I don't think there's any other potential problems with read() than that
it may not have all data up to date? Such as it would never temporarily
show the data as zero?

static int verify(const unsigned char *buf, size_t size)
{
	const unsigned char *checksum = buf + size;
	unsigned char xor;
	size_t i;

	xor = buf[0] ^ checksum[0];
	for (i = 1; i < size; i++) {
		if ((buf[i] ^ xor) != checksum[i])
			return 0;
	}
	return 1;
}

void write_data(const void *data, size_t size)
{
	unsigned char *checksum = buf + size;
	unsigned char xor;
	size_t i;

	memcpy(buf, data, size);

	checksum[0]++;
	xor = buf[0] ^ checksum[0];

	for (i = 1; i < size; i++)
		checksum[i] = buf[i] ^ xor;
}

void read_data(void *data, size_t size)
{
	unsigned char copy[size*2];

	do {
		memcpy(copy, buf, size*2);
	} while (!verify(copy, size));
	memcpy(data, copy, size);
}



  reply	other threads:[~2003-08-28  0:52 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-27 12:37 Lockless file reading Timo Sirainen
2003-08-27 12:42 ` Martin Konold
2003-08-27 12:52   ` Timo Sirainen
2003-08-27 13:40     ` Richard B. Johnson
2003-08-27 14:56       ` Timo Sirainen
2003-08-27 23:39         ` Jamie Lokier
2003-08-28  0:52           ` Timo Sirainen [this message]
2003-08-27 18:42             ` Nagendra Singh Tomar
2003-08-28  8:40               ` Timo Sirainen
2003-08-27 21:15                 ` Nagendra Singh Tomar
2003-08-28  9:35                   ` Timo Sirainen
2003-08-27 21:52                     ` Nagendra Singh Tomar
2003-08-28 13:26                       ` Matthias Andree
2003-08-28  9:17                 ` David Schwartz
2003-08-28  8:42               ` Valdis.Kletnieks
2003-08-28 20:13                 ` David B. Stevens
2003-08-28  1:50             ` Jamie Lokier
2003-08-28  3:17               ` Timo Sirainen
2003-08-28  6:01                 ` Valdis.Kletnieks
2003-08-28  6:13                 ` Jamie Lokier
2003-08-28  8:57                   ` Timo Sirainen
2003-08-28  9:56                     ` David Schwartz
2003-08-28 10:26                       ` Timo Sirainen
2003-08-27 22:58                         ` Nagendra Singh Tomar
2003-08-28 12:18                           ` Jamie Lokier
2003-08-28  0:39                             ` Nagendra Singh Tomar
2003-08-28 13:00                               ` Jamie Lokier
2003-08-28  1:06                                 ` Nagendra Singh Tomar
2003-08-28 21:49                             ` Bernd Eckenfels
2003-08-28 12:01                         ` Jamie Lokier
2003-08-28 13:28                           ` Timo Sirainen
2003-08-28 20:24                         ` David Schwartz
2003-08-28 12:44                       ` Ragnar Hojland Espinosa
2003-08-28 13:03                         ` Jamie Lokier
2003-08-28 17:26                           ` root
2003-08-28 17:35                             ` Jamie Lokier
2003-08-28 18:10                               ` root
2003-08-28 21:59                             ` Bernd Eckenfels
2003-08-28 23:02                               ` Jamie Lokier
2003-08-28 23:44                               ` Lockless file readingu root
2003-08-29 10:00                                 ` jlnance
2003-08-29 11:55                                   ` David Schwartz
2003-08-29 15:43                                   ` William Lee Irwin III
2003-08-28 20:37                         ` Lockless file reading David Schwartz
2003-08-28 22:11                           ` Bernd Eckenfels
2003-08-28 23:00                             ` Jamie Lokier
2003-08-29  0:47                             ` David Schwartz
2003-08-28  9:13                 ` Martin Konold
2003-08-28  9:27                   ` Timo Sirainen
2003-08-28  9:48                     ` Martin Konold
2003-08-28  0:03       ` Jamie Lokier
2003-08-28 12:08         ` Richard B. Johnson
2003-08-28 12:39           ` Jamie Lokier
2003-08-28 10:08   ` Matthias Andree
2003-08-28 10:54 ` Robin Rosenberg
2003-08-28 12:42   ` Jamie Lokier

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=1062031942.1454.147.camel@hurina \
    --to=tss@iki.fi \
    --cc=jamie@shareable.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.konold@erfrakon.de \
    --cc=root@chaos.analogic.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox