1N/A * lcnalloc.c - Cluster (de)allocation code. Part of the Linux-NTFS project. 1N/A * Copyright (c) 2002-2004 Anton Altaparmakov 1N/A * Copyright (c) 2004 Yura Pakhuchiy 1N/A * modify it under the terms of the GNU General Public License as published 1N/A * by the Free Software Foundation; either version 2 of the License, or 1N/A * (at your option) any later version. 1N/A * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 1N/A * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1N/A * GNU General Public License for more details. 1N/A * You should have received a copy of the GNU General Public License 1N/A * along with this program (in the main directory of the Linux-NTFS 1N/A * distribution in the file COPYING); if not, write to the Free Software 1N/A * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1N/A * ntfs_cluster_alloc - allocate clusters on an ntfs volume 1N/A * @vol: mounted ntfs volume on which to allocate the clusters 1N/A * @start_vcn: vcn to use for the first allocated cluster 1N/A * @count: number of clusters to allocate 1N/A * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none) 1N/A * @zone: zone from which to allocate the clusters 1N/A * Allocate @count clusters preferably starting at cluster @start_lcn or at the 1N/A * current allocator position if @start_lcn is -1, on the mounted ntfs volume 1N/A * @vol. @zone is either DATA_ZONE for allocation of normal clusters and 1N/A * MFT_ZONE for allocation of clusters for the master file table, i.e. the 1N/A * $MFT/$DATA attribute. 1N/A * On success return a runlist describing the allocated cluster(s). 1N/A * On error return NULL with errno set to the error code. 1N/A * Notes on the allocation algorithm 1N/A * ================================= 1N/A * There are two data zones. First is the area between the end of the mft zone 1N/A * and the end of the volume, and second is the area between the start of the 1N/A * volumes, the second data zone doesn't exist due to the mft zone being 1N/A * expanded to cover the start of the volume in order to reserve space for the 1N/A * mft bitmap attribute. 1N/A * This is not the prettiest function but the complexity stems from the need of 1N/A * implementing the mft vs data zoned approach and from the fact that we have 1N/A * access to the lcn bitmap in portions of up to 8192 bytes at a time, so we 1N/A * need to cope with crossing over boundaries of two buffers. Further, the fact 1N/A * that the allocator allows for caller supplied hints as to the location of 1N/A * where allocation should begin and the fact that the allocator keeps track of 1N/A * where in the data zones the next natural allocation should occur, contribute 1N/A * to the complexity of the function. But it should all be worthwhile, because 1N/A * this allocator should: 1) be a full implementation of the MFT zone approach 1N/A * used by Windows, 2) cause reduction in fragmentation as much as possible, 1N/A * and 3) be speedy in allocations (the code is not optimized for speed, but 1N/A * the algorithm is, so further speed improvements are probably possible). 1N/A * FIXME: We should be monitoring cluster allocation and increment the MFT zone 1N/A * size dynamically but this is something for the future. We will just cause 1N/A * heavier fragmentation by not doing it and I am not even sure Windows would 1N/A * grow the MFT zone dynamically, so it might even be correct not to do this. 1N/A * The overhead in doing dynamic MFT zone expansion would be very large and 1N/A * unlikely worth the effort. (AIA) 1N/A * TODO: I have added in double the required zone position pointer wrap around 1N/A * logic which can be optimized to having only one of the two logic sets. 1N/A * However, having the double logic will work fine, but if we have only one of 1N/A * the sets and we get it wrong somewhere, then we get into trouble, so 1N/A * removing the duplicate logic requires _very_ careful consideration of _all_ 1N/A * possible code paths. So at least for now, I am leaving the double logic - 1N/A * better safe than sorry... (AIA) 1N/A /* Return empty runlist if @count == 0 */ 1N/A /* Allocate memory. */ 1N/A * If no specific @start_lcn was requested, use the current data zone 1N/A * position, otherwise use the requested @start_lcn but make sure it 1N/A * lies outside the mft zone. Also set done_zones to 0 (no zones done) 1N/A * and pass depending on whether we are starting inside a zone (1) or 1N/A * at the beginning of a zone (2). If requesting from the MFT_ZONE, 1N/A * we either start at the current position within the mft zone or at 1N/A * the specified position. If the latter is out of bounds then we start 1N/A * at the beginning of the MFT_ZONE. 1N/A * zone_start and zone_end are the current search range. search_zone 1N/A * is 1 for mft zone, 2 for data zone 1 (end of mft zone till end of 1N/A * volume) and 4 for data zone 2 (start of volume till start of mft 1N/A * Zone starts at beginning of volume which means a 1N/A * single pass is sufficient. 1N/A * Starting at beginning of data1_zone which means a single 1N/A * pass in this zone is sufficient. 1N/A * Starting at beginning of volume which means a single pass 1N/A }
else /* if (zone == DATA_ZONE) */ {
1N/A /* Skip searching the mft zone. */ 1N/A * bmp_pos is the current bit position inside the bitmap. We use 1N/A * bmp_initial_pos to determine whether or not to do a zone switch. 1N/A /* Loop until all clusters are allocated, i.e. clusters == 0. */ 1N/A "search_zone = %i, pass = %i, zone_start = " 1N/A "0x%llx, zone_end = 0x%llx, bmp_initial_pos = " 1N/A "0x%llx, bmp_pos = 0x%llx, rlpos = %i, rlsize = " 1N/A /* Loop until we run out of free clusters. */ 1N/A /* Reached end of attribute. */ 1N/A "to zone_pass_done.\n");
1N/A * We might have read less than 8192 bytes if we are close to 1N/A * the end of the attribute. 1N/A "0x%llx, bmp_pos = 0x%llx, need_writeback = %i.\n",
1N/A "0x%llx, bmp_pos = 0x%llx, " 1N/A "need_writeback = %i, byte ofs = 0x%x, " 1N/A /* Skip full bytes. */ 1N/A /* If the bit is already set, go onto the next one. */ 1N/A /* Reallocate memory if necessary. */ 1N/A "going to wb_err_ret.\n");
1N/A /* Allocate the bitmap bit. */ 1N/A /* We need to write this bitmap buffer back to disk! */ 1N/A * Coalesce with previous run if adjacent LCNs. 1N/A * Otherwise, append a new run. 1N/A "prev_lcn = 0x%llx, lcn = 0x%llx, " 1N/A "bmp_pos = 0x%llx, prev_run_len = " 1N/A "0x%llx, rlpos = %i.\n",
1N/A "prev_run_len = 0x%llx.\n",
1N/A "run lcn 0x%llx, len 0x%llx).\n",
1N/A * Update the current zone position. Positions 1N/A * of already scanned zones have been updated 1N/A * during the respective zone switches. 1N/A "position, tc = 0x%llx, search_zone = %i.\n",
1N/A "0x%llx, bmp_pos = 0x%llx, need_writeback = %i.\n",
1N/A "buffer code path with error code %i.\n",
err);
1N/A "0x%llx, zone_end = 0x%llx.\n",
1N/A * Now do pass 2, scanning the first part of the zone 1N/A * we omitted in pass 1. 1N/A case 1:
/* mft_zone */ 1N/A case 2:
/* data1_zone */ 1N/A case 4:
/* data2_zone */ 1N/A "zone_start = 0x%llx, zone_end = " 1N/A "0x%llx, bmp_pos = 0x%llx.\n",
1N/A "before = 0x%x, done_zones after = 0x%x.\n",
1N/A /* Now switch to the next zone we haven't done yet. */ 1N/A /* Update mft zone position. */ 1N/A /* Switch from mft zone to data1 zone. */ 1N/A /* Update data1 zone position. */ 1N/A /* Switch from data1 zone to data2 zone. */ 1N/A /* Update data2 zone position. */ 1N/A /* Switch from data2 zone to data1 zone. */ 1N/A "%i, bmp_initial_pos = 0x%llx, " 1N/A "zone_start = 0x%llx, zone_end = " 1N/A "done_zones_check.\n");
1N/A /* Empty zone. Don't bother searching it. */ 1N/A }
/* done_zones == 7 */ 1N/A * All zones are finished! If DATA_ZONE, shrink mft zone. If 1N/A * MFT_ZONE, we have really run out of space. 1N/A "0x%llx, mft_zone_size = 0x%llx.\n",
1N/A /* Really no more space left on device. */ 1N/A }
/* zone == DATA_ZONE && mft_zone_size > 0 */ 1N/A else /* mft zone and data2 zone no longer exist. */ 1N/A "vol->mft_zone_start = 0x%llx, " 1N/A "vol->mft_zone_end = 0x%llx, vol->mft_zone_pos " 1N/A "= 0x%llx, search_zone = 2, pass = 2, " 1N/A "dones_zones = 0x%x, zone_start = 0x%llx, " 1N/A "zone_end = 0x%llx, vol->data1_zone_pos = " 1N/A "0x%llx, continuing outer while loop.\n",
1N/A /* Add runlist terminator element. */ 1N/A "with error code %i.\n",
err);
1N/A "with error code %i.\n",
err);
1N/A "allocate up to = 0x%llx clusters.\n",
1N/A /* Add runlist terminator element. */ 1N/A /* Deallocate all allocated clusters. */ 1N/A /* Free the runlist. */ 1N/A "free lcn = 0x%llx.\n",
1N/A * ntfs_cluster_free_from_rl - free clusters from runlist 1N/A * @vol: mounted ntfs volume on which to free the clusters 1N/A * @rl: runlist from which deallocate clusters 1N/A * On success return 0 and on error return -1 with errno set to the error code. 1N/A * ntfs_cluster_free - free clusters on an ntfs volume 1N/A * @vol: mounted ntfs volume on which to free the clusters 1N/A * @na: attribute whose runlist describes the clusters to free 1N/A * @start_vcn: vcn in @rl at which to start freeing clusters 1N/A * @count: number of clusters to free or -1 for all clusters 1N/A * Free @count clusters starting at the cluster @start_vcn in the runlist 1N/A * described by the attribute @na from the mounted ntfs volume @vol. 1N/A * If @count is -1, all clusters from @start_vcn to the end of the runlist 1N/A * On success return the number of deallocated clusters (not counting sparse 1N/A * clusters) and on error return -1 with errno set to the error code. 1N/A /* Find the starting cluster inside the run that needs freeing. */ 1N/A /* The number of clusters in this run that need freeing. */ 1N/A /* Do the actual freeing of the clusters in this run. */ 1N/A /* We have freed @to_free real clusters. */ 1N/A /* No real clusters were freed. */ 1N/A /* Go to the next run and adjust the number of clusters left to free. */ 1N/A * Loop over the remaining runs, using @count as a capping value, and 1N/A // FIXME: Need to try ntfs_attr_map_runlist() for attribute 1N/A // list support! (AIA) 1N/A // FIXME: Eeek! We need rollback! (AIA) 1N/A "to map runlist! Leaving inconsistent " 1N/A /* The number of clusters in this run that need freeing. */ 1N/A /* Do the actual freeing of the clusters in the run. */ 1N/A // FIXME: Eeek! We need rollback! (AIA) 1N/A "Leaving inconsistent metadata!\n");
1N/A /* We have freed @to_free real clusters. */ 1N/A // FIXME: Eeek! BUG() 1N/A "inconsistent metadata!\n", (
long long)
count);
1N/A /* Done. Return the number of actual clusters that were freed. */