git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4] compat: Fix read() of 2GB and more on Mac OS X

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Aug 19, 2013, 16:04 UTC
Message-ID
<CA+55aFzQhJqE4QDwJDKtkTtJpMNbz3_Aw5_Q3yTk5DnhLJyjCQ@mail.gmail.com>
In-Reply-To
<1376926879-30846-1-git-send-email-prohaska@zib.de>
On Mon, Aug 19, 2013 at 8:41 AM, Steffen Prohaska <prohaska@zib.de> wrote:
>
> The reason was that read() immediately returns with EINVAL if nbyte >=
> 2GB.  According to POSIX [1], if the value of nbyte passed to read() is
> greater than SSIZE_MAX, the result is implementation-defined.

Yeah, the OS X filesystem layer is an incredible piece of shit. Not only doesn't it follow POSIX, it fails *badly*. Because OS X kernel engineers apparently have the mental capacity of a retarded rodent on crack.

Linux also refuses to actually read more than a maximum value in one go (because quite frankly, doing more than 2GB at a time is just not reasonable, especially in unkillable disk wait), but at least Linux gives you the partial read, so that the usual "read until you're happy" works (which you have to do anyway with sockets, pipes, NFS intr mounts, etc etc). Returning EINVAL is a sign of a diseased mind.

I hate your patch for other reasons, though:
> The problem for read() is addressed in a similar way by introducing
> a wrapper function in compat that always reads less than 2GB.

Why do you do that? We already _have_ wrapper functions for read(), namely xread(). Exactly because you basically have to, in order to handle signals on interruptible filesystems (which aren't POSIX either, but at least sanely so) or from other random sources. And to handle the "you can't do reads that big" issue.

So why isn't the patch much more straightforward? Like the attached totally untested one that just limits the read/write size to 8MB (which is totally arbitrary, but small enough to not have any latency issues even on slow disks, and big enough that any reasonable IO subsystem will still get good throughput).

And by "totally untested" I mean that it actually passes the git test suite, but since I didn't apply your patch nor do I have OS X anywhere, I can't actually test that it fixes *your* problem. But it should.

                   Linus
 wrapper.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/wrapper.c b/wrapper.c
index 6a015de5f056..e996f3dae467 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -131,6 +131,13 @@ void *xcalloc(size_t nmemb, size_t size)
 }
 
 /*
+ * Doing IO in huge chunks only results in pain. OS X is buggy,
+ * and even in the absense of bugs it can result in bad latencies
+ * when you decide to kill the process.
+ */
+#define MAX_IO_SIZE (8*1024*1024)
+
+/*
  * xread() is the same a read(), but it automatically restarts read()
  * operations with a recoverable error (EAGAIN and EINTR). xread()
  * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
@@ -139,7 +146,8 @@ ssize_t xread(int fd, void *buf, size_t len)
 {
 	ssize_t nr;
 	while (1) {
-		nr = read(fd, buf, len);
+		nr = len < MAX_IO_SIZE ? len : MAX_IO_SIZE;
+		nr = read(fd, buf, nr);
 		if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 			continue;
 		return nr;
@@ -155,7 +163,8 @@ ssize_t xwrite(int fd, const void *buf, size_t len)
 {
 	ssize_t nr;
 	while (1) {
-		nr = write(fd, buf, len);
+		nr = len < MAX_IO_SIZE ? len : MAX_IO_SIZE;
+		nr = write(fd, buf, nr);
 		if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 			continue;
 		return nr;
Previous: Steffen ProhaskaNext: Steffen Prohaska
Message 19 of 37 in “xread(): Fix read error when filtering >= 2GB on Mac OS X”
  1. xread(): Fix read error when filtering >= 2GB on Mac OS XSteffen Prohaska, Aug 17, 2013
  2. John KeepingAug 17, 2013
  3. Torsten BögershausenAug 17, 2013
  4. Johannes SixtAug 17, 2013
  5. Jonathan NiederAug 17, 2013
  6. Kyle J. McKayAug 17, 2013
  7. Jonathan NiederAug 17, 2013
  8. compat: Fix read() of 2GB and more on Mac OS XSteffen Prohaska, Aug 19, 2013
  9. John KeepingAug 19, 2013
  10. Steffen ProhaskaAug 19, 2013
  11. Johannes SixtAug 19, 2013
  12. Stefan BellerAug 19, 2013
  13. Johannes SixtAug 19, 2013
  14. Steffen ProhaskaAug 19, 2013
  15. compat: Fix read() of 2GB and more on Mac OS XSteffen Prohaska, Aug 19, 2013
  16. Eric SunshineAug 19, 2013
  17. Junio C HamanoAug 19, 2013
  18. compat: Fix read() of 2GB and more on Mac OS XSteffen Prohaska, Aug 19, 2013
  19. Linus TorvaldsAug 19, 2013
  20. Steffen ProhaskaAug 19, 2013
  21. Junio C HamanoAug 19, 2013
  22. Junio C HamanoAug 19, 2013
  23. Linus TorvaldsAug 19, 2013
  24. Kyle J. McKayAug 19, 2013
  25. Linus TorvaldsAug 19, 2013
  26. Junio C HamanoAug 27, 2013
  27. 0/2 Fix IO of >=2GB on Mac OS X by limiting IO chunksSteffen Prohaska, Aug 20, 2013
  28. 1/2 xread, xwrite: Limit size of IO, fixing IO of 2GB and more on Mac OS XSteffen Prohaska, Aug 20, 2013
  29. Junio C HamanoAug 20, 2013
  30. Torsten BögershausenAug 21, 2013
  31. 2/2 Revert "compate/clipped-write.c: large write(2) fails on Mac OS X/XNU"Steffen Prohaska, Aug 20, 2013
  32. 0/2 Fix IO >= 2GB on Mac, fixed typoSteffen Prohaska, Aug 21, 2013
  33. 1/2 xread, xwrite: Limit size of IO, fixing IO of 2GB and more on Mac OS XSteffen Prohaska, Aug 21, 2013
  34. 2/2 Revert "compate/clipped-write.c: large write(2) fails on Mac OS X/XNU"Steffen Prohaska, Aug 21, 2013
  35. Junio C HamanoAug 21, 2013
  36. Johannes SixtAug 19, 2013
  37. Torsten BögershausenAug 19, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.