| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. |
| 5 |
* |
| 6 |
* This code builds two trees of free clusters extents. |
| 7 |
* Trees are sorted by start of extent and by length of extent. |
| 8 |
* NTFS_MAX_WND_EXTENTS defines the maximum number of elements in trees. |
| 9 |
* In extreme case code reads on-disk bitmap to find free clusters. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <linux/buffer_head.h> |
| 14 |
#include <linux/fs.h> |
| 15 |
#include <linux/kernel.h> |
| 16 |
|
| 17 |
#include "ntfs.h" |
| 18 |
#include "ntfs_fs.h" |
| 19 |
|
| 20 |
/* |
| 21 |
* Maximum number of extents in tree. |
| 22 |
*/ |
| 23 |
#define NTFS_MAX_WND_EXTENTS (32u * 1024u) |
| 24 |
|
| 25 |
struct rb_node_key { |
| 26 |
struct rb_node node; |
| 27 |
size_t key; |
| 28 |
}; |
| 29 |
|
| 30 |
struct e_node { |
| 31 |
struct rb_node_key start; /* Tree sorted by start. */ |
| 32 |
struct rb_node_key count; /* Tree sorted by len. */ |
| 33 |
}; |
| 34 |
|
| 35 |
static int wnd_rescan(struct wnd_bitmap *wnd); |
| 36 |
static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw); |
| 37 |
static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits); |
| 38 |
|
| 39 |
static struct kmem_cache *ntfs_enode_cachep; |
| 40 |
|
| 41 |
int __init ntfs3_init_bitmap(void) |
| 42 |
{ |
| 43 |
ntfs_enode_cachep = kmem_cache_create("ntfs3_enode_cache", |
| 44 |
sizeof(struct e_node), 0, |
| 45 |
SLAB_RECLAIM_ACCOUNT, NULL); |
| 46 |
return ntfs_enode_cachep ? 0 : -ENOMEM; |
| 47 |
} |
| 48 |
|
| 49 |
void ntfs3_exit_bitmap(void) |
| 50 |
{ |
| 51 |
kmem_cache_destroy(ntfs_enode_cachep); |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* wnd_scan |
| 56 |
* |
| 57 |
* b_pos + b_len - biggest fragment. |
| 58 |
* Scan range [wpos wbits) window @buf. |
| 59 |
* |
| 60 |
* Return: -1 if not found. |
| 61 |
*/ |
| 62 |
static size_t wnd_scan(const void *buf, size_t wbit, u32 wpos, u32 wend, |
| 63 |
size_t to_alloc, size_t *prev_tail, size_t *b_pos, |
| 64 |
size_t *b_len) |
| 65 |
{ |
| 66 |
while (wpos < wend) { |
| 67 |
size_t free_len; |
| 68 |
u32 free_bits, end; |
| 69 |
u32 used = find_next_zero_bit_le(buf, wend, wpos); |
| 70 |
|
| 71 |
if (used >= wend) { |
| 72 |
if (*b_len < *prev_tail) { |
| 73 |
*b_pos = wbit - *prev_tail; |
| 74 |
*b_len = *prev_tail; |
| 75 |
} |
| 76 |
|
| 77 |
*prev_tail = 0; |
| 78 |
return -1; |
| 79 |
} |
| 80 |
|
| 81 |
if (used > wpos) { |
| 82 |
wpos = used; |
| 83 |
if (*b_len < *prev_tail) { |
| 84 |
*b_pos = wbit - *prev_tail; |
| 85 |
*b_len = *prev_tail; |
| 86 |
} |
| 87 |
|
| 88 |
*prev_tail = 0; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Now we have a fragment [wpos, wend) staring with 0. |
| 93 |
*/ |
| 94 |
end = wpos + to_alloc - *prev_tail; |
| 95 |
free_bits = find_next_bit_le(buf, min(end, wend), wpos); |
| 96 |
|
| 97 |
free_len = *prev_tail + free_bits - wpos; |
| 98 |
|
| 99 |
if (*b_len < free_len) { |
| 100 |
*b_pos = wbit + wpos - *prev_tail; |
| 101 |
*b_len = free_len; |
| 102 |
} |
| 103 |
|
| 104 |
if (free_len >= to_alloc) |
| 105 |
return wbit + wpos - *prev_tail; |
| 106 |
|
| 107 |
if (free_bits >= wend) { |
| 108 |
*prev_tail += free_bits - wpos; |
| 109 |
return -1; |
| 110 |
} |
| 111 |
|
| 112 |
wpos = free_bits + 1; |
| 113 |
|
| 114 |
*prev_tail = 0; |
| 115 |
} |
| 116 |
|
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
/* |
| 121 |
* wnd_close - Frees all resources. |
| 122 |
*/ |
| 123 |
void wnd_close(struct wnd_bitmap *wnd) |
| 124 |
{ |
| 125 |
struct rb_node *node, *next; |
| 126 |
|
| 127 |
kvfree(wnd->free_bits); |
| 128 |
wnd->free_bits = NULL; |
| 129 |
run_close(&wnd->run); |
| 130 |
|
| 131 |
node = rb_first(&wnd->start_tree); |
| 132 |
|
| 133 |
while (node) { |
| 134 |
next = rb_next(node); |
| 135 |
rb_erase(node, &wnd->start_tree); |
| 136 |
kmem_cache_free(ntfs_enode_cachep, |
| 137 |
rb_entry(node, struct e_node, start.node)); |
| 138 |
node = next; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static struct rb_node *rb_lookup(struct rb_root *root, size_t v) |
| 143 |
{ |
| 144 |
struct rb_node **p = &root->rb_node; |
| 145 |
struct rb_node *r = NULL; |
| 146 |
|
| 147 |
while (*p) { |
| 148 |
struct rb_node_key *k; |
| 149 |
|
| 150 |
k = rb_entry(*p, struct rb_node_key, node); |
| 151 |
if (v < k->key) { |
| 152 |
p = &(*p)->rb_left; |
| 153 |
} else if (v > k->key) { |
| 154 |
r = &k->node; |
| 155 |
p = &(*p)->rb_right; |
| 156 |
} else { |
| 157 |
return &k->node; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return r; |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* rb_insert_count - Helper function to insert special kind of 'count' tree. |
| 166 |
*/ |
| 167 |
static inline bool rb_insert_count(struct rb_root *root, struct e_node *e) |
| 168 |
{ |
| 169 |
struct rb_node **p = &root->rb_node; |
| 170 |
struct rb_node *parent = NULL; |
| 171 |
size_t e_ckey = e->count.key; |
| 172 |
size_t e_skey = e->start.key; |
| 173 |
|
| 174 |
while (*p) { |
| 175 |
struct e_node *k = |
| 176 |
rb_entry(parent = *p, struct e_node, count.node); |
| 177 |
|
| 178 |
if (e_ckey > k->count.key) { |
| 179 |
p = &(*p)->rb_left; |
| 180 |
} else if (e_ckey < k->count.key) { |
| 181 |
p = &(*p)->rb_right; |
| 182 |
} else if (e_skey < k->start.key) { |
| 183 |
p = &(*p)->rb_left; |
| 184 |
} else if (e_skey > k->start.key) { |
| 185 |
p = &(*p)->rb_right; |
| 186 |
} else { |
| 187 |
WARN_ON(1); |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
rb_link_node(&e->count.node, parent, p); |
| 193 |
rb_insert_color(&e->count.node, root); |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
* rb_insert_start - Helper function to insert special kind of 'count' tree. |
| 199 |
*/ |
| 200 |
static inline bool rb_insert_start(struct rb_root *root, struct e_node *e) |
| 201 |
{ |
| 202 |
struct rb_node **p = &root->rb_node; |
| 203 |
struct rb_node *parent = NULL; |
| 204 |
size_t e_skey = e->start.key; |
| 205 |
|
| 206 |
while (*p) { |
| 207 |
struct e_node *k; |
| 208 |
|
| 209 |
parent = *p; |
| 210 |
|
| 211 |
k = rb_entry(parent, struct e_node, start.node); |
| 212 |
if (e_skey < k->start.key) { |
| 213 |
p = &(*p)->rb_left; |
| 214 |
} else if (e_skey > k->start.key) { |
| 215 |
p = &(*p)->rb_right; |
| 216 |
} else { |
| 217 |
WARN_ON(1); |
| 218 |
return false; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
rb_link_node(&e->start.node, parent, p); |
| 223 |
rb_insert_color(&e->start.node, root); |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* wnd_add_free_ext - Adds a new extent of free space. |
| 229 |
* @build: 1 when building tree. |
| 230 |
*/ |
| 231 |
static void wnd_add_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len, |
| 232 |
bool build) |
| 233 |
{ |
| 234 |
struct e_node *e, *e0 = NULL; |
| 235 |
size_t ib, end_in = bit + len; |
| 236 |
struct rb_node *n; |
| 237 |
|
| 238 |
if (build) { |
| 239 |
/* Use extent_min to filter too short extents. */ |
| 240 |
if (wnd->count >= NTFS_MAX_WND_EXTENTS && |
| 241 |
len <= wnd->extent_min) { |
| 242 |
wnd->uptodated = -1; |
| 243 |
return; |
| 244 |
} |
| 245 |
} else { |
| 246 |
/* Try to find extent before 'bit'. */ |
| 247 |
n = rb_lookup(&wnd->start_tree, bit); |
| 248 |
|
| 249 |
if (!n) { |
| 250 |
n = rb_first(&wnd->start_tree); |
| 251 |
} else { |
| 252 |
e = rb_entry(n, struct e_node, start.node); |
| 253 |
n = rb_next(n); |
| 254 |
if (e->start.key + e->count.key == bit) { |
| 255 |
/* Remove left. */ |
| 256 |
bit = e->start.key; |
| 257 |
len += e->count.key; |
| 258 |
rb_erase(&e->start.node, &wnd->start_tree); |
| 259 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 260 |
wnd->count -= 1; |
| 261 |
e0 = e; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
while (n) { |
| 266 |
size_t next_end; |
| 267 |
|
| 268 |
e = rb_entry(n, struct e_node, start.node); |
| 269 |
next_end = e->start.key + e->count.key; |
| 270 |
if (e->start.key > end_in) |
| 271 |
break; |
| 272 |
|
| 273 |
/* Remove right. */ |
| 274 |
n = rb_next(n); |
| 275 |
len += next_end - end_in; |
| 276 |
end_in = next_end; |
| 277 |
rb_erase(&e->start.node, &wnd->start_tree); |
| 278 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 279 |
wnd->count -= 1; |
| 280 |
|
| 281 |
if (!e0) |
| 282 |
e0 = e; |
| 283 |
else |
| 284 |
kmem_cache_free(ntfs_enode_cachep, e); |
| 285 |
} |
| 286 |
|
| 287 |
if (wnd->uptodated != 1) { |
| 288 |
/* Check bits before 'bit'. */ |
| 289 |
ib = wnd->zone_bit == wnd->zone_end || |
| 290 |
bit < wnd->zone_end ? |
| 291 |
0 : |
| 292 |
wnd->zone_end; |
| 293 |
|
| 294 |
while (bit > ib && wnd_is_free_hlp(wnd, bit - 1, 1)) { |
| 295 |
bit -= 1; |
| 296 |
len += 1; |
| 297 |
} |
| 298 |
|
| 299 |
/* Check bits after 'end_in'. */ |
| 300 |
ib = wnd->zone_bit == wnd->zone_end || |
| 301 |
end_in > wnd->zone_bit ? |
| 302 |
wnd->nbits : |
| 303 |
wnd->zone_bit; |
| 304 |
|
| 305 |
while (end_in < ib && wnd_is_free_hlp(wnd, end_in, 1)) { |
| 306 |
end_in += 1; |
| 307 |
len += 1; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
/* Insert new fragment. */ |
| 312 |
if (wnd->count >= NTFS_MAX_WND_EXTENTS) { |
| 313 |
if (e0) |
| 314 |
kmem_cache_free(ntfs_enode_cachep, e0); |
| 315 |
|
| 316 |
wnd->uptodated = -1; |
| 317 |
|
| 318 |
/* Compare with smallest fragment. */ |
| 319 |
n = rb_last(&wnd->count_tree); |
| 320 |
e = rb_entry(n, struct e_node, count.node); |
| 321 |
if (len <= e->count.key) |
| 322 |
goto out; /* Do not insert small fragments. */ |
| 323 |
|
| 324 |
if (build) { |
| 325 |
struct e_node *e2; |
| 326 |
|
| 327 |
n = rb_prev(n); |
| 328 |
e2 = rb_entry(n, struct e_node, count.node); |
| 329 |
/* Smallest fragment will be 'e2->count.key'. */ |
| 330 |
wnd->extent_min = e2->count.key; |
| 331 |
} |
| 332 |
|
| 333 |
/* Replace smallest fragment by new one. */ |
| 334 |
rb_erase(&e->start.node, &wnd->start_tree); |
| 335 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 336 |
wnd->count -= 1; |
| 337 |
} else { |
| 338 |
e = e0 ? e0 : kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC); |
| 339 |
if (!e) { |
| 340 |
wnd->uptodated = -1; |
| 341 |
goto out; |
| 342 |
} |
| 343 |
|
| 344 |
if (build && len <= wnd->extent_min) |
| 345 |
wnd->extent_min = len; |
| 346 |
} |
| 347 |
e->start.key = bit; |
| 348 |
e->count.key = len; |
| 349 |
if (len > wnd->extent_max) |
| 350 |
wnd->extent_max = len; |
| 351 |
|
| 352 |
rb_insert_start(&wnd->start_tree, e); |
| 353 |
rb_insert_count(&wnd->count_tree, e); |
| 354 |
wnd->count += 1; |
| 355 |
|
| 356 |
out:; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* wnd_remove_free_ext - Remove a run from the cached free space. |
| 361 |
*/ |
| 362 |
static void wnd_remove_free_ext(struct wnd_bitmap *wnd, size_t bit, size_t len) |
| 363 |
{ |
| 364 |
struct rb_node *n, *n3; |
| 365 |
struct e_node *e, *e3; |
| 366 |
size_t end_in = bit + len; |
| 367 |
size_t end3, end, new_key, new_len, max_new_len; |
| 368 |
|
| 369 |
/* Try to find extent before 'bit'. */ |
| 370 |
n = rb_lookup(&wnd->start_tree, bit); |
| 371 |
|
| 372 |
if (!n) |
| 373 |
return; |
| 374 |
|
| 375 |
e = rb_entry(n, struct e_node, start.node); |
| 376 |
end = e->start.key + e->count.key; |
| 377 |
|
| 378 |
new_key = new_len = 0; |
| 379 |
len = e->count.key; |
| 380 |
|
| 381 |
/* Range [bit,end_in) must be inside 'e' or outside 'e' and 'n'. */ |
| 382 |
if (e->start.key > bit) |
| 383 |
; |
| 384 |
else if (end_in <= end) { |
| 385 |
/* Range [bit,end_in) inside 'e'. */ |
| 386 |
new_key = end_in; |
| 387 |
new_len = end - end_in; |
| 388 |
len = bit - e->start.key; |
| 389 |
} else if (bit > end) { |
| 390 |
bool bmax = false; |
| 391 |
|
| 392 |
n3 = rb_next(n); |
| 393 |
|
| 394 |
while (n3) { |
| 395 |
e3 = rb_entry(n3, struct e_node, start.node); |
| 396 |
if (e3->start.key >= end_in) |
| 397 |
break; |
| 398 |
|
| 399 |
if (e3->count.key == wnd->extent_max) |
| 400 |
bmax = true; |
| 401 |
|
| 402 |
end3 = e3->start.key + e3->count.key; |
| 403 |
if (end3 > end_in) { |
| 404 |
e3->start.key = end_in; |
| 405 |
rb_erase(&e3->count.node, &wnd->count_tree); |
| 406 |
e3->count.key = end3 - end_in; |
| 407 |
rb_insert_count(&wnd->count_tree, e3); |
| 408 |
break; |
| 409 |
} |
| 410 |
|
| 411 |
n3 = rb_next(n3); |
| 412 |
rb_erase(&e3->start.node, &wnd->start_tree); |
| 413 |
rb_erase(&e3->count.node, &wnd->count_tree); |
| 414 |
wnd->count -= 1; |
| 415 |
kmem_cache_free(ntfs_enode_cachep, e3); |
| 416 |
} |
| 417 |
if (!bmax) |
| 418 |
return; |
| 419 |
n3 = rb_first(&wnd->count_tree); |
| 420 |
wnd->extent_max = |
| 421 |
n3 ? rb_entry(n3, struct e_node, count.node)->count.key : |
| 422 |
0; |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
if (e->count.key != wnd->extent_max) { |
| 427 |
; |
| 428 |
} else if (rb_prev(&e->count.node)) { |
| 429 |
; |
| 430 |
} else { |
| 431 |
n3 = rb_next(&e->count.node); |
| 432 |
max_new_len = max(len, new_len); |
| 433 |
if (!n3) { |
| 434 |
wnd->extent_max = max_new_len; |
| 435 |
} else { |
| 436 |
e3 = rb_entry(n3, struct e_node, count.node); |
| 437 |
wnd->extent_max = max(e3->count.key, max_new_len); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if (!len) { |
| 442 |
if (new_len) { |
| 443 |
e->start.key = new_key; |
| 444 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 445 |
e->count.key = new_len; |
| 446 |
rb_insert_count(&wnd->count_tree, e); |
| 447 |
} else { |
| 448 |
rb_erase(&e->start.node, &wnd->start_tree); |
| 449 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 450 |
wnd->count -= 1; |
| 451 |
kmem_cache_free(ntfs_enode_cachep, e); |
| 452 |
} |
| 453 |
goto out; |
| 454 |
} |
| 455 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 456 |
e->count.key = len; |
| 457 |
rb_insert_count(&wnd->count_tree, e); |
| 458 |
|
| 459 |
if (!new_len) |
| 460 |
goto out; |
| 461 |
|
| 462 |
if (wnd->count >= NTFS_MAX_WND_EXTENTS) { |
| 463 |
wnd->uptodated = -1; |
| 464 |
|
| 465 |
/* Get minimal extent. */ |
| 466 |
e = rb_entry(rb_last(&wnd->count_tree), struct e_node, |
| 467 |
count.node); |
| 468 |
if (e->count.key > new_len) |
| 469 |
goto out; |
| 470 |
|
| 471 |
/* Replace minimum. */ |
| 472 |
rb_erase(&e->start.node, &wnd->start_tree); |
| 473 |
rb_erase(&e->count.node, &wnd->count_tree); |
| 474 |
wnd->count -= 1; |
| 475 |
} else { |
| 476 |
e = kmem_cache_alloc(ntfs_enode_cachep, GFP_ATOMIC); |
| 477 |
if (!e) |
| 478 |
wnd->uptodated = -1; |
| 479 |
} |
| 480 |
|
| 481 |
if (e) { |
| 482 |
e->start.key = new_key; |
| 483 |
e->count.key = new_len; |
| 484 |
rb_insert_start(&wnd->start_tree, e); |
| 485 |
rb_insert_count(&wnd->count_tree, e); |
| 486 |
wnd->count += 1; |
| 487 |
} |
| 488 |
|
| 489 |
out: |
| 490 |
if (!wnd->count && 1 != wnd->uptodated) |
| 491 |
wnd_rescan(wnd); |
| 492 |
} |
| 493 |
|
| 494 |
/* |
| 495 |
* wnd_rescan - Scan all bitmap. Used while initialization. |
| 496 |
*/ |
| 497 |
static int wnd_rescan(struct wnd_bitmap *wnd) |
| 498 |
{ |
| 499 |
int err = 0; |
| 500 |
size_t prev_tail = 0; |
| 501 |
struct super_block *sb = wnd->sb; |
| 502 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 503 |
u64 lbo, len = 0; |
| 504 |
u32 blocksize = sb->s_blocksize; |
| 505 |
u8 cluster_bits = sbi->cluster_bits; |
| 506 |
u32 wbits = 8 * sb->s_blocksize; |
| 507 |
u32 used, frb; |
| 508 |
size_t wpos, wbit, iw, vbo; |
| 509 |
struct buffer_head *bh = NULL; |
| 510 |
CLST lcn, clen; |
| 511 |
struct file_ra_state *ra; |
| 512 |
struct address_space *mapping = sb->s_bdev->bd_mapping; |
| 513 |
|
| 514 |
wnd->uptodated = 0; |
| 515 |
wnd->extent_max = 0; |
| 516 |
wnd->extent_min = MINUS_ONE_T; |
| 517 |
wnd->total_zeroes = 0; |
| 518 |
|
| 519 |
vbo = 0; |
| 520 |
|
| 521 |
/* Allocate in memory instead of stack. Not critical if failed. */ |
| 522 |
ra = kzalloc_obj(*ra, GFP_NOFS); |
| 523 |
if (ra) { |
| 524 |
file_ra_state_init(ra, mapping); |
| 525 |
ra->ra_pages = (wnd->nbits / 8 + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 526 |
} |
| 527 |
|
| 528 |
for (iw = 0; iw < wnd->nwnd; iw++) { |
| 529 |
if (iw + 1 == wnd->nwnd) |
| 530 |
wbits = wnd->bits_last; |
| 531 |
|
| 532 |
if (wnd->inited) { |
| 533 |
if (!wnd->free_bits[iw]) { |
| 534 |
/* All ones. */ |
| 535 |
if (prev_tail) { |
| 536 |
wnd_add_free_ext(wnd, |
| 537 |
vbo * 8 - prev_tail, |
| 538 |
prev_tail, true); |
| 539 |
prev_tail = 0; |
| 540 |
} |
| 541 |
goto next_wnd; |
| 542 |
} |
| 543 |
if (wbits == wnd->free_bits[iw]) { |
| 544 |
/* All zeroes. */ |
| 545 |
prev_tail += wbits; |
| 546 |
wnd->total_zeroes += wbits; |
| 547 |
goto next_wnd; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (!len) { |
| 552 |
u32 off = vbo & sbi->cluster_mask; |
| 553 |
|
| 554 |
if (!run_lookup_entry(&wnd->run, vbo >> cluster_bits, |
| 555 |
&lcn, &clen, NULL)) { |
| 556 |
err = -ENOENT; |
| 557 |
goto out; |
| 558 |
} |
| 559 |
|
| 560 |
lbo = ((u64)lcn << cluster_bits) + off; |
| 561 |
len = ((u64)clen << cluster_bits) - off; |
| 562 |
} |
| 563 |
|
| 564 |
if (ra) { |
| 565 |
pgoff_t idx = lbo >> PAGE_SHIFT; |
| 566 |
if (!ra_has_index(ra, idx)) |
| 567 |
page_cache_sync_readahead(mapping, ra, NULL, |
| 568 |
idx, 1); |
| 569 |
} |
| 570 |
|
| 571 |
bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits); |
| 572 |
if (!bh) { |
| 573 |
err = -EIO; |
| 574 |
goto out; |
| 575 |
} |
| 576 |
|
| 577 |
used = ntfs_bitmap_weight_le(bh->b_data, wbits); |
| 578 |
if (used < wbits) { |
| 579 |
frb = wbits - used; |
| 580 |
wnd->free_bits[iw] = frb; |
| 581 |
wnd->total_zeroes += frb; |
| 582 |
} |
| 583 |
|
| 584 |
wpos = 0; |
| 585 |
wbit = vbo * 8; |
| 586 |
|
| 587 |
if (wbit + wbits > wnd->nbits) |
| 588 |
wbits = wnd->nbits - wbit; |
| 589 |
|
| 590 |
do { |
| 591 |
used = find_next_zero_bit_le(bh->b_data, wbits, wpos); |
| 592 |
|
| 593 |
if (used > wpos && prev_tail) { |
| 594 |
wnd_add_free_ext(wnd, wbit + wpos - prev_tail, |
| 595 |
prev_tail, true); |
| 596 |
prev_tail = 0; |
| 597 |
} |
| 598 |
|
| 599 |
wpos = used; |
| 600 |
|
| 601 |
if (wpos >= wbits) { |
| 602 |
/* No free blocks. */ |
| 603 |
prev_tail = 0; |
| 604 |
break; |
| 605 |
} |
| 606 |
|
| 607 |
frb = find_next_bit_le(bh->b_data, wbits, wpos); |
| 608 |
if (frb >= wbits) { |
| 609 |
/* Keep last free block. */ |
| 610 |
prev_tail += frb - wpos; |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
wnd_add_free_ext(wnd, wbit + wpos - prev_tail, |
| 615 |
frb + prev_tail - wpos, true); |
| 616 |
|
| 617 |
/* Skip free block and first '1'. */ |
| 618 |
wpos = frb + 1; |
| 619 |
/* Reset previous tail. */ |
| 620 |
prev_tail = 0; |
| 621 |
} while (wpos < wbits); |
| 622 |
|
| 623 |
next_wnd: |
| 624 |
|
| 625 |
if (bh) |
| 626 |
put_bh(bh); |
| 627 |
bh = NULL; |
| 628 |
|
| 629 |
vbo += blocksize; |
| 630 |
if (len) { |
| 631 |
len -= blocksize; |
| 632 |
lbo += blocksize; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
/* Add last block. */ |
| 637 |
if (prev_tail) |
| 638 |
wnd_add_free_ext(wnd, wnd->nbits - prev_tail, prev_tail, true); |
| 639 |
|
| 640 |
/* |
| 641 |
* Before init cycle wnd->uptodated was 0. |
| 642 |
* If any errors or limits occurs while initialization then |
| 643 |
* wnd->uptodated will be -1. |
| 644 |
* If 'uptodated' is still 0 then Tree is really updated. |
| 645 |
*/ |
| 646 |
if (!wnd->uptodated) |
| 647 |
wnd->uptodated = 1; |
| 648 |
|
| 649 |
if (wnd->zone_bit != wnd->zone_end) { |
| 650 |
size_t zlen = wnd->zone_end - wnd->zone_bit; |
| 651 |
|
| 652 |
wnd->zone_end = wnd->zone_bit; |
| 653 |
wnd_zone_set(wnd, wnd->zone_bit, zlen); |
| 654 |
} |
| 655 |
|
| 656 |
out: |
| 657 |
kfree(ra); |
| 658 |
return err; |
| 659 |
} |
| 660 |
|
| 661 |
int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits) |
| 662 |
{ |
| 663 |
int err; |
| 664 |
u32 blocksize = sb->s_blocksize; |
| 665 |
u32 wbits = blocksize * 8; |
| 666 |
|
| 667 |
init_rwsem(&wnd->rw_lock); |
| 668 |
|
| 669 |
wnd->sb = sb; |
| 670 |
wnd->nbits = nbits; |
| 671 |
wnd->total_zeroes = nbits; |
| 672 |
wnd->extent_max = MINUS_ONE_T; |
| 673 |
wnd->zone_bit = wnd->zone_end = 0; |
| 674 |
wnd->nwnd = bytes_to_block(sb, ntfs3_bitmap_size(nbits)); |
| 675 |
wnd->bits_last = nbits & (wbits - 1); |
| 676 |
if (!wnd->bits_last) |
| 677 |
wnd->bits_last = wbits; |
| 678 |
|
| 679 |
wnd->free_bits = |
| 680 |
kvmalloc_array(wnd->nwnd, sizeof(u16), GFP_KERNEL | __GFP_ZERO); |
| 681 |
|
| 682 |
if (!wnd->free_bits) |
| 683 |
return -ENOMEM; |
| 684 |
|
| 685 |
err = wnd_rescan(wnd); |
| 686 |
if (err) |
| 687 |
return err; |
| 688 |
|
| 689 |
wnd->inited = true; |
| 690 |
|
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
/* |
| 695 |
* wnd_map - Call sb_bread for requested window. |
| 696 |
*/ |
| 697 |
static struct buffer_head *wnd_map(struct wnd_bitmap *wnd, size_t iw) |
| 698 |
{ |
| 699 |
size_t vbo; |
| 700 |
CLST lcn, clen; |
| 701 |
struct super_block *sb = wnd->sb; |
| 702 |
struct ntfs_sb_info *sbi; |
| 703 |
struct buffer_head *bh; |
| 704 |
u64 lbo; |
| 705 |
|
| 706 |
sbi = sb->s_fs_info; |
| 707 |
vbo = (u64)iw << sb->s_blocksize_bits; |
| 708 |
|
| 709 |
if (!run_lookup_entry(&wnd->run, vbo >> sbi->cluster_bits, &lcn, &clen, |
| 710 |
NULL)) { |
| 711 |
return ERR_PTR(-ENOENT); |
| 712 |
} |
| 713 |
|
| 714 |
lbo = ((u64)lcn << sbi->cluster_bits) + (vbo & sbi->cluster_mask); |
| 715 |
|
| 716 |
bh = ntfs_bread(wnd->sb, lbo >> sb->s_blocksize_bits); |
| 717 |
if (!bh) |
| 718 |
return ERR_PTR(-EIO); |
| 719 |
|
| 720 |
return bh; |
| 721 |
} |
| 722 |
|
| 723 |
/* |
| 724 |
* wnd_set_free - Mark the bits range from bit to bit + bits as free. |
| 725 |
*/ |
| 726 |
int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits) |
| 727 |
{ |
| 728 |
int err = 0; |
| 729 |
struct super_block *sb = wnd->sb; |
| 730 |
u32 wbits = 8 * sb->s_blocksize; |
| 731 |
size_t iw = bit >> (sb->s_blocksize_bits + 3); |
| 732 |
u32 wbit = bit & (wbits - 1); |
| 733 |
struct buffer_head *bh; |
| 734 |
u32 op; |
| 735 |
|
| 736 |
for (; iw < wnd->nwnd && bits; iw++, bit += op, bits -= op, wbit = 0) { |
| 737 |
if (iw + 1 == wnd->nwnd) |
| 738 |
wbits = wnd->bits_last; |
| 739 |
|
| 740 |
op = min_t(u32, wbits - wbit, bits); |
| 741 |
|
| 742 |
bh = wnd_map(wnd, iw); |
| 743 |
if (IS_ERR(bh)) { |
| 744 |
err = PTR_ERR(bh); |
| 745 |
break; |
| 746 |
} |
| 747 |
|
| 748 |
lock_buffer(bh); |
| 749 |
|
| 750 |
ntfs_bitmap_clear_le(bh->b_data, wbit, op); |
| 751 |
|
| 752 |
wnd->free_bits[iw] += op; |
| 753 |
wnd->total_zeroes += op; |
| 754 |
|
| 755 |
set_buffer_uptodate(bh); |
| 756 |
mark_buffer_dirty(bh); |
| 757 |
unlock_buffer(bh); |
| 758 |
put_bh(bh); |
| 759 |
|
| 760 |
wnd_add_free_ext(wnd, bit, op, false); |
| 761 |
} |
| 762 |
return err; |
| 763 |
} |
| 764 |
|
| 765 |
/* |
| 766 |
* wnd_set_used - Mark the bits range from bit to bit + bits as used. |
| 767 |
*/ |
| 768 |
int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits) |
| 769 |
{ |
| 770 |
int err = 0; |
| 771 |
struct super_block *sb = wnd->sb; |
| 772 |
size_t iw = bit >> (sb->s_blocksize_bits + 3); |
| 773 |
u32 wbits = 8 * sb->s_blocksize; |
| 774 |
u32 wbit = bit & (wbits - 1); |
| 775 |
struct buffer_head *bh; |
| 776 |
u32 op; |
| 777 |
|
| 778 |
for (; iw < wnd->nwnd && bits; iw++, bit += op, bits -= op, wbit = 0) { |
| 779 |
if (unlikely(iw + 1 == wnd->nwnd)) |
| 780 |
wbits = wnd->bits_last; |
| 781 |
|
| 782 |
op = min_t(u32, wbits - wbit, bits); |
| 783 |
|
| 784 |
bh = wnd_map(wnd, iw); |
| 785 |
if (IS_ERR(bh)) { |
| 786 |
err = PTR_ERR(bh); |
| 787 |
break; |
| 788 |
} |
| 789 |
|
| 790 |
lock_buffer(bh); |
| 791 |
|
| 792 |
ntfs_bitmap_set_le(bh->b_data, wbit, op); |
| 793 |
wnd->free_bits[iw] -= op; |
| 794 |
wnd->total_zeroes -= op; |
| 795 |
|
| 796 |
set_buffer_uptodate(bh); |
| 797 |
mark_buffer_dirty(bh); |
| 798 |
unlock_buffer(bh); |
| 799 |
put_bh(bh); |
| 800 |
|
| 801 |
if (!RB_EMPTY_ROOT(&wnd->start_tree)) |
| 802 |
wnd_remove_free_ext(wnd, bit, op); |
| 803 |
} |
| 804 |
return err; |
| 805 |
} |
| 806 |
|
| 807 |
/* |
| 808 |
* wnd_set_used_safe - Mark the bits range from bit to bit + bits as used. |
| 809 |
* |
| 810 |
* Unlikely wnd_set_used/wnd_set_free this function is not full trusted. |
| 811 |
* It scans every bit in bitmap and marks free bit as used. |
| 812 |
* @done - how many bits were marked as used. |
| 813 |
* |
| 814 |
* NOTE: normally *done should be 0. |
| 815 |
*/ |
| 816 |
int wnd_set_used_safe(struct wnd_bitmap *wnd, size_t bit, size_t bits, |
| 817 |
size_t *done) |
| 818 |
{ |
| 819 |
size_t i, from = 0, len = 0; |
| 820 |
int err = 0; |
| 821 |
|
| 822 |
*done = 0; |
| 823 |
for (i = 0; i < bits; i++) { |
| 824 |
if (wnd_is_free(wnd, bit + i, 1)) { |
| 825 |
if (!len) |
| 826 |
from = bit + i; |
| 827 |
len += 1; |
| 828 |
} else if (len) { |
| 829 |
err = wnd_set_used(wnd, from, len); |
| 830 |
*done += len; |
| 831 |
len = 0; |
| 832 |
if (err) |
| 833 |
break; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
if (len) { |
| 838 |
/* last fragment. */ |
| 839 |
err = wnd_set_used(wnd, from, len); |
| 840 |
*done += len; |
| 841 |
} |
| 842 |
return err; |
| 843 |
} |
| 844 |
|
| 845 |
/* |
| 846 |
* wnd_is_free_hlp |
| 847 |
* |
| 848 |
* Return: True if all clusters [bit, bit+bits) are free (bitmap only). |
| 849 |
*/ |
| 850 |
static bool wnd_is_free_hlp(struct wnd_bitmap *wnd, size_t bit, size_t bits) |
| 851 |
{ |
| 852 |
struct super_block *sb = wnd->sb; |
| 853 |
size_t iw = bit >> (sb->s_blocksize_bits + 3); |
| 854 |
u32 wbits = 8 * sb->s_blocksize; |
| 855 |
u32 wbit = bit & (wbits - 1); |
| 856 |
u32 op; |
| 857 |
|
| 858 |
for (; iw < wnd->nwnd && bits; iw++, bits -= op, wbit = 0) { |
| 859 |
if (unlikely(iw + 1 == wnd->nwnd)) |
| 860 |
wbits = wnd->bits_last; |
| 861 |
|
| 862 |
op = min_t(u32, wbits - wbit, bits); |
| 863 |
|
| 864 |
if (wbits != wnd->free_bits[iw]) { |
| 865 |
bool ret; |
| 866 |
struct buffer_head *bh = wnd_map(wnd, iw); |
| 867 |
|
| 868 |
if (IS_ERR(bh)) |
| 869 |
return false; |
| 870 |
|
| 871 |
ret = are_bits_clear(bh->b_data, wbit, op); |
| 872 |
|
| 873 |
put_bh(bh); |
| 874 |
if (!ret) |
| 875 |
return false; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
return true; |
| 880 |
} |
| 881 |
|
| 882 |
/* |
| 883 |
* wnd_is_free |
| 884 |
* |
| 885 |
* Return: True if all clusters [bit, bit+bits) are free. |
| 886 |
*/ |
| 887 |
bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits) |
| 888 |
{ |
| 889 |
bool ret; |
| 890 |
struct rb_node *n; |
| 891 |
size_t end; |
| 892 |
struct e_node *e; |
| 893 |
|
| 894 |
if (RB_EMPTY_ROOT(&wnd->start_tree)) |
| 895 |
goto use_wnd; |
| 896 |
|
| 897 |
n = rb_lookup(&wnd->start_tree, bit); |
| 898 |
if (!n) |
| 899 |
goto use_wnd; |
| 900 |
|
| 901 |
e = rb_entry(n, struct e_node, start.node); |
| 902 |
|
| 903 |
end = e->start.key + e->count.key; |
| 904 |
|
| 905 |
if (bit < end && bit + bits <= end) |
| 906 |
return true; |
| 907 |
|
| 908 |
use_wnd: |
| 909 |
ret = wnd_is_free_hlp(wnd, bit, bits); |
| 910 |
|
| 911 |
return ret; |
| 912 |
} |
| 913 |
|
| 914 |
/* |
| 915 |
* wnd_is_used |
| 916 |
* |
| 917 |
* Return: True if all clusters [bit, bit+bits) are used. |
| 918 |
*/ |
| 919 |
bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits) |
| 920 |
{ |
| 921 |
bool ret = false; |
| 922 |
struct super_block *sb = wnd->sb; |
| 923 |
size_t iw = bit >> (sb->s_blocksize_bits + 3); |
| 924 |
u32 wbits = 8 * sb->s_blocksize; |
| 925 |
u32 wbit = bit & (wbits - 1); |
| 926 |
u32 op; |
| 927 |
size_t end; |
| 928 |
struct rb_node *n; |
| 929 |
struct e_node *e; |
| 930 |
|
| 931 |
if (RB_EMPTY_ROOT(&wnd->start_tree)) |
| 932 |
goto use_wnd; |
| 933 |
|
| 934 |
end = bit + bits; |
| 935 |
n = rb_lookup(&wnd->start_tree, end - 1); |
| 936 |
if (!n) |
| 937 |
goto use_wnd; |
| 938 |
|
| 939 |
e = rb_entry(n, struct e_node, start.node); |
| 940 |
if (e->start.key + e->count.key > bit) |
| 941 |
return false; |
| 942 |
|
| 943 |
use_wnd: |
| 944 |
for (; iw < wnd->nwnd && bits; iw++, bits -= op, wbit = 0) { |
| 945 |
if (unlikely(iw + 1 == wnd->nwnd)) |
| 946 |
wbits = wnd->bits_last; |
| 947 |
|
| 948 |
op = min_t(u32, wbits - wbit, bits); |
| 949 |
|
| 950 |
if (wnd->free_bits[iw]) { |
| 951 |
bool ret; |
| 952 |
struct buffer_head *bh = wnd_map(wnd, iw); |
| 953 |
|
| 954 |
if (IS_ERR(bh)) |
| 955 |
goto out; |
| 956 |
|
| 957 |
ret = are_bits_set(bh->b_data, wbit, op); |
| 958 |
put_bh(bh); |
| 959 |
if (!ret) |
| 960 |
goto out; |
| 961 |
} |
| 962 |
} |
| 963 |
ret = true; |
| 964 |
|
| 965 |
out: |
| 966 |
return ret; |
| 967 |
} |
| 968 |
|
| 969 |
/* |
| 970 |
* wnd_find - Look for free space. |
| 971 |
* |
| 972 |
* - flags - BITMAP_FIND_XXX flags |
| 973 |
* |
| 974 |
* Return: 0 if not found. |
| 975 |
*/ |
| 976 |
size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint, |
| 977 |
size_t flags, size_t *allocated) |
| 978 |
{ |
| 979 |
struct super_block *sb; |
| 980 |
u32 wbits, wpos, wzbit, wzend; |
| 981 |
size_t fnd, max_alloc, b_len, b_pos; |
| 982 |
size_t iw, prev_tail, nwnd, wbit, ebit, zbit, zend; |
| 983 |
size_t to_alloc0 = to_alloc; |
| 984 |
const struct e_node *e; |
| 985 |
const struct rb_node *pr, *cr; |
| 986 |
u8 log2_bits; |
| 987 |
bool fbits_valid; |
| 988 |
struct buffer_head *bh; |
| 989 |
|
| 990 |
/* Fast checking for available free space. */ |
| 991 |
if (flags & BITMAP_FIND_FULL) { |
| 992 |
size_t zeroes = wnd_zeroes(wnd); |
| 993 |
|
| 994 |
zeroes -= wnd->zone_end - wnd->zone_bit; |
| 995 |
if (zeroes < to_alloc0) |
| 996 |
goto no_space; |
| 997 |
|
| 998 |
if (to_alloc0 > wnd->extent_max) |
| 999 |
goto no_space; |
| 1000 |
} else { |
| 1001 |
if (to_alloc > wnd->extent_max) |
| 1002 |
to_alloc = wnd->extent_max; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (wnd->zone_bit <= hint && hint < wnd->zone_end) |
| 1006 |
hint = wnd->zone_end; |
| 1007 |
|
| 1008 |
max_alloc = wnd->nbits; |
| 1009 |
b_len = b_pos = 0; |
| 1010 |
|
| 1011 |
if (hint >= max_alloc) |
| 1012 |
hint = 0; |
| 1013 |
|
| 1014 |
if (RB_EMPTY_ROOT(&wnd->start_tree)) { |
| 1015 |
if (wnd->uptodated == 1) { |
| 1016 |
/* Extents tree is updated -> No free space. */ |
| 1017 |
goto no_space; |
| 1018 |
} |
| 1019 |
goto scan_bitmap; |
| 1020 |
} |
| 1021 |
|
| 1022 |
e = NULL; |
| 1023 |
if (!hint) |
| 1024 |
goto allocate_biggest; |
| 1025 |
|
| 1026 |
/* Use hint: Enumerate extents by start >= hint. */ |
| 1027 |
pr = NULL; |
| 1028 |
cr = wnd->start_tree.rb_node; |
| 1029 |
|
| 1030 |
for (;;) { |
| 1031 |
e = rb_entry(cr, struct e_node, start.node); |
| 1032 |
|
| 1033 |
if (e->start.key == hint) |
| 1034 |
break; |
| 1035 |
|
| 1036 |
if (e->start.key < hint) { |
| 1037 |
pr = cr; |
| 1038 |
cr = cr->rb_right; |
| 1039 |
if (!cr) |
| 1040 |
break; |
| 1041 |
continue; |
| 1042 |
} |
| 1043 |
|
| 1044 |
cr = cr->rb_left; |
| 1045 |
if (!cr) { |
| 1046 |
e = pr ? rb_entry(pr, struct e_node, start.node) : NULL; |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (!e) |
| 1052 |
goto allocate_biggest; |
| 1053 |
|
| 1054 |
if (e->start.key + e->count.key > hint) { |
| 1055 |
/* We have found extension with 'hint' inside. */ |
| 1056 |
size_t len = e->start.key + e->count.key - hint; |
| 1057 |
|
| 1058 |
if (len >= to_alloc && hint + to_alloc <= max_alloc) { |
| 1059 |
fnd = hint; |
| 1060 |
goto found; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (!(flags & BITMAP_FIND_FULL)) { |
| 1064 |
if (len > to_alloc) |
| 1065 |
len = to_alloc; |
| 1066 |
|
| 1067 |
if (hint + len <= max_alloc) { |
| 1068 |
fnd = hint; |
| 1069 |
to_alloc = len; |
| 1070 |
goto found; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
allocate_biggest: |
| 1076 |
/* Allocate from biggest free extent. */ |
| 1077 |
e = rb_entry(rb_first(&wnd->count_tree), struct e_node, count.node); |
| 1078 |
if (e->count.key != wnd->extent_max) |
| 1079 |
wnd->extent_max = e->count.key; |
| 1080 |
|
| 1081 |
if (e->count.key < max_alloc) { |
| 1082 |
if (e->count.key >= to_alloc) { |
| 1083 |
; |
| 1084 |
} else if (flags & BITMAP_FIND_FULL) { |
| 1085 |
if (e->count.key < to_alloc0) { |
| 1086 |
/* Biggest free block is less then requested. */ |
| 1087 |
goto no_space; |
| 1088 |
} |
| 1089 |
to_alloc = e->count.key; |
| 1090 |
} else if (-1 != wnd->uptodated) { |
| 1091 |
to_alloc = e->count.key; |
| 1092 |
} else { |
| 1093 |
/* Check if we can use more bits. */ |
| 1094 |
size_t op, max_check; |
| 1095 |
struct rb_root start_tree; |
| 1096 |
|
| 1097 |
memcpy(&start_tree, &wnd->start_tree, |
| 1098 |
sizeof(struct rb_root)); |
| 1099 |
memset(&wnd->start_tree, 0, sizeof(struct rb_root)); |
| 1100 |
|
| 1101 |
max_check = e->start.key + to_alloc; |
| 1102 |
if (max_check > max_alloc) |
| 1103 |
max_check = max_alloc; |
| 1104 |
for (op = e->start.key + e->count.key; op < max_check; |
| 1105 |
op++) { |
| 1106 |
if (!wnd_is_free(wnd, op, 1)) |
| 1107 |
break; |
| 1108 |
} |
| 1109 |
memcpy(&wnd->start_tree, &start_tree, |
| 1110 |
sizeof(struct rb_root)); |
| 1111 |
to_alloc = op - e->start.key; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Prepare to return. */ |
| 1115 |
fnd = e->start.key; |
| 1116 |
if (e->start.key + to_alloc > max_alloc) |
| 1117 |
to_alloc = max_alloc - e->start.key; |
| 1118 |
goto found; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (wnd->uptodated == 1) { |
| 1122 |
/* Extents tree is updated -> no free space. */ |
| 1123 |
goto no_space; |
| 1124 |
} |
| 1125 |
|
| 1126 |
b_len = e->count.key; |
| 1127 |
b_pos = e->start.key; |
| 1128 |
|
| 1129 |
scan_bitmap: |
| 1130 |
sb = wnd->sb; |
| 1131 |
log2_bits = sb->s_blocksize_bits + 3; |
| 1132 |
|
| 1133 |
/* At most two ranges [hint, max_alloc) + [0, hint). */ |
| 1134 |
Again: |
| 1135 |
|
| 1136 |
/* TODO: Optimize request for case nbits > wbits. */ |
| 1137 |
iw = hint >> log2_bits; |
| 1138 |
wbits = sb->s_blocksize * 8; |
| 1139 |
wpos = hint & (wbits - 1); |
| 1140 |
prev_tail = 0; |
| 1141 |
fbits_valid = true; |
| 1142 |
|
| 1143 |
if (max_alloc == wnd->nbits) { |
| 1144 |
nwnd = wnd->nwnd; |
| 1145 |
} else { |
| 1146 |
size_t t = max_alloc + wbits - 1; |
| 1147 |
|
| 1148 |
nwnd = likely(t > max_alloc) ? (t >> log2_bits) : wnd->nwnd; |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* Enumerate all windows. */ |
| 1152 |
for (; iw < nwnd; iw++) { |
| 1153 |
wbit = iw << log2_bits; |
| 1154 |
|
| 1155 |
if (!wnd->free_bits[iw]) { |
| 1156 |
if (prev_tail > b_len) { |
| 1157 |
b_pos = wbit - prev_tail; |
| 1158 |
b_len = prev_tail; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* Skip full used window. */ |
| 1162 |
prev_tail = 0; |
| 1163 |
wpos = 0; |
| 1164 |
continue; |
| 1165 |
} |
| 1166 |
|
| 1167 |
if (unlikely(iw + 1 == nwnd)) { |
| 1168 |
if (max_alloc == wnd->nbits) { |
| 1169 |
wbits = wnd->bits_last; |
| 1170 |
} else { |
| 1171 |
size_t t = max_alloc & (wbits - 1); |
| 1172 |
|
| 1173 |
if (t) { |
| 1174 |
wbits = t; |
| 1175 |
fbits_valid = false; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
if (wnd->zone_end > wnd->zone_bit) { |
| 1181 |
ebit = wbit + wbits; |
| 1182 |
zbit = max(wnd->zone_bit, wbit); |
| 1183 |
zend = min(wnd->zone_end, ebit); |
| 1184 |
|
| 1185 |
/* Here we have a window [wbit, ebit) and zone [zbit, zend). */ |
| 1186 |
if (zend <= zbit) { |
| 1187 |
/* Zone does not overlap window. */ |
| 1188 |
} else { |
| 1189 |
wzbit = zbit - wbit; |
| 1190 |
wzend = zend - wbit; |
| 1191 |
|
| 1192 |
/* Zone overlaps window. */ |
| 1193 |
if (wnd->free_bits[iw] == wzend - wzbit) { |
| 1194 |
prev_tail = 0; |
| 1195 |
wpos = 0; |
| 1196 |
continue; |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* Scan two ranges window: [wbit, zbit) and [zend, ebit). */ |
| 1200 |
bh = wnd_map(wnd, iw); |
| 1201 |
|
| 1202 |
if (IS_ERR(bh)) { |
| 1203 |
/* TODO: Error */ |
| 1204 |
prev_tail = 0; |
| 1205 |
wpos = 0; |
| 1206 |
continue; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* Scan range [wbit, zbit). */ |
| 1210 |
if (wpos < wzbit) { |
| 1211 |
/* Scan range [wpos, zbit). */ |
| 1212 |
fnd = wnd_scan(bh->b_data, wbit, wpos, |
| 1213 |
wzbit, to_alloc, |
| 1214 |
&prev_tail, &b_pos, |
| 1215 |
&b_len); |
| 1216 |
if (fnd != MINUS_ONE_T) { |
| 1217 |
put_bh(bh); |
| 1218 |
goto found; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
prev_tail = 0; |
| 1223 |
|
| 1224 |
/* Scan range [zend, ebit). */ |
| 1225 |
if (wzend < wbits) { |
| 1226 |
fnd = wnd_scan(bh->b_data, wbit, |
| 1227 |
max(wzend, wpos), wbits, |
| 1228 |
to_alloc, &prev_tail, |
| 1229 |
&b_pos, &b_len); |
| 1230 |
if (fnd != MINUS_ONE_T) { |
| 1231 |
put_bh(bh); |
| 1232 |
goto found; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
wpos = 0; |
| 1237 |
put_bh(bh); |
| 1238 |
continue; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
/* Current window does not overlap zone. */ |
| 1243 |
if (!wpos && fbits_valid && wnd->free_bits[iw] == wbits) { |
| 1244 |
/* Window is empty. */ |
| 1245 |
if (prev_tail + wbits >= to_alloc) { |
| 1246 |
fnd = wbit + wpos - prev_tail; |
| 1247 |
goto found; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* Increase 'prev_tail' and process next window. */ |
| 1251 |
prev_tail += wbits; |
| 1252 |
wpos = 0; |
| 1253 |
continue; |
| 1254 |
} |
| 1255 |
|
| 1256 |
/* Read window. */ |
| 1257 |
bh = wnd_map(wnd, iw); |
| 1258 |
if (IS_ERR(bh)) { |
| 1259 |
// TODO: Error. |
| 1260 |
prev_tail = 0; |
| 1261 |
wpos = 0; |
| 1262 |
continue; |
| 1263 |
} |
| 1264 |
|
| 1265 |
/* Scan range [wpos, eBits). */ |
| 1266 |
fnd = wnd_scan(bh->b_data, wbit, wpos, wbits, to_alloc, |
| 1267 |
&prev_tail, &b_pos, &b_len); |
| 1268 |
put_bh(bh); |
| 1269 |
if (fnd != MINUS_ONE_T) |
| 1270 |
goto found; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (b_len < prev_tail) { |
| 1274 |
/* The last fragment. */ |
| 1275 |
b_len = prev_tail; |
| 1276 |
b_pos = max_alloc - prev_tail; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (hint) { |
| 1280 |
/* |
| 1281 |
* We have scanned range [hint max_alloc). |
| 1282 |
* Prepare to scan range [0 hint + to_alloc). |
| 1283 |
*/ |
| 1284 |
size_t nextmax = hint + to_alloc; |
| 1285 |
|
| 1286 |
if (likely(nextmax >= hint) && nextmax < max_alloc) |
| 1287 |
max_alloc = nextmax; |
| 1288 |
hint = 0; |
| 1289 |
goto Again; |
| 1290 |
} |
| 1291 |
|
| 1292 |
if (!b_len) |
| 1293 |
goto no_space; |
| 1294 |
|
| 1295 |
wnd->extent_max = b_len; |
| 1296 |
|
| 1297 |
if (flags & BITMAP_FIND_FULL) |
| 1298 |
goto no_space; |
| 1299 |
|
| 1300 |
fnd = b_pos; |
| 1301 |
to_alloc = b_len; |
| 1302 |
|
| 1303 |
found: |
| 1304 |
if (flags & BITMAP_FIND_MARK_AS_USED) { |
| 1305 |
/* TODO: Optimize remove extent (pass 'e'?). */ |
| 1306 |
if (wnd_set_used(wnd, fnd, to_alloc)) |
| 1307 |
goto no_space; |
| 1308 |
} else if (wnd->extent_max != MINUS_ONE_T && |
| 1309 |
to_alloc > wnd->extent_max) { |
| 1310 |
wnd->extent_max = to_alloc; |
| 1311 |
} |
| 1312 |
|
| 1313 |
*allocated = fnd; |
| 1314 |
return to_alloc; |
| 1315 |
|
| 1316 |
no_space: |
| 1317 |
return 0; |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* |
| 1321 |
* wnd_extend - Extend bitmap ($MFT bitmap). |
| 1322 |
*/ |
| 1323 |
int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits) |
| 1324 |
{ |
| 1325 |
int err; |
| 1326 |
struct super_block *sb = wnd->sb; |
| 1327 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1328 |
u32 blocksize = sb->s_blocksize; |
| 1329 |
u32 wbits = blocksize * 8; |
| 1330 |
u32 b0, new_last; |
| 1331 |
size_t bits, iw, new_wnd; |
| 1332 |
size_t old_bits = wnd->nbits; |
| 1333 |
u16 *new_free; |
| 1334 |
|
| 1335 |
if (new_bits <= old_bits) |
| 1336 |
return -EINVAL; |
| 1337 |
|
| 1338 |
/* Align to 8 byte boundary. */ |
| 1339 |
new_wnd = bytes_to_block(sb, ntfs3_bitmap_size(new_bits)); |
| 1340 |
new_last = new_bits & (wbits - 1); |
| 1341 |
if (!new_last) |
| 1342 |
new_last = wbits; |
| 1343 |
|
| 1344 |
if (new_wnd != wnd->nwnd) { |
| 1345 |
new_free = kmalloc_array(new_wnd, sizeof(u16), GFP_NOFS); |
| 1346 |
if (!new_free) |
| 1347 |
return -ENOMEM; |
| 1348 |
|
| 1349 |
memcpy(new_free, wnd->free_bits, wnd->nwnd * sizeof(short)); |
| 1350 |
memset(new_free + wnd->nwnd, 0, |
| 1351 |
(new_wnd - wnd->nwnd) * sizeof(short)); |
| 1352 |
kvfree(wnd->free_bits); |
| 1353 |
wnd->free_bits = new_free; |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* Zero bits [old_bits,new_bits). */ |
| 1357 |
bits = new_bits - old_bits; |
| 1358 |
b0 = old_bits & (wbits - 1); |
| 1359 |
|
| 1360 |
for (iw = old_bits >> (sb->s_blocksize_bits + 3); bits; iw += 1) { |
| 1361 |
u32 op; |
| 1362 |
size_t frb; |
| 1363 |
u64 vbo, lbo, bytes; |
| 1364 |
struct buffer_head *bh; |
| 1365 |
|
| 1366 |
if (iw + 1 == new_wnd) |
| 1367 |
wbits = new_last; |
| 1368 |
|
| 1369 |
op = b0 + bits > wbits ? wbits - b0 : bits; |
| 1370 |
vbo = (u64)iw * blocksize; |
| 1371 |
|
| 1372 |
err = ntfs_vbo_to_lbo(sbi, &wnd->run, vbo, &lbo, &bytes); |
| 1373 |
if (err) |
| 1374 |
return err; |
| 1375 |
|
| 1376 |
bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits); |
| 1377 |
if (!bh) |
| 1378 |
return -EIO; |
| 1379 |
|
| 1380 |
lock_buffer(bh); |
| 1381 |
|
| 1382 |
ntfs_bitmap_clear_le(bh->b_data, b0, blocksize * 8 - b0); |
| 1383 |
frb = wbits - ntfs_bitmap_weight_le(bh->b_data, wbits); |
| 1384 |
wnd->total_zeroes += frb - wnd->free_bits[iw]; |
| 1385 |
wnd->free_bits[iw] = frb; |
| 1386 |
|
| 1387 |
set_buffer_uptodate(bh); |
| 1388 |
mark_buffer_dirty(bh); |
| 1389 |
unlock_buffer(bh); |
| 1390 |
/* err = sync_dirty_buffer(bh); */ |
| 1391 |
put_bh(bh); |
| 1392 |
|
| 1393 |
b0 = 0; |
| 1394 |
bits -= op; |
| 1395 |
} |
| 1396 |
|
| 1397 |
wnd->nbits = new_bits; |
| 1398 |
wnd->nwnd = new_wnd; |
| 1399 |
wnd->bits_last = new_last; |
| 1400 |
|
| 1401 |
wnd_add_free_ext(wnd, old_bits, new_bits - old_bits, false); |
| 1402 |
|
| 1403 |
return 0; |
| 1404 |
} |
| 1405 |
|
| 1406 |
void wnd_zone_set(struct wnd_bitmap *wnd, size_t lcn, size_t len) |
| 1407 |
{ |
| 1408 |
size_t zlen = wnd->zone_end - wnd->zone_bit; |
| 1409 |
|
| 1410 |
if (zlen) |
| 1411 |
wnd_add_free_ext(wnd, wnd->zone_bit, zlen, false); |
| 1412 |
|
| 1413 |
if (!RB_EMPTY_ROOT(&wnd->start_tree) && len) |
| 1414 |
wnd_remove_free_ext(wnd, lcn, len); |
| 1415 |
|
| 1416 |
wnd->zone_bit = lcn; |
| 1417 |
wnd->zone_end = lcn + len; |
| 1418 |
} |
| 1419 |
|
| 1420 |
int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range) |
| 1421 |
{ |
| 1422 |
int err = 0; |
| 1423 |
struct super_block *sb = sbi->sb; |
| 1424 |
struct wnd_bitmap *wnd = &sbi->used.bitmap; |
| 1425 |
u32 wbits = 8 * sb->s_blocksize; |
| 1426 |
CLST len = 0, lcn = 0, done = 0; |
| 1427 |
CLST minlen = bytes_to_cluster(sbi, range->minlen); |
| 1428 |
CLST lcn_from = bytes_to_cluster(sbi, range->start); |
| 1429 |
size_t iw = lcn_from >> (sb->s_blocksize_bits + 3); |
| 1430 |
u32 wbit = lcn_from & (wbits - 1); |
| 1431 |
CLST lcn_to; |
| 1432 |
|
| 1433 |
if (!minlen) |
| 1434 |
minlen = 1; |
| 1435 |
|
| 1436 |
if (range->len == (u64)-1) |
| 1437 |
lcn_to = wnd->nbits; |
| 1438 |
else |
| 1439 |
lcn_to = bytes_to_cluster(sbi, range->start + range->len); |
| 1440 |
|
| 1441 |
down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS); |
| 1442 |
|
| 1443 |
for (; iw < wnd->nwnd; iw++, wbit = 0) { |
| 1444 |
CLST lcn_wnd = iw * wbits; |
| 1445 |
struct buffer_head *bh; |
| 1446 |
|
| 1447 |
if (lcn_wnd > lcn_to) |
| 1448 |
break; |
| 1449 |
|
| 1450 |
if (!wnd->free_bits[iw]) |
| 1451 |
continue; |
| 1452 |
|
| 1453 |
if (iw + 1 == wnd->nwnd) |
| 1454 |
wbits = wnd->bits_last; |
| 1455 |
|
| 1456 |
if (lcn_wnd + wbits > lcn_to) |
| 1457 |
wbits = lcn_to - lcn_wnd; |
| 1458 |
|
| 1459 |
bh = wnd_map(wnd, iw); |
| 1460 |
if (IS_ERR(bh)) { |
| 1461 |
err = PTR_ERR(bh); |
| 1462 |
break; |
| 1463 |
} |
| 1464 |
|
| 1465 |
for (; wbit < wbits; wbit++) { |
| 1466 |
if (!test_bit_le(wbit, bh->b_data)) { |
| 1467 |
if (!len) |
| 1468 |
lcn = lcn_wnd + wbit; |
| 1469 |
len += 1; |
| 1470 |
continue; |
| 1471 |
} |
| 1472 |
if (len >= minlen) { |
| 1473 |
err = ntfs_discard(sbi, lcn, len); |
| 1474 |
if (err) |
| 1475 |
goto out; |
| 1476 |
done += len; |
| 1477 |
} |
| 1478 |
len = 0; |
| 1479 |
} |
| 1480 |
put_bh(bh); |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* Process the last fragment. */ |
| 1484 |
if (len >= minlen) { |
| 1485 |
err = ntfs_discard(sbi, lcn, len); |
| 1486 |
if (err) |
| 1487 |
goto out; |
| 1488 |
done += len; |
| 1489 |
} |
| 1490 |
|
| 1491 |
out: |
| 1492 |
range->len = (u64)done << sbi->cluster_bits; |
| 1493 |
|
| 1494 |
up_read(&wnd->rw_lock); |
| 1495 |
|
| 1496 |
return err; |
| 1497 |
} |
| 1498 |
|
| 1499 |
#if BITS_PER_LONG == 64 |
| 1500 |
typedef __le64 bitmap_ulong; |
| 1501 |
#define cpu_to_ul(x) cpu_to_le64(x) |
| 1502 |
#define ul_to_cpu(x) le64_to_cpu(x) |
| 1503 |
#else |
| 1504 |
typedef __le32 bitmap_ulong; |
| 1505 |
#define cpu_to_ul(x) cpu_to_le32(x) |
| 1506 |
#define ul_to_cpu(x) le32_to_cpu(x) |
| 1507 |
#endif |
| 1508 |
|
| 1509 |
void ntfs_bitmap_set_le(void *map, unsigned int start, int len) |
| 1510 |
{ |
| 1511 |
bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start); |
| 1512 |
const unsigned int size = start + len; |
| 1513 |
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 1514 |
bitmap_ulong mask_to_set = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start)); |
| 1515 |
|
| 1516 |
while (len - bits_to_set >= 0) { |
| 1517 |
*p |= mask_to_set; |
| 1518 |
len -= bits_to_set; |
| 1519 |
bits_to_set = BITS_PER_LONG; |
| 1520 |
mask_to_set = cpu_to_ul(~0UL); |
| 1521 |
p++; |
| 1522 |
} |
| 1523 |
if (len) { |
| 1524 |
mask_to_set &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size)); |
| 1525 |
*p |= mask_to_set; |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
void ntfs_bitmap_clear_le(void *map, unsigned int start, int len) |
| 1530 |
{ |
| 1531 |
bitmap_ulong *p = (bitmap_ulong *)map + BIT_WORD(start); |
| 1532 |
const unsigned int size = start + len; |
| 1533 |
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); |
| 1534 |
bitmap_ulong mask_to_clear = cpu_to_ul(BITMAP_FIRST_WORD_MASK(start)); |
| 1535 |
|
| 1536 |
while (len - bits_to_clear >= 0) { |
| 1537 |
*p &= ~mask_to_clear; |
| 1538 |
len -= bits_to_clear; |
| 1539 |
bits_to_clear = BITS_PER_LONG; |
| 1540 |
mask_to_clear = cpu_to_ul(~0UL); |
| 1541 |
p++; |
| 1542 |
} |
| 1543 |
if (len) { |
| 1544 |
mask_to_clear &= cpu_to_ul(BITMAP_LAST_WORD_MASK(size)); |
| 1545 |
*p &= ~mask_to_clear; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits) |
| 1550 |
{ |
| 1551 |
const ulong *bmp = bitmap; |
| 1552 |
unsigned int k, lim = bits / BITS_PER_LONG; |
| 1553 |
unsigned int w = 0; |
| 1554 |
|
| 1555 |
for (k = 0; k < lim; k++) |
| 1556 |
w += hweight_long(bmp[k]); |
| 1557 |
|
| 1558 |
if (bits % BITS_PER_LONG) { |
| 1559 |
w += hweight_long(ul_to_cpu(((bitmap_ulong *)bitmap)[k]) & |
| 1560 |
BITMAP_LAST_WORD_MASK(bits)); |
| 1561 |
} |
| 1562 |
|
| 1563 |
return w; |
| 1564 |
} |