attrib.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. /*
  15. * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage
  16. * preallocate algorithm.
  17. */
  18. #ifndef NTFS_MIN_LOG2_OF_CLUMP
  19. #define NTFS_MIN_LOG2_OF_CLUMP 16
  20. #endif
  21. #ifndef NTFS_MAX_LOG2_OF_CLUMP
  22. #define NTFS_MAX_LOG2_OF_CLUMP 26
  23. #endif
  24. // 16M
  25. #define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8))
  26. // 16G
  27. #define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8))
  28. static inline u64 get_pre_allocated(u64 size)
  29. {
  30. u32 clump;
  31. u8 align_shift;
  32. u64 ret;
  33. if (size <= NTFS_CLUMP_MIN) {
  34. clump = 1 << NTFS_MIN_LOG2_OF_CLUMP;
  35. align_shift = NTFS_MIN_LOG2_OF_CLUMP;
  36. } else if (size >= NTFS_CLUMP_MAX) {
  37. clump = 1 << NTFS_MAX_LOG2_OF_CLUMP;
  38. align_shift = NTFS_MAX_LOG2_OF_CLUMP;
  39. } else {
  40. align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 +
  41. __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP));
  42. clump = 1u << align_shift;
  43. }
  44. ret = (((size + clump - 1) >> align_shift)) << align_shift;
  45. return ret;
  46. }
  47. /*
  48. * attr_load_runs - Load all runs stored in @attr.
  49. */
  50. static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
  51. struct runs_tree *run, const CLST *vcn)
  52. {
  53. int err;
  54. CLST svcn = le64_to_cpu(attr->nres.svcn);
  55. CLST evcn = le64_to_cpu(attr->nres.evcn);
  56. u32 asize;
  57. u16 run_off;
  58. if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
  59. return 0;
  60. if (vcn && (evcn < *vcn || *vcn < svcn))
  61. return -EINVAL;
  62. asize = le32_to_cpu(attr->size);
  63. run_off = le16_to_cpu(attr->nres.run_off);
  64. if (run_off > asize)
  65. return -EINVAL;
  66. err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
  67. vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
  68. asize - run_off);
  69. if (err < 0)
  70. return err;
  71. return 0;
  72. }
  73. /*
  74. * run_deallocate_ex - Deallocate clusters.
  75. */
  76. static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
  77. CLST vcn, CLST len, CLST *done, bool trim)
  78. {
  79. int err = 0;
  80. CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
  81. size_t idx;
  82. if (!len)
  83. goto out;
  84. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
  85. failed:
  86. run_truncate(run, vcn0);
  87. err = -EINVAL;
  88. goto out;
  89. }
  90. for (;;) {
  91. if (clen > len)
  92. clen = len;
  93. if (!clen) {
  94. err = -EINVAL;
  95. goto out;
  96. }
  97. if (lcn != SPARSE_LCN) {
  98. if (sbi) {
  99. /* mark bitmap range [lcn + clen) as free and trim clusters. */
  100. mark_as_free_ex(sbi, lcn, clen, trim);
  101. }
  102. dn += clen;
  103. }
  104. len -= clen;
  105. if (!len)
  106. break;
  107. vcn_next = vcn + clen;
  108. if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
  109. vcn != vcn_next) {
  110. /* Save memory - don't load entire run. */
  111. goto failed;
  112. }
  113. }
  114. out:
  115. if (done)
  116. *done += dn;
  117. return err;
  118. }
  119. /*
  120. * attr_allocate_clusters - Find free space, mark it as used and store in @run.
  121. */
  122. int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
  123. CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
  124. enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
  125. CLST *new_lcn, CLST *new_len)
  126. {
  127. int err;
  128. CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
  129. size_t cnt = run->count;
  130. for (;;) {
  131. err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
  132. opt);
  133. if (err == -ENOSPC && pre) {
  134. pre = 0;
  135. if (*pre_alloc)
  136. *pre_alloc = 0;
  137. continue;
  138. }
  139. if (err)
  140. goto out;
  141. if (vcn == vcn0) {
  142. /* Return the first fragment. */
  143. if (new_lcn)
  144. *new_lcn = lcn;
  145. if (new_len)
  146. *new_len = flen;
  147. }
  148. /* Add new fragment into run storage. */
  149. if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) {
  150. /* Undo last 'ntfs_look_for_free_space' */
  151. mark_as_free_ex(sbi, lcn, len, false);
  152. err = -ENOMEM;
  153. goto out;
  154. }
  155. if (opt & ALLOCATE_ZERO) {
  156. u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
  157. err = blkdev_issue_zeroout(sbi->sb->s_bdev,
  158. (sector_t)lcn << shift,
  159. (sector_t)flen << shift,
  160. GFP_NOFS, 0);
  161. if (err)
  162. goto out;
  163. }
  164. vcn += flen;
  165. if (flen >= len || (opt & ALLOCATE_MFT) ||
  166. (fr && run->count - cnt >= fr)) {
  167. *alen = vcn - vcn0;
  168. return 0;
  169. }
  170. len -= flen;
  171. }
  172. out:
  173. /* Undo 'ntfs_look_for_free_space' */
  174. if (vcn - vcn0) {
  175. run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
  176. run_truncate(run, vcn0);
  177. }
  178. return err;
  179. }
  180. /*
  181. * attr_make_nonresident
  182. *
  183. * If page is not NULL - it is already contains resident data
  184. * and locked (called from ni_write_frame()).
  185. */
  186. int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
  187. struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
  188. u64 new_size, struct runs_tree *run,
  189. struct ATTRIB **ins_attr, struct page *page)
  190. {
  191. struct ntfs_sb_info *sbi;
  192. struct ATTRIB *attr_s;
  193. struct MFT_REC *rec;
  194. u32 used, asize, rsize, aoff;
  195. bool is_data;
  196. CLST len, alen;
  197. char *next;
  198. int err;
  199. if (attr->non_res) {
  200. *ins_attr = attr;
  201. return 0;
  202. }
  203. sbi = mi->sbi;
  204. rec = mi->mrec;
  205. attr_s = NULL;
  206. used = le32_to_cpu(rec->used);
  207. asize = le32_to_cpu(attr->size);
  208. next = Add2Ptr(attr, asize);
  209. aoff = PtrOffset(rec, attr);
  210. rsize = le32_to_cpu(attr->res.data_size);
  211. is_data = attr->type == ATTR_DATA && !attr->name_len;
  212. /* len - how many clusters required to store 'rsize' bytes */
  213. if (is_attr_compressed(attr)) {
  214. u8 shift = sbi->cluster_bits + NTFS_LZNT_CUNIT;
  215. len = ((rsize + (1u << shift) - 1) >> shift) << NTFS_LZNT_CUNIT;
  216. } else {
  217. len = bytes_to_cluster(sbi, rsize);
  218. }
  219. run_init(run);
  220. /* Make a copy of original attribute. */
  221. attr_s = kmemdup(attr, asize, GFP_NOFS);
  222. if (!attr_s) {
  223. err = -ENOMEM;
  224. goto out;
  225. }
  226. if (!len) {
  227. /* Empty resident -> Empty nonresident. */
  228. alen = 0;
  229. } else {
  230. const char *data = resident_data(attr);
  231. err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
  232. ALLOCATE_DEF, &alen, 0, NULL,
  233. NULL);
  234. if (err)
  235. goto out1;
  236. if (!rsize) {
  237. /* Empty resident -> Non empty nonresident. */
  238. } else if (!is_data) {
  239. err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
  240. if (err)
  241. goto out2;
  242. } else if (!page) {
  243. struct address_space *mapping = ni->vfs_inode.i_mapping;
  244. struct folio *folio;
  245. folio = __filemap_get_folio(
  246. mapping, 0, FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  247. mapping_gfp_mask(mapping));
  248. if (IS_ERR(folio)) {
  249. err = PTR_ERR(folio);
  250. goto out2;
  251. }
  252. folio_fill_tail(folio, 0, data, rsize);
  253. folio_mark_uptodate(folio);
  254. folio_mark_dirty(folio);
  255. folio_unlock(folio);
  256. folio_put(folio);
  257. }
  258. }
  259. /* Remove original attribute. */
  260. used -= asize;
  261. memmove(attr, Add2Ptr(attr, asize), used - aoff);
  262. rec->used = cpu_to_le32(used);
  263. mi->dirty = true;
  264. if (le)
  265. al_remove_le(ni, le);
  266. err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
  267. attr_s->name_len, run, 0, alen,
  268. attr_s->flags, &attr, NULL, NULL);
  269. if (err)
  270. goto out3;
  271. kfree(attr_s);
  272. attr->nres.data_size = cpu_to_le64(rsize);
  273. attr->nres.valid_size = attr->nres.data_size;
  274. *ins_attr = attr;
  275. if (is_data)
  276. ni->ni_flags &= ~NI_FLAG_RESIDENT;
  277. /* Resident attribute becomes non resident. */
  278. return 0;
  279. out3:
  280. attr = Add2Ptr(rec, aoff);
  281. memmove(next, attr, used - aoff);
  282. memcpy(attr, attr_s, asize);
  283. rec->used = cpu_to_le32(used + asize);
  284. mi->dirty = true;
  285. out2:
  286. /* Undo: do not trim new allocated clusters. */
  287. run_deallocate(sbi, run, false);
  288. run_close(run);
  289. out1:
  290. kfree(attr_s);
  291. out:
  292. return err;
  293. }
  294. /*
  295. * attr_set_size_res - Helper for attr_set_size().
  296. */
  297. static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
  298. struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
  299. u64 new_size, struct runs_tree *run,
  300. struct ATTRIB **ins_attr)
  301. {
  302. struct ntfs_sb_info *sbi = mi->sbi;
  303. struct MFT_REC *rec = mi->mrec;
  304. u32 used = le32_to_cpu(rec->used);
  305. u32 asize = le32_to_cpu(attr->size);
  306. u32 aoff = PtrOffset(rec, attr);
  307. u32 rsize = le32_to_cpu(attr->res.data_size);
  308. u32 tail = used - aoff - asize;
  309. char *next = Add2Ptr(attr, asize);
  310. s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
  311. if (dsize < 0) {
  312. memmove(next + dsize, next, tail);
  313. } else if (dsize > 0) {
  314. if (used + dsize > sbi->max_bytes_per_attr)
  315. return attr_make_nonresident(ni, attr, le, mi, new_size,
  316. run, ins_attr, NULL);
  317. memmove(next + dsize, next, tail);
  318. memset(next, 0, dsize);
  319. }
  320. if (new_size > rsize)
  321. memset(Add2Ptr(resident_data(attr), rsize), 0,
  322. new_size - rsize);
  323. rec->used = cpu_to_le32(used + dsize);
  324. attr->size = cpu_to_le32(asize + dsize);
  325. attr->res.data_size = cpu_to_le32(new_size);
  326. mi->dirty = true;
  327. *ins_attr = attr;
  328. return 0;
  329. }
  330. /*
  331. * attr_set_size - Change the size of attribute.
  332. *
  333. * Extend:
  334. * - Sparse/compressed: No allocated clusters.
  335. * - Normal: Append allocated and preallocated new clusters.
  336. * Shrink:
  337. * - No deallocate if @keep_prealloc is set.
  338. */
  339. int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
  340. const __le16 *name, u8 name_len, struct runs_tree *run,
  341. u64 new_size, const u64 *new_valid, bool keep_prealloc,
  342. struct ATTRIB **ret)
  343. {
  344. int err = 0;
  345. struct ntfs_sb_info *sbi = ni->mi.sbi;
  346. u8 cluster_bits = sbi->cluster_bits;
  347. bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
  348. !name_len;
  349. u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
  350. struct ATTRIB *attr = NULL, *attr_b;
  351. struct ATTR_LIST_ENTRY *le, *le_b;
  352. struct mft_inode *mi, *mi_b;
  353. CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
  354. CLST next_svcn, pre_alloc = -1, done = 0;
  355. bool is_ext, is_bad = false;
  356. bool dirty = false;
  357. u32 align;
  358. struct MFT_REC *rec;
  359. again:
  360. alen = 0;
  361. le_b = NULL;
  362. attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
  363. &mi_b);
  364. if (!attr_b) {
  365. err = -ENOENT;
  366. goto bad_inode;
  367. }
  368. if (!attr_b->non_res) {
  369. err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
  370. &attr_b);
  371. if (err)
  372. return err;
  373. /* Return if file is still resident. */
  374. if (!attr_b->non_res) {
  375. dirty = true;
  376. goto ok1;
  377. }
  378. /* Layout of records may be changed, so do a full search. */
  379. goto again;
  380. }
  381. is_ext = is_attr_ext(attr_b);
  382. align = sbi->cluster_size;
  383. if (is_ext)
  384. align <<= attr_b->nres.c_unit;
  385. old_valid = le64_to_cpu(attr_b->nres.valid_size);
  386. old_size = le64_to_cpu(attr_b->nres.data_size);
  387. old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
  388. again_1:
  389. old_alen = old_alloc >> cluster_bits;
  390. new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
  391. new_alen = new_alloc >> cluster_bits;
  392. if (keep_prealloc && new_size < old_size) {
  393. attr_b->nres.data_size = cpu_to_le64(new_size);
  394. mi_b->dirty = dirty = true;
  395. goto ok;
  396. }
  397. vcn = old_alen - 1;
  398. svcn = le64_to_cpu(attr_b->nres.svcn);
  399. evcn = le64_to_cpu(attr_b->nres.evcn);
  400. if (svcn <= vcn && vcn <= evcn) {
  401. attr = attr_b;
  402. le = le_b;
  403. mi = mi_b;
  404. } else if (!le_b) {
  405. err = -EINVAL;
  406. goto bad_inode;
  407. } else {
  408. le = le_b;
  409. attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
  410. &mi);
  411. if (!attr) {
  412. err = -EINVAL;
  413. goto bad_inode;
  414. }
  415. next_le_1:
  416. svcn = le64_to_cpu(attr->nres.svcn);
  417. evcn = le64_to_cpu(attr->nres.evcn);
  418. }
  419. /*
  420. * Here we have:
  421. * attr,mi,le - last attribute segment (containing 'vcn').
  422. * attr_b,mi_b,le_b - base (primary) attribute segment.
  423. */
  424. next_le:
  425. rec = mi->mrec;
  426. err = attr_load_runs(attr, ni, run, NULL);
  427. if (err)
  428. goto out;
  429. if (new_size > old_size) {
  430. CLST to_allocate;
  431. size_t free;
  432. if (new_alloc <= old_alloc) {
  433. attr_b->nres.data_size = cpu_to_le64(new_size);
  434. mi_b->dirty = dirty = true;
  435. goto ok;
  436. }
  437. /*
  438. * Add clusters. In simple case we have to:
  439. * - allocate space (vcn, lcn, len)
  440. * - update packed run in 'mi'
  441. * - update attr->nres.evcn
  442. * - update attr_b->nres.data_size/attr_b->nres.alloc_size
  443. */
  444. to_allocate = new_alen - old_alen;
  445. add_alloc_in_same_attr_seg:
  446. lcn = 0;
  447. if (is_mft) {
  448. /* MFT allocates clusters from MFT zone. */
  449. pre_alloc = 0;
  450. } else if (is_ext) {
  451. /* No preallocate for sparse/compress. */
  452. pre_alloc = 0;
  453. } else if (pre_alloc == -1) {
  454. pre_alloc = 0;
  455. if (type == ATTR_DATA && !name_len &&
  456. sbi->options->prealloc) {
  457. pre_alloc = bytes_to_cluster(
  458. sbi, get_pre_allocated(
  459. new_size)) -
  460. new_alen;
  461. }
  462. /* Get the last LCN to allocate from. */
  463. if (old_alen &&
  464. !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
  465. lcn = SPARSE_LCN;
  466. }
  467. if (lcn == SPARSE_LCN)
  468. lcn = 0;
  469. else if (lcn)
  470. lcn += 1;
  471. free = wnd_zeroes(&sbi->used.bitmap);
  472. if (to_allocate > free) {
  473. err = -ENOSPC;
  474. goto out;
  475. }
  476. if (pre_alloc && to_allocate + pre_alloc > free)
  477. pre_alloc = 0;
  478. }
  479. vcn = old_alen;
  480. if (is_ext) {
  481. if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
  482. false)) {
  483. err = -ENOMEM;
  484. goto out;
  485. }
  486. alen = to_allocate;
  487. } else {
  488. /* ~3 bytes per fragment. */
  489. err = attr_allocate_clusters(
  490. sbi, run, vcn, lcn, to_allocate, &pre_alloc,
  491. is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
  492. is_mft ? 0 :
  493. (sbi->record_size -
  494. le32_to_cpu(rec->used) + 8) /
  495. 3 +
  496. 1,
  497. NULL, NULL);
  498. if (err)
  499. goto out;
  500. }
  501. done += alen;
  502. vcn += alen;
  503. if (to_allocate > alen)
  504. to_allocate -= alen;
  505. else
  506. to_allocate = 0;
  507. pack_runs:
  508. err = mi_pack_runs(mi, attr, run, vcn - svcn);
  509. if (err)
  510. goto undo_1;
  511. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  512. new_alloc_tmp = (u64)next_svcn << cluster_bits;
  513. attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
  514. mi_b->dirty = dirty = true;
  515. if (next_svcn >= vcn && !to_allocate) {
  516. /* Normal way. Update attribute and exit. */
  517. attr_b->nres.data_size = cpu_to_le64(new_size);
  518. goto ok;
  519. }
  520. /* At least two MFT to avoid recursive loop. */
  521. if (is_mft && next_svcn == vcn &&
  522. ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
  523. new_size = new_alloc_tmp;
  524. attr_b->nres.data_size = attr_b->nres.alloc_size;
  525. goto ok;
  526. }
  527. if (le32_to_cpu(rec->used) < sbi->record_size) {
  528. old_alen = next_svcn;
  529. evcn = old_alen - 1;
  530. goto add_alloc_in_same_attr_seg;
  531. }
  532. attr_b->nres.data_size = attr_b->nres.alloc_size;
  533. if (new_alloc_tmp < old_valid)
  534. attr_b->nres.valid_size = attr_b->nres.data_size;
  535. if (type == ATTR_LIST) {
  536. err = ni_expand_list(ni);
  537. if (err)
  538. goto undo_2;
  539. if (next_svcn < vcn)
  540. goto pack_runs;
  541. /* Layout of records is changed. */
  542. goto again;
  543. }
  544. if (!ni->attr_list.size) {
  545. err = ni_create_attr_list(ni);
  546. /* In case of error layout of records is not changed. */
  547. if (err)
  548. goto undo_2;
  549. /* Layout of records is changed. */
  550. }
  551. if (next_svcn >= vcn) {
  552. /* This is MFT data, repeat. */
  553. goto again;
  554. }
  555. /* Insert new attribute segment. */
  556. err = ni_insert_nonresident(ni, type, name, name_len, run,
  557. next_svcn, vcn - next_svcn,
  558. attr_b->flags, &attr, &mi, NULL);
  559. /*
  560. * Layout of records maybe changed.
  561. * Find base attribute to update.
  562. */
  563. le_b = NULL;
  564. attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
  565. NULL, &mi_b);
  566. if (!attr_b) {
  567. err = -EINVAL;
  568. goto bad_inode;
  569. }
  570. if (err) {
  571. /* ni_insert_nonresident failed. */
  572. attr = NULL;
  573. goto undo_2;
  574. }
  575. /* keep runs for $MFT::$ATTR_DATA and $MFT::$ATTR_BITMAP. */
  576. if (ni->mi.rno != MFT_REC_MFT)
  577. run_truncate_head(run, evcn + 1);
  578. svcn = le64_to_cpu(attr->nres.svcn);
  579. evcn = le64_to_cpu(attr->nres.evcn);
  580. /*
  581. * Attribute is in consistency state.
  582. * Save this point to restore to if next steps fail.
  583. */
  584. old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
  585. attr_b->nres.valid_size = attr_b->nres.data_size =
  586. attr_b->nres.alloc_size = cpu_to_le64(old_size);
  587. mi_b->dirty = dirty = true;
  588. goto again_1;
  589. }
  590. if (new_size != old_size ||
  591. (new_alloc != old_alloc && !keep_prealloc)) {
  592. /*
  593. * Truncate clusters. In simple case we have to:
  594. * - update packed run in 'mi'
  595. * - update attr->nres.evcn
  596. * - update attr_b->nres.data_size/attr_b->nres.alloc_size
  597. * - mark and trim clusters as free (vcn, lcn, len)
  598. */
  599. CLST dlen = 0;
  600. vcn = max(svcn, new_alen);
  601. new_alloc_tmp = (u64)vcn << cluster_bits;
  602. if (vcn > svcn) {
  603. err = mi_pack_runs(mi, attr, run, vcn - svcn);
  604. if (err)
  605. goto out;
  606. } else if (le && le->vcn) {
  607. u16 le_sz = le16_to_cpu(le->size);
  608. /*
  609. * NOTE: List entries for one attribute are always
  610. * the same size. We deal with last entry (vcn==0)
  611. * and it is not first in entries array
  612. * (list entry for std attribute always first).
  613. * So it is safe to step back.
  614. */
  615. mi_remove_attr(NULL, mi, attr);
  616. if (!al_remove_le(ni, le)) {
  617. err = -EINVAL;
  618. goto bad_inode;
  619. }
  620. le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
  621. } else {
  622. attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
  623. mi->dirty = true;
  624. }
  625. attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
  626. if (vcn == new_alen) {
  627. attr_b->nres.data_size = cpu_to_le64(new_size);
  628. if (new_size < old_valid)
  629. attr_b->nres.valid_size =
  630. attr_b->nres.data_size;
  631. } else {
  632. if (new_alloc_tmp <=
  633. le64_to_cpu(attr_b->nres.data_size))
  634. attr_b->nres.data_size =
  635. attr_b->nres.alloc_size;
  636. if (new_alloc_tmp <
  637. le64_to_cpu(attr_b->nres.valid_size))
  638. attr_b->nres.valid_size =
  639. attr_b->nres.alloc_size;
  640. }
  641. mi_b->dirty = dirty = true;
  642. err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
  643. true);
  644. if (err)
  645. goto out;
  646. if (is_ext) {
  647. /* dlen - really deallocated clusters. */
  648. le64_sub_cpu(&attr_b->nres.total_size,
  649. ((u64)dlen << cluster_bits));
  650. }
  651. run_truncate(run, vcn);
  652. if (new_alloc_tmp <= new_alloc)
  653. goto ok;
  654. old_size = new_alloc_tmp;
  655. vcn = svcn - 1;
  656. if (le == le_b) {
  657. attr = attr_b;
  658. mi = mi_b;
  659. evcn = svcn - 1;
  660. svcn = 0;
  661. goto next_le;
  662. }
  663. if (le->type != type || le->name_len != name_len ||
  664. memcmp(le_name(le), name, name_len * sizeof(short))) {
  665. err = -EINVAL;
  666. goto bad_inode;
  667. }
  668. err = ni_load_mi(ni, le, &mi);
  669. if (err)
  670. goto out;
  671. attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
  672. if (!attr) {
  673. err = -EINVAL;
  674. goto bad_inode;
  675. }
  676. goto next_le_1;
  677. }
  678. ok:
  679. if (new_valid) {
  680. __le64 valid = cpu_to_le64(min(*new_valid, new_size));
  681. if (attr_b->nres.valid_size != valid) {
  682. attr_b->nres.valid_size = valid;
  683. mi_b->dirty = true;
  684. }
  685. }
  686. ok1:
  687. if (ret)
  688. *ret = attr_b;
  689. if (((type == ATTR_DATA && !name_len) ||
  690. (type == ATTR_ALLOC && name == I30_NAME))) {
  691. /* Update inode_set_bytes. */
  692. if (attr_b->non_res) {
  693. new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
  694. if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
  695. inode_set_bytes(&ni->vfs_inode, new_alloc);
  696. dirty = true;
  697. }
  698. }
  699. /* Don't forget to update duplicate information in parent. */
  700. if (dirty) {
  701. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  702. mark_inode_dirty(&ni->vfs_inode);
  703. }
  704. }
  705. return 0;
  706. undo_2:
  707. vcn -= alen;
  708. attr_b->nres.data_size = cpu_to_le64(old_size);
  709. attr_b->nres.valid_size = cpu_to_le64(old_valid);
  710. attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
  711. /* Restore 'attr' and 'mi'. */
  712. if (attr)
  713. goto restore_run;
  714. if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
  715. svcn <= le64_to_cpu(attr_b->nres.evcn)) {
  716. attr = attr_b;
  717. le = le_b;
  718. mi = mi_b;
  719. } else if (!le_b) {
  720. err = -EINVAL;
  721. goto bad_inode;
  722. } else {
  723. le = le_b;
  724. attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
  725. &svcn, &mi);
  726. if (!attr)
  727. goto bad_inode;
  728. }
  729. restore_run:
  730. if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
  731. is_bad = true;
  732. undo_1:
  733. run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
  734. run_truncate(run, vcn);
  735. out:
  736. if (is_bad) {
  737. bad_inode:
  738. _ntfs_bad_inode(&ni->vfs_inode);
  739. }
  740. return err;
  741. }
  742. /*
  743. * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
  744. *
  745. * @new == NULL means just to get current mapping for 'vcn'
  746. * @new != NULL means allocate real cluster if 'vcn' maps to hole
  747. * @zero - zeroout new allocated clusters
  748. *
  749. * NOTE:
  750. * - @new != NULL is called only for sparsed or compressed attributes.
  751. * - new allocated clusters are zeroed via blkdev_issue_zeroout.
  752. */
  753. int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
  754. CLST *len, bool *new, bool zero)
  755. {
  756. int err = 0;
  757. struct runs_tree *run = &ni->file.run;
  758. struct ntfs_sb_info *sbi;
  759. u8 cluster_bits;
  760. struct ATTRIB *attr, *attr_b;
  761. struct ATTR_LIST_ENTRY *le, *le_b;
  762. struct mft_inode *mi, *mi_b;
  763. CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
  764. CLST alloc, evcn;
  765. unsigned fr;
  766. u64 total_size, total_size0;
  767. int step = 0;
  768. if (new)
  769. *new = false;
  770. /* Try to find in cache. */
  771. down_read(&ni->file.run_lock);
  772. if (!run_lookup_entry(run, vcn, lcn, len, NULL))
  773. *len = 0;
  774. up_read(&ni->file.run_lock);
  775. if (*len && (*lcn != SPARSE_LCN || !new))
  776. return 0; /* Fast normal way without allocation. */
  777. /* No cluster in cache or we need to allocate cluster in hole. */
  778. sbi = ni->mi.sbi;
  779. cluster_bits = sbi->cluster_bits;
  780. ni_lock(ni);
  781. down_write(&ni->file.run_lock);
  782. /* Repeat the code above (under write lock). */
  783. if (!run_lookup_entry(run, vcn, lcn, len, NULL))
  784. *len = 0;
  785. if (*len) {
  786. if (*lcn != SPARSE_LCN || !new)
  787. goto out; /* normal way without allocation. */
  788. if (clen > *len)
  789. clen = *len;
  790. }
  791. le_b = NULL;
  792. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
  793. if (!attr_b) {
  794. err = -ENOENT;
  795. goto out;
  796. }
  797. if (!attr_b->non_res) {
  798. *lcn = RESIDENT_LCN;
  799. *len = 1;
  800. goto out;
  801. }
  802. asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
  803. if (vcn >= asize) {
  804. if (new) {
  805. err = -EINVAL;
  806. } else {
  807. *len = 1;
  808. *lcn = SPARSE_LCN;
  809. }
  810. goto out;
  811. }
  812. svcn = le64_to_cpu(attr_b->nres.svcn);
  813. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  814. attr = attr_b;
  815. le = le_b;
  816. mi = mi_b;
  817. if (le_b && (vcn < svcn || evcn1 <= vcn)) {
  818. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  819. &mi);
  820. if (!attr) {
  821. err = -EINVAL;
  822. goto out;
  823. }
  824. svcn = le64_to_cpu(attr->nres.svcn);
  825. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  826. }
  827. /* Load in cache actual information. */
  828. err = attr_load_runs(attr, ni, run, NULL);
  829. if (err)
  830. goto out;
  831. /* Check for compressed frame. */
  832. err = attr_is_frame_compressed(ni, attr_b, vcn >> NTFS_LZNT_CUNIT,
  833. &hint, run);
  834. if (err)
  835. goto out;
  836. if (hint) {
  837. /* if frame is compressed - don't touch it. */
  838. *lcn = COMPRESSED_LCN;
  839. /* length to the end of frame. */
  840. *len = NTFS_LZNT_CLUSTERS - (vcn & (NTFS_LZNT_CLUSTERS - 1));
  841. err = 0;
  842. goto out;
  843. }
  844. if (!*len) {
  845. if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
  846. if (*lcn != SPARSE_LCN || !new)
  847. goto ok; /* Slow normal way without allocation. */
  848. if (clen > *len)
  849. clen = *len;
  850. } else if (!new) {
  851. /* Here we may return -ENOENT.
  852. * In any case caller gets zero length. */
  853. goto ok;
  854. }
  855. }
  856. if (!is_attr_ext(attr_b)) {
  857. /* The code below only for sparsed or compressed attributes. */
  858. err = -EINVAL;
  859. goto out;
  860. }
  861. vcn0 = vcn;
  862. to_alloc = clen;
  863. fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
  864. /* Allocate frame aligned clusters.
  865. * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
  866. * ntfs3 uses 1 cluster per frame for new created sparsed files. */
  867. if (attr_b->nres.c_unit) {
  868. CLST clst_per_frame = 1u << attr_b->nres.c_unit;
  869. CLST cmask = ~(clst_per_frame - 1);
  870. /* Get frame aligned vcn and to_alloc. */
  871. vcn = vcn0 & cmask;
  872. to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
  873. if (fr < clst_per_frame)
  874. fr = clst_per_frame;
  875. zero = true;
  876. /* Check if 'vcn' and 'vcn0' in different attribute segments. */
  877. if (vcn < svcn || evcn1 <= vcn) {
  878. struct ATTRIB *attr2;
  879. /* Load runs for truncated vcn. */
  880. attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL,
  881. 0, &vcn, &mi);
  882. if (!attr2) {
  883. err = -EINVAL;
  884. goto out;
  885. }
  886. evcn1 = le64_to_cpu(attr2->nres.evcn) + 1;
  887. err = attr_load_runs(attr2, ni, run, NULL);
  888. if (err)
  889. goto out;
  890. }
  891. }
  892. if (vcn + to_alloc > asize)
  893. to_alloc = asize - vcn;
  894. /* Get the last LCN to allocate from. */
  895. hint = 0;
  896. if (vcn > evcn1) {
  897. if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
  898. false)) {
  899. err = -ENOMEM;
  900. goto out;
  901. }
  902. } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
  903. hint = -1;
  904. }
  905. /* Allocate and zeroout new clusters. */
  906. err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
  907. zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
  908. fr, lcn, len);
  909. if (err)
  910. goto out;
  911. *new = true;
  912. step = 1;
  913. end = vcn + alen;
  914. /* Save 'total_size0' to restore if error. */
  915. total_size0 = le64_to_cpu(attr_b->nres.total_size);
  916. total_size = total_size0 + ((u64)alen << cluster_bits);
  917. if (vcn != vcn0) {
  918. if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
  919. err = -EINVAL;
  920. goto out;
  921. }
  922. if (*lcn == SPARSE_LCN) {
  923. /* Internal error. Should not happened. */
  924. WARN_ON(1);
  925. err = -EINVAL;
  926. goto out;
  927. }
  928. /* Check case when vcn0 + len overlaps new allocated clusters. */
  929. if (vcn0 + *len > end)
  930. *len = end - vcn0;
  931. }
  932. repack:
  933. err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
  934. if (err)
  935. goto out;
  936. attr_b->nres.total_size = cpu_to_le64(total_size);
  937. inode_set_bytes(&ni->vfs_inode, total_size);
  938. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  939. mi_b->dirty = true;
  940. mark_inode_dirty(&ni->vfs_inode);
  941. /* Stored [vcn : next_svcn) from [vcn : end). */
  942. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  943. if (end <= evcn1) {
  944. if (next_svcn == evcn1) {
  945. /* Normal way. Update attribute and exit. */
  946. goto ok;
  947. }
  948. /* Add new segment [next_svcn : evcn1 - next_svcn). */
  949. if (!ni->attr_list.size) {
  950. err = ni_create_attr_list(ni);
  951. if (err)
  952. goto undo1;
  953. /* Layout of records is changed. */
  954. le_b = NULL;
  955. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
  956. 0, NULL, &mi_b);
  957. if (!attr_b) {
  958. err = -ENOENT;
  959. goto out;
  960. }
  961. attr = attr_b;
  962. le = le_b;
  963. mi = mi_b;
  964. goto repack;
  965. }
  966. }
  967. /*
  968. * The code below may require additional cluster (to extend attribute list)
  969. * and / or one MFT record
  970. * It is too complex to undo operations if -ENOSPC occurs deep inside
  971. * in 'ni_insert_nonresident'.
  972. * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
  973. */
  974. if (!ntfs_check_for_free_space(sbi, 1, 1)) {
  975. /* Undo step 1. */
  976. err = -ENOSPC;
  977. goto undo1;
  978. }
  979. step = 2;
  980. svcn = evcn1;
  981. /* Estimate next attribute. */
  982. attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
  983. if (!attr) {
  984. /* Insert new attribute segment. */
  985. goto ins_ext;
  986. }
  987. /* Try to update existed attribute segment. */
  988. alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
  989. evcn = le64_to_cpu(attr->nres.evcn);
  990. if (end < next_svcn)
  991. end = next_svcn;
  992. while (end > evcn) {
  993. /* Remove segment [svcn : evcn). */
  994. mi_remove_attr(NULL, mi, attr);
  995. if (!al_remove_le(ni, le)) {
  996. err = -EINVAL;
  997. goto out;
  998. }
  999. if (evcn + 1 >= alloc) {
  1000. /* Last attribute segment. */
  1001. evcn1 = evcn + 1;
  1002. goto ins_ext;
  1003. }
  1004. if (ni_load_mi(ni, le, &mi)) {
  1005. attr = NULL;
  1006. goto out;
  1007. }
  1008. attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, &le->id);
  1009. if (!attr) {
  1010. err = -EINVAL;
  1011. goto out;
  1012. }
  1013. svcn = le64_to_cpu(attr->nres.svcn);
  1014. evcn = le64_to_cpu(attr->nres.evcn);
  1015. }
  1016. if (end < svcn)
  1017. end = svcn;
  1018. err = attr_load_runs(attr, ni, run, &end);
  1019. if (err)
  1020. goto out;
  1021. evcn1 = evcn + 1;
  1022. attr->nres.svcn = cpu_to_le64(next_svcn);
  1023. err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
  1024. if (err)
  1025. goto out;
  1026. le->vcn = cpu_to_le64(next_svcn);
  1027. ni->attr_list.dirty = true;
  1028. mi->dirty = true;
  1029. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  1030. ins_ext:
  1031. if (evcn1 > next_svcn) {
  1032. err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
  1033. next_svcn, evcn1 - next_svcn,
  1034. attr_b->flags, &attr, &mi, NULL);
  1035. if (err)
  1036. goto out;
  1037. }
  1038. ok:
  1039. run_truncate_around(run, vcn);
  1040. out:
  1041. if (err && step > 1) {
  1042. /* Too complex to restore. */
  1043. _ntfs_bad_inode(&ni->vfs_inode);
  1044. }
  1045. up_write(&ni->file.run_lock);
  1046. ni_unlock(ni);
  1047. return err;
  1048. undo1:
  1049. /* Undo step1. */
  1050. attr_b->nres.total_size = cpu_to_le64(total_size0);
  1051. inode_set_bytes(&ni->vfs_inode, total_size0);
  1052. if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) ||
  1053. !run_add_entry(run, vcn, SPARSE_LCN, alen, false) ||
  1054. mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
  1055. _ntfs_bad_inode(&ni->vfs_inode);
  1056. }
  1057. goto out;
  1058. }
  1059. int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio)
  1060. {
  1061. u64 vbo;
  1062. struct ATTRIB *attr;
  1063. u32 data_size;
  1064. size_t len;
  1065. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
  1066. if (!attr)
  1067. return -EINVAL;
  1068. if (attr->non_res)
  1069. return E_NTFS_NONRESIDENT;
  1070. vbo = folio->index << PAGE_SHIFT;
  1071. data_size = le32_to_cpu(attr->res.data_size);
  1072. if (vbo > data_size)
  1073. len = 0;
  1074. else
  1075. len = min(data_size - vbo, folio_size(folio));
  1076. folio_fill_tail(folio, 0, resident_data(attr) + vbo, len);
  1077. folio_mark_uptodate(folio);
  1078. return 0;
  1079. }
  1080. int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio)
  1081. {
  1082. u64 vbo;
  1083. struct mft_inode *mi;
  1084. struct ATTRIB *attr;
  1085. u32 data_size;
  1086. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
  1087. if (!attr)
  1088. return -EINVAL;
  1089. if (attr->non_res) {
  1090. /* Return special error code to check this case. */
  1091. return E_NTFS_NONRESIDENT;
  1092. }
  1093. vbo = folio->index << PAGE_SHIFT;
  1094. data_size = le32_to_cpu(attr->res.data_size);
  1095. if (vbo < data_size) {
  1096. char *data = resident_data(attr);
  1097. size_t len = min(data_size - vbo, folio_size(folio));
  1098. memcpy_from_folio(data + vbo, folio, 0, len);
  1099. mi->dirty = true;
  1100. }
  1101. ni->i_valid = data_size;
  1102. return 0;
  1103. }
  1104. /*
  1105. * attr_load_runs_vcn - Load runs with VCN.
  1106. */
  1107. int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
  1108. const __le16 *name, u8 name_len, struct runs_tree *run,
  1109. CLST vcn)
  1110. {
  1111. struct ATTRIB *attr;
  1112. int err;
  1113. CLST svcn, evcn;
  1114. u16 ro;
  1115. if (!ni) {
  1116. /* Is record corrupted? */
  1117. return -ENOENT;
  1118. }
  1119. attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
  1120. if (!attr) {
  1121. /* Is record corrupted? */
  1122. return -ENOENT;
  1123. }
  1124. svcn = le64_to_cpu(attr->nres.svcn);
  1125. evcn = le64_to_cpu(attr->nres.evcn);
  1126. if (evcn < vcn || vcn < svcn) {
  1127. /* Is record corrupted? */
  1128. return -EINVAL;
  1129. }
  1130. ro = le16_to_cpu(attr->nres.run_off);
  1131. if (ro > le32_to_cpu(attr->size))
  1132. return -EINVAL;
  1133. err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
  1134. Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
  1135. if (err < 0)
  1136. return err;
  1137. return 0;
  1138. }
  1139. /*
  1140. * attr_load_runs_range - Load runs for given range [from to).
  1141. */
  1142. int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
  1143. const __le16 *name, u8 name_len, struct runs_tree *run,
  1144. u64 from, u64 to)
  1145. {
  1146. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1147. u8 cluster_bits = sbi->cluster_bits;
  1148. CLST vcn;
  1149. CLST vcn_last = (to - 1) >> cluster_bits;
  1150. CLST lcn, clen;
  1151. int err;
  1152. for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
  1153. if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
  1154. err = attr_load_runs_vcn(ni, type, name, name_len, run,
  1155. vcn);
  1156. if (err)
  1157. return err;
  1158. clen = 0; /* Next run_lookup_entry(vcn) must be success. */
  1159. }
  1160. }
  1161. return 0;
  1162. }
  1163. #ifdef CONFIG_NTFS3_LZX_XPRESS
  1164. /*
  1165. * attr_wof_frame_info
  1166. *
  1167. * Read header of Xpress/LZX file to get info about frame.
  1168. */
  1169. int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
  1170. struct runs_tree *run, u64 frame, u64 frames,
  1171. u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
  1172. {
  1173. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1174. u64 vbo[2], off[2], wof_size;
  1175. u32 voff;
  1176. u8 bytes_per_off;
  1177. char *addr;
  1178. struct folio *folio;
  1179. int i, err;
  1180. __le32 *off32;
  1181. __le64 *off64;
  1182. if (ni->vfs_inode.i_size < 0x100000000ull) {
  1183. /* File starts with array of 32 bit offsets. */
  1184. bytes_per_off = sizeof(__le32);
  1185. vbo[1] = frame << 2;
  1186. *vbo_data = frames << 2;
  1187. } else {
  1188. /* File starts with array of 64 bit offsets. */
  1189. bytes_per_off = sizeof(__le64);
  1190. vbo[1] = frame << 3;
  1191. *vbo_data = frames << 3;
  1192. }
  1193. /*
  1194. * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
  1195. * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
  1196. */
  1197. if (!attr->non_res) {
  1198. if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
  1199. _ntfs_bad_inode(&ni->vfs_inode);
  1200. return -EINVAL;
  1201. }
  1202. addr = resident_data(attr);
  1203. if (bytes_per_off == sizeof(__le32)) {
  1204. off32 = Add2Ptr(addr, vbo[1]);
  1205. off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
  1206. off[1] = le32_to_cpu(off32[0]);
  1207. } else {
  1208. off64 = Add2Ptr(addr, vbo[1]);
  1209. off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
  1210. off[1] = le64_to_cpu(off64[0]);
  1211. }
  1212. *vbo_data += off[0];
  1213. *ondisk_size = off[1] - off[0];
  1214. return 0;
  1215. }
  1216. wof_size = le64_to_cpu(attr->nres.data_size);
  1217. down_write(&ni->file.run_lock);
  1218. folio = ni->file.offs_folio;
  1219. if (!folio) {
  1220. folio = folio_alloc(GFP_KERNEL, 0);
  1221. if (!folio) {
  1222. err = -ENOMEM;
  1223. goto out;
  1224. }
  1225. folio->index = -1;
  1226. ni->file.offs_folio = folio;
  1227. }
  1228. folio_lock(folio);
  1229. addr = folio_address(folio);
  1230. if (vbo[1]) {
  1231. voff = vbo[1] & (PAGE_SIZE - 1);
  1232. vbo[0] = vbo[1] - bytes_per_off;
  1233. i = 0;
  1234. } else {
  1235. voff = 0;
  1236. vbo[0] = 0;
  1237. off[0] = 0;
  1238. i = 1;
  1239. }
  1240. do {
  1241. pgoff_t index = vbo[i] >> PAGE_SHIFT;
  1242. if (index != folio->index) {
  1243. struct page *page = &folio->page;
  1244. u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
  1245. u64 to = min(from + PAGE_SIZE, wof_size);
  1246. err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
  1247. ARRAY_SIZE(WOF_NAME), run,
  1248. from, to);
  1249. if (err)
  1250. goto out1;
  1251. err = ntfs_bio_pages(sbi, run, &page, 1, from,
  1252. to - from, REQ_OP_READ);
  1253. if (err) {
  1254. folio->index = -1;
  1255. goto out1;
  1256. }
  1257. folio->index = index;
  1258. }
  1259. if (i) {
  1260. if (bytes_per_off == sizeof(__le32)) {
  1261. off32 = Add2Ptr(addr, voff);
  1262. off[1] = le32_to_cpu(*off32);
  1263. } else {
  1264. off64 = Add2Ptr(addr, voff);
  1265. off[1] = le64_to_cpu(*off64);
  1266. }
  1267. } else if (!voff) {
  1268. if (bytes_per_off == sizeof(__le32)) {
  1269. off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
  1270. off[0] = le32_to_cpu(*off32);
  1271. } else {
  1272. off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
  1273. off[0] = le64_to_cpu(*off64);
  1274. }
  1275. } else {
  1276. /* Two values in one page. */
  1277. if (bytes_per_off == sizeof(__le32)) {
  1278. off32 = Add2Ptr(addr, voff);
  1279. off[0] = le32_to_cpu(off32[-1]);
  1280. off[1] = le32_to_cpu(off32[0]);
  1281. } else {
  1282. off64 = Add2Ptr(addr, voff);
  1283. off[0] = le64_to_cpu(off64[-1]);
  1284. off[1] = le64_to_cpu(off64[0]);
  1285. }
  1286. break;
  1287. }
  1288. } while (++i < 2);
  1289. *vbo_data += off[0];
  1290. *ondisk_size = off[1] - off[0];
  1291. out1:
  1292. folio_unlock(folio);
  1293. out:
  1294. up_write(&ni->file.run_lock);
  1295. return err;
  1296. }
  1297. #endif
  1298. /*
  1299. * attr_is_frame_compressed - Used to detect compressed frame.
  1300. *
  1301. * attr - base (primary) attribute segment.
  1302. * run - run to use, usually == &ni->file.run.
  1303. * Only base segments contains valid 'attr->nres.c_unit'
  1304. */
  1305. int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
  1306. CLST frame, CLST *clst_data, struct runs_tree *run)
  1307. {
  1308. int err;
  1309. u32 clst_frame;
  1310. CLST clen, lcn, vcn, alen, slen, vcn_next;
  1311. size_t idx;
  1312. *clst_data = 0;
  1313. if (!is_attr_compressed(attr))
  1314. return 0;
  1315. if (!attr->non_res)
  1316. return 0;
  1317. clst_frame = 1u << attr->nres.c_unit;
  1318. vcn = frame * clst_frame;
  1319. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
  1320. err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
  1321. attr->name_len, run, vcn);
  1322. if (err)
  1323. return err;
  1324. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
  1325. return -EINVAL;
  1326. }
  1327. if (lcn == SPARSE_LCN) {
  1328. /* Sparsed frame. */
  1329. return 0;
  1330. }
  1331. if (clen >= clst_frame) {
  1332. /*
  1333. * The frame is not compressed 'cause
  1334. * it does not contain any sparse clusters.
  1335. */
  1336. *clst_data = clst_frame;
  1337. return 0;
  1338. }
  1339. alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
  1340. slen = 0;
  1341. *clst_data = clen;
  1342. /*
  1343. * The frame is compressed if *clst_data + slen >= clst_frame.
  1344. * Check next fragments.
  1345. */
  1346. while ((vcn += clen) < alen) {
  1347. vcn_next = vcn;
  1348. if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
  1349. vcn_next != vcn) {
  1350. err = attr_load_runs_vcn(ni, attr->type,
  1351. attr_name(attr),
  1352. attr->name_len, run, vcn_next);
  1353. if (err)
  1354. return err;
  1355. vcn = vcn_next;
  1356. if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
  1357. return -EINVAL;
  1358. }
  1359. if (lcn == SPARSE_LCN) {
  1360. slen += clen;
  1361. } else {
  1362. if (slen) {
  1363. /*
  1364. * Data_clusters + sparse_clusters =
  1365. * not enough for frame.
  1366. */
  1367. return -EINVAL;
  1368. }
  1369. *clst_data += clen;
  1370. }
  1371. if (*clst_data + slen >= clst_frame) {
  1372. if (!slen) {
  1373. /*
  1374. * There is no sparsed clusters in this frame
  1375. * so it is not compressed.
  1376. */
  1377. *clst_data = clst_frame;
  1378. } else {
  1379. /* Frame is compressed. */
  1380. }
  1381. break;
  1382. }
  1383. }
  1384. return 0;
  1385. }
  1386. /*
  1387. * attr_allocate_frame - Allocate/free clusters for @frame.
  1388. *
  1389. * Assumed: down_write(&ni->file.run_lock);
  1390. */
  1391. int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
  1392. u64 new_valid)
  1393. {
  1394. int err = 0;
  1395. struct runs_tree *run = &ni->file.run;
  1396. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1397. struct ATTRIB *attr = NULL, *attr_b;
  1398. struct ATTR_LIST_ENTRY *le, *le_b;
  1399. struct mft_inode *mi, *mi_b;
  1400. CLST svcn, evcn1, next_svcn, len;
  1401. CLST vcn, end, clst_data;
  1402. u64 total_size, valid_size, data_size;
  1403. le_b = NULL;
  1404. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
  1405. if (!attr_b)
  1406. return -ENOENT;
  1407. if (!is_attr_ext(attr_b))
  1408. return -EINVAL;
  1409. vcn = frame << NTFS_LZNT_CUNIT;
  1410. total_size = le64_to_cpu(attr_b->nres.total_size);
  1411. svcn = le64_to_cpu(attr_b->nres.svcn);
  1412. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  1413. data_size = le64_to_cpu(attr_b->nres.data_size);
  1414. if (svcn <= vcn && vcn < evcn1) {
  1415. attr = attr_b;
  1416. le = le_b;
  1417. mi = mi_b;
  1418. } else if (!le_b) {
  1419. err = -EINVAL;
  1420. goto out;
  1421. } else {
  1422. le = le_b;
  1423. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  1424. &mi);
  1425. if (!attr) {
  1426. err = -EINVAL;
  1427. goto out;
  1428. }
  1429. svcn = le64_to_cpu(attr->nres.svcn);
  1430. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  1431. }
  1432. err = attr_load_runs(attr, ni, run, NULL);
  1433. if (err)
  1434. goto out;
  1435. err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data, run);
  1436. if (err)
  1437. goto out;
  1438. total_size -= (u64)clst_data << sbi->cluster_bits;
  1439. len = bytes_to_cluster(sbi, compr_size);
  1440. if (len == clst_data)
  1441. goto out;
  1442. if (len < clst_data) {
  1443. err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
  1444. NULL, true);
  1445. if (err)
  1446. goto out;
  1447. if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
  1448. false)) {
  1449. err = -ENOMEM;
  1450. goto out;
  1451. }
  1452. end = vcn + clst_data;
  1453. /* Run contains updated range [vcn + len : end). */
  1454. } else {
  1455. CLST alen, hint = 0;
  1456. /* Get the last LCN to allocate from. */
  1457. if (vcn + clst_data &&
  1458. !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
  1459. NULL)) {
  1460. hint = -1;
  1461. }
  1462. err = attr_allocate_clusters(sbi, run, vcn + clst_data,
  1463. hint + 1, len - clst_data, NULL,
  1464. ALLOCATE_DEF, &alen, 0, NULL,
  1465. NULL);
  1466. if (err)
  1467. goto out;
  1468. end = vcn + len;
  1469. /* Run contains updated range [vcn + clst_data : end). */
  1470. }
  1471. total_size += (u64)len << sbi->cluster_bits;
  1472. repack:
  1473. err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
  1474. if (err)
  1475. goto out;
  1476. attr_b->nres.total_size = cpu_to_le64(total_size);
  1477. inode_set_bytes(&ni->vfs_inode, total_size);
  1478. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  1479. mi_b->dirty = true;
  1480. mark_inode_dirty(&ni->vfs_inode);
  1481. /* Stored [vcn : next_svcn) from [vcn : end). */
  1482. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  1483. if (end <= evcn1) {
  1484. if (next_svcn == evcn1) {
  1485. /* Normal way. Update attribute and exit. */
  1486. goto ok;
  1487. }
  1488. /* Add new segment [next_svcn : evcn1 - next_svcn). */
  1489. if (!ni->attr_list.size) {
  1490. err = ni_create_attr_list(ni);
  1491. if (err)
  1492. goto out;
  1493. /* Layout of records is changed. */
  1494. le_b = NULL;
  1495. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
  1496. 0, NULL, &mi_b);
  1497. if (!attr_b) {
  1498. err = -ENOENT;
  1499. goto out;
  1500. }
  1501. attr = attr_b;
  1502. le = le_b;
  1503. mi = mi_b;
  1504. goto repack;
  1505. }
  1506. }
  1507. svcn = evcn1;
  1508. /* Estimate next attribute. */
  1509. attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
  1510. if (attr) {
  1511. CLST alloc = bytes_to_cluster(
  1512. sbi, le64_to_cpu(attr_b->nres.alloc_size));
  1513. CLST evcn = le64_to_cpu(attr->nres.evcn);
  1514. if (end < next_svcn)
  1515. end = next_svcn;
  1516. while (end > evcn) {
  1517. /* Remove segment [svcn : evcn). */
  1518. mi_remove_attr(NULL, mi, attr);
  1519. if (!al_remove_le(ni, le)) {
  1520. err = -EINVAL;
  1521. goto out;
  1522. }
  1523. if (evcn + 1 >= alloc) {
  1524. /* Last attribute segment. */
  1525. evcn1 = evcn + 1;
  1526. goto ins_ext;
  1527. }
  1528. if (ni_load_mi(ni, le, &mi)) {
  1529. attr = NULL;
  1530. goto out;
  1531. }
  1532. attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
  1533. &le->id);
  1534. if (!attr) {
  1535. err = -EINVAL;
  1536. goto out;
  1537. }
  1538. svcn = le64_to_cpu(attr->nres.svcn);
  1539. evcn = le64_to_cpu(attr->nres.evcn);
  1540. }
  1541. if (end < svcn)
  1542. end = svcn;
  1543. err = attr_load_runs(attr, ni, run, &end);
  1544. if (err)
  1545. goto out;
  1546. evcn1 = evcn + 1;
  1547. attr->nres.svcn = cpu_to_le64(next_svcn);
  1548. err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
  1549. if (err)
  1550. goto out;
  1551. le->vcn = cpu_to_le64(next_svcn);
  1552. ni->attr_list.dirty = true;
  1553. mi->dirty = true;
  1554. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  1555. }
  1556. ins_ext:
  1557. if (evcn1 > next_svcn) {
  1558. err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
  1559. next_svcn, evcn1 - next_svcn,
  1560. attr_b->flags, &attr, &mi, NULL);
  1561. if (err)
  1562. goto out;
  1563. }
  1564. ok:
  1565. run_truncate_around(run, vcn);
  1566. out:
  1567. if (attr_b) {
  1568. if (new_valid > data_size)
  1569. new_valid = data_size;
  1570. valid_size = le64_to_cpu(attr_b->nres.valid_size);
  1571. if (new_valid != valid_size) {
  1572. attr_b->nres.valid_size = cpu_to_le64(valid_size);
  1573. mi_b->dirty = true;
  1574. }
  1575. }
  1576. return err;
  1577. }
  1578. /*
  1579. * attr_collapse_range - Collapse range in file.
  1580. */
  1581. int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
  1582. {
  1583. int err = 0;
  1584. struct runs_tree *run = &ni->file.run;
  1585. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1586. struct ATTRIB *attr = NULL, *attr_b;
  1587. struct ATTR_LIST_ENTRY *le, *le_b;
  1588. struct mft_inode *mi, *mi_b;
  1589. CLST svcn, evcn1, len, dealloc, alen;
  1590. CLST vcn, end;
  1591. u64 valid_size, data_size, alloc_size, total_size;
  1592. u32 mask;
  1593. __le16 a_flags;
  1594. if (!bytes)
  1595. return 0;
  1596. le_b = NULL;
  1597. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
  1598. if (!attr_b)
  1599. return -ENOENT;
  1600. if (!attr_b->non_res) {
  1601. /* Attribute is resident. Nothing to do? */
  1602. return 0;
  1603. }
  1604. data_size = le64_to_cpu(attr_b->nres.data_size);
  1605. alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
  1606. a_flags = attr_b->flags;
  1607. if (is_attr_ext(attr_b)) {
  1608. total_size = le64_to_cpu(attr_b->nres.total_size);
  1609. mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
  1610. } else {
  1611. total_size = alloc_size;
  1612. mask = sbi->cluster_mask;
  1613. }
  1614. if ((vbo & mask) || (bytes & mask)) {
  1615. /* Allow to collapse only cluster aligned ranges. */
  1616. return -EINVAL;
  1617. }
  1618. if (vbo > data_size)
  1619. return -EINVAL;
  1620. down_write(&ni->file.run_lock);
  1621. if (vbo + bytes >= data_size) {
  1622. u64 new_valid = min(ni->i_valid, vbo);
  1623. /* Simple truncate file at 'vbo'. */
  1624. truncate_setsize(&ni->vfs_inode, vbo);
  1625. err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
  1626. &new_valid, true, NULL);
  1627. if (!err && new_valid < ni->i_valid)
  1628. ni->i_valid = new_valid;
  1629. goto out;
  1630. }
  1631. /*
  1632. * Enumerate all attribute segments and collapse.
  1633. */
  1634. alen = alloc_size >> sbi->cluster_bits;
  1635. vcn = vbo >> sbi->cluster_bits;
  1636. len = bytes >> sbi->cluster_bits;
  1637. end = vcn + len;
  1638. dealloc = 0;
  1639. svcn = le64_to_cpu(attr_b->nres.svcn);
  1640. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  1641. if (svcn <= vcn && vcn < evcn1) {
  1642. attr = attr_b;
  1643. le = le_b;
  1644. mi = mi_b;
  1645. } else if (!le_b) {
  1646. err = -EINVAL;
  1647. goto out;
  1648. } else {
  1649. le = le_b;
  1650. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  1651. &mi);
  1652. if (!attr) {
  1653. err = -EINVAL;
  1654. goto out;
  1655. }
  1656. svcn = le64_to_cpu(attr->nres.svcn);
  1657. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  1658. }
  1659. for (;;) {
  1660. if (svcn >= end) {
  1661. /* Shift VCN- */
  1662. attr->nres.svcn = cpu_to_le64(svcn - len);
  1663. attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
  1664. if (le) {
  1665. le->vcn = attr->nres.svcn;
  1666. ni->attr_list.dirty = true;
  1667. }
  1668. mi->dirty = true;
  1669. } else if (svcn < vcn || end < evcn1) {
  1670. CLST vcn1, eat, next_svcn;
  1671. /* Collapse a part of this attribute segment. */
  1672. err = attr_load_runs(attr, ni, run, &svcn);
  1673. if (err)
  1674. goto out;
  1675. vcn1 = max(vcn, svcn);
  1676. eat = min(end, evcn1) - vcn1;
  1677. err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
  1678. true);
  1679. if (err)
  1680. goto out;
  1681. if (!run_collapse_range(run, vcn1, eat)) {
  1682. err = -ENOMEM;
  1683. goto out;
  1684. }
  1685. if (svcn >= vcn) {
  1686. /* Shift VCN */
  1687. attr->nres.svcn = cpu_to_le64(vcn);
  1688. if (le) {
  1689. le->vcn = attr->nres.svcn;
  1690. ni->attr_list.dirty = true;
  1691. }
  1692. }
  1693. err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
  1694. if (err)
  1695. goto out;
  1696. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  1697. if (next_svcn + eat < evcn1) {
  1698. err = ni_insert_nonresident(
  1699. ni, ATTR_DATA, NULL, 0, run, next_svcn,
  1700. evcn1 - eat - next_svcn, a_flags, &attr,
  1701. &mi, &le);
  1702. if (err)
  1703. goto out;
  1704. /* Layout of records maybe changed. */
  1705. attr_b = NULL;
  1706. }
  1707. /* Free all allocated memory. */
  1708. run_truncate(run, 0);
  1709. } else {
  1710. u16 le_sz;
  1711. u16 roff = le16_to_cpu(attr->nres.run_off);
  1712. if (roff > le32_to_cpu(attr->size)) {
  1713. err = -EINVAL;
  1714. goto out;
  1715. }
  1716. run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
  1717. evcn1 - 1, svcn, Add2Ptr(attr, roff),
  1718. le32_to_cpu(attr->size) - roff);
  1719. /* Delete this attribute segment. */
  1720. mi_remove_attr(NULL, mi, attr);
  1721. if (!le)
  1722. break;
  1723. le_sz = le16_to_cpu(le->size);
  1724. if (!al_remove_le(ni, le)) {
  1725. err = -EINVAL;
  1726. goto out;
  1727. }
  1728. if (evcn1 >= alen)
  1729. break;
  1730. if (!svcn) {
  1731. /* Load next record that contains this attribute. */
  1732. if (ni_load_mi(ni, le, &mi)) {
  1733. err = -EINVAL;
  1734. goto out;
  1735. }
  1736. /* Look for required attribute. */
  1737. attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
  1738. 0, &le->id);
  1739. if (!attr) {
  1740. err = -EINVAL;
  1741. goto out;
  1742. }
  1743. goto next_attr;
  1744. }
  1745. le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
  1746. }
  1747. if (evcn1 >= alen)
  1748. break;
  1749. attr = ni_enum_attr_ex(ni, attr, &le, &mi);
  1750. if (!attr) {
  1751. err = -EINVAL;
  1752. goto out;
  1753. }
  1754. next_attr:
  1755. svcn = le64_to_cpu(attr->nres.svcn);
  1756. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  1757. }
  1758. if (!attr_b) {
  1759. le_b = NULL;
  1760. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
  1761. &mi_b);
  1762. if (!attr_b) {
  1763. err = -ENOENT;
  1764. goto out;
  1765. }
  1766. }
  1767. data_size -= bytes;
  1768. valid_size = ni->i_valid;
  1769. if (vbo + bytes <= valid_size)
  1770. valid_size -= bytes;
  1771. else if (vbo < valid_size)
  1772. valid_size = vbo;
  1773. attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
  1774. attr_b->nres.data_size = cpu_to_le64(data_size);
  1775. attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
  1776. total_size -= (u64)dealloc << sbi->cluster_bits;
  1777. if (is_attr_ext(attr_b))
  1778. attr_b->nres.total_size = cpu_to_le64(total_size);
  1779. mi_b->dirty = true;
  1780. /* Update inode size. */
  1781. ni->i_valid = valid_size;
  1782. i_size_write(&ni->vfs_inode, data_size);
  1783. inode_set_bytes(&ni->vfs_inode, total_size);
  1784. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  1785. mark_inode_dirty(&ni->vfs_inode);
  1786. out:
  1787. up_write(&ni->file.run_lock);
  1788. if (err)
  1789. _ntfs_bad_inode(&ni->vfs_inode);
  1790. return err;
  1791. }
  1792. /*
  1793. * attr_punch_hole
  1794. *
  1795. * Not for normal files.
  1796. */
  1797. int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
  1798. {
  1799. int err = 0;
  1800. struct runs_tree *run = &ni->file.run;
  1801. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1802. struct ATTRIB *attr = NULL, *attr_b;
  1803. struct ATTR_LIST_ENTRY *le, *le_b;
  1804. struct mft_inode *mi, *mi_b;
  1805. CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
  1806. u64 total_size, alloc_size;
  1807. u32 mask;
  1808. __le16 a_flags;
  1809. struct runs_tree run2;
  1810. if (!bytes)
  1811. return 0;
  1812. le_b = NULL;
  1813. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
  1814. if (!attr_b)
  1815. return -ENOENT;
  1816. if (!attr_b->non_res) {
  1817. u32 data_size = le32_to_cpu(attr_b->res.data_size);
  1818. u32 from, to;
  1819. if (vbo > data_size)
  1820. return 0;
  1821. from = vbo;
  1822. to = min_t(u64, vbo + bytes, data_size);
  1823. memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
  1824. return 0;
  1825. }
  1826. if (!is_attr_ext(attr_b))
  1827. return -EOPNOTSUPP;
  1828. alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
  1829. total_size = le64_to_cpu(attr_b->nres.total_size);
  1830. if (vbo >= alloc_size) {
  1831. /* NOTE: It is allowed. */
  1832. return 0;
  1833. }
  1834. mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
  1835. bytes += vbo;
  1836. if (bytes > alloc_size)
  1837. bytes = alloc_size;
  1838. bytes -= vbo;
  1839. if ((vbo & mask) || (bytes & mask)) {
  1840. /* We have to zero a range(s). */
  1841. if (frame_size == NULL) {
  1842. /* Caller insists range is aligned. */
  1843. return -EINVAL;
  1844. }
  1845. *frame_size = mask + 1;
  1846. return E_NTFS_NOTALIGNED;
  1847. }
  1848. down_write(&ni->file.run_lock);
  1849. run_init(&run2);
  1850. run_truncate(run, 0);
  1851. /*
  1852. * Enumerate all attribute segments and punch hole where necessary.
  1853. */
  1854. alen = alloc_size >> sbi->cluster_bits;
  1855. vcn = vbo >> sbi->cluster_bits;
  1856. len = bytes >> sbi->cluster_bits;
  1857. end = vcn + len;
  1858. hole = 0;
  1859. svcn = le64_to_cpu(attr_b->nres.svcn);
  1860. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  1861. a_flags = attr_b->flags;
  1862. if (svcn <= vcn && vcn < evcn1) {
  1863. attr = attr_b;
  1864. le = le_b;
  1865. mi = mi_b;
  1866. } else if (!le_b) {
  1867. err = -EINVAL;
  1868. goto bad_inode;
  1869. } else {
  1870. le = le_b;
  1871. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  1872. &mi);
  1873. if (!attr) {
  1874. err = -EINVAL;
  1875. goto bad_inode;
  1876. }
  1877. svcn = le64_to_cpu(attr->nres.svcn);
  1878. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  1879. }
  1880. while (svcn < end) {
  1881. CLST vcn1, zero, hole2 = hole;
  1882. err = attr_load_runs(attr, ni, run, &svcn);
  1883. if (err)
  1884. goto done;
  1885. vcn1 = max(vcn, svcn);
  1886. zero = min(end, evcn1) - vcn1;
  1887. /*
  1888. * Check range [vcn1 + zero).
  1889. * Calculate how many clusters there are.
  1890. * Don't do any destructive actions.
  1891. */
  1892. err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
  1893. if (err)
  1894. goto done;
  1895. /* Check if required range is already hole. */
  1896. if (hole2 == hole)
  1897. goto next_attr;
  1898. /* Make a clone of run to undo. */
  1899. err = run_clone(run, &run2);
  1900. if (err)
  1901. goto done;
  1902. /* Make a hole range (sparse) [vcn1 + zero). */
  1903. if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
  1904. err = -ENOMEM;
  1905. goto done;
  1906. }
  1907. /* Update run in attribute segment. */
  1908. err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
  1909. if (err)
  1910. goto done;
  1911. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  1912. if (next_svcn < evcn1) {
  1913. /* Insert new attribute segment. */
  1914. err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
  1915. next_svcn,
  1916. evcn1 - next_svcn, a_flags,
  1917. &attr, &mi, &le);
  1918. if (err)
  1919. goto undo_punch;
  1920. /* Layout of records maybe changed. */
  1921. attr_b = NULL;
  1922. }
  1923. /* Real deallocate. Should not fail. */
  1924. run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
  1925. next_attr:
  1926. /* Free all allocated memory. */
  1927. run_truncate(run, 0);
  1928. if (evcn1 >= alen)
  1929. break;
  1930. /* Get next attribute segment. */
  1931. attr = ni_enum_attr_ex(ni, attr, &le, &mi);
  1932. if (!attr) {
  1933. err = -EINVAL;
  1934. goto bad_inode;
  1935. }
  1936. svcn = le64_to_cpu(attr->nres.svcn);
  1937. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  1938. }
  1939. done:
  1940. if (!hole)
  1941. goto out;
  1942. if (!attr_b) {
  1943. attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
  1944. &mi_b);
  1945. if (!attr_b) {
  1946. err = -EINVAL;
  1947. goto bad_inode;
  1948. }
  1949. }
  1950. total_size -= (u64)hole << sbi->cluster_bits;
  1951. attr_b->nres.total_size = cpu_to_le64(total_size);
  1952. mi_b->dirty = true;
  1953. /* Update inode size. */
  1954. inode_set_bytes(&ni->vfs_inode, total_size);
  1955. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  1956. mark_inode_dirty(&ni->vfs_inode);
  1957. out:
  1958. run_close(&run2);
  1959. up_write(&ni->file.run_lock);
  1960. return err;
  1961. bad_inode:
  1962. _ntfs_bad_inode(&ni->vfs_inode);
  1963. goto out;
  1964. undo_punch:
  1965. /*
  1966. * Restore packed runs.
  1967. * 'mi_pack_runs' should not fail, cause we restore original.
  1968. */
  1969. if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
  1970. goto bad_inode;
  1971. goto done;
  1972. }
  1973. /*
  1974. * attr_insert_range - Insert range (hole) in file.
  1975. * Not for normal files.
  1976. */
  1977. int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
  1978. {
  1979. int err = 0;
  1980. struct runs_tree *run = &ni->file.run;
  1981. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1982. struct ATTRIB *attr = NULL, *attr_b;
  1983. struct ATTR_LIST_ENTRY *le, *le_b;
  1984. struct mft_inode *mi, *mi_b;
  1985. CLST vcn, svcn, evcn1, len, next_svcn;
  1986. u64 data_size, alloc_size;
  1987. u32 mask;
  1988. __le16 a_flags;
  1989. if (!bytes)
  1990. return 0;
  1991. le_b = NULL;
  1992. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
  1993. if (!attr_b)
  1994. return -ENOENT;
  1995. if (!is_attr_ext(attr_b)) {
  1996. /* It was checked above. See fallocate. */
  1997. return -EOPNOTSUPP;
  1998. }
  1999. if (!attr_b->non_res) {
  2000. data_size = le32_to_cpu(attr_b->res.data_size);
  2001. alloc_size = data_size;
  2002. mask = sbi->cluster_mask; /* cluster_size - 1 */
  2003. } else {
  2004. data_size = le64_to_cpu(attr_b->nres.data_size);
  2005. alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
  2006. mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
  2007. }
  2008. if (vbo >= data_size) {
  2009. /*
  2010. * Insert range after the file size is not allowed.
  2011. * If the offset is equal to or greater than the end of
  2012. * file, an error is returned. For such operations (i.e., inserting
  2013. * a hole at the end of file), ftruncate(2) should be used.
  2014. */
  2015. return -EINVAL;
  2016. }
  2017. if ((vbo & mask) || (bytes & mask)) {
  2018. /* Allow to insert only frame aligned ranges. */
  2019. return -EINVAL;
  2020. }
  2021. /*
  2022. * valid_size <= data_size <= alloc_size
  2023. * Check alloc_size for maximum possible.
  2024. */
  2025. if (bytes > sbi->maxbytes_sparse - alloc_size)
  2026. return -EFBIG;
  2027. vcn = vbo >> sbi->cluster_bits;
  2028. len = bytes >> sbi->cluster_bits;
  2029. down_write(&ni->file.run_lock);
  2030. if (!attr_b->non_res) {
  2031. err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
  2032. data_size + bytes, NULL, false, NULL);
  2033. le_b = NULL;
  2034. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
  2035. &mi_b);
  2036. if (!attr_b) {
  2037. err = -EINVAL;
  2038. goto bad_inode;
  2039. }
  2040. if (err)
  2041. goto out;
  2042. if (!attr_b->non_res) {
  2043. /* Still resident. */
  2044. char *data = Add2Ptr(attr_b,
  2045. le16_to_cpu(attr_b->res.data_off));
  2046. memmove(data + bytes, data, bytes);
  2047. memset(data, 0, bytes);
  2048. goto done;
  2049. }
  2050. /* Resident files becomes nonresident. */
  2051. data_size = le64_to_cpu(attr_b->nres.data_size);
  2052. alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
  2053. }
  2054. /*
  2055. * Enumerate all attribute segments and shift start vcn.
  2056. */
  2057. a_flags = attr_b->flags;
  2058. svcn = le64_to_cpu(attr_b->nres.svcn);
  2059. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  2060. if (svcn <= vcn && vcn < evcn1) {
  2061. attr = attr_b;
  2062. le = le_b;
  2063. mi = mi_b;
  2064. } else if (!le_b) {
  2065. err = -EINVAL;
  2066. goto bad_inode;
  2067. } else {
  2068. le = le_b;
  2069. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  2070. &mi);
  2071. if (!attr) {
  2072. err = -EINVAL;
  2073. goto bad_inode;
  2074. }
  2075. svcn = le64_to_cpu(attr->nres.svcn);
  2076. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  2077. }
  2078. run_truncate(run, 0); /* clear cached values. */
  2079. err = attr_load_runs(attr, ni, run, NULL);
  2080. if (err)
  2081. goto out;
  2082. if (!run_insert_range(run, vcn, len)) {
  2083. err = -ENOMEM;
  2084. goto out;
  2085. }
  2086. /* Try to pack in current record as much as possible. */
  2087. err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
  2088. if (err)
  2089. goto out;
  2090. next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
  2091. while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
  2092. attr->type == ATTR_DATA && !attr->name_len) {
  2093. le64_add_cpu(&attr->nres.svcn, len);
  2094. le64_add_cpu(&attr->nres.evcn, len);
  2095. if (le) {
  2096. le->vcn = attr->nres.svcn;
  2097. ni->attr_list.dirty = true;
  2098. }
  2099. mi->dirty = true;
  2100. }
  2101. if (next_svcn < evcn1 + len) {
  2102. err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
  2103. next_svcn, evcn1 + len - next_svcn,
  2104. a_flags, NULL, NULL, NULL);
  2105. le_b = NULL;
  2106. attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
  2107. &mi_b);
  2108. if (!attr_b) {
  2109. err = -EINVAL;
  2110. goto bad_inode;
  2111. }
  2112. if (err) {
  2113. /* ni_insert_nonresident failed. Try to undo. */
  2114. goto undo_insert_range;
  2115. }
  2116. }
  2117. /*
  2118. * Update primary attribute segment.
  2119. */
  2120. if (vbo <= ni->i_valid)
  2121. ni->i_valid += bytes;
  2122. attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
  2123. attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
  2124. /* ni->valid may be not equal valid_size (temporary). */
  2125. if (ni->i_valid > data_size + bytes)
  2126. attr_b->nres.valid_size = attr_b->nres.data_size;
  2127. else
  2128. attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
  2129. mi_b->dirty = true;
  2130. done:
  2131. i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes);
  2132. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  2133. mark_inode_dirty(&ni->vfs_inode);
  2134. out:
  2135. run_truncate(run, 0); /* clear cached values. */
  2136. up_write(&ni->file.run_lock);
  2137. return err;
  2138. bad_inode:
  2139. _ntfs_bad_inode(&ni->vfs_inode);
  2140. goto out;
  2141. undo_insert_range:
  2142. svcn = le64_to_cpu(attr_b->nres.svcn);
  2143. evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
  2144. if (svcn <= vcn && vcn < evcn1) {
  2145. attr = attr_b;
  2146. le = le_b;
  2147. mi = mi_b;
  2148. } else if (!le_b) {
  2149. goto bad_inode;
  2150. } else {
  2151. le = le_b;
  2152. attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
  2153. &mi);
  2154. if (!attr) {
  2155. goto bad_inode;
  2156. }
  2157. svcn = le64_to_cpu(attr->nres.svcn);
  2158. evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
  2159. }
  2160. if (attr_load_runs(attr, ni, run, NULL))
  2161. goto bad_inode;
  2162. if (!run_collapse_range(run, vcn, len))
  2163. goto bad_inode;
  2164. if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
  2165. goto bad_inode;
  2166. while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
  2167. attr->type == ATTR_DATA && !attr->name_len) {
  2168. le64_sub_cpu(&attr->nres.svcn, len);
  2169. le64_sub_cpu(&attr->nres.evcn, len);
  2170. if (le) {
  2171. le->vcn = attr->nres.svcn;
  2172. ni->attr_list.dirty = true;
  2173. }
  2174. mi->dirty = true;
  2175. }
  2176. goto out;
  2177. }
  2178. /*
  2179. * attr_force_nonresident
  2180. *
  2181. * Convert default data attribute into non resident form.
  2182. */
  2183. int attr_force_nonresident(struct ntfs_inode *ni)
  2184. {
  2185. int err;
  2186. struct ATTRIB *attr;
  2187. struct ATTR_LIST_ENTRY *le = NULL;
  2188. struct mft_inode *mi;
  2189. attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
  2190. if (!attr) {
  2191. _ntfs_bad_inode(&ni->vfs_inode);
  2192. return -ENOENT;
  2193. }
  2194. if (attr->non_res) {
  2195. /* Already non resident. */
  2196. return 0;
  2197. }
  2198. down_write(&ni->file.run_lock);
  2199. err = attr_make_nonresident(ni, attr, le, mi,
  2200. le32_to_cpu(attr->res.data_size),
  2201. &ni->file.run, &attr, NULL);
  2202. up_write(&ni->file.run_lock);
  2203. return err;
  2204. }
  2205. /*
  2206. * Change the compression of data attribute
  2207. */
  2208. int attr_set_compress(struct ntfs_inode *ni, bool compr)
  2209. {
  2210. struct ATTRIB *attr;
  2211. struct mft_inode *mi;
  2212. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
  2213. if (!attr)
  2214. return -ENOENT;
  2215. if (is_attr_compressed(attr) == !!compr) {
  2216. /* Already required compressed state. */
  2217. return 0;
  2218. }
  2219. if (attr->non_res) {
  2220. u16 run_off;
  2221. u32 run_size;
  2222. char *run;
  2223. if (attr->nres.data_size) {
  2224. /*
  2225. * There are rare cases when it possible to change
  2226. * compress state without big changes.
  2227. * TODO: Process these cases.
  2228. */
  2229. return -EOPNOTSUPP;
  2230. }
  2231. run_off = le16_to_cpu(attr->nres.run_off);
  2232. run_size = le32_to_cpu(attr->size) - run_off;
  2233. run = Add2Ptr(attr, run_off);
  2234. if (!compr) {
  2235. /* remove field 'attr->nres.total_size'. */
  2236. memmove(run - 8, run, run_size);
  2237. run_off -= 8;
  2238. }
  2239. if (!mi_resize_attr(mi, attr, compr ? +8 : -8)) {
  2240. /*
  2241. * Ignore rare case when there are no 8 bytes in record with attr.
  2242. * TODO: split attribute.
  2243. */
  2244. return -EOPNOTSUPP;
  2245. }
  2246. if (compr) {
  2247. /* Make a gap for 'attr->nres.total_size'. */
  2248. memmove(run + 8, run, run_size);
  2249. run_off += 8;
  2250. attr->nres.total_size = attr->nres.alloc_size;
  2251. }
  2252. attr->nres.run_off = cpu_to_le16(run_off);
  2253. }
  2254. /* Update attribute flags. */
  2255. if (compr) {
  2256. attr->flags &= ~ATTR_FLAG_SPARSED;
  2257. attr->flags |= ATTR_FLAG_COMPRESSED;
  2258. attr->nres.c_unit = NTFS_LZNT_CUNIT;
  2259. } else {
  2260. attr->flags &= ~ATTR_FLAG_COMPRESSED;
  2261. attr->nres.c_unit = 0;
  2262. }
  2263. mi->dirty = true;
  2264. return 0;
  2265. }