runlist.c 45 KB

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