runlist.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. /**
  2. * runlist.c - NTFS runlist handling code. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 Anton Altaparmakov
  5. * Copyright (c) 2002-2005 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 an 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_rl_realloc_nofail - Reallocate memory for runlists
  91. * @rl: original runlist
  92. * @old_size: number of runlist elements in the original runlist @rl
  93. * @new_size: number of runlist elements we need space for
  94. *
  95. * As the runlists grow, more memory will be required. To prevent the
  96. * kernel having to allocate and reallocate large numbers of small bits of
  97. * memory, this function returns an entire page of memory.
  98. *
  99. * This function guarantees that the allocation will succeed. It will sleep
  100. * for as long as it takes to complete the allocation.
  101. *
  102. * It is up to the caller to serialize access to the runlist @rl.
  103. *
  104. * N.B. If the new allocation doesn't require a different number of pages in
  105. * memory, the function will return the original pointer.
  106. *
  107. * On success, return a pointer to the newly allocated, or recycled, memory.
  108. * On error, return -errno. The following error codes are defined:
  109. * -ENOMEM - Not enough memory to allocate runlist array.
  110. * -EINVAL - Invalid parameters were passed in.
  111. */
  112. static inline runlist_element *ntfs_rl_realloc_nofail(runlist_element *rl,
  113. int old_size, int new_size)
  114. {
  115. runlist_element *new_rl;
  116. old_size = PAGE_ALIGN(old_size * sizeof(*rl));
  117. new_size = PAGE_ALIGN(new_size * sizeof(*rl));
  118. if (old_size == new_size)
  119. return rl;
  120. new_rl = ntfs_malloc_nofs_nofail(new_size);
  121. BUG_ON(!new_rl);
  122. if (likely(rl != NULL)) {
  123. if (unlikely(old_size > new_size))
  124. old_size = new_size;
  125. memcpy(new_rl, rl, old_size);
  126. ntfs_free(rl);
  127. }
  128. return new_rl;
  129. }
  130. /**
  131. * ntfs_are_rl_mergeable - test if two runlists can be joined together
  132. * @dst: original runlist
  133. * @src: new runlist to test for mergeability with @dst
  134. *
  135. * Test if two runlists can be joined together. For this, their VCNs and LCNs
  136. * must be adjacent.
  137. *
  138. * It is up to the caller to serialize access to the runlists @dst and @src.
  139. *
  140. * Return: TRUE Success, the runlists can be merged.
  141. * FALSE Failure, the runlists cannot be merged.
  142. */
  143. static inline BOOL ntfs_are_rl_mergeable(runlist_element *dst,
  144. runlist_element *src)
  145. {
  146. BUG_ON(!dst);
  147. BUG_ON(!src);
  148. /* We can merge unmapped regions even if they are misaligned. */
  149. if ((dst->lcn == LCN_RL_NOT_MAPPED) && (src->lcn == LCN_RL_NOT_MAPPED))
  150. return TRUE;
  151. /* If the runs are misaligned, we cannot merge them. */
  152. if ((dst->vcn + dst->length) != src->vcn)
  153. return FALSE;
  154. /* If both runs are non-sparse and contiguous, we can merge them. */
  155. if ((dst->lcn >= 0) && (src->lcn >= 0) &&
  156. ((dst->lcn + dst->length) == src->lcn))
  157. return TRUE;
  158. /* If we are merging two holes, we can merge them. */
  159. if ((dst->lcn == LCN_HOLE) && (src->lcn == LCN_HOLE))
  160. return TRUE;
  161. /* Cannot merge. */
  162. return FALSE;
  163. }
  164. /**
  165. * __ntfs_rl_merge - merge two runlists without testing if they can be merged
  166. * @dst: original, destination runlist
  167. * @src: new runlist to merge with @dst
  168. *
  169. * Merge the two runlists, writing into the destination runlist @dst. The
  170. * caller must make sure the runlists can be merged or this will corrupt the
  171. * destination runlist.
  172. *
  173. * It is up to the caller to serialize access to the runlists @dst and @src.
  174. */
  175. static inline void __ntfs_rl_merge(runlist_element *dst, runlist_element *src)
  176. {
  177. dst->length += src->length;
  178. }
  179. /**
  180. * ntfs_rl_append - append a runlist after a given element
  181. * @dst: original runlist to be worked on
  182. * @dsize: number of elements in @dst (including end marker)
  183. * @src: runlist to be inserted into @dst
  184. * @ssize: number of elements in @src (excluding end marker)
  185. * @loc: append the new runlist @src after this element in @dst
  186. *
  187. * Append the runlist @src after element @loc in @dst. Merge the right end of
  188. * the new runlist, if necessary. Adjust the size of the hole before the
  189. * appended runlist.
  190. *
  191. * It is up to the caller to serialize access to the runlists @dst and @src.
  192. *
  193. * On success, return a pointer to the new, combined, runlist. Note, both
  194. * runlists @dst and @src are deallocated before returning so you cannot use
  195. * the pointers for anything any more. (Strictly speaking the returned runlist
  196. * may be the same as @dst but this is irrelevant.)
  197. *
  198. * On error, return -errno. Both runlists are left unmodified. The following
  199. * error codes are defined:
  200. * -ENOMEM - Not enough memory to allocate runlist array.
  201. * -EINVAL - Invalid parameters were passed in.
  202. */
  203. static inline runlist_element *ntfs_rl_append(runlist_element *dst,
  204. int dsize, runlist_element *src, int ssize, int loc)
  205. {
  206. BOOL right = FALSE; /* Right end of @src needs merging. */
  207. int marker; /* End of the inserted runs. */
  208. BUG_ON(!dst);
  209. BUG_ON(!src);
  210. /* First, check if the right hand end needs merging. */
  211. if ((loc + 1) < dsize)
  212. right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
  213. /* Space required: @dst size + @src size, less one if we merged. */
  214. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right);
  215. if (IS_ERR(dst))
  216. return dst;
  217. /*
  218. * We are guaranteed to succeed from here so can start modifying the
  219. * original runlists.
  220. */
  221. /* First, merge the right hand end, if necessary. */
  222. if (right)
  223. __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
  224. /* First run after the @src runs that have been inserted. */
  225. marker = loc + ssize + 1;
  226. /* Move the tail of @dst out of the way, then copy in @src. */
  227. ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - (loc + 1 + right));
  228. ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
  229. /* Adjust the size of the preceding hole. */
  230. dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
  231. /* We may have changed the length of the file, so fix the end marker */
  232. if (dst[marker].lcn == LCN_ENOENT)
  233. dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
  234. return dst;
  235. }
  236. /**
  237. * ntfs_rl_insert - insert a runlist into another
  238. * @dst: original runlist to be worked on
  239. * @dsize: number of elements in @dst (including end marker)
  240. * @src: new runlist to be inserted
  241. * @ssize: number of elements in @src (excluding end marker)
  242. * @loc: insert the new runlist @src before this element in @dst
  243. *
  244. * Insert the runlist @src before element @loc in the runlist @dst. Merge the
  245. * left end of the new runlist, if necessary. Adjust the size of the hole
  246. * after the inserted runlist.
  247. *
  248. * It is up to the caller to serialize access to the runlists @dst and @src.
  249. *
  250. * On success, return a pointer to the new, combined, runlist. Note, both
  251. * runlists @dst and @src are deallocated before returning so you cannot use
  252. * the pointers for anything any more. (Strictly speaking the returned runlist
  253. * may be the same as @dst but this is irrelevant.)
  254. *
  255. * On error, return -errno. Both runlists are left unmodified. The following
  256. * error codes are defined:
  257. * -ENOMEM - Not enough memory to allocate runlist array.
  258. * -EINVAL - Invalid parameters were passed in.
  259. */
  260. static inline runlist_element *ntfs_rl_insert(runlist_element *dst,
  261. int dsize, runlist_element *src, int ssize, int loc)
  262. {
  263. BOOL left = FALSE; /* Left end of @src needs merging. */
  264. BOOL disc = FALSE; /* Discontinuity between @dst and @src. */
  265. int marker; /* End of the inserted runs. */
  266. BUG_ON(!dst);
  267. BUG_ON(!src);
  268. /*
  269. * disc => Discontinuity between the end of @dst and the start of @src.
  270. * This means we might need to insert a "not mapped" run.
  271. */
  272. if (loc == 0)
  273. disc = (src[0].vcn > 0);
  274. else {
  275. s64 merged_length;
  276. left = ntfs_are_rl_mergeable(dst + loc - 1, src);
  277. merged_length = dst[loc - 1].length;
  278. if (left)
  279. merged_length += src->length;
  280. disc = (src[0].vcn > dst[loc - 1].vcn + merged_length);
  281. }
  282. /*
  283. * Space required: @dst size + @src size, less one if we merged, plus
  284. * one if there was a discontinuity.
  285. */
  286. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc);
  287. if (IS_ERR(dst))
  288. return dst;
  289. /*
  290. * We are guaranteed to succeed from here so can start modifying the
  291. * original runlist.
  292. */
  293. if (left)
  294. __ntfs_rl_merge(dst + loc - 1, src);
  295. /*
  296. * First run after the @src runs that have been inserted.
  297. * Nominally, @marker equals @loc + @ssize, i.e. location + number of
  298. * runs in @src. However, if @left, then the first run in @src has
  299. * been merged with one in @dst. And if @disc, then @dst and @src do
  300. * not meet and we need an extra run to fill the gap.
  301. */
  302. marker = loc + ssize - left + disc;
  303. /* Move the tail of @dst out of the way, then copy in @src. */
  304. ntfs_rl_mm(dst, marker, loc, dsize - loc);
  305. ntfs_rl_mc(dst, loc + disc, src, left, ssize - left);
  306. /* Adjust the VCN of the first run after the insertion... */
  307. dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
  308. /* ... and the length. */
  309. if (dst[marker].lcn == LCN_HOLE || dst[marker].lcn == LCN_RL_NOT_MAPPED)
  310. dst[marker].length = dst[marker + 1].vcn - dst[marker].vcn;
  311. /* Writing beyond the end of the file and there is a discontinuity. */
  312. if (disc) {
  313. if (loc > 0) {
  314. dst[loc].vcn = dst[loc - 1].vcn + dst[loc - 1].length;
  315. dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn;
  316. } else {
  317. dst[loc].vcn = 0;
  318. dst[loc].length = dst[loc + 1].vcn;
  319. }
  320. dst[loc].lcn = LCN_RL_NOT_MAPPED;
  321. }
  322. return dst;
  323. }
  324. /**
  325. * ntfs_rl_replace - overwrite a runlist element with another runlist
  326. * @dst: original runlist to be worked on
  327. * @dsize: number of elements in @dst (including end marker)
  328. * @src: new runlist to be inserted
  329. * @ssize: number of elements in @src (excluding end marker)
  330. * @loc: index in runlist @dst to overwrite with @src
  331. *
  332. * Replace the runlist element @dst at @loc with @src. Merge the left and
  333. * right ends of the inserted runlist, if necessary.
  334. *
  335. * It is up to the caller to serialize access to the runlists @dst and @src.
  336. *
  337. * On success, return a pointer to the new, combined, runlist. Note, both
  338. * runlists @dst and @src are deallocated before returning so you cannot use
  339. * the pointers for anything any more. (Strictly speaking the returned runlist
  340. * may be the same as @dst but this is irrelevant.)
  341. *
  342. * On error, return -errno. Both runlists are left unmodified. The following
  343. * error codes are defined:
  344. * -ENOMEM - Not enough memory to allocate runlist array.
  345. * -EINVAL - Invalid parameters were passed in.
  346. */
  347. static inline runlist_element *ntfs_rl_replace(runlist_element *dst,
  348. int dsize, runlist_element *src, int ssize, int loc)
  349. {
  350. BOOL left = FALSE; /* Left end of @src needs merging. */
  351. BOOL right = FALSE; /* Right end of @src needs merging. */
  352. int tail; /* Start of tail of @dst. */
  353. int marker; /* End of the inserted runs. */
  354. BUG_ON(!dst);
  355. BUG_ON(!src);
  356. /* First, see if the left and right ends need merging. */
  357. if ((loc + 1) < dsize)
  358. right = ntfs_are_rl_mergeable(src + ssize - 1, dst + loc + 1);
  359. if (loc > 0)
  360. left = ntfs_are_rl_mergeable(dst + loc - 1, src);
  361. /*
  362. * Allocate some space. We will need less if the left, right, or both
  363. * ends get merged.
  364. */
  365. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left - right);
  366. if (IS_ERR(dst))
  367. return dst;
  368. /*
  369. * We are guaranteed to succeed from here so can start modifying the
  370. * original runlists.
  371. */
  372. /* First, merge the left and right ends, if necessary. */
  373. if (right)
  374. __ntfs_rl_merge(src + ssize - 1, dst + loc + 1);
  375. if (left)
  376. __ntfs_rl_merge(dst + loc - 1, src);
  377. /*
  378. * Offset of the tail of @dst. This needs to be moved out of the way
  379. * to make space for the runs to be copied from @src, i.e. the first
  380. * run of the tail of @dst.
  381. * Nominally, @tail equals @loc + 1, i.e. location, skipping the
  382. * replaced run. However, if @right, then one of @dst's runs is
  383. * already merged into @src.
  384. */
  385. tail = loc + right + 1;
  386. /*
  387. * First run after the @src runs that have been inserted, i.e. where
  388. * the tail of @dst needs to be moved to.
  389. * Nominally, @marker equals @loc + @ssize, i.e. location + number of
  390. * runs in @src. However, if @left, then the first run in @src has
  391. * been merged with one in @dst.
  392. */
  393. marker = loc + ssize - left;
  394. /* Move the tail of @dst out of the way, then copy in @src. */
  395. ntfs_rl_mm(dst, marker, tail, dsize - tail);
  396. ntfs_rl_mc(dst, loc, src, left, ssize - left);
  397. /* We may have changed the length of the file, so fix the end marker. */
  398. if (dsize - tail > 0 && dst[marker].lcn == LCN_ENOENT)
  399. dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length;
  400. return dst;
  401. }
  402. /**
  403. * ntfs_rl_split - insert a runlist into the centre of a hole
  404. * @dst: original runlist to be worked on
  405. * @dsize: number of elements in @dst (including end marker)
  406. * @src: new runlist to be inserted
  407. * @ssize: number of elements in @src (excluding end marker)
  408. * @loc: index in runlist @dst at which to split and insert @src
  409. *
  410. * Split the runlist @dst at @loc into two and insert @new in between the two
  411. * fragments. No merging of runlists is necessary. Adjust the size of the
  412. * holes either side.
  413. *
  414. * It is up to the caller to serialize access to the runlists @dst and @src.
  415. *
  416. * On success, return a pointer to the new, combined, runlist. Note, both
  417. * runlists @dst and @src are deallocated before returning so you cannot use
  418. * the pointers for anything any more. (Strictly speaking the returned runlist
  419. * may be the same as @dst but this is irrelevant.)
  420. *
  421. * On error, return -errno. Both runlists are left unmodified. The following
  422. * error codes are defined:
  423. * -ENOMEM - Not enough memory to allocate runlist array.
  424. * -EINVAL - Invalid parameters were passed in.
  425. */
  426. static inline runlist_element *ntfs_rl_split(runlist_element *dst, int dsize,
  427. runlist_element *src, int ssize, int loc)
  428. {
  429. BUG_ON(!dst);
  430. BUG_ON(!src);
  431. /* Space required: @dst size + @src size + one new hole. */
  432. dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1);
  433. if (IS_ERR(dst))
  434. return dst;
  435. /*
  436. * We are guaranteed to succeed from here so can start modifying the
  437. * original runlists.
  438. */
  439. /* Move the tail of @dst out of the way, then copy in @src. */
  440. ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc);
  441. ntfs_rl_mc(dst, loc + 1, src, 0, ssize);
  442. /* Adjust the size of the holes either size of @src. */
  443. dst[loc].length = dst[loc+1].vcn - dst[loc].vcn;
  444. dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length;
  445. dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn;
  446. return dst;
  447. }
  448. /**
  449. * ntfs_runlists_merge - merge two runlists into one
  450. * @drl: original runlist to be worked on
  451. * @srl: new runlist to be merged into @drl
  452. *
  453. * First we sanity check the two runlists @srl and @drl to make sure that they
  454. * are sensible and can be merged. The runlist @srl must be either after the
  455. * runlist @drl or completely within a hole (or unmapped region) in @drl.
  456. *
  457. * It is up to the caller to serialize access to the runlists @drl and @srl.
  458. *
  459. * Merging of runlists is necessary in two cases:
  460. * 1. When attribute lists are used and a further extent is being mapped.
  461. * 2. When new clusters are allocated to fill a hole or extend a file.
  462. *
  463. * There are four possible ways @srl can be merged. It can:
  464. * - be inserted at the beginning of a hole,
  465. * - split the hole in two and be inserted between the two fragments,
  466. * - be appended at the end of a hole, or it can
  467. * - replace the whole hole.
  468. * It can also be appended to the end of the runlist, which is just a variant
  469. * of the insert case.
  470. *
  471. * On success, return a pointer to the new, combined, runlist. Note, both
  472. * runlists @drl and @srl are deallocated before returning so you cannot use
  473. * the pointers for anything any more. (Strictly speaking the returned runlist
  474. * may be the same as @dst but this is irrelevant.)
  475. *
  476. * On error, return -errno. Both runlists are left unmodified. The following
  477. * error codes are defined:
  478. * -ENOMEM - Not enough memory to allocate runlist array.
  479. * -EINVAL - Invalid parameters were passed in.
  480. * -ERANGE - The runlists overlap and cannot be merged.
  481. */
  482. runlist_element *ntfs_runlists_merge(runlist_element *drl,
  483. runlist_element *srl)
  484. {
  485. int di, si; /* Current index into @[ds]rl. */
  486. int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */
  487. int dins; /* Index into @drl at which to insert @srl. */
  488. int dend, send; /* Last index into @[ds]rl. */
  489. int dfinal, sfinal; /* The last index into @[ds]rl with
  490. lcn >= LCN_HOLE. */
  491. int marker = 0;
  492. VCN marker_vcn = 0;
  493. #ifdef DEBUG
  494. ntfs_debug("dst:");
  495. ntfs_debug_dump_runlist(drl);
  496. ntfs_debug("src:");
  497. ntfs_debug_dump_runlist(srl);
  498. #endif
  499. /* Check for silly calling... */
  500. if (unlikely(!srl))
  501. return drl;
  502. if (IS_ERR(srl) || IS_ERR(drl))
  503. return ERR_PTR(-EINVAL);
  504. /* Check for the case where the first mapping is being done now. */
  505. if (unlikely(!drl)) {
  506. drl = srl;
  507. /* Complete the source runlist if necessary. */
  508. if (unlikely(drl[0].vcn)) {
  509. /* Scan to the end of the source runlist. */
  510. for (dend = 0; likely(drl[dend].length); dend++)
  511. ;
  512. dend++;
  513. drl = ntfs_rl_realloc(drl, dend, dend + 1);
  514. if (IS_ERR(drl))
  515. return drl;
  516. /* Insert start element at the front of the runlist. */
  517. ntfs_rl_mm(drl, 1, 0, dend);
  518. drl[0].vcn = 0;
  519. drl[0].lcn = LCN_RL_NOT_MAPPED;
  520. drl[0].length = drl[1].vcn;
  521. }
  522. goto finished;
  523. }
  524. si = di = 0;
  525. /* Skip any unmapped start element(s) in the source runlist. */
  526. while (srl[si].length && srl[si].lcn < LCN_HOLE)
  527. si++;
  528. /* Can't have an entirely unmapped source runlist. */
  529. BUG_ON(!srl[si].length);
  530. /* Record the starting points. */
  531. sstart = si;
  532. /*
  533. * Skip forward in @drl until we reach the position where @srl needs to
  534. * be inserted. If we reach the end of @drl, @srl just needs to be
  535. * appended to @drl.
  536. */
  537. for (; drl[di].length; di++) {
  538. if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
  539. break;
  540. }
  541. dins = di;
  542. /* Sanity check for illegal overlaps. */
  543. if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
  544. (srl[si].lcn >= 0)) {
  545. ntfs_error(NULL, "Run lists overlap. Cannot merge!");
  546. return ERR_PTR(-ERANGE);
  547. }
  548. /* Scan to the end of both runlists in order to know their sizes. */
  549. for (send = si; srl[send].length; send++)
  550. ;
  551. for (dend = di; drl[dend].length; dend++)
  552. ;
  553. if (srl[send].lcn == LCN_ENOENT)
  554. marker_vcn = srl[marker = send].vcn;
  555. /* Scan to the last element with lcn >= LCN_HOLE. */
  556. for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
  557. ;
  558. for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
  559. ;
  560. {
  561. BOOL start;
  562. BOOL finish;
  563. int ds = dend + 1; /* Number of elements in drl & srl */
  564. int ss = sfinal - sstart + 1;
  565. start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
  566. (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
  567. finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
  568. ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
  569. (srl[send - 1].vcn + srl[send - 1].length)));
  570. /* Or we will lose an end marker. */
  571. if (finish && !drl[dins].length)
  572. ss++;
  573. if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
  574. finish = FALSE;
  575. #if 0
  576. ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
  577. ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
  578. ntfs_debug("start = %i, finish = %i", start, finish);
  579. ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
  580. #endif
  581. if (start) {
  582. if (finish)
  583. drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
  584. else
  585. drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
  586. } else {
  587. if (finish)
  588. drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
  589. else
  590. drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
  591. }
  592. if (IS_ERR(drl)) {
  593. ntfs_error(NULL, "Merge failed.");
  594. return drl;
  595. }
  596. ntfs_free(srl);
  597. if (marker) {
  598. ntfs_debug("Triggering marker code.");
  599. for (ds = dend; drl[ds].length; ds++)
  600. ;
  601. /* We only need to care if @srl ended after @drl. */
  602. if (drl[ds].vcn <= marker_vcn) {
  603. int slots = 0;
  604. if (drl[ds].vcn == marker_vcn) {
  605. ntfs_debug("Old marker = 0x%llx, replacing "
  606. "with LCN_ENOENT.",
  607. (unsigned long long)
  608. drl[ds].lcn);
  609. drl[ds].lcn = LCN_ENOENT;
  610. goto finished;
  611. }
  612. /*
  613. * We need to create an unmapped runlist element in
  614. * @drl or extend an existing one before adding the
  615. * ENOENT terminator.
  616. */
  617. if (drl[ds].lcn == LCN_ENOENT) {
  618. ds--;
  619. slots = 1;
  620. }
  621. if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
  622. /* Add an unmapped runlist element. */
  623. if (!slots) {
  624. drl = ntfs_rl_realloc_nofail(drl, ds,
  625. ds + 2);
  626. slots = 2;
  627. }
  628. ds++;
  629. /* Need to set vcn if it isn't set already. */
  630. if (slots != 1)
  631. drl[ds].vcn = drl[ds - 1].vcn +
  632. drl[ds - 1].length;
  633. drl[ds].lcn = LCN_RL_NOT_MAPPED;
  634. /* We now used up a slot. */
  635. slots--;
  636. }
  637. drl[ds].length = marker_vcn - drl[ds].vcn;
  638. /* Finally add the ENOENT terminator. */
  639. ds++;
  640. if (!slots)
  641. drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
  642. drl[ds].vcn = marker_vcn;
  643. drl[ds].lcn = LCN_ENOENT;
  644. drl[ds].length = (s64)0;
  645. }
  646. }
  647. }
  648. finished:
  649. /* The merge was completed successfully. */
  650. ntfs_debug("Merged runlist:");
  651. ntfs_debug_dump_runlist(drl);
  652. return drl;
  653. }
  654. /**
  655. * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
  656. * @vol: ntfs volume on which the attribute resides
  657. * @attr: attribute record whose mapping pairs array to decompress
  658. * @old_rl: optional runlist in which to insert @attr's runlist
  659. *
  660. * It is up to the caller to serialize access to the runlist @old_rl.
  661. *
  662. * Decompress the attribute @attr's mapping pairs array into a runlist. On
  663. * success, return the decompressed runlist.
  664. *
  665. * If @old_rl is not NULL, decompressed runlist is inserted into the
  666. * appropriate place in @old_rl and the resultant, combined runlist is
  667. * returned. The original @old_rl is deallocated.
  668. *
  669. * On error, return -errno. @old_rl is left unmodified in that case.
  670. *
  671. * The following error codes are defined:
  672. * -ENOMEM - Not enough memory to allocate runlist array.
  673. * -EIO - Corrupt runlist.
  674. * -EINVAL - Invalid parameters were passed in.
  675. * -ERANGE - The two runlists overlap.
  676. *
  677. * FIXME: For now we take the conceptionally simplest approach of creating the
  678. * new runlist disregarding the already existing one and then splicing the
  679. * two into one, if that is possible (we check for overlap and discard the new
  680. * runlist if overlap present before returning ERR_PTR(-ERANGE)).
  681. */
  682. runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
  683. const ATTR_RECORD *attr, runlist_element *old_rl)
  684. {
  685. VCN vcn; /* Current vcn. */
  686. LCN lcn; /* Current lcn. */
  687. s64 deltaxcn; /* Change in [vl]cn. */
  688. runlist_element *rl; /* The output runlist. */
  689. u8 *buf; /* Current position in mapping pairs array. */
  690. u8 *attr_end; /* End of attribute. */
  691. int rlsize; /* Size of runlist buffer. */
  692. u16 rlpos; /* Current runlist position in units of
  693. runlist_elements. */
  694. u8 b; /* Current byte offset in buf. */
  695. #ifdef DEBUG
  696. /* Make sure attr exists and is non-resident. */
  697. if (!attr || !attr->non_resident || sle64_to_cpu(
  698. attr->data.non_resident.lowest_vcn) < (VCN)0) {
  699. ntfs_error(vol->sb, "Invalid arguments.");
  700. return ERR_PTR(-EINVAL);
  701. }
  702. #endif
  703. /* Start at vcn = lowest_vcn and lcn 0. */
  704. vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
  705. lcn = 0;
  706. /* Get start of the mapping pairs array. */
  707. buf = (u8*)attr + le16_to_cpu(
  708. attr->data.non_resident.mapping_pairs_offset);
  709. attr_end = (u8*)attr + le32_to_cpu(attr->length);
  710. if (unlikely(buf < (u8*)attr || buf > attr_end)) {
  711. ntfs_error(vol->sb, "Corrupt attribute.");
  712. return ERR_PTR(-EIO);
  713. }
  714. /* If the mapping pairs array is valid but empty, nothing to do. */
  715. if (!vcn && !*buf)
  716. return old_rl;
  717. /* Current position in runlist array. */
  718. rlpos = 0;
  719. /* Allocate first page and set current runlist size to one page. */
  720. rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
  721. if (unlikely(!rl))
  722. return ERR_PTR(-ENOMEM);
  723. /* Insert unmapped starting element if necessary. */
  724. if (vcn) {
  725. rl->vcn = 0;
  726. rl->lcn = LCN_RL_NOT_MAPPED;
  727. rl->length = vcn;
  728. rlpos++;
  729. }
  730. while (buf < attr_end && *buf) {
  731. /*
  732. * Allocate more memory if needed, including space for the
  733. * not-mapped and terminator elements. ntfs_malloc_nofs()
  734. * operates on whole pages only.
  735. */
  736. if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
  737. runlist_element *rl2;
  738. rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
  739. if (unlikely(!rl2)) {
  740. ntfs_free(rl);
  741. return ERR_PTR(-ENOMEM);
  742. }
  743. memcpy(rl2, rl, rlsize);
  744. ntfs_free(rl);
  745. rl = rl2;
  746. rlsize += PAGE_SIZE;
  747. }
  748. /* Enter the current vcn into the current runlist element. */
  749. rl[rlpos].vcn = vcn;
  750. /*
  751. * Get the change in vcn, i.e. the run length in clusters.
  752. * Doing it this way ensures that we signextend negative values.
  753. * A negative run length doesn't make any sense, but hey, I
  754. * didn't make up the NTFS specs and Windows NT4 treats the run
  755. * length as a signed value so that's how it is...
  756. */
  757. b = *buf & 0xf;
  758. if (b) {
  759. if (unlikely(buf + b > attr_end))
  760. goto io_error;
  761. for (deltaxcn = (s8)buf[b--]; b; b--)
  762. deltaxcn = (deltaxcn << 8) + buf[b];
  763. } else { /* The length entry is compulsory. */
  764. ntfs_error(vol->sb, "Missing length entry in mapping "
  765. "pairs array.");
  766. deltaxcn = (s64)-1;
  767. }
  768. /*
  769. * Assume a negative length to indicate data corruption and
  770. * hence clean-up and return NULL.
  771. */
  772. if (unlikely(deltaxcn < 0)) {
  773. ntfs_error(vol->sb, "Invalid length in mapping pairs "
  774. "array.");
  775. goto err_out;
  776. }
  777. /*
  778. * Enter the current run length into the current runlist
  779. * element.
  780. */
  781. rl[rlpos].length = deltaxcn;
  782. /* Increment the current vcn by the current run length. */
  783. vcn += deltaxcn;
  784. /*
  785. * There might be no lcn change at all, as is the case for
  786. * sparse clusters on NTFS 3.0+, in which case we set the lcn
  787. * to LCN_HOLE.
  788. */
  789. if (!(*buf & 0xf0))
  790. rl[rlpos].lcn = LCN_HOLE;
  791. else {
  792. /* Get the lcn change which really can be negative. */
  793. u8 b2 = *buf & 0xf;
  794. b = b2 + ((*buf >> 4) & 0xf);
  795. if (buf + b > attr_end)
  796. goto io_error;
  797. for (deltaxcn = (s8)buf[b--]; b > b2; b--)
  798. deltaxcn = (deltaxcn << 8) + buf[b];
  799. /* Change the current lcn to its new value. */
  800. lcn += deltaxcn;
  801. #ifdef DEBUG
  802. /*
  803. * On NTFS 1.2-, apparently can have lcn == -1 to
  804. * indicate a hole. But we haven't verified ourselves
  805. * whether it is really the lcn or the deltaxcn that is
  806. * -1. So if either is found give us a message so we
  807. * can investigate it further!
  808. */
  809. if (vol->major_ver < 3) {
  810. if (unlikely(deltaxcn == (LCN)-1))
  811. ntfs_error(vol->sb, "lcn delta == -1");
  812. if (unlikely(lcn == (LCN)-1))
  813. ntfs_error(vol->sb, "lcn == -1");
  814. }
  815. #endif
  816. /* Check lcn is not below -1. */
  817. if (unlikely(lcn < (LCN)-1)) {
  818. ntfs_error(vol->sb, "Invalid LCN < -1 in "
  819. "mapping pairs array.");
  820. goto err_out;
  821. }
  822. /* Enter the current lcn into the runlist element. */
  823. rl[rlpos].lcn = lcn;
  824. }
  825. /* Get to the next runlist element. */
  826. rlpos++;
  827. /* Increment the buffer position to the next mapping pair. */
  828. buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
  829. }
  830. if (unlikely(buf >= attr_end))
  831. goto io_error;
  832. /*
  833. * If there is a highest_vcn specified, it must be equal to the final
  834. * vcn in the runlist - 1, or something has gone badly wrong.
  835. */
  836. deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
  837. if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
  838. mpa_err:
  839. ntfs_error(vol->sb, "Corrupt mapping pairs array in "
  840. "non-resident attribute.");
  841. goto err_out;
  842. }
  843. /* Setup not mapped runlist element if this is the base extent. */
  844. if (!attr->data.non_resident.lowest_vcn) {
  845. VCN max_cluster;
  846. max_cluster = ((sle64_to_cpu(
  847. attr->data.non_resident.allocated_size) +
  848. vol->cluster_size - 1) >>
  849. vol->cluster_size_bits) - 1;
  850. /*
  851. * A highest_vcn of zero means this is a single extent
  852. * attribute so simply terminate the runlist with LCN_ENOENT).
  853. */
  854. if (deltaxcn) {
  855. /*
  856. * If there is a difference between the highest_vcn and
  857. * the highest cluster, the runlist is either corrupt
  858. * or, more likely, there are more extents following
  859. * this one.
  860. */
  861. if (deltaxcn < max_cluster) {
  862. ntfs_debug("More extents to follow; deltaxcn "
  863. "= 0x%llx, max_cluster = "
  864. "0x%llx",
  865. (unsigned long long)deltaxcn,
  866. (unsigned long long)
  867. max_cluster);
  868. rl[rlpos].vcn = vcn;
  869. vcn += rl[rlpos].length = max_cluster -
  870. deltaxcn;
  871. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  872. rlpos++;
  873. } else if (unlikely(deltaxcn > max_cluster)) {
  874. ntfs_error(vol->sb, "Corrupt attribute. "
  875. "deltaxcn = 0x%llx, "
  876. "max_cluster = 0x%llx",
  877. (unsigned long long)deltaxcn,
  878. (unsigned long long)
  879. max_cluster);
  880. goto mpa_err;
  881. }
  882. }
  883. rl[rlpos].lcn = LCN_ENOENT;
  884. } else /* Not the base extent. There may be more extents to follow. */
  885. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  886. /* Setup terminating runlist element. */
  887. rl[rlpos].vcn = vcn;
  888. rl[rlpos].length = (s64)0;
  889. /* If no existing runlist was specified, we are done. */
  890. if (!old_rl) {
  891. ntfs_debug("Mapping pairs array successfully decompressed:");
  892. ntfs_debug_dump_runlist(rl);
  893. return rl;
  894. }
  895. /* Now combine the new and old runlists checking for overlaps. */
  896. old_rl = ntfs_runlists_merge(old_rl, rl);
  897. if (likely(!IS_ERR(old_rl)))
  898. return old_rl;
  899. ntfs_free(rl);
  900. ntfs_error(vol->sb, "Failed to merge runlists.");
  901. return old_rl;
  902. io_error:
  903. ntfs_error(vol->sb, "Corrupt attribute.");
  904. err_out:
  905. ntfs_free(rl);
  906. return ERR_PTR(-EIO);
  907. }
  908. /**
  909. * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
  910. * @rl: runlist to use for conversion
  911. * @vcn: vcn to convert
  912. *
  913. * Convert the virtual cluster number @vcn of an attribute into a logical
  914. * cluster number (lcn) of a device using the runlist @rl to map vcns to their
  915. * corresponding lcns.
  916. *
  917. * It is up to the caller to serialize access to the runlist @rl.
  918. *
  919. * Since lcns must be >= 0, we use negative return codes with special meaning:
  920. *
  921. * Return code Meaning / Description
  922. * ==================================================
  923. * LCN_HOLE Hole / not allocated on disk.
  924. * LCN_RL_NOT_MAPPED This is part of the runlist which has not been
  925. * inserted into the runlist yet.
  926. * LCN_ENOENT There is no such vcn in the attribute.
  927. *
  928. * Locking: - The caller must have locked the runlist (for reading or writing).
  929. * - This function does not touch the lock, nor does it modify the
  930. * runlist.
  931. */
  932. LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
  933. {
  934. int i;
  935. BUG_ON(vcn < 0);
  936. /*
  937. * If rl is NULL, assume that we have found an unmapped runlist. The
  938. * caller can then attempt to map it and fail appropriately if
  939. * necessary.
  940. */
  941. if (unlikely(!rl))
  942. return LCN_RL_NOT_MAPPED;
  943. /* Catch out of lower bounds vcn. */
  944. if (unlikely(vcn < rl[0].vcn))
  945. return LCN_ENOENT;
  946. for (i = 0; likely(rl[i].length); i++) {
  947. if (unlikely(vcn < rl[i+1].vcn)) {
  948. if (likely(rl[i].lcn >= (LCN)0))
  949. return rl[i].lcn + (vcn - rl[i].vcn);
  950. return rl[i].lcn;
  951. }
  952. }
  953. /*
  954. * The terminator element is setup to the correct value, i.e. one of
  955. * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
  956. */
  957. if (likely(rl[i].lcn < (LCN)0))
  958. return rl[i].lcn;
  959. /* Just in case... We could replace this with BUG() some day. */
  960. return LCN_ENOENT;
  961. }
  962. #ifdef NTFS_RW
  963. /**
  964. * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
  965. * @rl: runlist to search
  966. * @vcn: vcn to find
  967. *
  968. * Find the virtual cluster number @vcn in the runlist @rl and return the
  969. * address of the runlist element containing the @vcn on success.
  970. *
  971. * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
  972. * the runlist.
  973. *
  974. * Locking: The runlist must be locked on entry.
  975. */
  976. runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
  977. {
  978. BUG_ON(vcn < 0);
  979. if (unlikely(!rl || vcn < rl[0].vcn))
  980. return NULL;
  981. while (likely(rl->length)) {
  982. if (unlikely(vcn < rl[1].vcn)) {
  983. if (likely(rl->lcn >= LCN_HOLE))
  984. return rl;
  985. return NULL;
  986. }
  987. rl++;
  988. }
  989. if (likely(rl->lcn == LCN_ENOENT))
  990. return rl;
  991. return NULL;
  992. }
  993. /**
  994. * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
  995. * @n: number for which to get the number of bytes for
  996. *
  997. * Return the number of bytes required to store @n unambiguously as
  998. * a signed number.
  999. *
  1000. * This is used in the context of the mapping pairs array to determine how
  1001. * many bytes will be needed in the array to store a given logical cluster
  1002. * number (lcn) or a specific run length.
  1003. *
  1004. * Return the number of bytes written. This function cannot fail.
  1005. */
  1006. static inline int ntfs_get_nr_significant_bytes(const s64 n)
  1007. {
  1008. s64 l = n;
  1009. int i;
  1010. s8 j;
  1011. i = 0;
  1012. do {
  1013. l >>= 8;
  1014. i++;
  1015. } while (l != 0 && l != -1);
  1016. j = (n >> 8 * (i - 1)) & 0xff;
  1017. /* If the sign bit is wrong, we need an extra byte. */
  1018. if ((n < 0 && j >= 0) || (n > 0 && j < 0))
  1019. i++;
  1020. return i;
  1021. }
  1022. /**
  1023. * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
  1024. * @vol: ntfs volume (needed for the ntfs version)
  1025. * @rl: locked runlist to determine the size of the mapping pairs of
  1026. * @first_vcn: first vcn which to include in the mapping pairs array
  1027. * @last_vcn: last vcn which to include in the mapping pairs array
  1028. *
  1029. * Walk the locked runlist @rl and calculate the size in bytes of the mapping
  1030. * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
  1031. * finishing with vcn @last_vcn.
  1032. *
  1033. * A @last_vcn of -1 means end of runlist and in that case the size of the
  1034. * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
  1035. * and finishing at the end of the runlist is determined.
  1036. *
  1037. * This for example allows us to allocate a buffer of the right size when
  1038. * building the mapping pairs array.
  1039. *
  1040. * If @rl is NULL, just return 1 (for the single terminator byte).
  1041. *
  1042. * Return the calculated size in bytes on success. On error, return -errno.
  1043. * The following error codes are defined:
  1044. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1045. * fully mapped runlists to this function.
  1046. * -EIO - The runlist is corrupt.
  1047. *
  1048. * Locking: @rl must be locked on entry (either for reading or writing), it
  1049. * remains locked throughout, and is left locked upon return.
  1050. */
  1051. int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
  1052. const runlist_element *rl, const VCN first_vcn,
  1053. const VCN last_vcn)
  1054. {
  1055. LCN prev_lcn;
  1056. int rls;
  1057. BOOL the_end = FALSE;
  1058. BUG_ON(first_vcn < 0);
  1059. BUG_ON(last_vcn < -1);
  1060. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1061. if (!rl) {
  1062. BUG_ON(first_vcn);
  1063. BUG_ON(last_vcn > 0);
  1064. return 1;
  1065. }
  1066. /* Skip to runlist element containing @first_vcn. */
  1067. while (rl->length && first_vcn >= rl[1].vcn)
  1068. rl++;
  1069. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1070. first_vcn < rl->vcn))
  1071. return -EINVAL;
  1072. prev_lcn = 0;
  1073. /* Always need the termining zero byte. */
  1074. rls = 1;
  1075. /* Do the first partial run if present. */
  1076. if (first_vcn > rl->vcn) {
  1077. s64 delta, length = rl->length;
  1078. /* We know rl->length != 0 already. */
  1079. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1080. goto err_out;
  1081. /*
  1082. * If @stop_vcn is given and finishes inside this run, cap the
  1083. * run length.
  1084. */
  1085. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1086. s64 s1 = last_vcn + 1;
  1087. if (unlikely(rl[1].vcn > s1))
  1088. length = s1 - rl->vcn;
  1089. the_end = TRUE;
  1090. }
  1091. delta = first_vcn - rl->vcn;
  1092. /* Header byte + length. */
  1093. rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
  1094. /*
  1095. * If the logical cluster number (lcn) denotes a hole and we
  1096. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1097. * zero space. On earlier NTFS versions we just store the lcn.
  1098. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1099. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1100. */
  1101. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1102. prev_lcn = rl->lcn;
  1103. if (likely(rl->lcn >= 0))
  1104. prev_lcn += delta;
  1105. /* Change in lcn. */
  1106. rls += ntfs_get_nr_significant_bytes(prev_lcn);
  1107. }
  1108. /* Go to next runlist element. */
  1109. rl++;
  1110. }
  1111. /* Do the full runs. */
  1112. for (; rl->length && !the_end; rl++) {
  1113. s64 length = rl->length;
  1114. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1115. goto err_out;
  1116. /*
  1117. * If @stop_vcn is given and finishes inside this run, cap the
  1118. * run length.
  1119. */
  1120. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1121. s64 s1 = last_vcn + 1;
  1122. if (unlikely(rl[1].vcn > s1))
  1123. length = s1 - rl->vcn;
  1124. the_end = TRUE;
  1125. }
  1126. /* Header byte + length. */
  1127. rls += 1 + ntfs_get_nr_significant_bytes(length);
  1128. /*
  1129. * If the logical cluster number (lcn) denotes a hole and we
  1130. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1131. * zero space. On earlier NTFS versions we just store the lcn.
  1132. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1133. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1134. */
  1135. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1136. /* Change in lcn. */
  1137. rls += ntfs_get_nr_significant_bytes(rl->lcn -
  1138. prev_lcn);
  1139. prev_lcn = rl->lcn;
  1140. }
  1141. }
  1142. return rls;
  1143. err_out:
  1144. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1145. rls = -EINVAL;
  1146. else
  1147. rls = -EIO;
  1148. return rls;
  1149. }
  1150. /**
  1151. * ntfs_write_significant_bytes - write the significant bytes of a number
  1152. * @dst: destination buffer to write to
  1153. * @dst_max: pointer to last byte of destination buffer for bounds checking
  1154. * @n: number whose significant bytes to write
  1155. *
  1156. * Store in @dst, the minimum bytes of the number @n which are required to
  1157. * identify @n unambiguously as a signed number, taking care not to exceed
  1158. * @dest_max, the maximum position within @dst to which we are allowed to
  1159. * write.
  1160. *
  1161. * This is used when building the mapping pairs array of a runlist to compress
  1162. * a given logical cluster number (lcn) or a specific run length to the minumum
  1163. * size possible.
  1164. *
  1165. * Return the number of bytes written on success. On error, i.e. the
  1166. * destination buffer @dst is too small, return -ENOSPC.
  1167. */
  1168. static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
  1169. const s64 n)
  1170. {
  1171. s64 l = n;
  1172. int i;
  1173. s8 j;
  1174. i = 0;
  1175. do {
  1176. if (unlikely(dst > dst_max))
  1177. goto err_out;
  1178. *dst++ = l & 0xffll;
  1179. l >>= 8;
  1180. i++;
  1181. } while (l != 0 && l != -1);
  1182. j = (n >> 8 * (i - 1)) & 0xff;
  1183. /* If the sign bit is wrong, we need an extra byte. */
  1184. if (n < 0 && j >= 0) {
  1185. if (unlikely(dst > dst_max))
  1186. goto err_out;
  1187. i++;
  1188. *dst = (s8)-1;
  1189. } else if (n > 0 && j < 0) {
  1190. if (unlikely(dst > dst_max))
  1191. goto err_out;
  1192. i++;
  1193. *dst = (s8)0;
  1194. }
  1195. return i;
  1196. err_out:
  1197. return -ENOSPC;
  1198. }
  1199. /**
  1200. * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
  1201. * @vol: ntfs volume (needed for the ntfs version)
  1202. * @dst: destination buffer to which to write the mapping pairs array
  1203. * @dst_len: size of destination buffer @dst in bytes
  1204. * @rl: locked runlist for which to build the mapping pairs array
  1205. * @first_vcn: first vcn which to include in the mapping pairs array
  1206. * @last_vcn: last vcn which to include in the mapping pairs array
  1207. * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
  1208. *
  1209. * Create the mapping pairs array from the locked runlist @rl, starting at vcn
  1210. * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
  1211. * @dst_len is the size of @dst in bytes and it should be at least equal to the
  1212. * value obtained by calling ntfs_get_size_for_mapping_pairs().
  1213. *
  1214. * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
  1215. * array corresponding to the runlist starting at vcn @first_vcn and finishing
  1216. * at the end of the runlist is created.
  1217. *
  1218. * If @rl is NULL, just write a single terminator byte to @dst.
  1219. *
  1220. * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
  1221. * the first vcn outside the destination buffer. Note that on error, @dst has
  1222. * been filled with all the mapping pairs that will fit, thus it can be treated
  1223. * as partial success, in that a new attribute extent needs to be created or
  1224. * the next extent has to be used and the mapping pairs build has to be
  1225. * continued with @first_vcn set to *@stop_vcn.
  1226. *
  1227. * Return 0 on success and -errno on error. The following error codes are
  1228. * defined:
  1229. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1230. * fully mapped runlists to this function.
  1231. * -EIO - The runlist is corrupt.
  1232. * -ENOSPC - The destination buffer is too small.
  1233. *
  1234. * Locking: @rl must be locked on entry (either for reading or writing), it
  1235. * remains locked throughout, and is left locked upon return.
  1236. */
  1237. int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
  1238. const int dst_len, const runlist_element *rl,
  1239. const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
  1240. {
  1241. LCN prev_lcn;
  1242. s8 *dst_max, *dst_next;
  1243. int err = -ENOSPC;
  1244. BOOL the_end = FALSE;
  1245. s8 len_len, lcn_len;
  1246. BUG_ON(first_vcn < 0);
  1247. BUG_ON(last_vcn < -1);
  1248. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1249. BUG_ON(dst_len < 1);
  1250. if (!rl) {
  1251. BUG_ON(first_vcn);
  1252. BUG_ON(last_vcn > 0);
  1253. if (stop_vcn)
  1254. *stop_vcn = 0;
  1255. /* Terminator byte. */
  1256. *dst = 0;
  1257. return 0;
  1258. }
  1259. /* Skip to runlist element containing @first_vcn. */
  1260. while (rl->length && first_vcn >= rl[1].vcn)
  1261. rl++;
  1262. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1263. first_vcn < rl->vcn))
  1264. return -EINVAL;
  1265. /*
  1266. * @dst_max is used for bounds checking in
  1267. * ntfs_write_significant_bytes().
  1268. */
  1269. dst_max = dst + dst_len - 1;
  1270. prev_lcn = 0;
  1271. /* Do the first partial run if present. */
  1272. if (first_vcn > rl->vcn) {
  1273. s64 delta, length = rl->length;
  1274. /* We know rl->length != 0 already. */
  1275. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1276. goto err_out;
  1277. /*
  1278. * If @stop_vcn is given and finishes inside this run, cap the
  1279. * run length.
  1280. */
  1281. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1282. s64 s1 = last_vcn + 1;
  1283. if (unlikely(rl[1].vcn > s1))
  1284. length = s1 - rl->vcn;
  1285. the_end = TRUE;
  1286. }
  1287. delta = first_vcn - rl->vcn;
  1288. /* Write length. */
  1289. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1290. length - delta);
  1291. if (unlikely(len_len < 0))
  1292. goto size_err;
  1293. /*
  1294. * If the logical cluster number (lcn) denotes a hole and we
  1295. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1296. * zero space. On earlier NTFS versions we just write the lcn
  1297. * change. FIXME: Do we need to write the lcn change or just
  1298. * the lcn in that case? Not sure as I have never seen this
  1299. * case on NT4. - We assume that we just need to write the lcn
  1300. * change until someone tells us otherwise... (AIA)
  1301. */
  1302. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1303. prev_lcn = rl->lcn;
  1304. if (likely(rl->lcn >= 0))
  1305. prev_lcn += delta;
  1306. /* Write change in lcn. */
  1307. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1308. len_len, dst_max, prev_lcn);
  1309. if (unlikely(lcn_len < 0))
  1310. goto size_err;
  1311. } else
  1312. lcn_len = 0;
  1313. dst_next = dst + len_len + lcn_len + 1;
  1314. if (unlikely(dst_next > dst_max))
  1315. goto size_err;
  1316. /* Update header byte. */
  1317. *dst = lcn_len << 4 | len_len;
  1318. /* Position at next mapping pairs array element. */
  1319. dst = dst_next;
  1320. /* Go to next runlist element. */
  1321. rl++;
  1322. }
  1323. /* Do the full runs. */
  1324. for (; rl->length && !the_end; rl++) {
  1325. s64 length = rl->length;
  1326. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1327. goto err_out;
  1328. /*
  1329. * If @stop_vcn is given and finishes inside this run, cap the
  1330. * run length.
  1331. */
  1332. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1333. s64 s1 = last_vcn + 1;
  1334. if (unlikely(rl[1].vcn > s1))
  1335. length = s1 - rl->vcn;
  1336. the_end = TRUE;
  1337. }
  1338. /* Write length. */
  1339. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1340. length);
  1341. if (unlikely(len_len < 0))
  1342. goto size_err;
  1343. /*
  1344. * If the logical cluster number (lcn) denotes a hole and we
  1345. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1346. * zero space. On earlier NTFS versions we just write the lcn
  1347. * change. FIXME: Do we need to write the lcn change or just
  1348. * the lcn in that case? Not sure as I have never seen this
  1349. * case on NT4. - We assume that we just need to write the lcn
  1350. * change until someone tells us otherwise... (AIA)
  1351. */
  1352. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1353. /* Write change in lcn. */
  1354. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1355. len_len, dst_max, rl->lcn - prev_lcn);
  1356. if (unlikely(lcn_len < 0))
  1357. goto size_err;
  1358. prev_lcn = rl->lcn;
  1359. } else
  1360. lcn_len = 0;
  1361. dst_next = dst + len_len + lcn_len + 1;
  1362. if (unlikely(dst_next > dst_max))
  1363. goto size_err;
  1364. /* Update header byte. */
  1365. *dst = lcn_len << 4 | len_len;
  1366. /* Position at next mapping pairs array element. */
  1367. dst = dst_next;
  1368. }
  1369. /* Success. */
  1370. err = 0;
  1371. size_err:
  1372. /* Set stop vcn. */
  1373. if (stop_vcn)
  1374. *stop_vcn = rl->vcn;
  1375. /* Add terminator byte. */
  1376. *dst = 0;
  1377. return err;
  1378. err_out:
  1379. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1380. err = -EINVAL;
  1381. else
  1382. err = -EIO;
  1383. return err;
  1384. }
  1385. /**
  1386. * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
  1387. * @vol: ntfs volume (needed for error output)
  1388. * @runlist: runlist to truncate
  1389. * @new_length: the new length of the runlist in VCNs
  1390. *
  1391. * Truncate the runlist described by @runlist as well as the memory buffer
  1392. * holding the runlist elements to a length of @new_length VCNs.
  1393. *
  1394. * If @new_length lies within the runlist, the runlist elements with VCNs of
  1395. * @new_length and above are discarded. As a special case if @new_length is
  1396. * zero, the runlist is discarded and set to NULL.
  1397. *
  1398. * If @new_length lies beyond the runlist, a sparse runlist element is added to
  1399. * the end of the runlist @runlist or if the last runlist element is a sparse
  1400. * one already, this is extended.
  1401. *
  1402. * Note, no checking is done for unmapped runlist elements. It is assumed that
  1403. * the caller has mapped any elements that need to be mapped already.
  1404. *
  1405. * Return 0 on success and -errno on error.
  1406. *
  1407. * Locking: The caller must hold @runlist->lock for writing.
  1408. */
  1409. int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
  1410. const s64 new_length)
  1411. {
  1412. runlist_element *rl;
  1413. int old_size;
  1414. ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
  1415. BUG_ON(!runlist);
  1416. BUG_ON(new_length < 0);
  1417. rl = runlist->rl;
  1418. if (!new_length) {
  1419. ntfs_debug("Freeing runlist.");
  1420. runlist->rl = NULL;
  1421. if (rl)
  1422. ntfs_free(rl);
  1423. return 0;
  1424. }
  1425. if (unlikely(!rl)) {
  1426. /*
  1427. * Create a runlist consisting of a sparse runlist element of
  1428. * length @new_length followed by a terminator runlist element.
  1429. */
  1430. rl = ntfs_malloc_nofs(PAGE_SIZE);
  1431. if (unlikely(!rl)) {
  1432. ntfs_error(vol->sb, "Not enough memory to allocate "
  1433. "runlist element buffer.");
  1434. return -ENOMEM;
  1435. }
  1436. runlist->rl = rl;
  1437. rl[1].length = rl->vcn = 0;
  1438. rl->lcn = LCN_HOLE;
  1439. rl[1].vcn = rl->length = new_length;
  1440. rl[1].lcn = LCN_ENOENT;
  1441. return 0;
  1442. }
  1443. BUG_ON(new_length < rl->vcn);
  1444. /* Find @new_length in the runlist. */
  1445. while (likely(rl->length && new_length >= rl[1].vcn))
  1446. rl++;
  1447. /*
  1448. * If not at the end of the runlist we need to shrink it.
  1449. * If at the end of the runlist we need to expand it.
  1450. */
  1451. if (rl->length) {
  1452. runlist_element *trl;
  1453. BOOL is_end;
  1454. ntfs_debug("Shrinking runlist.");
  1455. /* Determine the runlist size. */
  1456. trl = rl + 1;
  1457. while (likely(trl->length))
  1458. trl++;
  1459. old_size = trl - runlist->rl + 1;
  1460. /* Truncate the run. */
  1461. rl->length = new_length - rl->vcn;
  1462. /*
  1463. * If a run was partially truncated, make the following runlist
  1464. * element a terminator.
  1465. */
  1466. is_end = FALSE;
  1467. if (rl->length) {
  1468. rl++;
  1469. if (!rl->length)
  1470. is_end = TRUE;
  1471. rl->vcn = new_length;
  1472. rl->length = 0;
  1473. }
  1474. rl->lcn = LCN_ENOENT;
  1475. /* Reallocate memory if necessary. */
  1476. if (!is_end) {
  1477. int new_size = rl - runlist->rl + 1;
  1478. rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
  1479. if (IS_ERR(rl))
  1480. ntfs_warning(vol->sb, "Failed to shrink "
  1481. "runlist buffer. This just "
  1482. "wastes a bit of memory "
  1483. "temporarily so we ignore it "
  1484. "and return success.");
  1485. else
  1486. runlist->rl = rl;
  1487. }
  1488. } else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
  1489. ntfs_debug("Expanding runlist.");
  1490. /*
  1491. * If there is a previous runlist element and it is a sparse
  1492. * one, extend it. Otherwise need to add a new, sparse runlist
  1493. * element.
  1494. */
  1495. if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
  1496. (rl - 1)->length = new_length - (rl - 1)->vcn;
  1497. else {
  1498. /* Determine the runlist size. */
  1499. old_size = rl - runlist->rl + 1;
  1500. /* Reallocate memory if necessary. */
  1501. rl = ntfs_rl_realloc(runlist->rl, old_size,
  1502. old_size + 1);
  1503. if (IS_ERR(rl)) {
  1504. ntfs_error(vol->sb, "Failed to expand runlist "
  1505. "buffer, aborting.");
  1506. return PTR_ERR(rl);
  1507. }
  1508. runlist->rl = rl;
  1509. /*
  1510. * Set @rl to the same runlist element in the new
  1511. * runlist as before in the old runlist.
  1512. */
  1513. rl += old_size - 1;
  1514. /* Add a new, sparse runlist element. */
  1515. rl->lcn = LCN_HOLE;
  1516. rl->length = new_length - rl->vcn;
  1517. /* Add a new terminator runlist element. */
  1518. rl++;
  1519. rl->length = 0;
  1520. }
  1521. rl->vcn = new_length;
  1522. rl->lcn = LCN_ENOENT;
  1523. } else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
  1524. /* Runlist already has same size as requested. */
  1525. rl->lcn = LCN_ENOENT;
  1526. }
  1527. ntfs_debug("Done.");
  1528. return 0;
  1529. }
  1530. /**
  1531. * ntfs_rl_punch_nolock - punch a hole into a runlist
  1532. * @vol: ntfs volume (needed for error output)
  1533. * @runlist: runlist to punch a hole into
  1534. * @start: starting VCN of the hole to be created
  1535. * @length: size of the hole to be created in units of clusters
  1536. *
  1537. * Punch a hole into the runlist @runlist starting at VCN @start and of size
  1538. * @length clusters.
  1539. *
  1540. * Return 0 on success and -errno on error, in which case @runlist has not been
  1541. * modified.
  1542. *
  1543. * If @start and/or @start + @length are outside the runlist return error code
  1544. * -ENOENT.
  1545. *
  1546. * If the runlist contains unmapped or error elements between @start and @start
  1547. * + @length return error code -EINVAL.
  1548. *
  1549. * Locking: The caller must hold @runlist->lock for writing.
  1550. */
  1551. int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist,
  1552. const VCN start, const s64 length)
  1553. {
  1554. const VCN end = start + length;
  1555. s64 delta;
  1556. runlist_element *rl, *rl_end, *rl_real_end, *trl;
  1557. int old_size;
  1558. BOOL lcn_fixup = FALSE;
  1559. ntfs_debug("Entering for start 0x%llx, length 0x%llx.",
  1560. (long long)start, (long long)length);
  1561. BUG_ON(!runlist);
  1562. BUG_ON(start < 0);
  1563. BUG_ON(length < 0);
  1564. BUG_ON(end < 0);
  1565. rl = runlist->rl;
  1566. if (unlikely(!rl)) {
  1567. if (likely(!start && !length))
  1568. return 0;
  1569. return -ENOENT;
  1570. }
  1571. /* Find @start in the runlist. */
  1572. while (likely(rl->length && start >= rl[1].vcn))
  1573. rl++;
  1574. rl_end = rl;
  1575. /* Find @end in the runlist. */
  1576. while (likely(rl_end->length && end >= rl_end[1].vcn)) {
  1577. /* Verify there are no unmapped or error elements. */
  1578. if (unlikely(rl_end->lcn < LCN_HOLE))
  1579. return -EINVAL;
  1580. rl_end++;
  1581. }
  1582. /* Check the last element. */
  1583. if (unlikely(rl_end->length && rl_end->lcn < LCN_HOLE))
  1584. return -EINVAL;
  1585. /* This covers @start being out of bounds, too. */
  1586. if (!rl_end->length && end > rl_end->vcn)
  1587. return -ENOENT;
  1588. if (!length)
  1589. return 0;
  1590. if (!rl->length)
  1591. return -ENOENT;
  1592. rl_real_end = rl_end;
  1593. /* Determine the runlist size. */
  1594. while (likely(rl_real_end->length))
  1595. rl_real_end++;
  1596. old_size = rl_real_end - runlist->rl + 1;
  1597. /* If @start is in a hole simply extend the hole. */
  1598. if (rl->lcn == LCN_HOLE) {
  1599. /*
  1600. * If both @start and @end are in the same sparse run, we are
  1601. * done.
  1602. */
  1603. if (end <= rl[1].vcn) {
  1604. ntfs_debug("Done (requested hole is already sparse).");
  1605. return 0;
  1606. }
  1607. extend_hole:
  1608. /* Extend the hole. */
  1609. rl->length = end - rl->vcn;
  1610. /* If @end is in a hole, merge it with the current one. */
  1611. if (rl_end->lcn == LCN_HOLE) {
  1612. rl_end++;
  1613. rl->length = rl_end->vcn - rl->vcn;
  1614. }
  1615. /* We have done the hole. Now deal with the remaining tail. */
  1616. rl++;
  1617. /* Cut out all runlist elements up to @end. */
  1618. if (rl < rl_end)
  1619. memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
  1620. sizeof(*rl));
  1621. /* Adjust the beginning of the tail if necessary. */
  1622. if (end > rl->vcn) {
  1623. s64 delta = end - rl->vcn;
  1624. rl->vcn = end;
  1625. rl->length -= delta;
  1626. /* Only adjust the lcn if it is real. */
  1627. if (rl->lcn >= 0)
  1628. rl->lcn += delta;
  1629. }
  1630. shrink_allocation:
  1631. /* Reallocate memory if the allocation changed. */
  1632. if (rl < rl_end) {
  1633. rl = ntfs_rl_realloc(runlist->rl, old_size,
  1634. old_size - (rl_end - rl));
  1635. if (IS_ERR(rl))
  1636. ntfs_warning(vol->sb, "Failed to shrink "
  1637. "runlist buffer. This just "
  1638. "wastes a bit of memory "
  1639. "temporarily so we ignore it "
  1640. "and return success.");
  1641. else
  1642. runlist->rl = rl;
  1643. }
  1644. ntfs_debug("Done (extend hole).");
  1645. return 0;
  1646. }
  1647. /*
  1648. * If @start is at the beginning of a run things are easier as there is
  1649. * no need to split the first run.
  1650. */
  1651. if (start == rl->vcn) {
  1652. /*
  1653. * @start is at the beginning of a run.
  1654. *
  1655. * If the previous run is sparse, extend its hole.
  1656. *
  1657. * If @end is not in the same run, switch the run to be sparse
  1658. * and extend the newly created hole.
  1659. *
  1660. * Thus both of these cases reduce the problem to the above
  1661. * case of "@start is in a hole".
  1662. */
  1663. if (rl > runlist->rl && (rl - 1)->lcn == LCN_HOLE) {
  1664. rl--;
  1665. goto extend_hole;
  1666. }
  1667. if (end >= rl[1].vcn) {
  1668. rl->lcn = LCN_HOLE;
  1669. goto extend_hole;
  1670. }
  1671. /*
  1672. * The final case is when @end is in the same run as @start.
  1673. * For this need to split the run into two. One run for the
  1674. * sparse region between the beginning of the old run, i.e.
  1675. * @start, and @end and one for the remaining non-sparse
  1676. * region, i.e. between @end and the end of the old run.
  1677. */
  1678. trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
  1679. if (IS_ERR(trl))
  1680. goto enomem_out;
  1681. old_size++;
  1682. if (runlist->rl != trl) {
  1683. rl = trl + (rl - runlist->rl);
  1684. rl_end = trl + (rl_end - runlist->rl);
  1685. rl_real_end = trl + (rl_real_end - runlist->rl);
  1686. runlist->rl = trl;
  1687. }
  1688. split_end:
  1689. /* Shift all the runs up by one. */
  1690. memmove(rl + 1, rl, (rl_real_end - rl + 1) * sizeof(*rl));
  1691. /* Finally, setup the two split runs. */
  1692. rl->lcn = LCN_HOLE;
  1693. rl->length = length;
  1694. rl++;
  1695. rl->vcn += length;
  1696. /* Only adjust the lcn if it is real. */
  1697. if (rl->lcn >= 0 || lcn_fixup)
  1698. rl->lcn += length;
  1699. rl->length -= length;
  1700. ntfs_debug("Done (split one).");
  1701. return 0;
  1702. }
  1703. /*
  1704. * @start is neither in a hole nor at the beginning of a run.
  1705. *
  1706. * If @end is in a hole, things are easier as simply truncating the run
  1707. * @start is in to end at @start - 1, deleting all runs after that up
  1708. * to @end, and finally extending the beginning of the run @end is in
  1709. * to be @start is all that is needed.
  1710. */
  1711. if (rl_end->lcn == LCN_HOLE) {
  1712. /* Truncate the run containing @start. */
  1713. rl->length = start - rl->vcn;
  1714. rl++;
  1715. /* Cut out all runlist elements up to @end. */
  1716. if (rl < rl_end)
  1717. memmove(rl, rl_end, (rl_real_end - rl_end + 1) *
  1718. sizeof(*rl));
  1719. /* Extend the beginning of the run @end is in to be @start. */
  1720. rl->vcn = start;
  1721. rl->length = rl[1].vcn - start;
  1722. goto shrink_allocation;
  1723. }
  1724. /*
  1725. * If @end is not in a hole there are still two cases to distinguish.
  1726. * Either @end is or is not in the same run as @start.
  1727. *
  1728. * The second case is easier as it can be reduced to an already solved
  1729. * problem by truncating the run @start is in to end at @start - 1.
  1730. * Then, if @end is in the next run need to split the run into a sparse
  1731. * run followed by a non-sparse run (already covered above) and if @end
  1732. * is not in the next run switching it to be sparse, again reduces the
  1733. * problem to the already covered case of "@start is in a hole".
  1734. */
  1735. if (end >= rl[1].vcn) {
  1736. /*
  1737. * If @end is not in the next run, reduce the problem to the
  1738. * case of "@start is in a hole".
  1739. */
  1740. if (rl[1].length && end >= rl[2].vcn) {
  1741. /* Truncate the run containing @start. */
  1742. rl->length = start - rl->vcn;
  1743. rl++;
  1744. rl->vcn = start;
  1745. rl->lcn = LCN_HOLE;
  1746. goto extend_hole;
  1747. }
  1748. trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 1);
  1749. if (IS_ERR(trl))
  1750. goto enomem_out;
  1751. old_size++;
  1752. if (runlist->rl != trl) {
  1753. rl = trl + (rl - runlist->rl);
  1754. rl_end = trl + (rl_end - runlist->rl);
  1755. rl_real_end = trl + (rl_real_end - runlist->rl);
  1756. runlist->rl = trl;
  1757. }
  1758. /* Truncate the run containing @start. */
  1759. rl->length = start - rl->vcn;
  1760. rl++;
  1761. /*
  1762. * @end is in the next run, reduce the problem to the case
  1763. * where "@start is at the beginning of a run and @end is in
  1764. * the same run as @start".
  1765. */
  1766. delta = rl->vcn - start;
  1767. rl->vcn = start;
  1768. if (rl->lcn >= 0) {
  1769. rl->lcn -= delta;
  1770. /* Need this in case the lcn just became negative. */
  1771. lcn_fixup = TRUE;
  1772. }
  1773. rl->length += delta;
  1774. goto split_end;
  1775. }
  1776. /*
  1777. * The first case from above, i.e. @end is in the same run as @start.
  1778. * We need to split the run into three. One run for the non-sparse
  1779. * region between the beginning of the old run and @start, one for the
  1780. * sparse region between @start and @end, and one for the remaining
  1781. * non-sparse region, i.e. between @end and the end of the old run.
  1782. */
  1783. trl = ntfs_rl_realloc(runlist->rl, old_size, old_size + 2);
  1784. if (IS_ERR(trl))
  1785. goto enomem_out;
  1786. old_size += 2;
  1787. if (runlist->rl != trl) {
  1788. rl = trl + (rl - runlist->rl);
  1789. rl_end = trl + (rl_end - runlist->rl);
  1790. rl_real_end = trl + (rl_real_end - runlist->rl);
  1791. runlist->rl = trl;
  1792. }
  1793. /* Shift all the runs up by two. */
  1794. memmove(rl + 2, rl, (rl_real_end - rl + 1) * sizeof(*rl));
  1795. /* Finally, setup the three split runs. */
  1796. rl->length = start - rl->vcn;
  1797. rl++;
  1798. rl->vcn = start;
  1799. rl->lcn = LCN_HOLE;
  1800. rl->length = length;
  1801. rl++;
  1802. delta = end - rl->vcn;
  1803. rl->vcn = end;
  1804. rl->lcn += delta;
  1805. rl->length -= delta;
  1806. ntfs_debug("Done (split both).");
  1807. return 0;
  1808. enomem_out:
  1809. ntfs_error(vol->sb, "Not enough memory to extend runlist buffer.");
  1810. return -ENOMEM;
  1811. }
  1812. #endif /* NTFS_RW */