runlist.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /**
  2. * runlist.c - NTFS runlist handling code. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2004 Anton Altaparmakov
  5. * Copyright (c) 2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "debug.h"
  23. #include "dir.h"
  24. #include "endian.h"
  25. #include "malloc.h"
  26. #include "ntfs.h"
  27. /**
  28. * ntfs_rl_mm - runlist memmove
  29. *
  30. * It is up to the caller to serialize access to the runlist @base.
  31. */
  32. static inline void ntfs_rl_mm(runlist_element *base, int dst, int src,
  33. int size)
  34. {
  35. if (likely((dst != src) && (size > 0)))
  36. memmove(base + dst, base + src, size * sizeof (*base));
  37. }
  38. /**
  39. * ntfs_rl_mc - runlist memory copy
  40. *
  41. * It is up to the caller to serialize access to the runlists @dstbase and
  42. * @srcbase.
  43. */
  44. static inline void ntfs_rl_mc(runlist_element *dstbase, int dst,
  45. runlist_element *srcbase, int src, int size)
  46. {
  47. if (likely(size > 0))
  48. memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase));
  49. }
  50. /**
  51. * ntfs_rl_realloc - Reallocate memory for runlists
  52. * @rl: original runlist
  53. * @old_size: number of runlist elements in the original runlist @rl
  54. * @new_size: number of runlist elements we need space for
  55. *
  56. * As the runlists grow, more memory will be required. To prevent the
  57. * kernel having to allocate and reallocate large numbers of small bits of
  58. * memory, this function returns and entire page of memory.
  59. *
  60. * It is up to the caller to serialize access to the runlist @rl.
  61. *
  62. * N.B. If the new allocation doesn't require a different number of pages in
  63. * memory, the function will return the original pointer.
  64. *
  65. * On success, return a pointer to the newly allocated, or recycled, memory.
  66. * On error, return -errno. The following error codes are defined:
  67. * -ENOMEM - Not enough memory to allocate runlist array.
  68. * -EINVAL - Invalid parameters were passed in.
  69. */
  70. static inline runlist_element *ntfs_rl_realloc(runlist_element *rl,
  71. int old_size, int new_size)
  72. {
  73. runlist_element *new_rl;
  74. old_size = PAGE_ALIGN(old_size * sizeof(*rl));
  75. new_size = PAGE_ALIGN(new_size * sizeof(*rl));
  76. if (old_size == new_size)
  77. return rl;
  78. new_rl = ntfs_malloc_nofs(new_size);
  79. if (unlikely(!new_rl))
  80. return ERR_PTR(-ENOMEM);
  81. if (likely(rl != NULL)) {
  82. if (unlikely(old_size > new_size))
  83. old_size = new_size;
  84. memcpy(new_rl, rl, old_size);
  85. ntfs_free(rl);
  86. }
  87. return new_rl;
  88. }
  89. /**
  90. * ntfs_are_rl_mergeable - test if two runlists can be joined together
  91. * @dst: original runlist
  92. * @src: new runlist to test for mergeability with @dst
  93. *
  94. * Test if two runlists can be joined together. For this, their VCNs and LCNs
  95. * must be adjacent.
  96. *
  97. * It is up to the caller to serialize access to the runlists @dst and @src.
  98. *
  99. * Return: TRUE Success, the runlists can be merged.
  100. * FALSE Failure, the runlists cannot be merged.
  101. */
  102. static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
  103. runlist_element *src)
  104. {
  105. BUG_ON(!dst);
  106. BUG_ON(!src);
  107. if ((dst->lcn < 0) || (src->lcn < 0)) /* Are we merging holes? */
  108. return FALSE;
  109. if ((dst->lcn + dst->length) != src->lcn) /* Are the runs contiguous? */
  110. return FALSE;
  111. if ((dst->vcn + dst->length) != src->vcn) /* Are the runs misaligned? */
  112. return FALSE;
  113. return TRUE;
  114. }
  115. /**
  116. * __ntfs_rl_merge - merge two runlists without testing if they can be merged
  117. * @dst: original, destination runlist
  118. * @src: new runlist to merge with @dst
  119. *
  120. * Merge the two runlists, writing into the destination runlist @dst. The
  121. * caller must make sure the runlists can be merged or this will corrupt the
  122. * destination runlist.
  123. *
  124. * It is up to the caller to serialize access to the runlists @dst and @src.
  125. */
  126. static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
  127. {
  128. dst->length += src->length;
  129. }
  130. /**
  131. * ntfs_rl_append - append a runlist after a given element
  132. * @dst: original runlist to be worked on
  133. * @dsize: number of elements in @dst (including end marker)
  134. * @src: runlist to be inserted into @dst
  135. * @ssize: number of elements in @src (excluding end marker)
  136. * @loc: append the new runlist @src after this element in @dst
  137. *
  138. * Append the runlist @src after element @loc in @dst. Merge the right end of
  139. * the new runlist, if necessary. Adjust the size of the hole before the
  140. * appended runlist.
  141. *
  142. * It is up to the caller to serialize access to the runlists @dst and @src.
  143. *
  144. * On success, return a pointer to the new, combined, runlist. Note, both
  145. * runlists @dst and @src are deallocated before returning so you cannot use
  146. * the pointers for anything any more. (Strictly speaking the returned runlist
  147. * may be the same as @dst but this is irrelevant.)
  148. *
  149. * On error, return -errno. Both runlists are left unmodified. The following
  150. * error codes are defined:
  151. * -ENOMEM - Not enough memory to allocate runlist array.
  152. * -EINVAL - Invalid parameters were passed in.
  153. */
  154. static inline runlist_element *ntfs_rl_append(runlist_element *dst,
  155. int dsize, runlist_element *src, int ssize, int loc)
  156. {
  157. BOOL right;
  158. int magic;
  159. BUG_ON(!dst);
  160. BUG_ON(!src);
  161. /* First, check if the right hand end needs merging. */
  162. right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
  163. /* Space required: @dst size + @src size, less one if we merged. */
  164. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
  165. if (IS_ERR(dst))
  166. return dst;
  167. /*
  168. * We are guaranteed to succeed from here so can start modifying the
  169. * original runlists.
  170. */
  171. /* First, merge the right hand end, if necessary. */
  172. if (right)
  173. __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
  174. magic = loc + ssize;
  175. /* Move the tail of @dst out of the way, then copy in @src. */
  176. ntfs_rl_mm(dst, magic + 1, loc + 1 + right, dsize - loc - 1 - right);
  177. ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
  178. /* Adjust the size of the preceding hole. */
  179. dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
  180. /* We may have changed the length of the file, so fix the end marker */
  181. if (dst[magic + 1].lcn == LCN_ENOENT)
  182. dst[magic + 1].vcn = dst[magic].vcn + dst[magic].length;
  183. return dst;
  184. }
  185. /**
  186. * ntfs_rl_insert - insert a runlist into another
  187. * @dst: original runlist to be worked on
  188. * @dsize: number of elements in @dst (including end marker)
  189. * @src: new runlist to be inserted
  190. * @ssize: number of elements in @src (excluding end marker)
  191. * @loc: insert the new runlist @src before this element in @dst
  192. *
  193. * Insert the runlist @src before element @loc in the runlist @dst. Merge the
  194. * left end of the new runlist, if necessary. Adjust the size of the hole
  195. * after the inserted runlist.
  196. *
  197. * It is up to the caller to serialize access to the runlists @dst and @src.
  198. *
  199. * On success, return a pointer to the new, combined, runlist. Note, both
  200. * runlists @dst and @src are deallocated before returning so you cannot use
  201. * the pointers for anything any more. (Strictly speaking the returned runlist
  202. * may be the same as @dst but this is irrelevant.)
  203. *
  204. * On error, return -errno. Both runlists are left unmodified. The following
  205. * error codes are defined:
  206. * -ENOMEM - Not enough memory to allocate runlist array.
  207. * -EINVAL - Invalid parameters were passed in.
  208. */
  209. static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
  210. int dsize, runlist_element *src, int ssize, int loc)
  211. {
  212. BOOL left = FALSE;
  213. BOOL disc = FALSE; /* Discontinuity */
  214. BOOL hole = FALSE; /* Following a hole */
  215. int magic;
  216. BUG_ON(!dst);
  217. BUG_ON(!src);
  218. /* disc => Discontinuity between the end of @dst and the start of @src.
  219. * This means we might need to insert a hole.
  220. * hole => @dst ends with a hole or an unmapped region which we can
  221. * extend to match the discontinuity. */
  222. if (loc == 0)
  223. disc = (src[0].vcn > 0);
  224. else {
  225. s64 merged_length;
  226. left = ntfs_are_rl_mergeable(dst + loc - 1, src);
  227. merged_length = dst[loc - 1].length;
  228. if (left)
  229. merged_length += src->length;
  230. disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
  231. if (disc)
  232. hole = (dst[loc - 1].lcn == LCN_HOLE);
  233. }
  234. /* Space required: @dst size + @src size, less one if we merged, plus
  235. * one if there was a discontinuity, less one for a trailing hole. */
  236. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc - hole);
  237. if (IS_ERR(dst))
  238. return dst;
  239. /*
  240. * We are guaranteed to succeed from here so can start modifying the
  241. * original runlist.
  242. */
  243. if (left)
  244. __ntfs_rl_merge(dst + loc - 1, src);
  245. magic = loc + ssize - left + disc - hole;
  246. /* Move the tail of @dst out of the way, then copy in @src. */
  247. ntfs_rl_mm(dst, magic, loc, dsize - loc);
  248. ntfs_rl_mc(dst, loc + disc - hole, src, left, ssize - left);
  249. /* Adjust the VCN of the last run ... */
  250. if (dst[magic].lcn <= LCN_HOLE)
  251. dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
  252. /* ... and the length. */
  253. if (dst[magic].lcn == LCN_HOLE || dst[magic].lcn == LCN_RL_NOT_MAPPED)
  254. dst[magic].length = dst[magic + 1].vcn - dst[magic].vcn;
  255. /* Writing beyond the end of the file and there's a discontinuity. */
  256. if (disc) {
  257. if (hole)
  258. dst[loc - 1].length = dst[loc].vcn - dst[loc - 1].vcn;
  259. else {
  260. if (loc > 0) {
  261. dst[loc].vcn = dst[loc - 1].vcn +
  262. dst[loc - 1].length;
  263. dst[loc].length = dst[loc + 1].vcn -
  264. dst[loc].vcn;
  265. } else {
  266. dst[loc].vcn = 0;
  267. dst[loc].length = dst[loc + 1].vcn;
  268. }
  269. dst[loc].lcn = LCN_RL_NOT_MAPPED;
  270. }
  271. magic += hole;
  272. if (dst[magic].lcn == LCN_ENOENT)
  273. dst[magic].vcn = dst[magic - 1].vcn +
  274. dst[magic - 1].length;
  275. }
  276. return dst;
  277. }
  278. /**
  279. * ntfs_rl_replace - overwrite a runlist element with another runlist
  280. * @dst: original runlist to be worked on
  281. * @dsize: number of elements in @dst (including end marker)
  282. * @src: new runlist to be inserted
  283. * @ssize: number of elements in @src (excluding end marker)
  284. * @loc: index in runlist @dst to overwrite with @src
  285. *
  286. * Replace the runlist element @dst at @loc with @src. Merge the left and
  287. * right ends of the inserted runlist, if necessary.
  288. *
  289. * It is up to the caller to serialize access to the runlists @dst and @src.
  290. *
  291. * On success, return a pointer to the new, combined, runlist. Note, both
  292. * runlists @dst and @src are deallocated before returning so you cannot use
  293. * the pointers for anything any more. (Strictly speaking the returned runlist
  294. * may be the same as @dst but this is irrelevant.)
  295. *
  296. * On error, return -errno. Both runlists are left unmodified. The following
  297. * error codes are defined:
  298. * -ENOMEM - Not enough memory to allocate runlist array.
  299. * -EINVAL - Invalid parameters were passed in.
  300. */
  301. static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
  302. int dsize, runlist_element *src, int ssize, int loc)
  303. {
  304. BOOL left = FALSE;
  305. BOOL right;
  306. int magic;
  307. BUG_ON(!dst);
  308. BUG_ON(!src);
  309. /* First, merge the left and right ends, if necessary. */
  310. right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
  311. if (loc > 0)
  312. left = ntfs_are_rl_mergeable(dst + loc - 1, src);
  313. /* Allocate some space. We'll need less if the left, right, or both
  314. * ends were merged. */
  315. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left - right);
  316. if (IS_ERR(dst))
  317. return dst;
  318. /*
  319. * We are guaranteed to succeed from here so can start modifying the
  320. * original runlists.
  321. */
  322. if (right)
  323. __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
  324. if (left)
  325. __ntfs_rl_merge(dst + loc - 1, src);
  326. /* FIXME: What does this mean? (AIA) */
  327. magic = loc + ssize - left;
  328. /* Move the tail of @dst out of the way, then copy in @src. */
  329. ntfs_rl_mm(dst, magic, loc + right + 1, dsize - loc - right - 1);
  330. ntfs_rl_mc(dst, loc, src, left, ssize - left);
  331. /* We may have changed the length of the file, so fix the end marker */
  332. if (dst[magic].lcn == LCN_ENOENT)
  333. dst[magic].vcn = dst[magic - 1].vcn + dst[magic - 1].length;
  334. return dst;
  335. }
  336. /**
  337. * ntfs_rl_split - insert a runlist into the centre of a hole
  338. * @dst: original runlist to be worked on
  339. * @dsize: number of elements in @dst (including end marker)
  340. * @src: new runlist to be inserted
  341. * @ssize: number of elements in @src (excluding end marker)
  342. * @loc: index in runlist @dst at which to split and insert @src
  343. *
  344. * Split the runlist @dst at @loc into two and insert @new in between the two
  345. * fragments. No merging of runlists is necessary. Adjust the size of the
  346. * holes either side.
  347. *
  348. * It is up to the caller to serialize access to the runlists @dst and @src.
  349. *
  350. * On success, return a pointer to the new, combined, runlist. Note, both
  351. * runlists @dst and @src are deallocated before returning so you cannot use
  352. * the pointers for anything any more. (Strictly speaking the returned runlist
  353. * may be the same as @dst but this is irrelevant.)
  354. *
  355. * On error, return -errno. Both runlists are left unmodified. The following
  356. * error codes are defined:
  357. * -ENOMEM - Not enough memory to allocate runlist array.
  358. * -EINVAL - Invalid parameters were passed in.
  359. */
  360. static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
  361. runlist_element *src, int ssize, int loc)
  362. {
  363. BUG_ON(!dst);
  364. BUG_ON(!src);
  365. /* Space required: @dst size + @src size + one new hole. */
  366. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
  367. if (IS_ERR(dst))
  368. return dst;
  369. /*
  370. * We are guaranteed to succeed from here so can start modifying the
  371. * original runlists.
  372. */
  373. /* Move the tail of @dst out of the way, then copy in @src. */
  374. ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
  375. ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
  376. /* Adjust the size of the holes either size of @src. */
  377. dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
  378. dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
  379. dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
  380. return dst;
  381. }
  382. /**
  383. * ntfs_runlists_merge - merge two runlists into one
  384. * @drl: original runlist to be worked on
  385. * @srl: new runlist to be merged into @drl
  386. *
  387. * First we sanity check the two runlists @srl and @drl to make sure that they
  388. * are sensible and can be merged. The runlist @srl must be either after the
  389. * runlist @drl or completely within a hole (or unmapped region) in @drl.
  390. *
  391. * It is up to the caller to serialize access to the runlists @drl and @srl.
  392. *
  393. * Merging of runlists is necessary in two cases:
  394. * 1. When attribute lists are used and a further extent is being mapped.
  395. * 2. When new clusters are allocated to fill a hole or extend a file.
  396. *
  397. * There are four possible ways @srl can be merged. It can:
  398. * - be inserted at the beginning of a hole,
  399. * - split the hole in two and be inserted between the two fragments,
  400. * - be appended at the end of a hole, or it can
  401. * - replace the whole hole.
  402. * It can also be appended to the end of the runlist, which is just a variant
  403. * of the insert case.
  404. *
  405. * On success, return a pointer to the new, combined, runlist. Note, both
  406. * runlists @drl and @srl are deallocated before returning so you cannot use
  407. * the pointers for anything any more. (Strictly speaking the returned runlist
  408. * may be the same as @dst but this is irrelevant.)
  409. *
  410. * On error, return -errno. Both runlists are left unmodified. The following
  411. * error codes are defined:
  412. * -ENOMEM - Not enough memory to allocate runlist array.
  413. * -EINVAL - Invalid parameters were passed in.
  414. * -ERANGE - The runlists overlap and cannot be merged.
  415. */
  416. runlist_element *ntfs_runlists_merge(runlist_element *drl,
  417. runlist_element *srl)
  418. {
  419. int di, si; /* Current index into @[ds]rl. */
  420. int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
  421. int dins; /* Index into @drl at which to insert @srl. */
  422. int dend, send; /* Last index into @[ds]rl. */
  423. int dfinal, sfinal; /* The last index into @[ds]rl with
  424. lcn >= LCN_HOLE. */
  425. int marker = 0;
  426. VCN marker_vcn = 0;
  427. #ifdef DEBUG
  428. ntfs_debug("dst:");
  429. ntfs_debug_dump_runlist(drl);
  430. ntfs_debug("src:");
  431. ntfs_debug_dump_runlist(srl);
  432. #endif
  433. /* Check for silly calling... */
  434. if (unlikely(!srl))
  435. return drl;
  436. if (IS_ERR(srl) || IS_ERR(drl))
  437. return ERR_PTR(-EINVAL);
  438. /* Check for the case where the first mapping is being done now. */
  439. if (unlikely(!drl)) {
  440. drl = srl;
  441. /* Complete the source runlist if necessary. */
  442. if (unlikely(drl[0].vcn)) {
  443. /* Scan to the end of the source runlist. */
  444. for (dend = 0; likely(drl[dend].length); dend++)
  445. ;
  446. drl = ntfs_rl_realloc(drl, dend, dend + 1);
  447. if (IS_ERR(drl))
  448. return drl;
  449. /* Insert start element at the front of the runlist. */
  450. ntfs_rl_mm(drl, 1, 0, dend);
  451. drl[0].vcn = 0;
  452. drl[0].lcn = LCN_RL_NOT_MAPPED;
  453. drl[0].length = drl[1].vcn;
  454. }
  455. goto finished;
  456. }
  457. si = di = 0;
  458. /* Skip any unmapped start element(s) in the source runlist. */
  459. while (srl[si].length && srl[si].lcn < LCN_HOLE)
  460. si++;
  461. /* Can't have an entirely unmapped source runlist. */
  462. BUG_ON(!srl[si].length);
  463. /* Record the starting points. */
  464. sstart = si;
  465. /*
  466. * Skip forward in @drl until we reach the position where @srl needs to
  467. * be inserted. If we reach the end of @drl, @srl just needs to be
  468. * appended to @drl.
  469. */
  470. for (; drl[di].length; di++) {
  471. if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
  472. break;
  473. }
  474. dins = di;
  475. /* Sanity check for illegal overlaps. */
  476. if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
  477. (srl[si].lcn >= 0)) {
  478. ntfs_error(NULL, "Run lists overlap. Cannot merge!");
  479. return ERR_PTR(-ERANGE);
  480. }
  481. /* Scan to the end of both runlists in order to know their sizes. */
  482. for (send = si; srl[send].length; send++)
  483. ;
  484. for (dend = di; drl[dend].length; dend++)
  485. ;
  486. if (srl[send].lcn == LCN_ENOENT)
  487. marker_vcn = srl[marker = send].vcn;
  488. /* Scan to the last element with lcn >= LCN_HOLE. */
  489. for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
  490. ;
  491. for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
  492. ;
  493. {
  494. BOOL start;
  495. BOOL finish;
  496. int ds = dend + 1; /* Number of elements in drl & srl */
  497. int ss = sfinal - sstart + 1;
  498. start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
  499. (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
  500. finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
  501. ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
  502. (srl[send - 1].vcn + srl[send - 1].length)));
  503. /* Or we'll lose an end marker */
  504. if (start && finish && (drl[dins].length == 0))
  505. ss++;
  506. if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
  507. finish = FALSE;
  508. #if 0
  509. ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
  510. ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
  511. ntfs_debug("start = %i, finish = %i", start, finish);
  512. ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
  513. #endif
  514. if (start) {
  515. if (finish)
  516. drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
  517. else
  518. drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
  519. } else {
  520. if (finish)
  521. drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
  522. else
  523. drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
  524. }
  525. if (IS_ERR(drl)) {
  526. ntfs_error(NULL, "Merge failed.");
  527. return drl;
  528. }
  529. ntfs_free(srl);
  530. if (marker) {
  531. ntfs_debug("Triggering marker code.");
  532. for (ds = dend; drl[ds].length; ds++)
  533. ;
  534. /* We only need to care if @srl ended after @drl. */
  535. if (drl[ds].vcn <= marker_vcn) {
  536. int slots = 0;
  537. if (drl[ds].vcn == marker_vcn) {
  538. ntfs_debug("Old marker = 0x%llx, replacing "
  539. "with LCN_ENOENT.",
  540. (unsigned long long)
  541. drl[ds].lcn);
  542. drl[ds].lcn = LCN_ENOENT;
  543. goto finished;
  544. }
  545. /*
  546. * We need to create an unmapped runlist element in
  547. * @drl or extend an existing one before adding the
  548. * ENOENT terminator.
  549. */
  550. if (drl[ds].lcn == LCN_ENOENT) {
  551. ds--;
  552. slots = 1;
  553. }
  554. if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
  555. /* Add an unmapped runlist element. */
  556. if (!slots) {
  557. /* FIXME/TODO: We need to have the
  558. * extra memory already! (AIA) */
  559. drl = ntfs_rl_realloc(drl, ds, ds + 2);
  560. if (!drl)
  561. goto critical_error;
  562. slots = 2;
  563. }
  564. ds++;
  565. /* Need to set vcn if it isn't set already. */
  566. if (slots != 1)
  567. drl[ds].vcn = drl[ds - 1].vcn +
  568. drl[ds - 1].length;
  569. drl[ds].lcn = LCN_RL_NOT_MAPPED;
  570. /* We now used up a slot. */
  571. slots--;
  572. }
  573. drl[ds].length = marker_vcn - drl[ds].vcn;
  574. /* Finally add the ENOENT terminator. */
  575. ds++;
  576. if (!slots) {
  577. /* FIXME/TODO: We need to have the extra
  578. * memory already! (AIA) */
  579. drl = ntfs_rl_realloc(drl, ds, ds + 1);
  580. if (!drl)
  581. goto critical_error;
  582. }
  583. drl[ds].vcn = marker_vcn;
  584. drl[ds].lcn = LCN_ENOENT;
  585. drl[ds].length = (s64)0;
  586. }
  587. }
  588. }
  589. finished:
  590. /* The merge was completed successfully. */
  591. ntfs_debug("Merged runlist:");
  592. ntfs_debug_dump_runlist(drl);
  593. return drl;
  594. critical_error:
  595. /* Critical error! We cannot afford to fail here. */
  596. ntfs_error(NULL, "Critical error! Not enough memory.");
  597. panic("NTFS: Cannot continue.");
  598. }
  599. /**
  600. * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
  601. * @vol: ntfs volume on which the attribute resides
  602. * @attr: attribute record whose mapping pairs array to decompress
  603. * @old_rl: optional runlist in which to insert @attr's runlist
  604. *
  605. * It is up to the caller to serialize access to the runlist @old_rl.
  606. *
  607. * Decompress the attribute @attr's mapping pairs array into a runlist. On
  608. * success, return the decompressed runlist.
  609. *
  610. * If @old_rl is not NULL, decompressed runlist is inserted into the
  611. * appropriate place in @old_rl and the resultant, combined runlist is
  612. * returned. The original @old_rl is deallocated.
  613. *
  614. * On error, return -errno. @old_rl is left unmodified in that case.
  615. *
  616. * The following error codes are defined:
  617. * -ENOMEM - Not enough memory to allocate runlist array.
  618. * -EIO - Corrupt runlist.
  619. * -EINVAL - Invalid parameters were passed in.
  620. * -ERANGE - The two runlists overlap.
  621. *
  622. * FIXME: For now we take the conceptionally simplest approach of creating the
  623. * new runlist disregarding the already existing one and then splicing the
  624. * two into one, if that is possible (we check for overlap and discard the new
  625. * runlist if overlap present before returning ERR_PTR(-ERANGE)).
  626. */
  627. runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
  628. const ATTR_RECORD *attr, runlist_element *old_rl)
  629. {
  630. VCN vcn; /* Current vcn. */
  631. LCN lcn; /* Current lcn. */
  632. s64 deltaxcn; /* Change in [vl]cn. */
  633. runlist_element *rl; /* The output runlist. */
  634. u8 *buf; /* Current position in mapping pairs array. */
  635. u8 *attr_end; /* End of attribute. */
  636. int rlsize; /* Size of runlist buffer. */
  637. u16 rlpos; /* Current runlist position in units of
  638. runlist_elements. */
  639. u8 b; /* Current byte offset in buf. */
  640. #ifdef DEBUG
  641. /* Make sure attr exists and is non-resident. */
  642. if (!attr || !attr->non_resident || sle64_to_cpu(
  643. attr->data.non_resident.lowest_vcn) < (VCN)0) {
  644. ntfs_error(vol->sb, "Invalid arguments.");
  645. return ERR_PTR(-EINVAL);
  646. }
  647. #endif
  648. /* Start at vcn = lowest_vcn and lcn 0. */
  649. vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
  650. lcn = 0;
  651. /* Get start of the mapping pairs array. */
  652. buf = (u8*)attr + le16_to_cpu(
  653. attr->data.non_resident.mapping_pairs_offset);
  654. attr_end = (u8*)attr + le32_to_cpu(attr->length);
  655. if (unlikely(buf < (u8*)attr || buf > attr_end)) {
  656. ntfs_error(vol->sb, "Corrupt attribute.");
  657. return ERR_PTR(-EIO);
  658. }
  659. /* Current position in runlist array. */
  660. rlpos = 0;
  661. /* Allocate first page and set current runlist size to one page. */
  662. rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
  663. if (unlikely(!rl))
  664. return ERR_PTR(-ENOMEM);
  665. /* Insert unmapped starting element if necessary. */
  666. if (vcn) {
  667. rl->vcn = 0;
  668. rl->lcn = LCN_RL_NOT_MAPPED;
  669. rl->length = vcn;
  670. rlpos++;
  671. }
  672. while (buf < attr_end && *buf) {
  673. /*
  674. * Allocate more memory if needed, including space for the
  675. * not-mapped and terminator elements. ntfs_malloc_nofs()
  676. * operates on whole pages only.
  677. */
  678. if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
  679. runlist_element *rl2;
  680. rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
  681. if (unlikely(!rl2)) {
  682. ntfs_free(rl);
  683. return ERR_PTR(-ENOMEM);
  684. }
  685. memcpy(rl2, rl, rlsize);
  686. ntfs_free(rl);
  687. rl = rl2;
  688. rlsize += PAGE_SIZE;
  689. }
  690. /* Enter the current vcn into the current runlist element. */
  691. rl[rlpos].vcn = vcn;
  692. /*
  693. * Get the change in vcn, i.e. the run length in clusters.
  694. * Doing it this way ensures that we signextend negative values.
  695. * A negative run length doesn't make any sense, but hey, I
  696. * didn't make up the NTFS specs and Windows NT4 treats the run
  697. * length as a signed value so that's how it is...
  698. */
  699. b = *buf & 0xf;
  700. if (b) {
  701. if (unlikely(buf + b > attr_end))
  702. goto io_error;
  703. for (deltaxcn = (s8)buf[b--]; b; b--)
  704. deltaxcn = (deltaxcn << 8) + buf[b];
  705. } else { /* The length entry is compulsory. */
  706. ntfs_error(vol->sb, "Missing length entry in mapping "
  707. "pairs array.");
  708. deltaxcn = (s64)-1;
  709. }
  710. /*
  711. * Assume a negative length to indicate data corruption and
  712. * hence clean-up and return NULL.
  713. */
  714. if (unlikely(deltaxcn < 0)) {
  715. ntfs_error(vol->sb, "Invalid length in mapping pairs "
  716. "array.");
  717. goto err_out;
  718. }
  719. /*
  720. * Enter the current run length into the current runlist
  721. * element.
  722. */
  723. rl[rlpos].length = deltaxcn;
  724. /* Increment the current vcn by the current run length. */
  725. vcn += deltaxcn;
  726. /*
  727. * There might be no lcn change at all, as is the case for
  728. * sparse clusters on NTFS 3.0+, in which case we set the lcn
  729. * to LCN_HOLE.
  730. */
  731. if (!(*buf & 0xf0))
  732. rl[rlpos].lcn = LCN_HOLE;
  733. else {
  734. /* Get the lcn change which really can be negative. */
  735. u8 b2 = *buf & 0xf;
  736. b = b2 + ((*buf >> 4) & 0xf);
  737. if (buf + b > attr_end)
  738. goto io_error;
  739. for (deltaxcn = (s8)buf[b--]; b > b2; b--)
  740. deltaxcn = (deltaxcn << 8) + buf[b];
  741. /* Change the current lcn to its new value. */
  742. lcn += deltaxcn;
  743. #ifdef DEBUG
  744. /*
  745. * On NTFS 1.2-, apparently can have lcn == -1 to
  746. * indicate a hole. But we haven't verified ourselves
  747. * whether it is really the lcn or the deltaxcn that is
  748. * -1. So if either is found give us a message so we
  749. * can investigate it further!
  750. */
  751. if (vol->major_ver < 3) {
  752. if (unlikely(deltaxcn == (LCN)-1))
  753. ntfs_error(vol->sb, "lcn delta == -1");
  754. if (unlikely(lcn == (LCN)-1))
  755. ntfs_error(vol->sb, "lcn == -1");
  756. }
  757. #endif
  758. /* Check lcn is not below -1. */
  759. if (unlikely(lcn < (LCN)-1)) {
  760. ntfs_error(vol->sb, "Invalid LCN < -1 in "
  761. "mapping pairs array.");
  762. goto err_out;
  763. }
  764. /* Enter the current lcn into the runlist element. */
  765. rl[rlpos].lcn = lcn;
  766. }
  767. /* Get to the next runlist element. */
  768. rlpos++;
  769. /* Increment the buffer position to the next mapping pair. */
  770. buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
  771. }
  772. if (unlikely(buf >= attr_end))
  773. goto io_error;
  774. /*
  775. * If there is a highest_vcn specified, it must be equal to the final
  776. * vcn in the runlist - 1, or something has gone badly wrong.
  777. */
  778. deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
  779. if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
  780. mpa_err:
  781. ntfs_error(vol->sb, "Corrupt mapping pairs array in "
  782. "non-resident attribute.");
  783. goto err_out;
  784. }
  785. /* Setup not mapped runlist element if this is the base extent. */
  786. if (!attr->data.non_resident.lowest_vcn) {
  787. VCN max_cluster;
  788. max_cluster = (sle64_to_cpu(
  789. attr->data.non_resident.allocated_size) +
  790. vol->cluster_size - 1) >>
  791. vol->cluster_size_bits;
  792. /*
  793. * If there is a difference between the highest_vcn and the
  794. * highest cluster, the runlist is either corrupt or, more
  795. * likely, there are more extents following this one.
  796. */
  797. if (deltaxcn < --max_cluster) {
  798. ntfs_debug("More extents to follow; deltaxcn = 0x%llx, "
  799. "max_cluster = 0x%llx",
  800. (unsigned long long)deltaxcn,
  801. (unsigned long long)max_cluster);
  802. rl[rlpos].vcn = vcn;
  803. vcn += rl[rlpos].length = max_cluster - deltaxcn;
  804. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  805. rlpos++;
  806. } else if (unlikely(deltaxcn > max_cluster)) {
  807. ntfs_error(vol->sb, "Corrupt attribute. deltaxcn = "
  808. "0x%llx, max_cluster = 0x%llx",
  809. (unsigned long long)deltaxcn,
  810. (unsigned long long)max_cluster);
  811. goto mpa_err;
  812. }
  813. rl[rlpos].lcn = LCN_ENOENT;
  814. } else /* Not the base extent. There may be more extents to follow. */
  815. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  816. /* Setup terminating runlist element. */
  817. rl[rlpos].vcn = vcn;
  818. rl[rlpos].length = (s64)0;
  819. /* If no existing runlist was specified, we are done. */
  820. if (!old_rl) {
  821. ntfs_debug("Mapping pairs array successfully decompressed:");
  822. ntfs_debug_dump_runlist(rl);
  823. return rl;
  824. }
  825. /* Now combine the new and old runlists checking for overlaps. */
  826. old_rl = ntfs_runlists_merge(old_rl, rl);
  827. if (likely(!IS_ERR(old_rl)))
  828. return old_rl;
  829. ntfs_free(rl);
  830. ntfs_error(vol->sb, "Failed to merge runlists.");
  831. return old_rl;
  832. io_error:
  833. ntfs_error(vol->sb, "Corrupt attribute.");
  834. err_out:
  835. ntfs_free(rl);
  836. return ERR_PTR(-EIO);
  837. }
  838. /**
  839. * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
  840. * @rl: runlist to use for conversion
  841. * @vcn: vcn to convert
  842. *
  843. * Convert the virtual cluster number @vcn of an attribute into a logical
  844. * cluster number (lcn) of a device using the runlist @rl to map vcns to their
  845. * corresponding lcns.
  846. *
  847. * It is up to the caller to serialize access to the runlist @rl.
  848. *
  849. * Since lcns must be >= 0, we use negative return values with special meaning:
  850. *
  851. * Return value Meaning / Description
  852. * ==================================================
  853. * -1 = LCN_HOLE Hole / not allocated on disk.
  854. * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been
  855. * inserted into the runlist yet.
  856. * -3 = LCN_ENOENT There is no such vcn in the attribute.
  857. *
  858. * Locking: - The caller must have locked the runlist (for reading or writing).
  859. * - This function does not touch the lock.
  860. */
  861. LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
  862. {
  863. int i;
  864. BUG_ON(vcn < 0);
  865. /*
  866. * If rl is NULL, assume that we have found an unmapped runlist. The
  867. * caller can then attempt to map it and fail appropriately if
  868. * necessary.
  869. */
  870. if (unlikely(!rl))
  871. return LCN_RL_NOT_MAPPED;
  872. /* Catch out of lower bounds vcn. */
  873. if (unlikely(vcn < rl[0].vcn))
  874. return LCN_ENOENT;
  875. for (i = 0; likely(rl[i].length); i++) {
  876. if (unlikely(vcn < rl[i+1].vcn)) {
  877. if (likely(rl[i].lcn >= (LCN)0))
  878. return rl[i].lcn + (vcn - rl[i].vcn);
  879. return rl[i].lcn;
  880. }
  881. }
  882. /*
  883. * The terminator element is setup to the correct value, i.e. one of
  884. * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
  885. */
  886. if (likely(rl[i].lcn < (LCN)0))
  887. return rl[i].lcn;
  888. /* Just in case... We could replace this with BUG() some day. */
  889. return LCN_ENOENT;
  890. }
  891. /**
  892. * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
  893. * @n: number for which to get the number of bytes for
  894. *
  895. * Return the number of bytes required to store @n unambiguously as
  896. * a signed number.
  897. *
  898. * This is used in the context of the mapping pairs array to determine how
  899. * many bytes will be needed in the array to store a given logical cluster
  900. * number (lcn) or a specific run length.
  901. *
  902. * Return the number of bytes written. This function cannot fail.
  903. */
  904. static inline int ntfs_get_nr_significant_bytes(const s64 n)
  905. {
  906. s64 l = n;
  907. int i;
  908. s8 j;
  909. i = 0;
  910. do {
  911. l >>= 8;
  912. i++;
  913. } while (l != 0 && l != -1);
  914. j = (n >> 8 * (i - 1)) & 0xff;
  915. /* If the sign bit is wrong, we need an extra byte. */
  916. if ((n < 0 && j >= 0) || (n > 0 && j < 0))
  917. i++;
  918. return i;
  919. }
  920. /**
  921. * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
  922. * @vol: ntfs volume (needed for the ntfs version)
  923. * @rl: locked runlist to determine the size of the mapping pairs of
  924. * @start_vcn: vcn at which to start the mapping pairs array
  925. *
  926. * Walk the locked runlist @rl and calculate the size in bytes of the mapping
  927. * pairs array corresponding to the runlist @rl, starting at vcn @start_vcn.
  928. * This for example allows us to allocate a buffer of the right size when
  929. * building the mapping pairs array.
  930. *
  931. * If @rl is NULL, just return 1 (for the single terminator byte).
  932. *
  933. * Return the calculated size in bytes on success. On error, return -errno.
  934. * The following error codes are defined:
  935. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  936. * fully mapped runlists to this function.
  937. * -EIO - The runlist is corrupt.
  938. *
  939. * Locking: @rl must be locked on entry (either for reading or writing), it
  940. * remains locked throughout, and is left locked upon return.
  941. */
  942. int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
  943. const runlist_element *rl, const VCN start_vcn)
  944. {
  945. LCN prev_lcn;
  946. int rls;
  947. BUG_ON(start_vcn < 0);
  948. if (!rl) {
  949. BUG_ON(start_vcn);
  950. return 1;
  951. }
  952. /* Skip to runlist element containing @start_vcn. */
  953. while (rl->length && start_vcn >= rl[1].vcn)
  954. rl++;
  955. if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
  956. return -EINVAL;
  957. prev_lcn = 0;
  958. /* Always need the termining zero byte. */
  959. rls = 1;
  960. /* Do the first partial run if present. */
  961. if (start_vcn > rl->vcn) {
  962. s64 delta;
  963. /* We know rl->length != 0 already. */
  964. if (rl->length < 0 || rl->lcn < LCN_HOLE)
  965. goto err_out;
  966. delta = start_vcn - rl->vcn;
  967. /* Header byte + length. */
  968. rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta);
  969. /*
  970. * If the logical cluster number (lcn) denotes a hole and we
  971. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  972. * zero space. On earlier NTFS versions we just store the lcn.
  973. * Note: this assumes that on NTFS 1.2-, holes are stored with
  974. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  975. */
  976. if (rl->lcn >= 0 || vol->major_ver < 3) {
  977. prev_lcn = rl->lcn;
  978. if (rl->lcn >= 0)
  979. prev_lcn += delta;
  980. /* Change in lcn. */
  981. rls += ntfs_get_nr_significant_bytes(prev_lcn);
  982. }
  983. /* Go to next runlist element. */
  984. rl++;
  985. }
  986. /* Do the full runs. */
  987. for (; rl->length; rl++) {
  988. if (rl->length < 0 || rl->lcn < LCN_HOLE)
  989. goto err_out;
  990. /* Header byte + length. */
  991. rls += 1 + ntfs_get_nr_significant_bytes(rl->length);
  992. /*
  993. * If the logical cluster number (lcn) denotes a hole and we
  994. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  995. * zero space. On earlier NTFS versions we just store the lcn.
  996. * Note: this assumes that on NTFS 1.2-, holes are stored with
  997. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  998. */
  999. if (rl->lcn >= 0 || vol->major_ver < 3) {
  1000. /* Change in lcn. */
  1001. rls += ntfs_get_nr_significant_bytes(rl->lcn -
  1002. prev_lcn);
  1003. prev_lcn = rl->lcn;
  1004. }
  1005. }
  1006. return rls;
  1007. err_out:
  1008. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1009. rls = -EINVAL;
  1010. else
  1011. rls = -EIO;
  1012. return rls;
  1013. }
  1014. /**
  1015. * ntfs_write_significant_bytes - write the significant bytes of a number
  1016. * @dst: destination buffer to write to
  1017. * @dst_max: pointer to last byte of destination buffer for bounds checking
  1018. * @n: number whose significant bytes to write
  1019. *
  1020. * Store in @dst, the minimum bytes of the number @n which are required to
  1021. * identify @n unambiguously as a signed number, taking care not to exceed
  1022. * @dest_max, the maximum position within @dst to which we are allowed to
  1023. * write.
  1024. *
  1025. * This is used when building the mapping pairs array of a runlist to compress
  1026. * a given logical cluster number (lcn) or a specific run length to the minumum
  1027. * size possible.
  1028. *
  1029. * Return the number of bytes written on success. On error, i.e. the
  1030. * destination buffer @dst is too small, return -ENOSPC.
  1031. */
  1032. static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
  1033. const s64 n)
  1034. {
  1035. s64 l = n;
  1036. int i;
  1037. s8 j;
  1038. i = 0;
  1039. do {
  1040. if (dst > dst_max)
  1041. goto err_out;
  1042. *dst++ = l & 0xffll;
  1043. l >>= 8;
  1044. i++;
  1045. } while (l != 0 && l != -1);
  1046. j = (n >> 8 * (i - 1)) & 0xff;
  1047. /* If the sign bit is wrong, we need an extra byte. */
  1048. if (n < 0 && j >= 0) {
  1049. if (dst > dst_max)
  1050. goto err_out;
  1051. i++;
  1052. *dst = (s8)-1;
  1053. } else if (n > 0 && j < 0) {
  1054. if (dst > dst_max)
  1055. goto err_out;
  1056. i++;
  1057. *dst = (s8)0;
  1058. }
  1059. return i;
  1060. err_out:
  1061. return -ENOSPC;
  1062. }
  1063. /**
  1064. * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
  1065. * @vol: ntfs volume (needed for the ntfs version)
  1066. * @dst: destination buffer to which to write the mapping pairs array
  1067. * @dst_len: size of destination buffer @dst in bytes
  1068. * @rl: locked runlist for which to build the mapping pairs array
  1069. * @start_vcn: vcn at which to start the mapping pairs array
  1070. * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
  1071. *
  1072. * Create the mapping pairs array from the locked runlist @rl, starting at vcn
  1073. * @start_vcn and save the array in @dst. @dst_len is the size of @dst in
  1074. * bytes and it should be at least equal to the value obtained by calling
  1075. * ntfs_get_size_for_mapping_pairs().
  1076. *
  1077. * If @rl is NULL, just write a single terminator byte to @dst.
  1078. *
  1079. * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
  1080. * the first vcn outside the destination buffer. Note that on error, @dst has
  1081. * been filled with all the mapping pairs that will fit, thus it can be treated
  1082. * as partial success, in that a new attribute extent needs to be created or
  1083. * the next extent has to be used and the mapping pairs build has to be
  1084. * continued with @start_vcn set to *@stop_vcn.
  1085. *
  1086. * Return 0 on success and -errno on error. The following error codes are
  1087. * defined:
  1088. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1089. * fully mapped runlists to this function.
  1090. * -EIO - The runlist is corrupt.
  1091. * -ENOSPC - The destination buffer is too small.
  1092. *
  1093. * Locking: @rl must be locked on entry (either for reading or writing), it
  1094. * remains locked throughout, and is left locked upon return.
  1095. */
  1096. int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
  1097. const int dst_len, const runlist_element *rl,
  1098. const VCN start_vcn, VCN *const stop_vcn)
  1099. {
  1100. LCN prev_lcn;
  1101. s8 *dst_max, *dst_next;
  1102. int err = -ENOSPC;
  1103. s8 len_len, lcn_len;
  1104. BUG_ON(start_vcn < 0);
  1105. BUG_ON(dst_len < 1);
  1106. if (!rl) {
  1107. BUG_ON(start_vcn);
  1108. if (stop_vcn)
  1109. *stop_vcn = 0;
  1110. /* Terminator byte. */
  1111. *dst = 0;
  1112. return 0;
  1113. }
  1114. /* Skip to runlist element containing @start_vcn. */
  1115. while (rl->length && start_vcn >= rl[1].vcn)
  1116. rl++;
  1117. if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn)
  1118. return -EINVAL;
  1119. /*
  1120. * @dst_max is used for bounds checking in
  1121. * ntfs_write_significant_bytes().
  1122. */
  1123. dst_max = dst + dst_len - 1;
  1124. prev_lcn = 0;
  1125. /* Do the first partial run if present. */
  1126. if (start_vcn > rl->vcn) {
  1127. s64 delta;
  1128. /* We know rl->length != 0 already. */
  1129. if (rl->length < 0 || rl->lcn < LCN_HOLE)
  1130. goto err_out;
  1131. delta = start_vcn - rl->vcn;
  1132. /* Write length. */
  1133. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1134. rl->length - delta);
  1135. if (len_len < 0)
  1136. goto size_err;
  1137. /*
  1138. * If the logical cluster number (lcn) denotes a hole and we
  1139. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1140. * zero space. On earlier NTFS versions we just write the lcn
  1141. * change. FIXME: Do we need to write the lcn change or just
  1142. * the lcn in that case? Not sure as I have never seen this
  1143. * case on NT4. - We assume that we just need to write the lcn
  1144. * change until someone tells us otherwise... (AIA)
  1145. */
  1146. if (rl->lcn >= 0 || vol->major_ver < 3) {
  1147. prev_lcn = rl->lcn;
  1148. if (rl->lcn >= 0)
  1149. prev_lcn += delta;
  1150. /* Write change in lcn. */
  1151. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1152. len_len, dst_max, prev_lcn);
  1153. if (lcn_len < 0)
  1154. goto size_err;
  1155. } else
  1156. lcn_len = 0;
  1157. dst_next = dst + len_len + lcn_len + 1;
  1158. if (dst_next > dst_max)
  1159. goto size_err;
  1160. /* Update header byte. */
  1161. *dst = lcn_len << 4 | len_len;
  1162. /* Position at next mapping pairs array element. */
  1163. dst = dst_next;
  1164. /* Go to next runlist element. */
  1165. rl++;
  1166. }
  1167. /* Do the full runs. */
  1168. for (; rl->length; rl++) {
  1169. if (rl->length < 0 || rl->lcn < LCN_HOLE)
  1170. goto err_out;
  1171. /* Write length. */
  1172. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1173. rl->length);
  1174. if (len_len < 0)
  1175. goto size_err;
  1176. /*
  1177. * If the logical cluster number (lcn) denotes a hole and we
  1178. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1179. * zero space. On earlier NTFS versions we just write the lcn
  1180. * change. FIXME: Do we need to write the lcn change or just
  1181. * the lcn in that case? Not sure as I have never seen this
  1182. * case on NT4. - We assume that we just need to write the lcn
  1183. * change until someone tells us otherwise... (AIA)
  1184. */
  1185. if (rl->lcn >= 0 || vol->major_ver < 3) {
  1186. /* Write change in lcn. */
  1187. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1188. len_len, dst_max, rl->lcn - prev_lcn);
  1189. if (lcn_len < 0)
  1190. goto size_err;
  1191. prev_lcn = rl->lcn;
  1192. } else
  1193. lcn_len = 0;
  1194. dst_next = dst + len_len + lcn_len + 1;
  1195. if (dst_next > dst_max)
  1196. goto size_err;
  1197. /* Update header byte. */
  1198. *dst = lcn_len << 4 | len_len;
  1199. /* Position at next mapping pairs array element. */
  1200. dst = dst_next;
  1201. }
  1202. /* Success. */
  1203. err = 0;
  1204. size_err:
  1205. /* Set stop vcn. */
  1206. if (stop_vcn)
  1207. *stop_vcn = rl->vcn;
  1208. /* Add terminator byte. */
  1209. *dst = 0;
  1210. return err;
  1211. err_out:
  1212. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1213. err = -EINVAL;
  1214. else
  1215. err = -EIO;
  1216. return err;
  1217. }
  1218. /**
  1219. * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
  1220. * @runlist: runlist to truncate
  1221. * @new_length: the new length of the runlist in VCNs
  1222. *
  1223. * Truncate the runlist described by @runlist as well as the memory buffer
  1224. * holding the runlist elements to a length of @new_length VCNs.
  1225. *
  1226. * If @new_length lies within the runlist, the runlist elements with VCNs of
  1227. * @new_length and above are discarded.
  1228. *
  1229. * If @new_length lies beyond the runlist, a sparse runlist element is added to
  1230. * the end of the runlist @runlist or if the last runlist element is a sparse
  1231. * one already, this is extended.
  1232. *
  1233. * Return 0 on success and -errno on error.
  1234. *
  1235. * Locking: The caller must hold @runlist->lock for writing.
  1236. */
  1237. int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
  1238. const s64 new_length)
  1239. {
  1240. runlist_element *rl;
  1241. int old_size;
  1242. ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
  1243. BUG_ON(!runlist);
  1244. BUG_ON(new_length < 0);
  1245. rl = runlist->rl;
  1246. if (unlikely(!rl)) {
  1247. /*
  1248. * Create a runlist consisting of a sparse runlist element of
  1249. * length @new_length followed by a terminator runlist element.
  1250. */
  1251. rl = ntfs_malloc_nofs(PAGE_SIZE);
  1252. if (unlikely(!rl)) {
  1253. ntfs_error(vol->sb, "Not enough memory to allocate "
  1254. "runlist element buffer.");
  1255. return -ENOMEM;
  1256. }
  1257. runlist->rl = rl;
  1258. rl[1].length = rl->vcn = 0;
  1259. rl->lcn = LCN_HOLE;
  1260. rl[1].vcn = rl->length = new_length;
  1261. rl[1].lcn = LCN_ENOENT;
  1262. return 0;
  1263. }
  1264. BUG_ON(new_length < rl->vcn);
  1265. /* Find @new_length in the runlist. */
  1266. while (likely(rl->length && new_length >= rl[1].vcn))
  1267. rl++;
  1268. /*
  1269. * If not at the end of the runlist we need to shrink it.
  1270. * If at the end of the runlist we need to expand it.
  1271. */
  1272. if (rl->length) {
  1273. runlist_element *trl;
  1274. BOOL is_end;
  1275. ntfs_debug("Shrinking runlist.");
  1276. /* Determine the runlist size. */
  1277. trl = rl + 1;
  1278. while (likely(trl->length))
  1279. trl++;
  1280. old_size = trl - runlist->rl + 1;
  1281. /* Truncate the run. */
  1282. rl->length = new_length - rl->vcn;
  1283. /*
  1284. * If a run was partially truncated, make the following runlist
  1285. * element a terminator.
  1286. */
  1287. is_end = FALSE;
  1288. if (rl->length) {
  1289. rl++;
  1290. if (!rl->length)
  1291. is_end = TRUE;
  1292. rl->vcn = new_length;
  1293. rl->length = 0;
  1294. }
  1295. rl->lcn = LCN_ENOENT;
  1296. /* Reallocate memory if necessary. */
  1297. if (!is_end) {
  1298. int new_size = rl - runlist->rl + 1;
  1299. rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
  1300. if (IS_ERR(rl))
  1301. ntfs_warning(vol->sb, "Failed to shrink "
  1302. "runlist buffer. This just "
  1303. "wastes a bit of memory "
  1304. "temporarily so we ignore it "
  1305. "and return success.");
  1306. else
  1307. runlist->rl = rl;
  1308. }
  1309. } else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
  1310. ntfs_debug("Expanding runlist.");
  1311. /*
  1312. * If there is a previous runlist element and it is a sparse
  1313. * one, extend it. Otherwise need to add a new, sparse runlist
  1314. * element.
  1315. */
  1316. if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
  1317. (rl - 1)->length = new_length - (rl - 1)->vcn;
  1318. else {
  1319. /* Determine the runlist size. */
  1320. old_size = rl - runlist->rl + 1;
  1321. /* Reallocate memory if necessary. */
  1322. rl = ntfs_rl_realloc(runlist->rl, old_size,
  1323. old_size + 1);
  1324. if (IS_ERR(rl)) {
  1325. ntfs_error(vol->sb, "Failed to expand runlist "
  1326. "buffer, aborting.");
  1327. return PTR_ERR(rl);
  1328. }
  1329. runlist->rl = rl;
  1330. /*
  1331. * Set @rl to the same runlist element in the new
  1332. * runlist as before in the old runlist.
  1333. */
  1334. rl += old_size - 1;
  1335. /* Add a new, sparse runlist element. */
  1336. rl->lcn = LCN_HOLE;
  1337. rl->length = new_length - rl->vcn;
  1338. /* Add a new terminator runlist element. */
  1339. rl++;
  1340. rl->length = 0;
  1341. }
  1342. rl->vcn = new_length;
  1343. rl->lcn = LCN_ENOENT;
  1344. } else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
  1345. /* Runlist already has same size as requested. */
  1346. rl->lcn = LCN_ENOENT;
  1347. }
  1348. ntfs_debug("Done.");
  1349. return 0;
  1350. }