From owner-freebsd-questions@freebsd.org Tue Jun 15 19:16:14 2021 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 93DB6650EE4 for ; Tue, 15 Jun 2021 19:16:14 +0000 (UTC) (envelope-from michaelsprivate@gmail.com) Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4G4J2P4JgTz4tJR for ; Tue, 15 Jun 2021 19:16:13 +0000 (UTC) (envelope-from michaelsprivate@gmail.com) Received: by mail-il1-x135.google.com with SMTP id x12so115287ill.4 for ; Tue, 15 Jun 2021 12:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Q44Icndb2kP9YpYZ9Tt4eCkEvflDTf2AAXslKD785xo=; b=jJAy8c0PSzWvLa4ZlsiE3WM2ZHajceVmzhubwzlCh7sZ+WohwZiX7EVQpgQcuyRyb3 iNfo0Kgf1eqrVj3ohy6gt6BVSepkX7KvPnFNDAarIYniz0Ea9hU9cMc77mIH2FOqAXQ5 sZ3WClexuQlaD29T5Pf+kxQXllRFjNJJsXZHBwtkG4p5UFyLN3re+ATbo8+ouDmclcUa aINZVyGyIBLmh65WSnmzKfxQpuYOUe2M+xT18W5u2Rp2Cn1sEInTXXYrCwl12r6cjWnC as0xBEr287IbCW7IMhJPtD3FNw3cMu4C61OpXbU+6KCjaPM2IGzVLZS/4OpO6G3oxj/0 Qx5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Q44Icndb2kP9YpYZ9Tt4eCkEvflDTf2AAXslKD785xo=; b=fp9N8D8wRX4OSTr/frt6M8MigUmB/pF+AwwZlcEtcUmgDzZk6JfWY2XA5UA/91/STi bMTdbGq7Uqb24KZ/Kg3X3h1IQfkHFpg03/vMZNGsOcXpwG6uwSrC8+jdtCQalJKUFoEl YPMmD75x8fpHHwDHxWHFZzkUwzPAPXIxosUFG4mfSyhBEvJVz/Igv5Q7DNJL6m+1SVzx YR4hRQn4fvHprhkHFRRMYjUmfwapzbh94MxncVFuaR5Ky+ea/1kWb9UIifuxwlr/GLm8 UeSlWBHojpXTWNkEXqx0Jx835PfeV3EWUN1Sjda8AzVUnxlmvcibGhYSzdv0wR/dVHLT YgtQ== X-Gm-Message-State: AOAM531igrzrAQskBACCZ+4WT0EqF75El0ojp85yzetMISxgwL0uXek7 QxfM2ji+okloZfGPvH//hCVIFPVK3A6YzsXkt8E= X-Google-Smtp-Source: ABdhPJyPTOWZbAwnzn37sfGJpim5dtrjL9gjaUm66jFhmFKdVNSVhdsYlJ5RsJFtuGQs+EeldzVRpJw2bUVtpqTfzfc= X-Received: by 2002:a92:d451:: with SMTP id r17mr764761ilm.109.1623784572338; Tue, 15 Jun 2021 12:16:12 -0700 (PDT) MIME-Version: 1.0 References: <22440.1623740785@segfault.tristatelogic.com> <44e15917-0c92-08f2-462e-a1b3705f9afb@panix.com> In-Reply-To: <44e15917-0c92-08f2-462e-a1b3705f9afb@panix.com> From: Michael Schuster Date: Tue, 15 Jun 2021 21:16:01 +0200 Message-ID: Subject: Re: Is a successful call to write(2) atomic? To: Kurt Hackenberg Cc: freeBSD Mailing List X-Rspamd-Queue-Id: 4G4J2P4JgTz4tJR X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=jJAy8c0P; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of michaelsprivate@gmail.com designates 2607:f8b0:4864:20::135 as permitted sender) smtp.mailfrom=michaelsprivate@gmail.com X-Spamd-Result: default: False [-1.83 / 15.00]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; FROM_EQ_ENVFROM(0.00)[]; SUBJECT_ENDS_QUESTION(1.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[2607:f8b0:4864:20::135:from]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FROM_HAS_DN(0.00)[]; NEURAL_SPAM_SHORT(0.17)[0.173]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-questions@freebsd.org]; SPAMHAUS_ZRD(0.00)[2607:f8b0:4864:20::135:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::135:from]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[freebsd-questions] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.34 X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Jun 2021 19:16:14 -0000 On Tue, Jun 15, 2021 at 8:50 PM Kurt Hackenberg wrote: > On 2021/06/15 03:06, Ronald F. Guilmette wrote: > > > This is deeply distrubing. I never knew about this. I am furiously > reading > > the FreeBSD & Linux man pages for open(2). (It appears that both support > > that flags, O_DIRECT and O_SYNC, aqnd Linux also adds the O_DSYNC flag. > > No, write(2) is not guaranteed atomic, and that's not obvious. Probably > a lot of people have learned that the hard way. > > All I know about those flags is what I just read in the man page, but I > think they don't guarantee atomic writes either -- at least, they're not > intended to. > > The sync stuff means the system call won't return until the data has > been written to disk (as opposed to queuing the write to be executed > sometime later). That doesn't say anything about interleaving with other > processes. > > Direct -- non-cached -- sounds like sort of the same thing, but at a > different level, and it isn't even guaranteed to do that. The man page > doesn't say much. > I think what you're describing - synchronous or not - is orthogonal to what Ronald is asking. Let's take a step back: an atomic write() either writes everything or nothing - and that's all. There's nothing in that claim that says "everything must be in a contiguous block", nor, that all the data must be written in a single "operation" by the underlying system. So after consideration I don't think the observed behaviour is violating the claim that write() is atomic - I welcome correction, of course :-) regards Michael Either of those might slow things down a lot, without necessarily > solving your problem. Either might make the problem less likely to > occur, and so harder to debug. I suggest that you don't use those flags > for this purpose. > > This is an old, general problem: concurrent access to a shared resource. > There are two common solutions. Paul suggested one of them: serialize > access through a single process. The other is to serialize it through > some kind of lock, that can only be held by one process at a time. Each > process acquires the lock, uses the shared resource briefly, and > immediately releases the lock. Semaphores are that kind of lock, > invented for that purpose. > > Unix file systems have file locking, which does that for all or part of > a file; see fcntl(2). Note that Unix file locking is advisory -- > voluntary -- not compulsory. It only works if all the processes agree to > use it. Also, in the past, it has not always worked for files accessed > across the network, through NFS. I don't know whether it works through > modern NFS. It's best to use it only for local files. > > Both approaches work fine, if they're done correctly; they're both a > little complicated to implement. Getting it right requires a clear > understanding of the problem and the solution. Sounds like you have the > idea now. > _______________________________________________ > freebsd-questions@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to " > freebsd-questions-unsubscribe@freebsd.org" > -- Michael Schuster http://recursiveramblings.wordpress.com/ recursion, n: see 'recursion'