run.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. * TODO: try to use extents tree (instead of array)
  7. */
  8. #include <linux/blkdev.h>
  9. #include <linux/fs.h>
  10. #include <linux/log2.h>
  11. #include <linux/overflow.h>
  12. #include "debug.h"
  13. #include "ntfs.h"
  14. #include "ntfs_fs.h"
  15. /* runs_tree is a continues memory. Try to avoid big size. */
  16. #define NTFS3_RUN_MAX_BYTES 0x10000
  17. struct ntfs_run {
  18. CLST vcn; /* Virtual cluster number. */
  19. CLST len; /* Length in clusters. */
  20. CLST lcn; /* Logical cluster number. */
  21. };
  22. /*
  23. * run_lookup - Lookup the index of a MCB entry that is first <= vcn.
  24. *
  25. * Case of success it will return non-zero value and set
  26. * @index parameter to index of entry been found.
  27. * Case of entry missing from list 'index' will be set to
  28. * point to insertion position for the entry question.
  29. */
  30. static bool run_lookup(const struct runs_tree *run, CLST vcn, size_t *index)
  31. {
  32. size_t min_idx, max_idx, mid_idx;
  33. struct ntfs_run *r;
  34. if (!run->count) {
  35. *index = 0;
  36. return false;
  37. }
  38. min_idx = 0;
  39. max_idx = run->count - 1;
  40. /* Check boundary cases specially, 'cause they cover the often requests. */
  41. r = run->runs;
  42. if (vcn < r->vcn) {
  43. *index = 0;
  44. return false;
  45. }
  46. if (vcn < r->vcn + r->len) {
  47. *index = 0;
  48. return true;
  49. }
  50. r += max_idx;
  51. if (vcn >= r->vcn + r->len) {
  52. *index = run->count;
  53. return false;
  54. }
  55. if (vcn >= r->vcn) {
  56. *index = max_idx;
  57. return true;
  58. }
  59. do {
  60. mid_idx = min_idx + ((max_idx - min_idx) >> 1);
  61. r = run->runs + mid_idx;
  62. if (vcn < r->vcn) {
  63. max_idx = mid_idx - 1;
  64. if (!mid_idx)
  65. break;
  66. } else if (vcn >= r->vcn + r->len) {
  67. min_idx = mid_idx + 1;
  68. } else {
  69. *index = mid_idx;
  70. return true;
  71. }
  72. } while (min_idx <= max_idx);
  73. *index = max_idx + 1;
  74. return false;
  75. }
  76. /*
  77. * run_consolidate - Consolidate runs starting from a given one.
  78. */
  79. static void run_consolidate(struct runs_tree *run, size_t index)
  80. {
  81. size_t i;
  82. struct ntfs_run *r = run->runs + index;
  83. while (index + 1 < run->count) {
  84. /*
  85. * I should merge current run with next
  86. * if start of the next run lies inside one being tested.
  87. */
  88. struct ntfs_run *n = r + 1;
  89. CLST end = r->vcn + r->len;
  90. CLST dl;
  91. /* Stop if runs are not aligned one to another. */
  92. if (n->vcn > end)
  93. break;
  94. dl = end - n->vcn;
  95. /*
  96. * If range at index overlaps with next one
  97. * then I will either adjust it's start position
  98. * or (if completely matches) dust remove one from the list.
  99. */
  100. if (dl > 0) {
  101. if (n->len <= dl)
  102. goto remove_next_range;
  103. n->len -= dl;
  104. n->vcn += dl;
  105. if (n->lcn != SPARSE_LCN)
  106. n->lcn += dl;
  107. dl = 0;
  108. }
  109. /*
  110. * Stop if sparse mode does not match
  111. * both current and next runs.
  112. */
  113. if ((n->lcn == SPARSE_LCN) != (r->lcn == SPARSE_LCN)) {
  114. index += 1;
  115. r = n;
  116. continue;
  117. }
  118. /*
  119. * Check if volume block
  120. * of a next run lcn does not match
  121. * last volume block of the current run.
  122. */
  123. if (n->lcn != SPARSE_LCN && n->lcn != r->lcn + r->len)
  124. break;
  125. /*
  126. * Next and current are siblings.
  127. * Eat/join.
  128. */
  129. r->len += n->len - dl;
  130. remove_next_range:
  131. i = run->count - (index + 1);
  132. if (i > 1)
  133. memmove(n, n + 1, sizeof(*n) * (i - 1));
  134. run->count -= 1;
  135. }
  136. }
  137. /*
  138. * run_is_mapped_full
  139. *
  140. * Return: True if range [svcn - evcn] is mapped.
  141. */
  142. bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn)
  143. {
  144. size_t i;
  145. const struct ntfs_run *r, *end;
  146. CLST next_vcn;
  147. if (!run_lookup(run, svcn, &i))
  148. return false;
  149. end = run->runs + run->count;
  150. r = run->runs + i;
  151. for (;;) {
  152. next_vcn = r->vcn + r->len;
  153. if (next_vcn > evcn)
  154. return true;
  155. if (++r >= end)
  156. return false;
  157. if (r->vcn != next_vcn)
  158. return false;
  159. }
  160. }
  161. bool run_lookup_entry(const struct runs_tree *run, CLST vcn, CLST *lcn,
  162. CLST *len, size_t *index)
  163. {
  164. size_t idx;
  165. CLST gap;
  166. struct ntfs_run *r;
  167. /* Fail immediately if nrun was not touched yet. */
  168. if (!run->runs)
  169. return false;
  170. if (!run_lookup(run, vcn, &idx))
  171. return false;
  172. r = run->runs + idx;
  173. if (vcn >= r->vcn + r->len)
  174. return false;
  175. gap = vcn - r->vcn;
  176. if (r->len <= gap)
  177. return false;
  178. *lcn = r->lcn == SPARSE_LCN ? SPARSE_LCN : (r->lcn + gap);
  179. if (len)
  180. *len = r->len - gap;
  181. if (index)
  182. *index = idx;
  183. return true;
  184. }
  185. /*
  186. * run_truncate_head - Decommit the range before vcn.
  187. */
  188. void run_truncate_head(struct runs_tree *run, CLST vcn)
  189. {
  190. size_t index;
  191. struct ntfs_run *r;
  192. if (run_lookup(run, vcn, &index)) {
  193. r = run->runs + index;
  194. if (vcn > r->vcn) {
  195. CLST dlen = vcn - r->vcn;
  196. r->vcn = vcn;
  197. r->len -= dlen;
  198. if (r->lcn != SPARSE_LCN)
  199. r->lcn += dlen;
  200. }
  201. if (!index)
  202. return;
  203. }
  204. r = run->runs;
  205. memmove(r, r + index, sizeof(*r) * (run->count - index));
  206. run->count -= index;
  207. if (!run->count) {
  208. kvfree(run->runs);
  209. run->runs = NULL;
  210. run->allocated = 0;
  211. }
  212. }
  213. /*
  214. * run_truncate - Decommit the range after vcn.
  215. */
  216. void run_truncate(struct runs_tree *run, CLST vcn)
  217. {
  218. size_t index;
  219. /*
  220. * If I hit the range then
  221. * I have to truncate one.
  222. * If range to be truncated is becoming empty
  223. * then it will entirely be removed.
  224. */
  225. if (run_lookup(run, vcn, &index)) {
  226. struct ntfs_run *r = run->runs + index;
  227. r->len = vcn - r->vcn;
  228. if (r->len > 0)
  229. index += 1;
  230. }
  231. /*
  232. * At this point 'index' is set to position that
  233. * should be thrown away (including index itself)
  234. * Simple one - just set the limit.
  235. */
  236. run->count = index;
  237. /* Do not reallocate array 'runs'. Only free if possible. */
  238. if (!index) {
  239. kvfree(run->runs);
  240. run->runs = NULL;
  241. run->allocated = 0;
  242. }
  243. }
  244. /*
  245. * run_truncate_around - Trim head and tail if necessary.
  246. */
  247. void run_truncate_around(struct runs_tree *run, CLST vcn)
  248. {
  249. run_truncate_head(run, vcn);
  250. if (run->count >= NTFS3_RUN_MAX_BYTES / sizeof(struct ntfs_run) / 2)
  251. run_truncate(run, (run->runs + (run->count >> 1))->vcn);
  252. }
  253. /*
  254. * run_add_entry
  255. *
  256. * Sets location to known state.
  257. * Run to be added may overlap with existing location.
  258. *
  259. * Return: false if of memory.
  260. */
  261. bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len,
  262. bool is_mft)
  263. {
  264. size_t used, index;
  265. struct ntfs_run *r;
  266. bool inrange;
  267. CLST tail_vcn = 0, tail_len = 0, tail_lcn = 0;
  268. bool should_add_tail = false;
  269. /*
  270. * Lookup the insertion point.
  271. *
  272. * Execute bsearch for the entry containing
  273. * start position question.
  274. */
  275. inrange = run_lookup(run, vcn, &index);
  276. /*
  277. * Shortcut here would be case of
  278. * range not been found but one been added
  279. * continues previous run.
  280. * This case I can directly make use of
  281. * existing range as my start point.
  282. */
  283. if (!inrange && index > 0) {
  284. struct ntfs_run *t = run->runs + index - 1;
  285. if (t->vcn + t->len == vcn &&
  286. (t->lcn == SPARSE_LCN) == (lcn == SPARSE_LCN) &&
  287. (lcn == SPARSE_LCN || lcn == t->lcn + t->len)) {
  288. inrange = true;
  289. index -= 1;
  290. }
  291. }
  292. /*
  293. * At this point 'index' either points to the range
  294. * containing start position or to the insertion position
  295. * for a new range.
  296. * So first let's check if range I'm probing is here already.
  297. */
  298. if (!inrange) {
  299. requires_new_range:
  300. /*
  301. * Range was not found.
  302. * Insert at position 'index'
  303. */
  304. used = run->count * sizeof(struct ntfs_run);
  305. /*
  306. * Check allocated space.
  307. * If one is not enough to get one more entry
  308. * then it will be reallocated.
  309. */
  310. if (run->allocated < used + sizeof(struct ntfs_run)) {
  311. size_t bytes;
  312. struct ntfs_run *new_ptr;
  313. /* Use power of 2 for 'bytes'. */
  314. if (!used) {
  315. bytes = 64;
  316. } else if (used <= 16 * PAGE_SIZE) {
  317. if (is_power_of_2(run->allocated))
  318. bytes = run->allocated << 1;
  319. else
  320. bytes = (size_t)1
  321. << (2 + blksize_bits(used));
  322. } else {
  323. bytes = run->allocated + (16 * PAGE_SIZE);
  324. }
  325. WARN_ON(!is_mft && bytes > NTFS3_RUN_MAX_BYTES);
  326. new_ptr = kvmalloc(bytes, GFP_KERNEL);
  327. if (!new_ptr)
  328. return false;
  329. r = new_ptr + index;
  330. memcpy(new_ptr, run->runs,
  331. index * sizeof(struct ntfs_run));
  332. memcpy(r + 1, run->runs + index,
  333. sizeof(struct ntfs_run) * (run->count - index));
  334. kvfree(run->runs);
  335. run->runs = new_ptr;
  336. run->allocated = bytes;
  337. } else {
  338. size_t i = run->count - index;
  339. r = run->runs + index;
  340. /* memmove appears to be a bottle neck here... */
  341. if (i > 0)
  342. memmove(r + 1, r, sizeof(struct ntfs_run) * i);
  343. }
  344. r->vcn = vcn;
  345. r->lcn = lcn;
  346. r->len = len;
  347. run->count += 1;
  348. } else {
  349. r = run->runs + index;
  350. /*
  351. * If one of ranges was not allocated then we
  352. * have to split location we just matched and
  353. * insert current one.
  354. * A common case this requires tail to be reinserted
  355. * a recursive call.
  356. */
  357. if (((lcn == SPARSE_LCN) != (r->lcn == SPARSE_LCN)) ||
  358. (lcn != SPARSE_LCN && lcn != r->lcn + (vcn - r->vcn))) {
  359. CLST to_eat = vcn - r->vcn;
  360. CLST Tovcn = to_eat + len;
  361. should_add_tail = Tovcn < r->len;
  362. if (should_add_tail) {
  363. tail_lcn = r->lcn == SPARSE_LCN ?
  364. SPARSE_LCN :
  365. (r->lcn + Tovcn);
  366. tail_vcn = r->vcn + Tovcn;
  367. tail_len = r->len - Tovcn;
  368. }
  369. if (to_eat > 0) {
  370. r->len = to_eat;
  371. inrange = false;
  372. index += 1;
  373. goto requires_new_range;
  374. }
  375. /* lcn should match one were going to add. */
  376. r->lcn = lcn;
  377. }
  378. /*
  379. * If existing range fits then were done.
  380. * Otherwise extend found one and fall back to range jocode.
  381. */
  382. if (r->vcn + r->len < vcn + len)
  383. r->len += len - ((r->vcn + r->len) - vcn);
  384. }
  385. /*
  386. * And normalize it starting from insertion point.
  387. * It's possible that no insertion needed case if
  388. * start point lies within the range of an entry
  389. * that 'index' points to.
  390. */
  391. if (inrange && index > 0)
  392. index -= 1;
  393. run_consolidate(run, index);
  394. run_consolidate(run, index + 1);
  395. /*
  396. * A special case.
  397. * We have to add extra range a tail.
  398. */
  399. if (should_add_tail &&
  400. !run_add_entry(run, tail_vcn, tail_lcn, tail_len, is_mft))
  401. return false;
  402. return true;
  403. }
  404. /* run_collapse_range
  405. *
  406. * Helper for attr_collapse_range(),
  407. * which is helper for fallocate(collapse_range).
  408. */
  409. bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len)
  410. {
  411. size_t index, eat;
  412. struct ntfs_run *r, *e, *eat_start, *eat_end;
  413. CLST end;
  414. if (WARN_ON(!run_lookup(run, vcn, &index)))
  415. return true; /* Should never be here. */
  416. e = run->runs + run->count;
  417. r = run->runs + index;
  418. end = vcn + len;
  419. if (vcn > r->vcn) {
  420. if (r->vcn + r->len <= end) {
  421. /* Collapse tail of run .*/
  422. r->len = vcn - r->vcn;
  423. } else if (r->lcn == SPARSE_LCN) {
  424. /* Collapse a middle part of sparsed run. */
  425. r->len -= len;
  426. } else {
  427. /* Collapse a middle part of normal run, split. */
  428. if (!run_add_entry(run, vcn, SPARSE_LCN, len, false))
  429. return false;
  430. return run_collapse_range(run, vcn, len);
  431. }
  432. r += 1;
  433. }
  434. eat_start = r;
  435. eat_end = r;
  436. for (; r < e; r++) {
  437. CLST d;
  438. if (r->vcn >= end) {
  439. r->vcn -= len;
  440. continue;
  441. }
  442. if (r->vcn + r->len <= end) {
  443. /* Eat this run. */
  444. eat_end = r + 1;
  445. continue;
  446. }
  447. d = end - r->vcn;
  448. if (r->lcn != SPARSE_LCN)
  449. r->lcn += d;
  450. r->len -= d;
  451. r->vcn -= len - d;
  452. }
  453. eat = eat_end - eat_start;
  454. memmove(eat_start, eat_end, (e - eat_end) * sizeof(*r));
  455. run->count -= eat;
  456. return true;
  457. }
  458. /* run_insert_range
  459. *
  460. * Helper for attr_insert_range(),
  461. * which is helper for fallocate(insert_range).
  462. */
  463. bool run_insert_range(struct runs_tree *run, CLST vcn, CLST len)
  464. {
  465. size_t index;
  466. struct ntfs_run *r, *e;
  467. if (WARN_ON(!run_lookup(run, vcn, &index)))
  468. return false; /* Should never be here. */
  469. e = run->runs + run->count;
  470. r = run->runs + index;
  471. if (vcn > r->vcn)
  472. r += 1;
  473. for (; r < e; r++)
  474. r->vcn += len;
  475. r = run->runs + index;
  476. if (vcn > r->vcn) {
  477. /* split fragment. */
  478. CLST len1 = vcn - r->vcn;
  479. CLST len2 = r->len - len1;
  480. CLST lcn2 = r->lcn == SPARSE_LCN ? SPARSE_LCN : (r->lcn + len1);
  481. r->len = len1;
  482. if (!run_add_entry(run, vcn + len, lcn2, len2, false))
  483. return false;
  484. }
  485. if (!run_add_entry(run, vcn, SPARSE_LCN, len, false))
  486. return false;
  487. return true;
  488. }
  489. /*
  490. * run_get_entry - Return index-th mapped region.
  491. */
  492. bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn,
  493. CLST *lcn, CLST *len)
  494. {
  495. const struct ntfs_run *r;
  496. if (index >= run->count)
  497. return false;
  498. r = run->runs + index;
  499. if (!r->len)
  500. return false;
  501. if (vcn)
  502. *vcn = r->vcn;
  503. if (lcn)
  504. *lcn = r->lcn;
  505. if (len)
  506. *len = r->len;
  507. return true;
  508. }
  509. /*
  510. * run_packed_size - Calculate the size of packed int64.
  511. */
  512. #ifdef __BIG_ENDIAN
  513. static inline int run_packed_size(const s64 n)
  514. {
  515. const u8 *p = (const u8 *)&n + sizeof(n) - 1;
  516. if (n >= 0) {
  517. if (p[-7] || p[-6] || p[-5] || p[-4])
  518. p -= 4;
  519. if (p[-3] || p[-2])
  520. p -= 2;
  521. if (p[-1])
  522. p -= 1;
  523. if (p[0] & 0x80)
  524. p -= 1;
  525. } else {
  526. if (p[-7] != 0xff || p[-6] != 0xff || p[-5] != 0xff ||
  527. p[-4] != 0xff)
  528. p -= 4;
  529. if (p[-3] != 0xff || p[-2] != 0xff)
  530. p -= 2;
  531. if (p[-1] != 0xff)
  532. p -= 1;
  533. if (!(p[0] & 0x80))
  534. p -= 1;
  535. }
  536. return (const u8 *)&n + sizeof(n) - p;
  537. }
  538. /* Full trusted function. It does not check 'size' for errors. */
  539. static inline void run_pack_s64(u8 *run_buf, u8 size, s64 v)
  540. {
  541. const u8 *p = (u8 *)&v;
  542. switch (size) {
  543. case 8:
  544. run_buf[7] = p[0];
  545. fallthrough;
  546. case 7:
  547. run_buf[6] = p[1];
  548. fallthrough;
  549. case 6:
  550. run_buf[5] = p[2];
  551. fallthrough;
  552. case 5:
  553. run_buf[4] = p[3];
  554. fallthrough;
  555. case 4:
  556. run_buf[3] = p[4];
  557. fallthrough;
  558. case 3:
  559. run_buf[2] = p[5];
  560. fallthrough;
  561. case 2:
  562. run_buf[1] = p[6];
  563. fallthrough;
  564. case 1:
  565. run_buf[0] = p[7];
  566. }
  567. }
  568. /* Full trusted function. It does not check 'size' for errors. */
  569. static inline s64 run_unpack_s64(const u8 *run_buf, u8 size, s64 v)
  570. {
  571. u8 *p = (u8 *)&v;
  572. switch (size) {
  573. case 8:
  574. p[0] = run_buf[7];
  575. fallthrough;
  576. case 7:
  577. p[1] = run_buf[6];
  578. fallthrough;
  579. case 6:
  580. p[2] = run_buf[5];
  581. fallthrough;
  582. case 5:
  583. p[3] = run_buf[4];
  584. fallthrough;
  585. case 4:
  586. p[4] = run_buf[3];
  587. fallthrough;
  588. case 3:
  589. p[5] = run_buf[2];
  590. fallthrough;
  591. case 2:
  592. p[6] = run_buf[1];
  593. fallthrough;
  594. case 1:
  595. p[7] = run_buf[0];
  596. }
  597. return v;
  598. }
  599. #else
  600. static inline int run_packed_size(const s64 n)
  601. {
  602. const u8 *p = (const u8 *)&n;
  603. if (n >= 0) {
  604. if (p[7] || p[6] || p[5] || p[4])
  605. p += 4;
  606. if (p[3] || p[2])
  607. p += 2;
  608. if (p[1])
  609. p += 1;
  610. if (p[0] & 0x80)
  611. p += 1;
  612. } else {
  613. if (p[7] != 0xff || p[6] != 0xff || p[5] != 0xff ||
  614. p[4] != 0xff)
  615. p += 4;
  616. if (p[3] != 0xff || p[2] != 0xff)
  617. p += 2;
  618. if (p[1] != 0xff)
  619. p += 1;
  620. if (!(p[0] & 0x80))
  621. p += 1;
  622. }
  623. return 1 + p - (const u8 *)&n;
  624. }
  625. /* Full trusted function. It does not check 'size' for errors. */
  626. static inline void run_pack_s64(u8 *run_buf, u8 size, s64 v)
  627. {
  628. const u8 *p = (u8 *)&v;
  629. /* memcpy( run_buf, &v, size); Is it faster? */
  630. switch (size) {
  631. case 8:
  632. run_buf[7] = p[7];
  633. fallthrough;
  634. case 7:
  635. run_buf[6] = p[6];
  636. fallthrough;
  637. case 6:
  638. run_buf[5] = p[5];
  639. fallthrough;
  640. case 5:
  641. run_buf[4] = p[4];
  642. fallthrough;
  643. case 4:
  644. run_buf[3] = p[3];
  645. fallthrough;
  646. case 3:
  647. run_buf[2] = p[2];
  648. fallthrough;
  649. case 2:
  650. run_buf[1] = p[1];
  651. fallthrough;
  652. case 1:
  653. run_buf[0] = p[0];
  654. }
  655. }
  656. /* full trusted function. It does not check 'size' for errors */
  657. static inline s64 run_unpack_s64(const u8 *run_buf, u8 size, s64 v)
  658. {
  659. u8 *p = (u8 *)&v;
  660. /* memcpy( &v, run_buf, size); Is it faster? */
  661. switch (size) {
  662. case 8:
  663. p[7] = run_buf[7];
  664. fallthrough;
  665. case 7:
  666. p[6] = run_buf[6];
  667. fallthrough;
  668. case 6:
  669. p[5] = run_buf[5];
  670. fallthrough;
  671. case 5:
  672. p[4] = run_buf[4];
  673. fallthrough;
  674. case 4:
  675. p[3] = run_buf[3];
  676. fallthrough;
  677. case 3:
  678. p[2] = run_buf[2];
  679. fallthrough;
  680. case 2:
  681. p[1] = run_buf[1];
  682. fallthrough;
  683. case 1:
  684. p[0] = run_buf[0];
  685. }
  686. return v;
  687. }
  688. #endif
  689. /*
  690. * run_pack - Pack runs into buffer.
  691. *
  692. * packed_vcns - How much runs we have packed.
  693. * packed_size - How much bytes we have used run_buf.
  694. */
  695. int run_pack(const struct runs_tree *run, CLST svcn, CLST len, u8 *run_buf,
  696. u32 run_buf_size, CLST *packed_vcns)
  697. {
  698. CLST next_vcn, vcn, lcn;
  699. CLST prev_lcn = 0;
  700. CLST evcn1 = svcn + len;
  701. const struct ntfs_run *r, *r_end;
  702. int packed_size = 0;
  703. size_t i;
  704. s64 dlcn;
  705. int offset_size, size_size, tmp;
  706. *packed_vcns = 0;
  707. if (!len)
  708. goto out;
  709. /* Check all required entries [svcn, encv1) available. */
  710. if (!run_lookup(run, svcn, &i))
  711. return -ENOENT;
  712. r_end = run->runs + run->count;
  713. r = run->runs + i;
  714. for (next_vcn = r->vcn + r->len; next_vcn < evcn1;
  715. next_vcn = r->vcn + r->len) {
  716. if (++r >= r_end || r->vcn != next_vcn)
  717. return -ENOENT;
  718. }
  719. /* Repeat cycle above and pack runs. Assume no errors. */
  720. r = run->runs + i;
  721. len = svcn - r->vcn;
  722. vcn = svcn;
  723. lcn = r->lcn == SPARSE_LCN ? SPARSE_LCN : (r->lcn + len);
  724. len = r->len - len;
  725. for (;;) {
  726. next_vcn = vcn + len;
  727. if (next_vcn > evcn1)
  728. len = evcn1 - vcn;
  729. /* How much bytes required to pack len. */
  730. size_size = run_packed_size(len);
  731. /* offset_size - How much bytes is packed dlcn. */
  732. if (lcn == SPARSE_LCN) {
  733. offset_size = 0;
  734. dlcn = 0;
  735. } else {
  736. /* NOTE: lcn can be less than prev_lcn! */
  737. dlcn = (s64)lcn - prev_lcn;
  738. offset_size = run_packed_size(dlcn);
  739. prev_lcn = lcn;
  740. }
  741. tmp = run_buf_size - packed_size - 2 - offset_size;
  742. if (tmp <= 0)
  743. goto out;
  744. /* Can we store this entire run. */
  745. if (tmp < size_size)
  746. goto out;
  747. if (run_buf) {
  748. /* Pack run header. */
  749. run_buf[0] = ((u8)(size_size | (offset_size << 4)));
  750. run_buf += 1;
  751. /* Pack the length of run. */
  752. run_pack_s64(run_buf, size_size, len);
  753. run_buf += size_size;
  754. /* Pack the offset from previous LCN. */
  755. run_pack_s64(run_buf, offset_size, dlcn);
  756. run_buf += offset_size;
  757. }
  758. packed_size += 1 + offset_size + size_size;
  759. *packed_vcns += len;
  760. if (packed_size + 1 >= run_buf_size || next_vcn >= evcn1)
  761. goto out;
  762. r += 1;
  763. vcn = r->vcn;
  764. lcn = r->lcn;
  765. len = r->len;
  766. }
  767. out:
  768. /* Store last zero. */
  769. if (run_buf)
  770. run_buf[0] = 0;
  771. return packed_size + 1;
  772. }
  773. /*
  774. * run_unpack - Unpack packed runs from @run_buf.
  775. *
  776. * Return: Error if negative, or real used bytes.
  777. */
  778. int run_unpack(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
  779. CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
  780. int run_buf_size)
  781. {
  782. u64 prev_lcn, vcn64, lcn, next_vcn;
  783. const u8 *run_last, *run_0;
  784. bool is_mft = ino == MFT_REC_MFT;
  785. if (run_buf_size < 0)
  786. return -EINVAL;
  787. /* Check for empty. */
  788. if (evcn + 1 == svcn)
  789. return 0;
  790. if (evcn < svcn)
  791. return -EINVAL;
  792. run_0 = run_buf;
  793. run_last = run_buf + run_buf_size;
  794. prev_lcn = 0;
  795. vcn64 = svcn;
  796. /* Read all runs the chain. */
  797. /* size_size - How much bytes is packed len. */
  798. while (run_buf < run_last) {
  799. /* size_size - How much bytes is packed len. */
  800. u8 size_size = *run_buf & 0xF;
  801. /* offset_size - How much bytes is packed dlcn. */
  802. u8 offset_size = *run_buf++ >> 4;
  803. u64 len;
  804. if (!size_size)
  805. break;
  806. /*
  807. * Unpack runs.
  808. * NOTE: Runs are stored little endian order
  809. * "len" is unsigned value, "dlcn" is signed.
  810. * Large positive number requires to store 5 bytes
  811. * e.g.: 05 FF 7E FF FF 00 00 00
  812. */
  813. if (size_size > sizeof(len))
  814. return -EINVAL;
  815. len = run_unpack_s64(run_buf, size_size, 0);
  816. /* Skip size_size. */
  817. run_buf += size_size;
  818. if (!len)
  819. return -EINVAL;
  820. if (!offset_size)
  821. lcn = SPARSE_LCN64;
  822. else if (offset_size <= sizeof(s64)) {
  823. s64 dlcn;
  824. /* Initial value of dlcn is -1 or 0. */
  825. dlcn = (run_buf[offset_size - 1] & 0x80) ? (s64)-1 : 0;
  826. dlcn = run_unpack_s64(run_buf, offset_size, dlcn);
  827. /* Skip offset_size. */
  828. run_buf += offset_size;
  829. if (!dlcn)
  830. return -EINVAL;
  831. if (check_add_overflow(prev_lcn, dlcn, &lcn))
  832. return -EINVAL;
  833. prev_lcn = lcn;
  834. } else {
  835. /* The size of 'dlcn' can't be > 8. */
  836. return -EINVAL;
  837. }
  838. if (check_add_overflow(vcn64, len, &next_vcn))
  839. return -EINVAL;
  840. /* Check boundary. */
  841. if (next_vcn > evcn + 1)
  842. return -EINVAL;
  843. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  844. if (next_vcn > 0x100000000ull || (lcn + len) > 0x100000000ull) {
  845. ntfs_err(
  846. sbi->sb,
  847. "This driver is compiled without CONFIG_NTFS3_64BIT_CLUSTER (like windows driver).\n"
  848. "Volume contains 64 bits run: vcn %llx, lcn %llx, len %llx.\n"
  849. "Activate CONFIG_NTFS3_64BIT_CLUSTER to process this case",
  850. vcn64, lcn, len);
  851. return -EOPNOTSUPP;
  852. }
  853. #endif
  854. if (lcn != SPARSE_LCN64 && lcn + len > sbi->used.bitmap.nbits) {
  855. /* LCN range is out of volume. */
  856. return -EINVAL;
  857. }
  858. if (!run)
  859. ; /* Called from check_attr(fslog.c) to check run. */
  860. else if (run == RUN_DEALLOCATE) {
  861. /*
  862. * Called from ni_delete_all to free clusters
  863. * without storing in run.
  864. */
  865. if (lcn != SPARSE_LCN64)
  866. mark_as_free_ex(sbi, lcn, len, true);
  867. } else if (vcn64 >= vcn) {
  868. if (!run_add_entry(run, vcn64, lcn, len, is_mft))
  869. return -ENOMEM;
  870. } else if (next_vcn > vcn) {
  871. u64 dlen = vcn - vcn64;
  872. if (!run_add_entry(run, vcn, lcn + dlen, len - dlen,
  873. is_mft))
  874. return -ENOMEM;
  875. }
  876. vcn64 = next_vcn;
  877. }
  878. if (vcn64 != evcn + 1) {
  879. /* Not expected length of unpacked runs. */
  880. return -EINVAL;
  881. }
  882. return run_buf - run_0;
  883. }
  884. #ifdef NTFS3_CHECK_FREE_CLST
  885. /*
  886. * run_unpack_ex - Unpack packed runs from "run_buf".
  887. *
  888. * Checks unpacked runs to be used in bitmap.
  889. *
  890. * Return: Error if negative, or real used bytes.
  891. */
  892. int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
  893. CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
  894. int run_buf_size)
  895. {
  896. int ret, err;
  897. CLST next_vcn, lcn, len;
  898. size_t index, done;
  899. bool ok, zone;
  900. struct wnd_bitmap *wnd;
  901. ret = run_unpack(run, sbi, ino, svcn, evcn, vcn, run_buf, run_buf_size);
  902. if (ret <= 0)
  903. return ret;
  904. if (!sbi->used.bitmap.sb || !run || run == RUN_DEALLOCATE)
  905. return ret;
  906. if (ino == MFT_REC_BADCLUST)
  907. return ret;
  908. next_vcn = vcn = svcn;
  909. wnd = &sbi->used.bitmap;
  910. for (ok = run_lookup_entry(run, vcn, &lcn, &len, &index);
  911. next_vcn <= evcn;
  912. ok = run_get_entry(run, ++index, &vcn, &lcn, &len)) {
  913. if (!ok || next_vcn != vcn)
  914. return -EINVAL;
  915. next_vcn = vcn + len;
  916. if (lcn == SPARSE_LCN)
  917. continue;
  918. if (sbi->flags & NTFS_FLAGS_NEED_REPLAY)
  919. continue;
  920. down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
  921. zone = max(wnd->zone_bit, lcn) < min(wnd->zone_end, lcn + len);
  922. /* Check for free blocks. */
  923. ok = !zone && wnd_is_used(wnd, lcn, len);
  924. up_read(&wnd->rw_lock);
  925. if (ok)
  926. continue;
  927. /* Looks like volume is corrupted. */
  928. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  929. if (!down_write_trylock(&wnd->rw_lock))
  930. continue;
  931. if (zone) {
  932. /*
  933. * Range [lcn, lcn + len) intersects with zone.
  934. * To avoid complex with zone just turn it off.
  935. */
  936. wnd_zone_set(wnd, 0, 0);
  937. }
  938. /* Mark all zero bits as used in range [lcn, lcn+len). */
  939. err = wnd_set_used_safe(wnd, lcn, len, &done);
  940. if (zone) {
  941. /* Restore zone. Lock mft run. */
  942. struct rw_semaphore *lock;
  943. lock = is_mounted(sbi) ? &sbi->mft.ni->file.run_lock :
  944. NULL;
  945. if (lock)
  946. down_read(lock);
  947. ntfs_refresh_zone(sbi);
  948. if (lock)
  949. up_read(lock);
  950. }
  951. up_write(&wnd->rw_lock);
  952. if (err)
  953. return err;
  954. }
  955. return ret;
  956. }
  957. #endif
  958. /*
  959. * run_get_highest_vcn
  960. *
  961. * Return the highest vcn from a mapping pairs array
  962. * it used while replaying log file.
  963. */
  964. int run_get_highest_vcn(CLST vcn, const u8 *run_buf, u64 *highest_vcn)
  965. {
  966. u64 vcn64 = vcn;
  967. u8 size_size;
  968. while ((size_size = *run_buf & 0xF)) {
  969. u8 offset_size = *run_buf++ >> 4;
  970. u64 len;
  971. if (size_size > 8 || offset_size > 8)
  972. return -EINVAL;
  973. len = run_unpack_s64(run_buf, size_size, 0);
  974. if (!len)
  975. return -EINVAL;
  976. run_buf += size_size + offset_size;
  977. if (check_add_overflow(vcn64, len, &vcn64))
  978. return -EINVAL;
  979. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  980. if (vcn64 > 0x100000000ull)
  981. return -EINVAL;
  982. #endif
  983. }
  984. *highest_vcn = vcn64 - 1;
  985. return 0;
  986. }
  987. /*
  988. * run_clone
  989. *
  990. * Make a copy of run
  991. */
  992. int run_clone(const struct runs_tree *run, struct runs_tree *new_run)
  993. {
  994. size_t bytes = run->count * sizeof(struct ntfs_run);
  995. if (bytes > new_run->allocated) {
  996. struct ntfs_run *new_ptr = kvmalloc(bytes, GFP_KERNEL);
  997. if (!new_ptr)
  998. return -ENOMEM;
  999. kvfree(new_run->runs);
  1000. new_run->runs = new_ptr;
  1001. new_run->allocated = bytes;
  1002. }
  1003. memcpy(new_run->runs, run->runs, bytes);
  1004. new_run->count = run->count;
  1005. return 0;
  1006. }