yaffs_guts.c 175 KB

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