1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/* $NetBSD$ */
/*
* File "udf_bmap.c" is part of the UDFclient toolkit.
* File $Id: udf_bmap.c,v 1.28 2016/04/25 21:01:40 reinoud Exp $ $Name: $
*
* Copyright (c) 2003, 2004, 2005, 2006, 2011
* Reinoud Zandijk <reinoud@netbsd.org>
* All rights reserved.
*
* The UDFclient toolkit is distributed under the Clarified Artistic Licence.
* A copy of the licence is included in the distribution as
* `LICENCE.clearified.artistic' and a copy of the licence can also be
* requested at the GNU foundantion's website.
*
* Visit the UDFclient toolkit homepage http://www.13thmonkey.org/udftoolkit/
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* XXX strip list to bare minimum XXX */
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <dirent.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <time.h>
#include "uscsilib.h"
/* for locals */
#include "udf.h"
#include "udf_bswap.h"
#include "udf_discop.h"
#include "uio.h"
#include <pthread.h>
#ifndef MAX
# define MAX(a,b) ((a)>(b)?(a):(b))
# define MIN(a,b) ((a)<(b)?(a):(b))
#endif
//#define DEBUG(a) { a; }
#define DEBUG(a) if (0) { a; }
void udf_dump_allocentry_queue(char *msg, struct udf_alloc_entries *queue, uint32_t lb_size);
/******************************************************************************************
*
* Get and release free logical blocks from the free space tables
*
******************************************************************************************/
/*
* simple metadata distribution algorithm; first part of each block is
* metadata followed by data; the offset this blocking can be tweaked by
* part_offset.
*/
static int udf_allocate_lbs_on_rewritables(struct udf_partition *udf_partition, uint64_t part_offset, uint64_t metadata_granularity, uint32_t lb_size, uint64_t req_size, int content, uint32_t *res_start_lb, uint32_t *res_num_lbs) {
struct udf_allocentry *alloc_entry, *chosen;
struct udf_alloc_entries *queue;
uint64_t start;
uint64_t chosen_offset;
uint64_t size;
int error, is_meta;
/* allways unalloc_space_queue ? */
queue = &udf_partition->unalloc_space_queue;
/* TODO this is a simplification; no multiple queues yet XXX */
is_meta = (content != UDF_C_USERDATA);
/* start from the beginning of the unallocated space queue */
alloc_entry = TAILQ_FIRST(queue);
start = alloc_entry->lb_num;
assert(start == 0);
chosen = NULL;
chosen_offset = 0;
size = 0;
TAILQ_FOREACH(alloc_entry, queue, next_alloc) {
/* we can only start on lb_size (extra sanity check) */
assert(start % lb_size == 0);
/* piecewise if nessisary */
size = MIN(alloc_entry->len, req_size);
size = (uint64_t) lb_size * (size / lb_size);
if (size == 0) continue;
/* we can only give out on lb_size boundaries */
assert(size % lb_size == 0);
if (alloc_entry->flags == UDF_SPACE_FREE) {
DEBUG(printf("got free entry from %"PRIu64" + %"PRIu64"\n", start / lb_size, (start + alloc_entry->len)/lb_size));
if (is_meta) {
if (start + req_size + part_offset <= metadata_granularity) {
chosen = alloc_entry;
chosen_offset = 0;
}
} else {
chosen_offset = 0;
if (start + part_offset < metadata_granularity) {
chosen_offset = metadata_granularity - (start + part_offset);
}
if (chosen_offset + size <= alloc_entry->len) {
chosen = alloc_entry;
}
}
DEBUG(if (chosen && (size > 0)) printf("chosen\n"));
}
if (size <= 0) chosen = NULL;
if (chosen) break; /* foreach */
start = start + alloc_entry->len;
} /* FOREACH */
if (!chosen) return ENOSPC;
assert(size > 0);
assert(size % lb_size == 0);
assert(chosen->len % lb_size == 0);
assert(start + chosen_offset + size <= start + chosen->len);
start += chosen_offset;
*res_start_lb = start / lb_size;
*res_num_lbs = size / lb_size;
error = udf_mark_allocentry_queue(queue, lb_size, start, size, UDF_SPACE_ALLOCATED, NULL, NULL);
return error;
}
/* get at most req_lbs from the partition and mark the area used */
static int udf_allocate_lbs_on_partition(struct udf_log_vol *udf_log_vol, uint16_t vpart_num, uint32_t req_lbs, int content, uint32_t *res_start_lb, uint32_t *res_num_lbs) {
struct udf_partition *udf_partition;
struct udf_part_mapping *udf_part_mapping;
struct udf_alloc_entries *queue;
uint64_t part_length;
uint64_t metadata_granularity, metadata_blk_len;
uint64_t part_offset;
uint32_t lb_size, num_lbs;
uint64_t req_size;
uint32_t cnt;
int error;
assert(udf_log_vol);
lb_size = udf_log_vol->lb_size;
error = udf_logvol_vpart_to_partition(udf_log_vol, vpart_num, &udf_part_mapping, &udf_partition);
if (error) return error;
// part_start = (uint64_t) lb_size * udf_rw32(udf_partition->partition->start_loc);
part_length = (uint64_t) lb_size * udf_rw32(udf_partition->partition->part_len);
req_size = (uint64_t) lb_size * req_lbs;
/* lock against corruption with free */
UDF_MUTEX_LOCK(&udf_partition->partition_space_mutex);
switch (udf_part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_PHYSICAL :
case UDF_PART_MAPPING_SPARABLE :
/* sparable is just like the physical mapping */
/* allways unalloc_space_queue ? */
queue = &udf_partition->unalloc_space_queue;
udf_merge_allocentry_queue(queue, lb_size);
DEBUG(udf_dump_allocentry_queue("Alloc ", queue, lb_size));
metadata_granularity = part_length / 1024;
metadata_blk_len = metadata_granularity / 16;
part_offset = 0;
for (cnt = 0; cnt <= (metadata_granularity / metadata_blk_len); cnt++) {
num_lbs = 0;
error = udf_allocate_lbs_on_rewritables(udf_partition, part_offset, metadata_granularity, lb_size, req_size, content, res_start_lb, &num_lbs);
if (error) {
assert(error == ENOSPC);
/* disc is most likely getting full with (meta) data; shift window */
part_offset += metadata_blk_len;
num_lbs = 0;
} else {
assert(num_lbs >= 1);
udf_partition->free_unalloc_space -= num_lbs * lb_size;
udf_log_vol->free_space -= num_lbs * lb_size;
break; /* for */
}
}
*res_num_lbs = num_lbs;
break;
case UDF_PART_MAPPING_VIRTUAL :
/* strict increasing virtual adress giveout */
printf("UDF: get lbs from virtual partition mapping not implemented yet\n");
return EBADF;
case UDF_PART_MAPPING_META :
/* select blobs from the metadata file */
if (udf_part_mapping->meta_bitmap_file == NULL)
return EROFS;
printf("UDF: get lbs from metadata partition mapping not implemented yet\n");
break;
case UDF_PART_MAPPING_PSEUDO_RW :
/* strict increasing adress giveout from the pseudo over. track */
printf("UDF: get lbs from pseudo overwritable partition not implemented yet\n");
break;
}
/* sanity */
if (*res_num_lbs == 0)
*res_start_lb = 0;
/* release partition unalloced and freed space again */
UDF_MUTEX_UNLOCK(&udf_partition->partition_space_mutex);
return error;
}
/******************************************************************************************
*
* Upper level free space allocation and releasing.
*
******************************************************************************************/
int udf_allocate_lbs(struct udf_log_vol *udf_log_vol, int content, uint32_t req_lbs, char *what, uint16_t *res_vpart_num, uint32_t *res_start_lb, uint32_t *res_num_lbs) {
struct udf_partition *udf_partition;
struct udf_part_mapping *udf_part_mapping;
uint32_t num_lbs;
uint16_t vpart_num;
int is_meta, ok;
int error;
assert(udf_log_vol);
num_lbs = 0; /* shutup gcc */
/* select udf partition to write to depending on the contents */
is_meta = ((content == UDF_C_FIDS) || (content == UDF_C_NODE));
vpart_num = is_meta ? udf_log_vol->metadata_vpart : udf_log_vol->data_vpart;
/* TODO what about when space is freed in an earlier partition ? reset the vpart's ? */
DEBUG(printf("UDF allocate %d lbs for node `%s`\n", req_lbs, what));
do {
DEBUG(printf("do: vpart_num = %d\n", vpart_num));
error = udf_logvol_vpart_to_partition(udf_log_vol, vpart_num, &udf_part_mapping, &udf_partition);
ok = (udf_partition != NULL) && (udf_part_mapping != NULL);
if (ok && !is_meta) ok = udf_part_mapping->data_writable;
if (ok && is_meta) ok = udf_part_mapping->metadata_writable;
if (ok) {
/* try to snoop from this vpart */
error = udf_allocate_lbs_on_partition(udf_log_vol, vpart_num, req_lbs, content, res_start_lb, &num_lbs);
if (!error) {
*res_vpart_num = vpart_num;
if (res_num_lbs) *res_num_lbs = num_lbs;
}
ok = !error;
}
if (!ok) {
DEBUG(printf("advance vpart\n"));
vpart_num = is_meta ? ++(udf_log_vol->metadata_vpart) : ++(udf_log_vol->data_vpart);
if (vpart_num >= udf_log_vol->num_part_mappings) {
udf_log_vol->metadata_vpart = udf_log_vol->data_vpart = 0;
printf("UDF: logvol discs full ?\n");
return ENOSPC;
}
}
} while (!ok);
DEBUG(printf("udf_allocate_lbs: got space on vpart_num = %d; req %d, got %d\n", vpart_num, req_lbs, num_lbs));
assert((*res_start_lb != 0) && (num_lbs != 0));
return 0;
}
int udf_node_allocate_lbs(struct udf_node *udf_node, int req_lbs, uint16_t *res_vpart_num, uint32_t *res_start_lb, uint32_t *res_num_lbs) {
char *what;
int content;
/* content can be USERDATA or FID stream here; checking on udf filetype */
switch (udf_node->udf_filetype) {
case UDF_ICB_FILETYPE_DIRECTORY :
case UDF_ICB_FILETYPE_STREAMDIR :
content = UDF_C_FIDS;
what = "FID stream";
break;
default :
content = UDF_C_USERDATA;
what = "file content";
break;
}
return udf_allocate_lbs(udf_node->udf_log_vol, content, req_lbs, what, res_vpart_num, res_start_lb, res_num_lbs);
}
/* returns non zero if space is available */
int udf_confirm_freespace(struct udf_log_vol *udf_log_vol, int content, uint64_t size) {
content = content; /* not used now */
/* generic check for now */
if (udf_log_vol->free_space >= udf_log_vol->await_alloc_space + size + UDF_MINFREE_LOGVOL) {
return 1;
}
return 0;
}
int udf_release_lbs(struct udf_log_vol *udf_log_vol, uint16_t vpart_num, uint32_t lb_num, uint64_t size) {
struct udf_partition *udf_partition;
struct udf_part_mapping *udf_part_mapping;
struct udf_alloc_entries *queue;
uint32_t lb_size;
int error;
if (!udf_log_vol) return 0;
lb_size = udf_log_vol->lb_size;
error = udf_logvol_vpart_to_partition(udf_log_vol, vpart_num, &udf_part_mapping, &udf_partition);
if (error) return error;
/* space can only be freed in lb_size chuncks by definition */
size = (uint64_t) lb_size * ((size + lb_size -1) / lb_size);
switch (udf_part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_PHYSICAL :
case UDF_PART_MAPPING_SPARABLE :
/* sparable is just like the physical mapping */
queue = &udf_partition->unalloc_space_queue; /* TODO freed- for non sequential MO media */
UDF_MUTEX_LOCK(&udf_partition->partition_space_mutex);
error = udf_mark_allocentry_queue(queue, lb_size, (uint64_t) lb_num * lb_size, size, UDF_SPACE_FREE, NULL, NULL);
udf_partition->free_unalloc_space += size;
udf_log_vol->free_space += size;
UDF_MUTEX_UNLOCK(&udf_partition->partition_space_mutex);
return error;
case UDF_PART_MAPPING_VIRTUAL :
/* freeing is not applicable */
return 0;
case UDF_PART_MAPPING_META :
/* free blobs in the metadata file */
printf("UDF: freeing lbs from metadata partition mapping not implemented yet\n");
break;
case UDF_PART_MAPPING_PSEUDO_RW :
/* do we have to keep a space bitmap? */
printf("UDF: freeing lbs from pseudo rewritable partition mapping not implemented yet\n");
break;
}
return 0;
}
/* !!! needs to be called with alloc entry mutex held !!! */
int udf_node_release_extent(struct udf_node *udf_node, uint64_t from, uint64_t to) {
struct udf_allocentry *from_ae, *to_ae, *alloc_entry, *last_alloc_entry;
uint32_t lb_size, lbnum, len;
uint16_t vpart;
int error, flags;
assert(udf_node->alloc_mutex.locked);
assert(udf_node->udf_log_vol);
lb_size = udf_node->udf_log_vol->lb_size;
error = udf_splitup_allocentry_queue(&udf_node->alloc_entries, lb_size, from, to-from, &from_ae, &to_ae);
if (!error) {
alloc_entry = from_ae;
last_alloc_entry = TAILQ_NEXT(to_ae, next_alloc);
while (alloc_entry != last_alloc_entry) {
vpart = alloc_entry->vpart_num;
lbnum = alloc_entry->lb_num;
flags = alloc_entry->flags;
len = alloc_entry->len;
if (flags == UDF_SPACE_ALLOCATED) {
error = udf_release_lbs(udf_node->udf_log_vol, vpart, lbnum, len);
assert(!error);
alloc_entry->flags = UDF_SPACE_FREE; /* WORM: or freed? */
} else {
DEBUG(printf("udf_filepart_free_extent :freeing a non allocated piece : flags = %d\n", flags));
}
alloc_entry = TAILQ_NEXT(alloc_entry, next_alloc);
}
} else {
fprintf(stderr, "udf_filepart_free_extent: splitup failed\n");
}
return 0;
}
/* end of udf_bmap.c */
|