From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796540498; b=OdGXtqjPVfJHAujRQuCPBWXU07Est5akFnU70R/BwyAkQjg1wG3qVlVp7QbISSM06Zay p56AITqbdKv+e0e2XH48haheGLWnByiS7J5k2kc8iCT5i3Jul+eL38KYHO6xxW0ofl1ld a15n2aONQCd/CUErDEMhB+hx6OngfGgrS7rIOXwm6XrVrPWwojas2ljmesLXnH6BZeTA8 FBCit8pV0jfQOFot5RrPwR08Hq11PfiPc72Jj9XH0yaqUCpAJ+DMQP3Diq/QjvIyxN3o8 FtIeS3ophnXrpJ4YWHgP6Mb/JqDNDPX/P6S2pSmYPiAPKDSK6fJwZT2o2za8zhEQp/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796540498; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=hIazOxA/AsPpDWHWff0bN0KCFZCtFQG7L4HUaKFZ3f8=; b=EnmcGFUBJhl31moVc4TSMYCHW75nByi1uVhpDu0dOUajdhfRCmX8Gu60Is0ZY75TAAAa U2rxltwzmk+sVJWkj9uetIprx/2VDN5ktizcc0jddcyl+ALEvHF6Tna8gcuw+uBiaMgPE nLaLaFHNYNDOMRTugA3+Wjfxb2kWf1Z3TWCogp+YGN89FVbJ8jfz+56C4MBgtmuLaTQvX eUWuFOPPd22i2K3NmxNldlSqVrAw9JOuZx0yoJ+2prDKYM1BasAj/AzjCX0O1bReDD0pe 2y6MuD3saAtphKg4Ld4MEiUTG9Lupd40CkSYKdMbPDt5edhyvKRzatJhkL0xW7X6A+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rsO+gpeW; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30A0B606A31F for ; Sun, 6 Dec 2026 07:01:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 30A0B606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rsO+gpeW Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-3282db206d3so1631234eec.0 for ; Sat, 05 Dec 2026 23:01:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796540497; x=1797145297; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=hIazOxA/AsPpDWHWff0bN0KCFZCtFQG7L4HUaKFZ3f8=; b=rsO+gpeWo4+/7492l1jRsH8dD0ihDGs4M6PAG65smmOPeTWPiGc/WdtuXyuxS6L6uU oHeVwsJqI37lcvJts39Joe/9/FLzTnv4rvYgGysYJz7fe7dQ9Yh8fhtLcrp8gf2uoAoU hPTJ4kW0JVO5RpTKVE7UluqcBz4hHYMGBCRx7af54QVsluTc7Sh3+7hIZGJBmmv7USgS 4OYaFbfDlwgcGEP8xiPV7rBxltALRURnImIBXOCXjm6EQ5BwWKMfC0H+O0wPc2iflwnW WgpV3ymnLJhAXTH0CNNzOPSAFihwAkNmsnMRq83n1Q+WZCOzoxpltSxS7Mgzybu9YUa0 nHNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796540497; x=1797145297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=hIazOxA/AsPpDWHWff0bN0KCFZCtFQG7L4HUaKFZ3f8=; b=lWvcHhICfegLxejFKqJhgoM1AduUCLv1K5pHQ30eg3/JmbfFcjVJcwf8qDZj9SaaB5 aJUfqzeoqPInN6qzfGfM1br5CLkkt82EZdYKMxJYkY9mDx2Wo/XRDbaj8WEjVs1xnFxl R+I6UcmP563WsOwBkWtBakQNAOtwumlKuoKgSmYTO+7mc3ROOjKJi7k/A77X2pf4uN2v EvkNHCcbrTuj5czG9NOkgiGUBUlGRx+bsLx9FPVkGmBzBgbtOcAx18KJ49oGGEU6Afc6 lhyQRzQjnCD2y8S40HzAgAwcnY3cxEmaoeTZPfQ/yT35RR+D5ms71ydaly2rnCM4kkSk Fg7Q== X-Gm-Message-State: AB4q+uGm89YK+8Y/QWcT2xLNAEmAlvFZtUkfrtjureR5GlSq6JbSrawp OFbqKKOdqKiXMQihMSnIZfRaeCKyqEKF4G5EBlaNrEI2L7D11SziCBNlJ+O7XEVW X-Gm-Gg: AZFdTZekT/CAMYQhUEQyAfTYcUxor9voP+ii4hFzzC19yGKeNAEv2c4MiD26B+LmXn/ gMoTNyyFMnF9QCEQfsQdZ9tDy2LTfMyzYD+Jg2UTyQF7Oya6+g6imqR6dgSO/zoRFZb35g3p+p4 fpOoVIjZhnXhrSxRxCEMgIJhhBNxj34DAksZxEL6AFn6TYNecTaNxEfIpxR9smA6hM8TVItoXJc UM1bbmtGKubid4Q+3gvhXiFZGdSe5rOGwfEl9TgoBQbsdwJ8usEYppea8Hn6znpsSZzTRWfidnE wotK78OygbVoKUfDt8mreOGmKKprhkirmE7cHksKu3saUYOdew932F3tSB5tXY5WvJ+KoZVVmvp dsvs7/BTgFDI9J7VDFnx6sWZrxSS2YCMLFrJX+4Lm0WpHSsOInfqsAf0SkcgkisLRg71VAfM7Bo tYMUnz4ycaH12r2oQpXmQ0uUrtscNWFk2Cr20kRuXpa7FRP+fn8ZxNgZ8wzGGEn3O7Gxo0koSrt Y/pxtqsVGznyEbCFgOnk0rzOvcgJbTjF8wTHMdOAy+NV0accQNzVcDjqDObVzjMXegFORQrO26n CgKOlCGBVaVE1glDRvxufPZnPOd3 X-Received: by 2002:a05:693c:83ce:20b0:385:ce95:2e14 with SMTP id 5a478bee46e88-3875216af6amr7195460eec.30.1796540497167; Sat, 05 Dec 2026 23:01:37 -0800 (PST) Received: from aeron-host ([103.177.178.109]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521bb368sm41675801eec.10.2026.12.05.23.01.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 23:01:36 -0800 (PST) From: Abhishek Agarwal To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , Paul Tarjan , Erik Cervin-Edin , John Mellor , Nicolas Riebesel Subject: [PATCH 2/3] fsmonitor: keep watch paths below a renamed directory up to date Date: Sun, 6 Dec 2026 12:30:44 +0530 Message-ID: <20261206070045.12607-3-agab0323@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261206070045.12607-1-agab0323@gmail.com> References: <20261206070045.12607-1-agab0323@gmail.com> X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit An inotify watch belongs to a directory, not to its path, so after a directory is renamed, the watches on its subdirectories keep working. The Linux listener only records the new path of the renamed directory itself, though. When register_inotify() then walks the renamed directory, add_watch() asks for each subdirectory's watch with IN_MASK_CREATE, gets EEXIST, and returns without updating its maps. Those watches stay known by their old paths, so changes below them are reported for paths that no longer exist, and "git status" misses them until something resets the daemon's state. When the directory is already watched, ask for its watch again without IN_MASK_CREATE, which returns the existing watch descriptor and leaves the watch as it is, and record the new path in both maps. Erik's report [1] points out that this case was already asked about when the listener was reviewed. [1] https://lore.kernel.org/git/as4z1SskiPyrSKIa@vader/ Reported-by: Erik Cervin-Edin Signed-off-by: Abhishek Agarwal --- compat/fsmonitor/fsm-listen-linux.c | 102 ++++++++++++++++++++++------ t/t7527-builtin-fsmonitor.sh | 11 +++ 2 files changed, 91 insertions(+), 22 deletions(-) diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c index 8b5fe492d1..df91714f76 100644 --- a/compat/fsmonitor/fsm-listen-linux.c +++ b/compat/fsmonitor/fsm-listen-linux.c @@ -92,29 +92,17 @@ static int rename_entry_cmp(const void *cmp_data UNUSED, return e1->cookie != e2->cookie; } -/* - * Register an inotify watch, add watch descriptor to path mapping - * and the reverse mapping. - */ -static int add_watch(const char *path, struct fsm_listen_data *data) -{ - const char *interned = strintern(path); - struct watch_entry *w1, *w2; +/* All events but reads and opens, and only for directories. */ +#define WATCH_MASK \ + ((IN_ALL_EVENTS | IN_ONLYDIR) ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN) - /* add the inotify watch, don't allow watches to be modified */ - int wd = inotify_add_watch(data->fd_inotify, interned, - (IN_ALL_EVENTS | IN_ONLYDIR | IN_MASK_CREATE) - ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN); - if (wd < 0) { - if (errno == ENOENT || errno == ENOTDIR) - return 0; /* directory was deleted or is not a directory */ - if (errno == EEXIST) - return 0; /* watch already exists, no action needed */ - if (errno == ENOSPC) - return error(_("inotify watch limit reached; " - "increase fs.inotify.max_user_watches")); - return error_errno(_("inotify_add_watch('%s') failed"), interned); - } +/* + * Add watch descriptor to path mapping and the reverse mapping. + */ +static void record_watch(int wd, const char *interned, + struct fsm_listen_data *data) +{ + struct watch_entry *w1, *w2; /* add watch descriptor -> directory mapping */ CALLOC_ARRAY(w1, 1); @@ -129,7 +117,77 @@ static int add_watch(const char *path, struct fsm_listen_data *data) w2->dir = interned; hashmap_entry_init(&w2->ent, strhash(w2->dir)); hashmap_add(&data->revwatches, &w2->ent); +} +/* + * The directory is already watched, for example because it is below a + * directory that was renamed: a watch follows the directory, not its + * path, so we still know it by the path it had before the rename. + * Record its new path. + */ +static int update_watch_path(const char *interned, + struct fsm_listen_data *data) +{ + struct watch_entry k, *w1, *w2; + + /* without IN_MASK_CREATE, this returns the existing watch */ + int wd = inotify_add_watch(data->fd_inotify, interned, WATCH_MASK); + if (wd < 0) { + if (errno == ENOENT || errno == ENOTDIR) + return 0; /* deleted, or not a directory */ + return error_errno(_("inotify_add_watch('%s') failed"), + interned); + } + + k.wd = wd; + hashmap_entry_init(&k.ent, memhash(&k.wd, sizeof(int))); + w1 = hashmap_get_entry(&data->watches, &k, ent, NULL); + if (!w1) { + /* the old watch went away in the meantime */ + record_watch(wd, interned, data); + return 0; + } + if (w1->dir == interned) + return 0; /* already known under this path */ + + k.dir = w1->dir; + hashmap_entry_init(&k.ent, strhash(k.dir)); + w2 = hashmap_remove_entry(&data->revwatches, &k, ent, &wd); + if (!w2) + BUG("no reverse watch for '%s'", w1->dir); + + trace_printf_key(&trace_fsmonitor, "watch for '%s' is now '%s'", + w1->dir, interned); + w1->dir = interned; + w2->dir = interned; + hashmap_entry_init(&w2->ent, strhash(w2->dir)); + hashmap_add(&data->revwatches, &w2->ent); + return 0; +} + +/* + * Register an inotify watch, add watch descriptor to path mapping + * and the reverse mapping. + */ +static int add_watch(const char *path, struct fsm_listen_data *data) +{ + const char *interned = strintern(path); + + /* add the inotify watch, don't allow watches to be modified */ + int wd = inotify_add_watch(data->fd_inotify, interned, + WATCH_MASK | IN_MASK_CREATE); + if (wd < 0) { + if (errno == ENOENT || errno == ENOTDIR) + return 0; /* directory was deleted or is not a directory */ + if (errno == EEXIST) + return update_watch_path(interned, data); + if (errno == ENOSPC) + return error(_("inotify watch limit reached; " + "increase fs.inotify.max_user_watches")); + return error_errno(_("inotify_add_watch('%s') failed"), interned); + } + + record_watch(wd, interned, data); return 0; } diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh index e0c4fa0aac..b7041e1825 100755 --- a/t/t7527-builtin-fsmonitor.sh +++ b/t/t7527-builtin-fsmonitor.sh @@ -1497,4 +1497,15 @@ test_expect_success 'rename a directory again after its old path came back' ' check_fsmonitor_status back ' +test_expect_success 'edits below a renamed directory are reported' ' + test_when_finished "stop_daemon_delete_repo below" && + start_watching below old old/sub && + git -C below mv old new && + git -C below commit -q -m rename && + echo b >>below/new/sub/file && + echo " M new/sub/file" >expect && + git -C below status --porcelain >actual && + test_cmp expect actual +' + test_done -- 2.56.0