Skip to content

Commit 350425a

Browse files
committed
hlink: fix hard-link failure abort (#741) and the latent drain hang it exposes
When a hard-link group member fails its direct link() (EMLINK or EXDEV) it is written as a regular file and finishes first, claiming the group's prior_hlinks node; a later member's finish then hits the "Hlink node data ... already has path" check and aborts the whole transfer (code 13). Tolerate the duplicate finish: keep the first leader's path, log an FINFO warning, and continue. Removing the abort exposes a second, latent bug: finish_hard_link() decrements flist->in_progress for every file in the completion chain, but members that failed their direct link() in hard_link_check() were never counted, so the counter drifts negative and the final drain hangs forever. Add FLAG_HLINK_WAIT, set at the three increment sites, and gate the two affected decrements on it. Tested with a 65000-link pinned-inode EMLINK fixture (full repro and evidence in the PR description), an -H --link-dest regression matrix, and the upstream hardlink/link-dest test suite. hlink: fix hard-link failure abort (#741) and the latent drain hang it exposes When a hard-link group member fails its direct link() (EMLINK or EXDEV) it is written as a regular file and finishes first, claiming the group's prior_hlinks node; a later member's finish then hits the "Hlink node data ... already has path" check and aborts the whole transfer (code 13). Tolerate the duplicate finish: keep the first leader's path, log an FINFO warning, and continue. Removing the abort exposes a second, latent bug: finish_hard_link() decrements flist->in_progress for every file in the completion chain, but members that failed their direct link() in hard_link_check() were never counted, so the counter drifts negative and the final drain hangs forever. Add FLAG_HLINK_WAIT, set at the three increment sites, and gate the two affected decrements on it. Tested with a 65000-link pinned-inode EMLINK fixture (full repro and evidence in the PR description), an -H --link-dest regression matrix, and the upstream hardlink/link-dest test suite.
1 parent 8718cd2 commit 350425a

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎generator.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
22532253
if (statret != 0) {
22542254
#ifdef SUPPORT_HARD_LINKS
22552255
if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
2256+
file->flags |= FLAG_HLINK_WAIT;
22562257
cur_flist->in_progress++;
22572258
goto cleanup;
22582259
}
@@ -2358,6 +2359,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
23582359
/* pretend the file didn't exist */
23592360
#ifdef SUPPORT_HARD_LINKS
23602361
if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
2362+
file->flags |= FLAG_HLINK_WAIT;
23612363
cur_flist->in_progress++;
23622364
goto cleanup;
23632365
}
@@ -2595,7 +2597,10 @@ static void handle_skipped_hlink(struct file_struct *file, int itemizing,
25952597
return;
25962598

25972599
file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
2598-
cur_flist->in_progress--; /* undo prior increment */
2600+
if (file->flags & FLAG_HLINK_WAIT) {
2601+
file->flags &= ~FLAG_HLINK_WAIT;
2602+
cur_flist->in_progress--; /* undo prior increment */
2603+
}
25992604
f_name(file, fbuf);
26002605
recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
26012606

‎hlink.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ int hard_link_check(struct file_struct *file, int ndx, char *fname,
335335
F_HL_PREV(file) = F_HL_PREV(prev_file);
336336
F_HL_PREV(prev_file) = ndx;
337337
file->flags |= FLAG_FILE_SENT;
338+
file->flags |= FLAG_HLINK_WAIT;
338339
cur_flist->in_progress++;
339340
if (DEBUG_GTE(HLINK, 2)) {
340341
rprintf(FINFO, "hlink for %d (%s,%d): waiting for %d\n",
@@ -535,7 +536,10 @@ void finish_hard_link(struct file_struct *file, const char *fname, int fin_ndx,
535536
prev_statret = link_stat(f_name(file, prev_name), &prev_sx.st, 0);
536537
val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
537538
our_name, stp, fname, itemizing, code);
538-
flist->in_progress--;
539+
if (file->flags & FLAG_HLINK_WAIT) {
540+
file->flags &= ~FLAG_HLINK_WAIT;
541+
flist->in_progress--;
542+
}
539543
free_stat_x(&prev_sx);
540544
if (val < 0)
541545
continue;
@@ -555,9 +559,16 @@ void finish_hard_link(struct file_struct *file, const char *fname, int fin_ndx,
555559
exit_cleanup(RERR_MESSAGEIO);
556560
}
557561
if (CVAL(node->data, 0) != 0) {
558-
rprintf(FERROR, "Hlink node data for %d already has path=%s (%s)\n",
562+
/* A finished leader was already recorded for this group.
563+
* This can happen when an earlier member of the group failed
564+
* to hard-link (e.g. EMLINK or EXDEV) and was written as a
565+
* regular file, then finished. Keep the original leader's
566+
* path and tolerate the duplicate finish instead of aborting
567+
* the whole transfer. (Upstream: RsyncProject/rsync#741.) */
568+
rprintf(FINFO,
569+
"hlink: group %d already finished at path=%s (%s); ignoring duplicate finish\n",
559570
gnum, (char*)node->data, f_name(file, prev_name));
560-
exit_cleanup(RERR_MESSAGEIO);
571+
return;
561572
}
562573
free(node->data);
563574
node->data = strdup(our_name);

‎rsync.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define FLAG_TIME_FAILED (1<<11)/* generator */
9494
#define FLAG_MOD_NSEC (1<<12) /* sender/receiver/generator */
9595
#define FLAG_GOT_DIR_FLIST (1<<13)/* sender/receiver/generator - dir_flist only */
96+
#define FLAG_HLINK_WAIT (1<<14) /* generator (w/FLAG_HLINKED): counted in in_progress, pending hard-link processing */
9697

9798
/* These flags are passed to functions but not stored. */
9899

0 commit comments

Comments
 (0)