Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1990227imu; Fri, 14 Dec 2018 04:09:47 -0800 (PST) X-Google-Smtp-Source: AFSGD/VaFn2CqxLXCigoR+VCgKO64Mk28zUPneK6UJWc9f5n6jy3akjqXUdnb02DrNbeKhcRCCI7 X-Received: by 2002:a63:4665:: with SMTP id v37mr2496969pgk.425.1544789387483; Fri, 14 Dec 2018 04:09:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544789387; cv=none; d=google.com; s=arc-20160816; b=lrlXLRgjOzfCh5ECv816f0BemUMIKOy+uJmTdbcb54VLgbhgsCRW+bZi4syUmN4t/n Rl0cW/cct4uNolfFN2ugqPmU2nvy3vmnAU3Y6NfUEiXBoFvjkEeMurKEIuhIlE5QO61X FnGh/5ErXLdFnYnpUV4AcxGMUiu+Dbz6dRGtQunpsyNxDfbFnv6LBnE6mh34rp5naIB3 1g65SS4gwzbF4RtFfWgqOx73el13cxjc3WO71MxK4dSN/Dmp3uViGhWOJCia/thDn+cW JCdH3wAmB0bDxxnvTNFXOT0zSA65njzv12OLFf/PVahq0RqLm52Fy95RI2LQSy4ye8Gr J32w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8x6oROmY7PyZHX8c0W2IfqIW4vOtKwK9rsKDunQQtMo=; b=d5wLBc1QXil5ChzCV4t9FbeuMxdl2D8PJBRHFmi/joeuh9KsbSnX6bjXv+x3TaeKqg FONQjXJ7mpL7dUZ7gC3yxg6K+Rh47vrgzCD7UFl9UjFx+9llqblIn9ZTay6k/SyhLh3g hQH8XDa0Zi+Qo7cDcT/99Pi5UywFc4Lc8GkNcdV0HCS6UXXkLlc/pAWDUbsgqHN0mikY xmm4VbEgAFUO4UxYK8VfvCcmYqCQ4OOiKGpSsqlO3EfiQtk6a3yMTzsZ1AQKrXXsCcDF to2wjbWWj5+K87XhOw7E7QTmkToEOTi3JtYPEx7YM9251ThkxDb7NUyeAIjZ5QS8Kdqb B0SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xj6xXGbK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 31si4102765plh.274.2018.12.14.04.09.25; Fri, 14 Dec 2018 04:09:47 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xj6xXGbK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730956AbeLNMGh (ORCPT + 99 others); Fri, 14 Dec 2018 07:06:37 -0500 Received: from mail.kernel.org ([198.145.29.99]:51650 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730434AbeLNMGg (ORCPT ); Fri, 14 Dec 2018 07:06:36 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E10E721486; Fri, 14 Dec 2018 12:06:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544789194; bh=8ZJHuDY+b7OObQPRfemSvLrU4JxH6zqO1p0d1btbU2o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xj6xXGbKG8HPtxgXfA1dehWs9O/9zH9YKziWMow+t7rHdvdBeZHQqSn0cQKnOnhlr NL3TOsP/3ZN2InU7/K7X6Lge/GU9suo8aiaBGKVXi5om0tleU5wwBBMdT4i1vqWEt0 cwN022IFsNsR4BR8facLL7i9h5cNrAlOwY3DtOF8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Li Zhijian , Philip Li , Dominik Brodowski , Li Zhijian , Al Viro , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 131/142] initramfs: clean old path before creating a hardlink Date: Fri, 14 Dec 2018 13:00:16 +0100 Message-Id: <20181214115752.281723541@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115747.053633987@linuxfoundation.org> References: <20181214115747.053633987@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 7c0950d455d6ab610d2990a13120f935b75abf2c ] sys_link() can fail due to the new path already existing. This case ofen occurs when we use a concated initrd, for example: 1) prepare a basic rootfs, it contains a regular files rc.local lizhijian@:~/yocto-tiny-i386-2016-04-22$ cat etc/rc.local #!/bin/sh echo "Running /etc/rc.local..." yocto-tiny-i386-2016-04-22$ find . | sed 's,^\./,,' | cpio -o -H newc | gzip -n -9 >../rootfs.cgz 2) create a extra initrd which also includes a etc/rc.local lizhijian@:~/lkp-x86_64/etc$ echo "append initrd" >rc.local lizhijian@:~/lkp/lkp-x86_64/etc$ cat rc.local append initrd lizhijian@:~/lkp/lkp-x86_64/etc$ ln rc.local rc.local.hardlink append initrd lizhijian@:~/lkp/lkp-x86_64/etc$ stat rc.local rc.local.hardlink File: 'rc.local' Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 11296086 Links: 2 Access: (0664/-rw-rw-r--) Uid: ( 1002/lizhijian) Gid: ( 1002/lizhijian) Access: 2018-11-15 16:08:28.654464815 +0800 Modify: 2018-11-15 16:07:57.514903210 +0800 Change: 2018-11-15 16:08:24.180228872 +0800 Birth: - File: 'rc.local.hardlink' Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 11296086 Links: 2 Access: (0664/-rw-rw-r--) Uid: ( 1002/lizhijian) Gid: ( 1002/lizhijian) Access: 2018-11-15 16:08:28.654464815 +0800 Modify: 2018-11-15 16:07:57.514903210 +0800 Change: 2018-11-15 16:08:24.180228872 +0800 Birth: - lizhijian@:~/lkp/lkp-x86_64$ find . | sed 's,^\./,,' | cpio -o -H newc | gzip -n -9 >../rc-local.cgz lizhijian@:~/lkp/lkp-x86_64$ gzip -dc ../rc-local.cgz | cpio -t . etc etc/rc.local.hardlink <<< it will be extracted first at this initrd etc/rc.local 3) concate 2 initrds and boot lizhijian@:~/lkp$ cat rootfs.cgz rc-local.cgz >concate-initrd.cgz lizhijian@:~/lkp$ qemu-system-x86_64 -nographic -enable-kvm -cpu host -smp 1 -m 1024 -kernel ~/lkp/linux/arch/x86/boot/bzImage -append "console=ttyS0 earlyprint=ttyS0 ignore_loglevel" -initrd ./concate-initr.cgz -serial stdio -nodefaults In this case, sys_link(2) will fail and return -EEXIST, so we can only get the rc.local at rootfs.cgz instead of rc-local.cgz [akpm@linux-foundation.org: move code to avoid forward declaration] Link: http://lkml.kernel.org/r/1542352368-13299-1-git-send-email-lizhijian@cn.fujitsu.com Signed-off-by: Li Zhijian Cc: Philip Li Cc: Dominik Brodowski Cc: Li Zhijian Cc: Al Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- init/initramfs.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/init/initramfs.c b/init/initramfs.c index 640557788026..f6f4a1e4cd54 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -291,16 +291,6 @@ static int __init do_reset(void) return 1; } -static int __init maybe_link(void) -{ - if (nlink >= 2) { - char *old = find_link(major, minor, ino, mode, collected); - if (old) - return (ksys_link(old, collected) < 0) ? -1 : 1; - } - return 0; -} - static void __init clean_path(char *path, umode_t fmode) { struct kstat st; @@ -313,6 +303,18 @@ static void __init clean_path(char *path, umode_t fmode) } } +static int __init maybe_link(void) +{ + if (nlink >= 2) { + char *old = find_link(major, minor, ino, mode, collected); + if (old) { + clean_path(collected, 0); + return (ksys_link(old, collected) < 0) ? -1 : 1; + } + } + return 0; +} + static __initdata int wfd; static int __init do_name(void) -- 2.19.1