runlist.c 58 KB

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