From 910855a174cfcb7cb123d482bfdb96fd97019a8d Mon Sep 17 00:00:00 2001 From: "(none)!yura" <(none)!yura> Date: Mon, 2 Aug 2004 16:56:34 +0000 Subject: [PATCH] * bugfix for ntfs_cluster_alloc when @count = 0. * add ntfs_clster_free_from_rl and make ntfs_cluster_alloc use it (Logical change 1.468) --- libntfs/lcnalloc.c | 55 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/libntfs/lcnalloc.c b/libntfs/lcnalloc.c index a6c8501f..f2911020 100644 --- a/libntfs/lcnalloc.c +++ b/libntfs/lcnalloc.c @@ -2,6 +2,7 @@ * lcnalloc.c - Cluster (de)allocation code. Part of the Linux-NTFS project. * * Copyright (c) 2002-2004 Anton Altaparmakov + * Copyright (c) 2004 Yura Pakhuchiy * * This program/include file is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as published @@ -39,6 +40,7 @@ * @count: number of clusters to allocate * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none) * @zone: zone from which to allocate the clusters + * @start_vcn: * * Allocate @count clusters preferably starting at cluster @start_lcn or at the * current allocator position if @start_lcn is -1, on the mounted ntfs volume @@ -109,6 +111,24 @@ runlist *ntfs_cluster_alloc(ntfs_volume *vol, s64 count, LCN start_lcn, errno = EINVAL; return NULL; } + + /* Return empty runlist if @count == 0 */ + if (!count) { + rl = malloc (0x1000); + if (!rl) + return NULL; + rlpos = 0; + if (start_vcn) { + rl[0].vcn = 0; + rl[0].lcn = LCN_RL_NOT_MAPPED; + rl[0].length = start_vcn; + rlpos++; + } + rl[rlpos].vcn = start_vcn; + rl[rlpos].lcn = LCN_ENOENT; + rl[rlpos].length = 0; + return rl; + } /* Allocate memory. */ buf = (u8*)malloc(8192); @@ -754,16 +774,10 @@ err_ret: (long long)rl[0].lcn, (long long)count - clusters); } -//FIXME: We don't have an attribute just a run list here! Also rl is not -// terminated at this moment in time! (AIA) -#if 0 /* Deallocate all allocated clusters. */ Dprintf("%s(): Deallocating allocated clusters.\n", __FUNCTION__); - ntfs_cluster_free(vol, attrib_with_rl, 0, -1); -#endif - fprintf(stderr, "%s(): Eeek! Leaving inconsistent metadata.\n", - __FUNCTION__); + ntfs_cluster_free_from_rl (vol, rl); /* Free the runlist. */ free(rl); rl = NULL; @@ -779,6 +793,33 @@ err_ret: goto done_err_ret; } +/** + * ntfs_cluster_free_from_rl - free clusters from runlist + * @vol: mounted ntfs volume on which to free the clusters + * @rl: runlist from which deallocate clusters + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_cluster_free_from_rl(ntfs_volume *vol, runlist *rl) +{ + while (rl->length) { + if (rl->lcn < 0) { + rl++; + continue; + } + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, + rl->lcn, rl->length)) { + int eo = errno; + fprintf(stderr, "%s(): Eeek! Deallocation of " + "clusters failed.\n", __FUNCTION__); + errno = eo; + return -1; + } + rl++; + } + return 0; +} + /** * ntfs_cluster_free - free clusters on an ntfs volume * @vol: mounted ntfs volume on which to free the clusters