yaffs_guts.c 178 KB

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