frecord.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. #include <linux/fiemap.h>
  8. #include <linux/fs.h>
  9. #include <linux/minmax.h>
  10. #include <linux/vmalloc.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. #ifdef CONFIG_NTFS3_LZX_XPRESS
  15. #include "lib/lib.h"
  16. #endif
  17. static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
  18. CLST ino, struct rb_node *ins)
  19. {
  20. struct rb_node **p = &tree->rb_node;
  21. struct rb_node *pr = NULL;
  22. while (*p) {
  23. struct mft_inode *mi;
  24. pr = *p;
  25. mi = rb_entry(pr, struct mft_inode, node);
  26. if (mi->rno > ino)
  27. p = &pr->rb_left;
  28. else if (mi->rno < ino)
  29. p = &pr->rb_right;
  30. else
  31. return mi;
  32. }
  33. if (!ins)
  34. return NULL;
  35. rb_link_node(ins, pr, p);
  36. rb_insert_color(ins, tree);
  37. return rb_entry(ins, struct mft_inode, node);
  38. }
  39. /*
  40. * ni_find_mi - Find mft_inode by record number.
  41. */
  42. static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
  43. {
  44. return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
  45. }
  46. /*
  47. * ni_add_mi - Add new mft_inode into ntfs_inode.
  48. */
  49. static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
  50. {
  51. ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
  52. }
  53. /*
  54. * ni_remove_mi - Remove mft_inode from ntfs_inode.
  55. */
  56. void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
  57. {
  58. rb_erase(&mi->node, &ni->mi_tree);
  59. }
  60. /*
  61. * ni_std - Return: Pointer into std_info from primary record.
  62. */
  63. struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
  64. {
  65. const struct ATTRIB *attr;
  66. attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
  67. return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO)) :
  68. NULL;
  69. }
  70. /*
  71. * ni_std5
  72. *
  73. * Return: Pointer into std_info from primary record.
  74. */
  75. struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
  76. {
  77. const struct ATTRIB *attr;
  78. attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
  79. return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5)) :
  80. NULL;
  81. }
  82. /*
  83. * ni_clear - Clear resources allocated by ntfs_inode.
  84. */
  85. void ni_clear(struct ntfs_inode *ni)
  86. {
  87. struct rb_node *node;
  88. if (!ni->vfs_inode.i_nlink && ni->mi.mrec &&
  89. is_rec_inuse(ni->mi.mrec) &&
  90. !(ni->mi.sbi->flags & NTFS_FLAGS_LOG_REPLAYING))
  91. ni_delete_all(ni);
  92. al_destroy(ni);
  93. for (node = rb_first(&ni->mi_tree); node;) {
  94. struct rb_node *next = rb_next(node);
  95. struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
  96. rb_erase(node, &ni->mi_tree);
  97. mi_put(mi);
  98. node = next;
  99. }
  100. /* Bad inode always has mode == S_IFREG. */
  101. if (ni->ni_flags & NI_FLAG_DIR)
  102. indx_clear(&ni->dir);
  103. else {
  104. run_close(&ni->file.run);
  105. ntfs_sub_da(ni->mi.sbi, run_len(&ni->file.run_da));
  106. run_close(&ni->file.run_da);
  107. #ifdef CONFIG_NTFS3_LZX_XPRESS
  108. if (ni->file.offs_folio) {
  109. /* On-demand allocated page for offsets. */
  110. folio_put(ni->file.offs_folio);
  111. ni->file.offs_folio = NULL;
  112. }
  113. #endif
  114. }
  115. mi_clear(&ni->mi);
  116. }
  117. /*
  118. * ni_load_mi_ex - Find mft_inode by record number.
  119. */
  120. int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
  121. {
  122. int err;
  123. struct mft_inode *r;
  124. r = ni_find_mi(ni, rno);
  125. if (r)
  126. goto out;
  127. err = mi_get(ni->mi.sbi, rno, &r);
  128. if (err) {
  129. _ntfs_bad_inode(&ni->vfs_inode);
  130. return err;
  131. }
  132. ni_add_mi(ni, r);
  133. out:
  134. if (mi)
  135. *mi = r;
  136. return 0;
  137. }
  138. /*
  139. * ni_load_mi - Load mft_inode corresponded list_entry.
  140. */
  141. int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
  142. struct mft_inode **mi)
  143. {
  144. CLST rno;
  145. if (!le) {
  146. *mi = &ni->mi;
  147. return 0;
  148. }
  149. rno = ino_get(&le->ref);
  150. if (rno == ni->mi.rno) {
  151. *mi = &ni->mi;
  152. return 0;
  153. }
  154. return ni_load_mi_ex(ni, rno, mi);
  155. }
  156. /*
  157. * ni_find_attr
  158. *
  159. * Return: Attribute and record this attribute belongs to.
  160. */
  161. struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
  162. struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
  163. const __le16 *name, u8 name_len, const CLST *vcn,
  164. struct mft_inode **mi)
  165. {
  166. struct ATTR_LIST_ENTRY *le;
  167. struct mft_inode *m;
  168. if (!ni->attr_list.size ||
  169. (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
  170. if (le_o)
  171. *le_o = NULL;
  172. if (mi)
  173. *mi = &ni->mi;
  174. /* Look for required attribute in primary record. */
  175. return mi_find_attr(ni, &ni->mi, attr, type, name, name_len,
  176. NULL);
  177. }
  178. /* First look for list entry of required type. */
  179. le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
  180. if (!le)
  181. return NULL;
  182. if (le_o)
  183. *le_o = le;
  184. /* Load record that contains this attribute. */
  185. if (ni_load_mi(ni, le, &m))
  186. return NULL;
  187. /* Look for required attribute. */
  188. attr = mi_find_attr(ni, m, NULL, type, name, name_len, &le->id);
  189. if (!attr)
  190. goto out;
  191. if (!attr->non_res) {
  192. if (vcn && *vcn)
  193. goto out;
  194. } else if (!vcn) {
  195. if (attr->nres.svcn)
  196. goto out;
  197. } else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
  198. *vcn > le64_to_cpu(attr->nres.evcn)) {
  199. goto out;
  200. }
  201. if (mi)
  202. *mi = m;
  203. return attr;
  204. out:
  205. _ntfs_bad_inode(&ni->vfs_inode);
  206. return NULL;
  207. }
  208. /*
  209. * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
  210. */
  211. struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
  212. struct ATTR_LIST_ENTRY **le,
  213. struct mft_inode **mi)
  214. {
  215. struct mft_inode *mi2;
  216. struct ATTR_LIST_ENTRY *le2;
  217. /* Do we have an attribute list? */
  218. if (!ni->attr_list.size) {
  219. *le = NULL;
  220. if (mi)
  221. *mi = &ni->mi;
  222. /* Enum attributes in primary record. */
  223. return mi_enum_attr(ni, &ni->mi, attr);
  224. }
  225. /* Get next list entry. */
  226. le2 = *le = al_enumerate(ni, attr ? *le : NULL);
  227. if (!le2)
  228. return NULL;
  229. /* Load record that contains the required attribute. */
  230. if (ni_load_mi(ni, le2, &mi2))
  231. return NULL;
  232. if (mi)
  233. *mi = mi2;
  234. /* Find attribute in loaded record. */
  235. return rec_find_attr_le(ni, mi2, le2);
  236. }
  237. /*
  238. * ni_load_all_mi - Load all subrecords.
  239. */
  240. int ni_load_all_mi(struct ntfs_inode *ni)
  241. {
  242. int err;
  243. struct ATTR_LIST_ENTRY *le;
  244. if (!ni->attr_list.size)
  245. return 0;
  246. le = NULL;
  247. while ((le = al_enumerate(ni, le))) {
  248. CLST rno = ino_get(&le->ref);
  249. if (rno == ni->mi.rno)
  250. continue;
  251. err = ni_load_mi_ex(ni, rno, NULL);
  252. if (err)
  253. return err;
  254. }
  255. return 0;
  256. }
  257. /*
  258. * ni_add_subrecord - Allocate + format + attach a new subrecord.
  259. */
  260. bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
  261. {
  262. struct mft_inode *m;
  263. m = kzalloc_obj(struct mft_inode, GFP_NOFS);
  264. if (!m)
  265. return false;
  266. if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
  267. mi_put(m);
  268. return false;
  269. }
  270. mi_get_ref(&ni->mi, &m->mrec->parent_ref);
  271. *mi = ni_ins_mi(ni, &ni->mi_tree, m->rno, &m->node);
  272. if (*mi != m)
  273. mi_put(m);
  274. return true;
  275. }
  276. /*
  277. * ni_remove_attr - Remove all attributes for the given type/name/id.
  278. */
  279. int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
  280. const __le16 *name, u8 name_len, bool base_only,
  281. const __le16 *id)
  282. {
  283. int err;
  284. struct ATTRIB *attr;
  285. struct ATTR_LIST_ENTRY *le;
  286. struct mft_inode *mi;
  287. u32 type_in;
  288. int diff;
  289. if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
  290. attr = mi_find_attr(ni, &ni->mi, NULL, type, name, name_len,
  291. id);
  292. if (!attr)
  293. return -ENOENT;
  294. mi_remove_attr(ni, &ni->mi, attr);
  295. return 0;
  296. }
  297. type_in = le32_to_cpu(type);
  298. le = NULL;
  299. for (;;) {
  300. le = al_enumerate(ni, le);
  301. if (!le)
  302. return 0;
  303. next_le2:
  304. diff = le32_to_cpu(le->type) - type_in;
  305. if (diff < 0)
  306. continue;
  307. if (diff > 0)
  308. return 0;
  309. if (le->name_len != name_len)
  310. continue;
  311. if (name_len &&
  312. memcmp(le_name(le), name, name_len * sizeof(short)))
  313. continue;
  314. if (id && le->id != *id)
  315. continue;
  316. err = ni_load_mi(ni, le, &mi);
  317. if (err)
  318. return err;
  319. al_remove_le(ni, le);
  320. attr = mi_find_attr(ni, mi, NULL, type, name, name_len, id);
  321. if (!attr)
  322. return -ENOENT;
  323. mi_remove_attr(ni, mi, attr);
  324. if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
  325. return 0;
  326. goto next_le2;
  327. }
  328. }
  329. /*
  330. * ni_ins_new_attr - Insert the attribute into record.
  331. *
  332. * Return: Not full constructed attribute or NULL if not possible to create.
  333. */
  334. static struct ATTRIB *
  335. ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
  336. struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
  337. const __le16 *name, u8 name_len, u32 asize, u16 name_off,
  338. CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
  339. {
  340. int err;
  341. struct ATTRIB *attr;
  342. bool le_added = false;
  343. struct MFT_REF ref;
  344. mi_get_ref(mi, &ref);
  345. if (type != ATTR_LIST && !le && ni->attr_list.size) {
  346. err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
  347. &ref, &le);
  348. if (err) {
  349. /* No memory or no space. */
  350. return ERR_PTR(err);
  351. }
  352. le_added = true;
  353. /*
  354. * al_add_le -> attr_set_size (list) -> ni_expand_list
  355. * which moves some attributes out of primary record
  356. * this means that name may point into moved memory
  357. * reinit 'name' from le.
  358. */
  359. name = le->name;
  360. }
  361. attr = mi_insert_attr(ni, mi, type, name, name_len, asize, name_off);
  362. if (!attr) {
  363. if (le_added)
  364. al_remove_le(ni, le);
  365. return NULL;
  366. }
  367. if (type == ATTR_LIST) {
  368. /* Attr list is not in list entry array. */
  369. goto out;
  370. }
  371. if (!le)
  372. goto out;
  373. /* Update ATTRIB Id and record reference. */
  374. le->id = attr->id;
  375. ni->attr_list.dirty = true;
  376. le->ref = ref;
  377. out:
  378. if (ins_le)
  379. *ins_le = le;
  380. return attr;
  381. }
  382. /*
  383. * ni_repack
  384. *
  385. * Random write access to sparsed or compressed file may result to
  386. * not optimized packed runs.
  387. * Here is the place to optimize it.
  388. */
  389. static int ni_repack(struct ntfs_inode *ni)
  390. {
  391. #if 1
  392. return 0;
  393. #else
  394. int err = 0;
  395. struct ntfs_sb_info *sbi = ni->mi.sbi;
  396. struct mft_inode *mi, *mi_p = NULL;
  397. struct ATTRIB *attr = NULL, *attr_p;
  398. struct ATTR_LIST_ENTRY *le = NULL, *le_p;
  399. CLST alloc = 0;
  400. u8 cluster_bits = sbi->cluster_bits;
  401. CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
  402. u32 roff, rs = sbi->record_size;
  403. struct runs_tree run;
  404. run_init(&run);
  405. while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
  406. if (!attr->non_res)
  407. continue;
  408. svcn = le64_to_cpu(attr->nres.svcn);
  409. if (svcn != le64_to_cpu(le->vcn)) {
  410. err = -EINVAL;
  411. break;
  412. }
  413. if (!svcn) {
  414. alloc = le64_to_cpu(attr->nres.alloc_size) >>
  415. cluster_bits;
  416. mi_p = NULL;
  417. } else if (svcn != evcn + 1) {
  418. err = -EINVAL;
  419. break;
  420. }
  421. evcn = le64_to_cpu(attr->nres.evcn);
  422. if (svcn > evcn + 1) {
  423. err = -EINVAL;
  424. break;
  425. }
  426. if (!mi_p) {
  427. /* Do not try if not enough free space. */
  428. if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
  429. continue;
  430. /* Do not try if last attribute segment. */
  431. if (evcn + 1 == alloc)
  432. continue;
  433. run_close(&run);
  434. }
  435. roff = le16_to_cpu(attr->nres.run_off);
  436. if (roff > le32_to_cpu(attr->size)) {
  437. err = -EINVAL;
  438. break;
  439. }
  440. err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
  441. Add2Ptr(attr, roff),
  442. le32_to_cpu(attr->size) - roff);
  443. if (err < 0)
  444. break;
  445. if (!mi_p) {
  446. mi_p = mi;
  447. attr_p = attr;
  448. svcn_p = svcn;
  449. evcn_p = evcn;
  450. le_p = le;
  451. err = 0;
  452. continue;
  453. }
  454. /*
  455. * Run contains data from two records: mi_p and mi
  456. * Try to pack in one.
  457. */
  458. err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
  459. if (err)
  460. break;
  461. next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
  462. if (next_svcn >= evcn + 1) {
  463. /* We can remove this attribute segment. */
  464. al_remove_le(ni, le);
  465. mi_remove_attr(NULL, mi, attr);
  466. le = le_p;
  467. continue;
  468. }
  469. attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
  470. mi->dirty = true;
  471. ni->attr_list.dirty = true;
  472. if (evcn + 1 == alloc) {
  473. err = mi_pack_runs(mi, attr, &run,
  474. evcn + 1 - next_svcn);
  475. if (err)
  476. break;
  477. mi_p = NULL;
  478. } else {
  479. mi_p = mi;
  480. attr_p = attr;
  481. svcn_p = next_svcn;
  482. evcn_p = evcn;
  483. le_p = le;
  484. run_truncate_head(&run, next_svcn);
  485. }
  486. }
  487. if (err) {
  488. ntfs_inode_warn(&ni->vfs_inode, "repack problem");
  489. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  490. /* Pack loaded but not packed runs. */
  491. if (mi_p)
  492. mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
  493. }
  494. run_close(&run);
  495. return err;
  496. #endif
  497. }
  498. /*
  499. * ni_try_remove_attr_list
  500. *
  501. * Can we remove attribute list?
  502. * Check the case when primary record contains enough space for all attributes.
  503. */
  504. static int ni_try_remove_attr_list(struct ntfs_inode *ni)
  505. {
  506. int err = 0;
  507. struct ntfs_sb_info *sbi = ni->mi.sbi;
  508. struct ATTRIB *attr, *attr_list, *attr_ins;
  509. struct ATTR_LIST_ENTRY *le;
  510. struct mft_inode *mi;
  511. u32 asize, free;
  512. struct MFT_REF ref;
  513. struct MFT_REC *mrec;
  514. __le16 id;
  515. if (!ni->attr_list.dirty)
  516. return 0;
  517. err = ni_repack(ni);
  518. if (err)
  519. return err;
  520. attr_list = mi_find_attr(ni, &ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
  521. if (!attr_list)
  522. return 0;
  523. asize = le32_to_cpu(attr_list->size);
  524. /* Free space in primary record without attribute list. */
  525. free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
  526. mi_get_ref(&ni->mi, &ref);
  527. le = NULL;
  528. while ((le = al_enumerate(ni, le))) {
  529. if (!memcmp(&le->ref, &ref, sizeof(ref)))
  530. continue;
  531. if (le->vcn)
  532. return 0;
  533. mi = ni_find_mi(ni, ino_get(&le->ref));
  534. if (!mi)
  535. return 0;
  536. attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
  537. le->name_len, &le->id);
  538. if (!attr)
  539. return 0;
  540. asize = le32_to_cpu(attr->size);
  541. if (asize > free)
  542. return 0;
  543. free -= asize;
  544. }
  545. /* Make a copy of primary record to restore if error. */
  546. mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
  547. if (!mrec)
  548. return 0; /* Not critical. */
  549. /* It seems that attribute list can be removed from primary record. */
  550. mi_remove_attr(NULL, &ni->mi, attr_list);
  551. /*
  552. * Repeat the cycle above and copy all attributes to primary record.
  553. * Do not remove original attributes from subrecords!
  554. * It should be success!
  555. */
  556. le = NULL;
  557. while ((le = al_enumerate(ni, le))) {
  558. if (!memcmp(&le->ref, &ref, sizeof(ref)))
  559. continue;
  560. mi = ni_find_mi(ni, ino_get(&le->ref));
  561. if (!mi) {
  562. /* Should never happened, 'cause already checked. */
  563. goto out;
  564. }
  565. attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
  566. le->name_len, &le->id);
  567. if (!attr) {
  568. /* Should never happened, 'cause already checked. */
  569. goto out;
  570. }
  571. asize = le32_to_cpu(attr->size);
  572. /* Insert into primary record. */
  573. attr_ins = mi_insert_attr(ni, &ni->mi, le->type, le_name(le),
  574. le->name_len, asize,
  575. le16_to_cpu(attr->name_off));
  576. if (!attr_ins) {
  577. /*
  578. * No space in primary record (already checked).
  579. */
  580. goto out;
  581. }
  582. /* Copy all except id. */
  583. id = attr_ins->id;
  584. memcpy(attr_ins, attr, asize);
  585. attr_ins->id = id;
  586. }
  587. /*
  588. * Repeat the cycle above and remove all attributes from subrecords.
  589. */
  590. le = NULL;
  591. while ((le = al_enumerate(ni, le))) {
  592. if (!memcmp(&le->ref, &ref, sizeof(ref)))
  593. continue;
  594. mi = ni_find_mi(ni, ino_get(&le->ref));
  595. if (!mi)
  596. continue;
  597. attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
  598. le->name_len, &le->id);
  599. if (!attr)
  600. continue;
  601. /* Remove from original record. */
  602. mi_remove_attr(NULL, mi, attr);
  603. }
  604. run_deallocate(sbi, &ni->attr_list.run, true);
  605. run_close(&ni->attr_list.run);
  606. ni->attr_list.size = 0;
  607. kvfree(ni->attr_list.le);
  608. ni->attr_list.le = NULL;
  609. ni->attr_list.dirty = false;
  610. kfree(mrec);
  611. return 0;
  612. out:
  613. /* Restore primary record. */
  614. swap(mrec, ni->mi.mrec);
  615. kfree(mrec);
  616. return 0;
  617. }
  618. /*
  619. * ni_create_attr_list - Generates an attribute list for this primary record.
  620. */
  621. int ni_create_attr_list(struct ntfs_inode *ni)
  622. {
  623. struct ntfs_sb_info *sbi = ni->mi.sbi;
  624. int err;
  625. u32 lsize;
  626. struct ATTRIB *attr;
  627. struct ATTRIB *arr_move[7];
  628. struct ATTR_LIST_ENTRY *le, *le_b[7];
  629. struct MFT_REC *rec;
  630. bool is_mft;
  631. CLST rno = 0;
  632. struct mft_inode *mi;
  633. u32 free_b, nb, to_free, rs;
  634. u16 sz;
  635. is_mft = ni->mi.rno == MFT_REC_MFT;
  636. rec = ni->mi.mrec;
  637. rs = sbi->record_size;
  638. /*
  639. * Skip estimating exact memory requirement.
  640. * Looks like one record_size is always enough.
  641. */
  642. le = kzalloc(al_aligned(rs), GFP_NOFS);
  643. if (!le)
  644. return -ENOMEM;
  645. mi_get_ref(&ni->mi, &le->ref);
  646. ni->attr_list.le = le;
  647. attr = NULL;
  648. nb = 0;
  649. free_b = 0;
  650. attr = NULL;
  651. for (; (attr = mi_enum_attr(ni, &ni->mi, attr)); le = Add2Ptr(le, sz)) {
  652. sz = le_size(attr->name_len);
  653. le->type = attr->type;
  654. le->size = cpu_to_le16(sz);
  655. le->name_len = attr->name_len;
  656. le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
  657. le->vcn = 0;
  658. if (le != ni->attr_list.le)
  659. le->ref = ni->attr_list.le->ref;
  660. le->id = attr->id;
  661. if (attr->name_len)
  662. memcpy(le->name, attr_name(attr),
  663. sizeof(short) * attr->name_len);
  664. else if (attr->type == ATTR_STD)
  665. continue;
  666. else if (attr->type == ATTR_LIST)
  667. continue;
  668. else if (is_mft && attr->type == ATTR_DATA)
  669. continue;
  670. if (!nb || nb < ARRAY_SIZE(arr_move)) {
  671. le_b[nb] = le;
  672. arr_move[nb++] = attr;
  673. free_b += le32_to_cpu(attr->size);
  674. }
  675. }
  676. lsize = PtrOffset(ni->attr_list.le, le);
  677. ni->attr_list.size = lsize;
  678. to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
  679. if (to_free <= rs) {
  680. to_free = 0;
  681. } else {
  682. to_free -= rs;
  683. if (to_free > free_b) {
  684. err = -EINVAL;
  685. goto out;
  686. }
  687. }
  688. /* Allocate child MFT. */
  689. err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
  690. if (err)
  691. goto out;
  692. err = -EINVAL;
  693. /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
  694. while (to_free > 0) {
  695. struct ATTRIB *b = arr_move[--nb];
  696. u32 asize = le32_to_cpu(b->size);
  697. u16 name_off = le16_to_cpu(b->name_off);
  698. attr = mi_insert_attr(ni, mi, b->type, Add2Ptr(b, name_off),
  699. b->name_len, asize, name_off);
  700. if (!attr)
  701. goto out;
  702. mi_get_ref(mi, &le_b[nb]->ref);
  703. le_b[nb]->id = attr->id;
  704. /* Copy all except id. */
  705. memcpy(attr, b, asize);
  706. attr->id = le_b[nb]->id;
  707. /* Remove from primary record. */
  708. if (!mi_remove_attr(NULL, &ni->mi, b))
  709. goto out;
  710. if (to_free <= asize)
  711. break;
  712. to_free -= asize;
  713. if (!nb)
  714. goto out;
  715. }
  716. attr = mi_insert_attr(ni, &ni->mi, ATTR_LIST, NULL, 0,
  717. lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
  718. if (!attr)
  719. goto out;
  720. attr->non_res = 0;
  721. attr->flags = 0;
  722. attr->res.data_size = cpu_to_le32(lsize);
  723. attr->res.data_off = SIZEOF_RESIDENT_LE;
  724. attr->res.flags = 0;
  725. attr->res.res = 0;
  726. memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
  727. ni->attr_list.dirty = false;
  728. mark_inode_dirty(&ni->vfs_inode);
  729. return 0;
  730. out:
  731. kvfree(ni->attr_list.le);
  732. ni->attr_list.le = NULL;
  733. ni->attr_list.size = 0;
  734. return err;
  735. }
  736. /*
  737. * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
  738. */
  739. static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
  740. enum ATTR_TYPE type, const __le16 *name, u8 name_len,
  741. u32 asize, CLST svcn, u16 name_off, bool force_ext,
  742. struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
  743. struct ATTR_LIST_ENTRY **ins_le)
  744. {
  745. struct ATTRIB *attr;
  746. struct mft_inode *mi;
  747. CLST rno;
  748. u64 vbo;
  749. struct rb_node *node;
  750. int err;
  751. bool is_mft, is_mft_data;
  752. struct ntfs_sb_info *sbi = ni->mi.sbi;
  753. is_mft = ni->mi.rno == MFT_REC_MFT;
  754. is_mft_data = is_mft && type == ATTR_DATA && !name_len;
  755. if (asize > sbi->max_bytes_per_attr) {
  756. err = -EINVAL;
  757. goto out;
  758. }
  759. /*
  760. * Standard information and attr_list cannot be made external.
  761. * The Log File cannot have any external attributes.
  762. */
  763. if (type == ATTR_STD || type == ATTR_LIST ||
  764. ni->mi.rno == MFT_REC_LOG) {
  765. err = -EINVAL;
  766. goto out;
  767. }
  768. /* Create attribute list if it is not already existed. */
  769. if (!ni->attr_list.size) {
  770. err = ni_create_attr_list(ni);
  771. if (err)
  772. goto out;
  773. }
  774. vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
  775. if (force_ext)
  776. goto insert_ext;
  777. /* Load all subrecords into memory. */
  778. err = ni_load_all_mi(ni);
  779. if (err)
  780. goto out;
  781. /* Check each of loaded subrecord. */
  782. for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
  783. mi = rb_entry(node, struct mft_inode, node);
  784. if (is_mft_data &&
  785. (mi_enum_attr(ni, mi, NULL) ||
  786. vbo <= ((u64)mi->rno << sbi->record_bits))) {
  787. /* We can't accept this record 'cause MFT's bootstrapping. */
  788. continue;
  789. }
  790. if (is_mft &&
  791. mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
  792. /*
  793. * This child record already has a ATTR_DATA.
  794. * So it can't accept any other records.
  795. */
  796. continue;
  797. }
  798. if ((type != ATTR_NAME || name_len) &&
  799. mi_find_attr(ni, mi, NULL, type, name, name_len, NULL)) {
  800. /* Only indexed attributes can share same record. */
  801. continue;
  802. }
  803. /*
  804. * Do not try to insert this attribute
  805. * if there is no room in record.
  806. */
  807. if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
  808. continue;
  809. /* Try to insert attribute into this subrecord. */
  810. attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
  811. name_off, svcn, ins_le);
  812. if (!attr)
  813. continue;
  814. if (IS_ERR(attr))
  815. return PTR_ERR(attr);
  816. if (ins_attr)
  817. *ins_attr = attr;
  818. if (ins_mi)
  819. *ins_mi = mi;
  820. return 0;
  821. }
  822. insert_ext:
  823. /* We have to allocate a new child subrecord. */
  824. err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
  825. if (err)
  826. goto out;
  827. if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
  828. err = -EINVAL;
  829. goto out1;
  830. }
  831. attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
  832. name_off, svcn, ins_le);
  833. if (!attr) {
  834. err = -EINVAL;
  835. goto out2;
  836. }
  837. if (IS_ERR(attr)) {
  838. err = PTR_ERR(attr);
  839. goto out2;
  840. }
  841. if (ins_attr)
  842. *ins_attr = attr;
  843. if (ins_mi)
  844. *ins_mi = mi;
  845. return 0;
  846. out2:
  847. ni_remove_mi(ni, mi);
  848. out1:
  849. mi_put(mi);
  850. ntfs_mark_rec_free(sbi, rno, is_mft);
  851. out:
  852. return err;
  853. }
  854. /*
  855. * ni_insert_attr - Insert an attribute into the file.
  856. *
  857. * If the primary record has room, it will just insert the attribute.
  858. * If not, it may make the attribute external.
  859. * For $MFT::Data it may make room for the attribute by
  860. * making other attributes external.
  861. *
  862. * NOTE:
  863. * The ATTR_LIST and ATTR_STD cannot be made external.
  864. * This function does not fill new attribute full.
  865. * It only fills 'size'/'type'/'id'/'name_len' fields.
  866. */
  867. static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
  868. const __le16 *name, u8 name_len, u32 asize,
  869. u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
  870. struct mft_inode **ins_mi,
  871. struct ATTR_LIST_ENTRY **ins_le)
  872. {
  873. struct ntfs_sb_info *sbi = ni->mi.sbi;
  874. int err;
  875. struct ATTRIB *attr, *eattr;
  876. struct MFT_REC *rec;
  877. bool is_mft;
  878. struct ATTR_LIST_ENTRY *le;
  879. u32 list_reserve, max_free, free, used, t32;
  880. __le16 id;
  881. u16 t16;
  882. is_mft = ni->mi.rno == MFT_REC_MFT;
  883. rec = ni->mi.mrec;
  884. list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
  885. used = le32_to_cpu(rec->used);
  886. free = sbi->record_size - used;
  887. if (is_mft && type != ATTR_LIST) {
  888. /* Reserve space for the ATTRIB list. */
  889. if (free < list_reserve)
  890. free = 0;
  891. else
  892. free -= list_reserve;
  893. }
  894. if (asize <= free) {
  895. attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
  896. asize, name_off, svcn, ins_le);
  897. if (IS_ERR(attr)) {
  898. err = PTR_ERR(attr);
  899. goto out;
  900. }
  901. if (attr) {
  902. if (ins_attr)
  903. *ins_attr = attr;
  904. if (ins_mi)
  905. *ins_mi = &ni->mi;
  906. err = 0;
  907. goto out;
  908. }
  909. }
  910. if (!is_mft || type != ATTR_DATA || svcn) {
  911. /* This ATTRIB will be external. */
  912. err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
  913. svcn, name_off, false, ins_attr, ins_mi,
  914. ins_le);
  915. goto out;
  916. }
  917. /*
  918. * Here we have: "is_mft && type == ATTR_DATA && !svcn"
  919. *
  920. * The first chunk of the $MFT::Data ATTRIB must be the base record.
  921. * Evict as many other attributes as possible.
  922. */
  923. max_free = free;
  924. /* Estimate the result of moving all possible attributes away. */
  925. attr = NULL;
  926. while ((attr = mi_enum_attr(ni, &ni->mi, attr))) {
  927. if (attr->type == ATTR_STD)
  928. continue;
  929. if (attr->type == ATTR_LIST)
  930. continue;
  931. max_free += le32_to_cpu(attr->size);
  932. }
  933. if (max_free < asize + list_reserve) {
  934. /* Impossible to insert this attribute into primary record. */
  935. err = -EINVAL;
  936. goto out;
  937. }
  938. /* Start real attribute moving. */
  939. attr = NULL;
  940. for (;;) {
  941. attr = mi_enum_attr(ni, &ni->mi, attr);
  942. if (!attr) {
  943. /* We should never be here 'cause we have already check this case. */
  944. err = -EINVAL;
  945. goto out;
  946. }
  947. /* Skip attributes that MUST be primary record. */
  948. if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
  949. continue;
  950. le = NULL;
  951. if (ni->attr_list.size) {
  952. le = al_find_le(ni, NULL, attr);
  953. if (!le) {
  954. /* Really this is a serious bug. */
  955. err = -EINVAL;
  956. goto out;
  957. }
  958. }
  959. t32 = le32_to_cpu(attr->size);
  960. t16 = le16_to_cpu(attr->name_off);
  961. err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
  962. attr->name_len, t32, attr_svcn(attr), t16,
  963. false, &eattr, NULL, NULL);
  964. if (err)
  965. return err;
  966. id = eattr->id;
  967. memcpy(eattr, attr, t32);
  968. eattr->id = id;
  969. /* Remove from primary record. */
  970. mi_remove_attr(NULL, &ni->mi, attr);
  971. /* attr now points to next attribute. */
  972. if (attr->type == ATTR_END)
  973. goto out;
  974. }
  975. while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
  976. ;
  977. attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
  978. name_off, svcn, ins_le);
  979. if (!attr) {
  980. err = -EINVAL;
  981. goto out;
  982. }
  983. if (IS_ERR(attr)) {
  984. err = PTR_ERR(attr);
  985. goto out;
  986. }
  987. if (ins_attr)
  988. *ins_attr = attr;
  989. if (ins_mi)
  990. *ins_mi = &ni->mi;
  991. out:
  992. return err;
  993. }
  994. /* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
  995. static int ni_expand_mft_list(struct ntfs_inode *ni)
  996. {
  997. int err = 0;
  998. struct runs_tree *run = &ni->file.run;
  999. u32 asize, run_size, done = 0;
  1000. struct ATTRIB *attr;
  1001. struct rb_node *node;
  1002. CLST mft_min, mft_new, svcn, evcn, plen;
  1003. struct mft_inode *mi, *mi_min, *mi_new;
  1004. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1005. /* Find the nearest MFT. */
  1006. mft_min = 0;
  1007. mft_new = 0;
  1008. mi_min = NULL;
  1009. for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
  1010. mi = rb_entry(node, struct mft_inode, node);
  1011. attr = mi_enum_attr(ni, mi, NULL);
  1012. if (!attr) {
  1013. mft_min = mi->rno;
  1014. mi_min = mi;
  1015. break;
  1016. }
  1017. }
  1018. if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
  1019. mft_new = 0;
  1020. /* Really this is not critical. */
  1021. } else if (mft_min > mft_new) {
  1022. mft_min = mft_new;
  1023. mi_min = mi_new;
  1024. } else {
  1025. ntfs_mark_rec_free(sbi, mft_new, true);
  1026. mft_new = 0;
  1027. ni_remove_mi(ni, mi_new);
  1028. }
  1029. attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
  1030. if (!attr) {
  1031. err = -EINVAL;
  1032. goto out;
  1033. }
  1034. asize = le32_to_cpu(attr->size);
  1035. evcn = le64_to_cpu(attr->nres.evcn);
  1036. svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
  1037. if (evcn + 1 >= svcn) {
  1038. err = -EINVAL;
  1039. goto out;
  1040. }
  1041. /*
  1042. * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
  1043. *
  1044. * Update first part of ATTR_DATA in 'primary MFT.
  1045. */
  1046. err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
  1047. asize - SIZEOF_NONRESIDENT, &plen);
  1048. if (err < 0)
  1049. goto out;
  1050. run_size = ALIGN(err, 8);
  1051. err = 0;
  1052. if (plen < svcn) {
  1053. err = -EINVAL;
  1054. goto out;
  1055. }
  1056. attr->nres.evcn = cpu_to_le64(svcn - 1);
  1057. attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
  1058. /* 'done' - How many bytes of primary MFT becomes free. */
  1059. done = asize - run_size - SIZEOF_NONRESIDENT;
  1060. le32_sub_cpu(&ni->mi.mrec->used, done);
  1061. /* Estimate packed size (run_buf=NULL). */
  1062. err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
  1063. &plen);
  1064. if (err < 0)
  1065. goto out;
  1066. run_size = ALIGN(err, 8);
  1067. err = 0;
  1068. if (plen < evcn + 1 - svcn) {
  1069. err = -EINVAL;
  1070. goto out;
  1071. }
  1072. /*
  1073. * This function may implicitly call expand attr_list.
  1074. * Insert second part of ATTR_DATA in 'mi_min'.
  1075. */
  1076. attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
  1077. SIZEOF_NONRESIDENT + run_size,
  1078. SIZEOF_NONRESIDENT, svcn, NULL);
  1079. if (!attr) {
  1080. err = -EINVAL;
  1081. goto out;
  1082. }
  1083. if (IS_ERR(attr)) {
  1084. err = PTR_ERR(attr);
  1085. goto out;
  1086. }
  1087. attr->non_res = 1;
  1088. attr->name_off = SIZEOF_NONRESIDENT_LE;
  1089. attr->flags = 0;
  1090. /* This function can't fail - cause already checked above. */
  1091. run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
  1092. run_size, &plen);
  1093. attr->nres.svcn = cpu_to_le64(svcn);
  1094. attr->nres.evcn = cpu_to_le64(evcn);
  1095. attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
  1096. out:
  1097. if (mft_new) {
  1098. ntfs_mark_rec_free(sbi, mft_new, true);
  1099. ni_remove_mi(ni, mi_new);
  1100. }
  1101. return !err && !done ? -EOPNOTSUPP : err;
  1102. }
  1103. /*
  1104. * ni_expand_list - Move all possible attributes out of primary record.
  1105. */
  1106. int ni_expand_list(struct ntfs_inode *ni)
  1107. {
  1108. int err = 0;
  1109. u32 asize, done = 0;
  1110. struct ATTRIB *attr, *ins_attr;
  1111. struct ATTR_LIST_ENTRY *le;
  1112. bool is_mft = ni->mi.rno == MFT_REC_MFT;
  1113. struct MFT_REF ref;
  1114. mi_get_ref(&ni->mi, &ref);
  1115. le = NULL;
  1116. while ((le = al_enumerate(ni, le))) {
  1117. if (le->type == ATTR_STD)
  1118. continue;
  1119. if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
  1120. continue;
  1121. if (is_mft && le->type == ATTR_DATA)
  1122. continue;
  1123. /* Find attribute in primary record. */
  1124. attr = rec_find_attr_le(ni, &ni->mi, le);
  1125. if (!attr) {
  1126. err = -EINVAL;
  1127. goto out;
  1128. }
  1129. asize = le32_to_cpu(attr->size);
  1130. /* Always insert into new record to avoid collisions (deep recursive). */
  1131. err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
  1132. attr->name_len, asize, attr_svcn(attr),
  1133. le16_to_cpu(attr->name_off), true,
  1134. &ins_attr, NULL, NULL);
  1135. if (err)
  1136. goto out;
  1137. memcpy(ins_attr, attr, asize);
  1138. ins_attr->id = le->id;
  1139. /* Remove from primary record. */
  1140. mi_remove_attr(NULL, &ni->mi, attr);
  1141. done += asize;
  1142. goto out;
  1143. }
  1144. if (!is_mft) {
  1145. err = -EFBIG; /* Attr list is too big(?) */
  1146. goto out;
  1147. }
  1148. /* Split MFT data as much as possible. */
  1149. err = ni_expand_mft_list(ni);
  1150. out:
  1151. return !err && !done ? -EOPNOTSUPP : err;
  1152. }
  1153. /*
  1154. * ni_insert_nonresident - Insert new nonresident attribute.
  1155. */
  1156. int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
  1157. const __le16 *name, u8 name_len,
  1158. const struct runs_tree *run, CLST svcn, CLST len,
  1159. __le16 flags, struct ATTRIB **new_attr,
  1160. struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
  1161. {
  1162. int err;
  1163. CLST plen;
  1164. struct ATTRIB *attr;
  1165. bool is_ext = (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) &&
  1166. !svcn;
  1167. u32 name_size = ALIGN(name_len * sizeof(short), 8);
  1168. u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
  1169. u32 run_off = name_off + name_size;
  1170. u32 run_size, asize;
  1171. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1172. /* Estimate packed size (run_buf=NULL). */
  1173. err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
  1174. &plen);
  1175. if (err < 0)
  1176. goto out;
  1177. run_size = ALIGN(err, 8);
  1178. if (plen < len) {
  1179. err = -EINVAL;
  1180. goto out;
  1181. }
  1182. asize = run_off + run_size;
  1183. if (asize > sbi->max_bytes_per_attr) {
  1184. err = -EINVAL;
  1185. goto out;
  1186. }
  1187. err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
  1188. &attr, mi, le);
  1189. if (err)
  1190. goto out;
  1191. attr->non_res = 1;
  1192. attr->name_off = cpu_to_le16(name_off);
  1193. attr->flags = flags;
  1194. /* This function can't fail - cause already checked above. */
  1195. run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
  1196. attr->nres.svcn = cpu_to_le64(svcn);
  1197. attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
  1198. if (new_attr)
  1199. *new_attr = attr;
  1200. *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
  1201. attr->nres.alloc_size =
  1202. svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
  1203. attr->nres.data_size = attr->nres.alloc_size;
  1204. attr->nres.valid_size = attr->nres.alloc_size;
  1205. if (is_ext) {
  1206. if (flags & ATTR_FLAG_COMPRESSED)
  1207. attr->nres.c_unit = NTFS_LZNT_CUNIT;
  1208. attr->nres.total_size = attr->nres.alloc_size;
  1209. }
  1210. out:
  1211. return err;
  1212. }
  1213. /*
  1214. * ni_insert_resident - Inserts new resident attribute.
  1215. */
  1216. int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
  1217. enum ATTR_TYPE type, const __le16 *name, u8 name_len,
  1218. struct ATTRIB **new_attr, struct mft_inode **mi,
  1219. struct ATTR_LIST_ENTRY **le)
  1220. {
  1221. int err;
  1222. u32 name_size = ALIGN(name_len * sizeof(short), 8);
  1223. u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
  1224. struct ATTRIB *attr;
  1225. err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
  1226. 0, &attr, mi, le);
  1227. if (err)
  1228. return err;
  1229. attr->non_res = 0;
  1230. attr->flags = 0;
  1231. attr->res.data_size = cpu_to_le32(data_size);
  1232. attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
  1233. if (type == ATTR_NAME) {
  1234. attr->res.flags = RESIDENT_FLAG_INDEXED;
  1235. /* is_attr_indexed(attr)) == true */
  1236. le16_add_cpu(&ni->mi.mrec->hard_links, 1);
  1237. ni->mi.dirty = true;
  1238. }
  1239. attr->res.res = 0;
  1240. if (new_attr)
  1241. *new_attr = attr;
  1242. return 0;
  1243. }
  1244. /*
  1245. * ni_remove_attr_le - Remove attribute from record.
  1246. */
  1247. void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
  1248. struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
  1249. {
  1250. mi_remove_attr(ni, mi, attr);
  1251. if (le)
  1252. al_remove_le(ni, le);
  1253. }
  1254. /*
  1255. * ni_delete_all - Remove all attributes and frees allocates space.
  1256. *
  1257. * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
  1258. */
  1259. int ni_delete_all(struct ntfs_inode *ni)
  1260. {
  1261. int err;
  1262. struct ATTR_LIST_ENTRY *le = NULL;
  1263. struct ATTRIB *attr = NULL;
  1264. struct rb_node *node;
  1265. u16 roff;
  1266. u32 asize;
  1267. CLST svcn, evcn;
  1268. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1269. bool nt3 = is_ntfs3(sbi);
  1270. struct MFT_REF ref;
  1271. while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
  1272. if (!nt3 || attr->name_len) {
  1273. ;
  1274. } else if (attr->type == ATTR_REPARSE) {
  1275. mi_get_ref(&ni->mi, &ref);
  1276. ntfs_remove_reparse(sbi, 0, &ref);
  1277. } else if (attr->type == ATTR_ID && !attr->non_res &&
  1278. le32_to_cpu(attr->res.data_size) >=
  1279. sizeof(struct GUID)) {
  1280. ntfs_objid_remove(sbi, resident_data(attr));
  1281. }
  1282. if (!attr->non_res)
  1283. continue;
  1284. svcn = le64_to_cpu(attr->nres.svcn);
  1285. evcn = le64_to_cpu(attr->nres.evcn);
  1286. if (evcn + 1 <= svcn)
  1287. continue;
  1288. asize = le32_to_cpu(attr->size);
  1289. roff = le16_to_cpu(attr->nres.run_off);
  1290. if (roff > asize) {
  1291. /* ni_enum_attr_ex checks this case. */
  1292. continue;
  1293. }
  1294. /* run==1 means unpack and deallocate. */
  1295. run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
  1296. Add2Ptr(attr, roff), asize - roff);
  1297. }
  1298. if (ni->attr_list.size) {
  1299. run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
  1300. al_destroy(ni);
  1301. }
  1302. /* Free all subrecords. */
  1303. for (node = rb_first(&ni->mi_tree); node;) {
  1304. struct rb_node *next = rb_next(node);
  1305. struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
  1306. clear_rec_inuse(mi->mrec);
  1307. mi->dirty = true;
  1308. mi_write(mi, 0);
  1309. ntfs_mark_rec_free(sbi, mi->rno, false);
  1310. ni_remove_mi(ni, mi);
  1311. mi_put(mi);
  1312. node = next;
  1313. }
  1314. /* Free base record. */
  1315. clear_rec_inuse(ni->mi.mrec);
  1316. ni->mi.dirty = true;
  1317. err = mi_write(&ni->mi, 0);
  1318. ntfs_mark_rec_free(sbi, ni->mi.rno, false);
  1319. return err;
  1320. }
  1321. /* ni_fname_name
  1322. *
  1323. * Return: File name attribute by its value.
  1324. */
  1325. struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
  1326. const struct le_str *uni,
  1327. const struct MFT_REF *home_dir,
  1328. struct mft_inode **mi,
  1329. struct ATTR_LIST_ENTRY **le)
  1330. {
  1331. struct ATTRIB *attr = NULL;
  1332. struct ATTR_FILE_NAME *fname;
  1333. if (le)
  1334. *le = NULL;
  1335. /* Enumerate all names. */
  1336. next:
  1337. attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
  1338. if (!attr)
  1339. return NULL;
  1340. fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  1341. if (!fname)
  1342. goto next;
  1343. if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
  1344. goto next;
  1345. if (!uni)
  1346. return fname;
  1347. if (uni->len != fname->name_len)
  1348. goto next;
  1349. if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL,
  1350. false))
  1351. goto next;
  1352. return fname;
  1353. }
  1354. /*
  1355. * ni_fname_type
  1356. *
  1357. * Return: File name attribute with given type.
  1358. */
  1359. struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
  1360. struct mft_inode **mi,
  1361. struct ATTR_LIST_ENTRY **le)
  1362. {
  1363. struct ATTRIB *attr = NULL;
  1364. struct ATTR_FILE_NAME *fname;
  1365. *le = NULL;
  1366. if (name_type == FILE_NAME_POSIX)
  1367. return NULL;
  1368. /* Enumerate all names. */
  1369. for (;;) {
  1370. attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
  1371. if (!attr)
  1372. return NULL;
  1373. fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  1374. if (fname && name_type == fname->type)
  1375. return fname;
  1376. }
  1377. }
  1378. /*
  1379. * ni_new_attr_flags
  1380. *
  1381. * Process compressed/sparsed in special way.
  1382. * NOTE: You need to set ni->std_fa = new_fa
  1383. * after this function to keep internal structures in consistency.
  1384. */
  1385. int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
  1386. {
  1387. struct ATTRIB *attr;
  1388. struct mft_inode *mi;
  1389. __le16 new_aflags;
  1390. u32 new_asize;
  1391. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
  1392. if (!attr)
  1393. return -EINVAL;
  1394. new_aflags = attr->flags;
  1395. if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
  1396. new_aflags |= ATTR_FLAG_SPARSED;
  1397. else
  1398. new_aflags &= ~ATTR_FLAG_SPARSED;
  1399. if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
  1400. new_aflags |= ATTR_FLAG_COMPRESSED;
  1401. else
  1402. new_aflags &= ~ATTR_FLAG_COMPRESSED;
  1403. if (new_aflags == attr->flags)
  1404. return 0;
  1405. if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
  1406. (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
  1407. ntfs_inode_warn(&ni->vfs_inode,
  1408. "file can't be sparsed and compressed");
  1409. return -EOPNOTSUPP;
  1410. }
  1411. if (!attr->non_res)
  1412. goto out;
  1413. if (attr->nres.data_size) {
  1414. ntfs_inode_warn(
  1415. &ni->vfs_inode,
  1416. "one can change sparsed/compressed only for empty files");
  1417. return -EOPNOTSUPP;
  1418. }
  1419. /* Resize nonresident empty attribute in-place only. */
  1420. new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
  1421. (SIZEOF_NONRESIDENT_EX + 8) :
  1422. (SIZEOF_NONRESIDENT + 8);
  1423. if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
  1424. return -EOPNOTSUPP;
  1425. if (new_aflags & ATTR_FLAG_SPARSED) {
  1426. attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
  1427. /* Windows uses 16 clusters per frame but supports one cluster per frame too. */
  1428. attr->nres.c_unit = 0;
  1429. ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
  1430. } else if (new_aflags & ATTR_FLAG_COMPRESSED) {
  1431. attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
  1432. /* The only allowed: 16 clusters per frame. */
  1433. attr->nres.c_unit = NTFS_LZNT_CUNIT;
  1434. ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
  1435. } else {
  1436. attr->name_off = SIZEOF_NONRESIDENT_LE;
  1437. /* Normal files. */
  1438. attr->nres.c_unit = 0;
  1439. ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
  1440. }
  1441. attr->nres.run_off = attr->name_off;
  1442. out:
  1443. attr->flags = new_aflags;
  1444. mi->dirty = true;
  1445. return 0;
  1446. }
  1447. /*
  1448. * ni_parse_reparse
  1449. *
  1450. * buffer - memory for reparse buffer header
  1451. */
  1452. enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
  1453. struct REPARSE_DATA_BUFFER *buffer)
  1454. {
  1455. const struct REPARSE_DATA_BUFFER *rp = NULL;
  1456. u8 bits;
  1457. u16 len;
  1458. typeof(rp->CompressReparseBuffer) *cmpr;
  1459. /* Try to estimate reparse point. */
  1460. if (!attr->non_res) {
  1461. rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
  1462. } else if (le64_to_cpu(attr->nres.data_size) >=
  1463. sizeof(struct REPARSE_DATA_BUFFER)) {
  1464. struct runs_tree run;
  1465. run_init(&run);
  1466. if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
  1467. !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
  1468. sizeof(struct REPARSE_DATA_BUFFER),
  1469. NULL)) {
  1470. rp = buffer;
  1471. }
  1472. run_close(&run);
  1473. }
  1474. if (!rp)
  1475. return REPARSE_NONE;
  1476. len = le16_to_cpu(rp->ReparseDataLength);
  1477. switch (rp->ReparseTag) {
  1478. case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
  1479. break; /* Symbolic link. */
  1480. case IO_REPARSE_TAG_MOUNT_POINT:
  1481. break; /* Mount points and junctions. */
  1482. case IO_REPARSE_TAG_SYMLINK:
  1483. break;
  1484. case IO_REPARSE_TAG_COMPRESS:
  1485. /*
  1486. * WOF - Windows Overlay Filter - Used to compress files with
  1487. * LZX/Xpress.
  1488. *
  1489. * Unlike native NTFS file compression, the Windows
  1490. * Overlay Filter supports only read operations. This means
  1491. * that it doesn't need to sector-align each compressed chunk,
  1492. * so the compressed data can be packed more tightly together.
  1493. * If you open the file for writing, the WOF just decompresses
  1494. * the entire file, turning it back into a plain file.
  1495. *
  1496. * Ntfs3 driver decompresses the entire file only on write or
  1497. * change size requests.
  1498. */
  1499. cmpr = &rp->CompressReparseBuffer;
  1500. if (len < sizeof(*cmpr) ||
  1501. cmpr->WofVersion != WOF_CURRENT_VERSION ||
  1502. cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
  1503. cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
  1504. return REPARSE_NONE;
  1505. }
  1506. switch (cmpr->CompressionFormat) {
  1507. case WOF_COMPRESSION_XPRESS4K:
  1508. bits = 0xc; // 4k
  1509. break;
  1510. case WOF_COMPRESSION_XPRESS8K:
  1511. bits = 0xd; // 8k
  1512. break;
  1513. case WOF_COMPRESSION_XPRESS16K:
  1514. bits = 0xe; // 16k
  1515. break;
  1516. case WOF_COMPRESSION_LZX32K:
  1517. bits = 0xf; // 32k
  1518. break;
  1519. default:
  1520. bits = 0x10; // 64k
  1521. break;
  1522. }
  1523. ni_set_ext_compress_bits(ni, bits);
  1524. return REPARSE_COMPRESSED;
  1525. case IO_REPARSE_TAG_DEDUP:
  1526. ni->ni_flags |= NI_FLAG_DEDUPLICATED;
  1527. return REPARSE_DEDUPLICATED;
  1528. default:
  1529. if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
  1530. break;
  1531. return REPARSE_NONE;
  1532. }
  1533. if (buffer != rp)
  1534. memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
  1535. /* Looks like normal symlink. */
  1536. return REPARSE_LINK;
  1537. }
  1538. static struct page *ntfs_lock_new_page(struct address_space *mapping,
  1539. pgoff_t index, gfp_t gfp)
  1540. {
  1541. struct folio *folio = __filemap_get_folio(
  1542. mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
  1543. struct page *page;
  1544. if (IS_ERR(folio))
  1545. return ERR_CAST(folio);
  1546. if (!folio_test_uptodate(folio))
  1547. return folio_file_page(folio, index);
  1548. /* Use a temporary page to avoid data corruption */
  1549. folio_unlock(folio);
  1550. folio_put(folio);
  1551. page = alloc_page(gfp);
  1552. if (!page)
  1553. return ERR_PTR(-ENOMEM);
  1554. __SetPageLocked(page);
  1555. return page;
  1556. }
  1557. /*
  1558. * ni_read_folio_cmpr
  1559. *
  1560. * When decompressing, we typically obtain more than one page per reference.
  1561. * We inject the additional pages into the page cache.
  1562. */
  1563. int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio)
  1564. {
  1565. int err;
  1566. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1567. struct address_space *mapping = folio->mapping;
  1568. pgoff_t index;
  1569. u64 frame_vbo, vbo = folio_pos(folio);
  1570. struct page **pages = NULL; /* Array of at most 16 pages. stack? */
  1571. u8 frame_bits;
  1572. CLST frame;
  1573. u32 i, idx, frame_size, pages_per_frame;
  1574. gfp_t gfp_mask;
  1575. struct page *pg;
  1576. if (vbo >= i_size_read(&ni->vfs_inode)) {
  1577. folio_zero_range(folio, 0, folio_size(folio));
  1578. folio_mark_uptodate(folio);
  1579. err = 0;
  1580. goto out;
  1581. }
  1582. if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
  1583. /* Xpress or LZX. */
  1584. frame_bits = ni_ext_compress_bits(ni);
  1585. } else {
  1586. /* LZNT compression. */
  1587. frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
  1588. }
  1589. frame_size = 1u << frame_bits;
  1590. frame = vbo >> frame_bits;
  1591. frame_vbo = (u64)frame << frame_bits;
  1592. idx = (vbo - frame_vbo) >> PAGE_SHIFT;
  1593. pages_per_frame = frame_size >> PAGE_SHIFT;
  1594. pages = kzalloc_objs(struct page *, pages_per_frame, GFP_NOFS);
  1595. if (!pages) {
  1596. err = -ENOMEM;
  1597. goto out;
  1598. }
  1599. pages[idx] = &folio->page;
  1600. index = frame_vbo >> PAGE_SHIFT;
  1601. gfp_mask = mapping_gfp_mask(mapping);
  1602. for (i = 0; i < pages_per_frame; i++, index++) {
  1603. if (i == idx)
  1604. continue;
  1605. pg = ntfs_lock_new_page(mapping, index, gfp_mask);
  1606. if (IS_ERR(pg)) {
  1607. err = PTR_ERR(pg);
  1608. goto out1;
  1609. }
  1610. pages[i] = pg;
  1611. }
  1612. ni_lock(ni);
  1613. err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0);
  1614. ni_unlock(ni);
  1615. out1:
  1616. for (i = 0; i < pages_per_frame; i++) {
  1617. pg = pages[i];
  1618. if (i == idx || !pg)
  1619. continue;
  1620. unlock_page(pg);
  1621. put_page(pg);
  1622. }
  1623. out:
  1624. /* At this point, err contains 0 or -EIO depending on the "critical" page. */
  1625. kfree(pages);
  1626. folio_unlock(folio);
  1627. return err;
  1628. }
  1629. #ifdef CONFIG_NTFS3_LZX_XPRESS
  1630. /*
  1631. * ni_decompress_file - Decompress LZX/Xpress compressed file.
  1632. *
  1633. * Remove ATTR_DATA::WofCompressedData.
  1634. * Remove ATTR_REPARSE.
  1635. */
  1636. int ni_decompress_file(struct ntfs_inode *ni)
  1637. {
  1638. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1639. struct inode *inode = &ni->vfs_inode;
  1640. loff_t i_size = i_size_read(inode);
  1641. struct address_space *mapping = inode->i_mapping;
  1642. gfp_t gfp_mask = mapping_gfp_mask(mapping);
  1643. struct page **pages = NULL;
  1644. struct ATTR_LIST_ENTRY *le;
  1645. struct ATTRIB *attr;
  1646. CLST vcn, cend, lcn, clen, end;
  1647. pgoff_t index;
  1648. u64 vbo;
  1649. u8 frame_bits;
  1650. u32 i, frame_size, pages_per_frame, bytes;
  1651. struct mft_inode *mi;
  1652. int err;
  1653. /* Clusters for decompressed data. */
  1654. cend = bytes_to_cluster(sbi, i_size);
  1655. if (!i_size)
  1656. goto remove_wof;
  1657. /* Check in advance. */
  1658. if (cend > wnd_zeroes(&sbi->used.bitmap)) {
  1659. err = -ENOSPC;
  1660. goto out;
  1661. }
  1662. frame_bits = ni_ext_compress_bits(ni);
  1663. frame_size = 1u << frame_bits;
  1664. pages_per_frame = frame_size >> PAGE_SHIFT;
  1665. pages = kzalloc_objs(struct page *, pages_per_frame, GFP_NOFS);
  1666. if (!pages) {
  1667. err = -ENOMEM;
  1668. goto out;
  1669. }
  1670. /*
  1671. * Step 1: Decompress data and copy to new allocated clusters.
  1672. */
  1673. index = 0;
  1674. for (vbo = 0; vbo < i_size; vbo += bytes) {
  1675. bool new;
  1676. bytes = vbo + frame_size > i_size ? (i_size - vbo) : frame_size;
  1677. end = bytes_to_cluster(sbi, vbo + bytes);
  1678. for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
  1679. err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
  1680. &clen, &new, false, NULL,
  1681. false);
  1682. if (err)
  1683. goto out;
  1684. }
  1685. for (i = 0; i < pages_per_frame; i++, index++) {
  1686. struct page *pg;
  1687. pg = ntfs_lock_new_page(mapping, index, gfp_mask);
  1688. if (IS_ERR(pg)) {
  1689. while (i--) {
  1690. unlock_page(pages[i]);
  1691. put_page(pages[i]);
  1692. }
  1693. err = PTR_ERR(pg);
  1694. goto out;
  1695. }
  1696. pages[i] = pg;
  1697. }
  1698. err = ni_read_frame(ni, vbo, pages, pages_per_frame, 1);
  1699. for (i = 0; i < pages_per_frame; i++) {
  1700. unlock_page(pages[i]);
  1701. put_page(pages[i]);
  1702. }
  1703. if (err)
  1704. goto out;
  1705. cond_resched();
  1706. }
  1707. remove_wof:
  1708. /*
  1709. * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
  1710. * and ATTR_REPARSE.
  1711. */
  1712. attr = NULL;
  1713. le = NULL;
  1714. while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
  1715. CLST svcn, evcn;
  1716. u32 asize, roff;
  1717. if (attr->type == ATTR_REPARSE) {
  1718. struct MFT_REF ref;
  1719. mi_get_ref(&ni->mi, &ref);
  1720. ntfs_remove_reparse(sbi, 0, &ref);
  1721. }
  1722. if (!attr->non_res)
  1723. continue;
  1724. if (attr->type != ATTR_REPARSE &&
  1725. (attr->type != ATTR_DATA ||
  1726. attr->name_len != ARRAY_SIZE(WOF_NAME) ||
  1727. memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
  1728. continue;
  1729. svcn = le64_to_cpu(attr->nres.svcn);
  1730. evcn = le64_to_cpu(attr->nres.evcn);
  1731. if (evcn + 1 <= svcn)
  1732. continue;
  1733. asize = le32_to_cpu(attr->size);
  1734. roff = le16_to_cpu(attr->nres.run_off);
  1735. if (roff > asize) {
  1736. err = -EINVAL;
  1737. goto out;
  1738. }
  1739. /*run==1 Means unpack and deallocate. */
  1740. run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
  1741. Add2Ptr(attr, roff), asize - roff);
  1742. }
  1743. /*
  1744. * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
  1745. */
  1746. err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
  1747. false, NULL);
  1748. if (err)
  1749. goto out;
  1750. /*
  1751. * Step 4: Remove ATTR_REPARSE.
  1752. */
  1753. err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
  1754. if (err)
  1755. goto out;
  1756. /*
  1757. * Step 5: Remove sparse flag from data attribute.
  1758. */
  1759. attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
  1760. if (!attr) {
  1761. err = -EINVAL;
  1762. goto out;
  1763. }
  1764. if (attr->non_res && is_attr_sparsed(attr)) {
  1765. /* Sparsed attribute header is 8 bytes bigger than normal. */
  1766. struct MFT_REC *rec = mi->mrec;
  1767. u32 used = le32_to_cpu(rec->used);
  1768. u32 asize = le32_to_cpu(attr->size);
  1769. u16 roff = le16_to_cpu(attr->nres.run_off);
  1770. char *rbuf = Add2Ptr(attr, roff);
  1771. memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
  1772. attr->size = cpu_to_le32(asize - 8);
  1773. attr->flags &= ~ATTR_FLAG_SPARSED;
  1774. attr->nres.run_off = cpu_to_le16(roff - 8);
  1775. attr->nres.c_unit = 0;
  1776. rec->used = cpu_to_le32(used - 8);
  1777. mi->dirty = true;
  1778. ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
  1779. FILE_ATTRIBUTE_REPARSE_POINT);
  1780. mark_inode_dirty(inode);
  1781. }
  1782. /* Clear cached flag. */
  1783. ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
  1784. if (ni->file.offs_folio) {
  1785. folio_put(ni->file.offs_folio);
  1786. ni->file.offs_folio = NULL;
  1787. }
  1788. mapping->a_ops = &ntfs_aops;
  1789. out:
  1790. kfree(pages);
  1791. if (err)
  1792. _ntfs_bad_inode(inode);
  1793. return err;
  1794. }
  1795. /*
  1796. * decompress_lzx_xpress - External compression LZX/Xpress.
  1797. */
  1798. static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
  1799. size_t cmpr_size, void *unc, size_t unc_size,
  1800. u32 frame_size)
  1801. {
  1802. int err;
  1803. void *ctx;
  1804. if (cmpr_size == unc_size) {
  1805. /* Frame not compressed. */
  1806. memcpy(unc, cmpr, unc_size);
  1807. return 0;
  1808. }
  1809. err = 0;
  1810. if (frame_size == 0x8000) {
  1811. mutex_lock(&sbi->compress.mtx_lzx);
  1812. /* LZX: Frame compressed. */
  1813. ctx = sbi->compress.lzx;
  1814. if (!ctx) {
  1815. /* Lazy initialize LZX decompress context. */
  1816. ctx = lzx_allocate_decompressor();
  1817. if (!ctx) {
  1818. err = -ENOMEM;
  1819. goto out1;
  1820. }
  1821. sbi->compress.lzx = ctx;
  1822. }
  1823. if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
  1824. /* Treat all errors as "invalid argument". */
  1825. err = -EINVAL;
  1826. }
  1827. out1:
  1828. mutex_unlock(&sbi->compress.mtx_lzx);
  1829. } else {
  1830. /* XPRESS: Frame compressed. */
  1831. mutex_lock(&sbi->compress.mtx_xpress);
  1832. ctx = sbi->compress.xpress;
  1833. if (!ctx) {
  1834. /* Lazy initialize Xpress decompress context. */
  1835. ctx = xpress_allocate_decompressor();
  1836. if (!ctx) {
  1837. err = -ENOMEM;
  1838. goto out2;
  1839. }
  1840. sbi->compress.xpress = ctx;
  1841. }
  1842. if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
  1843. /* Treat all errors as "invalid argument". */
  1844. err = -EINVAL;
  1845. }
  1846. out2:
  1847. mutex_unlock(&sbi->compress.mtx_xpress);
  1848. }
  1849. return err;
  1850. }
  1851. #endif
  1852. /*
  1853. * ni_read_frame
  1854. *
  1855. * Pages - Array of locked pages.
  1856. */
  1857. int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
  1858. u32 pages_per_frame, int copy)
  1859. {
  1860. int err;
  1861. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1862. u8 cluster_bits = sbi->cluster_bits;
  1863. char *frame_ondisk = NULL;
  1864. char *frame_mem = NULL;
  1865. struct ATTR_LIST_ENTRY *le = NULL;
  1866. struct runs_tree *run = &ni->file.run;
  1867. u64 valid_size = ni->i_valid;
  1868. u64 vbo_disk;
  1869. size_t unc_size = 0;
  1870. u32 frame_size, i, ondisk_size;
  1871. struct page *pg;
  1872. struct ATTRIB *attr;
  1873. CLST frame, clst_data;
  1874. /*
  1875. * To simplify decompress algorithm do vmap for source
  1876. * and target pages.
  1877. */
  1878. frame_size = pages_per_frame << PAGE_SHIFT;
  1879. frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
  1880. if (!frame_mem) {
  1881. err = -ENOMEM;
  1882. goto out;
  1883. }
  1884. attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
  1885. if (!attr) {
  1886. err = -ENOENT;
  1887. goto out1;
  1888. }
  1889. if (!attr->non_res) {
  1890. u32 data_size = le32_to_cpu(attr->res.data_size);
  1891. memset(frame_mem, 0, frame_size);
  1892. if (frame_vbo < data_size) {
  1893. ondisk_size = data_size - frame_vbo;
  1894. memcpy(frame_mem, resident_data(attr) + frame_vbo,
  1895. min(ondisk_size, frame_size));
  1896. }
  1897. err = 0;
  1898. goto out1;
  1899. }
  1900. if (frame_vbo >= valid_size) {
  1901. memset(frame_mem, 0, frame_size);
  1902. err = 0;
  1903. goto out1;
  1904. }
  1905. if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
  1906. #ifndef CONFIG_NTFS3_LZX_XPRESS
  1907. err = -EOPNOTSUPP;
  1908. goto out1;
  1909. #else
  1910. loff_t i_size = i_size_read(&ni->vfs_inode);
  1911. u32 frame_bits = ni_ext_compress_bits(ni);
  1912. u64 frame64 = frame_vbo >> frame_bits;
  1913. u64 frames, vbo_data;
  1914. if (frame_size != (1u << frame_bits)) {
  1915. err = -EINVAL;
  1916. goto out1;
  1917. }
  1918. switch (frame_size) {
  1919. case 0x1000:
  1920. case 0x2000:
  1921. case 0x4000:
  1922. case 0x8000:
  1923. break;
  1924. default:
  1925. /* Unknown compression. */
  1926. err = -EOPNOTSUPP;
  1927. goto out1;
  1928. }
  1929. attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
  1930. ARRAY_SIZE(WOF_NAME), NULL, NULL);
  1931. if (!attr) {
  1932. ntfs_inode_err(
  1933. &ni->vfs_inode,
  1934. "external compressed file should contains data attribute \"WofCompressedData\"");
  1935. err = -EINVAL;
  1936. goto out1;
  1937. }
  1938. if (!attr->non_res) {
  1939. run = NULL;
  1940. } else {
  1941. run = run_alloc();
  1942. if (!run) {
  1943. err = -ENOMEM;
  1944. goto out1;
  1945. }
  1946. }
  1947. frames = (i_size - 1) >> frame_bits;
  1948. err = attr_wof_frame_info(ni, attr, run, frame64, frames,
  1949. frame_bits, &ondisk_size, &vbo_data);
  1950. if (err)
  1951. goto out1;
  1952. if (frame64 == frames) {
  1953. unc_size = 1 + ((i_size - 1) & (frame_size - 1));
  1954. ondisk_size = attr_size(attr) - vbo_data;
  1955. } else {
  1956. unc_size = frame_size;
  1957. }
  1958. if (ondisk_size > frame_size) {
  1959. err = -EINVAL;
  1960. goto out1;
  1961. }
  1962. if (!attr->non_res) {
  1963. if (vbo_data + ondisk_size >
  1964. le32_to_cpu(attr->res.data_size)) {
  1965. err = -EINVAL;
  1966. goto out1;
  1967. }
  1968. err = decompress_lzx_xpress(
  1969. sbi, Add2Ptr(resident_data(attr), vbo_data),
  1970. ondisk_size, frame_mem, unc_size, frame_size);
  1971. goto out1;
  1972. }
  1973. vbo_disk = vbo_data;
  1974. /* Load all runs to read [vbo_disk-vbo_to). */
  1975. err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
  1976. ARRAY_SIZE(WOF_NAME), run, vbo_disk,
  1977. vbo_data + ondisk_size);
  1978. if (err)
  1979. goto out1;
  1980. #endif
  1981. } else if (is_attr_compressed(attr)) {
  1982. /* LZNT compression. */
  1983. if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
  1984. err = -EOPNOTSUPP;
  1985. goto out1;
  1986. }
  1987. if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
  1988. err = -EOPNOTSUPP;
  1989. goto out1;
  1990. }
  1991. down_write(&ni->file.run_lock);
  1992. run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
  1993. frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
  1994. err = attr_is_frame_compressed(ni, attr, frame, &clst_data,
  1995. run);
  1996. up_write(&ni->file.run_lock);
  1997. if (err)
  1998. goto out1;
  1999. if (!clst_data) {
  2000. memset(frame_mem, 0, frame_size);
  2001. goto out1;
  2002. }
  2003. frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
  2004. ondisk_size = clst_data << cluster_bits;
  2005. if (clst_data >= NTFS_LZNT_CLUSTERS) {
  2006. /* Frame is not compressed. */
  2007. down_read(&ni->file.run_lock);
  2008. err = ntfs_read_run(sbi, run, frame_mem, frame_vbo,
  2009. ondisk_size);
  2010. up_read(&ni->file.run_lock);
  2011. goto out1;
  2012. }
  2013. vbo_disk = frame_vbo;
  2014. } else {
  2015. __builtin_unreachable();
  2016. err = -EINVAL;
  2017. goto out1;
  2018. }
  2019. /* Allocate memory to read compressed data to. */
  2020. frame_ondisk = kvmalloc(ondisk_size, GFP_KERNEL);
  2021. if (!frame_ondisk) {
  2022. err = -ENOMEM;
  2023. goto out1;
  2024. }
  2025. /* Read 'ondisk_size' bytes from disk. */
  2026. down_read(&ni->file.run_lock);
  2027. err = ntfs_read_run(sbi, run, frame_ondisk, vbo_disk, ondisk_size);
  2028. up_read(&ni->file.run_lock);
  2029. if (err)
  2030. goto out2;
  2031. #ifdef CONFIG_NTFS3_LZX_XPRESS
  2032. if (run != &ni->file.run) {
  2033. /* LZX or XPRESS */
  2034. err = decompress_lzx_xpress(sbi, frame_ondisk, ondisk_size,
  2035. frame_mem, unc_size, frame_size);
  2036. } else
  2037. #endif
  2038. {
  2039. /* LZNT - Native NTFS compression. */
  2040. unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
  2041. frame_size);
  2042. if ((ssize_t)unc_size < 0)
  2043. err = unc_size;
  2044. else if (!unc_size || unc_size > frame_size)
  2045. err = -EINVAL;
  2046. }
  2047. if (!err && valid_size < frame_vbo + frame_size) {
  2048. size_t ok = valid_size - frame_vbo;
  2049. memset(frame_mem + ok, 0, frame_size - ok);
  2050. }
  2051. out2:
  2052. kvfree(frame_ondisk);
  2053. out1:
  2054. #ifdef CONFIG_NTFS3_LZX_XPRESS
  2055. if (run != &ni->file.run)
  2056. run_free(run);
  2057. if (!err && copy) {
  2058. /* We are called from 'ni_decompress_file' */
  2059. /* Copy decompressed LZX or XPRESS data into new place. */
  2060. down_read(&ni->file.run_lock);
  2061. err = ntfs_write_run(sbi, &ni->file.run, frame_mem, frame_vbo,
  2062. frame_size);
  2063. up_read(&ni->file.run_lock);
  2064. }
  2065. #endif
  2066. vunmap(frame_mem);
  2067. out:
  2068. for (i = 0; i < pages_per_frame; i++) {
  2069. pg = pages[i];
  2070. SetPageUptodate(pg);
  2071. }
  2072. return err;
  2073. }
  2074. /*
  2075. * ni_write_frame
  2076. *
  2077. * Pages - Array of locked pages.
  2078. */
  2079. int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
  2080. u32 pages_per_frame)
  2081. {
  2082. int err;
  2083. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2084. struct folio *folio = page_folio(pages[0]);
  2085. u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
  2086. u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
  2087. u64 frame_vbo = folio_pos(folio);
  2088. CLST frame = frame_vbo >> frame_bits;
  2089. char *frame_ondisk = NULL;
  2090. struct ATTR_LIST_ENTRY *le = NULL;
  2091. char *frame_mem;
  2092. struct ATTRIB *attr;
  2093. struct mft_inode *mi;
  2094. size_t compr_size, ondisk_size;
  2095. struct lznt *lznt;
  2096. attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
  2097. if (!attr) {
  2098. err = -ENOENT;
  2099. goto out;
  2100. }
  2101. if (WARN_ON(!is_attr_compressed(attr))) {
  2102. err = -EINVAL;
  2103. goto out;
  2104. }
  2105. if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
  2106. err = -EOPNOTSUPP;
  2107. goto out;
  2108. }
  2109. if (!attr->non_res) {
  2110. down_write(&ni->file.run_lock);
  2111. err = attr_make_nonresident(ni, attr, le, mi,
  2112. le32_to_cpu(attr->res.data_size),
  2113. &ni->file.run, &attr, pages[0]);
  2114. up_write(&ni->file.run_lock);
  2115. if (err)
  2116. goto out;
  2117. }
  2118. if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
  2119. err = -EOPNOTSUPP;
  2120. goto out;
  2121. }
  2122. /* Allocate memory to write compressed data to. */
  2123. frame_ondisk = kvmalloc(frame_size, GFP_KERNEL);
  2124. if (!frame_ondisk) {
  2125. err = -ENOMEM;
  2126. goto out;
  2127. }
  2128. /* Map in-memory frame for read-only. */
  2129. frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
  2130. if (!frame_mem) {
  2131. err = -ENOMEM;
  2132. goto out1;
  2133. }
  2134. mutex_lock(&sbi->compress.mtx_lznt);
  2135. lznt = NULL;
  2136. if (!sbi->compress.lznt) {
  2137. /*
  2138. * LZNT implements two levels of compression:
  2139. * 0 - Standard compression
  2140. * 1 - Best compression, requires a lot of cpu
  2141. * use mount option?
  2142. */
  2143. lznt = get_lznt_ctx(0);
  2144. if (!lznt) {
  2145. mutex_unlock(&sbi->compress.mtx_lznt);
  2146. err = -ENOMEM;
  2147. goto out2;
  2148. }
  2149. sbi->compress.lznt = lznt;
  2150. lznt = NULL;
  2151. }
  2152. /* Compress: frame_mem -> frame_ondisk */
  2153. compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
  2154. frame_size, sbi->compress.lznt);
  2155. mutex_unlock(&sbi->compress.mtx_lznt);
  2156. kfree(lznt);
  2157. if (compr_size + sbi->cluster_size > frame_size) {
  2158. /* Frame is not compressed. */
  2159. compr_size = frame_size;
  2160. ondisk_size = frame_size;
  2161. } else if (compr_size) {
  2162. /* Frame is compressed. */
  2163. ondisk_size = ntfs_up_cluster(sbi, compr_size);
  2164. memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
  2165. } else {
  2166. /* Frame is sparsed. */
  2167. ondisk_size = 0;
  2168. }
  2169. down_write(&ni->file.run_lock);
  2170. run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
  2171. err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
  2172. up_write(&ni->file.run_lock);
  2173. if (err)
  2174. goto out2;
  2175. if (!ondisk_size)
  2176. goto out2;
  2177. down_read(&ni->file.run_lock);
  2178. err = ntfs_write_run(sbi, &ni->file.run,
  2179. ondisk_size < frame_size ? frame_ondisk :
  2180. frame_mem,
  2181. frame_vbo, ondisk_size);
  2182. up_read(&ni->file.run_lock);
  2183. out2:
  2184. vunmap(frame_mem);
  2185. out1:
  2186. kvfree(frame_ondisk);
  2187. out:
  2188. return err;
  2189. }
  2190. /*
  2191. * ni_remove_name - Removes name 'de' from MFT and from directory.
  2192. * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
  2193. */
  2194. int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  2195. struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
  2196. {
  2197. int err;
  2198. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2199. struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
  2200. struct ATTR_FILE_NAME *fname;
  2201. struct ATTR_LIST_ENTRY *le;
  2202. struct mft_inode *mi;
  2203. u16 de_key_size = le16_to_cpu(de->key_size);
  2204. u8 name_type;
  2205. *undo_step = 0;
  2206. /* Find name in record. */
  2207. mi_get_ref(&dir_ni->mi, &de_name->home);
  2208. fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len,
  2209. &de_name->home, &mi, &le);
  2210. if (!fname)
  2211. return -ENOENT;
  2212. memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
  2213. name_type = paired_name(fname->type);
  2214. /* Mark ntfs as dirty. It will be cleared at umount. */
  2215. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  2216. /* Step 1: Remove name from directory. */
  2217. err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
  2218. if (err)
  2219. return err;
  2220. /* Step 2: Remove name from MFT. */
  2221. ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
  2222. *undo_step = 2;
  2223. /* Get paired name. */
  2224. fname = ni_fname_type(ni, name_type, &mi, &le);
  2225. if (fname) {
  2226. u16 de2_key_size = fname_full_size(fname);
  2227. *de2 = Add2Ptr(de, 1024);
  2228. (*de2)->key_size = cpu_to_le16(de2_key_size);
  2229. memcpy(*de2 + 1, fname, de2_key_size);
  2230. /* Step 3: Remove paired name from directory. */
  2231. err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
  2232. de2_key_size, sbi);
  2233. if (err)
  2234. return err;
  2235. /* Step 4: Remove paired name from MFT. */
  2236. ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
  2237. *undo_step = 4;
  2238. }
  2239. return 0;
  2240. }
  2241. /*
  2242. * ni_remove_name_undo - Paired function for ni_remove_name.
  2243. *
  2244. * Return: True if ok
  2245. */
  2246. bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  2247. struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
  2248. {
  2249. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2250. struct ATTRIB *attr;
  2251. u16 de_key_size;
  2252. switch (undo_step) {
  2253. case 4:
  2254. de_key_size = le16_to_cpu(de2->key_size);
  2255. if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
  2256. &attr, NULL, NULL))
  2257. return false;
  2258. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
  2259. mi_get_ref(&ni->mi, &de2->ref);
  2260. de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
  2261. sizeof(struct NTFS_DE));
  2262. de2->flags = 0;
  2263. de2->res = 0;
  2264. if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL, 1))
  2265. return false;
  2266. fallthrough;
  2267. case 2:
  2268. de_key_size = le16_to_cpu(de->key_size);
  2269. if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
  2270. &attr, NULL, NULL))
  2271. return false;
  2272. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
  2273. mi_get_ref(&ni->mi, &de->ref);
  2274. if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
  2275. return false;
  2276. }
  2277. return true;
  2278. }
  2279. /*
  2280. * ni_add_name - Add new name into MFT and into directory.
  2281. */
  2282. int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  2283. struct NTFS_DE *de)
  2284. {
  2285. int err;
  2286. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2287. struct ATTRIB *attr;
  2288. struct ATTR_LIST_ENTRY *le;
  2289. struct mft_inode *mi;
  2290. struct ATTR_FILE_NAME *fname;
  2291. struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
  2292. u16 de_key_size = le16_to_cpu(de->key_size);
  2293. if (sbi->options->windows_names &&
  2294. !valid_windows_name(sbi, (struct le_str *)&de_name->name_len))
  2295. return -EINVAL;
  2296. /* If option "hide_dot_files" then set hidden attribute for dot files. */
  2297. if (ni->mi.sbi->options->hide_dot_files) {
  2298. if (de_name->name_len > 0 &&
  2299. le16_to_cpu(de_name->name[0]) == '.')
  2300. ni->std_fa |= FILE_ATTRIBUTE_HIDDEN;
  2301. else
  2302. ni->std_fa &= ~FILE_ATTRIBUTE_HIDDEN;
  2303. }
  2304. mi_get_ref(&ni->mi, &de->ref);
  2305. mi_get_ref(&dir_ni->mi, &de_name->home);
  2306. /* Fill duplicate from any ATTR_NAME. */
  2307. fname = ni_fname_name(ni, NULL, NULL, NULL, NULL);
  2308. if (fname)
  2309. memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup));
  2310. de_name->dup.fa = ni->std_fa;
  2311. /* Insert new name into MFT. */
  2312. err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
  2313. &mi, &le);
  2314. if (err)
  2315. return err;
  2316. memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
  2317. /* Insert new name into directory. */
  2318. err = indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 0);
  2319. if (err)
  2320. ni_remove_attr_le(ni, attr, mi, le);
  2321. return err;
  2322. }
  2323. /*
  2324. * ni_rename - Remove one name and insert new name.
  2325. */
  2326. int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
  2327. struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de)
  2328. {
  2329. int err;
  2330. struct NTFS_DE *de2 = NULL;
  2331. int undo = 0;
  2332. /*
  2333. * There are two possible ways to rename:
  2334. * 1) Add new name and remove old name.
  2335. * 2) Remove old name and add new name.
  2336. *
  2337. * In most cases (not all!) adding new name into MFT and into directory can
  2338. * allocate additional cluster(s).
  2339. * Second way may result to bad inode if we can't add new name
  2340. * and then can't restore (add) old name.
  2341. */
  2342. /*
  2343. * Way 1 - Add new + remove old.
  2344. */
  2345. err = ni_add_name(new_dir_ni, ni, new_de);
  2346. if (!err) {
  2347. err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
  2348. WARN_ON(err &&
  2349. ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo));
  2350. }
  2351. /*
  2352. * Way 2 - Remove old + add new.
  2353. */
  2354. /*
  2355. * err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
  2356. * if (!err) {
  2357. * err = ni_add_name(new_dir_ni, ni, new_de);
  2358. * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
  2359. * *is_bad = true;
  2360. * }
  2361. */
  2362. return err;
  2363. }
  2364. /*
  2365. * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
  2366. */
  2367. bool ni_is_dirty(struct inode *inode)
  2368. {
  2369. struct ntfs_inode *ni = ntfs_i(inode);
  2370. struct rb_node *node;
  2371. if (ni->mi.dirty || ni->attr_list.dirty ||
  2372. (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
  2373. return true;
  2374. for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
  2375. if (rb_entry(node, struct mft_inode, node)->dirty)
  2376. return true;
  2377. }
  2378. return false;
  2379. }
  2380. /*
  2381. * ni_seek_data_or_hole
  2382. *
  2383. * Helper function for ntfs_llseek( SEEK_DATA/SEEK_HOLE )
  2384. */
  2385. loff_t ni_seek_data_or_hole(struct ntfs_inode *ni, loff_t offset, bool data)
  2386. {
  2387. int err;
  2388. u8 cluster_bits = ni->mi.sbi->cluster_bits;
  2389. CLST vcn, lcn, clen;
  2390. loff_t vbo;
  2391. /* Enumerate all fragments. */
  2392. for (vcn = offset >> cluster_bits;; vcn += clen) {
  2393. err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL, false,
  2394. NULL, false);
  2395. if (err) {
  2396. return err;
  2397. }
  2398. if (lcn == RESIDENT_LCN) {
  2399. /* clen - resident size in bytes. clen == ni->vfs_inode.i_size */
  2400. if (offset >= clen) {
  2401. /* check eof. */
  2402. return -ENXIO;
  2403. }
  2404. if (data) {
  2405. return offset;
  2406. }
  2407. return clen;
  2408. }
  2409. if (lcn == EOF_LCN) {
  2410. if (data) {
  2411. return -ENXIO;
  2412. }
  2413. /* implicit hole at the end of file. */
  2414. return ni->vfs_inode.i_size;
  2415. }
  2416. if (data) {
  2417. /*
  2418. * Adjust the file offset to the next location in the file greater than
  2419. * or equal to offset containing data. If offset points to data, then
  2420. * the file offset is set to offset.
  2421. */
  2422. if (lcn != SPARSE_LCN) {
  2423. vbo = (u64)vcn << cluster_bits;
  2424. return max(vbo, offset);
  2425. }
  2426. } else {
  2427. /*
  2428. * Adjust the file offset to the next hole in the file greater than or
  2429. * equal to offset. If offset points into the middle of a hole, then the
  2430. * file offset is set to offset. If there is no hole past offset, then the
  2431. * file offset is adjusted to the end of the file
  2432. * (i.e., there is an implicit hole at the end of any file).
  2433. */
  2434. if (lcn == SPARSE_LCN &&
  2435. /* native compression hole begins at aligned vcn. */
  2436. (!(ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) ||
  2437. !(vcn & (NTFS_LZNT_CLUSTERS - 1)))) {
  2438. vbo = (u64)vcn << cluster_bits;
  2439. return max(vbo, offset);
  2440. }
  2441. }
  2442. if (!clen) {
  2443. /* Corrupted file. */
  2444. return -EINVAL;
  2445. }
  2446. }
  2447. }
  2448. /*
  2449. * ni_write_parents
  2450. *
  2451. * Helper function for ntfs_file_fsync.
  2452. */
  2453. int ni_write_parents(struct ntfs_inode *ni, int sync)
  2454. {
  2455. int err = 0;
  2456. struct ATTRIB *attr = NULL;
  2457. struct ATTR_LIST_ENTRY *le = NULL;
  2458. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2459. struct super_block *sb = sbi->sb;
  2460. while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
  2461. NULL))) {
  2462. struct inode *dir;
  2463. struct ATTR_FILE_NAME *fname;
  2464. fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  2465. if (!fname)
  2466. continue;
  2467. /* Check simple case when parent inode equals current inode. */
  2468. if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
  2469. if (MFT_REC_ROOT != ni->vfs_inode.i_ino) {
  2470. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  2471. err = -EINVAL;
  2472. }
  2473. continue;
  2474. }
  2475. dir = ntfs_iget5(sb, &fname->home, NULL);
  2476. if (IS_ERR(dir)) {
  2477. ntfs_inode_warn(
  2478. &ni->vfs_inode,
  2479. "failed to open parent directory r=%lx to write",
  2480. (long)ino_get(&fname->home));
  2481. continue;
  2482. }
  2483. if (!is_bad_inode(dir)) {
  2484. int err2 = write_inode_now(dir, sync);
  2485. if (!err)
  2486. err = err2;
  2487. }
  2488. iput(dir);
  2489. }
  2490. return err;
  2491. }
  2492. /*
  2493. * ni_update_parent
  2494. *
  2495. * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
  2496. */
  2497. static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
  2498. int sync)
  2499. {
  2500. struct ATTRIB *attr;
  2501. struct mft_inode *mi;
  2502. struct ATTR_LIST_ENTRY *le = NULL;
  2503. struct ntfs_sb_info *sbi = ni->mi.sbi;
  2504. struct super_block *sb = sbi->sb;
  2505. bool re_dirty = false;
  2506. if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
  2507. dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
  2508. attr = NULL;
  2509. dup->alloc_size = 0;
  2510. dup->data_size = 0;
  2511. } else {
  2512. dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
  2513. attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
  2514. &mi);
  2515. if (!attr) {
  2516. dup->alloc_size = dup->data_size = 0;
  2517. } else if (!attr->non_res) {
  2518. u32 data_size = le32_to_cpu(attr->res.data_size);
  2519. dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
  2520. dup->data_size = cpu_to_le64(data_size);
  2521. } else {
  2522. u64 new_valid = ni->i_valid;
  2523. u64 data_size = le64_to_cpu(attr->nres.data_size);
  2524. __le64 valid_le;
  2525. dup->alloc_size = is_attr_ext(attr) ?
  2526. attr->nres.total_size :
  2527. attr->nres.alloc_size;
  2528. dup->data_size = attr->nres.data_size;
  2529. if (new_valid > data_size)
  2530. new_valid = data_size;
  2531. valid_le = cpu_to_le64(new_valid);
  2532. if (valid_le != attr->nres.valid_size) {
  2533. attr->nres.valid_size = valid_le;
  2534. mi->dirty = true;
  2535. }
  2536. }
  2537. }
  2538. dup->extend_data = 0;
  2539. if (dup->fa & FILE_ATTRIBUTE_REPARSE_POINT) {
  2540. attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL,
  2541. NULL);
  2542. if (attr) {
  2543. const struct REPARSE_POINT *rp;
  2544. rp = resident_data_ex(attr,
  2545. sizeof(struct REPARSE_POINT));
  2546. /* If ATTR_REPARSE exists 'rp' can't be NULL. */
  2547. if (rp)
  2548. dup->extend_data = rp->ReparseTag;
  2549. }
  2550. } else if (ni->ni_flags & NI_FLAG_EA) {
  2551. attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
  2552. NULL);
  2553. if (attr) {
  2554. const struct EA_INFO *info;
  2555. info = resident_data_ex(attr, sizeof(struct EA_INFO));
  2556. /* If ATTR_EA_INFO exists 'info' can't be NULL. */
  2557. if (info)
  2558. dup->extend_data = info->size;
  2559. }
  2560. }
  2561. attr = NULL;
  2562. le = NULL;
  2563. while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
  2564. &mi))) {
  2565. struct inode *dir;
  2566. struct ATTR_FILE_NAME *fname;
  2567. fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
  2568. if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
  2569. continue;
  2570. /* Check simple case when parent inode equals current inode. */
  2571. if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
  2572. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  2573. continue;
  2574. }
  2575. /* ntfs_iget5 may sleep. */
  2576. dir = ntfs_iget5(sb, &fname->home, NULL);
  2577. if (IS_ERR(dir)) {
  2578. ntfs_inode_warn(
  2579. &ni->vfs_inode,
  2580. "failed to open parent directory r=%lx to update",
  2581. (long)ino_get(&fname->home));
  2582. continue;
  2583. }
  2584. if (!is_bad_inode(dir)) {
  2585. struct ntfs_inode *dir_ni = ntfs_i(dir);
  2586. if (!ni_trylock(dir_ni)) {
  2587. re_dirty = true;
  2588. } else {
  2589. indx_update_dup(dir_ni, sbi, fname, dup, sync);
  2590. ni_unlock(dir_ni);
  2591. memcpy(&fname->dup, dup, sizeof(fname->dup));
  2592. mi->dirty = true;
  2593. }
  2594. }
  2595. iput(dir);
  2596. }
  2597. return re_dirty;
  2598. }
  2599. /*
  2600. * ni_write_inode - Write MFT base record and all subrecords to disk.
  2601. */
  2602. int ni_write_inode(struct inode *inode, int sync, const char *hint)
  2603. {
  2604. int err = 0, err2;
  2605. struct ntfs_inode *ni = ntfs_i(inode);
  2606. struct super_block *sb = inode->i_sb;
  2607. struct ntfs_sb_info *sbi = sb->s_fs_info;
  2608. bool re_dirty = false;
  2609. struct ATTR_STD_INFO *std;
  2610. struct rb_node *node, *next;
  2611. struct NTFS_DUP_INFO dup;
  2612. if (is_bad_inode(inode) || sb_rdonly(sb))
  2613. return 0;
  2614. /* Avoid any operation if inode is bad. */
  2615. if (unlikely(is_bad_ni(ni)))
  2616. return -EINVAL;
  2617. if (unlikely(ntfs3_forced_shutdown(sb)))
  2618. return -EIO;
  2619. if (!ni_trylock(ni)) {
  2620. /* 'ni' is under modification, skip for now. */
  2621. mark_inode_dirty_sync(inode);
  2622. return 0;
  2623. }
  2624. if (!ni->mi.mrec)
  2625. goto out;
  2626. if (is_rec_inuse(ni->mi.mrec) &&
  2627. !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
  2628. bool modified = false;
  2629. struct timespec64 ts;
  2630. /* Update times in standard attribute. */
  2631. std = ni_std(ni);
  2632. if (!std) {
  2633. err = -EINVAL;
  2634. goto out;
  2635. }
  2636. /* Update the access times if they have changed. */
  2637. ts = inode_get_mtime(inode);
  2638. dup.m_time = kernel2nt(&ts);
  2639. if (std->m_time != dup.m_time) {
  2640. std->m_time = dup.m_time;
  2641. modified = true;
  2642. }
  2643. ts = inode_get_ctime(inode);
  2644. dup.c_time = kernel2nt(&ts);
  2645. if (std->c_time != dup.c_time) {
  2646. std->c_time = dup.c_time;
  2647. modified = true;
  2648. }
  2649. ts = inode_get_atime(inode);
  2650. dup.a_time = kernel2nt(&ts);
  2651. if (std->a_time != dup.a_time) {
  2652. std->a_time = dup.a_time;
  2653. modified = true;
  2654. }
  2655. dup.fa = ni->std_fa;
  2656. if (std->fa != dup.fa) {
  2657. std->fa = dup.fa;
  2658. modified = true;
  2659. }
  2660. /* std attribute is always in primary MFT record. */
  2661. if (modified)
  2662. ni->mi.dirty = true;
  2663. if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
  2664. (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
  2665. /* Avoid __wait_on_freeing_inode(inode). */
  2666. && (sb->s_flags & SB_ACTIVE)) {
  2667. dup.cr_time = std->cr_time;
  2668. /* Not critical if this function fail. */
  2669. re_dirty = ni_update_parent(ni, &dup, sync);
  2670. if (re_dirty)
  2671. ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
  2672. else
  2673. ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
  2674. }
  2675. /* Update attribute list. */
  2676. if (ni->attr_list.size && ni->attr_list.dirty) {
  2677. if (inode->i_ino != MFT_REC_MFT || sync) {
  2678. err = ni_try_remove_attr_list(ni);
  2679. if (err)
  2680. goto out;
  2681. }
  2682. err = al_update(ni, sync);
  2683. if (err)
  2684. goto out;
  2685. }
  2686. }
  2687. for (node = rb_first(&ni->mi_tree); node; node = next) {
  2688. struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
  2689. bool is_empty;
  2690. next = rb_next(node);
  2691. if (!mi->dirty)
  2692. continue;
  2693. is_empty = !mi_enum_attr(ni, mi, NULL);
  2694. if (is_empty)
  2695. clear_rec_inuse(mi->mrec);
  2696. err2 = mi_write(mi, sync);
  2697. if (!err && err2)
  2698. err = err2;
  2699. if (is_empty) {
  2700. ntfs_mark_rec_free(sbi, mi->rno, false);
  2701. rb_erase(node, &ni->mi_tree);
  2702. mi_put(mi);
  2703. }
  2704. }
  2705. if (ni->mi.dirty) {
  2706. err2 = mi_write(&ni->mi, sync);
  2707. if (!err && err2)
  2708. err = err2;
  2709. }
  2710. out:
  2711. ni_unlock(ni);
  2712. if (err) {
  2713. ntfs_inode_err(inode, "%s failed, %d.", hint, err);
  2714. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  2715. return err;
  2716. }
  2717. if (re_dirty)
  2718. mark_inode_dirty_sync(inode);
  2719. return 0;
  2720. }
  2721. /*
  2722. * Force to allocate all delay allocated clusters.
  2723. */
  2724. int ni_allocate_da_blocks(struct ntfs_inode *ni)
  2725. {
  2726. int err;
  2727. ni_lock(ni);
  2728. down_write(&ni->file.run_lock);
  2729. err = ni_allocate_da_blocks_locked(ni);
  2730. up_write(&ni->file.run_lock);
  2731. ni_unlock(ni);
  2732. return err;
  2733. }
  2734. /*
  2735. * Force to allocate all delay allocated clusters.
  2736. */
  2737. int ni_allocate_da_blocks_locked(struct ntfs_inode *ni)
  2738. {
  2739. int err;
  2740. if (!ni->file.run_da.count)
  2741. return 0;
  2742. if (is_sparsed(ni)) {
  2743. CLST vcn, lcn, clen, alen;
  2744. bool new;
  2745. /*
  2746. * Sparse file allocates clusters in 'attr_data_get_block_locked'
  2747. */
  2748. while (run_get_entry(&ni->file.run_da, 0, &vcn, &lcn, &clen)) {
  2749. /* TODO: zero=true? */
  2750. err = attr_data_get_block_locked(ni, vcn, clen, &lcn,
  2751. &alen, &new, true,
  2752. NULL, true);
  2753. if (err)
  2754. break;
  2755. if (!new) {
  2756. err = -EINVAL;
  2757. break;
  2758. }
  2759. }
  2760. } else {
  2761. /*
  2762. * Normal file allocates clusters in 'attr_set_size'
  2763. */
  2764. err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run,
  2765. ni->vfs_inode.i_size, &ni->i_valid,
  2766. false, NULL, true);
  2767. }
  2768. return err;
  2769. }