From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A2EAC4167B for ; Fri, 30 Dec 2022 10:58:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233838AbiL3K6B (ORCPT ); Fri, 30 Dec 2022 05:58:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229924AbiL3K6A (ORCPT ); Fri, 30 Dec 2022 05:58:00 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A79961A390 for ; Fri, 30 Dec 2022 02:57:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=I5P17+MDIkCpDQZB1LcswKgVFyMsYHYc2INWURQYUYc=; b=uLKdQC+SutNeMcCIIbqbNVAhR8 3DHYP8uTjAEPUWFWnJE4J1mbgPmlAwuDn+h3w7aM4shUlsx/YotASq6z5WoCY4tpfG/sJwGZXzWQK VujDU5KkSGAYtzv+g4YMk4nKP+//HvSDOHlj/lZBRsX0Ox2ingyf5tyyCWCTwIkKhZ6W/X0czDqyT AtiiDi3qIIGaw3xfuliTYOiZHCtnDnyA89ujbQziS+2f2TeYw72y+aHNaF9AyZ54HvSiaN/T+FLCx PcKY8awpO/1xLzhfVXfx09NdMPNKIgWDF2zRwvt5PQfwhd5M/AVlKB9geWHVJ7IsVVCVaGt21Ct2m 1nxDnOgg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pBD5Q-00AYIS-Po; Fri, 30 Dec 2022 10:58:04 +0000 Date: Fri, 30 Dec 2022 10:58:04 +0000 From: Matthew Wilcox To: Yun Levi Cc: linux-fsdevel@vger.kernel.org Subject: Re: [Question] Unlinking original file of bind mounted file. Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On Fri, Dec 30, 2022 at 05:08:31PM +0900, Yun Levi wrote: > Hello fs-devel folks, > > I have a few questions about below situation's handling. > > ====================================================== > 1. mount --bind {somefile} {target} > 2. rm -f {somefile} > ======================================================= > > when it happens, the step (2)'s operation is working -- it removes. > But, the inode of {somefile} is live with i_nlink = 0 with an orphan > state of ext4_inode_info in ext4-fs. > > IIUC, because ext4-inode-entry is removed in the disk via ext4_unlink, > and it seems possible > the inode_entry which is freed by unlink in step(2) will be used again > when a new file is created. No, that's not correct. Here's how to think about Unix files (not just ext4, going all the way back to the 1970s). Each inode has a reference count. All kinds of things hold a reference count to an inode; some of the more common ones are a name in a directory, an open file, a mmap of that open file, passing a file descriptor through a unix socket, etc, etc. Unlink removes a name from a directory. That causes the reference count to be decreased, but the inode will only be released if that causes the reference count to drop to 0. If the file is open, or it has multiple names, it won't be removed. mount --bind obviously isn't traditional Unix, but it fits in the same paradigm. It causes a new reference count to be taken on the inode. So you can remove the original name that was used to create the link, and that causes i_nlink to drop to 0, but the in-memory refcount is still positive, so the inode will not be reused.