runlist.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. drl = ntfs_rl_realloc(drl, dend, dend + 1);
  491. if (IS_ERR(drl))
  492. return drl;
  493. /* Insert start element at the front of the runlist. */
  494. ntfs_rl_mm(drl, 1, 0, dend);
  495. drl[0].vcn = 0;
  496. drl[0].lcn = LCN_RL_NOT_MAPPED;
  497. drl[0].length = drl[1].vcn;
  498. }
  499. goto finished;
  500. }
  501. si = di = 0;
  502. /* Skip any unmapped start element(s) in the source runlist. */
  503. while (srl[si].length && srl[si].lcn < LCN_HOLE)
  504. si++;
  505. /* Can't have an entirely unmapped source runlist. */
  506. BUG_ON(!srl[si].length);
  507. /* Record the starting points. */
  508. sstart = si;
  509. /*
  510. * Skip forward in @drl until we reach the position where @srl needs to
  511. * be inserted. If we reach the end of @drl, @srl just needs to be
  512. * appended to @drl.
  513. */
  514. for (; drl[di].length; di++) {
  515. if (drl[di].vcn + drl[di].length > srl[sstart].vcn)
  516. break;
  517. }
  518. dins = di;
  519. /* Sanity check for illegal overlaps. */
  520. if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) &&
  521. (srl[si].lcn >= 0)) {
  522. ntfs_error(NULL, "Run lists overlap. Cannot merge!");
  523. return ERR_PTR(-ERANGE);
  524. }
  525. /* Scan to the end of both runlists in order to know their sizes. */
  526. for (send = si; srl[send].length; send++)
  527. ;
  528. for (dend = di; drl[dend].length; dend++)
  529. ;
  530. if (srl[send].lcn == LCN_ENOENT)
  531. marker_vcn = srl[marker = send].vcn;
  532. /* Scan to the last element with lcn >= LCN_HOLE. */
  533. for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--)
  534. ;
  535. for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--)
  536. ;
  537. {
  538. BOOL start;
  539. BOOL finish;
  540. int ds = dend + 1; /* Number of elements in drl & srl */
  541. int ss = sfinal - sstart + 1;
  542. start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */
  543. (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */
  544. finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */
  545. ((drl[dins].vcn + drl[dins].length) <= /* End of hole */
  546. (srl[send - 1].vcn + srl[send - 1].length)));
  547. /* Or we'll lose an end marker */
  548. if (start && finish && (drl[dins].length == 0))
  549. ss++;
  550. if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn))
  551. finish = FALSE;
  552. #if 0
  553. ntfs_debug("dfinal = %i, dend = %i", dfinal, dend);
  554. ntfs_debug("sstart = %i, sfinal = %i, send = %i", sstart, sfinal, send);
  555. ntfs_debug("start = %i, finish = %i", start, finish);
  556. ntfs_debug("ds = %i, ss = %i, dins = %i", ds, ss, dins);
  557. #endif
  558. if (start) {
  559. if (finish)
  560. drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins);
  561. else
  562. drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins);
  563. } else {
  564. if (finish)
  565. drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins);
  566. else
  567. drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins);
  568. }
  569. if (IS_ERR(drl)) {
  570. ntfs_error(NULL, "Merge failed.");
  571. return drl;
  572. }
  573. ntfs_free(srl);
  574. if (marker) {
  575. ntfs_debug("Triggering marker code.");
  576. for (ds = dend; drl[ds].length; ds++)
  577. ;
  578. /* We only need to care if @srl ended after @drl. */
  579. if (drl[ds].vcn <= marker_vcn) {
  580. int slots = 0;
  581. if (drl[ds].vcn == marker_vcn) {
  582. ntfs_debug("Old marker = 0x%llx, replacing "
  583. "with LCN_ENOENT.",
  584. (unsigned long long)
  585. drl[ds].lcn);
  586. drl[ds].lcn = LCN_ENOENT;
  587. goto finished;
  588. }
  589. /*
  590. * We need to create an unmapped runlist element in
  591. * @drl or extend an existing one before adding the
  592. * ENOENT terminator.
  593. */
  594. if (drl[ds].lcn == LCN_ENOENT) {
  595. ds--;
  596. slots = 1;
  597. }
  598. if (drl[ds].lcn != LCN_RL_NOT_MAPPED) {
  599. /* Add an unmapped runlist element. */
  600. if (!slots) {
  601. drl = ntfs_rl_realloc_nofail(drl, ds,
  602. ds + 2);
  603. slots = 2;
  604. }
  605. ds++;
  606. /* Need to set vcn if it isn't set already. */
  607. if (slots != 1)
  608. drl[ds].vcn = drl[ds - 1].vcn +
  609. drl[ds - 1].length;
  610. drl[ds].lcn = LCN_RL_NOT_MAPPED;
  611. /* We now used up a slot. */
  612. slots--;
  613. }
  614. drl[ds].length = marker_vcn - drl[ds].vcn;
  615. /* Finally add the ENOENT terminator. */
  616. ds++;
  617. if (!slots)
  618. drl = ntfs_rl_realloc_nofail(drl, ds, ds + 1);
  619. drl[ds].vcn = marker_vcn;
  620. drl[ds].lcn = LCN_ENOENT;
  621. drl[ds].length = (s64)0;
  622. }
  623. }
  624. }
  625. finished:
  626. /* The merge was completed successfully. */
  627. ntfs_debug("Merged runlist:");
  628. ntfs_debug_dump_runlist(drl);
  629. return drl;
  630. }
  631. /**
  632. * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist
  633. * @vol: ntfs volume on which the attribute resides
  634. * @attr: attribute record whose mapping pairs array to decompress
  635. * @old_rl: optional runlist in which to insert @attr's runlist
  636. *
  637. * It is up to the caller to serialize access to the runlist @old_rl.
  638. *
  639. * Decompress the attribute @attr's mapping pairs array into a runlist. On
  640. * success, return the decompressed runlist.
  641. *
  642. * If @old_rl is not NULL, decompressed runlist is inserted into the
  643. * appropriate place in @old_rl and the resultant, combined runlist is
  644. * returned. The original @old_rl is deallocated.
  645. *
  646. * On error, return -errno. @old_rl is left unmodified in that case.
  647. *
  648. * The following error codes are defined:
  649. * -ENOMEM - Not enough memory to allocate runlist array.
  650. * -EIO - Corrupt runlist.
  651. * -EINVAL - Invalid parameters were passed in.
  652. * -ERANGE - The two runlists overlap.
  653. *
  654. * FIXME: For now we take the conceptionally simplest approach of creating the
  655. * new runlist disregarding the already existing one and then splicing the
  656. * two into one, if that is possible (we check for overlap and discard the new
  657. * runlist if overlap present before returning ERR_PTR(-ERANGE)).
  658. */
  659. runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol,
  660. const ATTR_RECORD *attr, runlist_element *old_rl)
  661. {
  662. VCN vcn; /* Current vcn. */
  663. LCN lcn; /* Current lcn. */
  664. s64 deltaxcn; /* Change in [vl]cn. */
  665. runlist_element *rl; /* The output runlist. */
  666. u8 *buf; /* Current position in mapping pairs array. */
  667. u8 *attr_end; /* End of attribute. */
  668. int rlsize; /* Size of runlist buffer. */
  669. u16 rlpos; /* Current runlist position in units of
  670. runlist_elements. */
  671. u8 b; /* Current byte offset in buf. */
  672. #ifdef DEBUG
  673. /* Make sure attr exists and is non-resident. */
  674. if (!attr || !attr->non_resident || sle64_to_cpu(
  675. attr->data.non_resident.lowest_vcn) < (VCN)0) {
  676. ntfs_error(vol->sb, "Invalid arguments.");
  677. return ERR_PTR(-EINVAL);
  678. }
  679. #endif
  680. /* Start at vcn = lowest_vcn and lcn 0. */
  681. vcn = sle64_to_cpu(attr->data.non_resident.lowest_vcn);
  682. lcn = 0;
  683. /* Get start of the mapping pairs array. */
  684. buf = (u8*)attr + le16_to_cpu(
  685. attr->data.non_resident.mapping_pairs_offset);
  686. attr_end = (u8*)attr + le32_to_cpu(attr->length);
  687. if (unlikely(buf < (u8*)attr || buf > attr_end)) {
  688. ntfs_error(vol->sb, "Corrupt attribute.");
  689. return ERR_PTR(-EIO);
  690. }
  691. /* Current position in runlist array. */
  692. rlpos = 0;
  693. /* Allocate first page and set current runlist size to one page. */
  694. rl = ntfs_malloc_nofs(rlsize = PAGE_SIZE);
  695. if (unlikely(!rl))
  696. return ERR_PTR(-ENOMEM);
  697. /* Insert unmapped starting element if necessary. */
  698. if (vcn) {
  699. rl->vcn = 0;
  700. rl->lcn = LCN_RL_NOT_MAPPED;
  701. rl->length = vcn;
  702. rlpos++;
  703. }
  704. while (buf < attr_end && *buf) {
  705. /*
  706. * Allocate more memory if needed, including space for the
  707. * not-mapped and terminator elements. ntfs_malloc_nofs()
  708. * operates on whole pages only.
  709. */
  710. if (((rlpos + 3) * sizeof(*old_rl)) > rlsize) {
  711. runlist_element *rl2;
  712. rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
  713. if (unlikely(!rl2)) {
  714. ntfs_free(rl);
  715. return ERR_PTR(-ENOMEM);
  716. }
  717. memcpy(rl2, rl, rlsize);
  718. ntfs_free(rl);
  719. rl = rl2;
  720. rlsize += PAGE_SIZE;
  721. }
  722. /* Enter the current vcn into the current runlist element. */
  723. rl[rlpos].vcn = vcn;
  724. /*
  725. * Get the change in vcn, i.e. the run length in clusters.
  726. * Doing it this way ensures that we signextend negative values.
  727. * A negative run length doesn't make any sense, but hey, I
  728. * didn't make up the NTFS specs and Windows NT4 treats the run
  729. * length as a signed value so that's how it is...
  730. */
  731. b = *buf & 0xf;
  732. if (b) {
  733. if (unlikely(buf + b > attr_end))
  734. goto io_error;
  735. for (deltaxcn = (s8)buf[b--]; b; b--)
  736. deltaxcn = (deltaxcn << 8) + buf[b];
  737. } else { /* The length entry is compulsory. */
  738. ntfs_error(vol->sb, "Missing length entry in mapping "
  739. "pairs array.");
  740. deltaxcn = (s64)-1;
  741. }
  742. /*
  743. * Assume a negative length to indicate data corruption and
  744. * hence clean-up and return NULL.
  745. */
  746. if (unlikely(deltaxcn < 0)) {
  747. ntfs_error(vol->sb, "Invalid length in mapping pairs "
  748. "array.");
  749. goto err_out;
  750. }
  751. /*
  752. * Enter the current run length into the current runlist
  753. * element.
  754. */
  755. rl[rlpos].length = deltaxcn;
  756. /* Increment the current vcn by the current run length. */
  757. vcn += deltaxcn;
  758. /*
  759. * There might be no lcn change at all, as is the case for
  760. * sparse clusters on NTFS 3.0+, in which case we set the lcn
  761. * to LCN_HOLE.
  762. */
  763. if (!(*buf & 0xf0))
  764. rl[rlpos].lcn = LCN_HOLE;
  765. else {
  766. /* Get the lcn change which really can be negative. */
  767. u8 b2 = *buf & 0xf;
  768. b = b2 + ((*buf >> 4) & 0xf);
  769. if (buf + b > attr_end)
  770. goto io_error;
  771. for (deltaxcn = (s8)buf[b--]; b > b2; b--)
  772. deltaxcn = (deltaxcn << 8) + buf[b];
  773. /* Change the current lcn to its new value. */
  774. lcn += deltaxcn;
  775. #ifdef DEBUG
  776. /*
  777. * On NTFS 1.2-, apparently can have lcn == -1 to
  778. * indicate a hole. But we haven't verified ourselves
  779. * whether it is really the lcn or the deltaxcn that is
  780. * -1. So if either is found give us a message so we
  781. * can investigate it further!
  782. */
  783. if (vol->major_ver < 3) {
  784. if (unlikely(deltaxcn == (LCN)-1))
  785. ntfs_error(vol->sb, "lcn delta == -1");
  786. if (unlikely(lcn == (LCN)-1))
  787. ntfs_error(vol->sb, "lcn == -1");
  788. }
  789. #endif
  790. /* Check lcn is not below -1. */
  791. if (unlikely(lcn < (LCN)-1)) {
  792. ntfs_error(vol->sb, "Invalid LCN < -1 in "
  793. "mapping pairs array.");
  794. goto err_out;
  795. }
  796. /* Enter the current lcn into the runlist element. */
  797. rl[rlpos].lcn = lcn;
  798. }
  799. /* Get to the next runlist element. */
  800. rlpos++;
  801. /* Increment the buffer position to the next mapping pair. */
  802. buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1;
  803. }
  804. if (unlikely(buf >= attr_end))
  805. goto io_error;
  806. /*
  807. * If there is a highest_vcn specified, it must be equal to the final
  808. * vcn in the runlist - 1, or something has gone badly wrong.
  809. */
  810. deltaxcn = sle64_to_cpu(attr->data.non_resident.highest_vcn);
  811. if (unlikely(deltaxcn && vcn - 1 != deltaxcn)) {
  812. mpa_err:
  813. ntfs_error(vol->sb, "Corrupt mapping pairs array in "
  814. "non-resident attribute.");
  815. goto err_out;
  816. }
  817. /* Setup not mapped runlist element if this is the base extent. */
  818. if (!attr->data.non_resident.lowest_vcn) {
  819. VCN max_cluster;
  820. max_cluster = ((sle64_to_cpu(
  821. attr->data.non_resident.allocated_size) +
  822. vol->cluster_size - 1) >>
  823. vol->cluster_size_bits) - 1;
  824. /*
  825. * A highest_vcn of zero means this is a single extent
  826. * attribute so simply terminate the runlist with LCN_ENOENT).
  827. */
  828. if (deltaxcn) {
  829. /*
  830. * If there is a difference between the highest_vcn and
  831. * the highest cluster, the runlist is either corrupt
  832. * or, more likely, there are more extents following
  833. * this one.
  834. */
  835. if (deltaxcn < max_cluster) {
  836. ntfs_debug("More extents to follow; deltaxcn "
  837. "= 0x%llx, max_cluster = "
  838. "0x%llx",
  839. (unsigned long long)deltaxcn,
  840. (unsigned long long)
  841. max_cluster);
  842. rl[rlpos].vcn = vcn;
  843. vcn += rl[rlpos].length = max_cluster -
  844. deltaxcn;
  845. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  846. rlpos++;
  847. } else if (unlikely(deltaxcn > max_cluster)) {
  848. ntfs_error(vol->sb, "Corrupt attribute. "
  849. "deltaxcn = 0x%llx, "
  850. "max_cluster = 0x%llx",
  851. (unsigned long long)deltaxcn,
  852. (unsigned long long)
  853. max_cluster);
  854. goto mpa_err;
  855. }
  856. }
  857. rl[rlpos].lcn = LCN_ENOENT;
  858. } else /* Not the base extent. There may be more extents to follow. */
  859. rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
  860. /* Setup terminating runlist element. */
  861. rl[rlpos].vcn = vcn;
  862. rl[rlpos].length = (s64)0;
  863. /* If no existing runlist was specified, we are done. */
  864. if (!old_rl) {
  865. ntfs_debug("Mapping pairs array successfully decompressed:");
  866. ntfs_debug_dump_runlist(rl);
  867. return rl;
  868. }
  869. /* Now combine the new and old runlists checking for overlaps. */
  870. old_rl = ntfs_runlists_merge(old_rl, rl);
  871. if (likely(!IS_ERR(old_rl)))
  872. return old_rl;
  873. ntfs_free(rl);
  874. ntfs_error(vol->sb, "Failed to merge runlists.");
  875. return old_rl;
  876. io_error:
  877. ntfs_error(vol->sb, "Corrupt attribute.");
  878. err_out:
  879. ntfs_free(rl);
  880. return ERR_PTR(-EIO);
  881. }
  882. /**
  883. * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist
  884. * @rl: runlist to use for conversion
  885. * @vcn: vcn to convert
  886. *
  887. * Convert the virtual cluster number @vcn of an attribute into a logical
  888. * cluster number (lcn) of a device using the runlist @rl to map vcns to their
  889. * corresponding lcns.
  890. *
  891. * It is up to the caller to serialize access to the runlist @rl.
  892. *
  893. * Since lcns must be >= 0, we use negative return codes with special meaning:
  894. *
  895. * Return code Meaning / Description
  896. * ==================================================
  897. * LCN_HOLE Hole / not allocated on disk.
  898. * LCN_RL_NOT_MAPPED This is part of the runlist which has not been
  899. * inserted into the runlist yet.
  900. * LCN_ENOENT There is no such vcn in the attribute.
  901. *
  902. * Locking: - The caller must have locked the runlist (for reading or writing).
  903. * - This function does not touch the lock, nor does it modify the
  904. * runlist.
  905. */
  906. LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
  907. {
  908. int i;
  909. BUG_ON(vcn < 0);
  910. /*
  911. * If rl is NULL, assume that we have found an unmapped runlist. The
  912. * caller can then attempt to map it and fail appropriately if
  913. * necessary.
  914. */
  915. if (unlikely(!rl))
  916. return LCN_RL_NOT_MAPPED;
  917. /* Catch out of lower bounds vcn. */
  918. if (unlikely(vcn < rl[0].vcn))
  919. return LCN_ENOENT;
  920. for (i = 0; likely(rl[i].length); i++) {
  921. if (unlikely(vcn < rl[i+1].vcn)) {
  922. if (likely(rl[i].lcn >= (LCN)0))
  923. return rl[i].lcn + (vcn - rl[i].vcn);
  924. return rl[i].lcn;
  925. }
  926. }
  927. /*
  928. * The terminator element is setup to the correct value, i.e. one of
  929. * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
  930. */
  931. if (likely(rl[i].lcn < (LCN)0))
  932. return rl[i].lcn;
  933. /* Just in case... We could replace this with BUG() some day. */
  934. return LCN_ENOENT;
  935. }
  936. #ifdef NTFS_RW
  937. /**
  938. * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
  939. * @rl: runlist to search
  940. * @vcn: vcn to find
  941. *
  942. * Find the virtual cluster number @vcn in the runlist @rl and return the
  943. * address of the runlist element containing the @vcn on success.
  944. *
  945. * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
  946. * the runlist.
  947. *
  948. * Locking: The runlist must be locked on entry.
  949. */
  950. runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
  951. {
  952. BUG_ON(vcn < 0);
  953. if (unlikely(!rl || vcn < rl[0].vcn))
  954. return NULL;
  955. while (likely(rl->length)) {
  956. if (unlikely(vcn < rl[1].vcn)) {
  957. if (likely(rl->lcn >= LCN_HOLE))
  958. return rl;
  959. return NULL;
  960. }
  961. rl++;
  962. }
  963. if (likely(rl->lcn == LCN_ENOENT))
  964. return rl;
  965. return NULL;
  966. }
  967. /**
  968. * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
  969. * @n: number for which to get the number of bytes for
  970. *
  971. * Return the number of bytes required to store @n unambiguously as
  972. * a signed number.
  973. *
  974. * This is used in the context of the mapping pairs array to determine how
  975. * many bytes will be needed in the array to store a given logical cluster
  976. * number (lcn) or a specific run length.
  977. *
  978. * Return the number of bytes written. This function cannot fail.
  979. */
  980. static inline int ntfs_get_nr_significant_bytes(const s64 n)
  981. {
  982. s64 l = n;
  983. int i;
  984. s8 j;
  985. i = 0;
  986. do {
  987. l >>= 8;
  988. i++;
  989. } while (l != 0 && l != -1);
  990. j = (n >> 8 * (i - 1)) & 0xff;
  991. /* If the sign bit is wrong, we need an extra byte. */
  992. if ((n < 0 && j >= 0) || (n > 0 && j < 0))
  993. i++;
  994. return i;
  995. }
  996. /**
  997. * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
  998. * @vol: ntfs volume (needed for the ntfs version)
  999. * @rl: locked runlist to determine the size of the mapping pairs of
  1000. * @first_vcn: first vcn which to include in the mapping pairs array
  1001. * @last_vcn: last vcn which to include in the mapping pairs array
  1002. *
  1003. * Walk the locked runlist @rl and calculate the size in bytes of the mapping
  1004. * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
  1005. * finishing with vcn @last_vcn.
  1006. *
  1007. * A @last_vcn of -1 means end of runlist and in that case the size of the
  1008. * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
  1009. * and finishing at the end of the runlist is determined.
  1010. *
  1011. * This for example allows us to allocate a buffer of the right size when
  1012. * building the mapping pairs array.
  1013. *
  1014. * If @rl is NULL, just return 1 (for the single terminator byte).
  1015. *
  1016. * Return the calculated size in bytes on success. On error, return -errno.
  1017. * The following error codes are defined:
  1018. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1019. * fully mapped runlists to this function.
  1020. * -EIO - The runlist is corrupt.
  1021. *
  1022. * Locking: @rl must be locked on entry (either for reading or writing), it
  1023. * remains locked throughout, and is left locked upon return.
  1024. */
  1025. int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
  1026. const runlist_element *rl, const VCN first_vcn,
  1027. const VCN last_vcn)
  1028. {
  1029. LCN prev_lcn;
  1030. int rls;
  1031. BOOL the_end = FALSE;
  1032. BUG_ON(first_vcn < 0);
  1033. BUG_ON(last_vcn < -1);
  1034. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1035. if (!rl) {
  1036. BUG_ON(first_vcn);
  1037. BUG_ON(last_vcn > 0);
  1038. return 1;
  1039. }
  1040. /* Skip to runlist element containing @first_vcn. */
  1041. while (rl->length && first_vcn >= rl[1].vcn)
  1042. rl++;
  1043. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1044. first_vcn < rl->vcn))
  1045. return -EINVAL;
  1046. prev_lcn = 0;
  1047. /* Always need the termining zero byte. */
  1048. rls = 1;
  1049. /* Do the first partial run if present. */
  1050. if (first_vcn > rl->vcn) {
  1051. s64 delta, length = rl->length;
  1052. /* We know rl->length != 0 already. */
  1053. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1054. goto err_out;
  1055. /*
  1056. * If @stop_vcn is given and finishes inside this run, cap the
  1057. * run length.
  1058. */
  1059. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1060. s64 s1 = last_vcn + 1;
  1061. if (unlikely(rl[1].vcn > s1))
  1062. length = s1 - rl->vcn;
  1063. the_end = TRUE;
  1064. }
  1065. delta = first_vcn - rl->vcn;
  1066. /* Header byte + length. */
  1067. rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
  1068. /*
  1069. * If the logical cluster number (lcn) denotes a hole and we
  1070. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1071. * zero space. On earlier NTFS versions we just store the lcn.
  1072. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1073. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1074. */
  1075. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1076. prev_lcn = rl->lcn;
  1077. if (likely(rl->lcn >= 0))
  1078. prev_lcn += delta;
  1079. /* Change in lcn. */
  1080. rls += ntfs_get_nr_significant_bytes(prev_lcn);
  1081. }
  1082. /* Go to next runlist element. */
  1083. rl++;
  1084. }
  1085. /* Do the full runs. */
  1086. for (; rl->length && !the_end; rl++) {
  1087. s64 length = rl->length;
  1088. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1089. goto err_out;
  1090. /*
  1091. * If @stop_vcn is given and finishes inside this run, cap the
  1092. * run length.
  1093. */
  1094. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1095. s64 s1 = last_vcn + 1;
  1096. if (unlikely(rl[1].vcn > s1))
  1097. length = s1 - rl->vcn;
  1098. the_end = TRUE;
  1099. }
  1100. /* Header byte + length. */
  1101. rls += 1 + ntfs_get_nr_significant_bytes(length);
  1102. /*
  1103. * If the logical cluster number (lcn) denotes a hole and we
  1104. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1105. * zero space. On earlier NTFS versions we just store the lcn.
  1106. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1107. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1108. */
  1109. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1110. /* Change in lcn. */
  1111. rls += ntfs_get_nr_significant_bytes(rl->lcn -
  1112. prev_lcn);
  1113. prev_lcn = rl->lcn;
  1114. }
  1115. }
  1116. return rls;
  1117. err_out:
  1118. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1119. rls = -EINVAL;
  1120. else
  1121. rls = -EIO;
  1122. return rls;
  1123. }
  1124. /**
  1125. * ntfs_write_significant_bytes - write the significant bytes of a number
  1126. * @dst: destination buffer to write to
  1127. * @dst_max: pointer to last byte of destination buffer for bounds checking
  1128. * @n: number whose significant bytes to write
  1129. *
  1130. * Store in @dst, the minimum bytes of the number @n which are required to
  1131. * identify @n unambiguously as a signed number, taking care not to exceed
  1132. * @dest_max, the maximum position within @dst to which we are allowed to
  1133. * write.
  1134. *
  1135. * This is used when building the mapping pairs array of a runlist to compress
  1136. * a given logical cluster number (lcn) or a specific run length to the minumum
  1137. * size possible.
  1138. *
  1139. * Return the number of bytes written on success. On error, i.e. the
  1140. * destination buffer @dst is too small, return -ENOSPC.
  1141. */
  1142. static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
  1143. const s64 n)
  1144. {
  1145. s64 l = n;
  1146. int i;
  1147. s8 j;
  1148. i = 0;
  1149. do {
  1150. if (unlikely(dst > dst_max))
  1151. goto err_out;
  1152. *dst++ = l & 0xffll;
  1153. l >>= 8;
  1154. i++;
  1155. } while (l != 0 && l != -1);
  1156. j = (n >> 8 * (i - 1)) & 0xff;
  1157. /* If the sign bit is wrong, we need an extra byte. */
  1158. if (n < 0 && j >= 0) {
  1159. if (unlikely(dst > dst_max))
  1160. goto err_out;
  1161. i++;
  1162. *dst = (s8)-1;
  1163. } else if (n > 0 && j < 0) {
  1164. if (unlikely(dst > dst_max))
  1165. goto err_out;
  1166. i++;
  1167. *dst = (s8)0;
  1168. }
  1169. return i;
  1170. err_out:
  1171. return -ENOSPC;
  1172. }
  1173. /**
  1174. * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
  1175. * @vol: ntfs volume (needed for the ntfs version)
  1176. * @dst: destination buffer to which to write the mapping pairs array
  1177. * @dst_len: size of destination buffer @dst in bytes
  1178. * @rl: locked runlist for which to build the mapping pairs array
  1179. * @first_vcn: first vcn which to include in the mapping pairs array
  1180. * @last_vcn: last vcn which to include in the mapping pairs array
  1181. * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
  1182. *
  1183. * Create the mapping pairs array from the locked runlist @rl, starting at vcn
  1184. * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
  1185. * @dst_len is the size of @dst in bytes and it should be at least equal to the
  1186. * value obtained by calling ntfs_get_size_for_mapping_pairs().
  1187. *
  1188. * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
  1189. * array corresponding to the runlist starting at vcn @first_vcn and finishing
  1190. * at the end of the runlist is created.
  1191. *
  1192. * If @rl is NULL, just write a single terminator byte to @dst.
  1193. *
  1194. * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
  1195. * the first vcn outside the destination buffer. Note that on error, @dst has
  1196. * been filled with all the mapping pairs that will fit, thus it can be treated
  1197. * as partial success, in that a new attribute extent needs to be created or
  1198. * the next extent has to be used and the mapping pairs build has to be
  1199. * continued with @first_vcn set to *@stop_vcn.
  1200. *
  1201. * Return 0 on success and -errno on error. The following error codes are
  1202. * defined:
  1203. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1204. * fully mapped runlists to this function.
  1205. * -EIO - The runlist is corrupt.
  1206. * -ENOSPC - The destination buffer is too small.
  1207. *
  1208. * Locking: @rl must be locked on entry (either for reading or writing), it
  1209. * remains locked throughout, and is left locked upon return.
  1210. */
  1211. int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
  1212. const int dst_len, const runlist_element *rl,
  1213. const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
  1214. {
  1215. LCN prev_lcn;
  1216. s8 *dst_max, *dst_next;
  1217. int err = -ENOSPC;
  1218. BOOL the_end = FALSE;
  1219. s8 len_len, lcn_len;
  1220. BUG_ON(first_vcn < 0);
  1221. BUG_ON(last_vcn < -1);
  1222. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1223. BUG_ON(dst_len < 1);
  1224. if (!rl) {
  1225. BUG_ON(first_vcn);
  1226. BUG_ON(last_vcn > 0);
  1227. if (stop_vcn)
  1228. *stop_vcn = 0;
  1229. /* Terminator byte. */
  1230. *dst = 0;
  1231. return 0;
  1232. }
  1233. /* Skip to runlist element containing @first_vcn. */
  1234. while (rl->length && first_vcn >= rl[1].vcn)
  1235. rl++;
  1236. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1237. first_vcn < rl->vcn))
  1238. return -EINVAL;
  1239. /*
  1240. * @dst_max is used for bounds checking in
  1241. * ntfs_write_significant_bytes().
  1242. */
  1243. dst_max = dst + dst_len - 1;
  1244. prev_lcn = 0;
  1245. /* Do the first partial run if present. */
  1246. if (first_vcn > rl->vcn) {
  1247. s64 delta, length = rl->length;
  1248. /* We know rl->length != 0 already. */
  1249. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1250. goto err_out;
  1251. /*
  1252. * If @stop_vcn is given and finishes inside this run, cap the
  1253. * run length.
  1254. */
  1255. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1256. s64 s1 = last_vcn + 1;
  1257. if (unlikely(rl[1].vcn > s1))
  1258. length = s1 - rl->vcn;
  1259. the_end = TRUE;
  1260. }
  1261. delta = first_vcn - rl->vcn;
  1262. /* Write length. */
  1263. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1264. length - delta);
  1265. if (unlikely(len_len < 0))
  1266. goto size_err;
  1267. /*
  1268. * If the logical cluster number (lcn) denotes a hole and we
  1269. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1270. * zero space. On earlier NTFS versions we just write the lcn
  1271. * change. FIXME: Do we need to write the lcn change or just
  1272. * the lcn in that case? Not sure as I have never seen this
  1273. * case on NT4. - We assume that we just need to write the lcn
  1274. * change until someone tells us otherwise... (AIA)
  1275. */
  1276. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1277. prev_lcn = rl->lcn;
  1278. if (likely(rl->lcn >= 0))
  1279. prev_lcn += delta;
  1280. /* Write change in lcn. */
  1281. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1282. len_len, dst_max, prev_lcn);
  1283. if (unlikely(lcn_len < 0))
  1284. goto size_err;
  1285. } else
  1286. lcn_len = 0;
  1287. dst_next = dst + len_len + lcn_len + 1;
  1288. if (unlikely(dst_next > dst_max))
  1289. goto size_err;
  1290. /* Update header byte. */
  1291. *dst = lcn_len << 4 | len_len;
  1292. /* Position at next mapping pairs array element. */
  1293. dst = dst_next;
  1294. /* Go to next runlist element. */
  1295. rl++;
  1296. }
  1297. /* Do the full runs. */
  1298. for (; rl->length && !the_end; rl++) {
  1299. s64 length = rl->length;
  1300. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1301. goto err_out;
  1302. /*
  1303. * If @stop_vcn is given and finishes inside this run, cap the
  1304. * run length.
  1305. */
  1306. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1307. s64 s1 = last_vcn + 1;
  1308. if (unlikely(rl[1].vcn > s1))
  1309. length = s1 - rl->vcn;
  1310. the_end = TRUE;
  1311. }
  1312. /* Write length. */
  1313. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1314. length);
  1315. if (unlikely(len_len < 0))
  1316. goto size_err;
  1317. /*
  1318. * If the logical cluster number (lcn) denotes a hole and we
  1319. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1320. * zero space. On earlier NTFS versions we just write the lcn
  1321. * change. FIXME: Do we need to write the lcn change or just
  1322. * the lcn in that case? Not sure as I have never seen this
  1323. * case on NT4. - We assume that we just need to write the lcn
  1324. * change until someone tells us otherwise... (AIA)
  1325. */
  1326. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1327. /* Write change in lcn. */
  1328. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1329. len_len, dst_max, rl->lcn - prev_lcn);
  1330. if (unlikely(lcn_len < 0))
  1331. goto size_err;
  1332. prev_lcn = rl->lcn;
  1333. } else
  1334. lcn_len = 0;
  1335. dst_next = dst + len_len + lcn_len + 1;
  1336. if (unlikely(dst_next > dst_max))
  1337. goto size_err;
  1338. /* Update header byte. */
  1339. *dst = lcn_len << 4 | len_len;
  1340. /* Position at next mapping pairs array element. */
  1341. dst = dst_next;
  1342. }
  1343. /* Success. */
  1344. err = 0;
  1345. size_err:
  1346. /* Set stop vcn. */
  1347. if (stop_vcn)
  1348. *stop_vcn = rl->vcn;
  1349. /* Add terminator byte. */
  1350. *dst = 0;
  1351. return err;
  1352. err_out:
  1353. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1354. err = -EINVAL;
  1355. else
  1356. err = -EIO;
  1357. return err;
  1358. }
  1359. /**
  1360. * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
  1361. * @runlist: runlist to truncate
  1362. * @new_length: the new length of the runlist in VCNs
  1363. *
  1364. * Truncate the runlist described by @runlist as well as the memory buffer
  1365. * holding the runlist elements to a length of @new_length VCNs.
  1366. *
  1367. * If @new_length lies within the runlist, the runlist elements with VCNs of
  1368. * @new_length and above are discarded.
  1369. *
  1370. * If @new_length lies beyond the runlist, a sparse runlist element is added to
  1371. * the end of the runlist @runlist or if the last runlist element is a sparse
  1372. * one already, this is extended.
  1373. *
  1374. * Return 0 on success and -errno on error.
  1375. *
  1376. * Locking: The caller must hold @runlist->lock for writing.
  1377. */
  1378. int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
  1379. const s64 new_length)
  1380. {
  1381. runlist_element *rl;
  1382. int old_size;
  1383. ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
  1384. BUG_ON(!runlist);
  1385. BUG_ON(new_length < 0);
  1386. rl = runlist->rl;
  1387. if (unlikely(!rl)) {
  1388. /*
  1389. * Create a runlist consisting of a sparse runlist element of
  1390. * length @new_length followed by a terminator runlist element.
  1391. */
  1392. rl = ntfs_malloc_nofs(PAGE_SIZE);
  1393. if (unlikely(!rl)) {
  1394. ntfs_error(vol->sb, "Not enough memory to allocate "
  1395. "runlist element buffer.");
  1396. return -ENOMEM;
  1397. }
  1398. runlist->rl = rl;
  1399. rl[1].length = rl->vcn = 0;
  1400. rl->lcn = LCN_HOLE;
  1401. rl[1].vcn = rl->length = new_length;
  1402. rl[1].lcn = LCN_ENOENT;
  1403. return 0;
  1404. }
  1405. BUG_ON(new_length < rl->vcn);
  1406. /* Find @new_length in the runlist. */
  1407. while (likely(rl->length && new_length >= rl[1].vcn))
  1408. rl++;
  1409. /*
  1410. * If not at the end of the runlist we need to shrink it.
  1411. * If at the end of the runlist we need to expand it.
  1412. */
  1413. if (rl->length) {
  1414. runlist_element *trl;
  1415. BOOL is_end;
  1416. ntfs_debug("Shrinking runlist.");
  1417. /* Determine the runlist size. */
  1418. trl = rl + 1;
  1419. while (likely(trl->length))
  1420. trl++;
  1421. old_size = trl - runlist->rl + 1;
  1422. /* Truncate the run. */
  1423. rl->length = new_length - rl->vcn;
  1424. /*
  1425. * If a run was partially truncated, make the following runlist
  1426. * element a terminator.
  1427. */
  1428. is_end = FALSE;
  1429. if (rl->length) {
  1430. rl++;
  1431. if (!rl->length)
  1432. is_end = TRUE;
  1433. rl->vcn = new_length;
  1434. rl->length = 0;
  1435. }
  1436. rl->lcn = LCN_ENOENT;
  1437. /* Reallocate memory if necessary. */
  1438. if (!is_end) {
  1439. int new_size = rl - runlist->rl + 1;
  1440. rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
  1441. if (IS_ERR(rl))
  1442. ntfs_warning(vol->sb, "Failed to shrink "
  1443. "runlist buffer. This just "
  1444. "wastes a bit of memory "
  1445. "temporarily so we ignore it "
  1446. "and return success.");
  1447. else
  1448. runlist->rl = rl;
  1449. }
  1450. } else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
  1451. ntfs_debug("Expanding runlist.");
  1452. /*
  1453. * If there is a previous runlist element and it is a sparse
  1454. * one, extend it. Otherwise need to add a new, sparse runlist
  1455. * element.
  1456. */
  1457. if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
  1458. (rl - 1)->length = new_length - (rl - 1)->vcn;
  1459. else {
  1460. /* Determine the runlist size. */
  1461. old_size = rl - runlist->rl + 1;
  1462. /* Reallocate memory if necessary. */
  1463. rl = ntfs_rl_realloc(runlist->rl, old_size,
  1464. old_size + 1);
  1465. if (IS_ERR(rl)) {
  1466. ntfs_error(vol->sb, "Failed to expand runlist "
  1467. "buffer, aborting.");
  1468. return PTR_ERR(rl);
  1469. }
  1470. runlist->rl = rl;
  1471. /*
  1472. * Set @rl to the same runlist element in the new
  1473. * runlist as before in the old runlist.
  1474. */
  1475. rl += old_size - 1;
  1476. /* Add a new, sparse runlist element. */
  1477. rl->lcn = LCN_HOLE;
  1478. rl->length = new_length - rl->vcn;
  1479. /* Add a new terminator runlist element. */
  1480. rl++;
  1481. rl->length = 0;
  1482. }
  1483. rl->vcn = new_length;
  1484. rl->lcn = LCN_ENOENT;
  1485. } else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
  1486. /* Runlist already has same size as requested. */
  1487. rl->lcn = LCN_ENOENT;
  1488. }
  1489. ntfs_debug("Done.");
  1490. return 0;
  1491. }
  1492. #endif /* NTFS_RW */