yaffs_guts.c 178 KB

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