runlist.c 49 KB

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