yaffs_guts.c 175 KB

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