return 0;
 }
 
+/*
+ * When processing the new references for an inode we may orphanize an existing
+ * directory inode because its old name conflicts with one of the new references
+ * of the current inode. Later, when processing another new reference of our
+ * inode, we might need to orphanize another inode, but the path we have in the
+ * reference reflects the pre-orphanization name of the directory we previously
+ * orphanized. For example:
+ *
+ * parent snapshot looks like:
+ *
+ * .                                     (ino 256)
+ * |----- f1                             (ino 257)
+ * |----- f2                             (ino 258)
+ * |----- d1/                            (ino 259)
+ *        |----- d2/                     (ino 260)
+ *
+ * send snapshot looks like:
+ *
+ * .                                     (ino 256)
+ * |----- d1                             (ino 258)
+ * |----- f2/                            (ino 259)
+ *        |----- f2_link/                (ino 260)
+ *        |       |----- f1              (ino 257)
+ *        |
+ *        |----- d2                      (ino 258)
+ *
+ * When processing inode 257 we compute the name for inode 259 as "d1", and we
+ * cache it in the name cache. Later when we start processing inode 258, when
+ * collecting all its new references we set a full path of "d1/d2" for its new
+ * reference with name "d2". When we start processing the new references we
+ * start by processing the new reference with name "d1", and this results in
+ * orphanizing inode 259, since its old reference causes a conflict. Then we
+ * move on the next new reference, with name "d2", and we find out we must
+ * orphanize inode 260, as its old reference conflicts with ours - but for the
+ * orphanization we use a source path corresponding to the path we stored in the
+ * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
+ * receiver fail since the path component "d1/" no longer exists, it was renamed
+ * to "o259-6-0/" when processing the previous new reference. So in this case we
+ * must recompute the path in the new reference and use it for the new
+ * orphanization operation.
+ */
+static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
+{
+       char *name;
+       int ret;
+
+       name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
+       if (!name)
+               return -ENOMEM;
+
+       fs_path_reset(ref->full_path);
+       ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
+       if (ret < 0)
+               goto out;
+
+       ret = fs_path_add(ref->full_path, name, ref->name_len);
+       if (ret < 0)
+               goto out;
+
+       /* Update the reference's base name pointer. */
+       set_ref_path(ref, ref->full_path);
+out:
+       kfree(name);
+       return ret;
+}
+
 /*
  * This does all the move/link/unlink/rmdir magic.
  */
                                struct name_cache_entry *nce;
                                struct waiting_dir_move *wdm;
 
+                               if (orphanized_dir) {
+                                       ret = refresh_ref_path(sctx, cur);
+                                       if (ret < 0)
+                                               goto out;
+                               }
+
                                ret = orphanize_inode(sctx, ow_inode, ow_gen,
                                                cur->full_path);
                                if (ret < 0)