Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6019169rwd; Wed, 24 May 2023 09:41:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7OjMX7WS6GINyjClzYRDivHe4FvTJkWOCOAi6wOvArah/7YAQwwzP4FN7R/mTZFBnPiZVp X-Received: by 2002:a17:90a:dd45:b0:255:80e4:1550 with SMTP id u5-20020a17090add4500b0025580e41550mr9140066pjv.8.1684946515143; Wed, 24 May 2023 09:41:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684946515; cv=none; d=google.com; s=arc-20160816; b=xz575XQYi0JePJCfDQGk67RqpVnGG6b+yBgVrD0qwGsCIlSsKysTESaeA65cqE9ZeF 1Ms/ndqsPc3vUM4Nzn97CQnw8g+CsFGJQasirj7uNe9nbyNMZrXQtt/BE/vgo0LMcaUa 9hVjMQNwJNPZZ6Lj04DC3xI22y9nwOY5veE0zYjt25GzxSCeIy5VtiCNJYlwLUs+1MGB XR2+x9HGG9zthZKg7xzZEz+w2eh1+00plWeNwttL3kSWmixud7X1CLH4u7+jjpQ1aPFd TEp6GlLLc0gkkBHX1cppzalI+1QZX6vKTQsJwrCNNUbJe+ieQ3GeSwHZ9d80cV0Yt7Ud s2MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature:dkim-signature; bh=Un5QTjftH9dU+V3I75wkntprxt44198mJ0mxj5LY1F4=; b=FfBeLOyXyUTGEyEcLq+Txi4L21a+CNwIE6j2vkDTp9ZeIJVVe+5V/1kLzXR4MDSZ5x 0PNJA/UgA4djo5YKIC9/kvBg1WcWCNjOPgDLFXAvM8f8+/sgDcn8Pv+q5110N2Q6zICi y6CAnzYLSyoy+Drm2SjGpq8spTq/+/h4IqHMpHrWJTeRVjBCWmD82w3RjvMsSXJsPhSh RGNmVi+zFAdMSpHJFtn4YYWza5dW4C0yQXSLGtWFM5/qAGOgVdhuo7xkG4jVAxIzmdp/ 5WbWmiENsEM7pLfGorY1Uc0XbOepIlSaQhMJ1qeMQHMc3+4Mo+Arsa8su/hydYaBH7fQ ZyMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=NzKoCE7p; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d66-20020a633645000000b0053efd751392si182707pga.827.2023.05.24.09.41.37; Wed, 24 May 2023 09:41:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=NzKoCE7p; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229521AbjEXQfI (ORCPT + 99 others); Wed, 24 May 2023 12:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjEXQfH (ORCPT ); Wed, 24 May 2023 12:35:07 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75E06E7; Wed, 24 May 2023 09:35:06 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 12AB4219B3; Wed, 24 May 2023 16:35:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1684946105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=Un5QTjftH9dU+V3I75wkntprxt44198mJ0mxj5LY1F4=; b=NzKoCE7pFgM8m61Eg2c1UZyAoxaQvU3JNYHAwKLp+JBiRpfR3G5QFrfQXcbJ5AdjqV8XTg qzNlEdAduRnLOe55mRKC3zM3nQyRJ6kbSUthMpZZHXvmkY2Bo170XAVeIRbQAOGy6W5anM uuEb+IKgJIElS7mRh7LjENXusMp/Il8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1684946105; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=Un5QTjftH9dU+V3I75wkntprxt44198mJ0mxj5LY1F4=; b=+WalUnwpayJnChGvTi5UKB5EVBifyu1ulwWMiaNudtQgcJnaeKW8YzjjM+HOQJdc+iriz4 VT4dTzLMeotCMcBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 056D213425; Wed, 24 May 2023 16:35:05 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id yRBHAbk8bmSSZQAAMHmgww (envelope-from ); Wed, 24 May 2023 16:35:05 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 98CDFA075C; Wed, 24 May 2023 18:35:04 +0200 (CEST) Date: Wed, 24 May 2023 18:35:04 +0200 From: Jan Kara To: linux-fsdevel@vger.kernel.org Cc: "Darrick J. Wong" , Ted Tso , Amir Goldstein , 'David Laight , Al Viro , Christian Brauner , Miklos Szeredi , linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org Subject: Locking for RENAME_EXCHANGE Message-ID: <20230524163504.lugqgz2ibe5vdom2@quack3> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Hello! This is again about the problem with directory renames I've already reported in [1]. To quickly sum it up some filesystems (so far we know at least about xfs, ext4, udf, reiserfs) need to lock the directory when it is being renamed into another directory. This is because we need to update the parent pointer in the directory in that case and if that races with other operation on the directory, bad things can happen. So far we've done the locking in the filesystem code but recently Darrick pointed out [2] that we've missed the RENAME_EXCHANGE case in our ext4 fix. That one is particularly nasty because RENAME_EXCHANGE can arbitrarily mix regular files and directories. Couple nasty arising cases: 1) We need to additionally lock two exchanged directories. Suppose a situation like: mkdir P; mkdir P/A; mkdir P/B; touch P/B/F CPU1 CPU2 renameat2("P/A", "P/B", RENAME_EXCHANGE); renameat2("P/B/F", "P/A", 0); Both operations need to lock A and B directories which are unrelated in the tree. This means we must establish stable lock ordering on directory locks even for the case when they are not in ancestor relationship. 2) We may need to lock a directory and a non-directory and they can be in parent-child relationship when hardlinks are involved: mkdir A; mkdir B; touch A/F; ln A/F B/F renameat2("A/F", "B"); And this is really nasty because we don't have a way to find out whether "A/F" and "B" are in any relationship - in particular whether B happens to be another parent of A/F or not. What I've decided to do is to make sure we always lock directory first in this mixed case and that *should* avoid all the deadlocks but I'm spelling this out here just in case people can think of some even more wicked case before I'll send patches. Also I wanted to ask (Miklos in particular as RENAME_EXCHANGE author): Why do we lock non-directories in RENAME_EXCHANGE case? If we didn't have to do that things would be somewhat simpler... Honza [1] https://lore.kernel.org/all/20230117123735.un7wbamlbdihninm@quack3 [2] https://lore.kernel.org/all/20230517045836.GA11594@frogsfrogsfrogs -- Jan Kara SUSE Labs, CR