fslog.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235
  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/blkdev.h>
  8. #include <linux/fs.h>
  9. #include <linux/random.h>
  10. #include <linux/slab.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. /*
  15. * LOG FILE structs
  16. */
  17. // clang-format off
  18. #define MaxLogFileSize 0x100000000ull
  19. #define DefaultLogPageSize 4096
  20. #define MinLogRecordPages 0x30
  21. struct RESTART_HDR {
  22. struct NTFS_RECORD_HEADER rhdr; // 'RSTR'
  23. __le32 sys_page_size; // 0x10: Page size of the system which initialized the log.
  24. __le32 page_size; // 0x14: Log page size used for this log file.
  25. __le16 ra_off; // 0x18:
  26. __le16 minor_ver; // 0x1A:
  27. __le16 major_ver; // 0x1C:
  28. __le16 fixups[];
  29. };
  30. #define LFS_NO_CLIENT 0xffff
  31. #define LFS_NO_CLIENT_LE cpu_to_le16(0xffff)
  32. struct CLIENT_REC {
  33. __le64 oldest_lsn;
  34. __le64 restart_lsn; // 0x08:
  35. __le16 prev_client; // 0x10:
  36. __le16 next_client; // 0x12:
  37. __le16 seq_num; // 0x14:
  38. u8 align[6]; // 0x16:
  39. __le32 name_bytes; // 0x1C: In bytes.
  40. __le16 name[32]; // 0x20: Name of client.
  41. };
  42. static_assert(sizeof(struct CLIENT_REC) == 0x60);
  43. /* Two copies of these will exist at the beginning of the log file */
  44. struct RESTART_AREA {
  45. __le64 current_lsn; // 0x00: Current logical end of log file.
  46. __le16 log_clients; // 0x08: Maximum number of clients.
  47. __le16 client_idx[2]; // 0x0A: Free/use index into the client record arrays.
  48. __le16 flags; // 0x0E: See RESTART_SINGLE_PAGE_IO.
  49. __le32 seq_num_bits; // 0x10: The number of bits in sequence number.
  50. __le16 ra_len; // 0x14:
  51. __le16 client_off; // 0x16:
  52. __le64 l_size; // 0x18: Usable log file size.
  53. __le32 last_lsn_data_len; // 0x20:
  54. __le16 rec_hdr_len; // 0x24: Log page data offset.
  55. __le16 data_off; // 0x26: Log page data length.
  56. __le32 open_log_count; // 0x28:
  57. __le32 align[5]; // 0x2C:
  58. struct CLIENT_REC clients[]; // 0x40:
  59. };
  60. struct LOG_REC_HDR {
  61. __le16 redo_op; // 0x00: NTFS_LOG_OPERATION
  62. __le16 undo_op; // 0x02: NTFS_LOG_OPERATION
  63. __le16 redo_off; // 0x04: Offset to Redo record.
  64. __le16 redo_len; // 0x06: Redo length.
  65. __le16 undo_off; // 0x08: Offset to Undo record.
  66. __le16 undo_len; // 0x0A: Undo length.
  67. __le16 target_attr; // 0x0C:
  68. __le16 lcns_follow; // 0x0E:
  69. __le16 record_off; // 0x10:
  70. __le16 attr_off; // 0x12:
  71. __le16 cluster_off; // 0x14:
  72. __le16 reserved; // 0x16:
  73. __le64 target_vcn; // 0x18:
  74. __le64 page_lcns[]; // 0x20:
  75. };
  76. static_assert(sizeof(struct LOG_REC_HDR) == 0x20);
  77. #define RESTART_ENTRY_ALLOCATED 0xFFFFFFFF
  78. #define RESTART_ENTRY_ALLOCATED_LE cpu_to_le32(0xFFFFFFFF)
  79. struct RESTART_TABLE {
  80. __le16 size; // 0x00: In bytes
  81. __le16 used; // 0x02: Entries
  82. __le16 total; // 0x04: Entries
  83. __le16 res[3]; // 0x06:
  84. __le32 free_goal; // 0x0C:
  85. __le32 first_free; // 0x10:
  86. __le32 last_free; // 0x14:
  87. };
  88. static_assert(sizeof(struct RESTART_TABLE) == 0x18);
  89. struct ATTR_NAME_ENTRY {
  90. __le16 off; // Offset in the Open attribute Table.
  91. __le16 name_bytes;
  92. __le16 name[];
  93. };
  94. struct OPEN_ATTR_ENRTY {
  95. __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
  96. __le32 bytes_per_index; // 0x04:
  97. enum ATTR_TYPE type; // 0x08:
  98. u8 is_dirty_pages; // 0x0C:
  99. u8 is_attr_name; // 0x0B: Faked field to manage 'ptr'
  100. u8 name_len; // 0x0C: Faked field to manage 'ptr'
  101. u8 res;
  102. struct MFT_REF ref; // 0x10: File Reference of file containing attribute
  103. __le64 open_record_lsn; // 0x18:
  104. void *ptr; // 0x20:
  105. };
  106. /* 32 bit version of 'struct OPEN_ATTR_ENRTY' */
  107. struct OPEN_ATTR_ENRTY_32 {
  108. __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
  109. __le32 ptr; // 0x04:
  110. struct MFT_REF ref; // 0x08:
  111. __le64 open_record_lsn; // 0x10:
  112. u8 is_dirty_pages; // 0x18:
  113. u8 is_attr_name; // 0x19:
  114. u8 res1[2];
  115. enum ATTR_TYPE type; // 0x1C:
  116. u8 name_len; // 0x20: In wchar
  117. u8 res2[3];
  118. __le32 AttributeName; // 0x24:
  119. __le32 bytes_per_index; // 0x28:
  120. };
  121. #define SIZEOF_OPENATTRIBUTEENTRY0 0x2c
  122. // static_assert( 0x2C == sizeof(struct OPEN_ATTR_ENRTY_32) );
  123. static_assert(sizeof(struct OPEN_ATTR_ENRTY) < SIZEOF_OPENATTRIBUTEENTRY0);
  124. /*
  125. * One entry exists in the Dirty Pages Table for each page which is dirty at
  126. * the time the Restart Area is written.
  127. */
  128. struct DIR_PAGE_ENTRY {
  129. __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
  130. __le32 target_attr; // 0x04: Index into the Open attribute Table
  131. __le32 transfer_len; // 0x08:
  132. __le32 lcns_follow; // 0x0C:
  133. __le64 vcn; // 0x10: Vcn of dirty page
  134. __le64 oldest_lsn; // 0x18:
  135. __le64 page_lcns[]; // 0x20:
  136. };
  137. static_assert(sizeof(struct DIR_PAGE_ENTRY) == 0x20);
  138. /* 32 bit version of 'struct DIR_PAGE_ENTRY' */
  139. struct DIR_PAGE_ENTRY_32 {
  140. __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
  141. __le32 target_attr; // 0x04: Index into the Open attribute Table
  142. __le32 transfer_len; // 0x08:
  143. __le32 lcns_follow; // 0x0C:
  144. __le32 reserved; // 0x10:
  145. __le32 vcn_low; // 0x14: Vcn of dirty page
  146. __le32 vcn_hi; // 0x18: Vcn of dirty page
  147. __le32 oldest_lsn_low; // 0x1C:
  148. __le32 oldest_lsn_hi; // 0x1C:
  149. __le32 page_lcns_low; // 0x24:
  150. __le32 page_lcns_hi; // 0x24:
  151. };
  152. static_assert(offsetof(struct DIR_PAGE_ENTRY_32, vcn_low) == 0x14);
  153. static_assert(sizeof(struct DIR_PAGE_ENTRY_32) == 0x2c);
  154. enum transact_state {
  155. TransactionUninitialized = 0,
  156. TransactionActive,
  157. TransactionPrepared,
  158. TransactionCommitted
  159. };
  160. struct TRANSACTION_ENTRY {
  161. __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
  162. u8 transact_state; // 0x04:
  163. u8 reserved[3]; // 0x05:
  164. __le64 first_lsn; // 0x08:
  165. __le64 prev_lsn; // 0x10:
  166. __le64 undo_next_lsn; // 0x18:
  167. __le32 undo_records; // 0x20: Number of undo log records pending abort
  168. __le32 undo_len; // 0x24: Total undo size
  169. };
  170. static_assert(sizeof(struct TRANSACTION_ENTRY) == 0x28);
  171. struct NTFS_RESTART {
  172. __le32 major_ver; // 0x00:
  173. __le32 minor_ver; // 0x04:
  174. __le64 check_point_start; // 0x08:
  175. __le64 open_attr_table_lsn; // 0x10:
  176. __le64 attr_names_lsn; // 0x18:
  177. __le64 dirty_pages_table_lsn; // 0x20:
  178. __le64 transact_table_lsn; // 0x28:
  179. __le32 open_attr_len; // 0x30: In bytes
  180. __le32 attr_names_len; // 0x34: In bytes
  181. __le32 dirty_pages_len; // 0x38: In bytes
  182. __le32 transact_table_len; // 0x3C: In bytes
  183. };
  184. static_assert(sizeof(struct NTFS_RESTART) == 0x40);
  185. struct NEW_ATTRIBUTE_SIZES {
  186. __le64 alloc_size;
  187. __le64 valid_size;
  188. __le64 data_size;
  189. __le64 total_size;
  190. };
  191. struct BITMAP_RANGE {
  192. __le32 bitmap_off;
  193. __le32 bits;
  194. };
  195. struct LCN_RANGE {
  196. __le64 lcn;
  197. __le64 len;
  198. };
  199. /* The following type defines the different log record types. */
  200. #define LfsClientRecord cpu_to_le32(1)
  201. #define LfsClientRestart cpu_to_le32(2)
  202. /* This is used to uniquely identify a client for a particular log file. */
  203. struct CLIENT_ID {
  204. __le16 seq_num;
  205. __le16 client_idx;
  206. };
  207. /* This is the header that begins every Log Record in the log file. */
  208. struct LFS_RECORD_HDR {
  209. __le64 this_lsn; // 0x00:
  210. __le64 client_prev_lsn; // 0x08:
  211. __le64 client_undo_next_lsn; // 0x10:
  212. __le32 client_data_len; // 0x18:
  213. struct CLIENT_ID client; // 0x1C: Owner of this log record.
  214. __le32 record_type; // 0x20: LfsClientRecord or LfsClientRestart.
  215. __le32 transact_id; // 0x24:
  216. __le16 flags; // 0x28: LOG_RECORD_MULTI_PAGE
  217. u8 align[6]; // 0x2A:
  218. };
  219. #define LOG_RECORD_MULTI_PAGE cpu_to_le16(1)
  220. static_assert(sizeof(struct LFS_RECORD_HDR) == 0x30);
  221. struct LFS_RECORD {
  222. __le16 next_record_off; // 0x00: Offset of the free space in the page,
  223. u8 align[6]; // 0x02:
  224. __le64 last_end_lsn; // 0x08: lsn for the last log record which ends on the page,
  225. };
  226. static_assert(sizeof(struct LFS_RECORD) == 0x10);
  227. struct RECORD_PAGE_HDR {
  228. struct NTFS_RECORD_HEADER rhdr; // 'RCRD'
  229. __le32 rflags; // 0x10: See LOG_PAGE_LOG_RECORD_END
  230. __le16 page_count; // 0x14:
  231. __le16 page_pos; // 0x16:
  232. struct LFS_RECORD record_hdr; // 0x18:
  233. __le16 fixups[10]; // 0x28:
  234. __le32 file_off; // 0x3c: Used when major version >= 2
  235. };
  236. // clang-format on
  237. // Page contains the end of a log record.
  238. #define LOG_PAGE_LOG_RECORD_END cpu_to_le32(0x00000001)
  239. static inline bool is_log_record_end(const struct RECORD_PAGE_HDR *hdr)
  240. {
  241. return hdr->rflags & LOG_PAGE_LOG_RECORD_END;
  242. }
  243. static_assert(offsetof(struct RECORD_PAGE_HDR, file_off) == 0x3c);
  244. /*
  245. * END of NTFS LOG structures
  246. */
  247. /* Define some tuning parameters to keep the restart tables a reasonable size. */
  248. #define INITIAL_NUMBER_TRANSACTIONS 5
  249. enum NTFS_LOG_OPERATION {
  250. Noop = 0x00,
  251. CompensationLogRecord = 0x01,
  252. InitializeFileRecordSegment = 0x02,
  253. DeallocateFileRecordSegment = 0x03,
  254. WriteEndOfFileRecordSegment = 0x04,
  255. CreateAttribute = 0x05,
  256. DeleteAttribute = 0x06,
  257. UpdateResidentValue = 0x07,
  258. UpdateNonresidentValue = 0x08,
  259. UpdateMappingPairs = 0x09,
  260. DeleteDirtyClusters = 0x0A,
  261. SetNewAttributeSizes = 0x0B,
  262. AddIndexEntryRoot = 0x0C,
  263. DeleteIndexEntryRoot = 0x0D,
  264. AddIndexEntryAllocation = 0x0E,
  265. DeleteIndexEntryAllocation = 0x0F,
  266. WriteEndOfIndexBuffer = 0x10,
  267. SetIndexEntryVcnRoot = 0x11,
  268. SetIndexEntryVcnAllocation = 0x12,
  269. UpdateFileNameRoot = 0x13,
  270. UpdateFileNameAllocation = 0x14,
  271. SetBitsInNonresidentBitMap = 0x15,
  272. ClearBitsInNonresidentBitMap = 0x16,
  273. HotFix = 0x17,
  274. EndTopLevelAction = 0x18,
  275. PrepareTransaction = 0x19,
  276. CommitTransaction = 0x1A,
  277. ForgetTransaction = 0x1B,
  278. OpenNonresidentAttribute = 0x1C,
  279. OpenAttributeTableDump = 0x1D,
  280. AttributeNamesDump = 0x1E,
  281. DirtyPageTableDump = 0x1F,
  282. TransactionTableDump = 0x20,
  283. UpdateRecordDataRoot = 0x21,
  284. UpdateRecordDataAllocation = 0x22,
  285. UpdateRelativeDataInIndex =
  286. 0x23, // NtOfsRestartUpdateRelativeDataInIndex
  287. UpdateRelativeDataInIndex2 = 0x24,
  288. ZeroEndOfFileRecord = 0x25,
  289. };
  290. /*
  291. * Array for log records which require a target attribute.
  292. * A true indicates that the corresponding restart operation
  293. * requires a target attribute.
  294. */
  295. static const u8 AttributeRequired[] = {
  296. 0xFC, 0xFB, 0xFF, 0x10, 0x06,
  297. };
  298. static inline bool is_target_required(u16 op)
  299. {
  300. bool ret = op <= UpdateRecordDataAllocation &&
  301. (AttributeRequired[op >> 3] >> (op & 7) & 1);
  302. return ret;
  303. }
  304. static inline bool can_skip_action(enum NTFS_LOG_OPERATION op)
  305. {
  306. switch (op) {
  307. case Noop:
  308. case DeleteDirtyClusters:
  309. case HotFix:
  310. case EndTopLevelAction:
  311. case PrepareTransaction:
  312. case CommitTransaction:
  313. case ForgetTransaction:
  314. case CompensationLogRecord:
  315. case OpenNonresidentAttribute:
  316. case OpenAttributeTableDump:
  317. case AttributeNamesDump:
  318. case DirtyPageTableDump:
  319. case TransactionTableDump:
  320. return true;
  321. default:
  322. return false;
  323. }
  324. }
  325. enum { lcb_ctx_undo_next, lcb_ctx_prev, lcb_ctx_next };
  326. /* Bytes per restart table. */
  327. static inline u32 bytes_per_rt(const struct RESTART_TABLE *rt)
  328. {
  329. return le16_to_cpu(rt->used) * le16_to_cpu(rt->size) +
  330. sizeof(struct RESTART_TABLE);
  331. }
  332. /* Log record length. */
  333. static inline u32 lrh_length(const struct LOG_REC_HDR *lr)
  334. {
  335. u16 t16 = le16_to_cpu(lr->lcns_follow);
  336. return struct_size(lr, page_lcns, max_t(u16, 1, t16));
  337. }
  338. struct lcb {
  339. struct LFS_RECORD_HDR *lrh; // Log record header of the current lsn.
  340. struct LOG_REC_HDR *log_rec;
  341. u32 ctx_mode; // lcb_ctx_undo_next/lcb_ctx_prev/lcb_ctx_next
  342. struct CLIENT_ID client;
  343. bool alloc; // If true the we should deallocate 'log_rec'.
  344. };
  345. static void lcb_put(struct lcb *lcb)
  346. {
  347. if (lcb->alloc)
  348. kfree(lcb->log_rec);
  349. kfree(lcb->lrh);
  350. kfree(lcb);
  351. }
  352. /* Find the oldest lsn from active clients. */
  353. static inline void oldest_client_lsn(const struct CLIENT_REC *ca,
  354. __le16 next_client, u64 *oldest_lsn)
  355. {
  356. while (next_client != LFS_NO_CLIENT_LE) {
  357. const struct CLIENT_REC *cr = ca + le16_to_cpu(next_client);
  358. u64 lsn = le64_to_cpu(cr->oldest_lsn);
  359. /* Ignore this block if it's oldest lsn is 0. */
  360. if (lsn && lsn < *oldest_lsn)
  361. *oldest_lsn = lsn;
  362. next_client = cr->next_client;
  363. }
  364. }
  365. static inline bool is_rst_page_hdr_valid(u32 file_off,
  366. const struct RESTART_HDR *rhdr)
  367. {
  368. u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
  369. u32 page_size = le32_to_cpu(rhdr->page_size);
  370. u32 end_usa;
  371. u16 ro;
  372. if (sys_page < SECTOR_SIZE || page_size < SECTOR_SIZE ||
  373. sys_page & (sys_page - 1) || page_size & (page_size - 1)) {
  374. return false;
  375. }
  376. /* Check that if the file offset isn't 0, it is the system page size. */
  377. if (file_off && file_off != sys_page)
  378. return false;
  379. /* Check support version 1.1+. */
  380. if (le16_to_cpu(rhdr->major_ver) <= 1 && !rhdr->minor_ver)
  381. return false;
  382. if (le16_to_cpu(rhdr->major_ver) > 2)
  383. return false;
  384. ro = le16_to_cpu(rhdr->ra_off);
  385. if (!IS_ALIGNED(ro, 8) || ro > sys_page)
  386. return false;
  387. end_usa = ((sys_page >> SECTOR_SHIFT) + 1) * sizeof(short);
  388. end_usa += le16_to_cpu(rhdr->rhdr.fix_off);
  389. if (ro < end_usa)
  390. return false;
  391. return true;
  392. }
  393. static inline bool is_rst_area_valid(const struct RESTART_HDR *rhdr)
  394. {
  395. const struct RESTART_AREA *ra;
  396. u16 cl, fl, ul;
  397. u32 off, l_size, seq_bits;
  398. u16 ro = le16_to_cpu(rhdr->ra_off);
  399. u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
  400. if (ro + offsetof(struct RESTART_AREA, l_size) >
  401. SECTOR_SIZE - sizeof(short))
  402. return false;
  403. ra = Add2Ptr(rhdr, ro);
  404. cl = le16_to_cpu(ra->log_clients);
  405. if (cl > 1)
  406. return false;
  407. off = le16_to_cpu(ra->client_off);
  408. if (!IS_ALIGNED(off, 8) || ro + off > SECTOR_SIZE - sizeof(short))
  409. return false;
  410. off += cl * sizeof(struct CLIENT_REC);
  411. if (off > sys_page)
  412. return false;
  413. /*
  414. * Check the restart length field and whether the entire
  415. * restart area is contained that length.
  416. */
  417. if (le16_to_cpu(rhdr->ra_off) + le16_to_cpu(ra->ra_len) > sys_page ||
  418. off > le16_to_cpu(ra->ra_len)) {
  419. return false;
  420. }
  421. /*
  422. * As a final check make sure that the use list and the free list
  423. * are either empty or point to a valid client.
  424. */
  425. fl = le16_to_cpu(ra->client_idx[0]);
  426. ul = le16_to_cpu(ra->client_idx[1]);
  427. if ((fl != LFS_NO_CLIENT && fl >= cl) ||
  428. (ul != LFS_NO_CLIENT && ul >= cl))
  429. return false;
  430. /* Make sure the sequence number bits match the log file size. */
  431. l_size = le64_to_cpu(ra->l_size);
  432. seq_bits = sizeof(u64) * 8 + 3;
  433. while (l_size) {
  434. l_size >>= 1;
  435. seq_bits -= 1;
  436. }
  437. if (seq_bits != le32_to_cpu(ra->seq_num_bits))
  438. return false;
  439. /* The log page data offset and record header length must be quad-aligned. */
  440. if (!IS_ALIGNED(le16_to_cpu(ra->data_off), 8) ||
  441. !IS_ALIGNED(le16_to_cpu(ra->rec_hdr_len), 8))
  442. return false;
  443. return true;
  444. }
  445. static inline bool is_client_area_valid(const struct RESTART_HDR *rhdr,
  446. bool usa_error)
  447. {
  448. u16 ro = le16_to_cpu(rhdr->ra_off);
  449. const struct RESTART_AREA *ra = Add2Ptr(rhdr, ro);
  450. u16 ra_len = le16_to_cpu(ra->ra_len);
  451. const struct CLIENT_REC *ca;
  452. u32 i;
  453. if (usa_error && ra_len + ro > SECTOR_SIZE - sizeof(short))
  454. return false;
  455. /* Find the start of the client array. */
  456. ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
  457. /*
  458. * Start with the free list.
  459. * Check that all the clients are valid and that there isn't a cycle.
  460. * Do the in-use list on the second pass.
  461. */
  462. for (i = 0; i < 2; i++) {
  463. u16 client_idx = le16_to_cpu(ra->client_idx[i]);
  464. bool first_client = true;
  465. u16 clients = le16_to_cpu(ra->log_clients);
  466. while (client_idx != LFS_NO_CLIENT) {
  467. const struct CLIENT_REC *cr;
  468. if (!clients ||
  469. client_idx >= le16_to_cpu(ra->log_clients))
  470. return false;
  471. clients -= 1;
  472. cr = ca + client_idx;
  473. client_idx = le16_to_cpu(cr->next_client);
  474. if (first_client) {
  475. first_client = false;
  476. if (cr->prev_client != LFS_NO_CLIENT_LE)
  477. return false;
  478. }
  479. }
  480. }
  481. return true;
  482. }
  483. /*
  484. * remove_client
  485. *
  486. * Remove a client record from a client record list an restart area.
  487. */
  488. static inline void remove_client(struct CLIENT_REC *ca,
  489. const struct CLIENT_REC *cr, __le16 *head)
  490. {
  491. if (cr->prev_client == LFS_NO_CLIENT_LE)
  492. *head = cr->next_client;
  493. else
  494. ca[le16_to_cpu(cr->prev_client)].next_client = cr->next_client;
  495. if (cr->next_client != LFS_NO_CLIENT_LE)
  496. ca[le16_to_cpu(cr->next_client)].prev_client = cr->prev_client;
  497. }
  498. /*
  499. * add_client - Add a client record to the start of a list.
  500. */
  501. static inline void add_client(struct CLIENT_REC *ca, u16 index, __le16 *head)
  502. {
  503. struct CLIENT_REC *cr = ca + index;
  504. cr->prev_client = LFS_NO_CLIENT_LE;
  505. cr->next_client = *head;
  506. if (*head != LFS_NO_CLIENT_LE)
  507. ca[le16_to_cpu(*head)].prev_client = cpu_to_le16(index);
  508. *head = cpu_to_le16(index);
  509. }
  510. /*
  511. * Enumerate restart table.
  512. *
  513. * @t - table to enumerate.
  514. * @c - current enumerated element.
  515. *
  516. * enumeration starts with @c == NULL
  517. * returns next element or NULL
  518. */
  519. static inline void *enum_rstbl(struct RESTART_TABLE *t, void *c)
  520. {
  521. __le32 *e;
  522. u32 bprt;
  523. u16 rsize;
  524. if (!t)
  525. return NULL;
  526. rsize = le16_to_cpu(t->size);
  527. if (!c) {
  528. /* start enumeration. */
  529. if (!t->total)
  530. return NULL;
  531. e = Add2Ptr(t, sizeof(struct RESTART_TABLE));
  532. } else {
  533. e = Add2Ptr(c, rsize);
  534. }
  535. /* Loop until we hit the first one allocated, or the end of the list. */
  536. for (bprt = bytes_per_rt(t); PtrOffset(t, e) < bprt;
  537. e = Add2Ptr(e, rsize)) {
  538. if (*e == RESTART_ENTRY_ALLOCATED_LE)
  539. return e;
  540. }
  541. return NULL;
  542. }
  543. /*
  544. * find_dp - Search for a @vcn in Dirty Page Table.
  545. */
  546. static inline struct DIR_PAGE_ENTRY *find_dp(struct RESTART_TABLE *dptbl,
  547. u32 target_attr, u64 vcn)
  548. {
  549. __le32 ta = cpu_to_le32(target_attr);
  550. struct DIR_PAGE_ENTRY *dp = NULL;
  551. while ((dp = enum_rstbl(dptbl, dp))) {
  552. u64 dp_vcn = le64_to_cpu(dp->vcn);
  553. if (dp->target_attr == ta && vcn >= dp_vcn &&
  554. vcn < dp_vcn + le32_to_cpu(dp->lcns_follow)) {
  555. return dp;
  556. }
  557. }
  558. return NULL;
  559. }
  560. static inline u32 norm_file_page(u32 page_size, u32 *l_size, bool use_default)
  561. {
  562. if (use_default)
  563. page_size = DefaultLogPageSize;
  564. /* Round the file size down to a system page boundary. */
  565. *l_size &= ~(page_size - 1);
  566. /* File should contain at least 2 restart pages and MinLogRecordPages pages. */
  567. if (*l_size < (MinLogRecordPages + 2) * page_size)
  568. return 0;
  569. return page_size;
  570. }
  571. static bool check_log_rec(const struct LOG_REC_HDR *lr, u32 bytes, u32 tr,
  572. u32 bytes_per_attr_entry)
  573. {
  574. u16 t16;
  575. if (bytes < sizeof(struct LOG_REC_HDR))
  576. return false;
  577. if (!tr)
  578. return false;
  579. if ((tr - sizeof(struct RESTART_TABLE)) %
  580. sizeof(struct TRANSACTION_ENTRY))
  581. return false;
  582. if (le16_to_cpu(lr->redo_off) & 7)
  583. return false;
  584. if (le16_to_cpu(lr->undo_off) & 7)
  585. return false;
  586. if (lr->target_attr)
  587. goto check_lcns;
  588. if (is_target_required(le16_to_cpu(lr->redo_op)))
  589. return false;
  590. if (is_target_required(le16_to_cpu(lr->undo_op)))
  591. return false;
  592. check_lcns:
  593. if (!lr->lcns_follow)
  594. goto check_length;
  595. t16 = le16_to_cpu(lr->target_attr);
  596. if ((t16 - sizeof(struct RESTART_TABLE)) % bytes_per_attr_entry)
  597. return false;
  598. check_length:
  599. if (bytes < lrh_length(lr))
  600. return false;
  601. return true;
  602. }
  603. static bool check_rstbl(const struct RESTART_TABLE *rt, size_t bytes)
  604. {
  605. u32 ts;
  606. u32 i, off;
  607. u16 rsize = le16_to_cpu(rt->size);
  608. u16 ne = le16_to_cpu(rt->used);
  609. u32 ff = le32_to_cpu(rt->first_free);
  610. u32 lf = le32_to_cpu(rt->last_free);
  611. ts = rsize * ne + sizeof(struct RESTART_TABLE);
  612. if (!rsize || rsize > bytes ||
  613. rsize + sizeof(struct RESTART_TABLE) > bytes || bytes < ts ||
  614. le16_to_cpu(rt->total) > ne || ff > ts - sizeof(__le32) ||
  615. lf > ts - sizeof(__le32) ||
  616. (ff && ff < sizeof(struct RESTART_TABLE)) ||
  617. (lf && lf < sizeof(struct RESTART_TABLE))) {
  618. return false;
  619. }
  620. /*
  621. * Verify each entry is either allocated or points
  622. * to a valid offset the table.
  623. */
  624. for (i = 0; i < ne; i++) {
  625. off = le32_to_cpu(*(__le32 *)Add2Ptr(
  626. rt, i * rsize + sizeof(struct RESTART_TABLE)));
  627. if (off != RESTART_ENTRY_ALLOCATED && off &&
  628. (off < sizeof(struct RESTART_TABLE) ||
  629. ((off - sizeof(struct RESTART_TABLE)) % rsize))) {
  630. return false;
  631. }
  632. }
  633. /*
  634. * Walk through the list headed by the first entry to make
  635. * sure none of the entries are currently being used.
  636. */
  637. for (off = ff; off;) {
  638. if (off == RESTART_ENTRY_ALLOCATED)
  639. return false;
  640. off = le32_to_cpu(*(__le32 *)Add2Ptr(rt, off));
  641. if (off > ts - sizeof(__le32))
  642. return false;
  643. }
  644. return true;
  645. }
  646. /*
  647. * free_rsttbl_idx - Free a previously allocated index a Restart Table.
  648. */
  649. static inline void free_rsttbl_idx(struct RESTART_TABLE *rt, u32 off)
  650. {
  651. __le32 *e;
  652. u32 lf = le32_to_cpu(rt->last_free);
  653. __le32 off_le = cpu_to_le32(off);
  654. e = Add2Ptr(rt, off);
  655. if (off < le32_to_cpu(rt->free_goal)) {
  656. *e = rt->first_free;
  657. rt->first_free = off_le;
  658. if (!lf)
  659. rt->last_free = off_le;
  660. } else {
  661. if (lf)
  662. *(__le32 *)Add2Ptr(rt, lf) = off_le;
  663. else
  664. rt->first_free = off_le;
  665. rt->last_free = off_le;
  666. *e = 0;
  667. }
  668. le16_sub_cpu(&rt->total, 1);
  669. }
  670. static inline struct RESTART_TABLE *init_rsttbl(u16 esize, u16 used)
  671. {
  672. __le32 *e, *last_free;
  673. u32 off;
  674. u32 bytes = esize * used + sizeof(struct RESTART_TABLE);
  675. u32 lf = sizeof(struct RESTART_TABLE) + (used - 1) * esize;
  676. struct RESTART_TABLE *t = kzalloc(bytes, GFP_NOFS);
  677. if (!t)
  678. return NULL;
  679. t->size = cpu_to_le16(esize);
  680. t->used = cpu_to_le16(used);
  681. t->free_goal = cpu_to_le32(~0u);
  682. t->first_free = cpu_to_le32(sizeof(struct RESTART_TABLE));
  683. t->last_free = cpu_to_le32(lf);
  684. e = (__le32 *)(t + 1);
  685. last_free = Add2Ptr(t, lf);
  686. for (off = sizeof(struct RESTART_TABLE) + esize; e < last_free;
  687. e = Add2Ptr(e, esize), off += esize) {
  688. *e = cpu_to_le32(off);
  689. }
  690. return t;
  691. }
  692. static inline struct RESTART_TABLE *extend_rsttbl(struct RESTART_TABLE *tbl,
  693. u32 add, u32 free_goal)
  694. {
  695. u16 esize = le16_to_cpu(tbl->size);
  696. __le32 osize = cpu_to_le32(bytes_per_rt(tbl));
  697. u32 used = le16_to_cpu(tbl->used);
  698. struct RESTART_TABLE *rt;
  699. rt = init_rsttbl(esize, used + add);
  700. if (!rt)
  701. return NULL;
  702. memcpy(rt + 1, tbl + 1, esize * used);
  703. rt->free_goal = free_goal == ~0u ?
  704. cpu_to_le32(~0u) :
  705. cpu_to_le32(sizeof(struct RESTART_TABLE) +
  706. free_goal * esize);
  707. if (tbl->first_free) {
  708. rt->first_free = tbl->first_free;
  709. *(__le32 *)Add2Ptr(rt, le32_to_cpu(tbl->last_free)) = osize;
  710. } else {
  711. rt->first_free = osize;
  712. }
  713. rt->total = tbl->total;
  714. kfree(tbl);
  715. return rt;
  716. }
  717. /*
  718. * alloc_rsttbl_idx
  719. *
  720. * Allocate an index from within a previously initialized Restart Table.
  721. */
  722. static inline void *alloc_rsttbl_idx(struct RESTART_TABLE **tbl)
  723. {
  724. u32 off;
  725. __le32 *e;
  726. struct RESTART_TABLE *t = *tbl;
  727. if (!t->first_free) {
  728. *tbl = t = extend_rsttbl(t, 16, ~0u);
  729. if (!t)
  730. return NULL;
  731. }
  732. off = le32_to_cpu(t->first_free);
  733. /* Dequeue this entry and zero it. */
  734. e = Add2Ptr(t, off);
  735. t->first_free = *e;
  736. memset(e, 0, le16_to_cpu(t->size));
  737. *e = RESTART_ENTRY_ALLOCATED_LE;
  738. /* If list is going empty, then we fix the last_free as well. */
  739. if (!t->first_free)
  740. t->last_free = 0;
  741. le16_add_cpu(&t->total, 1);
  742. return Add2Ptr(t, off);
  743. }
  744. /*
  745. * alloc_rsttbl_from_idx
  746. *
  747. * Allocate a specific index from within a previously initialized Restart Table.
  748. */
  749. static inline void *alloc_rsttbl_from_idx(struct RESTART_TABLE **tbl, u32 vbo)
  750. {
  751. u32 off;
  752. __le32 *e;
  753. struct RESTART_TABLE *rt = *tbl;
  754. u32 bytes = bytes_per_rt(rt);
  755. u16 esize = le16_to_cpu(rt->size);
  756. /* If the entry is not the table, we will have to extend the table. */
  757. if (vbo >= bytes) {
  758. /*
  759. * Extend the size by computing the number of entries between
  760. * the existing size and the desired index and adding 1 to that.
  761. */
  762. u32 bytes2idx = vbo - bytes;
  763. /*
  764. * There should always be an integral number of entries
  765. * being added. Now extend the table.
  766. */
  767. *tbl = rt = extend_rsttbl(rt, bytes2idx / esize + 1, bytes);
  768. if (!rt)
  769. return NULL;
  770. }
  771. /* See if the entry is already allocated, and just return if it is. */
  772. e = Add2Ptr(rt, vbo);
  773. if (*e == RESTART_ENTRY_ALLOCATED_LE)
  774. return e;
  775. /*
  776. * Walk through the table, looking for the entry we're
  777. * interested and the previous entry.
  778. */
  779. off = le32_to_cpu(rt->first_free);
  780. e = Add2Ptr(rt, off);
  781. if (off == vbo) {
  782. /* this is a match */
  783. rt->first_free = *e;
  784. goto skip_looking;
  785. }
  786. /*
  787. * Need to walk through the list looking for the predecessor
  788. * of our entry.
  789. */
  790. for (;;) {
  791. /* Remember the entry just found */
  792. u32 last_off = off;
  793. __le32 *last_e = e;
  794. /* Should never run of entries. */
  795. /* Lookup up the next entry the list. */
  796. off = le32_to_cpu(*last_e);
  797. e = Add2Ptr(rt, off);
  798. /* If this is our match we are done. */
  799. if (off == vbo) {
  800. *last_e = *e;
  801. /*
  802. * If this was the last entry, we update that
  803. * table as well.
  804. */
  805. if (le32_to_cpu(rt->last_free) == off)
  806. rt->last_free = cpu_to_le32(last_off);
  807. break;
  808. }
  809. }
  810. skip_looking:
  811. /* If the list is now empty, we fix the last_free as well. */
  812. if (!rt->first_free)
  813. rt->last_free = 0;
  814. /* Zero this entry. */
  815. memset(e, 0, esize);
  816. *e = RESTART_ENTRY_ALLOCATED_LE;
  817. le16_add_cpu(&rt->total, 1);
  818. return e;
  819. }
  820. struct restart_info {
  821. u64 last_lsn;
  822. struct RESTART_HDR *r_page;
  823. u32 vbo;
  824. bool chkdsk_was_run;
  825. bool valid_page;
  826. bool initialized;
  827. bool restart;
  828. };
  829. #define RESTART_SINGLE_PAGE_IO cpu_to_le16(0x0001)
  830. #define NTFSLOG_WRAPPED 0x00000001
  831. #define NTFSLOG_MULTIPLE_PAGE_IO 0x00000002
  832. #define NTFSLOG_NO_LAST_LSN 0x00000004
  833. #define NTFSLOG_REUSE_TAIL 0x00000010
  834. #define NTFSLOG_NO_OLDEST_LSN 0x00000020
  835. /* Helper struct to work with NTFS $LogFile. */
  836. struct ntfs_log {
  837. struct ntfs_inode *ni;
  838. u32 l_size;
  839. u32 orig_file_size;
  840. u32 sys_page_size;
  841. u32 sys_page_mask;
  842. u32 page_size;
  843. u32 page_mask; // page_size - 1
  844. u8 page_bits;
  845. struct RECORD_PAGE_HDR *one_page_buf;
  846. struct RESTART_TABLE *open_attr_tbl;
  847. u32 transaction_id;
  848. u32 clst_per_page;
  849. u32 first_page;
  850. u32 next_page;
  851. u32 ra_off;
  852. u32 data_off;
  853. u32 restart_size;
  854. u32 data_size;
  855. u16 record_header_len;
  856. u64 seq_num;
  857. u32 seq_num_bits;
  858. u32 file_data_bits;
  859. u32 seq_num_mask; /* (1 << file_data_bits) - 1 */
  860. struct RESTART_AREA *ra; /* In-memory image of the next restart area. */
  861. u32 ra_size; /* The usable size of the restart area. */
  862. /*
  863. * If true, then the in-memory restart area is to be written
  864. * to the first position on the disk.
  865. */
  866. bool init_ra;
  867. bool set_dirty; /* True if we need to set dirty flag. */
  868. u64 oldest_lsn;
  869. u32 oldest_lsn_off;
  870. u64 last_lsn;
  871. u32 total_avail;
  872. u32 total_avail_pages;
  873. u32 total_undo_commit;
  874. u32 max_current_avail;
  875. u32 current_avail;
  876. u32 reserved;
  877. short major_ver;
  878. short minor_ver;
  879. u32 l_flags; /* See NTFSLOG_XXX */
  880. u32 current_openlog_count; /* On-disk value for open_log_count. */
  881. struct CLIENT_ID client_id;
  882. u32 client_undo_commit;
  883. struct restart_info rst_info, rst_info2;
  884. };
  885. static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn)
  886. {
  887. u32 vbo = (lsn << log->seq_num_bits) >> (log->seq_num_bits - 3);
  888. return vbo;
  889. }
  890. /* Compute the offset in the log file of the next log page. */
  891. static inline u32 next_page_off(struct ntfs_log *log, u32 off)
  892. {
  893. off = (off & ~log->sys_page_mask) + log->page_size;
  894. return off >= log->l_size ? log->first_page : off;
  895. }
  896. static inline u32 lsn_to_page_off(struct ntfs_log *log, u64 lsn)
  897. {
  898. return (((u32)lsn) << 3) & log->page_mask;
  899. }
  900. static inline u64 vbo_to_lsn(struct ntfs_log *log, u32 off, u64 Seq)
  901. {
  902. return (off >> 3) + (Seq << log->file_data_bits);
  903. }
  904. static inline bool is_lsn_in_file(struct ntfs_log *log, u64 lsn)
  905. {
  906. return lsn >= log->oldest_lsn &&
  907. lsn <= le64_to_cpu(log->ra->current_lsn);
  908. }
  909. static inline u32 hdr_file_off(struct ntfs_log *log,
  910. struct RECORD_PAGE_HDR *hdr)
  911. {
  912. if (log->major_ver < 2)
  913. return le64_to_cpu(hdr->rhdr.lsn);
  914. return le32_to_cpu(hdr->file_off);
  915. }
  916. static inline u64 base_lsn(struct ntfs_log *log,
  917. const struct RECORD_PAGE_HDR *hdr, u64 lsn)
  918. {
  919. u64 h_lsn = le64_to_cpu(hdr->rhdr.lsn);
  920. u64 ret = (((h_lsn >> log->file_data_bits) +
  921. (lsn < (lsn_to_vbo(log, h_lsn) & ~log->page_mask) ? 1 : 0))
  922. << log->file_data_bits) +
  923. ((((is_log_record_end(hdr) &&
  924. h_lsn <= le64_to_cpu(hdr->record_hdr.last_end_lsn)) ?
  925. le16_to_cpu(hdr->record_hdr.next_record_off) :
  926. log->page_size) +
  927. lsn) >>
  928. 3);
  929. return ret;
  930. }
  931. static inline bool verify_client_lsn(struct ntfs_log *log,
  932. const struct CLIENT_REC *client, u64 lsn)
  933. {
  934. return lsn >= le64_to_cpu(client->oldest_lsn) &&
  935. lsn <= le64_to_cpu(log->ra->current_lsn) && lsn;
  936. }
  937. static int read_log_page(struct ntfs_log *log, u32 vbo,
  938. struct RECORD_PAGE_HDR **buffer, bool *usa_error)
  939. {
  940. int err = 0;
  941. u32 page_idx = vbo >> log->page_bits;
  942. u32 page_off = vbo & log->page_mask;
  943. u32 bytes = log->page_size - page_off;
  944. void *to_free = NULL;
  945. u32 page_vbo = page_idx << log->page_bits;
  946. struct RECORD_PAGE_HDR *page_buf;
  947. struct ntfs_inode *ni = log->ni;
  948. bool bBAAD;
  949. if (vbo >= log->l_size)
  950. return -EINVAL;
  951. if (!*buffer) {
  952. to_free = kmalloc(log->page_size, GFP_NOFS);
  953. if (!to_free)
  954. return -ENOMEM;
  955. *buffer = to_free;
  956. }
  957. page_buf = page_off ? log->one_page_buf : *buffer;
  958. err = ntfs_read_run_nb(ni->mi.sbi, &ni->file.run, page_vbo, page_buf,
  959. log->page_size, NULL);
  960. if (err)
  961. goto out;
  962. if (page_buf->rhdr.sign != NTFS_FFFF_SIGNATURE)
  963. ntfs_fix_post_read(&page_buf->rhdr, PAGE_SIZE, false);
  964. if (page_buf != *buffer)
  965. memcpy(*buffer, Add2Ptr(page_buf, page_off), bytes);
  966. bBAAD = page_buf->rhdr.sign == NTFS_BAAD_SIGNATURE;
  967. if (usa_error)
  968. *usa_error = bBAAD;
  969. /* Check that the update sequence array for this page is valid */
  970. /* If we don't allow errors, raise an error status */
  971. else if (bBAAD)
  972. err = -EINVAL;
  973. out:
  974. if (err && to_free) {
  975. kfree(to_free);
  976. *buffer = NULL;
  977. }
  978. return err;
  979. }
  980. /*
  981. * log_read_rst
  982. *
  983. * It walks through 512 blocks of the file looking for a valid
  984. * restart page header. It will stop the first time we find a
  985. * valid page header.
  986. */
  987. static int log_read_rst(struct ntfs_log *log, bool first,
  988. struct restart_info *info)
  989. {
  990. u32 skip;
  991. u64 vbo;
  992. struct RESTART_HDR *r_page = NULL;
  993. /* Determine which restart area we are looking for. */
  994. if (first) {
  995. vbo = 0;
  996. skip = 512;
  997. } else {
  998. vbo = 512;
  999. skip = 0;
  1000. }
  1001. /* Loop continuously until we succeed. */
  1002. for (; vbo < log->l_size; vbo = 2 * vbo + skip, skip = 0) {
  1003. bool usa_error;
  1004. bool brst, bchk;
  1005. struct RESTART_AREA *ra;
  1006. /* Read a page header at the current offset. */
  1007. if (read_log_page(log, vbo, (struct RECORD_PAGE_HDR **)&r_page,
  1008. &usa_error)) {
  1009. /* Ignore any errors. */
  1010. continue;
  1011. }
  1012. /* Exit if the signature is a log record page. */
  1013. if (r_page->rhdr.sign == NTFS_RCRD_SIGNATURE) {
  1014. info->initialized = true;
  1015. break;
  1016. }
  1017. brst = r_page->rhdr.sign == NTFS_RSTR_SIGNATURE;
  1018. bchk = r_page->rhdr.sign == NTFS_CHKD_SIGNATURE;
  1019. if (!bchk && !brst) {
  1020. if (r_page->rhdr.sign != NTFS_FFFF_SIGNATURE) {
  1021. /*
  1022. * Remember if the signature does not
  1023. * indicate uninitialized file.
  1024. */
  1025. info->initialized = true;
  1026. }
  1027. continue;
  1028. }
  1029. ra = NULL;
  1030. info->valid_page = false;
  1031. info->initialized = true;
  1032. info->vbo = vbo;
  1033. /* Let's check the restart area if this is a valid page. */
  1034. if (!is_rst_page_hdr_valid(vbo, r_page))
  1035. goto check_result;
  1036. ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
  1037. if (!is_rst_area_valid(r_page))
  1038. goto check_result;
  1039. /*
  1040. * We have a valid restart page header and restart area.
  1041. * If chkdsk was run or we have no clients then we have
  1042. * no more checking to do.
  1043. */
  1044. if (bchk || ra->client_idx[1] == LFS_NO_CLIENT_LE) {
  1045. info->valid_page = true;
  1046. goto check_result;
  1047. }
  1048. if (is_client_area_valid(r_page, usa_error)) {
  1049. info->valid_page = true;
  1050. ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
  1051. }
  1052. check_result:
  1053. /*
  1054. * If chkdsk was run then update the caller's
  1055. * values and return.
  1056. */
  1057. if (r_page->rhdr.sign == NTFS_CHKD_SIGNATURE) {
  1058. info->chkdsk_was_run = true;
  1059. info->last_lsn = le64_to_cpu(r_page->rhdr.lsn);
  1060. info->restart = true;
  1061. info->r_page = r_page;
  1062. return 0;
  1063. }
  1064. /*
  1065. * If we have a valid page then copy the values
  1066. * we need from it.
  1067. */
  1068. if (info->valid_page) {
  1069. info->last_lsn = le64_to_cpu(ra->current_lsn);
  1070. info->restart = true;
  1071. info->r_page = r_page;
  1072. return 0;
  1073. }
  1074. }
  1075. kfree(r_page);
  1076. return 0;
  1077. }
  1078. /*
  1079. * Ilog_init_pg_hdr - Init @log from restart page header.
  1080. */
  1081. static void log_init_pg_hdr(struct ntfs_log *log, u16 major_ver, u16 minor_ver)
  1082. {
  1083. log->sys_page_size = log->page_size;
  1084. log->sys_page_mask = log->page_mask;
  1085. log->clst_per_page = log->page_size >> log->ni->mi.sbi->cluster_bits;
  1086. if (!log->clst_per_page)
  1087. log->clst_per_page = 1;
  1088. log->first_page = major_ver >= 2 ? 0x22 * log->page_size :
  1089. 4 * log->page_size;
  1090. log->major_ver = major_ver;
  1091. log->minor_ver = minor_ver;
  1092. }
  1093. /*
  1094. * log_create - Init @log in cases when we don't have a restart area to use.
  1095. */
  1096. static void log_create(struct ntfs_log *log, const u64 last_lsn,
  1097. u32 open_log_count, bool wrapped, bool use_multi_page)
  1098. {
  1099. /* All file offsets must be quadword aligned. */
  1100. log->file_data_bits = blksize_bits(log->l_size) - 3;
  1101. log->seq_num_mask = (8 << log->file_data_bits) - 1;
  1102. log->seq_num_bits = sizeof(u64) * 8 - log->file_data_bits;
  1103. log->seq_num = (last_lsn >> log->file_data_bits) + 2;
  1104. log->next_page = log->first_page;
  1105. log->oldest_lsn = log->seq_num << log->file_data_bits;
  1106. log->oldest_lsn_off = 0;
  1107. log->last_lsn = log->oldest_lsn;
  1108. log->l_flags |= NTFSLOG_NO_LAST_LSN | NTFSLOG_NO_OLDEST_LSN;
  1109. /* Set the correct flags for the I/O and indicate if we have wrapped. */
  1110. if (wrapped)
  1111. log->l_flags |= NTFSLOG_WRAPPED;
  1112. if (use_multi_page)
  1113. log->l_flags |= NTFSLOG_MULTIPLE_PAGE_IO;
  1114. /* Compute the log page values. */
  1115. log->data_off = ALIGN(
  1116. offsetof(struct RECORD_PAGE_HDR, fixups) +
  1117. sizeof(short) * ((log->page_size >> SECTOR_SHIFT) + 1),
  1118. 8);
  1119. log->data_size = log->page_size - log->data_off;
  1120. log->record_header_len = sizeof(struct LFS_RECORD_HDR);
  1121. /* Remember the different page sizes for reservation. */
  1122. log->reserved = log->data_size - log->record_header_len;
  1123. /* Compute the restart page values. */
  1124. log->ra_off = ALIGN(
  1125. offsetof(struct RESTART_HDR, fixups) +
  1126. sizeof(short) *
  1127. ((log->sys_page_size >> SECTOR_SHIFT) + 1),
  1128. 8);
  1129. log->restart_size = log->sys_page_size - log->ra_off;
  1130. log->ra_size = struct_size(log->ra, clients, 1);
  1131. log->current_openlog_count = open_log_count;
  1132. /*
  1133. * The total available log file space is the number of
  1134. * log file pages times the space available on each page.
  1135. */
  1136. log->total_avail_pages = log->l_size - log->first_page;
  1137. log->total_avail = log->total_avail_pages >> log->page_bits;
  1138. /*
  1139. * We assume that we can't use the end of the page less than
  1140. * the file record size.
  1141. * Then we won't need to reserve more than the caller asks for.
  1142. */
  1143. log->max_current_avail = log->total_avail * log->reserved;
  1144. log->total_avail = log->total_avail * log->data_size;
  1145. log->current_avail = log->max_current_avail;
  1146. }
  1147. /*
  1148. * log_create_ra - Fill a restart area from the values stored in @log.
  1149. */
  1150. static struct RESTART_AREA *log_create_ra(struct ntfs_log *log)
  1151. {
  1152. struct CLIENT_REC *cr;
  1153. struct RESTART_AREA *ra = kzalloc(log->restart_size, GFP_NOFS);
  1154. if (!ra)
  1155. return NULL;
  1156. ra->current_lsn = cpu_to_le64(log->last_lsn);
  1157. ra->log_clients = cpu_to_le16(1);
  1158. ra->client_idx[1] = LFS_NO_CLIENT_LE;
  1159. if (log->l_flags & NTFSLOG_MULTIPLE_PAGE_IO)
  1160. ra->flags = RESTART_SINGLE_PAGE_IO;
  1161. ra->seq_num_bits = cpu_to_le32(log->seq_num_bits);
  1162. ra->ra_len = cpu_to_le16(log->ra_size);
  1163. ra->client_off = cpu_to_le16(offsetof(struct RESTART_AREA, clients));
  1164. ra->l_size = cpu_to_le64(log->l_size);
  1165. ra->rec_hdr_len = cpu_to_le16(log->record_header_len);
  1166. ra->data_off = cpu_to_le16(log->data_off);
  1167. ra->open_log_count = cpu_to_le32(log->current_openlog_count + 1);
  1168. cr = ra->clients;
  1169. cr->prev_client = LFS_NO_CLIENT_LE;
  1170. cr->next_client = LFS_NO_CLIENT_LE;
  1171. return ra;
  1172. }
  1173. static u32 final_log_off(struct ntfs_log *log, u64 lsn, u32 data_len)
  1174. {
  1175. u32 base_vbo = lsn << 3;
  1176. u32 final_log_off = (base_vbo & log->seq_num_mask) & ~log->page_mask;
  1177. u32 page_off = base_vbo & log->page_mask;
  1178. u32 tail = log->page_size - page_off;
  1179. page_off -= 1;
  1180. /* Add the length of the header. */
  1181. data_len += log->record_header_len;
  1182. /*
  1183. * If this lsn is contained this log page we are done.
  1184. * Otherwise we need to walk through several log pages.
  1185. */
  1186. if (data_len > tail) {
  1187. data_len -= tail;
  1188. tail = log->data_size;
  1189. page_off = log->data_off - 1;
  1190. for (;;) {
  1191. final_log_off = next_page_off(log, final_log_off);
  1192. /*
  1193. * We are done if the remaining bytes
  1194. * fit on this page.
  1195. */
  1196. if (data_len <= tail)
  1197. break;
  1198. data_len -= tail;
  1199. }
  1200. }
  1201. /*
  1202. * We add the remaining bytes to our starting position on this page
  1203. * and then add that value to the file offset of this log page.
  1204. */
  1205. return final_log_off + data_len + page_off;
  1206. }
  1207. static int next_log_lsn(struct ntfs_log *log, const struct LFS_RECORD_HDR *rh,
  1208. u64 *lsn)
  1209. {
  1210. int err;
  1211. u64 this_lsn = le64_to_cpu(rh->this_lsn);
  1212. u32 vbo = lsn_to_vbo(log, this_lsn);
  1213. u32 end =
  1214. final_log_off(log, this_lsn, le32_to_cpu(rh->client_data_len));
  1215. u32 hdr_off = end & ~log->sys_page_mask;
  1216. u64 seq = this_lsn >> log->file_data_bits;
  1217. struct RECORD_PAGE_HDR *page = NULL;
  1218. /* Remember if we wrapped. */
  1219. if (end <= vbo)
  1220. seq += 1;
  1221. /* Log page header for this page. */
  1222. err = read_log_page(log, hdr_off, &page, NULL);
  1223. if (err)
  1224. return err;
  1225. /*
  1226. * If the lsn we were given was not the last lsn on this page,
  1227. * then the starting offset for the next lsn is on a quad word
  1228. * boundary following the last file offset for the current lsn.
  1229. * Otherwise the file offset is the start of the data on the next page.
  1230. */
  1231. if (this_lsn == le64_to_cpu(page->rhdr.lsn)) {
  1232. /* If we wrapped, we need to increment the sequence number. */
  1233. hdr_off = next_page_off(log, hdr_off);
  1234. if (hdr_off == log->first_page)
  1235. seq += 1;
  1236. vbo = hdr_off + log->data_off;
  1237. } else {
  1238. vbo = ALIGN(end, 8);
  1239. }
  1240. /* Compute the lsn based on the file offset and the sequence count. */
  1241. *lsn = vbo_to_lsn(log, vbo, seq);
  1242. /*
  1243. * If this lsn is within the legal range for the file, we return true.
  1244. * Otherwise false indicates that there are no more lsn's.
  1245. */
  1246. if (!is_lsn_in_file(log, *lsn))
  1247. *lsn = 0;
  1248. kfree(page);
  1249. return 0;
  1250. }
  1251. /*
  1252. * current_log_avail - Calculate the number of bytes available for log records.
  1253. */
  1254. static u32 current_log_avail(struct ntfs_log *log)
  1255. {
  1256. u32 oldest_off, next_free_off, free_bytes;
  1257. if (log->l_flags & NTFSLOG_NO_LAST_LSN) {
  1258. /* The entire file is available. */
  1259. return log->max_current_avail;
  1260. }
  1261. /*
  1262. * If there is a last lsn the restart area then we know that we will
  1263. * have to compute the free range.
  1264. * If there is no oldest lsn then start at the first page of the file.
  1265. */
  1266. oldest_off = (log->l_flags & NTFSLOG_NO_OLDEST_LSN) ?
  1267. log->first_page :
  1268. (log->oldest_lsn_off & ~log->sys_page_mask);
  1269. /*
  1270. * We will use the next log page offset to compute the next free page.
  1271. * If we are going to reuse this page go to the next page.
  1272. * If we are at the first page then use the end of the file.
  1273. */
  1274. next_free_off = (log->l_flags & NTFSLOG_REUSE_TAIL) ?
  1275. log->next_page + log->page_size :
  1276. log->next_page == log->first_page ? log->l_size :
  1277. log->next_page;
  1278. /* If the two offsets are the same then there is no available space. */
  1279. if (oldest_off == next_free_off)
  1280. return 0;
  1281. /*
  1282. * If the free offset follows the oldest offset then subtract
  1283. * this range from the total available pages.
  1284. */
  1285. free_bytes =
  1286. oldest_off < next_free_off ?
  1287. log->total_avail_pages - (next_free_off - oldest_off) :
  1288. oldest_off - next_free_off;
  1289. free_bytes >>= log->page_bits;
  1290. return free_bytes * log->reserved;
  1291. }
  1292. static bool check_subseq_log_page(struct ntfs_log *log,
  1293. const struct RECORD_PAGE_HDR *rp, u32 vbo,
  1294. u64 seq)
  1295. {
  1296. u64 lsn_seq;
  1297. const struct NTFS_RECORD_HEADER *rhdr = &rp->rhdr;
  1298. u64 lsn = le64_to_cpu(rhdr->lsn);
  1299. if (rhdr->sign == NTFS_FFFF_SIGNATURE || !rhdr->sign)
  1300. return false;
  1301. /*
  1302. * If the last lsn on the page occurs was written after the page
  1303. * that caused the original error then we have a fatal error.
  1304. */
  1305. lsn_seq = lsn >> log->file_data_bits;
  1306. /*
  1307. * If the sequence number for the lsn the page is equal or greater
  1308. * than lsn we expect, then this is a subsequent write.
  1309. */
  1310. return lsn_seq >= seq ||
  1311. (lsn_seq == seq - 1 && log->first_page == vbo &&
  1312. vbo != (lsn_to_vbo(log, lsn) & ~log->page_mask));
  1313. }
  1314. /*
  1315. * last_log_lsn
  1316. *
  1317. * Walks through the log pages for a file, searching for the
  1318. * last log page written to the file.
  1319. */
  1320. static int last_log_lsn(struct ntfs_log *log)
  1321. {
  1322. int err;
  1323. bool usa_error = false;
  1324. bool replace_page = false;
  1325. bool reuse_page = log->l_flags & NTFSLOG_REUSE_TAIL;
  1326. bool wrapped_file, wrapped;
  1327. u32 page_cnt = 1, page_pos = 1;
  1328. u32 page_off = 0, page_off1 = 0, saved_off = 0;
  1329. u32 final_off, second_off, final_off_prev = 0, second_off_prev = 0;
  1330. u32 first_file_off = 0, second_file_off = 0;
  1331. u32 part_io_count = 0;
  1332. u32 tails = 0;
  1333. u32 this_off, curpage_off, nextpage_off, remain_pages;
  1334. u64 expected_seq, seq_base = 0, lsn_base = 0;
  1335. u64 best_lsn, best_lsn1, best_lsn2;
  1336. u64 lsn_cur, lsn1, lsn2;
  1337. u64 last_ok_lsn = reuse_page ? log->last_lsn : 0;
  1338. u16 cur_pos, best_page_pos;
  1339. struct RECORD_PAGE_HDR *page = NULL;
  1340. struct RECORD_PAGE_HDR *tst_page = NULL;
  1341. struct RECORD_PAGE_HDR *first_tail = NULL;
  1342. struct RECORD_PAGE_HDR *second_tail = NULL;
  1343. struct RECORD_PAGE_HDR *tail_page = NULL;
  1344. struct RECORD_PAGE_HDR *second_tail_prev = NULL;
  1345. struct RECORD_PAGE_HDR *first_tail_prev = NULL;
  1346. struct RECORD_PAGE_HDR *page_bufs = NULL;
  1347. struct RECORD_PAGE_HDR *best_page;
  1348. if (log->major_ver >= 2) {
  1349. final_off = 0x02 * log->page_size;
  1350. second_off = 0x12 * log->page_size;
  1351. // 0x10 == 0x12 - 0x2
  1352. page_bufs = kmalloc(log->page_size * 0x10, GFP_NOFS);
  1353. if (!page_bufs)
  1354. return -ENOMEM;
  1355. } else {
  1356. second_off = log->first_page - log->page_size;
  1357. final_off = second_off - log->page_size;
  1358. }
  1359. next_tail:
  1360. /* Read second tail page (at pos 3/0x12000). */
  1361. if (read_log_page(log, second_off, &second_tail, &usa_error) ||
  1362. usa_error || second_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
  1363. kfree(second_tail);
  1364. second_tail = NULL;
  1365. second_file_off = 0;
  1366. lsn2 = 0;
  1367. } else {
  1368. second_file_off = hdr_file_off(log, second_tail);
  1369. lsn2 = le64_to_cpu(second_tail->record_hdr.last_end_lsn);
  1370. }
  1371. /* Read first tail page (at pos 2/0x2000). */
  1372. if (read_log_page(log, final_off, &first_tail, &usa_error) ||
  1373. usa_error || first_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
  1374. kfree(first_tail);
  1375. first_tail = NULL;
  1376. first_file_off = 0;
  1377. lsn1 = 0;
  1378. } else {
  1379. first_file_off = hdr_file_off(log, first_tail);
  1380. lsn1 = le64_to_cpu(first_tail->record_hdr.last_end_lsn);
  1381. }
  1382. if (log->major_ver < 2) {
  1383. int best_page;
  1384. first_tail_prev = first_tail;
  1385. final_off_prev = first_file_off;
  1386. second_tail_prev = second_tail;
  1387. second_off_prev = second_file_off;
  1388. tails = 1;
  1389. if (!first_tail && !second_tail)
  1390. goto tail_read;
  1391. if (first_tail && second_tail)
  1392. best_page = lsn1 < lsn2 ? 1 : 0;
  1393. else if (first_tail)
  1394. best_page = 0;
  1395. else
  1396. best_page = 1;
  1397. page_off = best_page ? second_file_off : first_file_off;
  1398. seq_base = (best_page ? lsn2 : lsn1) >> log->file_data_bits;
  1399. goto tail_read;
  1400. }
  1401. best_lsn1 = first_tail ? base_lsn(log, first_tail, first_file_off) : 0;
  1402. best_lsn2 = second_tail ? base_lsn(log, second_tail, second_file_off) :
  1403. 0;
  1404. if (first_tail && second_tail) {
  1405. if (best_lsn1 > best_lsn2) {
  1406. best_lsn = best_lsn1;
  1407. best_page = first_tail;
  1408. this_off = first_file_off;
  1409. } else {
  1410. best_lsn = best_lsn2;
  1411. best_page = second_tail;
  1412. this_off = second_file_off;
  1413. }
  1414. } else if (first_tail) {
  1415. best_lsn = best_lsn1;
  1416. best_page = first_tail;
  1417. this_off = first_file_off;
  1418. } else if (second_tail) {
  1419. best_lsn = best_lsn2;
  1420. best_page = second_tail;
  1421. this_off = second_file_off;
  1422. } else {
  1423. goto tail_read;
  1424. }
  1425. best_page_pos = le16_to_cpu(best_page->page_pos);
  1426. if (!tails) {
  1427. if (best_page_pos == page_pos) {
  1428. seq_base = best_lsn >> log->file_data_bits;
  1429. saved_off = page_off = le32_to_cpu(best_page->file_off);
  1430. lsn_base = best_lsn;
  1431. memmove(page_bufs, best_page, log->page_size);
  1432. page_cnt = le16_to_cpu(best_page->page_count);
  1433. if (page_cnt > 1)
  1434. page_pos += 1;
  1435. tails = 1;
  1436. }
  1437. } else if (seq_base == (best_lsn >> log->file_data_bits) &&
  1438. saved_off + log->page_size == this_off &&
  1439. lsn_base < best_lsn &&
  1440. (page_pos != page_cnt || best_page_pos == page_pos ||
  1441. best_page_pos == 1) &&
  1442. (page_pos >= page_cnt || best_page_pos == page_pos)) {
  1443. u16 bppc = le16_to_cpu(best_page->page_count);
  1444. saved_off += log->page_size;
  1445. lsn_base = best_lsn;
  1446. memmove(Add2Ptr(page_bufs, tails * log->page_size), best_page,
  1447. log->page_size);
  1448. tails += 1;
  1449. if (best_page_pos != bppc) {
  1450. page_cnt = bppc;
  1451. page_pos = best_page_pos;
  1452. if (page_cnt > 1)
  1453. page_pos += 1;
  1454. } else {
  1455. page_pos = page_cnt = 1;
  1456. }
  1457. } else {
  1458. kfree(first_tail);
  1459. kfree(second_tail);
  1460. goto tail_read;
  1461. }
  1462. kfree(first_tail_prev);
  1463. first_tail_prev = first_tail;
  1464. final_off_prev = first_file_off;
  1465. first_tail = NULL;
  1466. kfree(second_tail_prev);
  1467. second_tail_prev = second_tail;
  1468. second_off_prev = second_file_off;
  1469. second_tail = NULL;
  1470. final_off += log->page_size;
  1471. second_off += log->page_size;
  1472. if (tails < 0x10)
  1473. goto next_tail;
  1474. tail_read:
  1475. first_tail = first_tail_prev;
  1476. final_off = final_off_prev;
  1477. second_tail = second_tail_prev;
  1478. second_off = second_off_prev;
  1479. page_cnt = page_pos = 1;
  1480. curpage_off = seq_base == log->seq_num ? min(log->next_page, page_off) :
  1481. log->next_page;
  1482. wrapped_file =
  1483. curpage_off == log->first_page &&
  1484. !(log->l_flags & (NTFSLOG_NO_LAST_LSN | NTFSLOG_REUSE_TAIL));
  1485. expected_seq = wrapped_file ? (log->seq_num + 1) : log->seq_num;
  1486. nextpage_off = curpage_off;
  1487. next_page:
  1488. tail_page = NULL;
  1489. /* Read the next log page. */
  1490. err = read_log_page(log, curpage_off, &page, &usa_error);
  1491. /* Compute the next log page offset the file. */
  1492. nextpage_off = next_page_off(log, curpage_off);
  1493. wrapped = nextpage_off == log->first_page;
  1494. if (tails > 1) {
  1495. struct RECORD_PAGE_HDR *cur_page =
  1496. Add2Ptr(page_bufs, curpage_off - page_off);
  1497. if (curpage_off == saved_off) {
  1498. tail_page = cur_page;
  1499. goto use_tail_page;
  1500. }
  1501. if (page_off > curpage_off || curpage_off >= saved_off)
  1502. goto use_tail_page;
  1503. if (page_off1)
  1504. goto use_cur_page;
  1505. if (!err && !usa_error &&
  1506. page->rhdr.sign == NTFS_RCRD_SIGNATURE &&
  1507. cur_page->rhdr.lsn == page->rhdr.lsn &&
  1508. cur_page->record_hdr.next_record_off ==
  1509. page->record_hdr.next_record_off &&
  1510. ((page_pos == page_cnt &&
  1511. le16_to_cpu(page->page_pos) == 1) ||
  1512. (page_pos != page_cnt &&
  1513. le16_to_cpu(page->page_pos) == page_pos + 1 &&
  1514. le16_to_cpu(page->page_count) == page_cnt))) {
  1515. cur_page = NULL;
  1516. goto use_tail_page;
  1517. }
  1518. page_off1 = page_off;
  1519. use_cur_page:
  1520. lsn_cur = le64_to_cpu(cur_page->rhdr.lsn);
  1521. if (last_ok_lsn !=
  1522. le64_to_cpu(cur_page->record_hdr.last_end_lsn) &&
  1523. ((lsn_cur >> log->file_data_bits) +
  1524. ((curpage_off <
  1525. (lsn_to_vbo(log, lsn_cur) & ~log->page_mask)) ?
  1526. 1 :
  1527. 0)) != expected_seq) {
  1528. goto check_tail;
  1529. }
  1530. if (!is_log_record_end(cur_page)) {
  1531. tail_page = NULL;
  1532. last_ok_lsn = lsn_cur;
  1533. goto next_page_1;
  1534. }
  1535. log->seq_num = expected_seq;
  1536. log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
  1537. log->last_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
  1538. log->ra->current_lsn = cur_page->record_hdr.last_end_lsn;
  1539. if (log->record_header_len <=
  1540. log->page_size -
  1541. le16_to_cpu(cur_page->record_hdr.next_record_off)) {
  1542. log->l_flags |= NTFSLOG_REUSE_TAIL;
  1543. log->next_page = curpage_off;
  1544. } else {
  1545. log->l_flags &= ~NTFSLOG_REUSE_TAIL;
  1546. log->next_page = nextpage_off;
  1547. }
  1548. if (wrapped_file)
  1549. log->l_flags |= NTFSLOG_WRAPPED;
  1550. last_ok_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
  1551. goto next_page_1;
  1552. }
  1553. /*
  1554. * If we are at the expected first page of a transfer check to see
  1555. * if either tail copy is at this offset.
  1556. * If this page is the last page of a transfer, check if we wrote
  1557. * a subsequent tail copy.
  1558. */
  1559. if (page_cnt == page_pos || page_cnt == page_pos + 1) {
  1560. /*
  1561. * Check if the offset matches either the first or second
  1562. * tail copy. It is possible it will match both.
  1563. */
  1564. if (curpage_off == final_off)
  1565. tail_page = first_tail;
  1566. /*
  1567. * If we already matched on the first page then
  1568. * check the ending lsn's.
  1569. */
  1570. if (curpage_off == second_off) {
  1571. if (!tail_page ||
  1572. (second_tail &&
  1573. le64_to_cpu(second_tail->record_hdr.last_end_lsn) >
  1574. le64_to_cpu(first_tail->record_hdr
  1575. .last_end_lsn))) {
  1576. tail_page = second_tail;
  1577. }
  1578. }
  1579. }
  1580. use_tail_page:
  1581. if (tail_page) {
  1582. /* We have a candidate for a tail copy. */
  1583. lsn_cur = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
  1584. if (last_ok_lsn < lsn_cur) {
  1585. /*
  1586. * If the sequence number is not expected,
  1587. * then don't use the tail copy.
  1588. */
  1589. if (expected_seq != (lsn_cur >> log->file_data_bits))
  1590. tail_page = NULL;
  1591. } else if (last_ok_lsn > lsn_cur) {
  1592. /*
  1593. * If the last lsn is greater than the one on
  1594. * this page then forget this tail.
  1595. */
  1596. tail_page = NULL;
  1597. }
  1598. }
  1599. /*
  1600. *If we have an error on the current page,
  1601. * we will break of this loop.
  1602. */
  1603. if (err || usa_error)
  1604. goto check_tail;
  1605. /*
  1606. * Done if the last lsn on this page doesn't match the previous known
  1607. * last lsn or the sequence number is not expected.
  1608. */
  1609. lsn_cur = le64_to_cpu(page->rhdr.lsn);
  1610. if (last_ok_lsn != lsn_cur &&
  1611. expected_seq != (lsn_cur >> log->file_data_bits)) {
  1612. goto check_tail;
  1613. }
  1614. /*
  1615. * Check that the page position and page count values are correct.
  1616. * If this is the first page of a transfer the position must be 1
  1617. * and the count will be unknown.
  1618. */
  1619. if (page_cnt == page_pos) {
  1620. if (page->page_pos != cpu_to_le16(1) &&
  1621. (!reuse_page || page->page_pos != page->page_count)) {
  1622. /*
  1623. * If the current page is the first page we are
  1624. * looking at and we are reusing this page then
  1625. * it can be either the first or last page of a
  1626. * transfer. Otherwise it can only be the first.
  1627. */
  1628. goto check_tail;
  1629. }
  1630. } else if (le16_to_cpu(page->page_count) != page_cnt ||
  1631. le16_to_cpu(page->page_pos) != page_pos + 1) {
  1632. /*
  1633. * The page position better be 1 more than the last page
  1634. * position and the page count better match.
  1635. */
  1636. goto check_tail;
  1637. }
  1638. /*
  1639. * We have a valid page the file and may have a valid page
  1640. * the tail copy area.
  1641. * If the tail page was written after the page the file then
  1642. * break of the loop.
  1643. */
  1644. if (tail_page &&
  1645. le64_to_cpu(tail_page->record_hdr.last_end_lsn) > lsn_cur) {
  1646. /* Remember if we will replace the page. */
  1647. replace_page = true;
  1648. goto check_tail;
  1649. }
  1650. tail_page = NULL;
  1651. if (is_log_record_end(page)) {
  1652. /*
  1653. * Since we have read this page we know the sequence number
  1654. * is the same as our expected value.
  1655. */
  1656. log->seq_num = expected_seq;
  1657. log->last_lsn = le64_to_cpu(page->record_hdr.last_end_lsn);
  1658. log->ra->current_lsn = page->record_hdr.last_end_lsn;
  1659. log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
  1660. /*
  1661. * If there is room on this page for another header then
  1662. * remember we want to reuse the page.
  1663. */
  1664. if (log->record_header_len <=
  1665. log->page_size -
  1666. le16_to_cpu(page->record_hdr.next_record_off)) {
  1667. log->l_flags |= NTFSLOG_REUSE_TAIL;
  1668. log->next_page = curpage_off;
  1669. } else {
  1670. log->l_flags &= ~NTFSLOG_REUSE_TAIL;
  1671. log->next_page = nextpage_off;
  1672. }
  1673. /* Remember if we wrapped the log file. */
  1674. if (wrapped_file)
  1675. log->l_flags |= NTFSLOG_WRAPPED;
  1676. }
  1677. /*
  1678. * Remember the last page count and position.
  1679. * Also remember the last known lsn.
  1680. */
  1681. page_cnt = le16_to_cpu(page->page_count);
  1682. page_pos = le16_to_cpu(page->page_pos);
  1683. last_ok_lsn = le64_to_cpu(page->rhdr.lsn);
  1684. next_page_1:
  1685. if (wrapped) {
  1686. expected_seq += 1;
  1687. wrapped_file = 1;
  1688. }
  1689. curpage_off = nextpage_off;
  1690. kfree(page);
  1691. page = NULL;
  1692. reuse_page = 0;
  1693. goto next_page;
  1694. check_tail:
  1695. if (tail_page) {
  1696. log->seq_num = expected_seq;
  1697. log->last_lsn = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
  1698. log->ra->current_lsn = tail_page->record_hdr.last_end_lsn;
  1699. log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
  1700. if (log->page_size -
  1701. le16_to_cpu(
  1702. tail_page->record_hdr.next_record_off) >=
  1703. log->record_header_len) {
  1704. log->l_flags |= NTFSLOG_REUSE_TAIL;
  1705. log->next_page = curpage_off;
  1706. } else {
  1707. log->l_flags &= ~NTFSLOG_REUSE_TAIL;
  1708. log->next_page = nextpage_off;
  1709. }
  1710. if (wrapped)
  1711. log->l_flags |= NTFSLOG_WRAPPED;
  1712. }
  1713. /* Remember that the partial IO will start at the next page. */
  1714. second_off = nextpage_off;
  1715. /*
  1716. * If the next page is the first page of the file then update
  1717. * the sequence number for log records which begon the next page.
  1718. */
  1719. if (wrapped)
  1720. expected_seq += 1;
  1721. /*
  1722. * If we have a tail copy or are performing single page I/O we can
  1723. * immediately look at the next page.
  1724. */
  1725. if (replace_page || (log->ra->flags & RESTART_SINGLE_PAGE_IO)) {
  1726. page_cnt = 2;
  1727. page_pos = 1;
  1728. goto check_valid;
  1729. }
  1730. if (page_pos != page_cnt)
  1731. goto check_valid;
  1732. /*
  1733. * If the next page causes us to wrap to the beginning of the log
  1734. * file then we know which page to check next.
  1735. */
  1736. if (wrapped) {
  1737. page_cnt = 2;
  1738. page_pos = 1;
  1739. goto check_valid;
  1740. }
  1741. cur_pos = 2;
  1742. next_test_page:
  1743. kfree(tst_page);
  1744. tst_page = NULL;
  1745. /* Walk through the file, reading log pages. */
  1746. err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
  1747. /*
  1748. * If we get a USA error then assume that we correctly found
  1749. * the end of the original transfer.
  1750. */
  1751. if (usa_error)
  1752. goto file_is_valid;
  1753. /*
  1754. * If we were able to read the page, we examine it to see if it
  1755. * is the same or different Io block.
  1756. */
  1757. if (err)
  1758. goto next_test_page_1;
  1759. if (le16_to_cpu(tst_page->page_pos) == cur_pos &&
  1760. check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
  1761. page_cnt = le16_to_cpu(tst_page->page_count) + 1;
  1762. page_pos = le16_to_cpu(tst_page->page_pos);
  1763. goto check_valid;
  1764. } else {
  1765. goto file_is_valid;
  1766. }
  1767. next_test_page_1:
  1768. nextpage_off = next_page_off(log, curpage_off);
  1769. wrapped = nextpage_off == log->first_page;
  1770. if (wrapped) {
  1771. expected_seq += 1;
  1772. page_cnt = 2;
  1773. page_pos = 1;
  1774. }
  1775. cur_pos += 1;
  1776. part_io_count += 1;
  1777. if (!wrapped)
  1778. goto next_test_page;
  1779. check_valid:
  1780. /* Skip over the remaining pages this transfer. */
  1781. remain_pages = page_cnt - page_pos - 1;
  1782. part_io_count += remain_pages;
  1783. while (remain_pages--) {
  1784. nextpage_off = next_page_off(log, curpage_off);
  1785. wrapped = nextpage_off == log->first_page;
  1786. if (wrapped)
  1787. expected_seq += 1;
  1788. }
  1789. /* Call our routine to check this log page. */
  1790. kfree(tst_page);
  1791. tst_page = NULL;
  1792. err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
  1793. if (!err && !usa_error &&
  1794. check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
  1795. err = -EINVAL;
  1796. goto out;
  1797. }
  1798. file_is_valid:
  1799. /* We have a valid file. */
  1800. if (page_off1 || tail_page) {
  1801. struct RECORD_PAGE_HDR *tmp_page;
  1802. if (sb_rdonly(log->ni->mi.sbi->sb)) {
  1803. err = -EROFS;
  1804. goto out;
  1805. }
  1806. if (page_off1) {
  1807. tmp_page = Add2Ptr(page_bufs, page_off1 - page_off);
  1808. tails -= (page_off1 - page_off) / log->page_size;
  1809. if (!tail_page)
  1810. tails -= 1;
  1811. } else {
  1812. tmp_page = tail_page;
  1813. tails = 1;
  1814. }
  1815. while (tails--) {
  1816. u64 off = hdr_file_off(log, tmp_page);
  1817. if (!page) {
  1818. page = kmalloc(log->page_size, GFP_NOFS);
  1819. if (!page) {
  1820. err = -ENOMEM;
  1821. goto out;
  1822. }
  1823. }
  1824. /*
  1825. * Correct page and copy the data from this page
  1826. * into it and flush it to disk.
  1827. */
  1828. memcpy(page, tmp_page, log->page_size);
  1829. /* Fill last flushed lsn value flush the page. */
  1830. if (log->major_ver < 2)
  1831. page->rhdr.lsn = page->record_hdr.last_end_lsn;
  1832. else
  1833. page->file_off = 0;
  1834. page->page_pos = page->page_count = cpu_to_le16(1);
  1835. ntfs_fix_pre_write(&page->rhdr, log->page_size);
  1836. err = ntfs_sb_write_run(log->ni->mi.sbi,
  1837. &log->ni->file.run, off, page,
  1838. log->page_size, 0);
  1839. if (err)
  1840. goto out;
  1841. if (part_io_count && second_off == off) {
  1842. second_off += log->page_size;
  1843. part_io_count -= 1;
  1844. }
  1845. tmp_page = Add2Ptr(tmp_page, log->page_size);
  1846. }
  1847. }
  1848. if (part_io_count) {
  1849. if (sb_rdonly(log->ni->mi.sbi->sb)) {
  1850. err = -EROFS;
  1851. goto out;
  1852. }
  1853. }
  1854. out:
  1855. kfree(second_tail);
  1856. kfree(first_tail);
  1857. kfree(page);
  1858. kfree(tst_page);
  1859. kfree(page_bufs);
  1860. return err;
  1861. }
  1862. /*
  1863. * read_log_rec_buf - Copy a log record from the file to a buffer.
  1864. *
  1865. * The log record may span several log pages and may even wrap the file.
  1866. */
  1867. static int read_log_rec_buf(struct ntfs_log *log,
  1868. const struct LFS_RECORD_HDR *rh, void *buffer)
  1869. {
  1870. int err;
  1871. struct RECORD_PAGE_HDR *ph = NULL;
  1872. u64 lsn = le64_to_cpu(rh->this_lsn);
  1873. u32 vbo = lsn_to_vbo(log, lsn) & ~log->page_mask;
  1874. u32 off = lsn_to_page_off(log, lsn) + log->record_header_len;
  1875. u32 data_len = le32_to_cpu(rh->client_data_len);
  1876. /*
  1877. * While there are more bytes to transfer,
  1878. * we continue to attempt to perform the read.
  1879. */
  1880. for (;;) {
  1881. bool usa_error;
  1882. u32 tail = log->page_size - off;
  1883. if (tail >= data_len)
  1884. tail = data_len;
  1885. data_len -= tail;
  1886. err = read_log_page(log, vbo, &ph, &usa_error);
  1887. if (err)
  1888. goto out;
  1889. /*
  1890. * The last lsn on this page better be greater or equal
  1891. * to the lsn we are copying.
  1892. */
  1893. if (lsn > le64_to_cpu(ph->rhdr.lsn)) {
  1894. err = -EINVAL;
  1895. goto out;
  1896. }
  1897. memcpy(buffer, Add2Ptr(ph, off), tail);
  1898. /* If there are no more bytes to transfer, we exit the loop. */
  1899. if (!data_len) {
  1900. if (!is_log_record_end(ph) ||
  1901. lsn > le64_to_cpu(ph->record_hdr.last_end_lsn)) {
  1902. err = -EINVAL;
  1903. goto out;
  1904. }
  1905. break;
  1906. }
  1907. if (ph->rhdr.lsn == ph->record_hdr.last_end_lsn ||
  1908. lsn > le64_to_cpu(ph->rhdr.lsn)) {
  1909. err = -EINVAL;
  1910. goto out;
  1911. }
  1912. vbo = next_page_off(log, vbo);
  1913. off = log->data_off;
  1914. /*
  1915. * Adjust our pointer the user's buffer to transfer
  1916. * the next block to.
  1917. */
  1918. buffer = Add2Ptr(buffer, tail);
  1919. }
  1920. out:
  1921. kfree(ph);
  1922. return err;
  1923. }
  1924. static int read_rst_area(struct ntfs_log *log, struct NTFS_RESTART **rst_,
  1925. u64 *lsn)
  1926. {
  1927. int err;
  1928. struct LFS_RECORD_HDR *rh = NULL;
  1929. const struct CLIENT_REC *cr =
  1930. Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
  1931. u64 lsnr, lsnc = le64_to_cpu(cr->restart_lsn);
  1932. u32 len;
  1933. struct NTFS_RESTART *rst;
  1934. *lsn = 0;
  1935. *rst_ = NULL;
  1936. /* If the client doesn't have a restart area, go ahead and exit now. */
  1937. if (!lsnc)
  1938. return 0;
  1939. err = read_log_page(log, lsn_to_vbo(log, lsnc),
  1940. (struct RECORD_PAGE_HDR **)&rh, NULL);
  1941. if (err)
  1942. return err;
  1943. rst = NULL;
  1944. lsnr = le64_to_cpu(rh->this_lsn);
  1945. if (lsnc != lsnr) {
  1946. /* If the lsn values don't match, then the disk is corrupt. */
  1947. err = -EINVAL;
  1948. goto out;
  1949. }
  1950. *lsn = lsnr;
  1951. len = le32_to_cpu(rh->client_data_len);
  1952. if (!len) {
  1953. err = 0;
  1954. goto out;
  1955. }
  1956. if (len < sizeof(struct NTFS_RESTART)) {
  1957. err = -EINVAL;
  1958. goto out;
  1959. }
  1960. rst = kmalloc(len, GFP_NOFS);
  1961. if (!rst) {
  1962. err = -ENOMEM;
  1963. goto out;
  1964. }
  1965. /* Copy the data into the 'rst' buffer. */
  1966. err = read_log_rec_buf(log, rh, rst);
  1967. if (err)
  1968. goto out;
  1969. *rst_ = rst;
  1970. rst = NULL;
  1971. out:
  1972. kfree(rh);
  1973. kfree(rst);
  1974. return err;
  1975. }
  1976. static int find_log_rec(struct ntfs_log *log, u64 lsn, struct lcb *lcb)
  1977. {
  1978. int err;
  1979. struct LFS_RECORD_HDR *rh = lcb->lrh;
  1980. u32 rec_len, len;
  1981. /* Read the record header for this lsn. */
  1982. if (!rh) {
  1983. err = read_log_page(log, lsn_to_vbo(log, lsn),
  1984. (struct RECORD_PAGE_HDR **)&rh, NULL);
  1985. lcb->lrh = rh;
  1986. if (err)
  1987. return err;
  1988. }
  1989. /*
  1990. * If the lsn the log record doesn't match the desired
  1991. * lsn then the disk is corrupt.
  1992. */
  1993. if (lsn != le64_to_cpu(rh->this_lsn))
  1994. return -EINVAL;
  1995. len = le32_to_cpu(rh->client_data_len);
  1996. /*
  1997. * Check that the length field isn't greater than the total
  1998. * available space the log file.
  1999. */
  2000. rec_len = len + log->record_header_len;
  2001. if (rec_len >= log->total_avail)
  2002. return -EINVAL;
  2003. /*
  2004. * If the entire log record is on this log page,
  2005. * put a pointer to the log record the context block.
  2006. */
  2007. if (rh->flags & LOG_RECORD_MULTI_PAGE) {
  2008. void *lr = kmalloc(len, GFP_NOFS);
  2009. if (!lr)
  2010. return -ENOMEM;
  2011. lcb->log_rec = lr;
  2012. lcb->alloc = true;
  2013. /* Copy the data into the buffer returned. */
  2014. err = read_log_rec_buf(log, rh, lr);
  2015. if (err)
  2016. return err;
  2017. } else {
  2018. /* If beyond the end of the current page -> an error. */
  2019. u32 page_off = lsn_to_page_off(log, lsn);
  2020. if (page_off + len + log->record_header_len > log->page_size)
  2021. return -EINVAL;
  2022. lcb->log_rec = Add2Ptr(rh, sizeof(struct LFS_RECORD_HDR));
  2023. lcb->alloc = false;
  2024. }
  2025. return 0;
  2026. }
  2027. /*
  2028. * read_log_rec_lcb - Init the query operation.
  2029. */
  2030. static int read_log_rec_lcb(struct ntfs_log *log, u64 lsn, u32 ctx_mode,
  2031. struct lcb **lcb_)
  2032. {
  2033. int err;
  2034. const struct CLIENT_REC *cr;
  2035. struct lcb *lcb;
  2036. switch (ctx_mode) {
  2037. case lcb_ctx_undo_next:
  2038. case lcb_ctx_prev:
  2039. case lcb_ctx_next:
  2040. break;
  2041. default:
  2042. return -EINVAL;
  2043. }
  2044. /* Check that the given lsn is the legal range for this client. */
  2045. cr = Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
  2046. if (!verify_client_lsn(log, cr, lsn))
  2047. return -EINVAL;
  2048. lcb = kzalloc(sizeof(struct lcb), GFP_NOFS);
  2049. if (!lcb)
  2050. return -ENOMEM;
  2051. lcb->client = log->client_id;
  2052. lcb->ctx_mode = ctx_mode;
  2053. /* Find the log record indicated by the given lsn. */
  2054. err = find_log_rec(log, lsn, lcb);
  2055. if (err)
  2056. goto out;
  2057. *lcb_ = lcb;
  2058. return 0;
  2059. out:
  2060. lcb_put(lcb);
  2061. *lcb_ = NULL;
  2062. return err;
  2063. }
  2064. /*
  2065. * find_client_next_lsn
  2066. *
  2067. * Attempt to find the next lsn to return to a client based on the context mode.
  2068. */
  2069. static int find_client_next_lsn(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
  2070. {
  2071. int err;
  2072. u64 next_lsn;
  2073. struct LFS_RECORD_HDR *hdr;
  2074. hdr = lcb->lrh;
  2075. *lsn = 0;
  2076. if (lcb_ctx_next != lcb->ctx_mode)
  2077. goto check_undo_next;
  2078. /* Loop as long as another lsn can be found. */
  2079. for (;;) {
  2080. u64 current_lsn;
  2081. err = next_log_lsn(log, hdr, &current_lsn);
  2082. if (err)
  2083. goto out;
  2084. if (!current_lsn)
  2085. break;
  2086. if (hdr != lcb->lrh)
  2087. kfree(hdr);
  2088. hdr = NULL;
  2089. err = read_log_page(log, lsn_to_vbo(log, current_lsn),
  2090. (struct RECORD_PAGE_HDR **)&hdr, NULL);
  2091. if (err)
  2092. goto out;
  2093. if (memcmp(&hdr->client, &lcb->client,
  2094. sizeof(struct CLIENT_ID))) {
  2095. /*err = -EINVAL; */
  2096. } else if (LfsClientRecord == hdr->record_type) {
  2097. kfree(lcb->lrh);
  2098. lcb->lrh = hdr;
  2099. *lsn = current_lsn;
  2100. return 0;
  2101. }
  2102. }
  2103. out:
  2104. if (hdr != lcb->lrh)
  2105. kfree(hdr);
  2106. return err;
  2107. check_undo_next:
  2108. if (lcb_ctx_undo_next == lcb->ctx_mode)
  2109. next_lsn = le64_to_cpu(hdr->client_undo_next_lsn);
  2110. else if (lcb_ctx_prev == lcb->ctx_mode)
  2111. next_lsn = le64_to_cpu(hdr->client_prev_lsn);
  2112. else
  2113. return 0;
  2114. if (!next_lsn)
  2115. return 0;
  2116. if (!verify_client_lsn(
  2117. log, Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off)),
  2118. next_lsn))
  2119. return 0;
  2120. hdr = NULL;
  2121. err = read_log_page(log, lsn_to_vbo(log, next_lsn),
  2122. (struct RECORD_PAGE_HDR **)&hdr, NULL);
  2123. if (err)
  2124. return err;
  2125. kfree(lcb->lrh);
  2126. lcb->lrh = hdr;
  2127. *lsn = next_lsn;
  2128. return 0;
  2129. }
  2130. static int read_next_log_rec(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
  2131. {
  2132. int err;
  2133. err = find_client_next_lsn(log, lcb, lsn);
  2134. if (err)
  2135. return err;
  2136. if (!*lsn)
  2137. return 0;
  2138. if (lcb->alloc)
  2139. kfree(lcb->log_rec);
  2140. lcb->log_rec = NULL;
  2141. lcb->alloc = false;
  2142. kfree(lcb->lrh);
  2143. lcb->lrh = NULL;
  2144. return find_log_rec(log, *lsn, lcb);
  2145. }
  2146. bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes)
  2147. {
  2148. __le16 mask;
  2149. u32 min_de, de_off, used, total;
  2150. const struct NTFS_DE *e;
  2151. if (hdr_has_subnode(hdr)) {
  2152. min_de = sizeof(struct NTFS_DE) + sizeof(u64);
  2153. mask = NTFS_IE_HAS_SUBNODES;
  2154. } else {
  2155. min_de = sizeof(struct NTFS_DE);
  2156. mask = 0;
  2157. }
  2158. de_off = le32_to_cpu(hdr->de_off);
  2159. used = le32_to_cpu(hdr->used);
  2160. total = le32_to_cpu(hdr->total);
  2161. if (de_off > bytes - min_de || used > bytes || total > bytes ||
  2162. de_off + min_de > used || used > total) {
  2163. return false;
  2164. }
  2165. e = Add2Ptr(hdr, de_off);
  2166. for (;;) {
  2167. u16 esize = le16_to_cpu(e->size);
  2168. struct NTFS_DE *next = Add2Ptr(e, esize);
  2169. if (esize < min_de || PtrOffset(hdr, next) > used ||
  2170. (e->flags & NTFS_IE_HAS_SUBNODES) != mask) {
  2171. return false;
  2172. }
  2173. if (de_is_last(e))
  2174. break;
  2175. e = next;
  2176. }
  2177. return true;
  2178. }
  2179. static inline bool check_index_buffer(const struct INDEX_BUFFER *ib, u32 bytes)
  2180. {
  2181. u16 fo;
  2182. const struct NTFS_RECORD_HEADER *r = &ib->rhdr;
  2183. if (r->sign != NTFS_INDX_SIGNATURE)
  2184. return false;
  2185. fo = (SECTOR_SIZE - ((bytes >> SECTOR_SHIFT) + 1) * sizeof(short));
  2186. if (le16_to_cpu(r->fix_off) > fo)
  2187. return false;
  2188. if ((le16_to_cpu(r->fix_num) - 1) * SECTOR_SIZE != bytes)
  2189. return false;
  2190. return check_index_header(&ib->ihdr,
  2191. bytes - offsetof(struct INDEX_BUFFER, ihdr));
  2192. }
  2193. static inline bool check_index_root(const struct ATTRIB *attr,
  2194. struct ntfs_sb_info *sbi)
  2195. {
  2196. bool ret;
  2197. const struct INDEX_ROOT *root = resident_data(attr);
  2198. u8 index_bits = le32_to_cpu(root->index_block_size) >=
  2199. sbi->cluster_size ?
  2200. sbi->cluster_bits :
  2201. SECTOR_SHIFT;
  2202. u8 block_clst = root->index_block_clst;
  2203. if (le32_to_cpu(attr->res.data_size) < sizeof(struct INDEX_ROOT) ||
  2204. (root->type != ATTR_NAME && root->type != ATTR_ZERO) ||
  2205. (root->type == ATTR_NAME &&
  2206. root->rule != NTFS_COLLATION_TYPE_FILENAME) ||
  2207. (le32_to_cpu(root->index_block_size) !=
  2208. (block_clst << index_bits)) ||
  2209. (block_clst != 1 && block_clst != 2 && block_clst != 4 &&
  2210. block_clst != 8 && block_clst != 0x10 && block_clst != 0x20 &&
  2211. block_clst != 0x40 && block_clst != 0x80)) {
  2212. return false;
  2213. }
  2214. ret = check_index_header(&root->ihdr,
  2215. le32_to_cpu(attr->res.data_size) -
  2216. offsetof(struct INDEX_ROOT, ihdr));
  2217. return ret;
  2218. }
  2219. static inline bool check_attr(const struct MFT_REC *rec,
  2220. const struct ATTRIB *attr,
  2221. struct ntfs_sb_info *sbi)
  2222. {
  2223. u32 asize = le32_to_cpu(attr->size);
  2224. u32 rsize = 0;
  2225. u64 dsize, svcn, evcn;
  2226. u16 run_off;
  2227. /* Check the fixed part of the attribute record header. */
  2228. if (asize >= sbi->record_size ||
  2229. asize + PtrOffset(rec, attr) >= sbi->record_size ||
  2230. (attr->name_len &&
  2231. le16_to_cpu(attr->name_off) + attr->name_len * sizeof(short) >
  2232. asize)) {
  2233. return false;
  2234. }
  2235. /* Check the attribute fields. */
  2236. switch (attr->non_res) {
  2237. case 0:
  2238. rsize = le32_to_cpu(attr->res.data_size);
  2239. if (rsize >= asize ||
  2240. le16_to_cpu(attr->res.data_off) + rsize > asize) {
  2241. return false;
  2242. }
  2243. break;
  2244. case 1:
  2245. dsize = le64_to_cpu(attr->nres.data_size);
  2246. svcn = le64_to_cpu(attr->nres.svcn);
  2247. evcn = le64_to_cpu(attr->nres.evcn);
  2248. run_off = le16_to_cpu(attr->nres.run_off);
  2249. if (svcn > evcn + 1 || run_off >= asize ||
  2250. le64_to_cpu(attr->nres.valid_size) > dsize ||
  2251. dsize > le64_to_cpu(attr->nres.alloc_size)) {
  2252. return false;
  2253. }
  2254. if (run_off > asize)
  2255. return false;
  2256. if (run_unpack(NULL, sbi, 0, svcn, evcn, svcn,
  2257. Add2Ptr(attr, run_off), asize - run_off) < 0) {
  2258. return false;
  2259. }
  2260. return true;
  2261. default:
  2262. return false;
  2263. }
  2264. switch (attr->type) {
  2265. case ATTR_NAME:
  2266. if (fname_full_size(Add2Ptr(
  2267. attr, le16_to_cpu(attr->res.data_off))) > asize) {
  2268. return false;
  2269. }
  2270. break;
  2271. case ATTR_ROOT:
  2272. return check_index_root(attr, sbi);
  2273. case ATTR_STD:
  2274. if (rsize < sizeof(struct ATTR_STD_INFO5) &&
  2275. rsize != sizeof(struct ATTR_STD_INFO)) {
  2276. return false;
  2277. }
  2278. break;
  2279. case ATTR_LIST:
  2280. case ATTR_ID:
  2281. case ATTR_SECURE:
  2282. case ATTR_LABEL:
  2283. case ATTR_VOL_INFO:
  2284. case ATTR_DATA:
  2285. case ATTR_ALLOC:
  2286. case ATTR_BITMAP:
  2287. case ATTR_REPARSE:
  2288. case ATTR_EA_INFO:
  2289. case ATTR_EA:
  2290. case ATTR_PROPERTYSET:
  2291. case ATTR_LOGGED_UTILITY_STREAM:
  2292. break;
  2293. default:
  2294. return false;
  2295. }
  2296. return true;
  2297. }
  2298. static inline bool check_file_record(const struct MFT_REC *rec,
  2299. const struct MFT_REC *rec2,
  2300. struct ntfs_sb_info *sbi)
  2301. {
  2302. const struct ATTRIB *attr;
  2303. u16 fo = le16_to_cpu(rec->rhdr.fix_off);
  2304. u16 fn = le16_to_cpu(rec->rhdr.fix_num);
  2305. u16 ao = le16_to_cpu(rec->attr_off);
  2306. u32 rs = sbi->record_size;
  2307. /* Check the file record header for consistency. */
  2308. if (rec->rhdr.sign != NTFS_FILE_SIGNATURE ||
  2309. fo > (SECTOR_SIZE - ((rs >> SECTOR_SHIFT) + 1) * sizeof(short)) ||
  2310. (fn - 1) * SECTOR_SIZE != rs || ao < MFTRECORD_FIXUP_OFFSET_1 ||
  2311. ao > sbi->record_size - SIZEOF_RESIDENT || !is_rec_inuse(rec) ||
  2312. le32_to_cpu(rec->total) != rs) {
  2313. return false;
  2314. }
  2315. /* Loop to check all of the attributes. */
  2316. for (attr = Add2Ptr(rec, ao); attr->type != ATTR_END;
  2317. attr = Add2Ptr(attr, le32_to_cpu(attr->size))) {
  2318. if (check_attr(rec, attr, sbi))
  2319. continue;
  2320. return false;
  2321. }
  2322. return true;
  2323. }
  2324. static inline int check_lsn(const struct NTFS_RECORD_HEADER *hdr,
  2325. const u64 *rlsn)
  2326. {
  2327. u64 lsn;
  2328. if (!rlsn)
  2329. return true;
  2330. lsn = le64_to_cpu(hdr->lsn);
  2331. if (hdr->sign == NTFS_HOLE_SIGNATURE)
  2332. return false;
  2333. if (*rlsn > lsn)
  2334. return true;
  2335. return false;
  2336. }
  2337. static inline bool check_if_attr(const struct MFT_REC *rec,
  2338. const struct LOG_REC_HDR *lrh)
  2339. {
  2340. u16 ro = le16_to_cpu(lrh->record_off);
  2341. u16 o = le16_to_cpu(rec->attr_off);
  2342. const struct ATTRIB *attr = Add2Ptr(rec, o);
  2343. while (o < ro) {
  2344. u32 asize;
  2345. if (attr->type == ATTR_END)
  2346. break;
  2347. asize = le32_to_cpu(attr->size);
  2348. if (!asize)
  2349. break;
  2350. o += asize;
  2351. attr = Add2Ptr(attr, asize);
  2352. }
  2353. return o == ro;
  2354. }
  2355. static inline bool check_if_index_root(const struct MFT_REC *rec,
  2356. const struct LOG_REC_HDR *lrh)
  2357. {
  2358. u16 ro = le16_to_cpu(lrh->record_off);
  2359. u16 o = le16_to_cpu(rec->attr_off);
  2360. const struct ATTRIB *attr = Add2Ptr(rec, o);
  2361. while (o < ro) {
  2362. u32 asize;
  2363. if (attr->type == ATTR_END)
  2364. break;
  2365. asize = le32_to_cpu(attr->size);
  2366. if (!asize)
  2367. break;
  2368. o += asize;
  2369. attr = Add2Ptr(attr, asize);
  2370. }
  2371. return o == ro && attr->type == ATTR_ROOT;
  2372. }
  2373. static inline bool check_if_root_index(const struct ATTRIB *attr,
  2374. const struct INDEX_HDR *hdr,
  2375. const struct LOG_REC_HDR *lrh)
  2376. {
  2377. u16 ao = le16_to_cpu(lrh->attr_off);
  2378. u32 de_off = le32_to_cpu(hdr->de_off);
  2379. u32 o = PtrOffset(attr, hdr) + de_off;
  2380. const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
  2381. u32 asize = le32_to_cpu(attr->size);
  2382. while (o < ao) {
  2383. u16 esize;
  2384. if (o >= asize)
  2385. break;
  2386. esize = le16_to_cpu(e->size);
  2387. if (!esize)
  2388. break;
  2389. o += esize;
  2390. e = Add2Ptr(e, esize);
  2391. }
  2392. return o == ao;
  2393. }
  2394. static inline bool check_if_alloc_index(const struct INDEX_HDR *hdr,
  2395. u32 attr_off)
  2396. {
  2397. u32 de_off = le32_to_cpu(hdr->de_off);
  2398. u32 o = offsetof(struct INDEX_BUFFER, ihdr) + de_off;
  2399. const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
  2400. u32 used = le32_to_cpu(hdr->used);
  2401. while (o < attr_off) {
  2402. u16 esize;
  2403. if (de_off >= used)
  2404. break;
  2405. esize = le16_to_cpu(e->size);
  2406. if (!esize)
  2407. break;
  2408. o += esize;
  2409. de_off += esize;
  2410. e = Add2Ptr(e, esize);
  2411. }
  2412. return o == attr_off;
  2413. }
  2414. static inline void change_attr_size(struct MFT_REC *rec, struct ATTRIB *attr,
  2415. u32 nsize)
  2416. {
  2417. u32 asize = le32_to_cpu(attr->size);
  2418. int dsize = nsize - asize;
  2419. u8 *next = Add2Ptr(attr, asize);
  2420. u32 used = le32_to_cpu(rec->used);
  2421. memmove(Add2Ptr(attr, nsize), next, used - PtrOffset(rec, next));
  2422. rec->used = cpu_to_le32(used + dsize);
  2423. attr->size = cpu_to_le32(nsize);
  2424. }
  2425. struct OpenAttr {
  2426. struct ATTRIB *attr;
  2427. struct runs_tree *run1;
  2428. struct runs_tree run0;
  2429. struct ntfs_inode *ni;
  2430. // CLST rno;
  2431. };
  2432. /*
  2433. * cmp_type_and_name
  2434. *
  2435. * Return: 0 if 'attr' has the same type and name.
  2436. */
  2437. static inline int cmp_type_and_name(const struct ATTRIB *a1,
  2438. const struct ATTRIB *a2)
  2439. {
  2440. return a1->type != a2->type || a1->name_len != a2->name_len ||
  2441. (a1->name_len && memcmp(attr_name(a1), attr_name(a2),
  2442. a1->name_len * sizeof(short)));
  2443. }
  2444. static struct OpenAttr *find_loaded_attr(struct ntfs_log *log,
  2445. const struct ATTRIB *attr, CLST rno)
  2446. {
  2447. struct OPEN_ATTR_ENRTY *oe = NULL;
  2448. while ((oe = enum_rstbl(log->open_attr_tbl, oe))) {
  2449. struct OpenAttr *op_attr;
  2450. if (ino_get(&oe->ref) != rno)
  2451. continue;
  2452. op_attr = (struct OpenAttr *)oe->ptr;
  2453. if (!cmp_type_and_name(op_attr->attr, attr))
  2454. return op_attr;
  2455. }
  2456. return NULL;
  2457. }
  2458. static struct ATTRIB *attr_create_nonres_log(struct ntfs_sb_info *sbi,
  2459. enum ATTR_TYPE type, u64 size,
  2460. const u16 *name, size_t name_len,
  2461. __le16 flags)
  2462. {
  2463. struct ATTRIB *attr;
  2464. u32 name_size = ALIGN(name_len * sizeof(short), 8);
  2465. bool is_ext = flags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED);
  2466. u32 asize = name_size +
  2467. (is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT);
  2468. attr = kzalloc(asize, GFP_NOFS);
  2469. if (!attr)
  2470. return NULL;
  2471. attr->type = type;
  2472. attr->size = cpu_to_le32(asize);
  2473. attr->flags = flags;
  2474. attr->non_res = 1;
  2475. attr->name_len = name_len;
  2476. attr->nres.evcn = cpu_to_le64((u64)bytes_to_cluster(sbi, size) - 1);
  2477. attr->nres.alloc_size = cpu_to_le64(ntfs_up_cluster(sbi, size));
  2478. attr->nres.data_size = cpu_to_le64(size);
  2479. attr->nres.valid_size = attr->nres.data_size;
  2480. if (is_ext) {
  2481. attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
  2482. if (is_attr_compressed(attr))
  2483. attr->nres.c_unit = NTFS_LZNT_CUNIT;
  2484. attr->nres.run_off =
  2485. cpu_to_le16(SIZEOF_NONRESIDENT_EX + name_size);
  2486. memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT_EX), name,
  2487. name_len * sizeof(short));
  2488. } else {
  2489. attr->name_off = SIZEOF_NONRESIDENT_LE;
  2490. attr->nres.run_off =
  2491. cpu_to_le16(SIZEOF_NONRESIDENT + name_size);
  2492. memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT), name,
  2493. name_len * sizeof(short));
  2494. }
  2495. return attr;
  2496. }
  2497. /*
  2498. * do_action - Common routine for the Redo and Undo Passes.
  2499. * @rlsn: If it is NULL then undo.
  2500. */
  2501. static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
  2502. const struct LOG_REC_HDR *lrh, u32 op, void *data,
  2503. u32 dlen, u32 rec_len, const u64 *rlsn)
  2504. {
  2505. int err = 0;
  2506. struct ntfs_sb_info *sbi = log->ni->mi.sbi;
  2507. struct inode *inode = NULL, *inode_parent;
  2508. struct mft_inode *mi = NULL, *mi2_child = NULL;
  2509. CLST rno = 0, rno_base = 0;
  2510. struct INDEX_BUFFER *ib = NULL;
  2511. struct MFT_REC *rec = NULL;
  2512. struct ATTRIB *attr = NULL, *attr2;
  2513. struct INDEX_HDR *hdr;
  2514. struct INDEX_ROOT *root;
  2515. struct NTFS_DE *e, *e1, *e2;
  2516. struct NEW_ATTRIBUTE_SIZES *new_sz;
  2517. struct ATTR_FILE_NAME *fname;
  2518. struct OpenAttr *oa, *oa2;
  2519. u32 nsize, t32, asize, used, esize, off, bits;
  2520. u16 id, id2;
  2521. u32 record_size = sbi->record_size;
  2522. u64 t64;
  2523. u16 roff = le16_to_cpu(lrh->record_off);
  2524. u16 aoff = le16_to_cpu(lrh->attr_off);
  2525. u64 lco = 0;
  2526. u64 cbo = (u64)le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
  2527. u64 tvo = le64_to_cpu(lrh->target_vcn) << sbi->cluster_bits;
  2528. u64 vbo = cbo + tvo;
  2529. void *buffer_le = NULL;
  2530. u32 bytes = 0;
  2531. bool a_dirty = false;
  2532. u16 data_off;
  2533. oa = oe->ptr;
  2534. /* Big switch to prepare. */
  2535. switch (op) {
  2536. /* ============================================================
  2537. * Process MFT records, as described by the current log record.
  2538. * ============================================================
  2539. */
  2540. case InitializeFileRecordSegment:
  2541. case DeallocateFileRecordSegment:
  2542. case WriteEndOfFileRecordSegment:
  2543. case CreateAttribute:
  2544. case DeleteAttribute:
  2545. case UpdateResidentValue:
  2546. case UpdateMappingPairs:
  2547. case SetNewAttributeSizes:
  2548. case AddIndexEntryRoot:
  2549. case DeleteIndexEntryRoot:
  2550. case SetIndexEntryVcnRoot:
  2551. case UpdateFileNameRoot:
  2552. case UpdateRecordDataRoot:
  2553. case ZeroEndOfFileRecord:
  2554. rno = vbo >> sbi->record_bits;
  2555. inode = ilookup(sbi->sb, rno);
  2556. if (inode) {
  2557. mi = &ntfs_i(inode)->mi;
  2558. } else if (op == InitializeFileRecordSegment) {
  2559. mi = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
  2560. if (!mi)
  2561. return -ENOMEM;
  2562. err = mi_format_new(mi, sbi, rno, 0, false);
  2563. if (err)
  2564. goto out;
  2565. } else {
  2566. /* Read from disk. */
  2567. err = mi_get(sbi, rno, &mi);
  2568. if (err)
  2569. return err;
  2570. }
  2571. rec = mi->mrec;
  2572. if (op == DeallocateFileRecordSegment)
  2573. goto skip_load_parent;
  2574. if (InitializeFileRecordSegment != op) {
  2575. if (rec->rhdr.sign == NTFS_BAAD_SIGNATURE)
  2576. goto dirty_vol;
  2577. if (!check_lsn(&rec->rhdr, rlsn))
  2578. goto out;
  2579. if (!check_file_record(rec, NULL, sbi))
  2580. goto dirty_vol;
  2581. attr = Add2Ptr(rec, roff);
  2582. }
  2583. if (is_rec_base(rec) || InitializeFileRecordSegment == op) {
  2584. rno_base = rno;
  2585. goto skip_load_parent;
  2586. }
  2587. rno_base = ino_get(&rec->parent_ref);
  2588. inode_parent = ntfs_iget5(sbi->sb, &rec->parent_ref, NULL);
  2589. if (IS_ERR(inode_parent))
  2590. goto skip_load_parent;
  2591. if (is_bad_inode(inode_parent)) {
  2592. iput(inode_parent);
  2593. goto skip_load_parent;
  2594. }
  2595. if (ni_load_mi_ex(ntfs_i(inode_parent), rno, &mi2_child)) {
  2596. iput(inode_parent);
  2597. } else {
  2598. if (mi2_child->mrec != mi->mrec)
  2599. memcpy(mi2_child->mrec, mi->mrec,
  2600. sbi->record_size);
  2601. if (inode)
  2602. iput(inode);
  2603. else if (mi)
  2604. mi_put(mi);
  2605. inode = inode_parent;
  2606. mi = mi2_child;
  2607. rec = mi2_child->mrec;
  2608. attr = Add2Ptr(rec, roff);
  2609. }
  2610. skip_load_parent:
  2611. inode_parent = NULL;
  2612. break;
  2613. /*
  2614. * Process attributes, as described by the current log record.
  2615. */
  2616. case UpdateNonresidentValue:
  2617. case AddIndexEntryAllocation:
  2618. case DeleteIndexEntryAllocation:
  2619. case WriteEndOfIndexBuffer:
  2620. case SetIndexEntryVcnAllocation:
  2621. case UpdateFileNameAllocation:
  2622. case SetBitsInNonresidentBitMap:
  2623. case ClearBitsInNonresidentBitMap:
  2624. case UpdateRecordDataAllocation:
  2625. attr = oa->attr;
  2626. bytes = UpdateNonresidentValue == op ? dlen : 0;
  2627. lco = (u64)le16_to_cpu(lrh->lcns_follow) << sbi->cluster_bits;
  2628. if (attr->type == ATTR_ALLOC) {
  2629. t32 = le32_to_cpu(oe->bytes_per_index);
  2630. if (bytes < t32)
  2631. bytes = t32;
  2632. }
  2633. if (!bytes)
  2634. bytes = lco - cbo;
  2635. bytes += roff;
  2636. if (attr->type == ATTR_ALLOC)
  2637. bytes = (bytes + 511) & ~511; // align
  2638. buffer_le = kmalloc(bytes, GFP_NOFS);
  2639. if (!buffer_le)
  2640. return -ENOMEM;
  2641. err = ntfs_read_run_nb(sbi, oa->run1, vbo, buffer_le, bytes,
  2642. NULL);
  2643. if (err)
  2644. goto out;
  2645. if (attr->type == ATTR_ALLOC && *(int *)buffer_le)
  2646. ntfs_fix_post_read(buffer_le, bytes, false);
  2647. break;
  2648. default:
  2649. WARN_ON(1);
  2650. }
  2651. /* Big switch to do operation. */
  2652. switch (op) {
  2653. case InitializeFileRecordSegment:
  2654. if (roff + dlen > record_size)
  2655. goto dirty_vol;
  2656. memcpy(Add2Ptr(rec, roff), data, dlen);
  2657. mi->dirty = true;
  2658. break;
  2659. case DeallocateFileRecordSegment:
  2660. clear_rec_inuse(rec);
  2661. le16_add_cpu(&rec->seq, 1);
  2662. mi->dirty = true;
  2663. break;
  2664. case WriteEndOfFileRecordSegment:
  2665. attr2 = (struct ATTRIB *)data;
  2666. if (!check_if_attr(rec, lrh) || roff + dlen > record_size)
  2667. goto dirty_vol;
  2668. memmove(attr, attr2, dlen);
  2669. rec->used = cpu_to_le32(ALIGN(roff + dlen, 8));
  2670. mi->dirty = true;
  2671. break;
  2672. case CreateAttribute:
  2673. attr2 = (struct ATTRIB *)data;
  2674. asize = le32_to_cpu(attr2->size);
  2675. used = le32_to_cpu(rec->used);
  2676. if (!check_if_attr(rec, lrh) || dlen < SIZEOF_RESIDENT ||
  2677. !IS_ALIGNED(asize, 8) ||
  2678. Add2Ptr(attr2, asize) > Add2Ptr(lrh, rec_len) ||
  2679. dlen > record_size - used) {
  2680. goto dirty_vol;
  2681. }
  2682. memmove(Add2Ptr(attr, asize), attr, used - roff);
  2683. memcpy(attr, attr2, asize);
  2684. rec->used = cpu_to_le32(used + asize);
  2685. id = le16_to_cpu(rec->next_attr_id);
  2686. id2 = le16_to_cpu(attr2->id);
  2687. if (id <= id2)
  2688. rec->next_attr_id = cpu_to_le16(id2 + 1);
  2689. if (is_attr_indexed(attr))
  2690. le16_add_cpu(&rec->hard_links, 1);
  2691. oa2 = find_loaded_attr(log, attr, rno_base);
  2692. if (oa2) {
  2693. void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
  2694. GFP_NOFS);
  2695. if (p2) {
  2696. // run_close(oa2->run1);
  2697. kfree(oa2->attr);
  2698. oa2->attr = p2;
  2699. }
  2700. }
  2701. mi->dirty = true;
  2702. break;
  2703. case DeleteAttribute:
  2704. asize = le32_to_cpu(attr->size);
  2705. used = le32_to_cpu(rec->used);
  2706. if (!check_if_attr(rec, lrh))
  2707. goto dirty_vol;
  2708. rec->used = cpu_to_le32(used - asize);
  2709. if (is_attr_indexed(attr))
  2710. le16_add_cpu(&rec->hard_links, -1);
  2711. memmove(attr, Add2Ptr(attr, asize), used - asize - roff);
  2712. mi->dirty = true;
  2713. break;
  2714. case UpdateResidentValue:
  2715. nsize = aoff + dlen;
  2716. if (!check_if_attr(rec, lrh))
  2717. goto dirty_vol;
  2718. asize = le32_to_cpu(attr->size);
  2719. used = le32_to_cpu(rec->used);
  2720. if (lrh->redo_len == lrh->undo_len) {
  2721. if (nsize > asize)
  2722. goto dirty_vol;
  2723. goto move_data;
  2724. }
  2725. if (nsize > asize && nsize - asize > record_size - used)
  2726. goto dirty_vol;
  2727. nsize = ALIGN(nsize, 8);
  2728. data_off = le16_to_cpu(attr->res.data_off);
  2729. if (nsize < asize) {
  2730. memmove(Add2Ptr(attr, aoff), data, dlen);
  2731. data = NULL; // To skip below memmove().
  2732. }
  2733. memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
  2734. used - le16_to_cpu(lrh->record_off) - asize);
  2735. rec->used = cpu_to_le32(used + nsize - asize);
  2736. attr->size = cpu_to_le32(nsize);
  2737. attr->res.data_size = cpu_to_le32(aoff + dlen - data_off);
  2738. move_data:
  2739. if (data)
  2740. memmove(Add2Ptr(attr, aoff), data, dlen);
  2741. oa2 = find_loaded_attr(log, attr, rno_base);
  2742. if (oa2) {
  2743. void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
  2744. GFP_NOFS);
  2745. if (p2) {
  2746. // run_close(&oa2->run0);
  2747. oa2->run1 = &oa2->run0;
  2748. kfree(oa2->attr);
  2749. oa2->attr = p2;
  2750. }
  2751. }
  2752. mi->dirty = true;
  2753. break;
  2754. case UpdateMappingPairs:
  2755. nsize = aoff + dlen;
  2756. asize = le32_to_cpu(attr->size);
  2757. used = le32_to_cpu(rec->used);
  2758. if (!check_if_attr(rec, lrh) || !attr->non_res ||
  2759. aoff < le16_to_cpu(attr->nres.run_off) || aoff > asize ||
  2760. (nsize > asize && nsize - asize > record_size - used)) {
  2761. goto dirty_vol;
  2762. }
  2763. nsize = ALIGN(nsize, 8);
  2764. memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
  2765. used - le16_to_cpu(lrh->record_off) - asize);
  2766. rec->used = cpu_to_le32(used + nsize - asize);
  2767. attr->size = cpu_to_le32(nsize);
  2768. memmove(Add2Ptr(attr, aoff), data, dlen);
  2769. if (run_get_highest_vcn(le64_to_cpu(attr->nres.svcn),
  2770. attr_run(attr), &t64)) {
  2771. goto dirty_vol;
  2772. }
  2773. attr->nres.evcn = cpu_to_le64(t64);
  2774. oa2 = find_loaded_attr(log, attr, rno_base);
  2775. if (oa2 && oa2->attr->non_res)
  2776. oa2->attr->nres.evcn = attr->nres.evcn;
  2777. mi->dirty = true;
  2778. break;
  2779. case SetNewAttributeSizes:
  2780. new_sz = data;
  2781. if (!check_if_attr(rec, lrh) || !attr->non_res)
  2782. goto dirty_vol;
  2783. attr->nres.alloc_size = new_sz->alloc_size;
  2784. attr->nres.data_size = new_sz->data_size;
  2785. attr->nres.valid_size = new_sz->valid_size;
  2786. if (dlen >= sizeof(struct NEW_ATTRIBUTE_SIZES))
  2787. attr->nres.total_size = new_sz->total_size;
  2788. oa2 = find_loaded_attr(log, attr, rno_base);
  2789. if (oa2) {
  2790. void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
  2791. GFP_NOFS);
  2792. if (p2) {
  2793. kfree(oa2->attr);
  2794. oa2->attr = p2;
  2795. }
  2796. }
  2797. mi->dirty = true;
  2798. break;
  2799. case AddIndexEntryRoot:
  2800. e = (struct NTFS_DE *)data;
  2801. esize = le16_to_cpu(e->size);
  2802. root = resident_data(attr);
  2803. hdr = &root->ihdr;
  2804. used = le32_to_cpu(hdr->used);
  2805. if (!check_if_index_root(rec, lrh) ||
  2806. !check_if_root_index(attr, hdr, lrh) ||
  2807. Add2Ptr(data, esize) > Add2Ptr(lrh, rec_len) ||
  2808. esize > le32_to_cpu(rec->total) - le32_to_cpu(rec->used)) {
  2809. goto dirty_vol;
  2810. }
  2811. e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
  2812. change_attr_size(rec, attr, le32_to_cpu(attr->size) + esize);
  2813. memmove(Add2Ptr(e1, esize), e1,
  2814. PtrOffset(e1, Add2Ptr(hdr, used)));
  2815. memmove(e1, e, esize);
  2816. le32_add_cpu(&attr->res.data_size, esize);
  2817. hdr->used = cpu_to_le32(used + esize);
  2818. le32_add_cpu(&hdr->total, esize);
  2819. mi->dirty = true;
  2820. break;
  2821. case DeleteIndexEntryRoot:
  2822. root = resident_data(attr);
  2823. hdr = &root->ihdr;
  2824. used = le32_to_cpu(hdr->used);
  2825. if (!check_if_index_root(rec, lrh) ||
  2826. !check_if_root_index(attr, hdr, lrh)) {
  2827. goto dirty_vol;
  2828. }
  2829. e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
  2830. esize = le16_to_cpu(e1->size);
  2831. e2 = Add2Ptr(e1, esize);
  2832. memmove(e1, e2, PtrOffset(e2, Add2Ptr(hdr, used)));
  2833. le32_sub_cpu(&attr->res.data_size, esize);
  2834. hdr->used = cpu_to_le32(used - esize);
  2835. le32_sub_cpu(&hdr->total, esize);
  2836. change_attr_size(rec, attr, le32_to_cpu(attr->size) - esize);
  2837. mi->dirty = true;
  2838. break;
  2839. case SetIndexEntryVcnRoot:
  2840. root = resident_data(attr);
  2841. hdr = &root->ihdr;
  2842. if (!check_if_index_root(rec, lrh) ||
  2843. !check_if_root_index(attr, hdr, lrh)) {
  2844. goto dirty_vol;
  2845. }
  2846. e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
  2847. de_set_vbn_le(e, *(__le64 *)data);
  2848. mi->dirty = true;
  2849. break;
  2850. case UpdateFileNameRoot:
  2851. root = resident_data(attr);
  2852. hdr = &root->ihdr;
  2853. if (!check_if_index_root(rec, lrh) ||
  2854. !check_if_root_index(attr, hdr, lrh)) {
  2855. goto dirty_vol;
  2856. }
  2857. e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
  2858. fname = (struct ATTR_FILE_NAME *)(e + 1);
  2859. memmove(&fname->dup, data, sizeof(fname->dup)); //
  2860. mi->dirty = true;
  2861. break;
  2862. case UpdateRecordDataRoot:
  2863. root = resident_data(attr);
  2864. hdr = &root->ihdr;
  2865. if (!check_if_index_root(rec, lrh) ||
  2866. !check_if_root_index(attr, hdr, lrh)) {
  2867. goto dirty_vol;
  2868. }
  2869. e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
  2870. memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
  2871. mi->dirty = true;
  2872. break;
  2873. case ZeroEndOfFileRecord:
  2874. if (roff + dlen > record_size)
  2875. goto dirty_vol;
  2876. memset(attr, 0, dlen);
  2877. mi->dirty = true;
  2878. break;
  2879. case UpdateNonresidentValue:
  2880. if (lco < cbo + roff + dlen)
  2881. goto dirty_vol;
  2882. memcpy(Add2Ptr(buffer_le, roff), data, dlen);
  2883. a_dirty = true;
  2884. if (attr->type == ATTR_ALLOC)
  2885. ntfs_fix_pre_write(buffer_le, bytes);
  2886. break;
  2887. case AddIndexEntryAllocation:
  2888. ib = Add2Ptr(buffer_le, roff);
  2889. hdr = &ib->ihdr;
  2890. e = data;
  2891. esize = le16_to_cpu(e->size);
  2892. e1 = Add2Ptr(ib, aoff);
  2893. if (is_baad(&ib->rhdr))
  2894. goto dirty_vol;
  2895. if (!check_lsn(&ib->rhdr, rlsn))
  2896. goto out;
  2897. used = le32_to_cpu(hdr->used);
  2898. if (!check_index_buffer(ib, bytes) ||
  2899. !check_if_alloc_index(hdr, aoff) ||
  2900. Add2Ptr(e, esize) > Add2Ptr(lrh, rec_len) ||
  2901. used + esize > le32_to_cpu(hdr->total)) {
  2902. goto dirty_vol;
  2903. }
  2904. memmove(Add2Ptr(e1, esize), e1,
  2905. PtrOffset(e1, Add2Ptr(hdr, used)));
  2906. memcpy(e1, e, esize);
  2907. hdr->used = cpu_to_le32(used + esize);
  2908. a_dirty = true;
  2909. ntfs_fix_pre_write(&ib->rhdr, bytes);
  2910. break;
  2911. case DeleteIndexEntryAllocation:
  2912. ib = Add2Ptr(buffer_le, roff);
  2913. hdr = &ib->ihdr;
  2914. e = Add2Ptr(ib, aoff);
  2915. esize = le16_to_cpu(e->size);
  2916. if (is_baad(&ib->rhdr))
  2917. goto dirty_vol;
  2918. if (!check_lsn(&ib->rhdr, rlsn))
  2919. goto out;
  2920. if (!check_index_buffer(ib, bytes) ||
  2921. !check_if_alloc_index(hdr, aoff)) {
  2922. goto dirty_vol;
  2923. }
  2924. e1 = Add2Ptr(e, esize);
  2925. nsize = esize;
  2926. used = le32_to_cpu(hdr->used);
  2927. memmove(e, e1, PtrOffset(e1, Add2Ptr(hdr, used)));
  2928. hdr->used = cpu_to_le32(used - nsize);
  2929. a_dirty = true;
  2930. ntfs_fix_pre_write(&ib->rhdr, bytes);
  2931. break;
  2932. case WriteEndOfIndexBuffer:
  2933. ib = Add2Ptr(buffer_le, roff);
  2934. hdr = &ib->ihdr;
  2935. e = Add2Ptr(ib, aoff);
  2936. if (is_baad(&ib->rhdr))
  2937. goto dirty_vol;
  2938. if (!check_lsn(&ib->rhdr, rlsn))
  2939. goto out;
  2940. if (!check_index_buffer(ib, bytes) ||
  2941. !check_if_alloc_index(hdr, aoff) ||
  2942. aoff + dlen > offsetof(struct INDEX_BUFFER, ihdr) +
  2943. le32_to_cpu(hdr->total)) {
  2944. goto dirty_vol;
  2945. }
  2946. hdr->used = cpu_to_le32(dlen + PtrOffset(hdr, e));
  2947. memmove(e, data, dlen);
  2948. a_dirty = true;
  2949. ntfs_fix_pre_write(&ib->rhdr, bytes);
  2950. break;
  2951. case SetIndexEntryVcnAllocation:
  2952. ib = Add2Ptr(buffer_le, roff);
  2953. hdr = &ib->ihdr;
  2954. e = Add2Ptr(ib, aoff);
  2955. if (is_baad(&ib->rhdr))
  2956. goto dirty_vol;
  2957. if (!check_lsn(&ib->rhdr, rlsn))
  2958. goto out;
  2959. if (!check_index_buffer(ib, bytes) ||
  2960. !check_if_alloc_index(hdr, aoff)) {
  2961. goto dirty_vol;
  2962. }
  2963. de_set_vbn_le(e, *(__le64 *)data);
  2964. a_dirty = true;
  2965. ntfs_fix_pre_write(&ib->rhdr, bytes);
  2966. break;
  2967. case UpdateFileNameAllocation:
  2968. ib = Add2Ptr(buffer_le, roff);
  2969. hdr = &ib->ihdr;
  2970. e = Add2Ptr(ib, aoff);
  2971. if (is_baad(&ib->rhdr))
  2972. goto dirty_vol;
  2973. if (!check_lsn(&ib->rhdr, rlsn))
  2974. goto out;
  2975. if (!check_index_buffer(ib, bytes) ||
  2976. !check_if_alloc_index(hdr, aoff)) {
  2977. goto dirty_vol;
  2978. }
  2979. fname = (struct ATTR_FILE_NAME *)(e + 1);
  2980. memmove(&fname->dup, data, sizeof(fname->dup));
  2981. a_dirty = true;
  2982. ntfs_fix_pre_write(&ib->rhdr, bytes);
  2983. break;
  2984. case SetBitsInNonresidentBitMap:
  2985. off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
  2986. bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
  2987. if (cbo + (off + 7) / 8 > lco ||
  2988. cbo + ((off + bits + 7) / 8) > lco) {
  2989. goto dirty_vol;
  2990. }
  2991. ntfs_bitmap_set_le(Add2Ptr(buffer_le, roff), off, bits);
  2992. a_dirty = true;
  2993. break;
  2994. case ClearBitsInNonresidentBitMap:
  2995. off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
  2996. bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
  2997. if (cbo + (off + 7) / 8 > lco ||
  2998. cbo + ((off + bits + 7) / 8) > lco) {
  2999. goto dirty_vol;
  3000. }
  3001. ntfs_bitmap_clear_le(Add2Ptr(buffer_le, roff), off, bits);
  3002. a_dirty = true;
  3003. break;
  3004. case UpdateRecordDataAllocation:
  3005. ib = Add2Ptr(buffer_le, roff);
  3006. hdr = &ib->ihdr;
  3007. e = Add2Ptr(ib, aoff);
  3008. if (is_baad(&ib->rhdr))
  3009. goto dirty_vol;
  3010. if (!check_lsn(&ib->rhdr, rlsn))
  3011. goto out;
  3012. if (!check_index_buffer(ib, bytes) ||
  3013. !check_if_alloc_index(hdr, aoff)) {
  3014. goto dirty_vol;
  3015. }
  3016. memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
  3017. a_dirty = true;
  3018. ntfs_fix_pre_write(&ib->rhdr, bytes);
  3019. break;
  3020. default:
  3021. WARN_ON(1);
  3022. }
  3023. if (rlsn) {
  3024. __le64 t64 = cpu_to_le64(*rlsn);
  3025. if (rec)
  3026. rec->rhdr.lsn = t64;
  3027. if (ib)
  3028. ib->rhdr.lsn = t64;
  3029. }
  3030. if (mi && mi->dirty) {
  3031. err = mi_write(mi, 0);
  3032. if (err)
  3033. goto out;
  3034. }
  3035. if (a_dirty) {
  3036. attr = oa->attr;
  3037. err = ntfs_sb_write_run(sbi, oa->run1, vbo, buffer_le, bytes,
  3038. 0);
  3039. if (err)
  3040. goto out;
  3041. }
  3042. out:
  3043. if (inode)
  3044. iput(inode);
  3045. else if (mi != mi2_child)
  3046. mi_put(mi);
  3047. kfree(buffer_le);
  3048. return err;
  3049. dirty_vol:
  3050. log->set_dirty = true;
  3051. goto out;
  3052. }
  3053. /*
  3054. * log_replay - Replays log and empties it.
  3055. *
  3056. * This function is called during mount operation.
  3057. * It replays log and empties it.
  3058. * Initialized is set false if logfile contains '-1'.
  3059. */
  3060. int log_replay(struct ntfs_inode *ni, bool *initialized)
  3061. {
  3062. int err;
  3063. struct ntfs_sb_info *sbi = ni->mi.sbi;
  3064. struct ntfs_log *log;
  3065. u64 rec_lsn, checkpt_lsn = 0, rlsn = 0;
  3066. struct ATTR_NAME_ENTRY *attr_names = NULL;
  3067. u32 attr_names_bytes = 0;
  3068. u32 oatbl_bytes = 0;
  3069. struct RESTART_TABLE *dptbl = NULL;
  3070. struct RESTART_TABLE *trtbl = NULL;
  3071. const struct RESTART_TABLE *rt;
  3072. struct RESTART_TABLE *oatbl = NULL;
  3073. struct inode *inode;
  3074. struct OpenAttr *oa;
  3075. struct ntfs_inode *ni_oe;
  3076. struct ATTRIB *attr = NULL;
  3077. u64 size, vcn, undo_next_lsn;
  3078. CLST rno, lcn, lcn0, len0, clen;
  3079. void *data;
  3080. struct NTFS_RESTART *rst = NULL;
  3081. struct lcb *lcb = NULL;
  3082. struct OPEN_ATTR_ENRTY *oe;
  3083. struct ATTR_NAME_ENTRY *ane;
  3084. struct TRANSACTION_ENTRY *tr;
  3085. struct DIR_PAGE_ENTRY *dp;
  3086. u32 i, bytes_per_attr_entry;
  3087. u32 vbo, tail, off, dlen;
  3088. u32 saved_len, rec_len, transact_id;
  3089. bool use_second_page;
  3090. struct RESTART_AREA *ra2, *ra = NULL;
  3091. struct CLIENT_REC *ca, *cr;
  3092. __le16 client;
  3093. struct RESTART_HDR *rh;
  3094. const struct LFS_RECORD_HDR *frh;
  3095. const struct LOG_REC_HDR *lrh;
  3096. bool is_mapped;
  3097. bool is_ro = sb_rdonly(sbi->sb);
  3098. u64 t64;
  3099. u16 t16;
  3100. u32 t32;
  3101. log = kzalloc(sizeof(struct ntfs_log), GFP_NOFS);
  3102. if (!log)
  3103. return -ENOMEM;
  3104. log->ni = ni;
  3105. log->l_size = log->orig_file_size = ni->vfs_inode.i_size;
  3106. /* Get the size of page. NOTE: To replay we can use default page. */
  3107. #if PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <= DefaultLogPageSize * 2
  3108. log->page_size = norm_file_page(PAGE_SIZE, &log->l_size, true);
  3109. #else
  3110. log->page_size = norm_file_page(PAGE_SIZE, &log->l_size, false);
  3111. #endif
  3112. if (!log->page_size) {
  3113. err = -EINVAL;
  3114. goto out;
  3115. }
  3116. log->one_page_buf = kmalloc(log->page_size, GFP_NOFS);
  3117. if (!log->one_page_buf) {
  3118. err = -ENOMEM;
  3119. goto out;
  3120. }
  3121. log->page_mask = log->page_size - 1;
  3122. log->page_bits = blksize_bits(log->page_size);
  3123. /* Look for a restart area on the disk. */
  3124. err = log_read_rst(log, true, &log->rst_info);
  3125. if (err)
  3126. goto out;
  3127. /* remember 'initialized' */
  3128. *initialized = log->rst_info.initialized;
  3129. if (!log->rst_info.restart) {
  3130. if (log->rst_info.initialized) {
  3131. /* No restart area but the file is not initialized. */
  3132. err = -EINVAL;
  3133. goto out;
  3134. }
  3135. log_init_pg_hdr(log, 1, 1);
  3136. log_create(log, 0, get_random_u32(), false, false);
  3137. ra = log_create_ra(log);
  3138. if (!ra) {
  3139. err = -ENOMEM;
  3140. goto out;
  3141. }
  3142. log->ra = ra;
  3143. log->init_ra = true;
  3144. goto process_log;
  3145. }
  3146. /*
  3147. * If the restart offset above wasn't zero then we won't
  3148. * look for a second restart.
  3149. */
  3150. if (log->rst_info.vbo)
  3151. goto check_restart_area;
  3152. err = log_read_rst(log, false, &log->rst_info2);
  3153. if (err)
  3154. goto out;
  3155. /* Determine which restart area to use. */
  3156. if (!log->rst_info2.restart ||
  3157. log->rst_info2.last_lsn <= log->rst_info.last_lsn)
  3158. goto use_first_page;
  3159. use_second_page = true;
  3160. if (log->rst_info.chkdsk_was_run &&
  3161. log->page_size != log->rst_info.vbo) {
  3162. struct RECORD_PAGE_HDR *sp = NULL;
  3163. bool usa_error;
  3164. if (!read_log_page(log, log->page_size, &sp, &usa_error) &&
  3165. sp->rhdr.sign == NTFS_CHKD_SIGNATURE) {
  3166. use_second_page = false;
  3167. }
  3168. kfree(sp);
  3169. }
  3170. if (use_second_page) {
  3171. kfree(log->rst_info.r_page);
  3172. memcpy(&log->rst_info, &log->rst_info2,
  3173. sizeof(struct restart_info));
  3174. log->rst_info2.r_page = NULL;
  3175. }
  3176. use_first_page:
  3177. kfree(log->rst_info2.r_page);
  3178. check_restart_area:
  3179. /*
  3180. * If the restart area is at offset 0, we want
  3181. * to write the second restart area first.
  3182. */
  3183. log->init_ra = !!log->rst_info.vbo;
  3184. /* If we have a valid page then grab a pointer to the restart area. */
  3185. ra2 = log->rst_info.valid_page ?
  3186. Add2Ptr(log->rst_info.r_page,
  3187. le16_to_cpu(log->rst_info.r_page->ra_off)) :
  3188. NULL;
  3189. if (log->rst_info.chkdsk_was_run ||
  3190. (ra2 && ra2->client_idx[1] == LFS_NO_CLIENT_LE)) {
  3191. bool wrapped = false;
  3192. bool use_multi_page = false;
  3193. u32 open_log_count;
  3194. /* Do some checks based on whether we have a valid log page. */
  3195. open_log_count = log->rst_info.valid_page ?
  3196. le32_to_cpu(ra2->open_log_count) :
  3197. get_random_u32();
  3198. log_init_pg_hdr(log, 1, 1);
  3199. log_create(log, log->rst_info.last_lsn, open_log_count, wrapped,
  3200. use_multi_page);
  3201. ra = log_create_ra(log);
  3202. if (!ra) {
  3203. err = -ENOMEM;
  3204. goto out;
  3205. }
  3206. log->ra = ra;
  3207. /* Put the restart areas and initialize
  3208. * the log file as required.
  3209. */
  3210. goto process_log;
  3211. }
  3212. if (!ra2) {
  3213. err = -EINVAL;
  3214. goto out;
  3215. }
  3216. /*
  3217. * If the log page or the system page sizes have changed, we can't
  3218. * use the log file. We must use the system page size instead of the
  3219. * default size if there is not a clean shutdown.
  3220. */
  3221. t32 = le32_to_cpu(log->rst_info.r_page->sys_page_size);
  3222. if (log->page_size != t32) {
  3223. log->l_size = log->orig_file_size;
  3224. log->page_size = norm_file_page(t32, &log->l_size,
  3225. t32 == DefaultLogPageSize);
  3226. }
  3227. if (log->page_size != t32 ||
  3228. log->page_size != le32_to_cpu(log->rst_info.r_page->page_size)) {
  3229. err = -EINVAL;
  3230. goto out;
  3231. }
  3232. log->page_mask = log->page_size - 1;
  3233. log->page_bits = blksize_bits(log->page_size);
  3234. /* If the file size has shrunk then we won't mount it. */
  3235. if (log->l_size < le64_to_cpu(ra2->l_size)) {
  3236. err = -EINVAL;
  3237. goto out;
  3238. }
  3239. log_init_pg_hdr(log, le16_to_cpu(log->rst_info.r_page->major_ver),
  3240. le16_to_cpu(log->rst_info.r_page->minor_ver));
  3241. log->l_size = le64_to_cpu(ra2->l_size);
  3242. log->seq_num_bits = le32_to_cpu(ra2->seq_num_bits);
  3243. log->file_data_bits = sizeof(u64) * 8 - log->seq_num_bits;
  3244. log->seq_num_mask = (8 << log->file_data_bits) - 1;
  3245. log->last_lsn = le64_to_cpu(ra2->current_lsn);
  3246. log->seq_num = log->last_lsn >> log->file_data_bits;
  3247. log->ra_off = le16_to_cpu(log->rst_info.r_page->ra_off);
  3248. log->restart_size = log->sys_page_size - log->ra_off;
  3249. log->record_header_len = le16_to_cpu(ra2->rec_hdr_len);
  3250. log->ra_size = le16_to_cpu(ra2->ra_len);
  3251. log->data_off = le16_to_cpu(ra2->data_off);
  3252. log->data_size = log->page_size - log->data_off;
  3253. log->reserved = log->data_size - log->record_header_len;
  3254. vbo = lsn_to_vbo(log, log->last_lsn);
  3255. if (vbo < log->first_page) {
  3256. /* This is a pseudo lsn. */
  3257. log->l_flags |= NTFSLOG_NO_LAST_LSN;
  3258. log->next_page = log->first_page;
  3259. goto find_oldest;
  3260. }
  3261. /* Find the end of this log record. */
  3262. off = final_log_off(log, log->last_lsn,
  3263. le32_to_cpu(ra2->last_lsn_data_len));
  3264. /* If we wrapped the file then increment the sequence number. */
  3265. if (off <= vbo) {
  3266. log->seq_num += 1;
  3267. log->l_flags |= NTFSLOG_WRAPPED;
  3268. }
  3269. /* Now compute the next log page to use. */
  3270. vbo &= ~log->sys_page_mask;
  3271. tail = log->page_size - (off & log->page_mask) - 1;
  3272. /*
  3273. *If we can fit another log record on the page,
  3274. * move back a page the log file.
  3275. */
  3276. if (tail >= log->record_header_len) {
  3277. log->l_flags |= NTFSLOG_REUSE_TAIL;
  3278. log->next_page = vbo;
  3279. } else {
  3280. log->next_page = next_page_off(log, vbo);
  3281. }
  3282. find_oldest:
  3283. /*
  3284. * Find the oldest client lsn. Use the last
  3285. * flushed lsn as a starting point.
  3286. */
  3287. log->oldest_lsn = log->last_lsn;
  3288. oldest_client_lsn(Add2Ptr(ra2, le16_to_cpu(ra2->client_off)),
  3289. ra2->client_idx[1], &log->oldest_lsn);
  3290. log->oldest_lsn_off = lsn_to_vbo(log, log->oldest_lsn);
  3291. if (log->oldest_lsn_off < log->first_page)
  3292. log->l_flags |= NTFSLOG_NO_OLDEST_LSN;
  3293. if (!(ra2->flags & RESTART_SINGLE_PAGE_IO))
  3294. log->l_flags |= NTFSLOG_WRAPPED | NTFSLOG_MULTIPLE_PAGE_IO;
  3295. log->current_openlog_count = le32_to_cpu(ra2->open_log_count);
  3296. log->total_avail_pages = log->l_size - log->first_page;
  3297. log->total_avail = log->total_avail_pages >> log->page_bits;
  3298. log->max_current_avail = log->total_avail * log->reserved;
  3299. log->total_avail = log->total_avail * log->data_size;
  3300. log->current_avail = current_log_avail(log);
  3301. ra = kzalloc(log->restart_size, GFP_NOFS);
  3302. if (!ra) {
  3303. err = -ENOMEM;
  3304. goto out;
  3305. }
  3306. log->ra = ra;
  3307. t16 = le16_to_cpu(ra2->client_off);
  3308. if (t16 == offsetof(struct RESTART_AREA, clients)) {
  3309. memcpy(ra, ra2, log->ra_size);
  3310. } else {
  3311. memcpy(ra, ra2, offsetof(struct RESTART_AREA, clients));
  3312. memcpy(ra->clients, Add2Ptr(ra2, t16),
  3313. le16_to_cpu(ra2->ra_len) - t16);
  3314. log->current_openlog_count = get_random_u32();
  3315. ra->open_log_count = cpu_to_le32(log->current_openlog_count);
  3316. log->ra_size = offsetof(struct RESTART_AREA, clients) +
  3317. sizeof(struct CLIENT_REC);
  3318. ra->client_off =
  3319. cpu_to_le16(offsetof(struct RESTART_AREA, clients));
  3320. ra->ra_len = cpu_to_le16(log->ra_size);
  3321. }
  3322. le32_add_cpu(&ra->open_log_count, 1);
  3323. /* Now we need to walk through looking for the last lsn. */
  3324. err = last_log_lsn(log);
  3325. if (err)
  3326. goto out;
  3327. log->current_avail = current_log_avail(log);
  3328. /* Remember which restart area to write first. */
  3329. log->init_ra = log->rst_info.vbo;
  3330. process_log:
  3331. /* 1.0, 1.1, 2.0 log->major_ver/minor_ver - short values. */
  3332. switch ((log->major_ver << 16) + log->minor_ver) {
  3333. case 0x10000:
  3334. case 0x10001:
  3335. case 0x20000:
  3336. break;
  3337. default:
  3338. ntfs_warn(sbi->sb, "\x24LogFile version %d.%d is not supported",
  3339. log->major_ver, log->minor_ver);
  3340. err = -EOPNOTSUPP;
  3341. log->set_dirty = true;
  3342. goto out;
  3343. }
  3344. /* One client "NTFS" per logfile. */
  3345. ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
  3346. for (client = ra->client_idx[1];; client = cr->next_client) {
  3347. if (client == LFS_NO_CLIENT_LE) {
  3348. /* Insert "NTFS" client LogFile. */
  3349. client = ra->client_idx[0];
  3350. if (client == LFS_NO_CLIENT_LE) {
  3351. err = -EINVAL;
  3352. goto out;
  3353. }
  3354. t16 = le16_to_cpu(client);
  3355. cr = ca + t16;
  3356. remove_client(ca, cr, &ra->client_idx[0]);
  3357. cr->restart_lsn = 0;
  3358. cr->oldest_lsn = cpu_to_le64(log->oldest_lsn);
  3359. cr->name_bytes = cpu_to_le32(8);
  3360. cr->name[0] = cpu_to_le16('N');
  3361. cr->name[1] = cpu_to_le16('T');
  3362. cr->name[2] = cpu_to_le16('F');
  3363. cr->name[3] = cpu_to_le16('S');
  3364. add_client(ca, t16, &ra->client_idx[1]);
  3365. break;
  3366. }
  3367. cr = ca + le16_to_cpu(client);
  3368. if (cpu_to_le32(8) == cr->name_bytes &&
  3369. cpu_to_le16('N') == cr->name[0] &&
  3370. cpu_to_le16('T') == cr->name[1] &&
  3371. cpu_to_le16('F') == cr->name[2] &&
  3372. cpu_to_le16('S') == cr->name[3])
  3373. break;
  3374. }
  3375. /* Update the client handle with the client block information. */
  3376. log->client_id.seq_num = cr->seq_num;
  3377. log->client_id.client_idx = client;
  3378. err = read_rst_area(log, &rst, &checkpt_lsn);
  3379. if (err)
  3380. goto out;
  3381. if (!rst)
  3382. goto out;
  3383. bytes_per_attr_entry = !rst->major_ver ? 0x2C : 0x28;
  3384. if (rst->check_point_start)
  3385. checkpt_lsn = le64_to_cpu(rst->check_point_start);
  3386. /* Allocate and Read the Transaction Table. */
  3387. if (!rst->transact_table_len)
  3388. goto check_dirty_page_table; /* reduce tab pressure. */
  3389. t64 = le64_to_cpu(rst->transact_table_lsn);
  3390. err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
  3391. if (err)
  3392. goto out;
  3393. lrh = lcb->log_rec;
  3394. frh = lcb->lrh;
  3395. rec_len = le32_to_cpu(frh->client_data_len);
  3396. if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
  3397. bytes_per_attr_entry)) {
  3398. err = -EINVAL;
  3399. goto out;
  3400. }
  3401. t16 = le16_to_cpu(lrh->redo_off);
  3402. rt = Add2Ptr(lrh, t16);
  3403. t32 = rec_len - t16;
  3404. /* Now check that this is a valid restart table. */
  3405. if (!check_rstbl(rt, t32)) {
  3406. err = -EINVAL;
  3407. goto out;
  3408. }
  3409. trtbl = kmemdup(rt, t32, GFP_NOFS);
  3410. if (!trtbl) {
  3411. err = -ENOMEM;
  3412. goto out;
  3413. }
  3414. lcb_put(lcb);
  3415. lcb = NULL;
  3416. check_dirty_page_table:
  3417. /* The next record back should be the Dirty Pages Table. */
  3418. if (!rst->dirty_pages_len)
  3419. goto check_attribute_names; /* reduce tab pressure. */
  3420. t64 = le64_to_cpu(rst->dirty_pages_table_lsn);
  3421. err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
  3422. if (err)
  3423. goto out;
  3424. lrh = lcb->log_rec;
  3425. frh = lcb->lrh;
  3426. rec_len = le32_to_cpu(frh->client_data_len);
  3427. if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
  3428. bytes_per_attr_entry)) {
  3429. err = -EINVAL;
  3430. goto out;
  3431. }
  3432. t16 = le16_to_cpu(lrh->redo_off);
  3433. rt = Add2Ptr(lrh, t16);
  3434. t32 = rec_len - t16;
  3435. /* Now check that this is a valid restart table. */
  3436. if (!check_rstbl(rt, t32)) {
  3437. err = -EINVAL;
  3438. goto out;
  3439. }
  3440. dptbl = kmemdup(rt, t32, GFP_NOFS);
  3441. if (!dptbl) {
  3442. err = -ENOMEM;
  3443. goto out;
  3444. }
  3445. /* Convert Ra version '0' into version '1'. */
  3446. if (rst->major_ver)
  3447. goto end_conv_1; /* reduce tab pressure. */
  3448. dp = NULL;
  3449. while ((dp = enum_rstbl(dptbl, dp))) {
  3450. struct DIR_PAGE_ENTRY_32 *dp0 = (struct DIR_PAGE_ENTRY_32 *)dp;
  3451. // NOTE: Danger. Check for of boundary.
  3452. memmove(&dp->vcn, &dp0->vcn_low,
  3453. 2 * sizeof(u64) +
  3454. le32_to_cpu(dp->lcns_follow) * sizeof(u64));
  3455. }
  3456. end_conv_1:
  3457. lcb_put(lcb);
  3458. lcb = NULL;
  3459. /*
  3460. * Go through the table and remove the duplicates,
  3461. * remembering the oldest lsn values.
  3462. */
  3463. if (sbi->cluster_size <= log->page_size)
  3464. goto trace_dp_table; /* reduce tab pressure. */
  3465. dp = NULL;
  3466. while ((dp = enum_rstbl(dptbl, dp))) {
  3467. struct DIR_PAGE_ENTRY *next = dp;
  3468. while ((next = enum_rstbl(dptbl, next))) {
  3469. if (next->target_attr == dp->target_attr &&
  3470. next->vcn == dp->vcn) {
  3471. if (le64_to_cpu(next->oldest_lsn) <
  3472. le64_to_cpu(dp->oldest_lsn)) {
  3473. dp->oldest_lsn = next->oldest_lsn;
  3474. }
  3475. free_rsttbl_idx(dptbl, PtrOffset(dptbl, next));
  3476. }
  3477. }
  3478. }
  3479. trace_dp_table:
  3480. check_attribute_names:
  3481. /* The next record should be the Attribute Names. */
  3482. if (!rst->attr_names_len)
  3483. goto check_attr_table; /* reduce tab pressure. */
  3484. t64 = le64_to_cpu(rst->attr_names_lsn);
  3485. err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
  3486. if (err)
  3487. goto out;
  3488. lrh = lcb->log_rec;
  3489. frh = lcb->lrh;
  3490. rec_len = le32_to_cpu(frh->client_data_len);
  3491. if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
  3492. bytes_per_attr_entry)) {
  3493. err = -EINVAL;
  3494. goto out;
  3495. }
  3496. t32 = lrh_length(lrh);
  3497. attr_names_bytes = rec_len - t32;
  3498. attr_names = kmemdup(Add2Ptr(lrh, t32), attr_names_bytes, GFP_NOFS);
  3499. if (!attr_names) {
  3500. err = -ENOMEM;
  3501. goto out;
  3502. }
  3503. lcb_put(lcb);
  3504. lcb = NULL;
  3505. check_attr_table:
  3506. /* The next record should be the attribute Table. */
  3507. if (!rst->open_attr_len)
  3508. goto check_attribute_names2; /* reduce tab pressure. */
  3509. t64 = le64_to_cpu(rst->open_attr_table_lsn);
  3510. err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
  3511. if (err)
  3512. goto out;
  3513. lrh = lcb->log_rec;
  3514. frh = lcb->lrh;
  3515. rec_len = le32_to_cpu(frh->client_data_len);
  3516. if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
  3517. bytes_per_attr_entry)) {
  3518. err = -EINVAL;
  3519. goto out;
  3520. }
  3521. t16 = le16_to_cpu(lrh->redo_off);
  3522. rt = Add2Ptr(lrh, t16);
  3523. oatbl_bytes = rec_len - t16;
  3524. if (!check_rstbl(rt, oatbl_bytes)) {
  3525. err = -EINVAL;
  3526. goto out;
  3527. }
  3528. oatbl = kmemdup(rt, oatbl_bytes, GFP_NOFS);
  3529. if (!oatbl) {
  3530. err = -ENOMEM;
  3531. goto out;
  3532. }
  3533. log->open_attr_tbl = oatbl;
  3534. /* Clear all of the Attr pointers. */
  3535. oe = NULL;
  3536. while ((oe = enum_rstbl(oatbl, oe))) {
  3537. if (!rst->major_ver) {
  3538. struct OPEN_ATTR_ENRTY_32 oe0;
  3539. /* Really 'oe' points to OPEN_ATTR_ENRTY_32. */
  3540. memcpy(&oe0, oe, SIZEOF_OPENATTRIBUTEENTRY0);
  3541. oe->bytes_per_index = oe0.bytes_per_index;
  3542. oe->type = oe0.type;
  3543. oe->is_dirty_pages = oe0.is_dirty_pages;
  3544. oe->name_len = 0;
  3545. oe->ref = oe0.ref;
  3546. oe->open_record_lsn = oe0.open_record_lsn;
  3547. }
  3548. oe->is_attr_name = 0;
  3549. oe->ptr = NULL;
  3550. }
  3551. lcb_put(lcb);
  3552. lcb = NULL;
  3553. check_attribute_names2:
  3554. if (attr_names && oatbl) {
  3555. off = 0;
  3556. for (;;) {
  3557. /* Check we can use attribute name entry 'ane'. */
  3558. static_assert(sizeof(*ane) == 4);
  3559. if (off + sizeof(*ane) > attr_names_bytes) {
  3560. /* just ignore the rest. */
  3561. break;
  3562. }
  3563. ane = Add2Ptr(attr_names, off);
  3564. t16 = le16_to_cpu(ane->off);
  3565. if (!t16) {
  3566. /* this is the only valid exit. */
  3567. break;
  3568. }
  3569. /* Check we can use open attribute entry 'oe'. */
  3570. if (t16 + sizeof(*oe) > oatbl_bytes) {
  3571. /* just ignore the rest. */
  3572. break;
  3573. }
  3574. /* TODO: Clear table on exit! */
  3575. oe = Add2Ptr(oatbl, t16);
  3576. t16 = le16_to_cpu(ane->name_bytes);
  3577. off += t16 + sizeof(*ane);
  3578. if (off > attr_names_bytes) {
  3579. /* just ignore the rest. */
  3580. break;
  3581. }
  3582. oe->name_len = t16 / sizeof(short);
  3583. oe->ptr = ane->name;
  3584. oe->is_attr_name = 2;
  3585. }
  3586. }
  3587. /*
  3588. * If the checkpt_lsn is zero, then this is a freshly
  3589. * formatted disk and we have no work to do.
  3590. */
  3591. if (!checkpt_lsn) {
  3592. err = 0;
  3593. goto out;
  3594. }
  3595. if (!oatbl) {
  3596. oatbl = init_rsttbl(bytes_per_attr_entry, 8);
  3597. if (!oatbl) {
  3598. err = -ENOMEM;
  3599. goto out;
  3600. }
  3601. }
  3602. log->open_attr_tbl = oatbl;
  3603. /* Start the analysis pass from the Checkpoint lsn. */
  3604. rec_lsn = checkpt_lsn;
  3605. /* Read the first lsn. */
  3606. err = read_log_rec_lcb(log, checkpt_lsn, lcb_ctx_next, &lcb);
  3607. if (err)
  3608. goto out;
  3609. /* Loop to read all subsequent records to the end of the log file. */
  3610. next_log_record_analyze:
  3611. err = read_next_log_rec(log, lcb, &rec_lsn);
  3612. if (err)
  3613. goto out;
  3614. if (!rec_lsn)
  3615. goto end_log_records_enumerate;
  3616. frh = lcb->lrh;
  3617. transact_id = le32_to_cpu(frh->transact_id);
  3618. rec_len = le32_to_cpu(frh->client_data_len);
  3619. lrh = lcb->log_rec;
  3620. if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
  3621. err = -EINVAL;
  3622. goto out;
  3623. }
  3624. /*
  3625. * The first lsn after the previous lsn remembered
  3626. * the checkpoint is the first candidate for the rlsn.
  3627. */
  3628. if (!rlsn)
  3629. rlsn = rec_lsn;
  3630. if (LfsClientRecord != frh->record_type)
  3631. goto next_log_record_analyze;
  3632. /*
  3633. * Now update the Transaction Table for this transaction. If there
  3634. * is no entry present or it is unallocated we allocate the entry.
  3635. */
  3636. if (!trtbl) {
  3637. trtbl = init_rsttbl(sizeof(struct TRANSACTION_ENTRY),
  3638. INITIAL_NUMBER_TRANSACTIONS);
  3639. if (!trtbl) {
  3640. err = -ENOMEM;
  3641. goto out;
  3642. }
  3643. }
  3644. tr = Add2Ptr(trtbl, transact_id);
  3645. if (transact_id >= bytes_per_rt(trtbl) ||
  3646. tr->next != RESTART_ENTRY_ALLOCATED_LE) {
  3647. tr = alloc_rsttbl_from_idx(&trtbl, transact_id);
  3648. if (!tr) {
  3649. err = -ENOMEM;
  3650. goto out;
  3651. }
  3652. tr->transact_state = TransactionActive;
  3653. tr->first_lsn = cpu_to_le64(rec_lsn);
  3654. }
  3655. tr->prev_lsn = tr->undo_next_lsn = cpu_to_le64(rec_lsn);
  3656. /*
  3657. * If this is a compensation log record, then change
  3658. * the undo_next_lsn to be the undo_next_lsn of this record.
  3659. */
  3660. if (lrh->undo_op == cpu_to_le16(CompensationLogRecord))
  3661. tr->undo_next_lsn = frh->client_undo_next_lsn;
  3662. /* Dispatch to handle log record depending on type. */
  3663. switch (le16_to_cpu(lrh->redo_op)) {
  3664. case InitializeFileRecordSegment:
  3665. case DeallocateFileRecordSegment:
  3666. case WriteEndOfFileRecordSegment:
  3667. case CreateAttribute:
  3668. case DeleteAttribute:
  3669. case UpdateResidentValue:
  3670. case UpdateNonresidentValue:
  3671. case UpdateMappingPairs:
  3672. case SetNewAttributeSizes:
  3673. case AddIndexEntryRoot:
  3674. case DeleteIndexEntryRoot:
  3675. case AddIndexEntryAllocation:
  3676. case DeleteIndexEntryAllocation:
  3677. case WriteEndOfIndexBuffer:
  3678. case SetIndexEntryVcnRoot:
  3679. case SetIndexEntryVcnAllocation:
  3680. case UpdateFileNameRoot:
  3681. case UpdateFileNameAllocation:
  3682. case SetBitsInNonresidentBitMap:
  3683. case ClearBitsInNonresidentBitMap:
  3684. case UpdateRecordDataRoot:
  3685. case UpdateRecordDataAllocation:
  3686. case ZeroEndOfFileRecord:
  3687. t16 = le16_to_cpu(lrh->target_attr);
  3688. t64 = le64_to_cpu(lrh->target_vcn);
  3689. dp = find_dp(dptbl, t16, t64);
  3690. if (dp)
  3691. goto copy_lcns;
  3692. /*
  3693. * Calculate the number of clusters per page the system
  3694. * which wrote the checkpoint, possibly creating the table.
  3695. */
  3696. if (dptbl) {
  3697. t32 = (le16_to_cpu(dptbl->size) -
  3698. sizeof(struct DIR_PAGE_ENTRY)) /
  3699. sizeof(u64);
  3700. } else {
  3701. t32 = log->clst_per_page;
  3702. kfree(dptbl);
  3703. dptbl = init_rsttbl(struct_size(dp, page_lcns, t32),
  3704. 32);
  3705. if (!dptbl) {
  3706. err = -ENOMEM;
  3707. goto out;
  3708. }
  3709. }
  3710. dp = alloc_rsttbl_idx(&dptbl);
  3711. if (!dp) {
  3712. err = -ENOMEM;
  3713. goto out;
  3714. }
  3715. dp->target_attr = cpu_to_le32(t16);
  3716. dp->transfer_len = cpu_to_le32(t32 << sbi->cluster_bits);
  3717. dp->lcns_follow = cpu_to_le32(t32);
  3718. dp->vcn = cpu_to_le64(t64 & ~((u64)t32 - 1));
  3719. dp->oldest_lsn = cpu_to_le64(rec_lsn);
  3720. copy_lcns:
  3721. /*
  3722. * Copy the Lcns from the log record into the Dirty Page Entry.
  3723. * TODO: For different page size support, must somehow make
  3724. * whole routine a loop, case Lcns do not fit below.
  3725. */
  3726. t16 = le16_to_cpu(lrh->lcns_follow);
  3727. for (i = 0; i < t16; i++) {
  3728. size_t j = (size_t)(le64_to_cpu(lrh->target_vcn) -
  3729. le64_to_cpu(dp->vcn));
  3730. dp->page_lcns[j + i] = lrh->page_lcns[i];
  3731. }
  3732. goto next_log_record_analyze;
  3733. case DeleteDirtyClusters: {
  3734. u32 range_count =
  3735. le16_to_cpu(lrh->redo_len) / sizeof(struct LCN_RANGE);
  3736. const struct LCN_RANGE *r =
  3737. Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
  3738. /* Loop through all of the Lcn ranges this log record. */
  3739. for (i = 0; i < range_count; i++, r++) {
  3740. u64 lcn0 = le64_to_cpu(r->lcn);
  3741. u64 lcn_e = lcn0 + le64_to_cpu(r->len) - 1;
  3742. dp = NULL;
  3743. while ((dp = enum_rstbl(dptbl, dp))) {
  3744. u32 j;
  3745. t32 = le32_to_cpu(dp->lcns_follow);
  3746. for (j = 0; j < t32; j++) {
  3747. t64 = le64_to_cpu(dp->page_lcns[j]);
  3748. if (t64 >= lcn0 && t64 <= lcn_e)
  3749. dp->page_lcns[j] = 0;
  3750. }
  3751. }
  3752. }
  3753. goto next_log_record_analyze;
  3754. }
  3755. case OpenNonresidentAttribute:
  3756. t16 = le16_to_cpu(lrh->target_attr);
  3757. if (t16 >= bytes_per_rt(oatbl)) {
  3758. /*
  3759. * Compute how big the table needs to be.
  3760. * Add 10 extra entries for some cushion.
  3761. */
  3762. u32 new_e = t16 / le16_to_cpu(oatbl->size);
  3763. new_e += 10 - le16_to_cpu(oatbl->used);
  3764. oatbl = extend_rsttbl(oatbl, new_e, ~0u);
  3765. log->open_attr_tbl = oatbl;
  3766. if (!oatbl) {
  3767. err = -ENOMEM;
  3768. goto out;
  3769. }
  3770. }
  3771. /* Point to the entry being opened. */
  3772. oe = alloc_rsttbl_from_idx(&oatbl, t16);
  3773. log->open_attr_tbl = oatbl;
  3774. if (!oe) {
  3775. err = -ENOMEM;
  3776. goto out;
  3777. }
  3778. /* Initialize this entry from the log record. */
  3779. t16 = le16_to_cpu(lrh->redo_off);
  3780. if (!rst->major_ver) {
  3781. /* Convert version '0' into version '1'. */
  3782. struct OPEN_ATTR_ENRTY_32 *oe0 = Add2Ptr(lrh, t16);
  3783. oe->bytes_per_index = oe0->bytes_per_index;
  3784. oe->type = oe0->type;
  3785. oe->is_dirty_pages = oe0->is_dirty_pages;
  3786. oe->name_len = 0; //oe0.name_len;
  3787. oe->ref = oe0->ref;
  3788. oe->open_record_lsn = oe0->open_record_lsn;
  3789. } else {
  3790. memcpy(oe, Add2Ptr(lrh, t16), bytes_per_attr_entry);
  3791. }
  3792. t16 = le16_to_cpu(lrh->undo_len);
  3793. if (t16) {
  3794. oe->ptr = kmalloc(t16, GFP_NOFS);
  3795. if (!oe->ptr) {
  3796. err = -ENOMEM;
  3797. goto out;
  3798. }
  3799. oe->name_len = t16 / sizeof(short);
  3800. memcpy(oe->ptr,
  3801. Add2Ptr(lrh, le16_to_cpu(lrh->undo_off)), t16);
  3802. oe->is_attr_name = 1;
  3803. } else {
  3804. oe->ptr = NULL;
  3805. oe->is_attr_name = 0;
  3806. }
  3807. goto next_log_record_analyze;
  3808. case HotFix:
  3809. t16 = le16_to_cpu(lrh->target_attr);
  3810. t64 = le64_to_cpu(lrh->target_vcn);
  3811. dp = find_dp(dptbl, t16, t64);
  3812. if (dp) {
  3813. size_t j = le64_to_cpu(lrh->target_vcn) -
  3814. le64_to_cpu(dp->vcn);
  3815. if (dp->page_lcns[j])
  3816. dp->page_lcns[j] = lrh->page_lcns[0];
  3817. }
  3818. goto next_log_record_analyze;
  3819. case EndTopLevelAction:
  3820. tr = Add2Ptr(trtbl, transact_id);
  3821. tr->prev_lsn = cpu_to_le64(rec_lsn);
  3822. tr->undo_next_lsn = frh->client_undo_next_lsn;
  3823. goto next_log_record_analyze;
  3824. case PrepareTransaction:
  3825. tr = Add2Ptr(trtbl, transact_id);
  3826. tr->transact_state = TransactionPrepared;
  3827. goto next_log_record_analyze;
  3828. case CommitTransaction:
  3829. tr = Add2Ptr(trtbl, transact_id);
  3830. tr->transact_state = TransactionCommitted;
  3831. goto next_log_record_analyze;
  3832. case ForgetTransaction:
  3833. free_rsttbl_idx(trtbl, transact_id);
  3834. goto next_log_record_analyze;
  3835. case Noop:
  3836. case OpenAttributeTableDump:
  3837. case AttributeNamesDump:
  3838. case DirtyPageTableDump:
  3839. case TransactionTableDump:
  3840. /* The following cases require no action the Analysis Pass. */
  3841. goto next_log_record_analyze;
  3842. default:
  3843. /*
  3844. * All codes will be explicitly handled.
  3845. * If we see a code we do not expect, then we are trouble.
  3846. */
  3847. goto next_log_record_analyze;
  3848. }
  3849. end_log_records_enumerate:
  3850. lcb_put(lcb);
  3851. lcb = NULL;
  3852. /*
  3853. * Scan the Dirty Page Table and Transaction Table for
  3854. * the lowest lsn, and return it as the Redo lsn.
  3855. */
  3856. dp = NULL;
  3857. while ((dp = enum_rstbl(dptbl, dp))) {
  3858. t64 = le64_to_cpu(dp->oldest_lsn);
  3859. if (t64 && t64 < rlsn)
  3860. rlsn = t64;
  3861. }
  3862. tr = NULL;
  3863. while ((tr = enum_rstbl(trtbl, tr))) {
  3864. t64 = le64_to_cpu(tr->first_lsn);
  3865. if (t64 && t64 < rlsn)
  3866. rlsn = t64;
  3867. }
  3868. /*
  3869. * Only proceed if the Dirty Page Table or Transaction
  3870. * table are not empty.
  3871. */
  3872. if ((!dptbl || !dptbl->total) && (!trtbl || !trtbl->total))
  3873. goto end_replay;
  3874. sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
  3875. if (is_ro)
  3876. goto out;
  3877. /* Reopen all of the attributes with dirty pages. */
  3878. oe = NULL;
  3879. next_open_attribute:
  3880. oe = enum_rstbl(oatbl, oe);
  3881. if (!oe) {
  3882. err = 0;
  3883. dp = NULL;
  3884. goto next_dirty_page;
  3885. }
  3886. oa = kzalloc(sizeof(struct OpenAttr), GFP_NOFS);
  3887. if (!oa) {
  3888. err = -ENOMEM;
  3889. goto out;
  3890. }
  3891. inode = ntfs_iget5(sbi->sb, &oe->ref, NULL);
  3892. if (IS_ERR(inode))
  3893. goto fake_attr;
  3894. if (is_bad_inode(inode)) {
  3895. iput(inode);
  3896. fake_attr:
  3897. if (oa->ni) {
  3898. iput(&oa->ni->vfs_inode);
  3899. oa->ni = NULL;
  3900. }
  3901. attr = attr_create_nonres_log(sbi, oe->type, 0, oe->ptr,
  3902. oe->name_len, 0);
  3903. if (!attr) {
  3904. kfree(oa);
  3905. err = -ENOMEM;
  3906. goto out;
  3907. }
  3908. oa->attr = attr;
  3909. oa->run1 = &oa->run0;
  3910. goto final_oe;
  3911. }
  3912. ni_oe = ntfs_i(inode);
  3913. oa->ni = ni_oe;
  3914. attr = ni_find_attr(ni_oe, NULL, NULL, oe->type, oe->ptr, oe->name_len,
  3915. NULL, NULL);
  3916. if (!attr)
  3917. goto fake_attr;
  3918. t32 = le32_to_cpu(attr->size);
  3919. oa->attr = kmemdup(attr, t32, GFP_NOFS);
  3920. if (!oa->attr)
  3921. goto fake_attr;
  3922. if (!S_ISDIR(inode->i_mode)) {
  3923. if (attr->type == ATTR_DATA && !attr->name_len) {
  3924. oa->run1 = &ni_oe->file.run;
  3925. goto final_oe;
  3926. }
  3927. } else {
  3928. if (attr->type == ATTR_ALLOC &&
  3929. attr->name_len == ARRAY_SIZE(I30_NAME) &&
  3930. !memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME))) {
  3931. oa->run1 = &ni_oe->dir.alloc_run;
  3932. goto final_oe;
  3933. }
  3934. }
  3935. if (attr->non_res) {
  3936. u16 roff = le16_to_cpu(attr->nres.run_off);
  3937. CLST svcn = le64_to_cpu(attr->nres.svcn);
  3938. if (roff > t32) {
  3939. kfree(oa->attr);
  3940. oa->attr = NULL;
  3941. goto fake_attr;
  3942. }
  3943. err = run_unpack(&oa->run0, sbi, inode->i_ino, svcn,
  3944. le64_to_cpu(attr->nres.evcn), svcn,
  3945. Add2Ptr(attr, roff), t32 - roff);
  3946. if (err < 0) {
  3947. kfree(oa->attr);
  3948. oa->attr = NULL;
  3949. goto fake_attr;
  3950. }
  3951. err = 0;
  3952. }
  3953. oa->run1 = &oa->run0;
  3954. attr = oa->attr;
  3955. final_oe:
  3956. if (oe->is_attr_name == 1)
  3957. kfree(oe->ptr);
  3958. oe->is_attr_name = 0;
  3959. oe->ptr = oa;
  3960. oe->name_len = attr->name_len;
  3961. goto next_open_attribute;
  3962. /*
  3963. * Now loop through the dirty page table to extract all of the Vcn/Lcn.
  3964. * Mapping that we have, and insert it into the appropriate run.
  3965. */
  3966. next_dirty_page:
  3967. dp = enum_rstbl(dptbl, dp);
  3968. if (!dp)
  3969. goto do_redo_1;
  3970. oe = Add2Ptr(oatbl, le32_to_cpu(dp->target_attr));
  3971. if (oe->next != RESTART_ENTRY_ALLOCATED_LE)
  3972. goto next_dirty_page;
  3973. oa = oe->ptr;
  3974. if (!oa)
  3975. goto next_dirty_page;
  3976. i = -1;
  3977. next_dirty_page_vcn:
  3978. i += 1;
  3979. if (i >= le32_to_cpu(dp->lcns_follow))
  3980. goto next_dirty_page;
  3981. vcn = le64_to_cpu(dp->vcn) + i;
  3982. size = (vcn + 1) << sbi->cluster_bits;
  3983. if (!dp->page_lcns[i])
  3984. goto next_dirty_page_vcn;
  3985. rno = ino_get(&oe->ref);
  3986. if (rno <= MFT_REC_MIRR &&
  3987. size < (MFT_REC_VOL + 1) * sbi->record_size &&
  3988. oe->type == ATTR_DATA) {
  3989. goto next_dirty_page_vcn;
  3990. }
  3991. lcn = le64_to_cpu(dp->page_lcns[i]);
  3992. if ((!run_lookup_entry(oa->run1, vcn, &lcn0, &len0, NULL) ||
  3993. lcn0 != lcn) &&
  3994. !run_add_entry(oa->run1, vcn, lcn, 1, false)) {
  3995. err = -ENOMEM;
  3996. goto out;
  3997. }
  3998. attr = oa->attr;
  3999. if (size > le64_to_cpu(attr->nres.alloc_size)) {
  4000. attr->nres.valid_size = attr->nres.data_size =
  4001. attr->nres.alloc_size = cpu_to_le64(size);
  4002. }
  4003. goto next_dirty_page_vcn;
  4004. do_redo_1:
  4005. /*
  4006. * Perform the Redo Pass, to restore all of the dirty pages to the same
  4007. * contents that they had immediately before the crash. If the dirty
  4008. * page table is empty, then we can skip the entire Redo Pass.
  4009. */
  4010. if (!dptbl || !dptbl->total)
  4011. goto do_undo_action;
  4012. rec_lsn = rlsn;
  4013. /*
  4014. * Read the record at the Redo lsn, before falling
  4015. * into common code to handle each record.
  4016. */
  4017. err = read_log_rec_lcb(log, rlsn, lcb_ctx_next, &lcb);
  4018. if (err)
  4019. goto out;
  4020. /*
  4021. * Now loop to read all of our log records forwards, until
  4022. * we hit the end of the file, cleaning up at the end.
  4023. */
  4024. do_action_next:
  4025. frh = lcb->lrh;
  4026. if (LfsClientRecord != frh->record_type)
  4027. goto read_next_log_do_action;
  4028. transact_id = le32_to_cpu(frh->transact_id);
  4029. rec_len = le32_to_cpu(frh->client_data_len);
  4030. lrh = lcb->log_rec;
  4031. if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
  4032. err = -EINVAL;
  4033. goto out;
  4034. }
  4035. /* Ignore log records that do not update pages. */
  4036. if (lrh->lcns_follow)
  4037. goto find_dirty_page;
  4038. goto read_next_log_do_action;
  4039. find_dirty_page:
  4040. t16 = le16_to_cpu(lrh->target_attr);
  4041. t64 = le64_to_cpu(lrh->target_vcn);
  4042. dp = find_dp(dptbl, t16, t64);
  4043. if (!dp)
  4044. goto read_next_log_do_action;
  4045. if (rec_lsn < le64_to_cpu(dp->oldest_lsn))
  4046. goto read_next_log_do_action;
  4047. t16 = le16_to_cpu(lrh->target_attr);
  4048. if (t16 >= bytes_per_rt(oatbl)) {
  4049. err = -EINVAL;
  4050. goto out;
  4051. }
  4052. oe = Add2Ptr(oatbl, t16);
  4053. if (oe->next != RESTART_ENTRY_ALLOCATED_LE) {
  4054. err = -EINVAL;
  4055. goto out;
  4056. }
  4057. oa = oe->ptr;
  4058. if (!oa) {
  4059. err = -EINVAL;
  4060. goto out;
  4061. }
  4062. attr = oa->attr;
  4063. vcn = le64_to_cpu(lrh->target_vcn);
  4064. if (!run_lookup_entry(oa->run1, vcn, &lcn, NULL, NULL) ||
  4065. lcn == SPARSE_LCN) {
  4066. goto read_next_log_do_action;
  4067. }
  4068. /* Point to the Redo data and get its length. */
  4069. data = Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
  4070. dlen = le16_to_cpu(lrh->redo_len);
  4071. /* Shorten length by any Lcns which were deleted. */
  4072. saved_len = dlen;
  4073. for (i = le16_to_cpu(lrh->lcns_follow); i; i--) {
  4074. size_t j;
  4075. u32 alen, voff;
  4076. voff = le16_to_cpu(lrh->record_off) +
  4077. le16_to_cpu(lrh->attr_off);
  4078. voff += le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
  4079. /* If the Vcn question is allocated, we can just get out. */
  4080. j = le64_to_cpu(lrh->target_vcn) - le64_to_cpu(dp->vcn);
  4081. if (dp->page_lcns[j + i - 1])
  4082. break;
  4083. if (!saved_len)
  4084. saved_len = 1;
  4085. /*
  4086. * Calculate the allocated space left relative to the
  4087. * log record Vcn, after removing this unallocated Vcn.
  4088. */
  4089. alen = (i - 1) << sbi->cluster_bits;
  4090. /*
  4091. * If the update described this log record goes beyond
  4092. * the allocated space, then we will have to reduce the length.
  4093. */
  4094. if (voff >= alen)
  4095. dlen = 0;
  4096. else if (voff + dlen > alen)
  4097. dlen = alen - voff;
  4098. }
  4099. /*
  4100. * If the resulting dlen from above is now zero,
  4101. * we can skip this log record.
  4102. */
  4103. if (!dlen && saved_len)
  4104. goto read_next_log_do_action;
  4105. t16 = le16_to_cpu(lrh->redo_op);
  4106. if (can_skip_action(t16))
  4107. goto read_next_log_do_action;
  4108. /* Apply the Redo operation a common routine. */
  4109. err = do_action(log, oe, lrh, t16, data, dlen, rec_len, &rec_lsn);
  4110. if (err)
  4111. goto out;
  4112. /* Keep reading and looping back until end of file. */
  4113. read_next_log_do_action:
  4114. err = read_next_log_rec(log, lcb, &rec_lsn);
  4115. if (!err && rec_lsn)
  4116. goto do_action_next;
  4117. lcb_put(lcb);
  4118. lcb = NULL;
  4119. do_undo_action:
  4120. /* Scan Transaction Table. */
  4121. tr = NULL;
  4122. transaction_table_next:
  4123. tr = enum_rstbl(trtbl, tr);
  4124. if (!tr)
  4125. goto undo_action_done;
  4126. if (TransactionActive != tr->transact_state || !tr->undo_next_lsn) {
  4127. free_rsttbl_idx(trtbl, PtrOffset(trtbl, tr));
  4128. goto transaction_table_next;
  4129. }
  4130. log->transaction_id = PtrOffset(trtbl, tr);
  4131. undo_next_lsn = le64_to_cpu(tr->undo_next_lsn);
  4132. /*
  4133. * We only have to do anything if the transaction has
  4134. * something its undo_next_lsn field.
  4135. */
  4136. if (!undo_next_lsn)
  4137. goto commit_undo;
  4138. /* Read the first record to be undone by this transaction. */
  4139. err = read_log_rec_lcb(log, undo_next_lsn, lcb_ctx_undo_next, &lcb);
  4140. if (err)
  4141. goto out;
  4142. /*
  4143. * Now loop to read all of our log records forwards,
  4144. * until we hit the end of the file, cleaning up at the end.
  4145. */
  4146. undo_action_next:
  4147. lrh = lcb->log_rec;
  4148. frh = lcb->lrh;
  4149. transact_id = le32_to_cpu(frh->transact_id);
  4150. rec_len = le32_to_cpu(frh->client_data_len);
  4151. if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
  4152. err = -EINVAL;
  4153. goto out;
  4154. }
  4155. if (lrh->undo_op == cpu_to_le16(Noop))
  4156. goto read_next_log_undo_action;
  4157. oe = Add2Ptr(oatbl, le16_to_cpu(lrh->target_attr));
  4158. oa = oe->ptr;
  4159. t16 = le16_to_cpu(lrh->lcns_follow);
  4160. if (!t16)
  4161. goto add_allocated_vcns;
  4162. is_mapped = run_lookup_entry(oa->run1, le64_to_cpu(lrh->target_vcn),
  4163. &lcn, &clen, NULL);
  4164. /*
  4165. * If the mapping isn't already the table or the mapping
  4166. * corresponds to a hole the mapping, we need to make sure
  4167. * there is no partial page already memory.
  4168. */
  4169. if (is_mapped && lcn != SPARSE_LCN && clen >= t16)
  4170. goto add_allocated_vcns;
  4171. vcn = le64_to_cpu(lrh->target_vcn);
  4172. vcn &= ~(u64)(log->clst_per_page - 1);
  4173. add_allocated_vcns:
  4174. for (i = 0, vcn = le64_to_cpu(lrh->target_vcn),
  4175. size = (vcn + 1) << sbi->cluster_bits;
  4176. i < t16; i++, vcn += 1, size += sbi->cluster_size) {
  4177. attr = oa->attr;
  4178. if (!attr->non_res) {
  4179. if (size > le32_to_cpu(attr->res.data_size))
  4180. attr->res.data_size = cpu_to_le32(size);
  4181. } else {
  4182. if (size > le64_to_cpu(attr->nres.data_size))
  4183. attr->nres.valid_size = attr->nres.data_size =
  4184. attr->nres.alloc_size =
  4185. cpu_to_le64(size);
  4186. }
  4187. }
  4188. t16 = le16_to_cpu(lrh->undo_op);
  4189. if (can_skip_action(t16))
  4190. goto read_next_log_undo_action;
  4191. /* Point to the Redo data and get its length. */
  4192. data = Add2Ptr(lrh, le16_to_cpu(lrh->undo_off));
  4193. dlen = le16_to_cpu(lrh->undo_len);
  4194. /* It is time to apply the undo action. */
  4195. err = do_action(log, oe, lrh, t16, data, dlen, rec_len, NULL);
  4196. read_next_log_undo_action:
  4197. /*
  4198. * Keep reading and looping back until we have read the
  4199. * last record for this transaction.
  4200. */
  4201. err = read_next_log_rec(log, lcb, &rec_lsn);
  4202. if (err)
  4203. goto out;
  4204. if (rec_lsn)
  4205. goto undo_action_next;
  4206. lcb_put(lcb);
  4207. lcb = NULL;
  4208. commit_undo:
  4209. free_rsttbl_idx(trtbl, log->transaction_id);
  4210. log->transaction_id = 0;
  4211. goto transaction_table_next;
  4212. undo_action_done:
  4213. ntfs_update_mftmirr(sbi, 0);
  4214. sbi->flags &= ~NTFS_FLAGS_NEED_REPLAY;
  4215. end_replay:
  4216. err = 0;
  4217. if (is_ro)
  4218. goto out;
  4219. rh = kzalloc(log->page_size, GFP_NOFS);
  4220. if (!rh) {
  4221. err = -ENOMEM;
  4222. goto out;
  4223. }
  4224. rh->rhdr.sign = NTFS_RSTR_SIGNATURE;
  4225. rh->rhdr.fix_off = cpu_to_le16(offsetof(struct RESTART_HDR, fixups));
  4226. t16 = (log->page_size >> SECTOR_SHIFT) + 1;
  4227. rh->rhdr.fix_num = cpu_to_le16(t16);
  4228. rh->sys_page_size = cpu_to_le32(log->page_size);
  4229. rh->page_size = cpu_to_le32(log->page_size);
  4230. t16 = ALIGN(offsetof(struct RESTART_HDR, fixups) + sizeof(short) * t16,
  4231. 8);
  4232. rh->ra_off = cpu_to_le16(t16);
  4233. rh->minor_ver = cpu_to_le16(1); // 0x1A:
  4234. rh->major_ver = cpu_to_le16(1); // 0x1C:
  4235. ra2 = Add2Ptr(rh, t16);
  4236. memcpy(ra2, ra, sizeof(struct RESTART_AREA));
  4237. ra2->client_idx[0] = 0;
  4238. ra2->client_idx[1] = LFS_NO_CLIENT_LE;
  4239. ra2->flags = cpu_to_le16(2);
  4240. le32_add_cpu(&ra2->open_log_count, 1);
  4241. ntfs_fix_pre_write(&rh->rhdr, log->page_size);
  4242. err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rh, log->page_size, 0);
  4243. if (!err)
  4244. err = ntfs_sb_write_run(sbi, &log->ni->file.run, log->page_size,
  4245. rh, log->page_size, 0);
  4246. kfree(rh);
  4247. if (err)
  4248. goto out;
  4249. out:
  4250. kfree(rst);
  4251. if (lcb)
  4252. lcb_put(lcb);
  4253. /*
  4254. * Scan the Open Attribute Table to close all of
  4255. * the open attributes.
  4256. */
  4257. oe = NULL;
  4258. while ((oe = enum_rstbl(oatbl, oe))) {
  4259. rno = ino_get(&oe->ref);
  4260. if (oe->is_attr_name == 1) {
  4261. kfree(oe->ptr);
  4262. oe->ptr = NULL;
  4263. continue;
  4264. }
  4265. if (oe->is_attr_name)
  4266. continue;
  4267. oa = oe->ptr;
  4268. if (!oa)
  4269. continue;
  4270. run_close(&oa->run0);
  4271. kfree(oa->attr);
  4272. if (oa->ni)
  4273. iput(&oa->ni->vfs_inode);
  4274. kfree(oa);
  4275. }
  4276. kfree(trtbl);
  4277. kfree(oatbl);
  4278. kfree(dptbl);
  4279. kfree(attr_names);
  4280. kfree(log->rst_info.r_page);
  4281. kfree(ra);
  4282. kfree(log->one_page_buf);
  4283. if (err)
  4284. sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
  4285. if (err == -EROFS)
  4286. err = 0;
  4287. else if (log->set_dirty)
  4288. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  4289. kfree(log);
  4290. return err;
  4291. }