frecord.c 75 KB

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