yaffs_guts.c 178 KB

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