Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6155190rwb; Wed, 18 Jan 2023 01:43:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXv3xev1ntkzwqK8dyY7fJtuTiwR0il9sZsRdnB65aMK+425b+Ilc6d4TCpkFkX17YHw3REu X-Received: by 2002:a17:902:ec82:b0:193:3215:960a with SMTP id x2-20020a170902ec8200b001933215960amr9205409plg.33.1674035006554; Wed, 18 Jan 2023 01:43:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674035006; cv=none; d=google.com; s=arc-20160816; b=oDe4zh0AquTbt5PU0qOGZtWZJ62ytiGg2+yFpW/0GP5DwCms0ikIebfmUm27O3fV3+ FiRX/BoNeuGAofOgNdsnyaIRBmGK/upY9iQ6fL9lI0/2SMebw/6wS+fyqAx8L4khApVn edGzFu+G9dxSLaDEezMlJXc5XKvKOHW3By1DDQfd8X1Dtoj3skjO2Lu/aqhjJLmjUhus 2lackfvgZiwzONuRnjDkifcWAOKXDk/gt3TbUWYILssvTu+KB7nwkz6mvSgn8xWRxpus cYNXoK3pB18jy03XcbrAEzJ7am1rGOWTUYovX9cKi5yeLO+IeGKOwPryLiAxEKXk7VgJ ktUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=tgBJPKX6OrvfX7vt7VeuwRYdAa1JYzlRkPsncNLXpXI=; b=JTYkPSEOT1cylWJW2b4rGJylOCaJy8adDAJX+w4OSqGwmkmVan4RV8arAhhh9YiE1j wZtTUHEriExbYy9SYo5QWwD/x2kdmaru8lXsvQ5AEAAxzjJoHRy0TN1GfykOLK12P8bI pDOF79lwiA8U9zlkndGyLsz4cWh03fcKWWLzgwMbvMz7649yRlmvJrWiqXgkKwvjA2pZ h7LiA7fvm8MGIAlNv/MlYBA7gRKcnY8X7HvKVUQigew4j/883/7WKWAJvNr80GOWWKmV sd3Yx39fh7Ax0miRp4aMyXcUldrQPG8P6RR76NYOtoD3yuoDNpnxKqadniNpVGDDsP7b 73Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=BmBHBQR+; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=atRN1Vcw; 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 m7-20020a170902f64700b00192659c74aesi35116436plg.362.2023.01.18.01.43.07; Wed, 18 Jan 2023 01:43:26 -0800 (PST) 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=BmBHBQR+; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=atRN1Vcw; 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 S229729AbjARJmT (ORCPT + 99 others); Wed, 18 Jan 2023 04:42:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230168AbjARJj1 (ORCPT ); Wed, 18 Jan 2023 04:39:27 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E2A16334; Wed, 18 Jan 2023 00:56:39 -0800 (PST) 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 4DF8B3E98B; Wed, 18 Jan 2023 08:56:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1674032198; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=tgBJPKX6OrvfX7vt7VeuwRYdAa1JYzlRkPsncNLXpXI=; b=BmBHBQR+zuTDcSZg7eP3ujTQmmYoHkhFL7UHQnOfaLn4X+8t6GJCTB2lHWId0KdMifpI2B 7vV8O4S7uzbYuMML8bte/sATQ7YPyN9lA0xGyolZk4BxPQZPanpexstsnYSPsf5RhFCUhV YWrEtQba5mUUXVSSL86STuKIwx1R3V0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1674032198; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=tgBJPKX6OrvfX7vt7VeuwRYdAa1JYzlRkPsncNLXpXI=; b=atRN1VcwzH95sn4fF0V2e0yJNbP4744YGS7ZBjufI9NJH7+jLIRm1F3G4QUTx17qhiGXr4 VDKCovOTvCse/FBg== 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 3E2DE138FE; Wed, 18 Jan 2023 08:56:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KIklD0a0x2NBJwAAMHmgww (envelope-from ); Wed, 18 Jan 2023 08:56:38 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 9C505A06B2; Wed, 18 Jan 2023 09:56:37 +0100 (CET) Date: Wed, 18 Jan 2023 09:56:37 +0100 From: Jan Kara To: Dave Chinner Cc: Jan Kara , Al Viro , linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, Ted Tso , linux-xfs@vger.kernel.org Subject: Re: Locking issue with directory renames Message-ID: <20230118085637.56u4tbocimzhrjly@quack3> References: <20230117123735.un7wbamlbdihninm@quack3> <20230117214457.GG360264@dread.disaster.area> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230117214457.GG360264@dread.disaster.area> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS 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, On Wed 18-01-23 08:44:57, Dave Chinner wrote: > On Tue, Jan 17, 2023 at 01:37:35PM +0100, Jan Kara wrote: > > I've some across an interesting issue that was spotted by syzbot [1]. The > > report is against UDF but AFAICS the problem exists for ext4 as well and > > possibly other filesystems. The problem is the following: When we are > > renaming directory 'dir' say rename("foo/dir", "bar/") we lock 'foo' and > > 'bar' but 'dir' is unlocked because the locking done by vfs_rename() is > > > > if (!is_dir || (flags & RENAME_EXCHANGE)) > > lock_two_nondirectories(source, target); > > else if (target) > > inode_lock(target); > > > > However some filesystems (e.g. UDF but ext4 as well, I suspect XFS may be > > hurt by this as well because it converts among multiple dir formats) need > > to update parent pointer in 'dir' and nothing protects this update against > > a race with someone else modifying 'dir'. Now this is mostly harmless > > because the parent pointer (".." directory entry) is at the beginning of > > the directory and stable however if for example the directory is converted > > from packed "in-inode" format to "expanded" format as a result of > > concurrent operation on 'dir', the filesystem gets corrupted (or crashes as > > in case of UDF). > > No, xfs_rename() does not have this problem - we pass four inodes to > the function - the source directory, source inode, destination > directory and destination inode. > > In the above case, "dir/" is passed to XFs as the source inode - the > src_dir is "foo/", the target dir is "bar/" and the target inode is > null. src_dir != target_dir, so we set the "new_parent" flag. the > srouce inode is a directory, so we set the src_is_directory flag, > too. > > We lock all three inodes that are passed. We do various things, then > run: > > if (new_parent && src_is_directory) { > /* > * Rewrite the ".." entry to point to the new > * directory. > */ > error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, > target_dp->i_ino, spaceres); > ASSERT(error != -EEXIST); > if (error) > goto out_trans_cancel; > } > > which replaces the ".." entry in source inode atomically whilst it > is locked. Any directory format changes that occur during the > rename are done while the ILOCK is held, so they appear atomic to > outside observers that are trying to parse the directory structure > (e.g. readdir). Thanks for explanation! I've missed the ILOCK locking in xfs_rename() when I was glancing over the function... Honza -- Jan Kara SUSE Labs, CR