runlist.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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 codes with special meaning:
  865. *
  866. * Return code Meaning / Description
  867. * ==================================================
  868. * LCN_HOLE Hole / not allocated on disk.
  869. * LCN_RL_NOT_MAPPED This is part of the runlist which has not been
  870. * inserted into the runlist yet.
  871. * 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, nor does it modify the
  875. * runlist.
  876. */
  877. LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn)
  878. {
  879. int i;
  880. BUG_ON(vcn < 0);
  881. /*
  882. * If rl is NULL, assume that we have found an unmapped runlist. The
  883. * caller can then attempt to map it and fail appropriately if
  884. * necessary.
  885. */
  886. if (unlikely(!rl))
  887. return LCN_RL_NOT_MAPPED;
  888. /* Catch out of lower bounds vcn. */
  889. if (unlikely(vcn < rl[0].vcn))
  890. return LCN_ENOENT;
  891. for (i = 0; likely(rl[i].length); i++) {
  892. if (unlikely(vcn < rl[i+1].vcn)) {
  893. if (likely(rl[i].lcn >= (LCN)0))
  894. return rl[i].lcn + (vcn - rl[i].vcn);
  895. return rl[i].lcn;
  896. }
  897. }
  898. /*
  899. * The terminator element is setup to the correct value, i.e. one of
  900. * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT.
  901. */
  902. if (likely(rl[i].lcn < (LCN)0))
  903. return rl[i].lcn;
  904. /* Just in case... We could replace this with BUG() some day. */
  905. return LCN_ENOENT;
  906. }
  907. #ifdef NTFS_RW
  908. /**
  909. * ntfs_rl_find_vcn_nolock - find a vcn in a runlist
  910. * @rl: runlist to search
  911. * @vcn: vcn to find
  912. *
  913. * Find the virtual cluster number @vcn in the runlist @rl and return the
  914. * address of the runlist element containing the @vcn on success.
  915. *
  916. * Return NULL if @rl is NULL or @vcn is in an unmapped part/out of bounds of
  917. * the runlist.
  918. *
  919. * Locking: The runlist must be locked on entry.
  920. */
  921. runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, const VCN vcn)
  922. {
  923. BUG_ON(vcn < 0);
  924. if (unlikely(!rl || vcn < rl[0].vcn))
  925. return NULL;
  926. while (likely(rl->length)) {
  927. if (unlikely(vcn < rl[1].vcn)) {
  928. if (likely(rl->lcn >= LCN_HOLE))
  929. return rl;
  930. return NULL;
  931. }
  932. rl++;
  933. }
  934. if (likely(rl->lcn == LCN_ENOENT))
  935. return rl;
  936. return NULL;
  937. }
  938. /**
  939. * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number
  940. * @n: number for which to get the number of bytes for
  941. *
  942. * Return the number of bytes required to store @n unambiguously as
  943. * a signed number.
  944. *
  945. * This is used in the context of the mapping pairs array to determine how
  946. * many bytes will be needed in the array to store a given logical cluster
  947. * number (lcn) or a specific run length.
  948. *
  949. * Return the number of bytes written. This function cannot fail.
  950. */
  951. static inline int ntfs_get_nr_significant_bytes(const s64 n)
  952. {
  953. s64 l = n;
  954. int i;
  955. s8 j;
  956. i = 0;
  957. do {
  958. l >>= 8;
  959. i++;
  960. } while (l != 0 && l != -1);
  961. j = (n >> 8 * (i - 1)) & 0xff;
  962. /* If the sign bit is wrong, we need an extra byte. */
  963. if ((n < 0 && j >= 0) || (n > 0 && j < 0))
  964. i++;
  965. return i;
  966. }
  967. /**
  968. * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array
  969. * @vol: ntfs volume (needed for the ntfs version)
  970. * @rl: locked runlist to determine the size of the mapping pairs of
  971. * @first_vcn: first vcn which to include in the mapping pairs array
  972. * @last_vcn: last vcn which to include in the mapping pairs array
  973. *
  974. * Walk the locked runlist @rl and calculate the size in bytes of the mapping
  975. * pairs array corresponding to the runlist @rl, starting at vcn @first_vcn and
  976. * finishing with vcn @last_vcn.
  977. *
  978. * A @last_vcn of -1 means end of runlist and in that case the size of the
  979. * mapping pairs array corresponding to the runlist starting at vcn @first_vcn
  980. * and finishing at the end of the runlist is determined.
  981. *
  982. * This for example allows us to allocate a buffer of the right size when
  983. * building the mapping pairs array.
  984. *
  985. * If @rl is NULL, just return 1 (for the single terminator byte).
  986. *
  987. * Return the calculated size in bytes on success. On error, return -errno.
  988. * The following error codes are defined:
  989. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  990. * fully mapped runlists to this function.
  991. * -EIO - The runlist is corrupt.
  992. *
  993. * Locking: @rl must be locked on entry (either for reading or writing), it
  994. * remains locked throughout, and is left locked upon return.
  995. */
  996. int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol,
  997. const runlist_element *rl, const VCN first_vcn,
  998. const VCN last_vcn)
  999. {
  1000. LCN prev_lcn;
  1001. int rls;
  1002. BOOL the_end = FALSE;
  1003. BUG_ON(first_vcn < 0);
  1004. BUG_ON(last_vcn < -1);
  1005. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1006. if (!rl) {
  1007. BUG_ON(first_vcn);
  1008. BUG_ON(last_vcn > 0);
  1009. return 1;
  1010. }
  1011. /* Skip to runlist element containing @first_vcn. */
  1012. while (rl->length && first_vcn >= rl[1].vcn)
  1013. rl++;
  1014. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1015. first_vcn < rl->vcn))
  1016. return -EINVAL;
  1017. prev_lcn = 0;
  1018. /* Always need the termining zero byte. */
  1019. rls = 1;
  1020. /* Do the first partial run if present. */
  1021. if (first_vcn > rl->vcn) {
  1022. s64 delta, length = rl->length;
  1023. /* We know rl->length != 0 already. */
  1024. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1025. goto err_out;
  1026. /*
  1027. * If @stop_vcn is given and finishes inside this run, cap the
  1028. * run length.
  1029. */
  1030. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1031. s64 s1 = last_vcn + 1;
  1032. if (unlikely(rl[1].vcn > s1))
  1033. length = s1 - rl->vcn;
  1034. the_end = TRUE;
  1035. }
  1036. delta = first_vcn - rl->vcn;
  1037. /* Header byte + length. */
  1038. rls += 1 + ntfs_get_nr_significant_bytes(length - delta);
  1039. /*
  1040. * If the logical cluster number (lcn) denotes a hole and we
  1041. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1042. * zero space. On earlier NTFS versions we just store the lcn.
  1043. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1044. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1045. */
  1046. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1047. prev_lcn = rl->lcn;
  1048. if (likely(rl->lcn >= 0))
  1049. prev_lcn += delta;
  1050. /* Change in lcn. */
  1051. rls += ntfs_get_nr_significant_bytes(prev_lcn);
  1052. }
  1053. /* Go to next runlist element. */
  1054. rl++;
  1055. }
  1056. /* Do the full runs. */
  1057. for (; rl->length && !the_end; rl++) {
  1058. s64 length = rl->length;
  1059. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1060. goto err_out;
  1061. /*
  1062. * If @stop_vcn is given and finishes inside this run, cap the
  1063. * run length.
  1064. */
  1065. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1066. s64 s1 = last_vcn + 1;
  1067. if (unlikely(rl[1].vcn > s1))
  1068. length = s1 - rl->vcn;
  1069. the_end = TRUE;
  1070. }
  1071. /* Header byte + length. */
  1072. rls += 1 + ntfs_get_nr_significant_bytes(length);
  1073. /*
  1074. * If the logical cluster number (lcn) denotes a hole and we
  1075. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1076. * zero space. On earlier NTFS versions we just store the lcn.
  1077. * Note: this assumes that on NTFS 1.2-, holes are stored with
  1078. * an lcn of -1 and not a delta_lcn of -1 (unless both are -1).
  1079. */
  1080. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1081. /* Change in lcn. */
  1082. rls += ntfs_get_nr_significant_bytes(rl->lcn -
  1083. prev_lcn);
  1084. prev_lcn = rl->lcn;
  1085. }
  1086. }
  1087. return rls;
  1088. err_out:
  1089. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1090. rls = -EINVAL;
  1091. else
  1092. rls = -EIO;
  1093. return rls;
  1094. }
  1095. /**
  1096. * ntfs_write_significant_bytes - write the significant bytes of a number
  1097. * @dst: destination buffer to write to
  1098. * @dst_max: pointer to last byte of destination buffer for bounds checking
  1099. * @n: number whose significant bytes to write
  1100. *
  1101. * Store in @dst, the minimum bytes of the number @n which are required to
  1102. * identify @n unambiguously as a signed number, taking care not to exceed
  1103. * @dest_max, the maximum position within @dst to which we are allowed to
  1104. * write.
  1105. *
  1106. * This is used when building the mapping pairs array of a runlist to compress
  1107. * a given logical cluster number (lcn) or a specific run length to the minumum
  1108. * size possible.
  1109. *
  1110. * Return the number of bytes written on success. On error, i.e. the
  1111. * destination buffer @dst is too small, return -ENOSPC.
  1112. */
  1113. static inline int ntfs_write_significant_bytes(s8 *dst, const s8 *dst_max,
  1114. const s64 n)
  1115. {
  1116. s64 l = n;
  1117. int i;
  1118. s8 j;
  1119. i = 0;
  1120. do {
  1121. if (unlikely(dst > dst_max))
  1122. goto err_out;
  1123. *dst++ = l & 0xffll;
  1124. l >>= 8;
  1125. i++;
  1126. } while (l != 0 && l != -1);
  1127. j = (n >> 8 * (i - 1)) & 0xff;
  1128. /* If the sign bit is wrong, we need an extra byte. */
  1129. if (n < 0 && j >= 0) {
  1130. if (unlikely(dst > dst_max))
  1131. goto err_out;
  1132. i++;
  1133. *dst = (s8)-1;
  1134. } else if (n > 0 && j < 0) {
  1135. if (unlikely(dst > dst_max))
  1136. goto err_out;
  1137. i++;
  1138. *dst = (s8)0;
  1139. }
  1140. return i;
  1141. err_out:
  1142. return -ENOSPC;
  1143. }
  1144. /**
  1145. * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist
  1146. * @vol: ntfs volume (needed for the ntfs version)
  1147. * @dst: destination buffer to which to write the mapping pairs array
  1148. * @dst_len: size of destination buffer @dst in bytes
  1149. * @rl: locked runlist for which to build the mapping pairs array
  1150. * @first_vcn: first vcn which to include in the mapping pairs array
  1151. * @last_vcn: last vcn which to include in the mapping pairs array
  1152. * @stop_vcn: first vcn outside destination buffer on success or -ENOSPC
  1153. *
  1154. * Create the mapping pairs array from the locked runlist @rl, starting at vcn
  1155. * @first_vcn and finishing with vcn @last_vcn and save the array in @dst.
  1156. * @dst_len is the size of @dst in bytes and it should be at least equal to the
  1157. * value obtained by calling ntfs_get_size_for_mapping_pairs().
  1158. *
  1159. * A @last_vcn of -1 means end of runlist and in that case the mapping pairs
  1160. * array corresponding to the runlist starting at vcn @first_vcn and finishing
  1161. * at the end of the runlist is created.
  1162. *
  1163. * If @rl is NULL, just write a single terminator byte to @dst.
  1164. *
  1165. * On success or -ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to
  1166. * the first vcn outside the destination buffer. Note that on error, @dst has
  1167. * been filled with all the mapping pairs that will fit, thus it can be treated
  1168. * as partial success, in that a new attribute extent needs to be created or
  1169. * the next extent has to be used and the mapping pairs build has to be
  1170. * continued with @first_vcn set to *@stop_vcn.
  1171. *
  1172. * Return 0 on success and -errno on error. The following error codes are
  1173. * defined:
  1174. * -EINVAL - Run list contains unmapped elements. Make sure to only pass
  1175. * fully mapped runlists to this function.
  1176. * -EIO - The runlist is corrupt.
  1177. * -ENOSPC - The destination buffer is too small.
  1178. *
  1179. * Locking: @rl must be locked on entry (either for reading or writing), it
  1180. * remains locked throughout, and is left locked upon return.
  1181. */
  1182. int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst,
  1183. const int dst_len, const runlist_element *rl,
  1184. const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn)
  1185. {
  1186. LCN prev_lcn;
  1187. s8 *dst_max, *dst_next;
  1188. int err = -ENOSPC;
  1189. BOOL the_end = FALSE;
  1190. s8 len_len, lcn_len;
  1191. BUG_ON(first_vcn < 0);
  1192. BUG_ON(last_vcn < -1);
  1193. BUG_ON(last_vcn >= 0 && first_vcn > last_vcn);
  1194. BUG_ON(dst_len < 1);
  1195. if (!rl) {
  1196. BUG_ON(first_vcn);
  1197. BUG_ON(last_vcn > 0);
  1198. if (stop_vcn)
  1199. *stop_vcn = 0;
  1200. /* Terminator byte. */
  1201. *dst = 0;
  1202. return 0;
  1203. }
  1204. /* Skip to runlist element containing @first_vcn. */
  1205. while (rl->length && first_vcn >= rl[1].vcn)
  1206. rl++;
  1207. if (unlikely((!rl->length && first_vcn > rl->vcn) ||
  1208. first_vcn < rl->vcn))
  1209. return -EINVAL;
  1210. /*
  1211. * @dst_max is used for bounds checking in
  1212. * ntfs_write_significant_bytes().
  1213. */
  1214. dst_max = dst + dst_len - 1;
  1215. prev_lcn = 0;
  1216. /* Do the first partial run if present. */
  1217. if (first_vcn > rl->vcn) {
  1218. s64 delta, length = rl->length;
  1219. /* We know rl->length != 0 already. */
  1220. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1221. goto err_out;
  1222. /*
  1223. * If @stop_vcn is given and finishes inside this run, cap the
  1224. * run length.
  1225. */
  1226. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1227. s64 s1 = last_vcn + 1;
  1228. if (unlikely(rl[1].vcn > s1))
  1229. length = s1 - rl->vcn;
  1230. the_end = TRUE;
  1231. }
  1232. delta = first_vcn - rl->vcn;
  1233. /* Write length. */
  1234. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1235. length - delta);
  1236. if (unlikely(len_len < 0))
  1237. goto size_err;
  1238. /*
  1239. * If the logical cluster number (lcn) denotes a hole and we
  1240. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1241. * zero space. On earlier NTFS versions we just write the lcn
  1242. * change. FIXME: Do we need to write the lcn change or just
  1243. * the lcn in that case? Not sure as I have never seen this
  1244. * case on NT4. - We assume that we just need to write the lcn
  1245. * change until someone tells us otherwise... (AIA)
  1246. */
  1247. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1248. prev_lcn = rl->lcn;
  1249. if (likely(rl->lcn >= 0))
  1250. prev_lcn += delta;
  1251. /* Write change in lcn. */
  1252. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1253. len_len, dst_max, prev_lcn);
  1254. if (unlikely(lcn_len < 0))
  1255. goto size_err;
  1256. } else
  1257. lcn_len = 0;
  1258. dst_next = dst + len_len + lcn_len + 1;
  1259. if (unlikely(dst_next > dst_max))
  1260. goto size_err;
  1261. /* Update header byte. */
  1262. *dst = lcn_len << 4 | len_len;
  1263. /* Position at next mapping pairs array element. */
  1264. dst = dst_next;
  1265. /* Go to next runlist element. */
  1266. rl++;
  1267. }
  1268. /* Do the full runs. */
  1269. for (; rl->length && !the_end; rl++) {
  1270. s64 length = rl->length;
  1271. if (unlikely(length < 0 || rl->lcn < LCN_HOLE))
  1272. goto err_out;
  1273. /*
  1274. * If @stop_vcn is given and finishes inside this run, cap the
  1275. * run length.
  1276. */
  1277. if (unlikely(last_vcn >= 0 && rl[1].vcn > last_vcn)) {
  1278. s64 s1 = last_vcn + 1;
  1279. if (unlikely(rl[1].vcn > s1))
  1280. length = s1 - rl->vcn;
  1281. the_end = TRUE;
  1282. }
  1283. /* Write length. */
  1284. len_len = ntfs_write_significant_bytes(dst + 1, dst_max,
  1285. length);
  1286. if (unlikely(len_len < 0))
  1287. goto size_err;
  1288. /*
  1289. * If the logical cluster number (lcn) denotes a hole and we
  1290. * are on NTFS 3.0+, we don't store it at all, i.e. we need
  1291. * zero space. On earlier NTFS versions we just write the lcn
  1292. * change. FIXME: Do we need to write the lcn change or just
  1293. * the lcn in that case? Not sure as I have never seen this
  1294. * case on NT4. - We assume that we just need to write the lcn
  1295. * change until someone tells us otherwise... (AIA)
  1296. */
  1297. if (likely(rl->lcn >= 0 || vol->major_ver < 3)) {
  1298. /* Write change in lcn. */
  1299. lcn_len = ntfs_write_significant_bytes(dst + 1 +
  1300. len_len, dst_max, rl->lcn - prev_lcn);
  1301. if (unlikely(lcn_len < 0))
  1302. goto size_err;
  1303. prev_lcn = rl->lcn;
  1304. } else
  1305. lcn_len = 0;
  1306. dst_next = dst + len_len + lcn_len + 1;
  1307. if (unlikely(dst_next > dst_max))
  1308. goto size_err;
  1309. /* Update header byte. */
  1310. *dst = lcn_len << 4 | len_len;
  1311. /* Position at next mapping pairs array element. */
  1312. dst = dst_next;
  1313. }
  1314. /* Success. */
  1315. err = 0;
  1316. size_err:
  1317. /* Set stop vcn. */
  1318. if (stop_vcn)
  1319. *stop_vcn = rl->vcn;
  1320. /* Add terminator byte. */
  1321. *dst = 0;
  1322. return err;
  1323. err_out:
  1324. if (rl->lcn == LCN_RL_NOT_MAPPED)
  1325. err = -EINVAL;
  1326. else
  1327. err = -EIO;
  1328. return err;
  1329. }
  1330. /**
  1331. * ntfs_rl_truncate_nolock - truncate a runlist starting at a specified vcn
  1332. * @runlist: runlist to truncate
  1333. * @new_length: the new length of the runlist in VCNs
  1334. *
  1335. * Truncate the runlist described by @runlist as well as the memory buffer
  1336. * holding the runlist elements to a length of @new_length VCNs.
  1337. *
  1338. * If @new_length lies within the runlist, the runlist elements with VCNs of
  1339. * @new_length and above are discarded.
  1340. *
  1341. * If @new_length lies beyond the runlist, a sparse runlist element is added to
  1342. * the end of the runlist @runlist or if the last runlist element is a sparse
  1343. * one already, this is extended.
  1344. *
  1345. * Return 0 on success and -errno on error.
  1346. *
  1347. * Locking: The caller must hold @runlist->lock for writing.
  1348. */
  1349. int ntfs_rl_truncate_nolock(const ntfs_volume *vol, runlist *const runlist,
  1350. const s64 new_length)
  1351. {
  1352. runlist_element *rl;
  1353. int old_size;
  1354. ntfs_debug("Entering for new_length 0x%llx.", (long long)new_length);
  1355. BUG_ON(!runlist);
  1356. BUG_ON(new_length < 0);
  1357. rl = runlist->rl;
  1358. if (unlikely(!rl)) {
  1359. /*
  1360. * Create a runlist consisting of a sparse runlist element of
  1361. * length @new_length followed by a terminator runlist element.
  1362. */
  1363. rl = ntfs_malloc_nofs(PAGE_SIZE);
  1364. if (unlikely(!rl)) {
  1365. ntfs_error(vol->sb, "Not enough memory to allocate "
  1366. "runlist element buffer.");
  1367. return -ENOMEM;
  1368. }
  1369. runlist->rl = rl;
  1370. rl[1].length = rl->vcn = 0;
  1371. rl->lcn = LCN_HOLE;
  1372. rl[1].vcn = rl->length = new_length;
  1373. rl[1].lcn = LCN_ENOENT;
  1374. return 0;
  1375. }
  1376. BUG_ON(new_length < rl->vcn);
  1377. /* Find @new_length in the runlist. */
  1378. while (likely(rl->length && new_length >= rl[1].vcn))
  1379. rl++;
  1380. /*
  1381. * If not at the end of the runlist we need to shrink it.
  1382. * If at the end of the runlist we need to expand it.
  1383. */
  1384. if (rl->length) {
  1385. runlist_element *trl;
  1386. BOOL is_end;
  1387. ntfs_debug("Shrinking runlist.");
  1388. /* Determine the runlist size. */
  1389. trl = rl + 1;
  1390. while (likely(trl->length))
  1391. trl++;
  1392. old_size = trl - runlist->rl + 1;
  1393. /* Truncate the run. */
  1394. rl->length = new_length - rl->vcn;
  1395. /*
  1396. * If a run was partially truncated, make the following runlist
  1397. * element a terminator.
  1398. */
  1399. is_end = FALSE;
  1400. if (rl->length) {
  1401. rl++;
  1402. if (!rl->length)
  1403. is_end = TRUE;
  1404. rl->vcn = new_length;
  1405. rl->length = 0;
  1406. }
  1407. rl->lcn = LCN_ENOENT;
  1408. /* Reallocate memory if necessary. */
  1409. if (!is_end) {
  1410. int new_size = rl - runlist->rl + 1;
  1411. rl = ntfs_rl_realloc(runlist->rl, old_size, new_size);
  1412. if (IS_ERR(rl))
  1413. ntfs_warning(vol->sb, "Failed to shrink "
  1414. "runlist buffer. This just "
  1415. "wastes a bit of memory "
  1416. "temporarily so we ignore it "
  1417. "and return success.");
  1418. else
  1419. runlist->rl = rl;
  1420. }
  1421. } else if (likely(/* !rl->length && */ new_length > rl->vcn)) {
  1422. ntfs_debug("Expanding runlist.");
  1423. /*
  1424. * If there is a previous runlist element and it is a sparse
  1425. * one, extend it. Otherwise need to add a new, sparse runlist
  1426. * element.
  1427. */
  1428. if ((rl > runlist->rl) && ((rl - 1)->lcn == LCN_HOLE))
  1429. (rl - 1)->length = new_length - (rl - 1)->vcn;
  1430. else {
  1431. /* Determine the runlist size. */
  1432. old_size = rl - runlist->rl + 1;
  1433. /* Reallocate memory if necessary. */
  1434. rl = ntfs_rl_realloc(runlist->rl, old_size,
  1435. old_size + 1);
  1436. if (IS_ERR(rl)) {
  1437. ntfs_error(vol->sb, "Failed to expand runlist "
  1438. "buffer, aborting.");
  1439. return PTR_ERR(rl);
  1440. }
  1441. runlist->rl = rl;
  1442. /*
  1443. * Set @rl to the same runlist element in the new
  1444. * runlist as before in the old runlist.
  1445. */
  1446. rl += old_size - 1;
  1447. /* Add a new, sparse runlist element. */
  1448. rl->lcn = LCN_HOLE;
  1449. rl->length = new_length - rl->vcn;
  1450. /* Add a new terminator runlist element. */
  1451. rl++;
  1452. rl->length = 0;
  1453. }
  1454. rl->vcn = new_length;
  1455. rl->lcn = LCN_ENOENT;
  1456. } else /* if (unlikely(!rl->length && new_length == rl->vcn)) */ {
  1457. /* Runlist already has same size as requested. */
  1458. rl->lcn = LCN_ENOENT;
  1459. }
  1460. ntfs_debug("Done.");
  1461. return 0;
  1462. }
  1463. #endif /* NTFS_RW */