From 24d811c68222f42eb241f2457dde88391dae90e0 Mon Sep 17 00:00:00 2001 From: "cantab.net!aia21" Date: Sun, 15 Aug 2004 21:04:22 +0000 Subject: [PATCH] Fix some bugs in error recovery code path (runlist unterminated and one bit was not recovered). (Logical change 1.498) --- libntfs/lcnalloc.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/libntfs/lcnalloc.c b/libntfs/lcnalloc.c index 860dee2a..de9a0cad 100644 --- a/libntfs/lcnalloc.c +++ b/libntfs/lcnalloc.c @@ -273,23 +273,16 @@ runlist *ntfs_cluster_alloc(ntfs_volume *vol, s64 count, LCN start_lcn, __FUNCTION__); continue; } - /* Allocate the bitmap bit. */ - *byte |= bit; - /* We need to write this bitmap buffer back to disk! */ - need_writeback = 1; - Dprintf("%s(): *byte = 0x%x, need_writeback is set.\n", - __FUNCTION__, (unsigned int)*byte); /* Reallocate memory if necessary. */ - if ((rlpos + 2) * (int)sizeof(runlist) >= rlsize) { + if ((rlpos + 3) * (int)sizeof(runlist) >= rlsize) { Dprintf("%s(): Reallocating space.\n", __FUNCTION__); - /* Setup first free bit return value. */ - if (!rl) { - start_lcn = lcn + bmp_pos; - Dprintf("%s(): start_lcn = 0x%llx.\n", + if (!rl) + Dprintf("%s(): First free bit is at " + "LCN = 0x%llx.\n", __FUNCTION__, - (long long)start_lcn); - } + (long long)(lcn + + bmp_pos)); rlsize += 4096; trl = (runlist*)realloc(rl, rlsize); if (!trl) { @@ -305,6 +298,12 @@ runlist *ntfs_cluster_alloc(ntfs_volume *vol, s64 count, LCN start_lcn, "0x%x.\n", __FUNCTION__, rlsize); } + /* Allocate the bitmap bit. */ + *byte |= bit; + /* We need to write this bitmap buffer back to disk! */ + need_writeback = 1; + Dprintf("%s(): *byte = 0x%x, need_writeback is set.\n", + __FUNCTION__, (unsigned int)*byte); /* * Coalesce with previous run if adjacent LCNs. * Otherwise, append a new run. @@ -775,6 +774,10 @@ err_ret: (long long)rl[0].lcn, (long long)count - clusters); } + /* Add runlist terminator element. */ + rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length; + rl[rlpos].lcn = LCN_ENOENT; + rl[rlpos].length = 0; /* Deallocate all allocated clusters. */ Dprintf("%s(): Deallocating allocated clusters.\n", __FUNCTION__);