yaffs_guts.c 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. const char *yaffs_guts_c_version =
  14. "$Id: yaffs_guts.c,v 1.1 2007/08/13 07:18:33 jsgood Exp $";
  15. #include "yportenv.h"
  16. #include "yaffsinterface.h"
  17. #include "yaffs_guts.h"
  18. #include "yaffs_tagsvalidity.h"
  19. #include "yaffs_tagscompat.h"
  20. #ifndef CONFIG_YAFFS_USE_OWN_SORT
  21. #include "yaffs_qsort.h"
  22. #endif
  23. #include "yaffs_nand.h"
  24. #include "yaffs_checkptrw.h"
  25. #include "yaffs_nand.h"
  26. #include "yaffs_packedtags2.h"
  27. #ifdef CONFIG_YAFFS_WINCE
  28. void yfsd_LockYAFFS(BOOL fsLockOnly);
  29. void yfsd_UnlockYAFFS(BOOL fsLockOnly);
  30. #endif
  31. #define YAFFS_PASSIVE_GC_CHUNKS 2
  32. #include "yaffs_ecc.h"
  33. /* Robustification (if it ever comes about...) */
  34. static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
  35. static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
  36. static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
  37. const __u8 * data,
  38. const yaffs_ExtendedTags * tags);
  39. static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
  40. const yaffs_ExtendedTags * tags);
  41. /* Other local prototypes */
  42. static int yaffs_UnlinkObject( yaffs_Object *obj);
  43. static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
  44. static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
  45. static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
  46. const __u8 * buffer,
  47. yaffs_ExtendedTags * tags,
  48. int useReserve);
  49. static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
  50. int chunkInNAND, int inScan);
  51. static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
  52. yaffs_ObjectType type);
  53. static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
  54. yaffs_Object * obj);
  55. static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
  56. int force, int isShrink, int shadows);
  57. static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
  58. static int yaffs_CheckStructures(void);
  59. static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
  60. int chunkOffset, int *limit);
  61. static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
  62. static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
  63. static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
  64. static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
  65. int lineNo);
  66. static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  67. int chunkInNAND);
  68. static int yaffs_UnlinkWorker(yaffs_Object * obj);
  69. static void yaffs_DestroyObject(yaffs_Object * obj);
  70. static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
  71. int chunkInObject);
  72. loff_t yaffs_GetFileSize(yaffs_Object * obj);
  73. static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
  74. static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
  75. static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
  76. #ifdef YAFFS_PARANOID
  77. static int yaffs_CheckFileSanity(yaffs_Object * in);
  78. #else
  79. #define yaffs_CheckFileSanity(in)
  80. #endif
  81. static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
  82. static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
  83. static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
  84. static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
  85. yaffs_ExtendedTags * tags);
  86. static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
  87. static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
  88. yaffs_FileStructure * fStruct,
  89. __u32 chunkId);
  90. /* Function to calculate chunk and offset */
  91. static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
  92. {
  93. if(dev->chunkShift){
  94. /* Easy-peasy power of 2 case */
  95. *chunk = (__u32)(addr >> dev->chunkShift);
  96. *offset = (__u32)(addr & dev->chunkMask);
  97. }
  98. else if(dev->crumbsPerChunk)
  99. {
  100. /* Case where we're using "crumbs" */
  101. *offset = (__u32)(addr & dev->crumbMask);
  102. addr >>= dev->crumbShift;
  103. *chunk = ((__u32)addr)/dev->crumbsPerChunk;
  104. *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
  105. }
  106. else
  107. YBUG();
  108. }
  109. /* Function to return the number of shifts for a power of 2 greater than or equal
  110. * to the given number
  111. * Note we don't try to cater for all possible numbers and this does not have to
  112. * be hellishly efficient.
  113. */
  114. static __u32 ShiftsGE(__u32 x)
  115. {
  116. int extraBits;
  117. int nShifts;
  118. nShifts = extraBits = 0;
  119. while(x>1){
  120. if(x & 1) extraBits++;
  121. x>>=1;
  122. nShifts++;
  123. }
  124. if(extraBits)
  125. nShifts++;
  126. return nShifts;
  127. }
  128. /* Function to return the number of shifts to get a 1 in bit 0
  129. */
  130. static __u32 ShiftDiv(__u32 x)
  131. {
  132. int nShifts;
  133. nShifts = 0;
  134. if(!x) return 0;
  135. while( !(x&1)){
  136. x>>=1;
  137. nShifts++;
  138. }
  139. return nShifts;
  140. }
  141. /*
  142. * Temporary buffer manipulations.
  143. */
  144. static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
  145. {
  146. int i;
  147. __u8 *buf = (__u8 *)1;
  148. memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
  149. for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  150. dev->tempBuffer[i].line = 0; /* not in use */
  151. dev->tempBuffer[i].buffer = buf =
  152. YMALLOC_DMA(dev->nDataBytesPerChunk);
  153. }
  154. return buf ? YAFFS_OK : YAFFS_FAIL;
  155. }
  156. static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
  157. {
  158. int i, j;
  159. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  160. if (dev->tempBuffer[i].line == 0) {
  161. dev->tempBuffer[i].line = lineNo;
  162. if ((i + 1) > dev->maxTemp) {
  163. dev->maxTemp = i + 1;
  164. for (j = 0; j <= i; j++)
  165. dev->tempBuffer[j].maxLine =
  166. dev->tempBuffer[j].line;
  167. }
  168. return dev->tempBuffer[i].buffer;
  169. }
  170. }
  171. T(YAFFS_TRACE_BUFFERS,
  172. (TSTR("Out of temp buffers at line %d, other held by lines:"),
  173. lineNo));
  174. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  175. T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
  176. }
  177. T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
  178. /*
  179. * If we got here then we have to allocate an unmanaged one
  180. * This is not good.
  181. */
  182. dev->unmanagedTempAllocations++;
  183. return YMALLOC(dev->nDataBytesPerChunk);
  184. }
  185. static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
  186. int lineNo)
  187. {
  188. int i;
  189. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  190. if (dev->tempBuffer[i].buffer == buffer) {
  191. dev->tempBuffer[i].line = 0;
  192. return;
  193. }
  194. }
  195. if (buffer) {
  196. /* assume it is an unmanaged one. */
  197. T(YAFFS_TRACE_BUFFERS,
  198. (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
  199. lineNo));
  200. YFREE(buffer);
  201. dev->unmanagedTempDeallocations++;
  202. }
  203. }
  204. /*
  205. * Determine if we have a managed buffer.
  206. */
  207. int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
  208. {
  209. int i;
  210. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  211. if (dev->tempBuffer[i].buffer == buffer)
  212. return 1;
  213. }
  214. for (i = 0; i < dev->nShortOpCaches; i++) {
  215. if( dev->srCache[i].data == buffer )
  216. return 1;
  217. }
  218. if (buffer == dev->checkpointBuffer)
  219. return 1;
  220. T(YAFFS_TRACE_ALWAYS,
  221. (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
  222. return 0;
  223. }
  224. /*
  225. * Chunk bitmap manipulations
  226. */
  227. static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
  228. {
  229. if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  230. T(YAFFS_TRACE_ERROR,
  231. (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
  232. blk));
  233. YBUG();
  234. }
  235. return dev->chunkBits +
  236. (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
  237. }
  238. static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
  239. {
  240. if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
  241. chunk < 0 || chunk >= dev->nChunksPerBlock) {
  242. T(YAFFS_TRACE_ERROR,
  243. (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
  244. YBUG();
  245. }
  246. }
  247. static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
  248. {
  249. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  250. memset(blkBits, 0, dev->chunkBitmapStride);
  251. }
  252. static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
  253. {
  254. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  255. yaffs_VerifyChunkBitId(dev,blk,chunk);
  256. blkBits[chunk / 8] &= ~(1 << (chunk & 7));
  257. }
  258. static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
  259. {
  260. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  261. yaffs_VerifyChunkBitId(dev,blk,chunk);
  262. blkBits[chunk / 8] |= (1 << (chunk & 7));
  263. }
  264. static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
  265. {
  266. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  267. yaffs_VerifyChunkBitId(dev,blk,chunk);
  268. return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  269. }
  270. static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
  271. {
  272. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  273. int i;
  274. for (i = 0; i < dev->chunkBitmapStride; i++) {
  275. if (*blkBits)
  276. return 1;
  277. blkBits++;
  278. }
  279. return 0;
  280. }
  281. static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
  282. {
  283. __u8 *blkBits = yaffs_BlockBits(dev, blk);
  284. int i;
  285. int n = 0;
  286. for (i = 0; i < dev->chunkBitmapStride; i++) {
  287. __u8 x = *blkBits;
  288. while(x){
  289. if(x & 1)
  290. n++;
  291. x >>=1;
  292. }
  293. blkBits++;
  294. }
  295. return n;
  296. }
  297. /*
  298. * Verification code
  299. */
  300. static int yaffs_SkipVerification(yaffs_Device *dev)
  301. {
  302. return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  303. }
  304. static int yaffs_SkipFullVerification(yaffs_Device *dev)
  305. {
  306. return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
  307. }
  308. static int yaffs_SkipNANDVerification(yaffs_Device *dev)
  309. {
  310. return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
  311. }
  312. static const char * blockStateName[] = {
  313. "Unknown",
  314. "Needs scanning",
  315. "Scanning",
  316. "Empty",
  317. "Allocating",
  318. "Full",
  319. "Dirty",
  320. "Checkpoint",
  321. "Collecting",
  322. "Dead"
  323. };
  324. static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
  325. {
  326. int actuallyUsed;
  327. int inUse;
  328. if(yaffs_SkipVerification(dev))
  329. return;
  330. /* Report illegal runtime states */
  331. if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
  332. T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
  333. switch(bi->blockState){
  334. case YAFFS_BLOCK_STATE_UNKNOWN:
  335. case YAFFS_BLOCK_STATE_SCANNING:
  336. case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
  337. T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
  338. n,blockStateName[bi->blockState]));
  339. }
  340. /* Check pages in use and soft deletions are legal */
  341. actuallyUsed = bi->pagesInUse - bi->softDeletions;
  342. if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
  343. bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
  344. actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
  345. T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
  346. n,bi->pagesInUse,bi->softDeletions));
  347. /* Check chunk bitmap legal */
  348. inUse = yaffs_CountChunkBits(dev,n);
  349. if(inUse != bi->pagesInUse)
  350. T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
  351. n,bi->pagesInUse,inUse));
  352. /* Check that the sequence number is valid.
  353. * Ten million is legal, but is very unlikely
  354. */
  355. if(dev->isYaffs2 &&
  356. (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
  357. (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
  358. T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
  359. n,bi->sequenceNumber));
  360. }
  361. static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
  362. {
  363. yaffs_VerifyBlock(dev,bi,n);
  364. /* After collection the block should be in the erased state */
  365. /* TODO: This will need to change if we do partial gc */
  366. if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
  367. T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
  368. n,bi->blockState));
  369. }
  370. }
  371. static void yaffs_VerifyBlocks(yaffs_Device *dev)
  372. {
  373. int i;
  374. int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
  375. int nIllegalBlockStates = 0;
  376. if(yaffs_SkipVerification(dev))
  377. return;
  378. memset(nBlocksPerState,0,sizeof(nBlocksPerState));
  379. for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
  380. yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
  381. yaffs_VerifyBlock(dev,bi,i);
  382. if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
  383. nBlocksPerState[bi->blockState]++;
  384. else
  385. nIllegalBlockStates++;
  386. }
  387. T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
  388. T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
  389. T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
  390. if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  391. T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
  392. for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  393. T(YAFFS_TRACE_VERIFY,
  394. (TSTR("%s %d blocks"TENDSTR),
  395. blockStateName[i],nBlocksPerState[i]));
  396. if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
  397. T(YAFFS_TRACE_VERIFY,
  398. (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
  399. dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
  400. if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
  401. T(YAFFS_TRACE_VERIFY,
  402. (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
  403. dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
  404. if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  405. T(YAFFS_TRACE_VERIFY,
  406. (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
  407. nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
  408. T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
  409. }
  410. /*
  411. * Verify the object header. oh must be valid, but obj and tags may be NULL in which
  412. * case those tests will not be performed.
  413. */
  414. static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
  415. {
  416. if(yaffs_SkipVerification(obj->myDev))
  417. return;
  418. if(!(tags && obj && oh)){
  419. T(YAFFS_TRACE_VERIFY,
  420. (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
  421. (__u32)tags,(__u32)obj,(__u32)oh));
  422. return;
  423. }
  424. if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  425. oh->type > YAFFS_OBJECT_TYPE_MAX)
  426. T(YAFFS_TRACE_VERIFY,
  427. (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
  428. tags->objectId, oh->type));
  429. if(tags->objectId != obj->objectId)
  430. T(YAFFS_TRACE_VERIFY,
  431. (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
  432. tags->objectId, obj->objectId));
  433. /*
  434. * Check that the object's parent ids match if parentCheck requested.
  435. *
  436. * Tests do not apply to the root object.
  437. */
  438. if(parentCheck && tags->objectId > 1 && !obj->parent)
  439. T(YAFFS_TRACE_VERIFY,
  440. (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
  441. tags->objectId, oh->parentObjectId));
  442. if(parentCheck && obj->parent &&
  443. oh->parentObjectId != obj->parent->objectId &&
  444. (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
  445. obj->parent->objectId != YAFFS_OBJECTID_DELETED))
  446. T(YAFFS_TRACE_VERIFY,
  447. (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
  448. tags->objectId, oh->parentObjectId, obj->parent->objectId));
  449. if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
  450. T(YAFFS_TRACE_VERIFY,
  451. (TSTR("Obj %d header name is NULL"TENDSTR),
  452. obj->objectId));
  453. if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
  454. T(YAFFS_TRACE_VERIFY,
  455. (TSTR("Obj %d header name is 0xFF"TENDSTR),
  456. obj->objectId));
  457. }
  458. static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
  459. __u32 level, int chunkOffset)
  460. {
  461. int i;
  462. yaffs_Device *dev = obj->myDev;
  463. int ok = 1;
  464. int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  465. if (tn) {
  466. if (level > 0) {
  467. for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
  468. if (tn->internal[i]) {
  469. ok = yaffs_VerifyTnodeWorker(obj,
  470. tn->internal[i],
  471. level - 1,
  472. (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  473. }
  474. }
  475. } else if (level == 0) {
  476. int i;
  477. yaffs_ExtendedTags tags;
  478. __u32 objectId = obj->objectId;
  479. chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
  480. for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
  481. __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
  482. if(theChunk > 0){
  483. /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
  484. yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
  485. if(tags.objectId != objectId || tags.chunkId != chunkOffset){
  486. T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  487. objectId, chunkOffset, theChunk,
  488. tags.objectId, tags.chunkId));
  489. }
  490. }
  491. chunkOffset++;
  492. }
  493. }
  494. }
  495. return ok;
  496. }
  497. static void yaffs_VerifyFile(yaffs_Object *obj)
  498. {
  499. int requiredTallness;
  500. int actualTallness;
  501. __u32 lastChunk;
  502. __u32 x;
  503. __u32 i;
  504. int ok;
  505. yaffs_Device *dev;
  506. yaffs_ExtendedTags tags;
  507. yaffs_Tnode *tn;
  508. __u32 objectId;
  509. if(obj && yaffs_SkipVerification(obj->myDev))
  510. return;
  511. dev = obj->myDev;
  512. objectId = obj->objectId;
  513. /* Check file size is consistent with tnode depth */
  514. lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
  515. x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
  516. requiredTallness = 0;
  517. while (x> 0) {
  518. x >>= YAFFS_TNODES_INTERNAL_BITS;
  519. requiredTallness++;
  520. }
  521. actualTallness = obj->variant.fileVariant.topLevel;
  522. if(requiredTallness > actualTallness )
  523. T(YAFFS_TRACE_VERIFY,
  524. (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
  525. obj->objectId,actualTallness, requiredTallness));
  526. /* Check that the chunks in the tnode tree are all correct.
  527. * We do this by scanning through the tnode tree and
  528. * checking the tags for every chunk match.
  529. */
  530. if(yaffs_SkipNANDVerification(dev))
  531. return;
  532. for(i = 1; i <= lastChunk; i++){
  533. tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
  534. if (tn) {
  535. __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
  536. if(theChunk > 0){
  537. /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
  538. yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
  539. if(tags.objectId != objectId || tags.chunkId != i){
  540. T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  541. objectId, i, theChunk,
  542. tags.objectId, tags.chunkId));
  543. }
  544. }
  545. }
  546. }
  547. }
  548. static void yaffs_VerifyDirectory(yaffs_Object *obj)
  549. {
  550. if(obj && yaffs_SkipVerification(obj->myDev))
  551. return;
  552. }
  553. static void yaffs_VerifyHardLink(yaffs_Object *obj)
  554. {
  555. if(obj && yaffs_SkipVerification(obj->myDev))
  556. return;
  557. /* Verify sane equivalent object */
  558. }
  559. static void yaffs_VerifySymlink(yaffs_Object *obj)
  560. {
  561. if(obj && yaffs_SkipVerification(obj->myDev))
  562. return;
  563. /* Verify symlink string */
  564. }
  565. static void yaffs_VerifySpecial(yaffs_Object *obj)
  566. {
  567. if(obj && yaffs_SkipVerification(obj->myDev))
  568. return;
  569. }
  570. static void yaffs_VerifyObject(yaffs_Object *obj)
  571. {
  572. yaffs_Device *dev;
  573. __u32 chunkMin;
  574. __u32 chunkMax;
  575. __u32 chunkIdOk;
  576. __u32 chunkIsLive;
  577. if(!obj)
  578. return;
  579. dev = obj->myDev;
  580. if(yaffs_SkipVerification(dev))
  581. return;
  582. /* Check sane object header chunk */
  583. chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
  584. chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
  585. chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
  586. chunkIsLive = chunkIdOk &&
  587. yaffs_CheckChunkBit(dev,
  588. obj->chunkId / dev->nChunksPerBlock,
  589. obj->chunkId % dev->nChunksPerBlock);
  590. if(!obj->fake &&
  591. (!chunkIdOk || !chunkIsLive)) {
  592. T(YAFFS_TRACE_VERIFY,
  593. (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
  594. obj->objectId,obj->chunkId,
  595. chunkIdOk ? "" : ",out of range",
  596. chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
  597. }
  598. if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
  599. yaffs_ExtendedTags tags;
  600. yaffs_ObjectHeader *oh;
  601. __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
  602. oh = (yaffs_ObjectHeader *)buffer;
  603. yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
  604. yaffs_VerifyObjectHeader(obj,oh,&tags,1);
  605. yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
  606. }
  607. /* Verify it has a parent */
  608. if(obj && !obj->fake &&
  609. (!obj->parent || obj->parent->myDev != dev)){
  610. T(YAFFS_TRACE_VERIFY,
  611. (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
  612. obj->objectId,obj->parent));
  613. }
  614. /* Verify parent is a directory */
  615. if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
  616. T(YAFFS_TRACE_VERIFY,
  617. (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
  618. obj->objectId,obj->parent->variantType));
  619. }
  620. switch(obj->variantType){
  621. case YAFFS_OBJECT_TYPE_FILE:
  622. yaffs_VerifyFile(obj);
  623. break;
  624. case YAFFS_OBJECT_TYPE_SYMLINK:
  625. yaffs_VerifySymlink(obj);
  626. break;
  627. case YAFFS_OBJECT_TYPE_DIRECTORY:
  628. yaffs_VerifyDirectory(obj);
  629. break;
  630. case YAFFS_OBJECT_TYPE_HARDLINK:
  631. yaffs_VerifyHardLink(obj);
  632. break;
  633. case YAFFS_OBJECT_TYPE_SPECIAL:
  634. yaffs_VerifySpecial(obj);
  635. break;
  636. case YAFFS_OBJECT_TYPE_UNKNOWN:
  637. default:
  638. T(YAFFS_TRACE_VERIFY,
  639. (TSTR("Obj %d has illegaltype %d"TENDSTR),
  640. obj->objectId,obj->variantType));
  641. break;
  642. }
  643. }
  644. static void yaffs_VerifyObjects(yaffs_Device *dev)
  645. {
  646. yaffs_Object *obj;
  647. int i;
  648. struct list_head *lh;
  649. if(yaffs_SkipVerification(dev))
  650. return;
  651. /* Iterate through the objects in each hash entry */
  652. for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
  653. list_for_each(lh, &dev->objectBucket[i].list) {
  654. if (lh) {
  655. obj = list_entry(lh, yaffs_Object, hashLink);
  656. yaffs_VerifyObject(obj);
  657. }
  658. }
  659. }
  660. }
  661. /*
  662. * Simple hash function. Needs to have a reasonable spread
  663. */
  664. static Y_INLINE int yaffs_HashFunction(int n)
  665. {
  666. n = abs(n);
  667. return (n % YAFFS_NOBJECT_BUCKETS);
  668. }
  669. /*
  670. * Access functions to useful fake objects
  671. */
  672. yaffs_Object *yaffs_Root(yaffs_Device * dev)
  673. {
  674. return dev->rootDir;
  675. }
  676. yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
  677. {
  678. return dev->lostNFoundDir;
  679. }
  680. /*
  681. * Erased NAND checking functions
  682. */
  683. int yaffs_CheckFF(__u8 * buffer, int nBytes)
  684. {
  685. /* Horrible, slow implementation */
  686. while (nBytes--) {
  687. if (*buffer != 0xFF)
  688. return 0;
  689. buffer++;
  690. }
  691. return 1;
  692. }
  693. static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  694. int chunkInNAND)
  695. {
  696. int retval = YAFFS_OK;
  697. __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
  698. yaffs_ExtendedTags tags;
  699. int result;
  700. result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
  701. if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
  702. retval = YAFFS_FAIL;
  703. if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
  704. T(YAFFS_TRACE_NANDACCESS,
  705. (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
  706. retval = YAFFS_FAIL;
  707. }
  708. yaffs_ReleaseTempBuffer(dev, data, __LINE__);
  709. return retval;
  710. }
  711. static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
  712. const __u8 * data,
  713. yaffs_ExtendedTags * tags,
  714. int useReserve)
  715. {
  716. int chunk;
  717. int writeOk = 0;
  718. int erasedOk = 1;
  719. int attempts = 0;
  720. yaffs_BlockInfo *bi;
  721. yaffs_InvalidateCheckpoint(dev);
  722. do {
  723. chunk = yaffs_AllocateChunk(dev, useReserve,&bi);
  724. if (chunk >= 0) {
  725. /* First check this chunk is erased, if it needs checking.
  726. * The checking policy (unless forced always on) is as follows:
  727. * Check the first page we try to write in a block.
  728. * - If the check passes then we don't need to check any more.
  729. * - If the check fails, we check again...
  730. * If the block has been erased, we don't need to check.
  731. *
  732. * However, if the block has been prioritised for gc, then
  733. * we think there might be something odd about this block
  734. * and stop using it.
  735. *
  736. * Rationale:
  737. * We should only ever see chunks that have not been erased
  738. * if there was a partially written chunk due to power loss
  739. * This checking policy should catch that case with very
  740. * few checks and thus save a lot of checks that are most likely not
  741. * needed.
  742. */
  743. if(bi->gcPrioritise){
  744. yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  745. } else {
  746. #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  747. bi->skipErasedCheck = 0;
  748. #endif
  749. if(!bi->skipErasedCheck){
  750. erasedOk = yaffs_CheckChunkErased(dev, chunk);
  751. if(erasedOk && !bi->gcPrioritise)
  752. bi->skipErasedCheck = 1;
  753. }
  754. if (!erasedOk) {
  755. T(YAFFS_TRACE_ERROR,
  756. (TSTR
  757. ("**>> yaffs chunk %d was not erased"
  758. TENDSTR), chunk));
  759. } else {
  760. writeOk =
  761. yaffs_WriteChunkWithTagsToNAND(dev, chunk,
  762. data, tags);
  763. }
  764. attempts++;
  765. if (writeOk) {
  766. /*
  767. * Copy the data into the robustification buffer.
  768. * NB We do this at the end to prevent duplicates in the case of a write error.
  769. * Todo
  770. */
  771. yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
  772. } else {
  773. /* The erased check or write failed */
  774. yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
  775. }
  776. }
  777. }
  778. } while (chunk >= 0 && !writeOk);
  779. if (attempts > 1) {
  780. T(YAFFS_TRACE_ERROR,
  781. (TSTR("**>> yaffs write required %d attempts" TENDSTR),
  782. attempts));
  783. dev->nRetriedWrites += (attempts - 1);
  784. }
  785. return chunk;
  786. }
  787. /*
  788. * Block retiring for handling a broken block.
  789. */
  790. static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
  791. {
  792. yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  793. yaffs_InvalidateCheckpoint(dev);
  794. yaffs_MarkBlockBad(dev, blockInNAND);
  795. bi->blockState = YAFFS_BLOCK_STATE_DEAD;
  796. bi->gcPrioritise = 0;
  797. bi->needsRetiring = 0;
  798. dev->nRetiredBlocks++;
  799. }
  800. /*
  801. * Functions for robustisizing TODO
  802. *
  803. */
  804. static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
  805. const __u8 * data,
  806. const yaffs_ExtendedTags * tags)
  807. {
  808. }
  809. static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
  810. const yaffs_ExtendedTags * tags)
  811. {
  812. }
  813. void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
  814. {
  815. if(!bi->gcPrioritise){
  816. bi->gcPrioritise = 1;
  817. dev->hasPendingPrioritisedGCs = 1;
  818. bi->chunkErrorStrikes ++;
  819. if(bi->chunkErrorStrikes > 3){
  820. bi->needsRetiring = 1; /* Too many stikes, so retire this */
  821. T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
  822. }
  823. }
  824. }
  825. static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
  826. {
  827. int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  828. yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  829. yaffs_HandleChunkError(dev,bi);
  830. if(erasedOk ) {
  831. /* Was an actual write failure, so mark the block for retirement */
  832. bi->needsRetiring = 1;
  833. T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  834. (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
  835. }
  836. /* Delete the chunk */
  837. yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  838. }
  839. /*---------------- Name handling functions ------------*/
  840. static __u16 yaffs_CalcNameSum(const YCHAR * name)
  841. {
  842. __u16 sum = 0;
  843. __u16 i = 1;
  844. YUCHAR *bname = (YUCHAR *) name;
  845. if (bname) {
  846. while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
  847. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  848. sum += yaffs_toupper(*bname) * i;
  849. #else
  850. sum += (*bname) * i;
  851. #endif
  852. i++;
  853. bname++;
  854. }
  855. }
  856. return sum;
  857. }
  858. static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
  859. {
  860. #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  861. if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
  862. yaffs_strcpy(obj->shortName, name);
  863. } else {
  864. obj->shortName[0] = _Y('\0');
  865. }
  866. #endif
  867. obj->sum = yaffs_CalcNameSum(name);
  868. }
  869. /*-------------------- TNODES -------------------
  870. * List of spare tnodes
  871. * The list is hooked together using the first pointer
  872. * in the tnode.
  873. */
  874. /* yaffs_CreateTnodes creates a bunch more tnodes and
  875. * adds them to the tnode free list.
  876. * Don't use this function directly
  877. */
  878. static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
  879. {
  880. int i;
  881. int tnodeSize;
  882. yaffs_Tnode *newTnodes;
  883. __u8 *mem;
  884. yaffs_Tnode *curr;
  885. yaffs_Tnode *next;
  886. yaffs_TnodeList *tnl;
  887. if (nTnodes < 1)
  888. return YAFFS_OK;
  889. /* Calculate the tnode size in bytes for variable width tnode support.
  890. * Must be a multiple of 32-bits */
  891. tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  892. /* make these things */
  893. newTnodes = YMALLOC(nTnodes * tnodeSize);
  894. mem = (__u8 *)newTnodes;
  895. if (!newTnodes) {
  896. T(YAFFS_TRACE_ERROR,
  897. (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
  898. return YAFFS_FAIL;
  899. }
  900. /* Hook them into the free list */
  901. #if 0
  902. for (i = 0; i < nTnodes - 1; i++) {
  903. newTnodes[i].internal[0] = &newTnodes[i + 1];
  904. #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  905. newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  906. #endif
  907. }
  908. newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
  909. #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  910. newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  911. #endif
  912. dev->freeTnodes = newTnodes;
  913. #else
  914. /* New hookup for wide tnodes */
  915. for(i = 0; i < nTnodes -1; i++) {
  916. curr = (yaffs_Tnode *) &mem[i * tnodeSize];
  917. next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
  918. curr->internal[0] = next;
  919. }
  920. curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
  921. curr->internal[0] = dev->freeTnodes;
  922. dev->freeTnodes = (yaffs_Tnode *)mem;
  923. #endif
  924. dev->nFreeTnodes += nTnodes;
  925. dev->nTnodesCreated += nTnodes;
  926. /* Now add this bunch of tnodes to a list for freeing up.
  927. * NB If we can't add this to the management list it isn't fatal
  928. * but it just means we can't free this bunch of tnodes later.
  929. */
  930. tnl = YMALLOC(sizeof(yaffs_TnodeList));
  931. if (!tnl) {
  932. T(YAFFS_TRACE_ERROR,
  933. (TSTR
  934. ("yaffs: Could not add tnodes to management list" TENDSTR)));
  935. return YAFFS_FAIL;
  936. } else {
  937. tnl->tnodes = newTnodes;
  938. tnl->next = dev->allocatedTnodeList;
  939. dev->allocatedTnodeList = tnl;
  940. }
  941. T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
  942. return YAFFS_OK;
  943. }
  944. /* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
  945. static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
  946. {
  947. yaffs_Tnode *tn = NULL;
  948. /* If there are none left make more */
  949. if (!dev->freeTnodes) {
  950. yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
  951. }
  952. if (dev->freeTnodes) {
  953. tn = dev->freeTnodes;
  954. #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  955. if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
  956. /* Hoosterman, this thing looks like it isn't in the list */
  957. T(YAFFS_TRACE_ALWAYS,
  958. (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
  959. }
  960. #endif
  961. dev->freeTnodes = dev->freeTnodes->internal[0];
  962. dev->nFreeTnodes--;
  963. }
  964. return tn;
  965. }
  966. static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
  967. {
  968. yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
  969. if(tn)
  970. memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
  971. return tn;
  972. }
  973. /* FreeTnode frees up a tnode and puts it back on the free list */
  974. static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
  975. {
  976. if (tn) {
  977. #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  978. if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
  979. /* Hoosterman, this thing looks like it is already in the list */
  980. T(YAFFS_TRACE_ALWAYS,
  981. (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
  982. }
  983. tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  984. #endif
  985. tn->internal[0] = dev->freeTnodes;
  986. dev->freeTnodes = tn;
  987. dev->nFreeTnodes++;
  988. }
  989. }
  990. static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
  991. {
  992. /* Free the list of allocated tnodes */
  993. yaffs_TnodeList *tmp;
  994. while (dev->allocatedTnodeList) {
  995. tmp = dev->allocatedTnodeList->next;
  996. YFREE(dev->allocatedTnodeList->tnodes);
  997. YFREE(dev->allocatedTnodeList);
  998. dev->allocatedTnodeList = tmp;
  999. }
  1000. dev->freeTnodes = NULL;
  1001. dev->nFreeTnodes = 0;
  1002. }
  1003. static void yaffs_InitialiseTnodes(yaffs_Device * dev)
  1004. {
  1005. dev->allocatedTnodeList = NULL;
  1006. dev->freeTnodes = NULL;
  1007. dev->nFreeTnodes = 0;
  1008. dev->nTnodesCreated = 0;
  1009. }
  1010. void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
  1011. {
  1012. __u32 *map = (__u32 *)tn;
  1013. __u32 bitInMap;
  1014. __u32 bitInWord;
  1015. __u32 wordInMap;
  1016. __u32 mask;
  1017. pos &= YAFFS_TNODES_LEVEL0_MASK;
  1018. val >>= dev->chunkGroupBits;
  1019. bitInMap = pos * dev->tnodeWidth;
  1020. wordInMap = bitInMap /32;
  1021. bitInWord = bitInMap & (32 -1);
  1022. mask = dev->tnodeMask << bitInWord;
  1023. map[wordInMap] &= ~mask;
  1024. map[wordInMap] |= (mask & (val << bitInWord));
  1025. if(dev->tnodeWidth > (32-bitInWord)) {
  1026. bitInWord = (32 - bitInWord);
  1027. wordInMap++;;
  1028. mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
  1029. map[wordInMap] &= ~mask;
  1030. map[wordInMap] |= (mask & (val >> bitInWord));
  1031. }
  1032. }
  1033. static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
  1034. {
  1035. __u32 *map = (__u32 *)tn;
  1036. __u32 bitInMap;
  1037. __u32 bitInWord;
  1038. __u32 wordInMap;
  1039. __u32 val;
  1040. pos &= YAFFS_TNODES_LEVEL0_MASK;
  1041. bitInMap = pos * dev->tnodeWidth;
  1042. wordInMap = bitInMap /32;
  1043. bitInWord = bitInMap & (32 -1);
  1044. val = map[wordInMap] >> bitInWord;
  1045. if(dev->tnodeWidth > (32-bitInWord)) {
  1046. bitInWord = (32 - bitInWord);
  1047. wordInMap++;;
  1048. val |= (map[wordInMap] << bitInWord);
  1049. }
  1050. val &= dev->tnodeMask;
  1051. val <<= dev->chunkGroupBits;
  1052. return val;
  1053. }
  1054. /* ------------------- End of individual tnode manipulation -----------------*/
  1055. /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  1056. * The look up tree is represented by the top tnode and the number of topLevel
  1057. * in the tree. 0 means only the level 0 tnode is in the tree.
  1058. */
  1059. /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  1060. static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
  1061. yaffs_FileStructure * fStruct,
  1062. __u32 chunkId)
  1063. {
  1064. yaffs_Tnode *tn = fStruct->top;
  1065. __u32 i;
  1066. int requiredTallness;
  1067. int level = fStruct->topLevel;
  1068. /* Check sane level and chunk Id */
  1069. if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
  1070. return NULL;
  1071. }
  1072. if (chunkId > YAFFS_MAX_CHUNK_ID) {
  1073. return NULL;
  1074. }
  1075. /* First check we're tall enough (ie enough topLevel) */
  1076. i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  1077. requiredTallness = 0;
  1078. while (i) {
  1079. i >>= YAFFS_TNODES_INTERNAL_BITS;
  1080. requiredTallness++;
  1081. }
  1082. if (requiredTallness > fStruct->topLevel) {
  1083. /* Not tall enough, so we can't find it, return NULL. */
  1084. return NULL;
  1085. }
  1086. /* Traverse down to level 0 */
  1087. while (level > 0 && tn) {
  1088. tn = tn->
  1089. internal[(chunkId >>
  1090. ( YAFFS_TNODES_LEVEL0_BITS +
  1091. (level - 1) *
  1092. YAFFS_TNODES_INTERNAL_BITS)
  1093. ) &
  1094. YAFFS_TNODES_INTERNAL_MASK];
  1095. level--;
  1096. }
  1097. return tn;
  1098. }
  1099. /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  1100. * This happens in two steps:
  1101. * 1. If the tree isn't tall enough, then make it taller.
  1102. * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  1103. *
  1104. * Used when modifying the tree.
  1105. *
  1106. * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  1107. * be plugged into the ttree.
  1108. */
  1109. static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
  1110. yaffs_FileStructure * fStruct,
  1111. __u32 chunkId,
  1112. yaffs_Tnode *passedTn)
  1113. {
  1114. int requiredTallness;
  1115. int i;
  1116. int l;
  1117. yaffs_Tnode *tn;
  1118. __u32 x;
  1119. /* Check sane level and page Id */
  1120. if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
  1121. return NULL;
  1122. }
  1123. if (chunkId > YAFFS_MAX_CHUNK_ID) {
  1124. return NULL;
  1125. }
  1126. /* First check we're tall enough (ie enough topLevel) */
  1127. x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  1128. requiredTallness = 0;
  1129. while (x) {
  1130. x >>= YAFFS_TNODES_INTERNAL_BITS;
  1131. requiredTallness++;
  1132. }
  1133. if (requiredTallness > fStruct->topLevel) {
  1134. /* Not tall enough,gotta make the tree taller */
  1135. for (i = fStruct->topLevel; i < requiredTallness; i++) {
  1136. tn = yaffs_GetTnode(dev);
  1137. if (tn) {
  1138. tn->internal[0] = fStruct->top;
  1139. fStruct->top = tn;
  1140. } else {
  1141. T(YAFFS_TRACE_ERROR,
  1142. (TSTR("yaffs: no more tnodes" TENDSTR)));
  1143. }
  1144. }
  1145. fStruct->topLevel = requiredTallness;
  1146. }
  1147. /* Traverse down to level 0, adding anything we need */
  1148. l = fStruct->topLevel;
  1149. tn = fStruct->top;
  1150. if(l > 0) {
  1151. while (l > 0 && tn) {
  1152. x = (chunkId >>
  1153. ( YAFFS_TNODES_LEVEL0_BITS +
  1154. (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  1155. YAFFS_TNODES_INTERNAL_MASK;
  1156. if((l>1) && !tn->internal[x]){
  1157. /* Add missing non-level-zero tnode */
  1158. tn->internal[x] = yaffs_GetTnode(dev);
  1159. } else if(l == 1) {
  1160. /* Looking from level 1 at level 0 */
  1161. if (passedTn) {
  1162. /* If we already have one, then release it.*/
  1163. if(tn->internal[x])
  1164. yaffs_FreeTnode(dev,tn->internal[x]);
  1165. tn->internal[x] = passedTn;
  1166. } else if(!tn->internal[x]) {
  1167. /* Don't have one, none passed in */
  1168. tn->internal[x] = yaffs_GetTnode(dev);
  1169. }
  1170. }
  1171. tn = tn->internal[x];
  1172. l--;
  1173. }
  1174. } else {
  1175. /* top is level 0 */
  1176. if(passedTn) {
  1177. memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
  1178. yaffs_FreeTnode(dev,passedTn);
  1179. }
  1180. }
  1181. return tn;
  1182. }
  1183. static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
  1184. yaffs_ExtendedTags * tags, int objectId,
  1185. int chunkInInode)
  1186. {
  1187. int j;
  1188. for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
  1189. if (yaffs_CheckChunkBit
  1190. (dev, theChunk / dev->nChunksPerBlock,
  1191. theChunk % dev->nChunksPerBlock)) {
  1192. yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
  1193. tags);
  1194. if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
  1195. /* found it; */
  1196. return theChunk;
  1197. }
  1198. }
  1199. theChunk++;
  1200. }
  1201. return -1;
  1202. }
  1203. /* DeleteWorker scans backwards through the tnode tree and deletes all the
  1204. * chunks and tnodes in the file
  1205. * Returns 1 if the tree was deleted.
  1206. * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
  1207. */
  1208. static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
  1209. int chunkOffset, int *limit)
  1210. {
  1211. int i;
  1212. int chunkInInode;
  1213. int theChunk;
  1214. yaffs_ExtendedTags tags;
  1215. int foundChunk;
  1216. yaffs_Device *dev = in->myDev;
  1217. int allDone = 1;
  1218. if (tn) {
  1219. if (level > 0) {
  1220. for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  1221. i--) {
  1222. if (tn->internal[i]) {
  1223. if (limit && (*limit) < 0) {
  1224. allDone = 0;
  1225. } else {
  1226. allDone =
  1227. yaffs_DeleteWorker(in,
  1228. tn->
  1229. internal
  1230. [i],
  1231. level -
  1232. 1,
  1233. (chunkOffset
  1234. <<
  1235. YAFFS_TNODES_INTERNAL_BITS)
  1236. + i,
  1237. limit);
  1238. }
  1239. if (allDone) {
  1240. yaffs_FreeTnode(dev,
  1241. tn->
  1242. internal[i]);
  1243. tn->internal[i] = NULL;
  1244. }
  1245. }
  1246. }
  1247. return (allDone) ? 1 : 0;
  1248. } else if (level == 0) {
  1249. int hitLimit = 0;
  1250. for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
  1251. i--) {
  1252. theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
  1253. if (theChunk) {
  1254. chunkInInode =
  1255. (chunkOffset <<
  1256. YAFFS_TNODES_LEVEL0_BITS) + i;
  1257. foundChunk =
  1258. yaffs_FindChunkInGroup(dev,
  1259. theChunk,
  1260. &tags,
  1261. in->objectId,
  1262. chunkInInode);
  1263. if (foundChunk > 0) {
  1264. yaffs_DeleteChunk(dev,
  1265. foundChunk, 1,
  1266. __LINE__);
  1267. in->nDataChunks--;
  1268. if (limit) {
  1269. *limit = *limit - 1;
  1270. if (*limit <= 0) {
  1271. hitLimit = 1;
  1272. }
  1273. }
  1274. }
  1275. yaffs_PutLevel0Tnode(dev,tn,i,0);
  1276. }
  1277. }
  1278. return (i < 0) ? 1 : 0;
  1279. }
  1280. }
  1281. return 1;
  1282. }
  1283. static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
  1284. {
  1285. yaffs_BlockInfo *theBlock;
  1286. T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
  1287. theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
  1288. if (theBlock) {
  1289. theBlock->softDeletions++;
  1290. dev->nFreeChunks++;
  1291. }
  1292. }
  1293. /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  1294. * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  1295. * of the tnode.
  1296. * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  1297. */
  1298. static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
  1299. __u32 level, int chunkOffset)
  1300. {
  1301. int i;
  1302. int theChunk;
  1303. int allDone = 1;
  1304. yaffs_Device *dev = in->myDev;
  1305. if (tn) {
  1306. if (level > 0) {
  1307. for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  1308. i--) {
  1309. if (tn->internal[i]) {
  1310. allDone =
  1311. yaffs_SoftDeleteWorker(in,
  1312. tn->
  1313. internal[i],
  1314. level - 1,
  1315. (chunkOffset
  1316. <<
  1317. YAFFS_TNODES_INTERNAL_BITS)
  1318. + i);
  1319. if (allDone) {
  1320. yaffs_FreeTnode(dev,
  1321. tn->
  1322. internal[i]);
  1323. tn->internal[i] = NULL;
  1324. } else {
  1325. /* Hoosterman... how could this happen? */
  1326. }
  1327. }
  1328. }
  1329. return (allDone) ? 1 : 0;
  1330. } else if (level == 0) {
  1331. for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  1332. theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
  1333. if (theChunk) {
  1334. /* Note this does not find the real chunk, only the chunk group.
  1335. * We make an assumption that a chunk group is not larger than
  1336. * a block.
  1337. */
  1338. yaffs_SoftDeleteChunk(dev, theChunk);
  1339. yaffs_PutLevel0Tnode(dev,tn,i,0);
  1340. }
  1341. }
  1342. return 1;
  1343. }
  1344. }
  1345. return 1;
  1346. }
  1347. static void yaffs_SoftDeleteFile(yaffs_Object * obj)
  1348. {
  1349. if (obj->deleted &&
  1350. obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
  1351. if (obj->nDataChunks <= 0) {
  1352. /* Empty file with no duplicate object headers, just delete it immediately */
  1353. yaffs_FreeTnode(obj->myDev,
  1354. obj->variant.fileVariant.top);
  1355. obj->variant.fileVariant.top = NULL;
  1356. T(YAFFS_TRACE_TRACING,
  1357. (TSTR("yaffs: Deleting empty file %d" TENDSTR),
  1358. obj->objectId));
  1359. yaffs_DoGenericObjectDeletion(obj);
  1360. } else {
  1361. yaffs_SoftDeleteWorker(obj,
  1362. obj->variant.fileVariant.top,
  1363. obj->variant.fileVariant.
  1364. topLevel, 0);
  1365. obj->softDeleted = 1;
  1366. }
  1367. }
  1368. }
  1369. /* Pruning removes any part of the file structure tree that is beyond the
  1370. * bounds of the file (ie that does not point to chunks).
  1371. *
  1372. * A file should only get pruned when its size is reduced.
  1373. *
  1374. * Before pruning, the chunks must be pulled from the tree and the
  1375. * level 0 tnode entries must be zeroed out.
  1376. * Could also use this for file deletion, but that's probably better handled
  1377. * by a special case.
  1378. */
  1379. static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
  1380. __u32 level, int del0)
  1381. {
  1382. int i;
  1383. int hasData;
  1384. if (tn) {
  1385. hasData = 0;
  1386. for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  1387. if (tn->internal[i] && level > 0) {
  1388. tn->internal[i] =
  1389. yaffs_PruneWorker(dev, tn->internal[i],
  1390. level - 1,
  1391. (i == 0) ? del0 : 1);
  1392. }
  1393. if (tn->internal[i]) {
  1394. hasData++;
  1395. }
  1396. }
  1397. if (hasData == 0 && del0) {
  1398. /* Free and return NULL */
  1399. yaffs_FreeTnode(dev, tn);
  1400. tn = NULL;
  1401. }
  1402. }
  1403. return tn;
  1404. }
  1405. static int yaffs_PruneFileStructure(yaffs_Device * dev,
  1406. yaffs_FileStructure * fStruct)
  1407. {
  1408. int i;
  1409. int hasData;
  1410. int done = 0;
  1411. yaffs_Tnode *tn;
  1412. if (fStruct->topLevel > 0) {
  1413. fStruct->top =
  1414. yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
  1415. /* Now we have a tree with all the non-zero branches NULL but the height
  1416. * is the same as it was.
  1417. * Let's see if we can trim internal tnodes to shorten the tree.
  1418. * We can do this if only the 0th element in the tnode is in use
  1419. * (ie all the non-zero are NULL)
  1420. */
  1421. while (fStruct->topLevel && !done) {
  1422. tn = fStruct->top;
  1423. hasData = 0;
  1424. for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  1425. if (tn->internal[i]) {
  1426. hasData++;
  1427. }
  1428. }
  1429. if (!hasData) {
  1430. fStruct->top = tn->internal[0];
  1431. fStruct->topLevel--;
  1432. yaffs_FreeTnode(dev, tn);
  1433. } else {
  1434. done = 1;
  1435. }
  1436. }
  1437. }
  1438. return YAFFS_OK;
  1439. }
  1440. /*-------------------- End of File Structure functions.-------------------*/
  1441. /* yaffs_CreateFreeObjects creates a bunch more objects and
  1442. * adds them to the object free list.
  1443. */
  1444. static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
  1445. {
  1446. int i;
  1447. yaffs_Object *newObjects;
  1448. yaffs_ObjectList *list;
  1449. if (nObjects < 1)
  1450. return YAFFS_OK;
  1451. /* make these things */
  1452. newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
  1453. list = YMALLOC(sizeof(yaffs_ObjectList));
  1454. if (!newObjects || !list) {
  1455. if(newObjects)
  1456. YFREE(newObjects);
  1457. if(list)
  1458. YFREE(list);
  1459. T(YAFFS_TRACE_ALLOCATE,
  1460. (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
  1461. return YAFFS_FAIL;
  1462. }
  1463. /* Hook them into the free list */
  1464. for (i = 0; i < nObjects - 1; i++) {
  1465. newObjects[i].siblings.next =
  1466. (struct list_head *)(&newObjects[i + 1]);
  1467. }
  1468. newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
  1469. dev->freeObjects = newObjects;
  1470. dev->nFreeObjects += nObjects;
  1471. dev->nObjectsCreated += nObjects;
  1472. /* Now add this bunch of Objects to a list for freeing up. */
  1473. list->objects = newObjects;
  1474. list->next = dev->allocatedObjectList;
  1475. dev->allocatedObjectList = list;
  1476. return YAFFS_OK;
  1477. }
  1478. /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  1479. static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
  1480. {
  1481. yaffs_Object *tn = NULL;
  1482. /* If there are none left make more */
  1483. if (!dev->freeObjects) {
  1484. yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
  1485. }
  1486. if (dev->freeObjects) {
  1487. tn = dev->freeObjects;
  1488. dev->freeObjects =
  1489. (yaffs_Object *) (dev->freeObjects->siblings.next);
  1490. dev->nFreeObjects--;
  1491. /* Now sweeten it up... */
  1492. memset(tn, 0, sizeof(yaffs_Object));
  1493. tn->myDev = dev;
  1494. tn->chunkId = -1;
  1495. tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
  1496. INIT_LIST_HEAD(&(tn->hardLinks));
  1497. INIT_LIST_HEAD(&(tn->hashLink));
  1498. INIT_LIST_HEAD(&tn->siblings);
  1499. /* Add it to the lost and found directory.
  1500. * NB Can't put root or lostNFound in lostNFound so
  1501. * check if lostNFound exists first
  1502. */
  1503. if (dev->lostNFoundDir) {
  1504. yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
  1505. }
  1506. }
  1507. return tn;
  1508. }
  1509. static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
  1510. __u32 mode)
  1511. {
  1512. yaffs_Object *obj =
  1513. yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  1514. if (obj) {
  1515. obj->fake = 1; /* it is fake so it has no NAND presence... */
  1516. obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
  1517. obj->unlinkAllowed = 0; /* ... or unlink it */
  1518. obj->deleted = 0;
  1519. obj->unlinked = 0;
  1520. obj->yst_mode = mode;
  1521. obj->myDev = dev;
  1522. obj->chunkId = 0; /* Not a valid chunk. */
  1523. }
  1524. return obj;
  1525. }
  1526. static void yaffs_UnhashObject(yaffs_Object * tn)
  1527. {
  1528. int bucket;
  1529. yaffs_Device *dev = tn->myDev;
  1530. /* If it is still linked into the bucket list, free from the list */
  1531. if (!list_empty(&tn->hashLink)) {
  1532. list_del_init(&tn->hashLink);
  1533. bucket = yaffs_HashFunction(tn->objectId);
  1534. dev->objectBucket[bucket].count--;
  1535. }
  1536. }
  1537. /* FreeObject frees up a Object and puts it back on the free list */
  1538. static void yaffs_FreeObject(yaffs_Object * tn)
  1539. {
  1540. yaffs_Device *dev = tn->myDev;
  1541. #ifdef __KERNEL__
  1542. if (tn->myInode) {
  1543. /* We're still hooked up to a cached inode.
  1544. * Don't delete now, but mark for later deletion
  1545. */
  1546. tn->deferedFree = 1;
  1547. return;
  1548. }
  1549. #endif
  1550. yaffs_UnhashObject(tn);
  1551. /* Link into the free list. */
  1552. tn->siblings.next = (struct list_head *)(dev->freeObjects);
  1553. dev->freeObjects = tn;
  1554. dev->nFreeObjects++;
  1555. }
  1556. #ifdef __KERNEL__
  1557. void yaffs_HandleDeferedFree(yaffs_Object * obj)
  1558. {
  1559. if (obj->deferedFree) {
  1560. yaffs_FreeObject(obj);
  1561. }
  1562. }
  1563. #endif
  1564. static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
  1565. {
  1566. /* Free the list of allocated Objects */
  1567. yaffs_ObjectList *tmp;
  1568. while (dev->allocatedObjectList) {
  1569. tmp = dev->allocatedObjectList->next;
  1570. YFREE(dev->allocatedObjectList->objects);
  1571. YFREE(dev->allocatedObjectList);
  1572. dev->allocatedObjectList = tmp;
  1573. }
  1574. dev->freeObjects = NULL;
  1575. dev->nFreeObjects = 0;
  1576. }
  1577. static void yaffs_InitialiseObjects(yaffs_Device * dev)
  1578. {
  1579. int i;
  1580. dev->allocatedObjectList = NULL;
  1581. dev->freeObjects = NULL;
  1582. dev->nFreeObjects = 0;
  1583. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  1584. INIT_LIST_HEAD(&dev->objectBucket[i].list);
  1585. dev->objectBucket[i].count = 0;
  1586. }
  1587. }
  1588. static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
  1589. {
  1590. static int x = 0;
  1591. int i;
  1592. int l = 999;
  1593. int lowest = 999999;
  1594. /* First let's see if we can find one that's empty. */
  1595. for (i = 0; i < 10 && lowest > 0; i++) {
  1596. x++;
  1597. x %= YAFFS_NOBJECT_BUCKETS;
  1598. if (dev->objectBucket[x].count < lowest) {
  1599. lowest = dev->objectBucket[x].count;
  1600. l = x;
  1601. }
  1602. }
  1603. /* If we didn't find an empty list, then try
  1604. * looking a bit further for a short one
  1605. */
  1606. for (i = 0; i < 10 && lowest > 3; i++) {
  1607. x++;
  1608. x %= YAFFS_NOBJECT_BUCKETS;
  1609. if (dev->objectBucket[x].count < lowest) {
  1610. lowest = dev->objectBucket[x].count;
  1611. l = x;
  1612. }
  1613. }
  1614. return l;
  1615. }
  1616. static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
  1617. {
  1618. int bucket = yaffs_FindNiceObjectBucket(dev);
  1619. /* Now find an object value that has not already been taken
  1620. * by scanning the list.
  1621. */
  1622. int found = 0;
  1623. struct list_head *i;
  1624. __u32 n = (__u32) bucket;
  1625. /* yaffs_CheckObjectHashSanity(); */
  1626. while (!found) {
  1627. found = 1;
  1628. n += YAFFS_NOBJECT_BUCKETS;
  1629. if (1 || dev->objectBucket[bucket].count > 0) {
  1630. list_for_each(i, &dev->objectBucket[bucket].list) {
  1631. /* If there is already one in the list */
  1632. if (i
  1633. && list_entry(i, yaffs_Object,
  1634. hashLink)->objectId == n) {
  1635. found = 0;
  1636. }
  1637. }
  1638. }
  1639. }
  1640. return n;
  1641. }
  1642. static void yaffs_HashObject(yaffs_Object * in)
  1643. {
  1644. int bucket = yaffs_HashFunction(in->objectId);
  1645. yaffs_Device *dev = in->myDev;
  1646. list_add(&in->hashLink, &dev->objectBucket[bucket].list);
  1647. dev->objectBucket[bucket].count++;
  1648. }
  1649. yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
  1650. {
  1651. int bucket = yaffs_HashFunction(number);
  1652. struct list_head *i;
  1653. yaffs_Object *in;
  1654. list_for_each(i, &dev->objectBucket[bucket].list) {
  1655. /* Look if it is in the list */
  1656. if (i) {
  1657. in = list_entry(i, yaffs_Object, hashLink);
  1658. if (in->objectId == number) {
  1659. #ifdef __KERNEL__
  1660. /* Don't tell the VFS about this one if it is defered free */
  1661. if (in->deferedFree)
  1662. return NULL;
  1663. #endif
  1664. return in;
  1665. }
  1666. }
  1667. }
  1668. return NULL;
  1669. }
  1670. yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
  1671. yaffs_ObjectType type)
  1672. {
  1673. yaffs_Object *theObject;
  1674. yaffs_Tnode *tn;
  1675. if (number < 0) {
  1676. number = yaffs_CreateNewObjectNumber(dev);
  1677. }
  1678. theObject = yaffs_AllocateEmptyObject(dev);
  1679. if(!theObject)
  1680. return NULL;
  1681. if(type == YAFFS_OBJECT_TYPE_FILE){
  1682. tn = yaffs_GetTnode(dev);
  1683. if(!tn){
  1684. yaffs_FreeObject(theObject);
  1685. return NULL;
  1686. }
  1687. }
  1688. if (theObject) {
  1689. theObject->fake = 0;
  1690. theObject->renameAllowed = 1;
  1691. theObject->unlinkAllowed = 1;
  1692. theObject->objectId = number;
  1693. yaffs_HashObject(theObject);
  1694. theObject->variantType = type;
  1695. #ifdef CONFIG_YAFFS_WINCE
  1696. yfsd_WinFileTimeNow(theObject->win_atime);
  1697. theObject->win_ctime[0] = theObject->win_mtime[0] =
  1698. theObject->win_atime[0];
  1699. theObject->win_ctime[1] = theObject->win_mtime[1] =
  1700. theObject->win_atime[1];
  1701. #else
  1702. theObject->yst_atime = theObject->yst_mtime =
  1703. theObject->yst_ctime = Y_CURRENT_TIME;
  1704. #endif
  1705. switch (type) {
  1706. case YAFFS_OBJECT_TYPE_FILE:
  1707. theObject->variant.fileVariant.fileSize = 0;
  1708. theObject->variant.fileVariant.scannedFileSize = 0;
  1709. theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
  1710. theObject->variant.fileVariant.topLevel = 0;
  1711. theObject->variant.fileVariant.top = tn;
  1712. break;
  1713. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1714. INIT_LIST_HEAD(&theObject->variant.directoryVariant.
  1715. children);
  1716. break;
  1717. case YAFFS_OBJECT_TYPE_SYMLINK:
  1718. case YAFFS_OBJECT_TYPE_HARDLINK:
  1719. case YAFFS_OBJECT_TYPE_SPECIAL:
  1720. /* No action required */
  1721. break;
  1722. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1723. /* todo this should not happen */
  1724. break;
  1725. }
  1726. }
  1727. return theObject;
  1728. }
  1729. static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
  1730. int number,
  1731. yaffs_ObjectType type)
  1732. {
  1733. yaffs_Object *theObject = NULL;
  1734. if (number > 0) {
  1735. theObject = yaffs_FindObjectByNumber(dev, number);
  1736. }
  1737. if (!theObject) {
  1738. theObject = yaffs_CreateNewObject(dev, number, type);
  1739. }
  1740. return theObject;
  1741. }
  1742. static YCHAR *yaffs_CloneString(const YCHAR * str)
  1743. {
  1744. YCHAR *newStr = NULL;
  1745. if (str && *str) {
  1746. newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
  1747. if(newStr)
  1748. yaffs_strcpy(newStr, str);
  1749. }
  1750. return newStr;
  1751. }
  1752. /*
  1753. * Mknod (create) a new object.
  1754. * equivalentObject only has meaning for a hard link;
  1755. * aliasString only has meaning for a sumlink.
  1756. * rdev only has meaning for devices (a subset of special objects)
  1757. */
  1758. static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
  1759. yaffs_Object * parent,
  1760. const YCHAR * name,
  1761. __u32 mode,
  1762. __u32 uid,
  1763. __u32 gid,
  1764. yaffs_Object * equivalentObject,
  1765. const YCHAR * aliasString, __u32 rdev)
  1766. {
  1767. yaffs_Object *in;
  1768. YCHAR *str;
  1769. yaffs_Device *dev = parent->myDev;
  1770. /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
  1771. if (yaffs_FindObjectByName(parent, name)) {
  1772. return NULL;
  1773. }
  1774. in = yaffs_CreateNewObject(dev, -1, type);
  1775. if(type == YAFFS_OBJECT_TYPE_SYMLINK){
  1776. str = yaffs_CloneString(aliasString);
  1777. if(!str){
  1778. yaffs_FreeObject(in);
  1779. return NULL;
  1780. }
  1781. }
  1782. if (in) {
  1783. in->chunkId = -1;
  1784. in->valid = 1;
  1785. in->variantType = type;
  1786. in->yst_mode = mode;
  1787. #ifdef CONFIG_YAFFS_WINCE
  1788. yfsd_WinFileTimeNow(in->win_atime);
  1789. in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
  1790. in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
  1791. #else
  1792. in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
  1793. in->yst_rdev = rdev;
  1794. in->yst_uid = uid;
  1795. in->yst_gid = gid;
  1796. #endif
  1797. in->nDataChunks = 0;
  1798. yaffs_SetObjectName(in, name);
  1799. in->dirty = 1;
  1800. yaffs_AddObjectToDirectory(parent, in);
  1801. in->myDev = parent->myDev;
  1802. switch (type) {
  1803. case YAFFS_OBJECT_TYPE_SYMLINK:
  1804. in->variant.symLinkVariant.alias = str;
  1805. break;
  1806. case YAFFS_OBJECT_TYPE_HARDLINK:
  1807. in->variant.hardLinkVariant.equivalentObject =
  1808. equivalentObject;
  1809. in->variant.hardLinkVariant.equivalentObjectId =
  1810. equivalentObject->objectId;
  1811. list_add(&in->hardLinks, &equivalentObject->hardLinks);
  1812. break;
  1813. case YAFFS_OBJECT_TYPE_FILE:
  1814. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1815. case YAFFS_OBJECT_TYPE_SPECIAL:
  1816. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1817. /* do nothing */
  1818. break;
  1819. }
  1820. if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
  1821. /* Could not create the object header, fail the creation */
  1822. yaffs_DestroyObject(in);
  1823. in = NULL;
  1824. }
  1825. }
  1826. return in;
  1827. }
  1828. yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
  1829. __u32 mode, __u32 uid, __u32 gid)
  1830. {
  1831. return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  1832. uid, gid, NULL, NULL, 0);
  1833. }
  1834. yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
  1835. __u32 mode, __u32 uid, __u32 gid)
  1836. {
  1837. return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  1838. mode, uid, gid, NULL, NULL, 0);
  1839. }
  1840. yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
  1841. __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
  1842. {
  1843. return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  1844. uid, gid, NULL, NULL, rdev);
  1845. }
  1846. yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
  1847. __u32 mode, __u32 uid, __u32 gid,
  1848. const YCHAR * alias)
  1849. {
  1850. return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  1851. uid, gid, NULL, alias, 0);
  1852. }
  1853. /* yaffs_Link returns the object id of the equivalent object.*/
  1854. yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
  1855. yaffs_Object * equivalentObject)
  1856. {
  1857. /* Get the real object in case we were fed a hard link as an equivalent object */
  1858. equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
  1859. if (yaffs_MknodObject
  1860. (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  1861. equivalentObject, NULL, 0)) {
  1862. return equivalentObject;
  1863. } else {
  1864. return NULL;
  1865. }
  1866. }
  1867. static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
  1868. const YCHAR * newName, int force, int shadows)
  1869. {
  1870. int unlinkOp;
  1871. int deleteOp;
  1872. yaffs_Object *existingTarget;
  1873. if (newDir == NULL) {
  1874. newDir = obj->parent; /* use the old directory */
  1875. }
  1876. if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  1877. T(YAFFS_TRACE_ALWAYS,
  1878. (TSTR
  1879. ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
  1880. TENDSTR)));
  1881. YBUG();
  1882. }
  1883. /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  1884. if (obj->myDev->isYaffs2) {
  1885. unlinkOp = (newDir == obj->myDev->unlinkedDir);
  1886. } else {
  1887. unlinkOp = (newDir == obj->myDev->unlinkedDir
  1888. && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
  1889. }
  1890. deleteOp = (newDir == obj->myDev->deletedDir);
  1891. existingTarget = yaffs_FindObjectByName(newDir, newName);
  1892. /* If the object is a file going into the unlinked directory,
  1893. * then it is OK to just stuff it in since duplicate names are allowed.
  1894. * else only proceed if the new name does not exist and if we're putting
  1895. * it into a directory.
  1896. */
  1897. if ((unlinkOp ||
  1898. deleteOp ||
  1899. force ||
  1900. (shadows > 0) ||
  1901. !existingTarget) &&
  1902. newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
  1903. yaffs_SetObjectName(obj, newName);
  1904. obj->dirty = 1;
  1905. yaffs_AddObjectToDirectory(newDir, obj);
  1906. if (unlinkOp)
  1907. obj->unlinked = 1;
  1908. /* If it is a deletion then we mark it as a shrink for gc purposes. */
  1909. if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
  1910. return YAFFS_OK;
  1911. }
  1912. return YAFFS_FAIL;
  1913. }
  1914. int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
  1915. yaffs_Object * newDir, const YCHAR * newName)
  1916. {
  1917. yaffs_Object *obj;
  1918. yaffs_Object *existingTarget;
  1919. int force = 0;
  1920. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  1921. /* Special case for case insemsitive systems (eg. WinCE).
  1922. * While look-up is case insensitive, the name isn't.
  1923. * Therefore we might want to change x.txt to X.txt
  1924. */
  1925. if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
  1926. force = 1;
  1927. }
  1928. #endif
  1929. obj = yaffs_FindObjectByName(oldDir, oldName);
  1930. /* Check new name to long. */
  1931. if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
  1932. yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
  1933. /* ENAMETOOLONG */
  1934. return YAFFS_FAIL;
  1935. else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
  1936. yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
  1937. /* ENAMETOOLONG */
  1938. return YAFFS_FAIL;
  1939. if (obj && obj->renameAllowed) {
  1940. /* Now do the handling for an existing target, if there is one */
  1941. existingTarget = yaffs_FindObjectByName(newDir, newName);
  1942. if (existingTarget &&
  1943. existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
  1944. !list_empty(&existingTarget->variant.directoryVariant.children)) {
  1945. /* There is a target that is a non-empty directory, so we fail */
  1946. return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
  1947. } else if (existingTarget && existingTarget != obj) {
  1948. /* Nuke the target first, using shadowing,
  1949. * but only if it isn't the same object
  1950. */
  1951. yaffs_ChangeObjectName(obj, newDir, newName, force,
  1952. existingTarget->objectId);
  1953. yaffs_UnlinkObject(existingTarget);
  1954. }
  1955. return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
  1956. }
  1957. return YAFFS_FAIL;
  1958. }
  1959. /*------------------------- Block Management and Page Allocation ----------------*/
  1960. static int yaffs_InitialiseBlocks(yaffs_Device * dev)
  1961. {
  1962. int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  1963. dev->blockInfo = NULL;
  1964. dev->chunkBits = NULL;
  1965. dev->allocationBlock = -1; /* force it to get a new one */
  1966. /* If the first allocation strategy fails, thry the alternate one */
  1967. dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
  1968. if(!dev->blockInfo){
  1969. dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
  1970. dev->blockInfoAlt = 1;
  1971. }
  1972. else
  1973. dev->blockInfoAlt = 0;
  1974. if(dev->blockInfo){
  1975. /* Set up dynamic blockinfo stuff. */
  1976. dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
  1977. dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
  1978. if(!dev->chunkBits){
  1979. dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
  1980. dev->chunkBitsAlt = 1;
  1981. }
  1982. else
  1983. dev->chunkBitsAlt = 0;
  1984. }
  1985. if (dev->blockInfo && dev->chunkBits) {
  1986. memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
  1987. memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
  1988. return YAFFS_OK;
  1989. }
  1990. return YAFFS_FAIL;
  1991. }
  1992. static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
  1993. {
  1994. if(dev->blockInfoAlt && dev->blockInfo)
  1995. YFREE_ALT(dev->blockInfo);
  1996. else if(dev->blockInfo)
  1997. YFREE(dev->blockInfo);
  1998. dev->blockInfoAlt = 0;
  1999. dev->blockInfo = NULL;
  2000. if(dev->chunkBitsAlt && dev->chunkBits)
  2001. YFREE_ALT(dev->chunkBits);
  2002. else if(dev->chunkBits)
  2003. YFREE(dev->chunkBits);
  2004. dev->chunkBitsAlt = 0;
  2005. dev->chunkBits = NULL;
  2006. }
  2007. static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
  2008. yaffs_BlockInfo * bi)
  2009. {
  2010. int i;
  2011. __u32 seq;
  2012. yaffs_BlockInfo *b;
  2013. if (!dev->isYaffs2)
  2014. return 1; /* disqualification only applies to yaffs2. */
  2015. if (!bi->hasShrinkHeader)
  2016. return 1; /* can gc */
  2017. /* Find the oldest dirty sequence number if we don't know it and save it
  2018. * so we don't have to keep recomputing it.
  2019. */
  2020. if (!dev->oldestDirtySequence) {
  2021. seq = dev->sequenceNumber;
  2022. for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
  2023. i++) {
  2024. b = yaffs_GetBlockInfo(dev, i);
  2025. if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
  2026. (b->pagesInUse - b->softDeletions) <
  2027. dev->nChunksPerBlock && b->sequenceNumber < seq) {
  2028. seq = b->sequenceNumber;
  2029. }
  2030. }
  2031. dev->oldestDirtySequence = seq;
  2032. }
  2033. /* Can't do gc of this block if there are any blocks older than this one that have
  2034. * discarded pages.
  2035. */
  2036. return (bi->sequenceNumber <= dev->oldestDirtySequence);
  2037. }
  2038. /* FindDiretiestBlock is used to select the dirtiest block (or close enough)
  2039. * for garbage collection.
  2040. */
  2041. static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
  2042. int aggressive)
  2043. {
  2044. int b = dev->currentDirtyChecker;
  2045. int i;
  2046. int iterations;
  2047. int dirtiest = -1;
  2048. int pagesInUse = 0;
  2049. int prioritised=0;
  2050. yaffs_BlockInfo *bi;
  2051. static int nonAggressiveSkip = 0;
  2052. int pendingPrioritisedExist = 0;
  2053. /* First let's see if we need to grab a prioritised block */
  2054. if(dev->hasPendingPrioritisedGCs){
  2055. for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
  2056. bi = yaffs_GetBlockInfo(dev, i);
  2057. //yaffs_VerifyBlock(dev,bi,i);
  2058. if(bi->gcPrioritise) {
  2059. pendingPrioritisedExist = 1;
  2060. if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  2061. yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
  2062. pagesInUse = (bi->pagesInUse - bi->softDeletions);
  2063. dirtiest = i;
  2064. prioritised = 1;
  2065. aggressive = 1; /* Fool the non-aggressive skip logiv below */
  2066. }
  2067. }
  2068. }
  2069. if(!pendingPrioritisedExist) /* None found, so we can clear this */
  2070. dev->hasPendingPrioritisedGCs = 0;
  2071. }
  2072. /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  2073. * search harder.
  2074. * else (we're doing a leasurely gc), then we only bother to do this if the
  2075. * block has only a few pages in use.
  2076. */
  2077. nonAggressiveSkip--;
  2078. if (!aggressive && (nonAggressiveSkip > 0)) {
  2079. return -1;
  2080. }
  2081. if(!prioritised)
  2082. pagesInUse =
  2083. (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
  2084. if (aggressive) {
  2085. iterations =
  2086. dev->internalEndBlock - dev->internalStartBlock + 1;
  2087. } else {
  2088. iterations =
  2089. dev->internalEndBlock - dev->internalStartBlock + 1;
  2090. iterations = iterations / 16;
  2091. if (iterations > 200) {
  2092. iterations = 200;
  2093. }
  2094. }
  2095. for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
  2096. b++;
  2097. if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
  2098. b = dev->internalStartBlock;
  2099. }
  2100. if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
  2101. T(YAFFS_TRACE_ERROR,
  2102. (TSTR("**>> Block %d is not valid" TENDSTR), b));
  2103. YBUG();
  2104. }
  2105. bi = yaffs_GetBlockInfo(dev, b);
  2106. #if 0
  2107. if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
  2108. dirtiest = b;
  2109. pagesInUse = 0;
  2110. }
  2111. else
  2112. #endif
  2113. if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  2114. (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
  2115. yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
  2116. dirtiest = b;
  2117. pagesInUse = (bi->pagesInUse - bi->softDeletions);
  2118. }
  2119. }
  2120. dev->currentDirtyChecker = b;
  2121. if (dirtiest > 0) {
  2122. T(YAFFS_TRACE_GC,
  2123. (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
  2124. dev->nChunksPerBlock - pagesInUse,prioritised));
  2125. }
  2126. dev->oldestDirtySequence = 0;
  2127. if (dirtiest > 0) {
  2128. nonAggressiveSkip = 4;
  2129. }
  2130. return dirtiest;
  2131. }
  2132. static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
  2133. {
  2134. yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
  2135. int erasedOk = 0;
  2136. /* If the block is still healthy erase it and mark as clean.
  2137. * If the block has had a data failure, then retire it.
  2138. */
  2139. T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  2140. (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
  2141. blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
  2142. bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
  2143. if (!bi->needsRetiring) {
  2144. yaffs_InvalidateCheckpoint(dev);
  2145. erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
  2146. if (!erasedOk) {
  2147. dev->nErasureFailures++;
  2148. T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  2149. (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
  2150. }
  2151. }
  2152. if (erasedOk &&
  2153. ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
  2154. int i;
  2155. for (i = 0; i < dev->nChunksPerBlock; i++) {
  2156. if (!yaffs_CheckChunkErased
  2157. (dev, blockNo * dev->nChunksPerBlock + i)) {
  2158. T(YAFFS_TRACE_ERROR,
  2159. (TSTR
  2160. (">>Block %d erasure supposedly OK, but chunk %d not erased"
  2161. TENDSTR), blockNo, i));
  2162. }
  2163. }
  2164. }
  2165. if (erasedOk) {
  2166. /* Clean it up... */
  2167. bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
  2168. dev->nErasedBlocks++;
  2169. bi->pagesInUse = 0;
  2170. bi->softDeletions = 0;
  2171. bi->hasShrinkHeader = 0;
  2172. bi->skipErasedCheck = 1; /* This is clean, so no need to check */
  2173. bi->gcPrioritise = 0;
  2174. yaffs_ClearChunkBits(dev, blockNo);
  2175. T(YAFFS_TRACE_ERASE,
  2176. (TSTR("Erased block %d" TENDSTR), blockNo));
  2177. } else {
  2178. dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
  2179. yaffs_RetireBlock(dev, blockNo);
  2180. T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  2181. (TSTR("**>> Block %d retired" TENDSTR), blockNo));
  2182. }
  2183. }
  2184. static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
  2185. {
  2186. int i;
  2187. yaffs_BlockInfo *bi;
  2188. if (dev->nErasedBlocks < 1) {
  2189. /* Hoosterman we've got a problem.
  2190. * Can't get space to gc
  2191. */
  2192. T(YAFFS_TRACE_ERROR,
  2193. (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
  2194. return -1;
  2195. }
  2196. /* Find an empty block. */
  2197. for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  2198. dev->allocationBlockFinder++;
  2199. if (dev->allocationBlockFinder < dev->internalStartBlock
  2200. || dev->allocationBlockFinder > dev->internalEndBlock) {
  2201. dev->allocationBlockFinder = dev->internalStartBlock;
  2202. }
  2203. bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
  2204. if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
  2205. bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
  2206. dev->sequenceNumber++;
  2207. bi->sequenceNumber = dev->sequenceNumber;
  2208. dev->nErasedBlocks--;
  2209. T(YAFFS_TRACE_ALLOCATE,
  2210. (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
  2211. dev->allocationBlockFinder, dev->sequenceNumber,
  2212. dev->nErasedBlocks));
  2213. return dev->allocationBlockFinder;
  2214. }
  2215. }
  2216. T(YAFFS_TRACE_ALWAYS,
  2217. (TSTR
  2218. ("yaffs tragedy: no more eraased blocks, but there should have been %d"
  2219. TENDSTR), dev->nErasedBlocks));
  2220. return -1;
  2221. }
  2222. // Check if there's space to allocate...
  2223. // Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
  2224. static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
  2225. {
  2226. int reservedChunks;
  2227. int reservedBlocks = dev->nReservedBlocks;
  2228. int checkpointBlocks;
  2229. checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
  2230. if(checkpointBlocks < 0)
  2231. checkpointBlocks = 0;
  2232. reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
  2233. return (dev->nFreeChunks > reservedChunks);
  2234. }
  2235. static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
  2236. {
  2237. int retVal;
  2238. yaffs_BlockInfo *bi;
  2239. if (dev->allocationBlock < 0) {
  2240. /* Get next block to allocate off */
  2241. dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
  2242. dev->allocationPage = 0;
  2243. }
  2244. if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
  2245. /* Not enough space to allocate unless we're allowed to use the reserve. */
  2246. return -1;
  2247. }
  2248. if (dev->nErasedBlocks < dev->nReservedBlocks
  2249. && dev->allocationPage == 0) {
  2250. T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
  2251. }
  2252. /* Next page please.... */
  2253. if (dev->allocationBlock >= 0) {
  2254. bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
  2255. retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
  2256. dev->allocationPage;
  2257. bi->pagesInUse++;
  2258. yaffs_SetChunkBit(dev, dev->allocationBlock,
  2259. dev->allocationPage);
  2260. dev->allocationPage++;
  2261. dev->nFreeChunks--;
  2262. /* If the block is full set the state to full */
  2263. if (dev->allocationPage >= dev->nChunksPerBlock) {
  2264. bi->blockState = YAFFS_BLOCK_STATE_FULL;
  2265. dev->allocationBlock = -1;
  2266. }
  2267. if(blockUsedPtr)
  2268. *blockUsedPtr = bi;
  2269. return retVal;
  2270. }
  2271. T(YAFFS_TRACE_ERROR,
  2272. (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
  2273. return -1;
  2274. }
  2275. static int yaffs_GetErasedChunks(yaffs_Device * dev)
  2276. {
  2277. int n;
  2278. n = dev->nErasedBlocks * dev->nChunksPerBlock;
  2279. if (dev->allocationBlock > 0) {
  2280. n += (dev->nChunksPerBlock - dev->allocationPage);
  2281. }
  2282. return n;
  2283. }
  2284. static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
  2285. {
  2286. int oldChunk;
  2287. int newChunk;
  2288. int chunkInBlock;
  2289. int markNAND;
  2290. int retVal = YAFFS_OK;
  2291. int cleanups = 0;
  2292. int i;
  2293. int isCheckpointBlock;
  2294. int matchingChunk;
  2295. int chunksBefore = yaffs_GetErasedChunks(dev);
  2296. int chunksAfter;
  2297. yaffs_ExtendedTags tags;
  2298. yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
  2299. yaffs_Object *object;
  2300. isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
  2301. bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
  2302. T(YAFFS_TRACE_TRACING,
  2303. (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
  2304. bi->pagesInUse, bi->hasShrinkHeader));
  2305. /*yaffs_VerifyFreeChunks(dev); */
  2306. bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
  2307. /* Take off the number of soft deleted entries because
  2308. * they're going to get really deleted during GC.
  2309. */
  2310. dev->nFreeChunks -= bi->softDeletions;
  2311. dev->isDoingGC = 1;
  2312. if (isCheckpointBlock ||
  2313. !yaffs_StillSomeChunkBits(dev, block)) {
  2314. T(YAFFS_TRACE_TRACING,
  2315. (TSTR
  2316. ("Collecting block %d that has no chunks in use" TENDSTR),
  2317. block));
  2318. yaffs_BlockBecameDirty(dev, block);
  2319. } else {
  2320. __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  2321. yaffs_VerifyBlock(dev,bi,block);
  2322. for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
  2323. chunkInBlock < dev->nChunksPerBlock
  2324. && yaffs_StillSomeChunkBits(dev, block);
  2325. chunkInBlock++, oldChunk++) {
  2326. if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
  2327. /* This page is in use and might need to be copied off */
  2328. markNAND = 1;
  2329. yaffs_InitialiseTags(&tags);
  2330. yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
  2331. buffer, &tags);
  2332. object =
  2333. yaffs_FindObjectByNumber(dev,
  2334. tags.objectId);
  2335. T(YAFFS_TRACE_GC_DETAIL,
  2336. (TSTR
  2337. ("Collecting page %d, %d %d %d " TENDSTR),
  2338. chunkInBlock, tags.objectId, tags.chunkId,
  2339. tags.byteCount));
  2340. if(object && !yaffs_SkipVerification(dev)){
  2341. if(tags.chunkId == 0)
  2342. matchingChunk = object->chunkId;
  2343. else if(object->softDeleted)
  2344. matchingChunk = oldChunk; /* Defeat the test */
  2345. else
  2346. matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
  2347. if(oldChunk != matchingChunk)
  2348. T(YAFFS_TRACE_ERROR,
  2349. (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
  2350. oldChunk,matchingChunk,tags.objectId, tags.chunkId));
  2351. }
  2352. if (!object) {
  2353. T(YAFFS_TRACE_ERROR,
  2354. (TSTR
  2355. ("page %d in gc has no object: %d %d %d "
  2356. TENDSTR), oldChunk,
  2357. tags.objectId, tags.chunkId, tags.byteCount));
  2358. }
  2359. if (object && object->deleted
  2360. && tags.chunkId != 0) {
  2361. /* Data chunk in a deleted file, throw it away
  2362. * It's a soft deleted data chunk,
  2363. * No need to copy this, just forget about it and
  2364. * fix up the object.
  2365. */
  2366. object->nDataChunks--;
  2367. if (object->nDataChunks <= 0) {
  2368. /* remeber to clean up the object */
  2369. dev->gcCleanupList[cleanups] =
  2370. tags.objectId;
  2371. cleanups++;
  2372. }
  2373. markNAND = 0;
  2374. } else if (0
  2375. /* Todo object && object->deleted && object->nDataChunks == 0 */
  2376. ) {
  2377. /* Deleted object header with no data chunks.
  2378. * Can be discarded and the file deleted.
  2379. */
  2380. object->chunkId = 0;
  2381. yaffs_FreeTnode(object->myDev,
  2382. object->variant.
  2383. fileVariant.top);
  2384. object->variant.fileVariant.top = NULL;
  2385. yaffs_DoGenericObjectDeletion(object);
  2386. } else if (object) {
  2387. /* It's either a data chunk in a live file or
  2388. * an ObjectHeader, so we're interested in it.
  2389. * NB Need to keep the ObjectHeaders of deleted files
  2390. * until the whole file has been deleted off
  2391. */
  2392. tags.serialNumber++;
  2393. dev->nGCCopies++;
  2394. if (tags.chunkId == 0) {
  2395. /* It is an object Id,
  2396. * We need to nuke the shrinkheader flags first
  2397. * We no longer want the shrinkHeader flag since its work is done
  2398. * and if it is left in place it will mess up scanning.
  2399. * Also, clear out any shadowing stuff
  2400. */
  2401. yaffs_ObjectHeader *oh;
  2402. oh = (yaffs_ObjectHeader *)buffer;
  2403. oh->isShrink = 0;
  2404. oh->shadowsObject = -1;
  2405. tags.extraShadows = 0;
  2406. tags.extraIsShrinkHeader = 0;
  2407. yaffs_VerifyObjectHeader(object,oh,&tags,1);
  2408. }
  2409. newChunk =
  2410. yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
  2411. if (newChunk < 0) {
  2412. retVal = YAFFS_FAIL;
  2413. } else {
  2414. /* Ok, now fix up the Tnodes etc. */
  2415. if (tags.chunkId == 0) {
  2416. /* It's a header */
  2417. object->chunkId = newChunk;
  2418. object->serial = tags.serialNumber;
  2419. } else {
  2420. /* It's a data chunk */
  2421. yaffs_PutChunkIntoFile
  2422. (object,
  2423. tags.chunkId,
  2424. newChunk, 0);
  2425. }
  2426. }
  2427. }
  2428. yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
  2429. }
  2430. }
  2431. yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  2432. /* Do any required cleanups */
  2433. for (i = 0; i < cleanups; i++) {
  2434. /* Time to delete the file too */
  2435. object =
  2436. yaffs_FindObjectByNumber(dev,
  2437. dev->gcCleanupList[i]);
  2438. if (object) {
  2439. yaffs_FreeTnode(dev,
  2440. object->variant.fileVariant.
  2441. top);
  2442. object->variant.fileVariant.top = NULL;
  2443. T(YAFFS_TRACE_GC,
  2444. (TSTR
  2445. ("yaffs: About to finally delete object %d"
  2446. TENDSTR), object->objectId));
  2447. yaffs_DoGenericObjectDeletion(object);
  2448. object->myDev->nDeletedFiles--;
  2449. }
  2450. }
  2451. }
  2452. yaffs_VerifyCollectedBlock(dev,bi,block);
  2453. if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
  2454. T(YAFFS_TRACE_GC,
  2455. (TSTR
  2456. ("gc did not increase free chunks before %d after %d"
  2457. TENDSTR), chunksBefore, chunksAfter));
  2458. }
  2459. dev->isDoingGC = 0;
  2460. return YAFFS_OK;
  2461. }
  2462. /* New garbage collector
  2463. * If we're very low on erased blocks then we do aggressive garbage collection
  2464. * otherwise we do "leasurely" garbage collection.
  2465. * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  2466. * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  2467. *
  2468. * The idea is to help clear out space in a more spread-out manner.
  2469. * Dunno if it really does anything useful.
  2470. */
  2471. static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
  2472. {
  2473. int block;
  2474. int aggressive;
  2475. int gcOk = YAFFS_OK;
  2476. int maxTries = 0;
  2477. int checkpointBlockAdjust;
  2478. if (dev->isDoingGC) {
  2479. /* Bail out so we don't get recursive gc */
  2480. return YAFFS_OK;
  2481. }
  2482. /* This loop should pass the first time.
  2483. * We'll only see looping here if the erase of the collected block fails.
  2484. */
  2485. do {
  2486. maxTries++;
  2487. checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
  2488. if(checkpointBlockAdjust < 0)
  2489. checkpointBlockAdjust = 0;
  2490. if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
  2491. /* We need a block soon...*/
  2492. aggressive = 1;
  2493. } else {
  2494. /* We're in no hurry */
  2495. aggressive = 0;
  2496. }
  2497. block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
  2498. if (block > 0) {
  2499. dev->garbageCollections++;
  2500. if (!aggressive) {
  2501. dev->passiveGarbageCollections++;
  2502. }
  2503. T(YAFFS_TRACE_GC,
  2504. (TSTR
  2505. ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
  2506. dev->nErasedBlocks, aggressive));
  2507. gcOk = yaffs_GarbageCollectBlock(dev, block);
  2508. }
  2509. if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
  2510. T(YAFFS_TRACE_GC,
  2511. (TSTR
  2512. ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
  2513. TENDSTR), dev->nErasedBlocks, maxTries, block));
  2514. }
  2515. } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
  2516. && (maxTries < 2));
  2517. return aggressive ? gcOk : YAFFS_OK;
  2518. }
  2519. /*------------------------- TAGS --------------------------------*/
  2520. static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
  2521. int chunkInObject)
  2522. {
  2523. return (tags->chunkId == chunkInObject &&
  2524. tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
  2525. }
  2526. /*-------------------- Data file manipulation -----------------*/
  2527. static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
  2528. yaffs_ExtendedTags * tags)
  2529. {
  2530. /*Get the Tnode, then get the level 0 offset chunk offset */
  2531. yaffs_Tnode *tn;
  2532. int theChunk = -1;
  2533. yaffs_ExtendedTags localTags;
  2534. int retVal = -1;
  2535. yaffs_Device *dev = in->myDev;
  2536. if (!tags) {
  2537. /* Passed a NULL, so use our own tags space */
  2538. tags = &localTags;
  2539. }
  2540. tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  2541. if (tn) {
  2542. theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
  2543. retVal =
  2544. yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  2545. chunkInInode);
  2546. }
  2547. return retVal;
  2548. }
  2549. static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
  2550. yaffs_ExtendedTags * tags)
  2551. {
  2552. /* Get the Tnode, then get the level 0 offset chunk offset */
  2553. yaffs_Tnode *tn;
  2554. int theChunk = -1;
  2555. yaffs_ExtendedTags localTags;
  2556. yaffs_Device *dev = in->myDev;
  2557. int retVal = -1;
  2558. if (!tags) {
  2559. /* Passed a NULL, so use our own tags space */
  2560. tags = &localTags;
  2561. }
  2562. tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  2563. if (tn) {
  2564. theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
  2565. retVal =
  2566. yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  2567. chunkInInode);
  2568. /* Delete the entry in the filestructure (if found) */
  2569. if (retVal != -1) {
  2570. yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
  2571. }
  2572. } else {
  2573. /*T(("No level 0 found for %d\n", chunkInInode)); */
  2574. }
  2575. if (retVal == -1) {
  2576. /* T(("Could not find %d to delete\n",chunkInInode)); */
  2577. }
  2578. return retVal;
  2579. }
  2580. #ifdef YAFFS_PARANOID
  2581. static int yaffs_CheckFileSanity(yaffs_Object * in)
  2582. {
  2583. int chunk;
  2584. int nChunks;
  2585. int fSize;
  2586. int failed = 0;
  2587. int objId;
  2588. yaffs_Tnode *tn;
  2589. yaffs_Tags localTags;
  2590. yaffs_Tags *tags = &localTags;
  2591. int theChunk;
  2592. int chunkDeleted;
  2593. if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
  2594. /* T(("Object not a file\n")); */
  2595. return YAFFS_FAIL;
  2596. }
  2597. objId = in->objectId;
  2598. fSize = in->variant.fileVariant.fileSize;
  2599. nChunks =
  2600. (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
  2601. for (chunk = 1; chunk <= nChunks; chunk++) {
  2602. tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
  2603. chunk);
  2604. if (tn) {
  2605. theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
  2606. if (yaffs_CheckChunkBits
  2607. (dev, theChunk / dev->nChunksPerBlock,
  2608. theChunk % dev->nChunksPerBlock)) {
  2609. yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
  2610. tags,
  2611. &chunkDeleted);
  2612. if (yaffs_TagsMatch
  2613. (tags, in->objectId, chunk, chunkDeleted)) {
  2614. /* found it; */
  2615. }
  2616. } else {
  2617. failed = 1;
  2618. }
  2619. } else {
  2620. /* T(("No level 0 found for %d\n", chunk)); */
  2621. }
  2622. }
  2623. return failed ? YAFFS_FAIL : YAFFS_OK;
  2624. }
  2625. #endif
  2626. static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
  2627. int chunkInNAND, int inScan)
  2628. {
  2629. /* NB inScan is zero unless scanning.
  2630. * For forward scanning, inScan is > 0;
  2631. * for backward scanning inScan is < 0
  2632. */
  2633. yaffs_Tnode *tn;
  2634. yaffs_Device *dev = in->myDev;
  2635. int existingChunk;
  2636. yaffs_ExtendedTags existingTags;
  2637. yaffs_ExtendedTags newTags;
  2638. unsigned existingSerial, newSerial;
  2639. if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
  2640. /* Just ignore an attempt at putting a chunk into a non-file during scanning
  2641. * If it is not during Scanning then something went wrong!
  2642. */
  2643. if (!inScan) {
  2644. T(YAFFS_TRACE_ERROR,
  2645. (TSTR
  2646. ("yaffs tragedy:attempt to put data chunk into a non-file"
  2647. TENDSTR)));
  2648. YBUG();
  2649. }
  2650. yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  2651. return YAFFS_OK;
  2652. }
  2653. tn = yaffs_AddOrFindLevel0Tnode(dev,
  2654. &in->variant.fileVariant,
  2655. chunkInInode,
  2656. NULL);
  2657. if (!tn) {
  2658. return YAFFS_FAIL;
  2659. }
  2660. existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
  2661. if (inScan != 0) {
  2662. /* If we're scanning then we need to test for duplicates
  2663. * NB This does not need to be efficient since it should only ever
  2664. * happen when the power fails during a write, then only one
  2665. * chunk should ever be affected.
  2666. *
  2667. * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  2668. * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  2669. */
  2670. if (existingChunk != 0) {
  2671. /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
  2672. * thus we have to do a FindChunkInFile to get the real chunk id.
  2673. *
  2674. * We have a duplicate now we need to decide which one to use:
  2675. *
  2676. * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  2677. * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  2678. * YAFFS1: Get both sets of tags and compare serial numbers.
  2679. */
  2680. if (inScan > 0) {
  2681. /* Only do this for forward scanning */
  2682. yaffs_ReadChunkWithTagsFromNAND(dev,
  2683. chunkInNAND,
  2684. NULL, &newTags);
  2685. /* Do a proper find */
  2686. existingChunk =
  2687. yaffs_FindChunkInFile(in, chunkInInode,
  2688. &existingTags);
  2689. }
  2690. if (existingChunk <= 0) {
  2691. /*Hoosterman - how did this happen? */
  2692. T(YAFFS_TRACE_ERROR,
  2693. (TSTR
  2694. ("yaffs tragedy: existing chunk < 0 in scan"
  2695. TENDSTR)));
  2696. }
  2697. /* NB The deleted flags should be false, otherwise the chunks will
  2698. * not be loaded during a scan
  2699. */
  2700. newSerial = newTags.serialNumber;
  2701. existingSerial = existingTags.serialNumber;
  2702. if ((inScan > 0) &&
  2703. (in->myDev->isYaffs2 ||
  2704. existingChunk <= 0 ||
  2705. ((existingSerial + 1) & 3) == newSerial)) {
  2706. /* Forward scanning.
  2707. * Use new
  2708. * Delete the old one and drop through to update the tnode
  2709. */
  2710. yaffs_DeleteChunk(dev, existingChunk, 1,
  2711. __LINE__);
  2712. } else {
  2713. /* Backward scanning or we want to use the existing one
  2714. * Use existing.
  2715. * Delete the new one and return early so that the tnode isn't changed
  2716. */
  2717. yaffs_DeleteChunk(dev, chunkInNAND, 1,
  2718. __LINE__);
  2719. return YAFFS_OK;
  2720. }
  2721. }
  2722. }
  2723. if (existingChunk == 0) {
  2724. in->nDataChunks++;
  2725. }
  2726. yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
  2727. return YAFFS_OK;
  2728. }
  2729. static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
  2730. __u8 * buffer)
  2731. {
  2732. int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
  2733. if (chunkInNAND >= 0) {
  2734. return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
  2735. buffer,NULL);
  2736. } else {
  2737. T(YAFFS_TRACE_NANDACCESS,
  2738. (TSTR("Chunk %d not found zero instead" TENDSTR),
  2739. chunkInNAND));
  2740. /* get sane (zero) data if you read a hole */
  2741. memset(buffer, 0, in->myDev->nDataBytesPerChunk);
  2742. return 0;
  2743. }
  2744. }
  2745. void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
  2746. {
  2747. int block;
  2748. int page;
  2749. yaffs_ExtendedTags tags;
  2750. yaffs_BlockInfo *bi;
  2751. if (chunkId <= 0)
  2752. return;
  2753. dev->nDeletions++;
  2754. block = chunkId / dev->nChunksPerBlock;
  2755. page = chunkId % dev->nChunksPerBlock;
  2756. if(!yaffs_CheckChunkBit(dev,block,page))
  2757. T(YAFFS_TRACE_VERIFY,
  2758. (TSTR("Deleting invalid chunk %d"TENDSTR),
  2759. chunkId));
  2760. bi = yaffs_GetBlockInfo(dev, block);
  2761. T(YAFFS_TRACE_DELETION,
  2762. (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
  2763. if (markNAND &&
  2764. bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
  2765. yaffs_InitialiseTags(&tags);
  2766. tags.chunkDeleted = 1;
  2767. yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
  2768. yaffs_HandleUpdateChunk(dev, chunkId, &tags);
  2769. } else {
  2770. dev->nUnmarkedDeletions++;
  2771. }
  2772. /* Pull out of the management area.
  2773. * If the whole block became dirty, this will kick off an erasure.
  2774. */
  2775. if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
  2776. bi->blockState == YAFFS_BLOCK_STATE_FULL ||
  2777. bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  2778. bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
  2779. dev->nFreeChunks++;
  2780. yaffs_ClearChunkBit(dev, block, page);
  2781. bi->pagesInUse--;
  2782. if (bi->pagesInUse == 0 &&
  2783. !bi->hasShrinkHeader &&
  2784. bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
  2785. bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  2786. yaffs_BlockBecameDirty(dev, block);
  2787. }
  2788. } else {
  2789. /* T(("Bad news deleting chunk %d\n",chunkId)); */
  2790. }
  2791. }
  2792. static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
  2793. const __u8 * buffer, int nBytes,
  2794. int useReserve)
  2795. {
  2796. /* Find old chunk Need to do this to get serial number
  2797. * Write new one and patch into tree.
  2798. * Invalidate old tags.
  2799. */
  2800. int prevChunkId;
  2801. yaffs_ExtendedTags prevTags;
  2802. int newChunkId;
  2803. yaffs_ExtendedTags newTags;
  2804. yaffs_Device *dev = in->myDev;
  2805. yaffs_CheckGarbageCollection(dev);
  2806. /* Get the previous chunk at this location in the file if it exists */
  2807. prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
  2808. /* Set up new tags */
  2809. yaffs_InitialiseTags(&newTags);
  2810. newTags.chunkId = chunkInInode;
  2811. newTags.objectId = in->objectId;
  2812. newTags.serialNumber =
  2813. (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
  2814. newTags.byteCount = nBytes;
  2815. newChunkId =
  2816. yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  2817. useReserve);
  2818. if (newChunkId >= 0) {
  2819. yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
  2820. if (prevChunkId >= 0) {
  2821. yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
  2822. }
  2823. yaffs_CheckFileSanity(in);
  2824. }
  2825. return newChunkId;
  2826. }
  2827. /* UpdateObjectHeader updates the header on NAND for an object.
  2828. * If name is not NULL, then that new name is used.
  2829. */
  2830. int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
  2831. int isShrink, int shadows)
  2832. {
  2833. yaffs_BlockInfo *bi;
  2834. yaffs_Device *dev = in->myDev;
  2835. int prevChunkId;
  2836. int retVal = 0;
  2837. int result = 0;
  2838. int newChunkId;
  2839. yaffs_ExtendedTags newTags;
  2840. yaffs_ExtendedTags oldTags;
  2841. __u8 *buffer = NULL;
  2842. YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
  2843. yaffs_ObjectHeader *oh = NULL;
  2844. yaffs_strcpy(oldName,"silly old name");
  2845. if (!in->fake || force) {
  2846. yaffs_CheckGarbageCollection(dev);
  2847. yaffs_CheckObjectDetailsLoaded(in);
  2848. buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
  2849. oh = (yaffs_ObjectHeader *) buffer;
  2850. prevChunkId = in->chunkId;
  2851. if (prevChunkId >= 0) {
  2852. result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
  2853. buffer, &oldTags);
  2854. yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
  2855. memcpy(oldName, oh->name, sizeof(oh->name));
  2856. }
  2857. memset(buffer, 0xFF, dev->nDataBytesPerChunk);
  2858. oh->type = in->variantType;
  2859. oh->yst_mode = in->yst_mode;
  2860. oh->shadowsObject = shadows;
  2861. #ifdef CONFIG_YAFFS_WINCE
  2862. oh->win_atime[0] = in->win_atime[0];
  2863. oh->win_ctime[0] = in->win_ctime[0];
  2864. oh->win_mtime[0] = in->win_mtime[0];
  2865. oh->win_atime[1] = in->win_atime[1];
  2866. oh->win_ctime[1] = in->win_ctime[1];
  2867. oh->win_mtime[1] = in->win_mtime[1];
  2868. #else
  2869. oh->yst_uid = in->yst_uid;
  2870. oh->yst_gid = in->yst_gid;
  2871. oh->yst_atime = in->yst_atime;
  2872. oh->yst_mtime = in->yst_mtime;
  2873. oh->yst_ctime = in->yst_ctime;
  2874. oh->yst_rdev = in->yst_rdev;
  2875. #endif
  2876. if (in->parent) {
  2877. oh->parentObjectId = in->parent->objectId;
  2878. } else {
  2879. oh->parentObjectId = 0;
  2880. }
  2881. if (name && *name) {
  2882. memset(oh->name, 0, sizeof(oh->name));
  2883. yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
  2884. } else if (prevChunkId>=0) {
  2885. memcpy(oh->name, oldName, sizeof(oh->name));
  2886. } else {
  2887. memset(oh->name, 0, sizeof(oh->name));
  2888. }
  2889. oh->isShrink = isShrink;
  2890. switch (in->variantType) {
  2891. case YAFFS_OBJECT_TYPE_UNKNOWN:
  2892. /* Should not happen */
  2893. break;
  2894. case YAFFS_OBJECT_TYPE_FILE:
  2895. oh->fileSize =
  2896. (oh->parentObjectId == YAFFS_OBJECTID_DELETED
  2897. || oh->parentObjectId ==
  2898. YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
  2899. fileVariant.fileSize;
  2900. break;
  2901. case YAFFS_OBJECT_TYPE_HARDLINK:
  2902. oh->equivalentObjectId =
  2903. in->variant.hardLinkVariant.equivalentObjectId;
  2904. break;
  2905. case YAFFS_OBJECT_TYPE_SPECIAL:
  2906. /* Do nothing */
  2907. break;
  2908. case YAFFS_OBJECT_TYPE_DIRECTORY:
  2909. /* Do nothing */
  2910. break;
  2911. case YAFFS_OBJECT_TYPE_SYMLINK:
  2912. yaffs_strncpy(oh->alias,
  2913. in->variant.symLinkVariant.alias,
  2914. YAFFS_MAX_ALIAS_LENGTH);
  2915. oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  2916. break;
  2917. }
  2918. /* Tags */
  2919. yaffs_InitialiseTags(&newTags);
  2920. in->serial++;
  2921. newTags.chunkId = 0;
  2922. newTags.objectId = in->objectId;
  2923. newTags.serialNumber = in->serial;
  2924. /* Add extra info for file header */
  2925. newTags.extraHeaderInfoAvailable = 1;
  2926. newTags.extraParentObjectId = oh->parentObjectId;
  2927. newTags.extraFileLength = oh->fileSize;
  2928. newTags.extraIsShrinkHeader = oh->isShrink;
  2929. newTags.extraEquivalentObjectId = oh->equivalentObjectId;
  2930. newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
  2931. newTags.extraObjectType = in->variantType;
  2932. yaffs_VerifyObjectHeader(in,oh,&newTags,1);
  2933. /* Create new chunk in NAND */
  2934. newChunkId =
  2935. yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  2936. (prevChunkId >= 0) ? 1 : 0);
  2937. if (newChunkId >= 0) {
  2938. in->chunkId = newChunkId;
  2939. if (prevChunkId >= 0) {
  2940. yaffs_DeleteChunk(dev, prevChunkId, 1,
  2941. __LINE__);
  2942. }
  2943. if(!yaffs_ObjectHasCachedWriteData(in))
  2944. in->dirty = 0;
  2945. /* If this was a shrink, then mark the block that the chunk lives on */
  2946. if (isShrink) {
  2947. bi = yaffs_GetBlockInfo(in->myDev,
  2948. newChunkId /in->myDev-> nChunksPerBlock);
  2949. bi->hasShrinkHeader = 1;
  2950. }
  2951. }
  2952. retVal = newChunkId;
  2953. }
  2954. if (buffer)
  2955. yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  2956. return retVal;
  2957. }
  2958. /*------------------------ Short Operations Cache ----------------------------------------
  2959. * In many situations where there is no high level buffering (eg WinCE) a lot of
  2960. * reads might be short sequential reads, and a lot of writes may be short
  2961. * sequential writes. eg. scanning/writing a jpeg file.
  2962. * In these cases, a short read/write cache can provide a huge perfomance benefit
  2963. * with dumb-as-a-rock code.
  2964. * In Linux, the page cache provides read buffering aand the short op cache provides write
  2965. * buffering.
  2966. *
  2967. * There are a limited number (~10) of cache chunks per device so that we don't
  2968. * need a very intelligent search.
  2969. */
  2970. static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
  2971. {
  2972. yaffs_Device *dev = obj->myDev;
  2973. int i;
  2974. yaffs_ChunkCache *cache;
  2975. int nCaches = obj->myDev->nShortOpCaches;
  2976. for(i = 0; i < nCaches; i++){
  2977. cache = &dev->srCache[i];
  2978. if (cache->object == obj &&
  2979. cache->dirty)
  2980. return 1;
  2981. }
  2982. return 0;
  2983. }
  2984. static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
  2985. {
  2986. yaffs_Device *dev = obj->myDev;
  2987. int lowest = -99; /* Stop compiler whining. */
  2988. int i;
  2989. yaffs_ChunkCache *cache;
  2990. int chunkWritten = 0;
  2991. int nCaches = obj->myDev->nShortOpCaches;
  2992. if (nCaches > 0) {
  2993. do {
  2994. cache = NULL;
  2995. /* Find the dirty cache for this object with the lowest chunk id. */
  2996. for (i = 0; i < nCaches; i++) {
  2997. if (dev->srCache[i].object == obj &&
  2998. dev->srCache[i].dirty) {
  2999. if (!cache
  3000. || dev->srCache[i].chunkId <
  3001. lowest) {
  3002. cache = &dev->srCache[i];
  3003. lowest = cache->chunkId;
  3004. }
  3005. }
  3006. }
  3007. if (cache && !cache->locked) {
  3008. /* Write it out and free it up */
  3009. chunkWritten =
  3010. yaffs_WriteChunkDataToObject(cache->object,
  3011. cache->chunkId,
  3012. cache->data,
  3013. cache->nBytes,
  3014. 1);
  3015. cache->dirty = 0;
  3016. cache->object = NULL;
  3017. }
  3018. } while (cache && chunkWritten > 0);
  3019. if (cache) {
  3020. /* Hoosterman, disk full while writing cache out. */
  3021. T(YAFFS_TRACE_ERROR,
  3022. (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
  3023. }
  3024. }
  3025. }
  3026. /*yaffs_FlushEntireDeviceCache(dev)
  3027. *
  3028. *
  3029. */
  3030. void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
  3031. {
  3032. yaffs_Object *obj;
  3033. int nCaches = dev->nShortOpCaches;
  3034. int i;
  3035. /* Find a dirty object in the cache and flush it...
  3036. * until there are no further dirty objects.
  3037. */
  3038. do {
  3039. obj = NULL;
  3040. for( i = 0; i < nCaches && !obj; i++) {
  3041. if (dev->srCache[i].object &&
  3042. dev->srCache[i].dirty)
  3043. obj = dev->srCache[i].object;
  3044. }
  3045. if(obj)
  3046. yaffs_FlushFilesChunkCache(obj);
  3047. } while(obj);
  3048. }
  3049. /* Grab us a cache chunk for use.
  3050. * First look for an empty one.
  3051. * Then look for the least recently used non-dirty one.
  3052. * Then look for the least recently used dirty one...., flush and look again.
  3053. */
  3054. static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
  3055. {
  3056. int i;
  3057. int usage;
  3058. int theOne;
  3059. if (dev->nShortOpCaches > 0) {
  3060. for (i = 0; i < dev->nShortOpCaches; i++) {
  3061. if (!dev->srCache[i].object)
  3062. return &dev->srCache[i];
  3063. }
  3064. return NULL;
  3065. theOne = -1;
  3066. usage = 0; /* just to stop the compiler grizzling */
  3067. for (i = 0; i < dev->nShortOpCaches; i++) {
  3068. if (!dev->srCache[i].dirty &&
  3069. ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
  3070. theOne < 0)) {
  3071. usage = dev->srCache[i].lastUse;
  3072. theOne = i;
  3073. }
  3074. }
  3075. return theOne >= 0 ? &dev->srCache[theOne] : NULL;
  3076. } else {
  3077. return NULL;
  3078. }
  3079. }
  3080. static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
  3081. {
  3082. yaffs_ChunkCache *cache;
  3083. yaffs_Object *theObj;
  3084. int usage;
  3085. int i;
  3086. int pushout;
  3087. if (dev->nShortOpCaches > 0) {
  3088. /* Try find a non-dirty one... */
  3089. cache = yaffs_GrabChunkCacheWorker(dev);
  3090. if (!cache) {
  3091. /* They were all dirty, find the last recently used object and flush
  3092. * its cache, then find again.
  3093. * NB what's here is not very accurate, we actually flush the object
  3094. * the last recently used page.
  3095. */
  3096. /* With locking we can't assume we can use entry zero */
  3097. theObj = NULL;
  3098. usage = -1;
  3099. cache = NULL;
  3100. pushout = -1;
  3101. for (i = 0; i < dev->nShortOpCaches; i++) {
  3102. if (dev->srCache[i].object &&
  3103. !dev->srCache[i].locked &&
  3104. (dev->srCache[i].lastUse < usage || !cache))
  3105. {
  3106. usage = dev->srCache[i].lastUse;
  3107. theObj = dev->srCache[i].object;
  3108. cache = &dev->srCache[i];
  3109. pushout = i;
  3110. }
  3111. }
  3112. if (!cache || cache->dirty) {
  3113. /* Flush and try again */
  3114. yaffs_FlushFilesChunkCache(theObj);
  3115. cache = yaffs_GrabChunkCacheWorker(dev);
  3116. }
  3117. }
  3118. return cache;
  3119. } else
  3120. return NULL;
  3121. }
  3122. /* Find a cached chunk */
  3123. static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
  3124. int chunkId)
  3125. {
  3126. yaffs_Device *dev = obj->myDev;
  3127. int i;
  3128. if (dev->nShortOpCaches > 0) {
  3129. for (i = 0; i < dev->nShortOpCaches; i++) {
  3130. if (dev->srCache[i].object == obj &&
  3131. dev->srCache[i].chunkId == chunkId) {
  3132. dev->cacheHits++;
  3133. return &dev->srCache[i];
  3134. }
  3135. }
  3136. }
  3137. return NULL;
  3138. }
  3139. /* Mark the chunk for the least recently used algorithym */
  3140. static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
  3141. int isAWrite)
  3142. {
  3143. if (dev->nShortOpCaches > 0) {
  3144. if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
  3145. /* Reset the cache usages */
  3146. int i;
  3147. for (i = 1; i < dev->nShortOpCaches; i++) {
  3148. dev->srCache[i].lastUse = 0;
  3149. }
  3150. dev->srLastUse = 0;
  3151. }
  3152. dev->srLastUse++;
  3153. cache->lastUse = dev->srLastUse;
  3154. if (isAWrite) {
  3155. cache->dirty = 1;
  3156. }
  3157. }
  3158. }
  3159. /* Invalidate a single cache page.
  3160. * Do this when a whole page gets written,
  3161. * ie the short cache for this page is no longer valid.
  3162. */
  3163. static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
  3164. {
  3165. if (object->myDev->nShortOpCaches > 0) {
  3166. yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
  3167. if (cache) {
  3168. cache->object = NULL;
  3169. }
  3170. }
  3171. }
  3172. /* Invalidate all the cache pages associated with this object
  3173. * Do this whenever ther file is deleted or resized.
  3174. */
  3175. static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
  3176. {
  3177. int i;
  3178. yaffs_Device *dev = in->myDev;
  3179. if (dev->nShortOpCaches > 0) {
  3180. /* Invalidate it. */
  3181. for (i = 0; i < dev->nShortOpCaches; i++) {
  3182. if (dev->srCache[i].object == in) {
  3183. dev->srCache[i].object = NULL;
  3184. }
  3185. }
  3186. }
  3187. }
  3188. /*--------------------- Checkpointing --------------------*/
  3189. static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
  3190. {
  3191. yaffs_CheckpointValidity cp;
  3192. memset(&cp,0,sizeof(cp));
  3193. cp.structType = sizeof(cp);
  3194. cp.magic = YAFFS_MAGIC;
  3195. cp.version = YAFFS_CHECKPOINT_VERSION;
  3196. cp.head = (head) ? 1 : 0;
  3197. return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
  3198. 1 : 0;
  3199. }
  3200. static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
  3201. {
  3202. yaffs_CheckpointValidity cp;
  3203. int ok;
  3204. ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
  3205. if(ok)
  3206. ok = (cp.structType == sizeof(cp)) &&
  3207. (cp.magic == YAFFS_MAGIC) &&
  3208. (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  3209. (cp.head == ((head) ? 1 : 0));
  3210. return ok ? 1 : 0;
  3211. }
  3212. static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
  3213. yaffs_Device *dev)
  3214. {
  3215. cp->nErasedBlocks = dev->nErasedBlocks;
  3216. cp->allocationBlock = dev->allocationBlock;
  3217. cp->allocationPage = dev->allocationPage;
  3218. cp->nFreeChunks = dev->nFreeChunks;
  3219. cp->nDeletedFiles = dev->nDeletedFiles;
  3220. cp->nUnlinkedFiles = dev->nUnlinkedFiles;
  3221. cp->nBackgroundDeletions = dev->nBackgroundDeletions;
  3222. cp->sequenceNumber = dev->sequenceNumber;
  3223. cp->oldestDirtySequence = dev->oldestDirtySequence;
  3224. }
  3225. static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
  3226. yaffs_CheckpointDevice *cp)
  3227. {
  3228. dev->nErasedBlocks = cp->nErasedBlocks;
  3229. dev->allocationBlock = cp->allocationBlock;
  3230. dev->allocationPage = cp->allocationPage;
  3231. dev->nFreeChunks = cp->nFreeChunks;
  3232. dev->nDeletedFiles = cp->nDeletedFiles;
  3233. dev->nUnlinkedFiles = cp->nUnlinkedFiles;
  3234. dev->nBackgroundDeletions = cp->nBackgroundDeletions;
  3235. dev->sequenceNumber = cp->sequenceNumber;
  3236. dev->oldestDirtySequence = cp->oldestDirtySequence;
  3237. }
  3238. static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
  3239. {
  3240. yaffs_CheckpointDevice cp;
  3241. __u32 nBytes;
  3242. __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  3243. int ok;
  3244. /* Write device runtime values*/
  3245. yaffs_DeviceToCheckpointDevice(&cp,dev);
  3246. cp.structType = sizeof(cp);
  3247. ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
  3248. /* Write block info */
  3249. if(ok) {
  3250. nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  3251. ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
  3252. }
  3253. /* Write chunk bits */
  3254. if(ok) {
  3255. nBytes = nBlocks * dev->chunkBitmapStride;
  3256. ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
  3257. }
  3258. return ok ? 1 : 0;
  3259. }
  3260. static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
  3261. {
  3262. yaffs_CheckpointDevice cp;
  3263. __u32 nBytes;
  3264. __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  3265. int ok;
  3266. ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
  3267. if(!ok)
  3268. return 0;
  3269. if(cp.structType != sizeof(cp))
  3270. return 0;
  3271. yaffs_CheckpointDeviceToDevice(dev,&cp);
  3272. nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  3273. ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
  3274. if(!ok)
  3275. return 0;
  3276. nBytes = nBlocks * dev->chunkBitmapStride;
  3277. ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
  3278. return ok ? 1 : 0;
  3279. }
  3280. static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
  3281. yaffs_Object *obj)
  3282. {
  3283. cp->objectId = obj->objectId;
  3284. cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
  3285. cp->chunkId = obj->chunkId;
  3286. cp->variantType = obj->variantType;
  3287. cp->deleted = obj->deleted;
  3288. cp->softDeleted = obj->softDeleted;
  3289. cp->unlinked = obj->unlinked;
  3290. cp->fake = obj->fake;
  3291. cp->renameAllowed = obj->renameAllowed;
  3292. cp->unlinkAllowed = obj->unlinkAllowed;
  3293. cp->serial = obj->serial;
  3294. cp->nDataChunks = obj->nDataChunks;
  3295. if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  3296. cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
  3297. else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  3298. cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
  3299. }
  3300. static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
  3301. {
  3302. yaffs_Object *parent;
  3303. obj->objectId = cp->objectId;
  3304. if(cp->parentId)
  3305. parent = yaffs_FindOrCreateObjectByNumber(
  3306. obj->myDev,
  3307. cp->parentId,
  3308. YAFFS_OBJECT_TYPE_DIRECTORY);
  3309. else
  3310. parent = NULL;
  3311. if(parent)
  3312. yaffs_AddObjectToDirectory(parent, obj);
  3313. obj->chunkId = cp->chunkId;
  3314. obj->variantType = cp->variantType;
  3315. obj->deleted = cp->deleted;
  3316. obj->softDeleted = cp->softDeleted;
  3317. obj->unlinked = cp->unlinked;
  3318. obj->fake = cp->fake;
  3319. obj->renameAllowed = cp->renameAllowed;
  3320. obj->unlinkAllowed = cp->unlinkAllowed;
  3321. obj->serial = cp->serial;
  3322. obj->nDataChunks = cp->nDataChunks;
  3323. if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  3324. obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
  3325. else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  3326. obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
  3327. if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
  3328. obj->lazyLoaded = 1;
  3329. }
  3330. static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
  3331. __u32 level, int chunkOffset)
  3332. {
  3333. int i;
  3334. yaffs_Device *dev = in->myDev;
  3335. int ok = 1;
  3336. int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  3337. if (tn) {
  3338. if (level > 0) {
  3339. for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
  3340. if (tn->internal[i]) {
  3341. ok = yaffs_CheckpointTnodeWorker(in,
  3342. tn->internal[i],
  3343. level - 1,
  3344. (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  3345. }
  3346. }
  3347. } else if (level == 0) {
  3348. __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
  3349. /* printf("write tnode at %d\n",baseOffset); */
  3350. ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
  3351. if(ok)
  3352. ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
  3353. }
  3354. }
  3355. return ok;
  3356. }
  3357. static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
  3358. {
  3359. __u32 endMarker = ~0;
  3360. int ok = 1;
  3361. if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
  3362. ok = yaffs_CheckpointTnodeWorker(obj,
  3363. obj->variant.fileVariant.top,
  3364. obj->variant.fileVariant.topLevel,
  3365. 0);
  3366. if(ok)
  3367. ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
  3368. sizeof(endMarker));
  3369. }
  3370. return ok ? 1 : 0;
  3371. }
  3372. static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
  3373. {
  3374. __u32 baseChunk;
  3375. int ok = 1;
  3376. yaffs_Device *dev = obj->myDev;
  3377. yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
  3378. yaffs_Tnode *tn;
  3379. int nread = 0;
  3380. ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
  3381. while(ok && (~baseChunk)){
  3382. nread++;
  3383. /* Read level 0 tnode */
  3384. /* printf("read tnode at %d\n",baseChunk); */
  3385. tn = yaffs_GetTnodeRaw(dev);
  3386. if(tn)
  3387. ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
  3388. (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
  3389. else
  3390. ok = 0;
  3391. if(tn && ok){
  3392. ok = yaffs_AddOrFindLevel0Tnode(dev,
  3393. fileStructPtr,
  3394. baseChunk,
  3395. tn) ? 1 : 0;
  3396. }
  3397. if(ok)
  3398. ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
  3399. }
  3400. T(YAFFS_TRACE_CHECKPOINT,(
  3401. TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
  3402. nread,baseChunk,ok));
  3403. return ok ? 1 : 0;
  3404. }
  3405. static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
  3406. {
  3407. yaffs_Object *obj;
  3408. yaffs_CheckpointObject cp;
  3409. int i;
  3410. int ok = 1;
  3411. struct list_head *lh;
  3412. /* Iterate through the objects in each hash entry,
  3413. * dumping them to the checkpointing stream.
  3414. */
  3415. for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
  3416. list_for_each(lh, &dev->objectBucket[i].list) {
  3417. if (lh) {
  3418. obj = list_entry(lh, yaffs_Object, hashLink);
  3419. if (!obj->deferedFree) {
  3420. yaffs_ObjectToCheckpointObject(&cp,obj);
  3421. cp.structType = sizeof(cp);
  3422. T(YAFFS_TRACE_CHECKPOINT,(
  3423. TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
  3424. cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
  3425. ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
  3426. if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
  3427. ok = yaffs_WriteCheckpointTnodes(obj);
  3428. }
  3429. }
  3430. }
  3431. }
  3432. }
  3433. /* Dump end of list */
  3434. memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
  3435. cp.structType = sizeof(cp);
  3436. if(ok)
  3437. ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
  3438. return ok ? 1 : 0;
  3439. }
  3440. static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
  3441. {
  3442. yaffs_Object *obj;
  3443. yaffs_CheckpointObject cp;
  3444. int ok = 1;
  3445. int done = 0;
  3446. yaffs_Object *hardList = NULL;
  3447. while(ok && !done) {
  3448. ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
  3449. if(cp.structType != sizeof(cp)) {
  3450. T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
  3451. cp.structType,sizeof(cp),ok));
  3452. ok = 0;
  3453. }
  3454. T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
  3455. cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
  3456. if(ok && cp.objectId == ~0)
  3457. done = 1;
  3458. else if(ok){
  3459. obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
  3460. if(obj) {
  3461. yaffs_CheckpointObjectToObject(obj,&cp);
  3462. if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  3463. ok = yaffs_ReadCheckpointTnodes(obj);
  3464. } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  3465. obj->hardLinks.next =
  3466. (struct list_head *)
  3467. hardList;
  3468. hardList = obj;
  3469. }
  3470. }
  3471. }
  3472. }
  3473. if(ok)
  3474. yaffs_HardlinkFixup(dev,hardList);
  3475. return ok ? 1 : 0;
  3476. }
  3477. static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
  3478. {
  3479. __u32 checkpointSum;
  3480. int ok;
  3481. yaffs_GetCheckpointSum(dev,&checkpointSum);
  3482. ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
  3483. if(!ok)
  3484. return 0;
  3485. return 1;
  3486. }
  3487. static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
  3488. {
  3489. __u32 checkpointSum0;
  3490. __u32 checkpointSum1;
  3491. int ok;
  3492. yaffs_GetCheckpointSum(dev,&checkpointSum0);
  3493. ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
  3494. if(!ok)
  3495. return 0;
  3496. if(checkpointSum0 != checkpointSum1)
  3497. return 0;
  3498. return 1;
  3499. }
  3500. static int yaffs_WriteCheckpointData(yaffs_Device *dev)
  3501. {
  3502. int ok = 1;
  3503. if(dev->skipCheckpointWrite || !dev->isYaffs2){
  3504. T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
  3505. ok = 0;
  3506. }
  3507. if(ok)
  3508. ok = yaffs_CheckpointOpen(dev,1);
  3509. if(ok){
  3510. T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
  3511. ok = yaffs_WriteCheckpointValidityMarker(dev,1);
  3512. }
  3513. if(ok){
  3514. T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
  3515. ok = yaffs_WriteCheckpointDevice(dev);
  3516. }
  3517. if(ok){
  3518. T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
  3519. ok = yaffs_WriteCheckpointObjects(dev);
  3520. }
  3521. if(ok){
  3522. T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
  3523. ok = yaffs_WriteCheckpointValidityMarker(dev,0);
  3524. }
  3525. if(ok){
  3526. ok = yaffs_WriteCheckpointSum(dev);
  3527. }
  3528. if(!yaffs_CheckpointClose(dev))
  3529. ok = 0;
  3530. if(ok)
  3531. dev->isCheckpointed = 1;
  3532. else
  3533. dev->isCheckpointed = 0;
  3534. return dev->isCheckpointed;
  3535. }
  3536. static int yaffs_ReadCheckpointData(yaffs_Device *dev)
  3537. {
  3538. int ok = 1;
  3539. if(dev->skipCheckpointRead || !dev->isYaffs2){
  3540. T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
  3541. ok = 0;
  3542. }
  3543. if(ok)
  3544. ok = yaffs_CheckpointOpen(dev,0); /* open for read */
  3545. if(ok){
  3546. T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
  3547. ok = yaffs_ReadCheckpointValidityMarker(dev,1);
  3548. }
  3549. if(ok){
  3550. T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
  3551. ok = yaffs_ReadCheckpointDevice(dev);
  3552. }
  3553. if(ok){
  3554. T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
  3555. ok = yaffs_ReadCheckpointObjects(dev);
  3556. }
  3557. if(ok){
  3558. T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
  3559. ok = yaffs_ReadCheckpointValidityMarker(dev,0);
  3560. }
  3561. if(ok){
  3562. ok = yaffs_ReadCheckpointSum(dev);
  3563. T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
  3564. }
  3565. if(!yaffs_CheckpointClose(dev))
  3566. ok = 0;
  3567. if(ok)
  3568. dev->isCheckpointed = 1;
  3569. else
  3570. dev->isCheckpointed = 0;
  3571. return ok ? 1 : 0;
  3572. }
  3573. static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
  3574. {
  3575. if(dev->isCheckpointed ||
  3576. dev->blocksInCheckpoint > 0){
  3577. dev->isCheckpointed = 0;
  3578. yaffs_CheckpointInvalidateStream(dev);
  3579. if(dev->superBlock && dev->markSuperBlockDirty)
  3580. dev->markSuperBlockDirty(dev->superBlock);
  3581. }
  3582. }
  3583. int yaffs_CheckpointSave(yaffs_Device *dev)
  3584. {
  3585. T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
  3586. yaffs_VerifyObjects(dev);
  3587. yaffs_VerifyBlocks(dev);
  3588. yaffs_VerifyFreeChunks(dev);
  3589. if(!dev->isCheckpointed) {
  3590. yaffs_InvalidateCheckpoint(dev);
  3591. yaffs_WriteCheckpointData(dev);
  3592. }
  3593. T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
  3594. return dev->isCheckpointed;
  3595. }
  3596. int yaffs_CheckpointRestore(yaffs_Device *dev)
  3597. {
  3598. int retval;
  3599. T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
  3600. retval = yaffs_ReadCheckpointData(dev);
  3601. if(dev->isCheckpointed){
  3602. yaffs_VerifyObjects(dev);
  3603. yaffs_VerifyBlocks(dev);
  3604. yaffs_VerifyFreeChunks(dev);
  3605. }
  3606. T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
  3607. return retval;
  3608. }
  3609. /*--------------------- File read/write ------------------------
  3610. * Read and write have very similar structures.
  3611. * In general the read/write has three parts to it
  3612. * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  3613. * Some complete chunks
  3614. * An incomplete chunk to end off with
  3615. *
  3616. * Curve-balls: the first chunk might also be the last chunk.
  3617. */
  3618. int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
  3619. int nBytes)
  3620. {
  3621. int chunk;
  3622. int start;
  3623. int nToCopy;
  3624. int n = nBytes;
  3625. int nDone = 0;
  3626. yaffs_ChunkCache *cache;
  3627. yaffs_Device *dev;
  3628. dev = in->myDev;
  3629. while (n > 0) {
  3630. //chunk = offset / dev->nDataBytesPerChunk + 1;
  3631. //start = offset % dev->nDataBytesPerChunk;
  3632. yaffs_AddrToChunk(dev,offset,&chunk,&start);
  3633. chunk++;
  3634. /* OK now check for the curveball where the start and end are in
  3635. * the same chunk.
  3636. */
  3637. if ((start + n) < dev->nDataBytesPerChunk) {
  3638. nToCopy = n;
  3639. } else {
  3640. nToCopy = dev->nDataBytesPerChunk - start;
  3641. }
  3642. cache = yaffs_FindChunkCache(in, chunk);
  3643. /* If the chunk is already in the cache or it is less than a whole chunk
  3644. * then use the cache (if there is caching)
  3645. * else bypass the cache.
  3646. */
  3647. if (cache || nToCopy != dev->nDataBytesPerChunk) {
  3648. if (dev->nShortOpCaches > 0) {
  3649. /* If we can't find the data in the cache, then load it up. */
  3650. if (!cache) {
  3651. cache = yaffs_GrabChunkCache(in->myDev);
  3652. cache->object = in;
  3653. cache->chunkId = chunk;
  3654. cache->dirty = 0;
  3655. cache->locked = 0;
  3656. yaffs_ReadChunkDataFromObject(in, chunk,
  3657. cache->
  3658. data);
  3659. cache->nBytes = 0;
  3660. }
  3661. yaffs_UseChunkCache(dev, cache, 0);
  3662. cache->locked = 1;
  3663. #ifdef CONFIG_YAFFS_WINCE
  3664. yfsd_UnlockYAFFS(TRUE);
  3665. #endif
  3666. memcpy(buffer, &cache->data[start], nToCopy);
  3667. #ifdef CONFIG_YAFFS_WINCE
  3668. yfsd_LockYAFFS(TRUE);
  3669. #endif
  3670. cache->locked = 0;
  3671. } else {
  3672. /* Read into the local buffer then copy..*/
  3673. __u8 *localBuffer =
  3674. yaffs_GetTempBuffer(dev, __LINE__);
  3675. yaffs_ReadChunkDataFromObject(in, chunk,
  3676. localBuffer);
  3677. #ifdef CONFIG_YAFFS_WINCE
  3678. yfsd_UnlockYAFFS(TRUE);
  3679. #endif
  3680. memcpy(buffer, &localBuffer[start], nToCopy);
  3681. #ifdef CONFIG_YAFFS_WINCE
  3682. yfsd_LockYAFFS(TRUE);
  3683. #endif
  3684. yaffs_ReleaseTempBuffer(dev, localBuffer,
  3685. __LINE__);
  3686. }
  3687. } else {
  3688. #ifdef CONFIG_YAFFS_WINCE
  3689. __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
  3690. /* Under WinCE can't do direct transfer. Need to use a local buffer.
  3691. * This is because we otherwise screw up WinCE's memory mapper
  3692. */
  3693. yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
  3694. #ifdef CONFIG_YAFFS_WINCE
  3695. yfsd_UnlockYAFFS(TRUE);
  3696. #endif
  3697. memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
  3698. #ifdef CONFIG_YAFFS_WINCE
  3699. yfsd_LockYAFFS(TRUE);
  3700. yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
  3701. #endif
  3702. #else
  3703. /* A full chunk. Read directly into the supplied buffer. */
  3704. yaffs_ReadChunkDataFromObject(in, chunk, buffer);
  3705. #endif
  3706. }
  3707. n -= nToCopy;
  3708. offset += nToCopy;
  3709. buffer += nToCopy;
  3710. nDone += nToCopy;
  3711. }
  3712. return nDone;
  3713. }
  3714. int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
  3715. int nBytes, int writeThrough)
  3716. {
  3717. int chunk;
  3718. int start;
  3719. int nToCopy;
  3720. int n = nBytes;
  3721. int nDone = 0;
  3722. int nToWriteBack;
  3723. int startOfWrite = offset;
  3724. int chunkWritten = 0;
  3725. int nBytesRead;
  3726. yaffs_Device *dev;
  3727. dev = in->myDev;
  3728. while (n > 0 && chunkWritten >= 0) {
  3729. //chunk = offset / dev->nDataBytesPerChunk + 1;
  3730. //start = offset % dev->nDataBytesPerChunk;
  3731. yaffs_AddrToChunk(dev,offset,&chunk,&start);
  3732. chunk++;
  3733. /* OK now check for the curveball where the start and end are in
  3734. * the same chunk.
  3735. */
  3736. if ((start + n) < dev->nDataBytesPerChunk) {
  3737. nToCopy = n;
  3738. /* Now folks, to calculate how many bytes to write back....
  3739. * If we're overwriting and not writing to then end of file then
  3740. * we need to write back as much as was there before.
  3741. */
  3742. nBytesRead =
  3743. in->variant.fileVariant.fileSize -
  3744. ((chunk - 1) * dev->nDataBytesPerChunk);
  3745. if (nBytesRead > dev->nDataBytesPerChunk) {
  3746. nBytesRead = dev->nDataBytesPerChunk;
  3747. }
  3748. nToWriteBack =
  3749. (nBytesRead >
  3750. (start + n)) ? nBytesRead : (start + n);
  3751. } else {
  3752. nToCopy = dev->nDataBytesPerChunk - start;
  3753. nToWriteBack = dev->nDataBytesPerChunk;
  3754. }
  3755. if (nToCopy != dev->nDataBytesPerChunk) {
  3756. /* An incomplete start or end chunk (or maybe both start and end chunk) */
  3757. if (dev->nShortOpCaches > 0) {
  3758. yaffs_ChunkCache *cache;
  3759. /* If we can't find the data in the cache, then load the cache */
  3760. cache = yaffs_FindChunkCache(in, chunk);
  3761. if (!cache
  3762. && yaffs_CheckSpaceForAllocation(in->
  3763. myDev)) {
  3764. cache = yaffs_GrabChunkCache(in->myDev);
  3765. cache->object = in;
  3766. cache->chunkId = chunk;
  3767. cache->dirty = 0;
  3768. cache->locked = 0;
  3769. yaffs_ReadChunkDataFromObject(in, chunk,
  3770. cache->
  3771. data);
  3772. }
  3773. else if(cache &&
  3774. !cache->dirty &&
  3775. !yaffs_CheckSpaceForAllocation(in->myDev)){
  3776. /* Drop the cache if it was a read cache item and
  3777. * no space check has been made for it.
  3778. */
  3779. cache = NULL;
  3780. }
  3781. if (cache) {
  3782. yaffs_UseChunkCache(dev, cache, 1);
  3783. cache->locked = 1;
  3784. #ifdef CONFIG_YAFFS_WINCE
  3785. yfsd_UnlockYAFFS(TRUE);
  3786. #endif
  3787. memcpy(&cache->data[start], buffer,
  3788. nToCopy);
  3789. #ifdef CONFIG_YAFFS_WINCE
  3790. yfsd_LockYAFFS(TRUE);
  3791. #endif
  3792. cache->locked = 0;
  3793. cache->nBytes = nToWriteBack;
  3794. if (writeThrough) {
  3795. chunkWritten =
  3796. yaffs_WriteChunkDataToObject
  3797. (cache->object,
  3798. cache->chunkId,
  3799. cache->data, cache->nBytes,
  3800. 1);
  3801. cache->dirty = 0;
  3802. }
  3803. } else {
  3804. chunkWritten = -1; /* fail the write */
  3805. }
  3806. } else {
  3807. /* An incomplete start or end chunk (or maybe both start and end chunk)
  3808. * Read into the local buffer then copy, then copy over and write back.
  3809. */
  3810. __u8 *localBuffer =
  3811. yaffs_GetTempBuffer(dev, __LINE__);
  3812. yaffs_ReadChunkDataFromObject(in, chunk,
  3813. localBuffer);
  3814. #ifdef CONFIG_YAFFS_WINCE
  3815. yfsd_UnlockYAFFS(TRUE);
  3816. #endif
  3817. memcpy(&localBuffer[start], buffer, nToCopy);
  3818. #ifdef CONFIG_YAFFS_WINCE
  3819. yfsd_LockYAFFS(TRUE);
  3820. #endif
  3821. chunkWritten =
  3822. yaffs_WriteChunkDataToObject(in, chunk,
  3823. localBuffer,
  3824. nToWriteBack,
  3825. 0);
  3826. yaffs_ReleaseTempBuffer(dev, localBuffer,
  3827. __LINE__);
  3828. }
  3829. } else {
  3830. #ifdef CONFIG_YAFFS_WINCE
  3831. /* Under WinCE can't do direct transfer. Need to use a local buffer.
  3832. * This is because we otherwise screw up WinCE's memory mapper
  3833. */
  3834. __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
  3835. #ifdef CONFIG_YAFFS_WINCE
  3836. yfsd_UnlockYAFFS(TRUE);
  3837. #endif
  3838. memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
  3839. #ifdef CONFIG_YAFFS_WINCE
  3840. yfsd_LockYAFFS(TRUE);
  3841. #endif
  3842. chunkWritten =
  3843. yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
  3844. dev->nDataBytesPerChunk,
  3845. 0);
  3846. yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
  3847. #else
  3848. /* A full chunk. Write directly from the supplied buffer. */
  3849. chunkWritten =
  3850. yaffs_WriteChunkDataToObject(in, chunk, buffer,
  3851. dev->nDataBytesPerChunk,
  3852. 0);
  3853. #endif
  3854. /* Since we've overwritten the cached data, we better invalidate it. */
  3855. yaffs_InvalidateChunkCache(in, chunk);
  3856. }
  3857. if (chunkWritten >= 0) {
  3858. n -= nToCopy;
  3859. offset += nToCopy;
  3860. buffer += nToCopy;
  3861. nDone += nToCopy;
  3862. }
  3863. }
  3864. /* Update file object */
  3865. if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
  3866. in->variant.fileVariant.fileSize = (startOfWrite + nDone);
  3867. }
  3868. in->dirty = 1;
  3869. return nDone;
  3870. }
  3871. /* ---------------------- File resizing stuff ------------------ */
  3872. static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
  3873. {
  3874. yaffs_Device *dev = in->myDev;
  3875. int oldFileSize = in->variant.fileVariant.fileSize;
  3876. int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
  3877. int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
  3878. dev->nDataBytesPerChunk;
  3879. int i;
  3880. int chunkId;
  3881. /* Delete backwards so that we don't end up with holes if
  3882. * power is lost part-way through the operation.
  3883. */
  3884. for (i = lastDel; i >= startDel; i--) {
  3885. /* NB this could be optimised somewhat,
  3886. * eg. could retrieve the tags and write them without
  3887. * using yaffs_DeleteChunk
  3888. */
  3889. chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
  3890. if (chunkId > 0) {
  3891. if (chunkId <
  3892. (dev->internalStartBlock * dev->nChunksPerBlock)
  3893. || chunkId >=
  3894. ((dev->internalEndBlock +
  3895. 1) * dev->nChunksPerBlock)) {
  3896. T(YAFFS_TRACE_ALWAYS,
  3897. (TSTR("Found daft chunkId %d for %d" TENDSTR),
  3898. chunkId, i));
  3899. } else {
  3900. in->nDataChunks--;
  3901. yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
  3902. }
  3903. }
  3904. }
  3905. }
  3906. int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
  3907. {
  3908. int oldFileSize = in->variant.fileVariant.fileSize;
  3909. int newSizeOfPartialChunk;
  3910. int newFullChunks;
  3911. yaffs_Device *dev = in->myDev;
  3912. yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
  3913. yaffs_FlushFilesChunkCache(in);
  3914. yaffs_InvalidateWholeChunkCache(in);
  3915. yaffs_CheckGarbageCollection(dev);
  3916. if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
  3917. return yaffs_GetFileSize(in);
  3918. }
  3919. if (newSize == oldFileSize) {
  3920. return oldFileSize;
  3921. }
  3922. if (newSize < oldFileSize) {
  3923. yaffs_PruneResizedChunks(in, newSize);
  3924. if (newSizeOfPartialChunk != 0) {
  3925. int lastChunk = 1 + newFullChunks;
  3926. __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
  3927. /* Got to read and rewrite the last chunk with its new size and zero pad */
  3928. yaffs_ReadChunkDataFromObject(in, lastChunk,
  3929. localBuffer);
  3930. memset(localBuffer + newSizeOfPartialChunk, 0,
  3931. dev->nDataBytesPerChunk - newSizeOfPartialChunk);
  3932. yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
  3933. newSizeOfPartialChunk, 1);
  3934. yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
  3935. }
  3936. in->variant.fileVariant.fileSize = newSize;
  3937. yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
  3938. } else {
  3939. /* newsSize > oldFileSize */
  3940. in->variant.fileVariant.fileSize = newSize;
  3941. }
  3942. /* Write a new object header.
  3943. * show we've shrunk the file, if need be
  3944. * Do this only if the file is not in the deleted directories.
  3945. */
  3946. if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
  3947. in->parent->objectId != YAFFS_OBJECTID_DELETED) {
  3948. yaffs_UpdateObjectHeader(in, NULL, 0,
  3949. (newSize < oldFileSize) ? 1 : 0, 0);
  3950. }
  3951. return newSize;
  3952. }
  3953. loff_t yaffs_GetFileSize(yaffs_Object * obj)
  3954. {
  3955. obj = yaffs_GetEquivalentObject(obj);
  3956. switch (obj->variantType) {
  3957. case YAFFS_OBJECT_TYPE_FILE:
  3958. return obj->variant.fileVariant.fileSize;
  3959. case YAFFS_OBJECT_TYPE_SYMLINK:
  3960. return yaffs_strlen(obj->variant.symLinkVariant.alias);
  3961. default:
  3962. return 0;
  3963. }
  3964. }
  3965. int yaffs_FlushFile(yaffs_Object * in, int updateTime)
  3966. {
  3967. int retVal;
  3968. if (in->dirty) {
  3969. yaffs_FlushFilesChunkCache(in);
  3970. if (updateTime) {
  3971. #ifdef CONFIG_YAFFS_WINCE
  3972. yfsd_WinFileTimeNow(in->win_mtime);
  3973. #else
  3974. in->yst_mtime = Y_CURRENT_TIME;
  3975. #endif
  3976. }
  3977. retVal =
  3978. (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
  3979. 0) ? YAFFS_OK : YAFFS_FAIL;
  3980. } else {
  3981. retVal = YAFFS_OK;
  3982. }
  3983. return retVal;
  3984. }
  3985. static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
  3986. {
  3987. /* First off, invalidate the file's data in the cache, without flushing. */
  3988. yaffs_InvalidateWholeChunkCache(in);
  3989. if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
  3990. /* Move to the unlinked directory so we have a record that it was deleted. */
  3991. yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
  3992. }
  3993. yaffs_RemoveObjectFromDirectory(in);
  3994. yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
  3995. in->chunkId = -1;
  3996. yaffs_FreeObject(in);
  3997. return YAFFS_OK;
  3998. }
  3999. /* yaffs_DeleteFile deletes the whole file data
  4000. * and the inode associated with the file.
  4001. * It does not delete the links associated with the file.
  4002. */
  4003. static int yaffs_UnlinkFile(yaffs_Object * in)
  4004. {
  4005. int retVal;
  4006. int immediateDeletion = 0;
  4007. if (1) {
  4008. #ifdef __KERNEL__
  4009. if (!in->myInode) {
  4010. immediateDeletion = 1;
  4011. }
  4012. #else
  4013. if (in->inUse <= 0) {
  4014. immediateDeletion = 1;
  4015. }
  4016. #endif
  4017. if (immediateDeletion) {
  4018. retVal =
  4019. yaffs_ChangeObjectName(in, in->myDev->deletedDir,
  4020. "deleted", 0, 0);
  4021. T(YAFFS_TRACE_TRACING,
  4022. (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
  4023. in->objectId));
  4024. in->deleted = 1;
  4025. in->myDev->nDeletedFiles++;
  4026. if (0 && in->myDev->isYaffs2) {
  4027. yaffs_ResizeFile(in, 0);
  4028. }
  4029. yaffs_SoftDeleteFile(in);
  4030. } else {
  4031. retVal =
  4032. yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
  4033. "unlinked", 0, 0);
  4034. }
  4035. }
  4036. return retVal;
  4037. }
  4038. int yaffs_DeleteFile(yaffs_Object * in)
  4039. {
  4040. int retVal = YAFFS_OK;
  4041. if (in->nDataChunks > 0) {
  4042. /* Use soft deletion if there is data in the file */
  4043. if (!in->unlinked) {
  4044. retVal = yaffs_UnlinkFile(in);
  4045. }
  4046. if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
  4047. in->deleted = 1;
  4048. in->myDev->nDeletedFiles++;
  4049. yaffs_SoftDeleteFile(in);
  4050. }
  4051. return in->deleted ? YAFFS_OK : YAFFS_FAIL;
  4052. } else {
  4053. /* The file has no data chunks so we toss it immediately */
  4054. yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
  4055. in->variant.fileVariant.top = NULL;
  4056. yaffs_DoGenericObjectDeletion(in);
  4057. return YAFFS_OK;
  4058. }
  4059. }
  4060. static int yaffs_DeleteDirectory(yaffs_Object * in)
  4061. {
  4062. /* First check that the directory is empty. */
  4063. if (list_empty(&in->variant.directoryVariant.children)) {
  4064. return yaffs_DoGenericObjectDeletion(in);
  4065. }
  4066. return YAFFS_FAIL;
  4067. }
  4068. static int yaffs_DeleteSymLink(yaffs_Object * in)
  4069. {
  4070. YFREE(in->variant.symLinkVariant.alias);
  4071. return yaffs_DoGenericObjectDeletion(in);
  4072. }
  4073. static int yaffs_DeleteHardLink(yaffs_Object * in)
  4074. {
  4075. /* remove this hardlink from the list assocaited with the equivalent
  4076. * object
  4077. */
  4078. list_del(&in->hardLinks);
  4079. return yaffs_DoGenericObjectDeletion(in);
  4080. }
  4081. static void yaffs_DestroyObject(yaffs_Object * obj)
  4082. {
  4083. switch (obj->variantType) {
  4084. case YAFFS_OBJECT_TYPE_FILE:
  4085. yaffs_DeleteFile(obj);
  4086. break;
  4087. case YAFFS_OBJECT_TYPE_DIRECTORY:
  4088. yaffs_DeleteDirectory(obj);
  4089. break;
  4090. case YAFFS_OBJECT_TYPE_SYMLINK:
  4091. yaffs_DeleteSymLink(obj);
  4092. break;
  4093. case YAFFS_OBJECT_TYPE_HARDLINK:
  4094. yaffs_DeleteHardLink(obj);
  4095. break;
  4096. case YAFFS_OBJECT_TYPE_SPECIAL:
  4097. yaffs_DoGenericObjectDeletion(obj);
  4098. break;
  4099. case YAFFS_OBJECT_TYPE_UNKNOWN:
  4100. break; /* should not happen. */
  4101. }
  4102. }
  4103. static int yaffs_UnlinkWorker(yaffs_Object * obj)
  4104. {
  4105. if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  4106. return yaffs_DeleteHardLink(obj);
  4107. } else if (!list_empty(&obj->hardLinks)) {
  4108. /* Curve ball: We're unlinking an object that has a hardlink.
  4109. *
  4110. * This problem arises because we are not strictly following
  4111. * The Linux link/inode model.
  4112. *
  4113. * We can't really delete the object.
  4114. * Instead, we do the following:
  4115. * - Select a hardlink.
  4116. * - Unhook it from the hard links
  4117. * - Unhook it from its parent directory (so that the rename can work)
  4118. * - Rename the object to the hardlink's name.
  4119. * - Delete the hardlink
  4120. */
  4121. yaffs_Object *hl;
  4122. int retVal;
  4123. YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  4124. hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
  4125. list_del_init(&hl->hardLinks);
  4126. list_del_init(&hl->siblings);
  4127. yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  4128. retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
  4129. if (retVal == YAFFS_OK) {
  4130. retVal = yaffs_DoGenericObjectDeletion(hl);
  4131. }
  4132. return retVal;
  4133. } else {
  4134. switch (obj->variantType) {
  4135. case YAFFS_OBJECT_TYPE_FILE:
  4136. return yaffs_UnlinkFile(obj);
  4137. break;
  4138. case YAFFS_OBJECT_TYPE_DIRECTORY:
  4139. return yaffs_DeleteDirectory(obj);
  4140. break;
  4141. case YAFFS_OBJECT_TYPE_SYMLINK:
  4142. return yaffs_DeleteSymLink(obj);
  4143. break;
  4144. case YAFFS_OBJECT_TYPE_SPECIAL:
  4145. return yaffs_DoGenericObjectDeletion(obj);
  4146. break;
  4147. case YAFFS_OBJECT_TYPE_HARDLINK:
  4148. case YAFFS_OBJECT_TYPE_UNKNOWN:
  4149. default:
  4150. return YAFFS_FAIL;
  4151. }
  4152. }
  4153. }
  4154. static int yaffs_UnlinkObject( yaffs_Object *obj)
  4155. {
  4156. if (obj && obj->unlinkAllowed) {
  4157. return yaffs_UnlinkWorker(obj);
  4158. }
  4159. return YAFFS_FAIL;
  4160. }
  4161. int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
  4162. {
  4163. yaffs_Object *obj;
  4164. obj = yaffs_FindObjectByName(dir, name);
  4165. return yaffs_UnlinkObject(obj);
  4166. }
  4167. /*----------------------- Initialisation Scanning ---------------------- */
  4168. static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
  4169. int backwardScanning)
  4170. {
  4171. yaffs_Object *obj;
  4172. if (!backwardScanning) {
  4173. /* Handle YAFFS1 forward scanning case
  4174. * For YAFFS1 we always do the deletion
  4175. */
  4176. } else {
  4177. /* Handle YAFFS2 case (backward scanning)
  4178. * If the shadowed object exists then ignore.
  4179. */
  4180. if (yaffs_FindObjectByNumber(dev, objId)) {
  4181. return;
  4182. }
  4183. }
  4184. /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  4185. * We put it in unlinked dir to be cleaned up after the scanning
  4186. */
  4187. obj =
  4188. yaffs_FindOrCreateObjectByNumber(dev, objId,
  4189. YAFFS_OBJECT_TYPE_FILE);
  4190. yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
  4191. obj->variant.fileVariant.shrinkSize = 0;
  4192. obj->valid = 1; /* So that we don't read any other info for this file */
  4193. }
  4194. typedef struct {
  4195. int seq;
  4196. int block;
  4197. } yaffs_BlockIndex;
  4198. static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
  4199. {
  4200. yaffs_Object *hl;
  4201. yaffs_Object *in;
  4202. while (hardList) {
  4203. hl = hardList;
  4204. hardList = (yaffs_Object *) (hardList->hardLinks.next);
  4205. in = yaffs_FindObjectByNumber(dev,
  4206. hl->variant.hardLinkVariant.
  4207. equivalentObjectId);
  4208. if (in) {
  4209. /* Add the hardlink pointers */
  4210. hl->variant.hardLinkVariant.equivalentObject = in;
  4211. list_add(&hl->hardLinks, &in->hardLinks);
  4212. } else {
  4213. /* Todo Need to report/handle this better.
  4214. * Got a problem... hardlink to a non-existant object
  4215. */
  4216. hl->variant.hardLinkVariant.equivalentObject = NULL;
  4217. INIT_LIST_HEAD(&hl->hardLinks);
  4218. }
  4219. }
  4220. }
  4221. static int ybicmp(const void *a, const void *b){
  4222. register int aseq = ((yaffs_BlockIndex *)a)->seq;
  4223. register int bseq = ((yaffs_BlockIndex *)b)->seq;
  4224. register int ablock = ((yaffs_BlockIndex *)a)->block;
  4225. register int bblock = ((yaffs_BlockIndex *)b)->block;
  4226. if( aseq == bseq )
  4227. return ablock - bblock;
  4228. else
  4229. return aseq - bseq;
  4230. }
  4231. static int yaffs_Scan(yaffs_Device * dev)
  4232. {
  4233. yaffs_ExtendedTags tags;
  4234. int blk;
  4235. int blockIterator;
  4236. int startIterator;
  4237. int endIterator;
  4238. int nBlocksToScan = 0;
  4239. int result;
  4240. int chunk;
  4241. int c;
  4242. int deleted;
  4243. yaffs_BlockState state;
  4244. yaffs_Object *hardList = NULL;
  4245. yaffs_BlockInfo *bi;
  4246. int sequenceNumber;
  4247. yaffs_ObjectHeader *oh;
  4248. yaffs_Object *in;
  4249. yaffs_Object *parent;
  4250. int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  4251. int alloc_failed = 0;
  4252. __u8 *chunkData;
  4253. yaffs_BlockIndex *blockIndex = NULL;
  4254. if (dev->isYaffs2) {
  4255. T(YAFFS_TRACE_SCAN,
  4256. (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
  4257. return YAFFS_FAIL;
  4258. }
  4259. //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
  4260. T(YAFFS_TRACE_SCAN,
  4261. (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
  4262. dev->internalStartBlock, dev->internalEndBlock));
  4263. chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  4264. dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  4265. if (dev->isYaffs2) {
  4266. blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
  4267. if(!blockIndex)
  4268. return YAFFS_FAIL;
  4269. }
  4270. /* Scan all the blocks to determine their state */
  4271. for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  4272. bi = yaffs_GetBlockInfo(dev, blk);
  4273. yaffs_ClearChunkBits(dev, blk);
  4274. bi->pagesInUse = 0;
  4275. bi->softDeletions = 0;
  4276. yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  4277. bi->blockState = state;
  4278. bi->sequenceNumber = sequenceNumber;
  4279. T(YAFFS_TRACE_SCAN_DEBUG,
  4280. (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  4281. state, sequenceNumber));
  4282. if (state == YAFFS_BLOCK_STATE_DEAD) {
  4283. T(YAFFS_TRACE_BAD_BLOCKS,
  4284. (TSTR("block %d is bad" TENDSTR), blk));
  4285. } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  4286. T(YAFFS_TRACE_SCAN_DEBUG,
  4287. (TSTR("Block empty " TENDSTR)));
  4288. dev->nErasedBlocks++;
  4289. dev->nFreeChunks += dev->nChunksPerBlock;
  4290. } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  4291. /* Determine the highest sequence number */
  4292. if (dev->isYaffs2 &&
  4293. sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  4294. sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  4295. blockIndex[nBlocksToScan].seq = sequenceNumber;
  4296. blockIndex[nBlocksToScan].block = blk;
  4297. nBlocksToScan++;
  4298. if (sequenceNumber >= dev->sequenceNumber) {
  4299. dev->sequenceNumber = sequenceNumber;
  4300. }
  4301. } else if (dev->isYaffs2) {
  4302. /* TODO: Nasty sequence number! */
  4303. T(YAFFS_TRACE_SCAN,
  4304. (TSTR
  4305. ("Block scanning block %d has bad sequence number %d"
  4306. TENDSTR), blk, sequenceNumber));
  4307. }
  4308. }
  4309. }
  4310. /* Sort the blocks
  4311. * Dungy old bubble sort for now...
  4312. */
  4313. if (dev->isYaffs2) {
  4314. yaffs_BlockIndex temp;
  4315. int i;
  4316. int j;
  4317. for (i = 0; i < nBlocksToScan; i++)
  4318. for (j = i + 1; j < nBlocksToScan; j++)
  4319. if (blockIndex[i].seq > blockIndex[j].seq) {
  4320. temp = blockIndex[j];
  4321. blockIndex[j] = blockIndex[i];
  4322. blockIndex[i] = temp;
  4323. }
  4324. }
  4325. /* Now scan the blocks looking at the data. */
  4326. if (dev->isYaffs2) {
  4327. startIterator = 0;
  4328. endIterator = nBlocksToScan - 1;
  4329. T(YAFFS_TRACE_SCAN_DEBUG,
  4330. (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
  4331. } else {
  4332. startIterator = dev->internalStartBlock;
  4333. endIterator = dev->internalEndBlock;
  4334. }
  4335. /* For each block.... */
  4336. for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
  4337. blockIterator++) {
  4338. if (dev->isYaffs2) {
  4339. /* get the block to scan in the correct order */
  4340. blk = blockIndex[blockIterator].block;
  4341. } else {
  4342. blk = blockIterator;
  4343. }
  4344. bi = yaffs_GetBlockInfo(dev, blk);
  4345. state = bi->blockState;
  4346. deleted = 0;
  4347. /* For each chunk in each block that needs scanning....*/
  4348. for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
  4349. state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  4350. /* Read the tags and decide what to do */
  4351. chunk = blk * dev->nChunksPerBlock + c;
  4352. result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  4353. &tags);
  4354. /* Let's have a good look at this chunk... */
  4355. if (!dev->isYaffs2 && tags.chunkDeleted) {
  4356. /* YAFFS1 only...
  4357. * A deleted chunk
  4358. */
  4359. deleted++;
  4360. dev->nFreeChunks++;
  4361. /*T((" %d %d deleted\n",blk,c)); */
  4362. } else if (!tags.chunkUsed) {
  4363. /* An unassigned chunk in the block
  4364. * This means that either the block is empty or
  4365. * this is the one being allocated from
  4366. */
  4367. if (c == 0) {
  4368. /* We're looking at the first chunk in the block so the block is unused */
  4369. state = YAFFS_BLOCK_STATE_EMPTY;
  4370. dev->nErasedBlocks++;
  4371. } else {
  4372. /* this is the block being allocated from */
  4373. T(YAFFS_TRACE_SCAN,
  4374. (TSTR
  4375. (" Allocating from %d %d" TENDSTR),
  4376. blk, c));
  4377. state = YAFFS_BLOCK_STATE_ALLOCATING;
  4378. dev->allocationBlock = blk;
  4379. dev->allocationPage = c;
  4380. dev->allocationBlockFinder = blk;
  4381. /* Set it to here to encourage the allocator to go forth from here. */
  4382. /* Yaffs2 sanity check:
  4383. * This should be the one with the highest sequence number
  4384. */
  4385. if (dev->isYaffs2
  4386. && (dev->sequenceNumber !=
  4387. bi->sequenceNumber)) {
  4388. T(YAFFS_TRACE_ALWAYS,
  4389. (TSTR
  4390. ("yaffs: Allocation block %d was not highest sequence id:"
  4391. " block seq = %d, dev seq = %d"
  4392. TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
  4393. }
  4394. }
  4395. dev->nFreeChunks += (dev->nChunksPerBlock - c);
  4396. } else if (tags.chunkId > 0) {
  4397. /* chunkId > 0 so it is a data chunk... */
  4398. unsigned int endpos;
  4399. yaffs_SetChunkBit(dev, blk, c);
  4400. bi->pagesInUse++;
  4401. in = yaffs_FindOrCreateObjectByNumber(dev,
  4402. tags.
  4403. objectId,
  4404. YAFFS_OBJECT_TYPE_FILE);
  4405. /* PutChunkIntoFile checks for a clash (two data chunks with
  4406. * the same chunkId).
  4407. */
  4408. if(!in)
  4409. alloc_failed = 1;
  4410. if(in){
  4411. if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
  4412. alloc_failed = 1;
  4413. }
  4414. endpos =
  4415. (tags.chunkId - 1) * dev->nDataBytesPerChunk +
  4416. tags.byteCount;
  4417. if (in &&
  4418. in->variantType == YAFFS_OBJECT_TYPE_FILE
  4419. && in->variant.fileVariant.scannedFileSize <
  4420. endpos) {
  4421. in->variant.fileVariant.
  4422. scannedFileSize = endpos;
  4423. if (!dev->useHeaderFileSize) {
  4424. in->variant.fileVariant.
  4425. fileSize =
  4426. in->variant.fileVariant.
  4427. scannedFileSize;
  4428. }
  4429. }
  4430. /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
  4431. } else {
  4432. /* chunkId == 0, so it is an ObjectHeader.
  4433. * Thus, we read in the object header and make the object
  4434. */
  4435. yaffs_SetChunkBit(dev, blk, c);
  4436. bi->pagesInUse++;
  4437. result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
  4438. chunkData,
  4439. NULL);
  4440. oh = (yaffs_ObjectHeader *) chunkData;
  4441. in = yaffs_FindObjectByNumber(dev,
  4442. tags.objectId);
  4443. if (in && in->variantType != oh->type) {
  4444. /* This should not happen, but somehow
  4445. * Wev'e ended up with an objectId that has been reused but not yet
  4446. * deleted, and worse still it has changed type. Delete the old object.
  4447. */
  4448. yaffs_DestroyObject(in);
  4449. in = 0;
  4450. }
  4451. in = yaffs_FindOrCreateObjectByNumber(dev,
  4452. tags.
  4453. objectId,
  4454. oh->type);
  4455. if(!in)
  4456. alloc_failed = 1;
  4457. if (in && oh->shadowsObject > 0) {
  4458. yaffs_HandleShadowedObject(dev,
  4459. oh->
  4460. shadowsObject,
  4461. 0);
  4462. }
  4463. if (in && in->valid) {
  4464. /* We have already filled this one. We have a duplicate and need to resolve it. */
  4465. unsigned existingSerial = in->serial;
  4466. unsigned newSerial = tags.serialNumber;
  4467. if (dev->isYaffs2 ||
  4468. ((existingSerial + 1) & 3) ==
  4469. newSerial) {
  4470. /* Use new one - destroy the exisiting one */
  4471. yaffs_DeleteChunk(dev,
  4472. in->chunkId,
  4473. 1, __LINE__);
  4474. in->valid = 0;
  4475. } else {
  4476. /* Use existing - destroy this one. */
  4477. yaffs_DeleteChunk(dev, chunk, 1,
  4478. __LINE__);
  4479. }
  4480. }
  4481. if (in && !in->valid &&
  4482. (tags.objectId == YAFFS_OBJECTID_ROOT ||
  4483. tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
  4484. /* We only load some info, don't fiddle with directory structure */
  4485. in->valid = 1;
  4486. in->variantType = oh->type;
  4487. in->yst_mode = oh->yst_mode;
  4488. #ifdef CONFIG_YAFFS_WINCE
  4489. in->win_atime[0] = oh->win_atime[0];
  4490. in->win_ctime[0] = oh->win_ctime[0];
  4491. in->win_mtime[0] = oh->win_mtime[0];
  4492. in->win_atime[1] = oh->win_atime[1];
  4493. in->win_ctime[1] = oh->win_ctime[1];
  4494. in->win_mtime[1] = oh->win_mtime[1];
  4495. #else
  4496. in->yst_uid = oh->yst_uid;
  4497. in->yst_gid = oh->yst_gid;
  4498. in->yst_atime = oh->yst_atime;
  4499. in->yst_mtime = oh->yst_mtime;
  4500. in->yst_ctime = oh->yst_ctime;
  4501. in->yst_rdev = oh->yst_rdev;
  4502. #endif
  4503. in->chunkId = chunk;
  4504. } else if (in && !in->valid) {
  4505. /* we need to load this info */
  4506. in->valid = 1;
  4507. in->variantType = oh->type;
  4508. in->yst_mode = oh->yst_mode;
  4509. #ifdef CONFIG_YAFFS_WINCE
  4510. in->win_atime[0] = oh->win_atime[0];
  4511. in->win_ctime[0] = oh->win_ctime[0];
  4512. in->win_mtime[0] = oh->win_mtime[0];
  4513. in->win_atime[1] = oh->win_atime[1];
  4514. in->win_ctime[1] = oh->win_ctime[1];
  4515. in->win_mtime[1] = oh->win_mtime[1];
  4516. #else
  4517. in->yst_uid = oh->yst_uid;
  4518. in->yst_gid = oh->yst_gid;
  4519. in->yst_atime = oh->yst_atime;
  4520. in->yst_mtime = oh->yst_mtime;
  4521. in->yst_ctime = oh->yst_ctime;
  4522. in->yst_rdev = oh->yst_rdev;
  4523. #endif
  4524. in->chunkId = chunk;
  4525. yaffs_SetObjectName(in, oh->name);
  4526. in->dirty = 0;
  4527. /* directory stuff...
  4528. * hook up to parent
  4529. */
  4530. parent =
  4531. yaffs_FindOrCreateObjectByNumber
  4532. (dev, oh->parentObjectId,
  4533. YAFFS_OBJECT_TYPE_DIRECTORY);
  4534. if (parent->variantType ==
  4535. YAFFS_OBJECT_TYPE_UNKNOWN) {
  4536. /* Set up as a directory */
  4537. parent->variantType =
  4538. YAFFS_OBJECT_TYPE_DIRECTORY;
  4539. INIT_LIST_HEAD(&parent->variant.
  4540. directoryVariant.
  4541. children);
  4542. } else if (parent->variantType !=
  4543. YAFFS_OBJECT_TYPE_DIRECTORY)
  4544. {
  4545. /* Hoosterman, another problem....
  4546. * We're trying to use a non-directory as a directory
  4547. */
  4548. T(YAFFS_TRACE_ERROR,
  4549. (TSTR
  4550. ("yaffs tragedy: attempting to use non-directory as"
  4551. " a directory in scan. Put in lost+found."
  4552. TENDSTR)));
  4553. parent = dev->lostNFoundDir;
  4554. }
  4555. yaffs_AddObjectToDirectory(parent, in);
  4556. if (0 && (parent == dev->deletedDir ||
  4557. parent == dev->unlinkedDir)) {
  4558. in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  4559. dev->nDeletedFiles++;
  4560. }
  4561. /* Note re hardlinks.
  4562. * Since we might scan a hardlink before its equivalent object is scanned
  4563. * we put them all in a list.
  4564. * After scanning is complete, we should have all the objects, so we run through this
  4565. * list and fix up all the chains.
  4566. */
  4567. switch (in->variantType) {
  4568. case YAFFS_OBJECT_TYPE_UNKNOWN:
  4569. /* Todo got a problem */
  4570. break;
  4571. case YAFFS_OBJECT_TYPE_FILE:
  4572. if (dev->isYaffs2
  4573. && oh->isShrink) {
  4574. /* Prune back the shrunken chunks */
  4575. yaffs_PruneResizedChunks
  4576. (in, oh->fileSize);
  4577. /* Mark the block as having a shrinkHeader */
  4578. bi->hasShrinkHeader = 1;
  4579. }
  4580. if (dev->useHeaderFileSize)
  4581. in->variant.fileVariant.
  4582. fileSize =
  4583. oh->fileSize;
  4584. break;
  4585. case YAFFS_OBJECT_TYPE_HARDLINK:
  4586. in->variant.hardLinkVariant.
  4587. equivalentObjectId =
  4588. oh->equivalentObjectId;
  4589. in->hardLinks.next =
  4590. (struct list_head *)
  4591. hardList;
  4592. hardList = in;
  4593. break;
  4594. case YAFFS_OBJECT_TYPE_DIRECTORY:
  4595. /* Do nothing */
  4596. break;
  4597. case YAFFS_OBJECT_TYPE_SPECIAL:
  4598. /* Do nothing */
  4599. break;
  4600. case YAFFS_OBJECT_TYPE_SYMLINK:
  4601. in->variant.symLinkVariant.alias =
  4602. yaffs_CloneString(oh->alias);
  4603. if(!in->variant.symLinkVariant.alias)
  4604. alloc_failed = 1;
  4605. break;
  4606. }
  4607. if (parent == dev->deletedDir) {
  4608. yaffs_DestroyObject(in);
  4609. bi->hasShrinkHeader = 1;
  4610. }
  4611. }
  4612. }
  4613. }
  4614. if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  4615. /* If we got this far while scanning, then the block is fully allocated.*/
  4616. state = YAFFS_BLOCK_STATE_FULL;
  4617. }
  4618. bi->blockState = state;
  4619. /* Now let's see if it was dirty */
  4620. if (bi->pagesInUse == 0 &&
  4621. !bi->hasShrinkHeader &&
  4622. bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  4623. yaffs_BlockBecameDirty(dev, blk);
  4624. }
  4625. }
  4626. if (blockIndex) {
  4627. YFREE(blockIndex);
  4628. }
  4629. /* Ok, we've done all the scanning.
  4630. * Fix up the hard link chains.
  4631. * We should now have scanned all the objects, now it's time to add these
  4632. * hardlinks.
  4633. */
  4634. yaffs_HardlinkFixup(dev,hardList);
  4635. /* Handle the unlinked files. Since they were left in an unlinked state we should
  4636. * just delete them.
  4637. */
  4638. {
  4639. struct list_head *i;
  4640. struct list_head *n;
  4641. yaffs_Object *l;
  4642. /* Soft delete all the unlinked files */
  4643. list_for_each_safe(i, n,
  4644. &dev->unlinkedDir->variant.directoryVariant.
  4645. children) {
  4646. if (i) {
  4647. l = list_entry(i, yaffs_Object, siblings);
  4648. yaffs_DestroyObject(l);
  4649. }
  4650. }
  4651. }
  4652. yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  4653. if(alloc_failed){
  4654. return YAFFS_FAIL;
  4655. }
  4656. T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
  4657. return YAFFS_OK;
  4658. }
  4659. static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
  4660. {
  4661. __u8 *chunkData;
  4662. yaffs_ObjectHeader *oh;
  4663. yaffs_Device *dev = in->myDev;
  4664. yaffs_ExtendedTags tags;
  4665. int result;
  4666. int alloc_failed = 0;
  4667. if(!in)
  4668. return;
  4669. #if 0
  4670. T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
  4671. in->objectId,
  4672. in->lazyLoaded ? "not yet" : "already"));
  4673. #endif
  4674. if(in->lazyLoaded){
  4675. in->lazyLoaded = 0;
  4676. chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  4677. result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
  4678. oh = (yaffs_ObjectHeader *) chunkData;
  4679. in->yst_mode = oh->yst_mode;
  4680. #ifdef CONFIG_YAFFS_WINCE
  4681. in->win_atime[0] = oh->win_atime[0];
  4682. in->win_ctime[0] = oh->win_ctime[0];
  4683. in->win_mtime[0] = oh->win_mtime[0];
  4684. in->win_atime[1] = oh->win_atime[1];
  4685. in->win_ctime[1] = oh->win_ctime[1];
  4686. in->win_mtime[1] = oh->win_mtime[1];
  4687. #else
  4688. in->yst_uid = oh->yst_uid;
  4689. in->yst_gid = oh->yst_gid;
  4690. in->yst_atime = oh->yst_atime;
  4691. in->yst_mtime = oh->yst_mtime;
  4692. in->yst_ctime = oh->yst_ctime;
  4693. in->yst_rdev = oh->yst_rdev;
  4694. #endif
  4695. yaffs_SetObjectName(in, oh->name);
  4696. if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
  4697. in->variant.symLinkVariant.alias =
  4698. yaffs_CloneString(oh->alias);
  4699. if(!in->variant.symLinkVariant.alias)
  4700. alloc_failed = 1; /* Not returned to caller */
  4701. }
  4702. yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
  4703. }
  4704. }
  4705. static int yaffs_ScanBackwards(yaffs_Device * dev)
  4706. {
  4707. yaffs_ExtendedTags tags;
  4708. int blk;
  4709. int blockIterator;
  4710. int startIterator;
  4711. int endIterator;
  4712. int nBlocksToScan = 0;
  4713. int chunk;
  4714. int result;
  4715. int c;
  4716. int deleted;
  4717. yaffs_BlockState state;
  4718. yaffs_Object *hardList = NULL;
  4719. yaffs_BlockInfo *bi;
  4720. int sequenceNumber;
  4721. yaffs_ObjectHeader *oh;
  4722. yaffs_Object *in;
  4723. yaffs_Object *parent;
  4724. int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  4725. int itsUnlinked;
  4726. __u8 *chunkData;
  4727. int fileSize;
  4728. int isShrink;
  4729. int foundChunksInBlock;
  4730. int equivalentObjectId;
  4731. int alloc_failed = 0;
  4732. yaffs_BlockIndex *blockIndex = NULL;
  4733. int altBlockIndex = 0;
  4734. if (!dev->isYaffs2) {
  4735. T(YAFFS_TRACE_SCAN,
  4736. (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
  4737. return YAFFS_FAIL;
  4738. }
  4739. T(YAFFS_TRACE_SCAN,
  4740. (TSTR
  4741. ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
  4742. TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
  4743. dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  4744. blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
  4745. if(!blockIndex) {
  4746. blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
  4747. altBlockIndex = 1;
  4748. }
  4749. if(!blockIndex) {
  4750. T(YAFFS_TRACE_SCAN,
  4751. (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
  4752. return YAFFS_FAIL;
  4753. }
  4754. dev->blocksInCheckpoint = 0;
  4755. chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  4756. /* Scan all the blocks to determine their state */
  4757. for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  4758. bi = yaffs_GetBlockInfo(dev, blk);
  4759. yaffs_ClearChunkBits(dev, blk);
  4760. bi->pagesInUse = 0;
  4761. bi->softDeletions = 0;
  4762. yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  4763. bi->blockState = state;
  4764. bi->sequenceNumber = sequenceNumber;
  4765. if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  4766. bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
  4767. T(YAFFS_TRACE_SCAN_DEBUG,
  4768. (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  4769. state, sequenceNumber));
  4770. if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
  4771. dev->blocksInCheckpoint++;
  4772. } else if (state == YAFFS_BLOCK_STATE_DEAD) {
  4773. T(YAFFS_TRACE_BAD_BLOCKS,
  4774. (TSTR("block %d is bad" TENDSTR), blk));
  4775. } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  4776. T(YAFFS_TRACE_SCAN_DEBUG,
  4777. (TSTR("Block empty " TENDSTR)));
  4778. dev->nErasedBlocks++;
  4779. dev->nFreeChunks += dev->nChunksPerBlock;
  4780. } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  4781. /* Determine the highest sequence number */
  4782. if (dev->isYaffs2 &&
  4783. sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  4784. sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  4785. blockIndex[nBlocksToScan].seq = sequenceNumber;
  4786. blockIndex[nBlocksToScan].block = blk;
  4787. nBlocksToScan++;
  4788. if (sequenceNumber >= dev->sequenceNumber) {
  4789. dev->sequenceNumber = sequenceNumber;
  4790. }
  4791. } else if (dev->isYaffs2) {
  4792. /* TODO: Nasty sequence number! */
  4793. T(YAFFS_TRACE_SCAN,
  4794. (TSTR
  4795. ("Block scanning block %d has bad sequence number %d"
  4796. TENDSTR), blk, sequenceNumber));
  4797. }
  4798. }
  4799. }
  4800. T(YAFFS_TRACE_SCAN,
  4801. (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
  4802. YYIELD();
  4803. /* Sort the blocks */
  4804. #ifndef CONFIG_YAFFS_USE_OWN_SORT
  4805. {
  4806. /* Use qsort now. */
  4807. qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
  4808. }
  4809. #else
  4810. {
  4811. /* Dungy old bubble sort... */
  4812. yaffs_BlockIndex temp;
  4813. int i;
  4814. int j;
  4815. for (i = 0; i < nBlocksToScan; i++)
  4816. for (j = i + 1; j < nBlocksToScan; j++)
  4817. if (blockIndex[i].seq > blockIndex[j].seq) {
  4818. temp = blockIndex[j];
  4819. blockIndex[j] = blockIndex[i];
  4820. blockIndex[i] = temp;
  4821. }
  4822. }
  4823. #endif
  4824. YYIELD();
  4825. T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
  4826. /* Now scan the blocks looking at the data. */
  4827. startIterator = 0;
  4828. endIterator = nBlocksToScan - 1;
  4829. T(YAFFS_TRACE_SCAN_DEBUG,
  4830. (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
  4831. /* For each block.... backwards */
  4832. for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
  4833. blockIterator--) {
  4834. /* Cooperative multitasking! This loop can run for so
  4835. long that watchdog timers expire. */
  4836. YYIELD();
  4837. /* get the block to scan in the correct order */
  4838. blk = blockIndex[blockIterator].block;
  4839. bi = yaffs_GetBlockInfo(dev, blk);
  4840. state = bi->blockState;
  4841. deleted = 0;
  4842. /* For each chunk in each block that needs scanning.... */
  4843. foundChunksInBlock = 0;
  4844. for (c = dev->nChunksPerBlock - 1;
  4845. !alloc_failed && c >= 0 &&
  4846. (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  4847. state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
  4848. /* Scan backwards...
  4849. * Read the tags and decide what to do
  4850. */
  4851. chunk = blk * dev->nChunksPerBlock + c;
  4852. result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  4853. &tags);
  4854. /* Let's have a good look at this chunk... */
  4855. if (!tags.chunkUsed) {
  4856. /* An unassigned chunk in the block.
  4857. * If there are used chunks after this one, then
  4858. * it is a chunk that was skipped due to failing the erased
  4859. * check. Just skip it so that it can be deleted.
  4860. * But, more typically, We get here when this is an unallocated
  4861. * chunk and his means that either the block is empty or
  4862. * this is the one being allocated from
  4863. */
  4864. if(foundChunksInBlock)
  4865. {
  4866. /* This is a chunk that was skipped due to failing the erased check */
  4867. } else if (c == 0) {
  4868. /* We're looking at the first chunk in the block so the block is unused */
  4869. state = YAFFS_BLOCK_STATE_EMPTY;
  4870. dev->nErasedBlocks++;
  4871. } else {
  4872. if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  4873. state == YAFFS_BLOCK_STATE_ALLOCATING) {
  4874. if(dev->sequenceNumber == bi->sequenceNumber) {
  4875. /* this is the block being allocated from */
  4876. T(YAFFS_TRACE_SCAN,
  4877. (TSTR
  4878. (" Allocating from %d %d"
  4879. TENDSTR), blk, c));
  4880. state = YAFFS_BLOCK_STATE_ALLOCATING;
  4881. dev->allocationBlock = blk;
  4882. dev->allocationPage = c;
  4883. dev->allocationBlockFinder = blk;
  4884. }
  4885. else {
  4886. /* This is a partially written block that is not
  4887. * the current allocation block. This block must have
  4888. * had a write failure, so set up for retirement.
  4889. */
  4890. bi->needsRetiring = 1;
  4891. bi->gcPrioritise = 1;
  4892. T(YAFFS_TRACE_ALWAYS,
  4893. (TSTR("Partially written block %d being set for retirement" TENDSTR),
  4894. blk));
  4895. }
  4896. }
  4897. }
  4898. dev->nFreeChunks++;
  4899. } else if (tags.chunkId > 0) {
  4900. /* chunkId > 0 so it is a data chunk... */
  4901. unsigned int endpos;
  4902. __u32 chunkBase =
  4903. (tags.chunkId - 1) * dev->nDataBytesPerChunk;
  4904. foundChunksInBlock = 1;
  4905. yaffs_SetChunkBit(dev, blk, c);
  4906. bi->pagesInUse++;
  4907. in = yaffs_FindOrCreateObjectByNumber(dev,
  4908. tags.
  4909. objectId,
  4910. YAFFS_OBJECT_TYPE_FILE);
  4911. if(!in){
  4912. /* Out of memory */
  4913. alloc_failed = 1;
  4914. }
  4915. if (in &&
  4916. in->variantType == YAFFS_OBJECT_TYPE_FILE
  4917. && chunkBase <
  4918. in->variant.fileVariant.shrinkSize) {
  4919. /* This has not been invalidated by a resize */
  4920. if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
  4921. chunk, -1)){
  4922. alloc_failed = 1;
  4923. }
  4924. /* File size is calculated by looking at the data chunks if we have not
  4925. * seen an object header yet. Stop this practice once we find an object header.
  4926. */
  4927. endpos =
  4928. (tags.chunkId -
  4929. 1) * dev->nDataBytesPerChunk +
  4930. tags.byteCount;
  4931. if (!in->valid && /* have not got an object header yet */
  4932. in->variant.fileVariant.
  4933. scannedFileSize < endpos) {
  4934. in->variant.fileVariant.
  4935. scannedFileSize = endpos;
  4936. in->variant.fileVariant.
  4937. fileSize =
  4938. in->variant.fileVariant.
  4939. scannedFileSize;
  4940. }
  4941. } else if(in) {
  4942. /* This chunk has been invalidated by a resize, so delete */
  4943. yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  4944. }
  4945. } else {
  4946. /* chunkId == 0, so it is an ObjectHeader.
  4947. * Thus, we read in the object header and make the object
  4948. */
  4949. foundChunksInBlock = 1;
  4950. yaffs_SetChunkBit(dev, blk, c);
  4951. bi->pagesInUse++;
  4952. oh = NULL;
  4953. in = NULL;
  4954. if (tags.extraHeaderInfoAvailable) {
  4955. in = yaffs_FindOrCreateObjectByNumber
  4956. (dev, tags.objectId,
  4957. tags.extraObjectType);
  4958. }
  4959. if (!in ||
  4960. #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  4961. !in->valid ||
  4962. #endif
  4963. tags.extraShadows ||
  4964. (!in->valid &&
  4965. (tags.objectId == YAFFS_OBJECTID_ROOT ||
  4966. tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
  4967. ) {
  4968. /* If we don't have valid info then we need to read the chunk
  4969. * TODO In future we can probably defer reading the chunk and
  4970. * living with invalid data until needed.
  4971. */
  4972. result = yaffs_ReadChunkWithTagsFromNAND(dev,
  4973. chunk,
  4974. chunkData,
  4975. NULL);
  4976. oh = (yaffs_ObjectHeader *) chunkData;
  4977. if (!in)
  4978. in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
  4979. }
  4980. if (!in) {
  4981. /* TODO Hoosterman we have a problem! */
  4982. T(YAFFS_TRACE_ERROR,
  4983. (TSTR
  4984. ("yaffs tragedy: Could not make object for object %d "
  4985. "at chunk %d during scan"
  4986. TENDSTR), tags.objectId, chunk));
  4987. }
  4988. if (in->valid) {
  4989. /* We have already filled this one.
  4990. * We have a duplicate that will be discarded, but
  4991. * we first have to suck out resize info if it is a file.
  4992. */
  4993. if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
  4994. ((oh &&
  4995. oh-> type == YAFFS_OBJECT_TYPE_FILE)||
  4996. (tags.extraHeaderInfoAvailable &&
  4997. tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
  4998. ) {
  4999. __u32 thisSize =
  5000. (oh) ? oh->fileSize : tags.
  5001. extraFileLength;
  5002. __u32 parentObjectId =
  5003. (oh) ? oh->
  5004. parentObjectId : tags.
  5005. extraParentObjectId;
  5006. unsigned isShrink =
  5007. (oh) ? oh->isShrink : tags.
  5008. extraIsShrinkHeader;
  5009. /* If it is deleted (unlinked at start also means deleted)
  5010. * we treat the file size as being zeroed at this point.
  5011. */
  5012. if (parentObjectId ==
  5013. YAFFS_OBJECTID_DELETED
  5014. || parentObjectId ==
  5015. YAFFS_OBJECTID_UNLINKED) {
  5016. thisSize = 0;
  5017. isShrink = 1;
  5018. }
  5019. if (isShrink &&
  5020. in->variant.fileVariant.
  5021. shrinkSize > thisSize) {
  5022. in->variant.fileVariant.
  5023. shrinkSize =
  5024. thisSize;
  5025. }
  5026. if (isShrink) {
  5027. bi->hasShrinkHeader = 1;
  5028. }
  5029. }
  5030. /* Use existing - destroy this one. */
  5031. yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  5032. }
  5033. if (!in->valid &&
  5034. (tags.objectId == YAFFS_OBJECTID_ROOT ||
  5035. tags.objectId ==
  5036. YAFFS_OBJECTID_LOSTNFOUND)) {
  5037. /* We only load some info, don't fiddle with directory structure */
  5038. in->valid = 1;
  5039. if(oh) {
  5040. in->variantType = oh->type;
  5041. in->yst_mode = oh->yst_mode;
  5042. #ifdef CONFIG_YAFFS_WINCE
  5043. in->win_atime[0] = oh->win_atime[0];
  5044. in->win_ctime[0] = oh->win_ctime[0];
  5045. in->win_mtime[0] = oh->win_mtime[0];
  5046. in->win_atime[1] = oh->win_atime[1];
  5047. in->win_ctime[1] = oh->win_ctime[1];
  5048. in->win_mtime[1] = oh->win_mtime[1];
  5049. #else
  5050. in->yst_uid = oh->yst_uid;
  5051. in->yst_gid = oh->yst_gid;
  5052. in->yst_atime = oh->yst_atime;
  5053. in->yst_mtime = oh->yst_mtime;
  5054. in->yst_ctime = oh->yst_ctime;
  5055. in->yst_rdev = oh->yst_rdev;
  5056. #endif
  5057. } else {
  5058. in->variantType = tags.extraObjectType;
  5059. in->lazyLoaded = 1;
  5060. }
  5061. in->chunkId = chunk;
  5062. } else if (!in->valid) {
  5063. /* we need to load this info */
  5064. in->valid = 1;
  5065. in->chunkId = chunk;
  5066. if(oh) {
  5067. in->variantType = oh->type;
  5068. in->yst_mode = oh->yst_mode;
  5069. #ifdef CONFIG_YAFFS_WINCE
  5070. in->win_atime[0] = oh->win_atime[0];
  5071. in->win_ctime[0] = oh->win_ctime[0];
  5072. in->win_mtime[0] = oh->win_mtime[0];
  5073. in->win_atime[1] = oh->win_atime[1];
  5074. in->win_ctime[1] = oh->win_ctime[1];
  5075. in->win_mtime[1] = oh->win_mtime[1];
  5076. #else
  5077. in->yst_uid = oh->yst_uid;
  5078. in->yst_gid = oh->yst_gid;
  5079. in->yst_atime = oh->yst_atime;
  5080. in->yst_mtime = oh->yst_mtime;
  5081. in->yst_ctime = oh->yst_ctime;
  5082. in->yst_rdev = oh->yst_rdev;
  5083. #endif
  5084. if (oh->shadowsObject > 0)
  5085. yaffs_HandleShadowedObject(dev,
  5086. oh->
  5087. shadowsObject,
  5088. 1);
  5089. yaffs_SetObjectName(in, oh->name);
  5090. parent =
  5091. yaffs_FindOrCreateObjectByNumber
  5092. (dev, oh->parentObjectId,
  5093. YAFFS_OBJECT_TYPE_DIRECTORY);
  5094. fileSize = oh->fileSize;
  5095. isShrink = oh->isShrink;
  5096. equivalentObjectId = oh->equivalentObjectId;
  5097. }
  5098. else {
  5099. in->variantType = tags.extraObjectType;
  5100. parent =
  5101. yaffs_FindOrCreateObjectByNumber
  5102. (dev, tags.extraParentObjectId,
  5103. YAFFS_OBJECT_TYPE_DIRECTORY);
  5104. fileSize = tags.extraFileLength;
  5105. isShrink = tags.extraIsShrinkHeader;
  5106. equivalentObjectId = tags.extraEquivalentObjectId;
  5107. in->lazyLoaded = 1;
  5108. }
  5109. in->dirty = 0;
  5110. /* directory stuff...
  5111. * hook up to parent
  5112. */
  5113. if (parent->variantType ==
  5114. YAFFS_OBJECT_TYPE_UNKNOWN) {
  5115. /* Set up as a directory */
  5116. parent->variantType =
  5117. YAFFS_OBJECT_TYPE_DIRECTORY;
  5118. INIT_LIST_HEAD(&parent->variant.
  5119. directoryVariant.
  5120. children);
  5121. } else if (parent->variantType !=
  5122. YAFFS_OBJECT_TYPE_DIRECTORY)
  5123. {
  5124. /* Hoosterman, another problem....
  5125. * We're trying to use a non-directory as a directory
  5126. */
  5127. T(YAFFS_TRACE_ERROR,
  5128. (TSTR
  5129. ("yaffs tragedy: attempting to use non-directory as"
  5130. " a directory in scan. Put in lost+found."
  5131. TENDSTR)));
  5132. parent = dev->lostNFoundDir;
  5133. }
  5134. yaffs_AddObjectToDirectory(parent, in);
  5135. itsUnlinked = (parent == dev->deletedDir) ||
  5136. (parent == dev->unlinkedDir);
  5137. if (isShrink) {
  5138. /* Mark the block as having a shrinkHeader */
  5139. bi->hasShrinkHeader = 1;
  5140. }
  5141. /* Note re hardlinks.
  5142. * Since we might scan a hardlink before its equivalent object is scanned
  5143. * we put them all in a list.
  5144. * After scanning is complete, we should have all the objects, so we run
  5145. * through this list and fix up all the chains.
  5146. */
  5147. switch (in->variantType) {
  5148. case YAFFS_OBJECT_TYPE_UNKNOWN:
  5149. /* Todo got a problem */
  5150. break;
  5151. case YAFFS_OBJECT_TYPE_FILE:
  5152. if (in->variant.fileVariant.
  5153. scannedFileSize < fileSize) {
  5154. /* This covers the case where the file size is greater
  5155. * than where the data is
  5156. * This will happen if the file is resized to be larger
  5157. * than its current data extents.
  5158. */
  5159. in->variant.fileVariant.fileSize = fileSize;
  5160. in->variant.fileVariant.scannedFileSize =
  5161. in->variant.fileVariant.fileSize;
  5162. }
  5163. if (isShrink &&
  5164. in->variant.fileVariant.shrinkSize > fileSize) {
  5165. in->variant.fileVariant.shrinkSize = fileSize;
  5166. }
  5167. break;
  5168. case YAFFS_OBJECT_TYPE_HARDLINK:
  5169. if(!itsUnlinked) {
  5170. in->variant.hardLinkVariant.equivalentObjectId =
  5171. equivalentObjectId;
  5172. in->hardLinks.next =
  5173. (struct list_head *) hardList;
  5174. hardList = in;
  5175. }
  5176. break;
  5177. case YAFFS_OBJECT_TYPE_DIRECTORY:
  5178. /* Do nothing */
  5179. break;
  5180. case YAFFS_OBJECT_TYPE_SPECIAL:
  5181. /* Do nothing */
  5182. break;
  5183. case YAFFS_OBJECT_TYPE_SYMLINK:
  5184. if(oh){
  5185. in->variant.symLinkVariant.alias =
  5186. yaffs_CloneString(oh->
  5187. alias);
  5188. if(!in->variant.symLinkVariant.alias)
  5189. alloc_failed = 1;
  5190. }
  5191. break;
  5192. }
  5193. }
  5194. }
  5195. } /* End of scanning for each chunk */
  5196. if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  5197. /* If we got this far while scanning, then the block is fully allocated. */
  5198. state = YAFFS_BLOCK_STATE_FULL;
  5199. }
  5200. bi->blockState = state;
  5201. /* Now let's see if it was dirty */
  5202. if (bi->pagesInUse == 0 &&
  5203. !bi->hasShrinkHeader &&
  5204. bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  5205. yaffs_BlockBecameDirty(dev, blk);
  5206. }
  5207. }
  5208. if (altBlockIndex)
  5209. YFREE_ALT(blockIndex);
  5210. else
  5211. YFREE(blockIndex);
  5212. /* Ok, we've done all the scanning.
  5213. * Fix up the hard link chains.
  5214. * We should now have scanned all the objects, now it's time to add these
  5215. * hardlinks.
  5216. */
  5217. yaffs_HardlinkFixup(dev,hardList);
  5218. /*
  5219. * Sort out state of unlinked and deleted objects.
  5220. */
  5221. {
  5222. struct list_head *i;
  5223. struct list_head *n;
  5224. yaffs_Object *l;
  5225. /* Soft delete all the unlinked files */
  5226. list_for_each_safe(i, n,
  5227. &dev->unlinkedDir->variant.directoryVariant.
  5228. children) {
  5229. if (i) {
  5230. l = list_entry(i, yaffs_Object, siblings);
  5231. yaffs_DestroyObject(l);
  5232. }
  5233. }
  5234. /* Soft delete all the deletedDir files */
  5235. list_for_each_safe(i, n,
  5236. &dev->deletedDir->variant.directoryVariant.
  5237. children) {
  5238. if (i) {
  5239. l = list_entry(i, yaffs_Object, siblings);
  5240. yaffs_DestroyObject(l);
  5241. }
  5242. }
  5243. }
  5244. yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  5245. if(alloc_failed){
  5246. return YAFFS_FAIL;
  5247. }
  5248. T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
  5249. return YAFFS_OK;
  5250. }
  5251. /*------------------------------ Directory Functions ----------------------------- */
  5252. static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
  5253. {
  5254. yaffs_Device *dev = obj->myDev;
  5255. if(dev && dev->removeObjectCallback)
  5256. dev->removeObjectCallback(obj);
  5257. list_del_init(&obj->siblings);
  5258. obj->parent = NULL;
  5259. }
  5260. static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
  5261. yaffs_Object * obj)
  5262. {
  5263. if (!directory) {
  5264. T(YAFFS_TRACE_ALWAYS,
  5265. (TSTR
  5266. ("tragedy: Trying to add an object to a null pointer directory"
  5267. TENDSTR)));
  5268. YBUG();
  5269. }
  5270. if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  5271. T(YAFFS_TRACE_ALWAYS,
  5272. (TSTR
  5273. ("tragedy: Trying to add an object to a non-directory"
  5274. TENDSTR)));
  5275. YBUG();
  5276. }
  5277. if (obj->siblings.prev == NULL) {
  5278. /* Not initialised */
  5279. INIT_LIST_HEAD(&obj->siblings);
  5280. } else if (!list_empty(&obj->siblings)) {
  5281. /* If it is holed up somewhere else, un hook it */
  5282. yaffs_RemoveObjectFromDirectory(obj);
  5283. }
  5284. /* Now add it */
  5285. list_add(&obj->siblings, &directory->variant.directoryVariant.children);
  5286. obj->parent = directory;
  5287. if (directory == obj->myDev->unlinkedDir
  5288. || directory == obj->myDev->deletedDir) {
  5289. obj->unlinked = 1;
  5290. obj->myDev->nUnlinkedFiles++;
  5291. obj->renameAllowed = 0;
  5292. }
  5293. }
  5294. yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
  5295. const YCHAR * name)
  5296. {
  5297. int sum;
  5298. struct list_head *i;
  5299. YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  5300. yaffs_Object *l;
  5301. if (!name) {
  5302. return NULL;
  5303. }
  5304. if (!directory) {
  5305. T(YAFFS_TRACE_ALWAYS,
  5306. (TSTR
  5307. ("tragedy: yaffs_FindObjectByName: null pointer directory"
  5308. TENDSTR)));
  5309. YBUG();
  5310. }
  5311. if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  5312. T(YAFFS_TRACE_ALWAYS,
  5313. (TSTR
  5314. ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  5315. YBUG();
  5316. }
  5317. sum = yaffs_CalcNameSum(name);
  5318. list_for_each(i, &directory->variant.directoryVariant.children) {
  5319. if (i) {
  5320. l = list_entry(i, yaffs_Object, siblings);
  5321. yaffs_CheckObjectDetailsLoaded(l);
  5322. /* Special case for lost-n-found */
  5323. if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  5324. if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
  5325. return l;
  5326. }
  5327. } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
  5328. {
  5329. /* LostnFound cunk called Objxxx
  5330. * Do a real check
  5331. */
  5332. yaffs_GetObjectName(l, buffer,
  5333. YAFFS_MAX_NAME_LENGTH);
  5334. if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
  5335. return l;
  5336. }
  5337. }
  5338. }
  5339. }
  5340. return NULL;
  5341. }
  5342. #if 0
  5343. int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
  5344. int (*fn) (yaffs_Object *))
  5345. {
  5346. struct list_head *i;
  5347. yaffs_Object *l;
  5348. if (!theDir) {
  5349. T(YAFFS_TRACE_ALWAYS,
  5350. (TSTR
  5351. ("tragedy: yaffs_FindObjectByName: null pointer directory"
  5352. TENDSTR)));
  5353. YBUG();
  5354. }
  5355. if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  5356. T(YAFFS_TRACE_ALWAYS,
  5357. (TSTR
  5358. ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  5359. YBUG();
  5360. }
  5361. list_for_each(i, &theDir->variant.directoryVariant.children) {
  5362. if (i) {
  5363. l = list_entry(i, yaffs_Object, siblings);
  5364. if (l && !fn(l)) {
  5365. return YAFFS_FAIL;
  5366. }
  5367. }
  5368. }
  5369. return YAFFS_OK;
  5370. }
  5371. #endif
  5372. /* GetEquivalentObject dereferences any hard links to get to the
  5373. * actual object.
  5374. */
  5375. yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
  5376. {
  5377. if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  5378. /* We want the object id of the equivalent object, not this one */
  5379. obj = obj->variant.hardLinkVariant.equivalentObject;
  5380. yaffs_CheckObjectDetailsLoaded(obj);
  5381. }
  5382. return obj;
  5383. }
  5384. int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
  5385. {
  5386. memset(name, 0, buffSize * sizeof(YCHAR));
  5387. yaffs_CheckObjectDetailsLoaded(obj);
  5388. if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  5389. yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
  5390. } else if (obj->chunkId <= 0) {
  5391. YCHAR locName[20];
  5392. /* make up a name */
  5393. yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
  5394. obj->objectId);
  5395. yaffs_strncpy(name, locName, buffSize - 1);
  5396. }
  5397. #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  5398. else if (obj->shortName[0]) {
  5399. yaffs_strcpy(name, obj->shortName);
  5400. }
  5401. #endif
  5402. else {
  5403. int result;
  5404. __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
  5405. yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
  5406. memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
  5407. if (obj->chunkId >= 0) {
  5408. result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
  5409. obj->chunkId, buffer,
  5410. NULL);
  5411. }
  5412. yaffs_strncpy(name, oh->name, buffSize - 1);
  5413. yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
  5414. }
  5415. return yaffs_strlen(name);
  5416. }
  5417. int yaffs_GetObjectFileLength(yaffs_Object * obj)
  5418. {
  5419. /* Dereference any hard linking */
  5420. obj = yaffs_GetEquivalentObject(obj);
  5421. if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  5422. return obj->variant.fileVariant.fileSize;
  5423. }
  5424. if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
  5425. return yaffs_strlen(obj->variant.symLinkVariant.alias);
  5426. } else {
  5427. /* Only a directory should drop through to here */
  5428. return obj->myDev->nDataBytesPerChunk;
  5429. }
  5430. }
  5431. int yaffs_GetObjectLinkCount(yaffs_Object * obj)
  5432. {
  5433. int count = 0;
  5434. struct list_head *i;
  5435. if (!obj->unlinked) {
  5436. count++; /* the object itself */
  5437. }
  5438. list_for_each(i, &obj->hardLinks) {
  5439. count++; /* add the hard links; */
  5440. }
  5441. return count;
  5442. }
  5443. int yaffs_GetObjectInode(yaffs_Object * obj)
  5444. {
  5445. obj = yaffs_GetEquivalentObject(obj);
  5446. return obj->objectId;
  5447. }
  5448. unsigned yaffs_GetObjectType(yaffs_Object * obj)
  5449. {
  5450. obj = yaffs_GetEquivalentObject(obj);
  5451. switch (obj->variantType) {
  5452. case YAFFS_OBJECT_TYPE_FILE:
  5453. return DT_REG;
  5454. break;
  5455. case YAFFS_OBJECT_TYPE_DIRECTORY:
  5456. return DT_DIR;
  5457. break;
  5458. case YAFFS_OBJECT_TYPE_SYMLINK:
  5459. return DT_LNK;
  5460. break;
  5461. case YAFFS_OBJECT_TYPE_HARDLINK:
  5462. return DT_REG;
  5463. break;
  5464. case YAFFS_OBJECT_TYPE_SPECIAL:
  5465. if (S_ISFIFO(obj->yst_mode))
  5466. return DT_FIFO;
  5467. if (S_ISCHR(obj->yst_mode))
  5468. return DT_CHR;
  5469. if (S_ISBLK(obj->yst_mode))
  5470. return DT_BLK;
  5471. if (S_ISSOCK(obj->yst_mode))
  5472. return DT_SOCK;
  5473. default:
  5474. return DT_REG;
  5475. break;
  5476. }
  5477. }
  5478. YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
  5479. {
  5480. obj = yaffs_GetEquivalentObject(obj);
  5481. if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
  5482. return yaffs_CloneString(obj->variant.symLinkVariant.alias);
  5483. } else {
  5484. return yaffs_CloneString(_Y(""));
  5485. }
  5486. }
  5487. #ifndef CONFIG_YAFFS_WINCE
  5488. int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
  5489. {
  5490. unsigned int valid = attr->ia_valid;
  5491. if (valid & ATTR_MODE)
  5492. obj->yst_mode = attr->ia_mode;
  5493. if (valid & ATTR_UID)
  5494. obj->yst_uid = attr->ia_uid;
  5495. if (valid & ATTR_GID)
  5496. obj->yst_gid = attr->ia_gid;
  5497. if (valid & ATTR_ATIME)
  5498. obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  5499. if (valid & ATTR_CTIME)
  5500. obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  5501. if (valid & ATTR_MTIME)
  5502. obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  5503. if (valid & ATTR_SIZE)
  5504. yaffs_ResizeFile(obj, attr->ia_size);
  5505. yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
  5506. return YAFFS_OK;
  5507. }
  5508. int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
  5509. {
  5510. unsigned int valid = 0;
  5511. attr->ia_mode = obj->yst_mode;
  5512. valid |= ATTR_MODE;
  5513. attr->ia_uid = obj->yst_uid;
  5514. valid |= ATTR_UID;
  5515. attr->ia_gid = obj->yst_gid;
  5516. valid |= ATTR_GID;
  5517. Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  5518. valid |= ATTR_ATIME;
  5519. Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  5520. valid |= ATTR_CTIME;
  5521. Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  5522. valid |= ATTR_MTIME;
  5523. attr->ia_size = yaffs_GetFileSize(obj);
  5524. valid |= ATTR_SIZE;
  5525. attr->ia_valid = valid;
  5526. return YAFFS_OK;
  5527. }
  5528. #endif
  5529. #if 0
  5530. int yaffs_DumpObject(yaffs_Object * obj)
  5531. {
  5532. YCHAR name[257];
  5533. yaffs_GetObjectName(obj, name, 256);
  5534. T(YAFFS_TRACE_ALWAYS,
  5535. (TSTR
  5536. ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
  5537. " chunk %d type %d size %d\n"
  5538. TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
  5539. obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
  5540. yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
  5541. return YAFFS_OK;
  5542. }
  5543. #endif
  5544. /*---------------------------- Initialisation code -------------------------------------- */
  5545. static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
  5546. {
  5547. /* Common functions, gotta have */
  5548. if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
  5549. return 0;
  5550. #ifdef CONFIG_YAFFS_YAFFS2
  5551. /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  5552. if (dev->writeChunkWithTagsToNAND &&
  5553. dev->readChunkWithTagsFromNAND &&
  5554. !dev->writeChunkToNAND &&
  5555. !dev->readChunkFromNAND &&
  5556. dev->markNANDBlockBad && dev->queryNANDBlock)
  5557. return 1;
  5558. #endif
  5559. /* Can use the "spare" style interface for yaffs1 */
  5560. if (!dev->isYaffs2 &&
  5561. !dev->writeChunkWithTagsToNAND &&
  5562. !dev->readChunkWithTagsFromNAND &&
  5563. dev->writeChunkToNAND &&
  5564. dev->readChunkFromNAND &&
  5565. !dev->markNANDBlockBad && !dev->queryNANDBlock)
  5566. return 1;
  5567. return 0; /* bad */
  5568. }
  5569. static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
  5570. {
  5571. /* Initialise the unlinked, deleted, root and lost and found directories */
  5572. dev->lostNFoundDir = dev->rootDir = NULL;
  5573. dev->unlinkedDir = dev->deletedDir = NULL;
  5574. dev->unlinkedDir =
  5575. yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  5576. dev->deletedDir =
  5577. yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  5578. dev->rootDir =
  5579. yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
  5580. YAFFS_ROOT_MODE | S_IFDIR);
  5581. dev->lostNFoundDir =
  5582. yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
  5583. YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  5584. if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
  5585. yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
  5586. return YAFFS_OK;
  5587. }
  5588. return YAFFS_FAIL;
  5589. }
  5590. int yaffs_GutsInitialise(yaffs_Device * dev)
  5591. {
  5592. int init_failed = 0;
  5593. unsigned x;
  5594. int bits;
  5595. T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
  5596. /* Check stuff that must be set */
  5597. if (!dev) {
  5598. T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
  5599. return YAFFS_FAIL;
  5600. }
  5601. dev->internalStartBlock = dev->startBlock;
  5602. dev->internalEndBlock = dev->endBlock;
  5603. dev->blockOffset = 0;
  5604. dev->chunkOffset = 0;
  5605. dev->nFreeChunks = 0;
  5606. if (dev->startBlock == 0) {
  5607. dev->internalStartBlock = dev->startBlock + 1;
  5608. dev->internalEndBlock = dev->endBlock + 1;
  5609. dev->blockOffset = 1;
  5610. dev->chunkOffset = dev->nChunksPerBlock;
  5611. }
  5612. /* Check geometry parameters. */
  5613. if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
  5614. (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
  5615. dev->nChunksPerBlock < 2 ||
  5616. dev->nReservedBlocks < 2 ||
  5617. dev->internalStartBlock <= 0 ||
  5618. dev->internalEndBlock <= 0 ||
  5619. dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
  5620. ) {
  5621. T(YAFFS_TRACE_ALWAYS,
  5622. (TSTR
  5623. ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
  5624. TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
  5625. return YAFFS_FAIL;
  5626. }
  5627. if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
  5628. T(YAFFS_TRACE_ALWAYS,
  5629. (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
  5630. return YAFFS_FAIL;
  5631. }
  5632. /* Got the right mix of functions? */
  5633. if (!yaffs_CheckDevFunctions(dev)) {
  5634. /* Function missing */
  5635. T(YAFFS_TRACE_ALWAYS,
  5636. (TSTR
  5637. ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
  5638. return YAFFS_FAIL;
  5639. }
  5640. /* This is really a compilation check. */
  5641. if (!yaffs_CheckStructures()) {
  5642. T(YAFFS_TRACE_ALWAYS,
  5643. (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
  5644. return YAFFS_FAIL;
  5645. }
  5646. if (dev->isMounted) {
  5647. T(YAFFS_TRACE_ALWAYS,
  5648. (TSTR("yaffs: device already mounted\n" TENDSTR)));
  5649. return YAFFS_FAIL;
  5650. }
  5651. /* Finished with most checks. One or two more checks happen later on too. */
  5652. dev->isMounted = 1;
  5653. /* OK now calculate a few things for the device */
  5654. /*
  5655. * Calculate all the chunk size manipulation numbers:
  5656. */
  5657. /* Start off assuming it is a power of 2 */
  5658. dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
  5659. dev->chunkMask = (1<<dev->chunkShift) - 1;
  5660. if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
  5661. /* Yes it is a power of 2, disable crumbs */
  5662. dev->crumbMask = 0;
  5663. dev->crumbShift = 0;
  5664. dev->crumbsPerChunk = 0;
  5665. } else {
  5666. /* Not a power of 2, use crumbs instead */
  5667. dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
  5668. dev->crumbMask = (1<<dev->crumbShift)-1;
  5669. dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
  5670. dev->chunkShift = 0;
  5671. dev->chunkMask = 0;
  5672. }
  5673. /*
  5674. * Calculate chunkGroupBits.
  5675. * We need to find the next power of 2 > than internalEndBlock
  5676. */
  5677. x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
  5678. bits = ShiftsGE(x);
  5679. /* Set up tnode width if wide tnodes are enabled. */
  5680. if(!dev->wideTnodesDisabled){
  5681. /* bits must be even so that we end up with 32-bit words */
  5682. if(bits & 1)
  5683. bits++;
  5684. if(bits < 16)
  5685. dev->tnodeWidth = 16;
  5686. else
  5687. dev->tnodeWidth = bits;
  5688. }
  5689. else
  5690. dev->tnodeWidth = 16;
  5691. dev->tnodeMask = (1<<dev->tnodeWidth)-1;
  5692. /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  5693. * so if the bitwidth of the
  5694. * chunk range we're using is greater than 16 we need
  5695. * to figure out chunk shift and chunkGroupSize
  5696. */
  5697. if (bits <= dev->tnodeWidth)
  5698. dev->chunkGroupBits = 0;
  5699. else
  5700. dev->chunkGroupBits = bits - dev->tnodeWidth;
  5701. dev->chunkGroupSize = 1 << dev->chunkGroupBits;
  5702. if (dev->nChunksPerBlock < dev->chunkGroupSize) {
  5703. /* We have a problem because the soft delete won't work if
  5704. * the chunk group size > chunks per block.
  5705. * This can be remedied by using larger "virtual blocks".
  5706. */
  5707. T(YAFFS_TRACE_ALWAYS,
  5708. (TSTR("yaffs: chunk group too large\n" TENDSTR)));
  5709. return YAFFS_FAIL;
  5710. }
  5711. /* OK, we've finished verifying the device, lets continue with initialisation */
  5712. /* More device initialisation */
  5713. dev->garbageCollections = 0;
  5714. dev->passiveGarbageCollections = 0;
  5715. dev->currentDirtyChecker = 0;
  5716. dev->bufferedBlock = -1;
  5717. dev->doingBufferedBlockRewrite = 0;
  5718. dev->nDeletedFiles = 0;
  5719. dev->nBackgroundDeletions = 0;
  5720. dev->nUnlinkedFiles = 0;
  5721. dev->eccFixed = 0;
  5722. dev->eccUnfixed = 0;
  5723. dev->tagsEccFixed = 0;
  5724. dev->tagsEccUnfixed = 0;
  5725. dev->nErasureFailures = 0;
  5726. dev->nErasedBlocks = 0;
  5727. dev->isDoingGC = 0;
  5728. dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
  5729. /* Initialise temporary buffers and caches. */
  5730. if(!yaffs_InitialiseTempBuffers(dev))
  5731. init_failed = 1;
  5732. dev->srCache = NULL;
  5733. dev->gcCleanupList = NULL;
  5734. if (!init_failed &&
  5735. dev->nShortOpCaches > 0) {
  5736. int i;
  5737. __u8 *buf;
  5738. int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
  5739. if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
  5740. dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
  5741. }
  5742. buf = dev->srCache = YMALLOC(srCacheBytes);
  5743. if(dev->srCache)
  5744. memset(dev->srCache,0,srCacheBytes);
  5745. for (i = 0; i < dev->nShortOpCaches && buf; i++) {
  5746. dev->srCache[i].object = NULL;
  5747. dev->srCache[i].lastUse = 0;
  5748. dev->srCache[i].dirty = 0;
  5749. dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
  5750. }
  5751. if(!buf)
  5752. init_failed = 1;
  5753. dev->srLastUse = 0;
  5754. }
  5755. dev->cacheHits = 0;
  5756. if(!init_failed){
  5757. dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
  5758. if(!dev->gcCleanupList)
  5759. init_failed = 1;
  5760. }
  5761. if (dev->isYaffs2) {
  5762. dev->useHeaderFileSize = 1;
  5763. }
  5764. if(!init_failed && !yaffs_InitialiseBlocks(dev))
  5765. init_failed = 1;
  5766. yaffs_InitialiseTnodes(dev);
  5767. yaffs_InitialiseObjects(dev);
  5768. if(!init_failed && !yaffs_CreateInitialDirectories(dev))
  5769. init_failed = 1;
  5770. if(!init_failed){
  5771. /* Now scan the flash. */
  5772. if (dev->isYaffs2) {
  5773. if(yaffs_CheckpointRestore(dev)) {
  5774. T(YAFFS_TRACE_ALWAYS,
  5775. (TSTR("yaffs: restored from checkpoint" TENDSTR)));
  5776. } else {
  5777. /* Clean up the mess caused by an aborted checkpoint load
  5778. * and scan backwards.
  5779. */
  5780. yaffs_DeinitialiseBlocks(dev);
  5781. yaffs_DeinitialiseTnodes(dev);
  5782. yaffs_DeinitialiseObjects(dev);
  5783. dev->nErasedBlocks = 0;
  5784. dev->nFreeChunks = 0;
  5785. dev->allocationBlock = -1;
  5786. dev->allocationPage = -1;
  5787. dev->nDeletedFiles = 0;
  5788. dev->nUnlinkedFiles = 0;
  5789. dev->nBackgroundDeletions = 0;
  5790. dev->oldestDirtySequence = 0;
  5791. if(!init_failed && !yaffs_InitialiseBlocks(dev))
  5792. init_failed = 1;
  5793. yaffs_InitialiseTnodes(dev);
  5794. yaffs_InitialiseObjects(dev);
  5795. if(!init_failed && !yaffs_CreateInitialDirectories(dev))
  5796. init_failed = 1;
  5797. if(!init_failed && !yaffs_ScanBackwards(dev))
  5798. init_failed = 1;
  5799. }
  5800. }else
  5801. if(!yaffs_Scan(dev))
  5802. init_failed = 1;
  5803. }
  5804. if(init_failed){
  5805. /* Clean up the mess */
  5806. T(YAFFS_TRACE_TRACING,
  5807. (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
  5808. yaffs_Deinitialise(dev);
  5809. return YAFFS_FAIL;
  5810. }
  5811. /* Zero out stats */
  5812. dev->nPageReads = 0;
  5813. dev->nPageWrites = 0;
  5814. dev->nBlockErasures = 0;
  5815. dev->nGCCopies = 0;
  5816. dev->nRetriedWrites = 0;
  5817. dev->nRetiredBlocks = 0;
  5818. yaffs_VerifyFreeChunks(dev);
  5819. yaffs_VerifyBlocks(dev);
  5820. T(YAFFS_TRACE_TRACING,
  5821. (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
  5822. return YAFFS_OK;
  5823. }
  5824. void yaffs_Deinitialise(yaffs_Device * dev)
  5825. {
  5826. if (dev->isMounted) {
  5827. int i;
  5828. yaffs_DeinitialiseBlocks(dev);
  5829. yaffs_DeinitialiseTnodes(dev);
  5830. yaffs_DeinitialiseObjects(dev);
  5831. if (dev->nShortOpCaches > 0 &&
  5832. dev->srCache) {
  5833. for (i = 0; i < dev->nShortOpCaches; i++) {
  5834. if(dev->srCache[i].data)
  5835. YFREE(dev->srCache[i].data);
  5836. dev->srCache[i].data = NULL;
  5837. }
  5838. YFREE(dev->srCache);
  5839. dev->srCache = NULL;
  5840. }
  5841. YFREE(dev->gcCleanupList);
  5842. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  5843. YFREE(dev->tempBuffer[i].buffer);
  5844. }
  5845. dev->isMounted = 0;
  5846. }
  5847. }
  5848. static int yaffs_CountFreeChunks(yaffs_Device * dev)
  5849. {
  5850. int nFree;
  5851. int b;
  5852. yaffs_BlockInfo *blk;
  5853. for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
  5854. b++) {
  5855. blk = yaffs_GetBlockInfo(dev, b);
  5856. switch (blk->blockState) {
  5857. case YAFFS_BLOCK_STATE_EMPTY:
  5858. case YAFFS_BLOCK_STATE_ALLOCATING:
  5859. case YAFFS_BLOCK_STATE_COLLECTING:
  5860. case YAFFS_BLOCK_STATE_FULL:
  5861. nFree +=
  5862. (dev->nChunksPerBlock - blk->pagesInUse +
  5863. blk->softDeletions);
  5864. break;
  5865. default:
  5866. break;
  5867. }
  5868. }
  5869. return nFree;
  5870. }
  5871. int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
  5872. {
  5873. /* This is what we report to the outside world */
  5874. int nFree;
  5875. int nDirtyCacheChunks;
  5876. int blocksForCheckpoint;
  5877. #if 1
  5878. nFree = dev->nFreeChunks;
  5879. #else
  5880. nFree = yaffs_CountFreeChunks(dev);
  5881. #endif
  5882. nFree += dev->nDeletedFiles;
  5883. /* Now count the number of dirty chunks in the cache and subtract those */
  5884. {
  5885. int i;
  5886. for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
  5887. if (dev->srCache[i].dirty)
  5888. nDirtyCacheChunks++;
  5889. }
  5890. }
  5891. nFree -= nDirtyCacheChunks;
  5892. nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
  5893. /* Now we figure out how much to reserve for the checkpoint and report that... */
  5894. blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
  5895. if(blocksForCheckpoint < 0)
  5896. blocksForCheckpoint = 0;
  5897. nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
  5898. if (nFree < 0)
  5899. nFree = 0;
  5900. return nFree;
  5901. }
  5902. static int yaffs_freeVerificationFailures;
  5903. static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
  5904. {
  5905. int counted;
  5906. int difference;
  5907. if(yaffs_SkipVerification(dev))
  5908. return;
  5909. counted = yaffs_CountFreeChunks(dev);
  5910. difference = dev->nFreeChunks - counted;
  5911. if (difference) {
  5912. T(YAFFS_TRACE_ALWAYS,
  5913. (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
  5914. dev->nFreeChunks, counted, difference));
  5915. yaffs_freeVerificationFailures++;
  5916. }
  5917. }
  5918. /*---------------------------------------- YAFFS test code ----------------------*/
  5919. #define yaffs_CheckStruct(structure,syze, name) \
  5920. if(sizeof(structure) != syze) \
  5921. { \
  5922. T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
  5923. name,syze,sizeof(structure))); \
  5924. return YAFFS_FAIL; \
  5925. }
  5926. static int yaffs_CheckStructures(void)
  5927. {
  5928. /* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
  5929. /* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
  5930. /* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
  5931. #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5932. yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
  5933. #endif
  5934. yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
  5935. return YAFFS_OK;
  5936. }