yaffs2.patch 396 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068
  1. diff -Nur linux-2.6.34.orig/fs/Kconfig linux-2.6.34/fs/Kconfig
  2. --- linux-2.6.34.orig/fs/Kconfig 2010-05-16 23:17:36.000000000 +0200
  3. +++ linux-2.6.34/fs/Kconfig 2010-05-20 20:16:22.261614078 +0200
  4. @@ -174,6 +174,10 @@
  5. source "fs/befs/Kconfig"
  6. source "fs/bfs/Kconfig"
  7. source "fs/efs/Kconfig"
  8. +
  9. +# Patched by YAFFS
  10. +source "fs/yaffs2/Kconfig"
  11. +
  12. source "fs/jffs2/Kconfig"
  13. # UBIFS File system configuration
  14. source "fs/ubifs/Kconfig"
  15. diff -Nur linux-2.6.34.orig/fs/Makefile linux-2.6.34/fs/Makefile
  16. --- linux-2.6.34.orig/fs/Makefile 2010-05-16 23:17:36.000000000 +0200
  17. +++ linux-2.6.34/fs/Makefile 2010-05-20 20:16:56.351613526 +0200
  18. @@ -124,5 +124,6 @@
  19. obj-$(CONFIG_OCFS2_FS) += ocfs2/
  20. obj-$(CONFIG_BTRFS_FS) += btrfs/
  21. obj-$(CONFIG_GFS2_FS) += gfs2/
  22. +obj-$(CONFIG_YAFFS_FS) += yaffs2/
  23. obj-$(CONFIG_EXOFS_FS) += exofs/
  24. obj-$(CONFIG_CEPH_FS) += ceph/
  25. diff -Nur linux-2.6.34.orig/fs/Makefile.pre.yaffs linux-2.6.34/fs/Makefile.pre.yaffs
  26. --- linux-2.6.34.orig/fs/Makefile.pre.yaffs 1970-01-01 01:00:00.000000000 +0100
  27. +++ linux-2.6.34/fs/Makefile.pre.yaffs 2010-05-20 20:16:22.271612558 +0200
  28. @@ -0,0 +1,126 @@
  29. +#
  30. +# Makefile for the Linux filesystems.
  31. +#
  32. +# 14 Sep 2000, Christoph Hellwig <hch@infradead.org>
  33. +# Rewritten to use lists instead of if-statements.
  34. +#
  35. +
  36. +obj-y := open.o read_write.o file_table.o super.o \
  37. + char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \
  38. + ioctl.o readdir.o select.o fifo.o dcache.o inode.o \
  39. + attr.o bad_inode.o file.o filesystems.o namespace.o \
  40. + seq_file.o xattr.o libfs.o fs-writeback.o \
  41. + pnode.o drop_caches.o splice.o sync.o utimes.o \
  42. + stack.o
  43. +
  44. +ifeq ($(CONFIG_BLOCK),y)
  45. +obj-y += buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
  46. +else
  47. +obj-y += no-block.o
  48. +endif
  49. +
  50. +obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o
  51. +obj-y += notify/
  52. +obj-$(CONFIG_EPOLL) += eventpoll.o
  53. +obj-$(CONFIG_ANON_INODES) += anon_inodes.o
  54. +obj-$(CONFIG_SIGNALFD) += signalfd.o
  55. +obj-$(CONFIG_TIMERFD) += timerfd.o
  56. +obj-$(CONFIG_EVENTFD) += eventfd.o
  57. +obj-$(CONFIG_AIO) += aio.o
  58. +obj-$(CONFIG_FILE_LOCKING) += locks.o
  59. +obj-$(CONFIG_COMPAT) += compat.o compat_ioctl.o
  60. +
  61. +nfsd-$(CONFIG_NFSD) := nfsctl.o
  62. +obj-y += $(nfsd-y) $(nfsd-m)
  63. +
  64. +obj-$(CONFIG_BINFMT_AOUT) += binfmt_aout.o
  65. +obj-$(CONFIG_BINFMT_EM86) += binfmt_em86.o
  66. +obj-$(CONFIG_BINFMT_MISC) += binfmt_misc.o
  67. +
  68. +# binfmt_script is always there
  69. +obj-y += binfmt_script.o
  70. +
  71. +obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o
  72. +obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o
  73. +obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o
  74. +obj-$(CONFIG_BINFMT_SOM) += binfmt_som.o
  75. +obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o
  76. +
  77. +obj-$(CONFIG_FS_MBCACHE) += mbcache.o
  78. +obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.o xattr_acl.o
  79. +obj-$(CONFIG_NFS_COMMON) += nfs_common/
  80. +obj-$(CONFIG_GENERIC_ACL) += generic_acl.o
  81. +
  82. +obj-$(CONFIG_QUOTA) += dquot.o
  83. +obj-$(CONFIG_QFMT_V1) += quota_v1.o
  84. +obj-$(CONFIG_QFMT_V2) += quota_v2.o
  85. +obj-$(CONFIG_QUOTA_TREE) += quota_tree.o
  86. +obj-$(CONFIG_QUOTACTL) += quota.o
  87. +
  88. +obj-$(CONFIG_PROC_FS) += proc/
  89. +obj-y += partitions/
  90. +obj-$(CONFIG_SYSFS) += sysfs/
  91. +obj-$(CONFIG_CONFIGFS_FS) += configfs/
  92. +obj-y += devpts/
  93. +
  94. +obj-$(CONFIG_PROFILING) += dcookies.o
  95. +obj-$(CONFIG_DLM) += dlm/
  96. +
  97. +# Do not add any filesystems before this line
  98. +obj-$(CONFIG_REISERFS_FS) += reiserfs/
  99. +obj-$(CONFIG_EXT3_FS) += ext3/ # Before ext2 so root fs can be ext3
  100. +obj-$(CONFIG_EXT2_FS) += ext2/
  101. +# We place ext4 after ext2 so plain ext2 root fs's are mounted using ext2
  102. +# unless explicitly requested by rootfstype
  103. +obj-$(CONFIG_EXT4_FS) += ext4/
  104. +obj-$(CONFIG_JBD) += jbd/
  105. +obj-$(CONFIG_JBD2) += jbd2/
  106. +obj-$(CONFIG_CRAMFS) += cramfs/
  107. +obj-$(CONFIG_SQUASHFS) += squashfs/
  108. +obj-y += ramfs/
  109. +obj-$(CONFIG_HUGETLBFS) += hugetlbfs/
  110. +obj-$(CONFIG_CODA_FS) += coda/
  111. +obj-$(CONFIG_MINIX_FS) += minix/
  112. +obj-$(CONFIG_FAT_FS) += fat/
  113. +obj-$(CONFIG_BFS_FS) += bfs/
  114. +obj-$(CONFIG_ISO9660_FS) += isofs/
  115. +obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
  116. +obj-$(CONFIG_HFS_FS) += hfs/
  117. +obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
  118. +obj-$(CONFIG_VXFS_FS) += freevxfs/
  119. +obj-$(CONFIG_NFS_FS) += nfs/
  120. +obj-$(CONFIG_EXPORTFS) += exportfs/
  121. +obj-$(CONFIG_NFSD) += nfsd/
  122. +obj-$(CONFIG_LOCKD) += lockd/
  123. +obj-$(CONFIG_NLS) += nls/
  124. +obj-$(CONFIG_SYSV_FS) += sysv/
  125. +obj-$(CONFIG_SMB_FS) += smbfs/
  126. +obj-$(CONFIG_CIFS) += cifs/
  127. +obj-$(CONFIG_NCP_FS) += ncpfs/
  128. +obj-$(CONFIG_HPFS_FS) += hpfs/
  129. +obj-$(CONFIG_NTFS_FS) += ntfs/
  130. +obj-$(CONFIG_UFS_FS) += ufs/
  131. +obj-$(CONFIG_EFS_FS) += efs/
  132. +obj-$(CONFIG_JFFS2_FS) += jffs2/
  133. +obj-$(CONFIG_UBIFS_FS) += ubifs/
  134. +obj-$(CONFIG_AFFS_FS) += affs/
  135. +obj-$(CONFIG_ROMFS_FS) += romfs/
  136. +obj-$(CONFIG_QNX4FS_FS) += qnx4/
  137. +obj-$(CONFIG_AUTOFS_FS) += autofs/
  138. +obj-$(CONFIG_AUTOFS4_FS) += autofs4/
  139. +obj-$(CONFIG_ADFS_FS) += adfs/
  140. +obj-$(CONFIG_FUSE_FS) += fuse/
  141. +obj-$(CONFIG_UDF_FS) += udf/
  142. +obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
  143. +obj-$(CONFIG_OMFS_FS) += omfs/
  144. +obj-$(CONFIG_JFS_FS) += jfs/
  145. +obj-$(CONFIG_XFS_FS) += xfs/
  146. +obj-$(CONFIG_9P_FS) += 9p/
  147. +obj-$(CONFIG_AFS_FS) += afs/
  148. +obj-$(CONFIG_BEFS_FS) += befs/
  149. +obj-$(CONFIG_HOSTFS) += hostfs/
  150. +obj-$(CONFIG_HPPFS) += hppfs/
  151. +obj-$(CONFIG_DEBUG_FS) += debugfs/
  152. +obj-$(CONFIG_OCFS2_FS) += ocfs2/
  153. +obj-$(CONFIG_BTRFS_FS) += btrfs/
  154. +obj-$(CONFIG_GFS2_FS) += gfs2/
  155. diff -Nur linux-2.6.34.orig/fs/yaffs2/devextras.h linux-2.6.34/fs/yaffs2/devextras.h
  156. --- linux-2.6.34.orig/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
  157. +++ linux-2.6.34/fs/yaffs2/devextras.h 2010-05-20 20:16:22.271612558 +0200
  158. @@ -0,0 +1,196 @@
  159. +/*
  160. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  161. + *
  162. + * Copyright (C) 2002-2007 Aleph One Ltd.
  163. + * for Toby Churchill Ltd and Brightstar Engineering
  164. + *
  165. + * Created by Charles Manning <charles@aleph1.co.uk>
  166. + *
  167. + * This program is free software; you can redistribute it and/or modify
  168. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  169. + * published by the Free Software Foundation.
  170. + *
  171. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  172. + */
  173. +
  174. +/*
  175. + * This file is just holds extra declarations of macros that would normally
  176. + * be providesd in the Linux kernel. These macros have been written from
  177. + * scratch but are functionally equivalent to the Linux ones.
  178. + *
  179. + */
  180. +
  181. +#ifndef __EXTRAS_H__
  182. +#define __EXTRAS_H__
  183. +
  184. +
  185. +#if !(defined __KERNEL__)
  186. +
  187. +/* Definition of types */
  188. +typedef unsigned char __u8;
  189. +typedef unsigned short __u16;
  190. +typedef unsigned __u32;
  191. +
  192. +#endif
  193. +
  194. +/*
  195. + * This is a simple doubly linked list implementation that matches the
  196. + * way the Linux kernel doubly linked list implementation works.
  197. + */
  198. +
  199. +struct ylist_head {
  200. + struct ylist_head *next; /* next in chain */
  201. + struct ylist_head *prev; /* previous in chain */
  202. +};
  203. +
  204. +
  205. +/* Initialise a static list */
  206. +#define YLIST_HEAD(name) \
  207. +struct ylist_head name = { &(name), &(name)}
  208. +
  209. +
  210. +
  211. +/* Initialise a list head to an empty list */
  212. +#define YINIT_LIST_HEAD(p) \
  213. +do { \
  214. + (p)->next = (p);\
  215. + (p)->prev = (p); \
  216. +} while (0)
  217. +
  218. +
  219. +/* Add an element to a list */
  220. +static __inline__ void ylist_add(struct ylist_head *newEntry,
  221. + struct ylist_head *list)
  222. +{
  223. + struct ylist_head *listNext = list->next;
  224. +
  225. + list->next = newEntry;
  226. + newEntry->prev = list;
  227. + newEntry->next = listNext;
  228. + listNext->prev = newEntry;
  229. +
  230. +}
  231. +
  232. +static __inline__ void ylist_add_tail(struct ylist_head *newEntry,
  233. + struct ylist_head *list)
  234. +{
  235. + struct ylist_head *listPrev = list->prev;
  236. +
  237. + list->prev = newEntry;
  238. + newEntry->next = list;
  239. + newEntry->prev = listPrev;
  240. + listPrev->next = newEntry;
  241. +
  242. +}
  243. +
  244. +
  245. +/* Take an element out of its current list, with or without
  246. + * reinitialising the links.of the entry*/
  247. +static __inline__ void ylist_del(struct ylist_head *entry)
  248. +{
  249. + struct ylist_head *listNext = entry->next;
  250. + struct ylist_head *listPrev = entry->prev;
  251. +
  252. + listNext->prev = listPrev;
  253. + listPrev->next = listNext;
  254. +
  255. +}
  256. +
  257. +static __inline__ void ylist_del_init(struct ylist_head *entry)
  258. +{
  259. + ylist_del(entry);
  260. + entry->next = entry->prev = entry;
  261. +}
  262. +
  263. +
  264. +/* Test if the list is empty */
  265. +static __inline__ int ylist_empty(struct ylist_head *entry)
  266. +{
  267. + return (entry->next == entry);
  268. +}
  269. +
  270. +
  271. +/* ylist_entry takes a pointer to a list entry and offsets it to that
  272. + * we can find a pointer to the object it is embedded in.
  273. + */
  274. +
  275. +
  276. +#define ylist_entry(entry, type, member) \
  277. + ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
  278. +
  279. +
  280. +/* ylist_for_each and list_for_each_safe iterate over lists.
  281. + * ylist_for_each_safe uses temporary storage to make the list delete safe
  282. + */
  283. +
  284. +#define ylist_for_each(itervar, list) \
  285. + for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
  286. +
  287. +#define ylist_for_each_safe(itervar, saveVar, list) \
  288. + for (itervar = (list)->next, saveVar = (list)->next->next; \
  289. + itervar != (list); itervar = saveVar, saveVar = saveVar->next)
  290. +
  291. +
  292. +#if !(defined __KERNEL__)
  293. +
  294. +
  295. +#ifndef WIN32
  296. +#include <sys/stat.h>
  297. +#endif
  298. +
  299. +
  300. +#ifdef CONFIG_YAFFS_PROVIDE_DEFS
  301. +/* File types */
  302. +
  303. +
  304. +#define DT_UNKNOWN 0
  305. +#define DT_FIFO 1
  306. +#define DT_CHR 2
  307. +#define DT_DIR 4
  308. +#define DT_BLK 6
  309. +#define DT_REG 8
  310. +#define DT_LNK 10
  311. +#define DT_SOCK 12
  312. +#define DT_WHT 14
  313. +
  314. +
  315. +#ifndef WIN32
  316. +#include <sys/stat.h>
  317. +#endif
  318. +
  319. +/*
  320. + * Attribute flags. These should be or-ed together to figure out what
  321. + * has been changed!
  322. + */
  323. +#define ATTR_MODE 1
  324. +#define ATTR_UID 2
  325. +#define ATTR_GID 4
  326. +#define ATTR_SIZE 8
  327. +#define ATTR_ATIME 16
  328. +#define ATTR_MTIME 32
  329. +#define ATTR_CTIME 64
  330. +
  331. +struct iattr {
  332. + unsigned int ia_valid;
  333. + unsigned ia_mode;
  334. + unsigned ia_uid;
  335. + unsigned ia_gid;
  336. + unsigned ia_size;
  337. + unsigned ia_atime;
  338. + unsigned ia_mtime;
  339. + unsigned ia_ctime;
  340. + unsigned int ia_attr_flags;
  341. +};
  342. +
  343. +#endif
  344. +
  345. +#else
  346. +
  347. +#include <linux/types.h>
  348. +#include <linux/fs.h>
  349. +#include <linux/stat.h>
  350. +
  351. +#endif
  352. +
  353. +
  354. +#endif
  355. diff -Nur linux-2.6.34.orig/fs/yaffs2/Kconfig linux-2.6.34/fs/yaffs2/Kconfig
  356. --- linux-2.6.34.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  357. +++ linux-2.6.34/fs/yaffs2/Kconfig 2010-05-20 20:16:22.271612558 +0200
  358. @@ -0,0 +1,156 @@
  359. +#
  360. +# YAFFS file system configurations
  361. +#
  362. +
  363. +config YAFFS_FS
  364. + tristate "YAFFS2 file system support"
  365. + default n
  366. + depends on MTD_BLOCK
  367. + select YAFFS_YAFFS1
  368. + select YAFFS_YAFFS2
  369. + help
  370. + YAFFS2, or Yet Another Flash Filing System, is a filing system
  371. + optimised for NAND Flash chips.
  372. +
  373. + To compile the YAFFS2 file system support as a module, choose M
  374. + here: the module will be called yaffs2.
  375. +
  376. + If unsure, say N.
  377. +
  378. + Further information on YAFFS2 is available at
  379. + <http://www.aleph1.co.uk/yaffs/>.
  380. +
  381. +config YAFFS_YAFFS1
  382. + bool "512 byte / page devices"
  383. + depends on YAFFS_FS
  384. + default y
  385. + help
  386. + Enable YAFFS1 support -- yaffs for 512 byte / page devices
  387. +
  388. + Not needed for 2K-page devices.
  389. +
  390. + If unsure, say Y.
  391. +
  392. +config YAFFS_9BYTE_TAGS
  393. + bool "Use older-style on-NAND data format with pageStatus byte"
  394. + depends on YAFFS_YAFFS1
  395. + default n
  396. + help
  397. +
  398. + Older-style on-NAND data format has a "pageStatus" byte to record
  399. + chunk/page state. This byte is zero when the page is discarded.
  400. + Choose this option if you have existing on-NAND data using this
  401. + format that you need to continue to support. New data written
  402. + also uses the older-style format. Note: Use of this option
  403. + generally requires that MTD's oob layout be adjusted to use the
  404. + older-style format. See notes on tags formats and MTD versions
  405. + in yaffs_mtdif1.c.
  406. +
  407. + If unsure, say N.
  408. +
  409. +config YAFFS_DOES_ECC
  410. + bool "Lets Yaffs do its own ECC"
  411. + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
  412. + default n
  413. + help
  414. + This enables Yaffs to use its own ECC functions instead of using
  415. + the ones from the generic MTD-NAND driver.
  416. +
  417. + If unsure, say N.
  418. +
  419. +config YAFFS_ECC_WRONG_ORDER
  420. + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
  421. + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
  422. + default n
  423. + help
  424. + This makes yaffs_ecc.c use the same ecc byte order as Steven
  425. + Hill's nand_ecc.c. If not set, then you get the same ecc byte
  426. + order as SmartMedia.
  427. +
  428. + If unsure, say N.
  429. +
  430. +config YAFFS_YAFFS2
  431. + bool "2048 byte (or larger) / page devices"
  432. + depends on YAFFS_FS
  433. + default y
  434. + help
  435. + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
  436. +
  437. + If unsure, say Y.
  438. +
  439. +config YAFFS_AUTO_YAFFS2
  440. + bool "Autoselect yaffs2 format"
  441. + depends on YAFFS_YAFFS2
  442. + default y
  443. + help
  444. + Without this, you need to explicitely use yaffs2 as the file
  445. + system type. With this, you can say "yaffs" and yaffs or yaffs2
  446. + will be used depending on the device page size (yaffs on
  447. + 512-byte page devices, yaffs2 on 2K page devices).
  448. +
  449. + If unsure, say Y.
  450. +
  451. +config YAFFS_DISABLE_LAZY_LOAD
  452. + bool "Disable lazy loading"
  453. + depends on YAFFS_YAFFS2
  454. + default n
  455. + help
  456. + "Lazy loading" defers loading file details until they are
  457. + required. This saves mount time, but makes the first look-up
  458. + a bit longer.
  459. +
  460. + Lazy loading will only happen if enabled by this option being 'n'
  461. + and if the appropriate tags are available, else yaffs2 will
  462. + automatically fall back to immediate loading and do the right
  463. + thing.
  464. +
  465. + Lazy laoding will be required by checkpointing.
  466. +
  467. + Setting this to 'y' will disable lazy loading.
  468. +
  469. + If unsure, say N.
  470. +
  471. +
  472. +config YAFFS_DISABLE_WIDE_TNODES
  473. + bool "Turn off wide tnodes"
  474. + depends on YAFFS_FS
  475. + default n
  476. + help
  477. + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
  478. + page devices and >=128MB for 2k page devices. They use slightly
  479. + more RAM but are faster since they eliminate chunk group
  480. + searching.
  481. +
  482. + Setting this to 'y' will force tnode width to 16 bits and save
  483. + memory but make large arrays slower.
  484. +
  485. + If unsure, say N.
  486. +
  487. +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  488. + bool "Force chunk erase check"
  489. + depends on YAFFS_FS
  490. + default n
  491. + help
  492. + Normally YAFFS only checks chunks before writing until an erased
  493. + chunk is found. This helps to detect any partially written
  494. + chunks that might have happened due to power loss.
  495. +
  496. + Enabling this forces on the test that chunks are erased in flash
  497. + before writing to them. This takes more time but is potentially
  498. + a bit more secure.
  499. +
  500. + Suggest setting Y during development and ironing out driver
  501. + issues etc. Suggest setting to N if you want faster writing.
  502. +
  503. + If unsure, say Y.
  504. +
  505. +config YAFFS_SHORT_NAMES_IN_RAM
  506. + bool "Cache short names in RAM"
  507. + depends on YAFFS_FS
  508. + default y
  509. + help
  510. + If this config is set, then short names are stored with the
  511. + yaffs_Object. This costs an extra 16 bytes of RAM per object,
  512. + but makes look-ups faster.
  513. +
  514. + If unsure, say Y.
  515. diff -Nur linux-2.6.34.orig/fs/yaffs2/Makefile linux-2.6.34/fs/yaffs2/Makefile
  516. --- linux-2.6.34.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  517. +++ linux-2.6.34/fs/yaffs2/Makefile 2010-05-20 20:16:22.271612558 +0200
  518. @@ -0,0 +1,10 @@
  519. +#
  520. +# Makefile for the linux YAFFS filesystem routines.
  521. +#
  522. +
  523. +obj-$(CONFIG_YAFFS_FS) += yaffs.o
  524. +
  525. +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
  526. +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
  527. +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
  528. +yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
  529. diff -Nur linux-2.6.34.orig/fs/yaffs2/moduleconfig.h linux-2.6.34/fs/yaffs2/moduleconfig.h
  530. --- linux-2.6.34.orig/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
  531. +++ linux-2.6.34/fs/yaffs2/moduleconfig.h 2010-05-20 20:16:22.281614392 +0200
  532. @@ -0,0 +1,65 @@
  533. +/*
  534. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  535. + *
  536. + * Copyright (C) 2002-2007 Aleph One Ltd.
  537. + * for Toby Churchill Ltd and Brightstar Engineering
  538. + *
  539. + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
  540. + *
  541. + * This program is free software; you can redistribute it and/or modify
  542. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  543. + * published by the Free Software Foundation.
  544. + *
  545. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  546. + */
  547. +
  548. +#ifndef __YAFFS_CONFIG_H__
  549. +#define __YAFFS_CONFIG_H__
  550. +
  551. +#ifdef YAFFS_OUT_OF_TREE
  552. +
  553. +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
  554. +#define CONFIG_YAFFS_FS
  555. +#define CONFIG_YAFFS_YAFFS1
  556. +#define CONFIG_YAFFS_YAFFS2
  557. +
  558. +/* These options are independent of each other. Select those that matter. */
  559. +
  560. +/* Default: Not selected */
  561. +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
  562. +/* #define CONFIG_YAFFS_DOES_ECC */
  563. +
  564. +/* Default: Not selected */
  565. +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
  566. +/* CONFIG_YAFFS_DOES_ECC is set */
  567. +/* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
  568. +
  569. +/* Default: Selected */
  570. +/* Meaning: Disables testing whether chunks are erased before writing to them*/
  571. +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
  572. +
  573. +/* Default: Selected */
  574. +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
  575. +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  576. +
  577. +/* Default: 10 */
  578. +/* Meaning: set the count of blocks to reserve for checkpointing */
  579. +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
  580. +
  581. +/*
  582. +Older-style on-NAND data format has a "pageStatus" byte to record
  583. +chunk/page state. This byte is zeroed when the page is discarded.
  584. +Choose this option if you have existing on-NAND data in this format
  585. +that you need to continue to support. New data written also uses the
  586. +older-style format.
  587. +Note: Use of this option generally requires that MTD's oob layout be
  588. +adjusted to use the older-style format. See notes on tags formats and
  589. +MTD versions in yaffs_mtdif1.c.
  590. +*/
  591. +/* Default: Not selected */
  592. +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
  593. +/* #define CONFIG_YAFFS_9BYTE_TAGS */
  594. +
  595. +#endif /* YAFFS_OUT_OF_TREE */
  596. +
  597. +#endif /* __YAFFS_CONFIG_H__ */
  598. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_checkptrw.c linux-2.6.34/fs/yaffs2/yaffs_checkptrw.c
  599. --- linux-2.6.34.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  600. +++ linux-2.6.34/fs/yaffs2/yaffs_checkptrw.c 2010-05-20 20:16:22.281614392 +0200
  601. @@ -0,0 +1,394 @@
  602. +/*
  603. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  604. + *
  605. + * Copyright (C) 2002-2007 Aleph One Ltd.
  606. + * for Toby Churchill Ltd and Brightstar Engineering
  607. + *
  608. + * Created by Charles Manning <charles@aleph1.co.uk>
  609. + *
  610. + * This program is free software; you can redistribute it and/or modify
  611. + * it under the terms of the GNU General Public License version 2 as
  612. + * published by the Free Software Foundation.
  613. + */
  614. +
  615. +const char *yaffs_checkptrw_c_version =
  616. + "$Id: yaffs_checkptrw.c,v 1.18 2009-03-06 17:20:49 wookey Exp $";
  617. +
  618. +
  619. +#include "yaffs_checkptrw.h"
  620. +#include "yaffs_getblockinfo.h"
  621. +
  622. +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
  623. +{
  624. + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
  625. +
  626. + T(YAFFS_TRACE_CHECKPOINT,
  627. + (TSTR("checkpt blocks available = %d" TENDSTR),
  628. + blocksAvailable));
  629. +
  630. + return (blocksAvailable <= 0) ? 0 : 1;
  631. +}
  632. +
  633. +
  634. +static int yaffs_CheckpointErase(yaffs_Device *dev)
  635. +{
  636. + int i;
  637. +
  638. + if (!dev->eraseBlockInNAND)
  639. + return 0;
  640. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
  641. + dev->internalStartBlock, dev->internalEndBlock));
  642. +
  643. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  644. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  645. + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
  646. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("erasing checkpt block %d"TENDSTR), i));
  647. + if (dev->eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
  648. + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
  649. + dev->nErasedBlocks++;
  650. + dev->nFreeChunks += dev->nChunksPerBlock;
  651. + } else {
  652. + dev->markNANDBlockBad(dev, i);
  653. + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
  654. + }
  655. + }
  656. + }
  657. +
  658. + dev->blocksInCheckpoint = 0;
  659. +
  660. + return 1;
  661. +}
  662. +
  663. +
  664. +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
  665. +{
  666. + int i;
  667. + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
  668. + T(YAFFS_TRACE_CHECKPOINT,
  669. + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
  670. + dev->nErasedBlocks, dev->nReservedBlocks, blocksAvailable, dev->checkpointNextBlock));
  671. +
  672. + if (dev->checkpointNextBlock >= 0 &&
  673. + dev->checkpointNextBlock <= dev->internalEndBlock &&
  674. + blocksAvailable > 0) {
  675. +
  676. + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
  677. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  678. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
  679. + dev->checkpointNextBlock = i + 1;
  680. + dev->checkpointCurrentBlock = i;
  681. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("allocating checkpt block %d"TENDSTR), i));
  682. + return;
  683. + }
  684. + }
  685. + }
  686. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
  687. +
  688. + dev->checkpointNextBlock = -1;
  689. + dev->checkpointCurrentBlock = -1;
  690. +}
  691. +
  692. +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
  693. +{
  694. + int i;
  695. + yaffs_ExtendedTags tags;
  696. +
  697. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
  698. + dev->blocksInCheckpoint, dev->checkpointNextBlock));
  699. +
  700. + if (dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
  701. + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
  702. + int chunk = i * dev->nChunksPerBlock;
  703. + int realignedChunk = chunk - dev->chunkOffset;
  704. +
  705. + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
  706. + NULL, &tags);
  707. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
  708. + i, tags.objectId, tags.sequenceNumber, tags.eccResult));
  709. +
  710. + if (tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  711. + /* Right kind of block */
  712. + dev->checkpointNextBlock = tags.objectId;
  713. + dev->checkpointCurrentBlock = i;
  714. + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
  715. + dev->blocksInCheckpoint++;
  716. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found checkpt block %d"TENDSTR), i));
  717. + return;
  718. + }
  719. + }
  720. +
  721. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
  722. +
  723. + dev->checkpointNextBlock = -1;
  724. + dev->checkpointCurrentBlock = -1;
  725. +}
  726. +
  727. +
  728. +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
  729. +{
  730. +
  731. + /* Got the functions we need? */
  732. + if (!dev->writeChunkWithTagsToNAND ||
  733. + !dev->readChunkWithTagsFromNAND ||
  734. + !dev->eraseBlockInNAND ||
  735. + !dev->markNANDBlockBad)
  736. + return 0;
  737. +
  738. + if (forWriting && !yaffs_CheckpointSpaceOk(dev))
  739. + return 0;
  740. +
  741. + if (!dev->checkpointBuffer)
  742. + dev->checkpointBuffer = YMALLOC_DMA(dev->totalBytesPerChunk);
  743. + if (!dev->checkpointBuffer)
  744. + return 0;
  745. +
  746. +
  747. + dev->checkpointPageSequence = 0;
  748. +
  749. + dev->checkpointOpenForWrite = forWriting;
  750. +
  751. + dev->checkpointByteCount = 0;
  752. + dev->checkpointSum = 0;
  753. + dev->checkpointXor = 0;
  754. + dev->checkpointCurrentBlock = -1;
  755. + dev->checkpointCurrentChunk = -1;
  756. + dev->checkpointNextBlock = dev->internalStartBlock;
  757. +
  758. + /* Erase all the blocks in the checkpoint area */
  759. + if (forWriting) {
  760. + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
  761. + dev->checkpointByteOffset = 0;
  762. + return yaffs_CheckpointErase(dev);
  763. + } else {
  764. + int i;
  765. + /* Set to a value that will kick off a read */
  766. + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
  767. + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
  768. + * going to be way more than we need */
  769. + dev->blocksInCheckpoint = 0;
  770. + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
  771. + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
  772. + for (i = 0; i < dev->checkpointMaxBlocks; i++)
  773. + dev->checkpointBlockList[i] = -1;
  774. + }
  775. +
  776. + return 1;
  777. +}
  778. +
  779. +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
  780. +{
  781. + __u32 compositeSum;
  782. + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
  783. + *sum = compositeSum;
  784. + return 1;
  785. +}
  786. +
  787. +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
  788. +{
  789. + int chunk;
  790. + int realignedChunk;
  791. +
  792. + yaffs_ExtendedTags tags;
  793. +
  794. + if (dev->checkpointCurrentBlock < 0) {
  795. + yaffs_CheckpointFindNextErasedBlock(dev);
  796. + dev->checkpointCurrentChunk = 0;
  797. + }
  798. +
  799. + if (dev->checkpointCurrentBlock < 0)
  800. + return 0;
  801. +
  802. + tags.chunkDeleted = 0;
  803. + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
  804. + tags.chunkId = dev->checkpointPageSequence + 1;
  805. + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  806. + tags.byteCount = dev->nDataBytesPerChunk;
  807. + if (dev->checkpointCurrentChunk == 0) {
  808. + /* First chunk we write for the block? Set block state to
  809. + checkpoint */
  810. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
  811. + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
  812. + dev->blocksInCheckpoint++;
  813. + }
  814. +
  815. + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
  816. +
  817. +
  818. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
  819. + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk, tags.objectId, tags.chunkId));
  820. +
  821. + realignedChunk = chunk - dev->chunkOffset;
  822. +
  823. + dev->writeChunkWithTagsToNAND(dev, realignedChunk,
  824. + dev->checkpointBuffer, &tags);
  825. + dev->checkpointByteOffset = 0;
  826. + dev->checkpointPageSequence++;
  827. + dev->checkpointCurrentChunk++;
  828. + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock) {
  829. + dev->checkpointCurrentChunk = 0;
  830. + dev->checkpointCurrentBlock = -1;
  831. + }
  832. + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
  833. +
  834. + return 1;
  835. +}
  836. +
  837. +
  838. +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
  839. +{
  840. + int i = 0;
  841. + int ok = 1;
  842. +
  843. +
  844. + __u8 * dataBytes = (__u8 *)data;
  845. +
  846. +
  847. +
  848. + if (!dev->checkpointBuffer)
  849. + return 0;
  850. +
  851. + if (!dev->checkpointOpenForWrite)
  852. + return -1;
  853. +
  854. + while (i < nBytes && ok) {
  855. + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
  856. + dev->checkpointSum += *dataBytes;
  857. + dev->checkpointXor ^= *dataBytes;
  858. +
  859. + dev->checkpointByteOffset++;
  860. + i++;
  861. + dataBytes++;
  862. + dev->checkpointByteCount++;
  863. +
  864. +
  865. + if (dev->checkpointByteOffset < 0 ||
  866. + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
  867. + ok = yaffs_CheckpointFlushBuffer(dev);
  868. + }
  869. +
  870. + return i;
  871. +}
  872. +
  873. +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
  874. +{
  875. + int i = 0;
  876. + int ok = 1;
  877. + yaffs_ExtendedTags tags;
  878. +
  879. +
  880. + int chunk;
  881. + int realignedChunk;
  882. +
  883. + __u8 *dataBytes = (__u8 *)data;
  884. +
  885. + if (!dev->checkpointBuffer)
  886. + return 0;
  887. +
  888. + if (dev->checkpointOpenForWrite)
  889. + return -1;
  890. +
  891. + while (i < nBytes && ok) {
  892. +
  893. +
  894. + if (dev->checkpointByteOffset < 0 ||
  895. + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
  896. +
  897. + if (dev->checkpointCurrentBlock < 0) {
  898. + yaffs_CheckpointFindNextCheckpointBlock(dev);
  899. + dev->checkpointCurrentChunk = 0;
  900. + }
  901. +
  902. + if (dev->checkpointCurrentBlock < 0)
  903. + ok = 0;
  904. + else {
  905. + chunk = dev->checkpointCurrentBlock *
  906. + dev->nChunksPerBlock +
  907. + dev->checkpointCurrentChunk;
  908. +
  909. + realignedChunk = chunk - dev->chunkOffset;
  910. +
  911. + /* read in the next chunk */
  912. + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
  913. + dev->readChunkWithTagsFromNAND(dev,
  914. + realignedChunk,
  915. + dev->checkpointBuffer,
  916. + &tags);
  917. +
  918. + if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
  919. + tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
  920. + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
  921. + ok = 0;
  922. +
  923. + dev->checkpointByteOffset = 0;
  924. + dev->checkpointPageSequence++;
  925. + dev->checkpointCurrentChunk++;
  926. +
  927. + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
  928. + dev->checkpointCurrentBlock = -1;
  929. + }
  930. + }
  931. +
  932. + if (ok) {
  933. + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
  934. + dev->checkpointSum += *dataBytes;
  935. + dev->checkpointXor ^= *dataBytes;
  936. + dev->checkpointByteOffset++;
  937. + i++;
  938. + dataBytes++;
  939. + dev->checkpointByteCount++;
  940. + }
  941. + }
  942. +
  943. + return i;
  944. +}
  945. +
  946. +int yaffs_CheckpointClose(yaffs_Device *dev)
  947. +{
  948. +
  949. + if (dev->checkpointOpenForWrite) {
  950. + if (dev->checkpointByteOffset != 0)
  951. + yaffs_CheckpointFlushBuffer(dev);
  952. + } else {
  953. + int i;
  954. + for (i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++) {
  955. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointBlockList[i]);
  956. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
  957. + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
  958. + else {
  959. + /* Todo this looks odd... */
  960. + }
  961. + }
  962. + YFREE(dev->checkpointBlockList);
  963. + dev->checkpointBlockList = NULL;
  964. + }
  965. +
  966. + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
  967. + dev->nErasedBlocks -= dev->blocksInCheckpoint;
  968. +
  969. +
  970. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
  971. + dev->checkpointByteCount));
  972. +
  973. + if (dev->checkpointBuffer) {
  974. + /* free the buffer */
  975. + YFREE(dev->checkpointBuffer);
  976. + dev->checkpointBuffer = NULL;
  977. + return 1;
  978. + } else
  979. + return 0;
  980. +}
  981. +
  982. +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
  983. +{
  984. + /* Erase the first checksum block */
  985. +
  986. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate"TENDSTR)));
  987. +
  988. + if (!yaffs_CheckpointSpaceOk(dev))
  989. + return 0;
  990. +
  991. + return yaffs_CheckpointErase(dev);
  992. +}
  993. +
  994. +
  995. +
  996. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_checkptrw.h linux-2.6.34/fs/yaffs2/yaffs_checkptrw.h
  997. --- linux-2.6.34.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  998. +++ linux-2.6.34/fs/yaffs2/yaffs_checkptrw.h 2010-05-20 20:16:22.281614392 +0200
  999. @@ -0,0 +1,35 @@
  1000. +/*
  1001. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1002. + *
  1003. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1004. + * for Toby Churchill Ltd and Brightstar Engineering
  1005. + *
  1006. + * Created by Charles Manning <charles@aleph1.co.uk>
  1007. + *
  1008. + * This program is free software; you can redistribute it and/or modify
  1009. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1010. + * published by the Free Software Foundation.
  1011. + *
  1012. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1013. + */
  1014. +
  1015. +#ifndef __YAFFS_CHECKPTRW_H__
  1016. +#define __YAFFS_CHECKPTRW_H__
  1017. +
  1018. +#include "yaffs_guts.h"
  1019. +
  1020. +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
  1021. +
  1022. +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes);
  1023. +
  1024. +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes);
  1025. +
  1026. +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
  1027. +
  1028. +int yaffs_CheckpointClose(yaffs_Device *dev);
  1029. +
  1030. +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
  1031. +
  1032. +
  1033. +#endif
  1034. +
  1035. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_ecc.c linux-2.6.34/fs/yaffs2/yaffs_ecc.c
  1036. --- linux-2.6.34.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1037. +++ linux-2.6.34/fs/yaffs2/yaffs_ecc.c 2010-05-20 20:16:22.281614392 +0200
  1038. @@ -0,0 +1,326 @@
  1039. +/*
  1040. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1041. + *
  1042. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1043. + * for Toby Churchill Ltd and Brightstar Engineering
  1044. + *
  1045. + * Created by Charles Manning <charles@aleph1.co.uk>
  1046. + *
  1047. + * This program is free software; you can redistribute it and/or modify
  1048. + * it under the terms of the GNU General Public License version 2 as
  1049. + * published by the Free Software Foundation.
  1050. + */
  1051. +
  1052. +/*
  1053. + * This code implements the ECC algorithm used in SmartMedia.
  1054. + *
  1055. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1056. + * The two unused bit are set to 1.
  1057. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1058. + * blocks are used on a 512-byte NAND page.
  1059. + *
  1060. + */
  1061. +
  1062. +/* Table generated by gen-ecc.c
  1063. + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  1064. + * for each byte of data. These are instead provided in a table in bits7..2.
  1065. + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  1066. + * this bytes influence on the line parity.
  1067. + */
  1068. +
  1069. +const char *yaffs_ecc_c_version =
  1070. + "$Id: yaffs_ecc.c,v 1.11 2009-03-06 17:20:50 wookey Exp $";
  1071. +
  1072. +#include "yportenv.h"
  1073. +
  1074. +#include "yaffs_ecc.h"
  1075. +
  1076. +static const unsigned char column_parity_table[] = {
  1077. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1078. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1079. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1080. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1081. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1082. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1083. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1084. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1085. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1086. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1087. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1088. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1089. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1090. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1091. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1092. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1093. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1094. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1095. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1096. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1097. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1098. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1099. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1100. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1101. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1102. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1103. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1104. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1105. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1106. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1107. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1108. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1109. +};
  1110. +
  1111. +/* Count the bits in an unsigned char or a U32 */
  1112. +
  1113. +static int yaffs_CountBits(unsigned char x)
  1114. +{
  1115. + int r = 0;
  1116. + while (x) {
  1117. + if (x & 1)
  1118. + r++;
  1119. + x >>= 1;
  1120. + }
  1121. + return r;
  1122. +}
  1123. +
  1124. +static int yaffs_CountBits32(unsigned x)
  1125. +{
  1126. + int r = 0;
  1127. + while (x) {
  1128. + if (x & 1)
  1129. + r++;
  1130. + x >>= 1;
  1131. + }
  1132. + return r;
  1133. +}
  1134. +
  1135. +/* Calculate the ECC for a 256-byte block of data */
  1136. +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
  1137. +{
  1138. + unsigned int i;
  1139. +
  1140. + unsigned char col_parity = 0;
  1141. + unsigned char line_parity = 0;
  1142. + unsigned char line_parity_prime = 0;
  1143. + unsigned char t;
  1144. + unsigned char b;
  1145. +
  1146. + for (i = 0; i < 256; i++) {
  1147. + b = column_parity_table[*data++];
  1148. + col_parity ^= b;
  1149. +
  1150. + if (b & 0x01) { /* odd number of bits in the byte */
  1151. + line_parity ^= i;
  1152. + line_parity_prime ^= ~i;
  1153. + }
  1154. + }
  1155. +
  1156. + ecc[2] = (~col_parity) | 0x03;
  1157. +
  1158. + t = 0;
  1159. + if (line_parity & 0x80)
  1160. + t |= 0x80;
  1161. + if (line_parity_prime & 0x80)
  1162. + t |= 0x40;
  1163. + if (line_parity & 0x40)
  1164. + t |= 0x20;
  1165. + if (line_parity_prime & 0x40)
  1166. + t |= 0x10;
  1167. + if (line_parity & 0x20)
  1168. + t |= 0x08;
  1169. + if (line_parity_prime & 0x20)
  1170. + t |= 0x04;
  1171. + if (line_parity & 0x10)
  1172. + t |= 0x02;
  1173. + if (line_parity_prime & 0x10)
  1174. + t |= 0x01;
  1175. + ecc[1] = ~t;
  1176. +
  1177. + t = 0;
  1178. + if (line_parity & 0x08)
  1179. + t |= 0x80;
  1180. + if (line_parity_prime & 0x08)
  1181. + t |= 0x40;
  1182. + if (line_parity & 0x04)
  1183. + t |= 0x20;
  1184. + if (line_parity_prime & 0x04)
  1185. + t |= 0x10;
  1186. + if (line_parity & 0x02)
  1187. + t |= 0x08;
  1188. + if (line_parity_prime & 0x02)
  1189. + t |= 0x04;
  1190. + if (line_parity & 0x01)
  1191. + t |= 0x02;
  1192. + if (line_parity_prime & 0x01)
  1193. + t |= 0x01;
  1194. + ecc[0] = ~t;
  1195. +
  1196. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1197. + /* Swap the bytes into the wrong order */
  1198. + t = ecc[0];
  1199. + ecc[0] = ecc[1];
  1200. + ecc[1] = t;
  1201. +#endif
  1202. +}
  1203. +
  1204. +
  1205. +/* Correct the ECC on a 256 byte block of data */
  1206. +
  1207. +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  1208. + const unsigned char *test_ecc)
  1209. +{
  1210. + unsigned char d0, d1, d2; /* deltas */
  1211. +
  1212. + d0 = read_ecc[0] ^ test_ecc[0];
  1213. + d1 = read_ecc[1] ^ test_ecc[1];
  1214. + d2 = read_ecc[2] ^ test_ecc[2];
  1215. +
  1216. + if ((d0 | d1 | d2) == 0)
  1217. + return 0; /* no error */
  1218. +
  1219. + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  1220. + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  1221. + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  1222. + /* Single bit (recoverable) error in data */
  1223. +
  1224. + unsigned byte;
  1225. + unsigned bit;
  1226. +
  1227. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1228. + /* swap the bytes to correct for the wrong order */
  1229. + unsigned char t;
  1230. +
  1231. + t = d0;
  1232. + d0 = d1;
  1233. + d1 = t;
  1234. +#endif
  1235. +
  1236. + bit = byte = 0;
  1237. +
  1238. + if (d1 & 0x80)
  1239. + byte |= 0x80;
  1240. + if (d1 & 0x20)
  1241. + byte |= 0x40;
  1242. + if (d1 & 0x08)
  1243. + byte |= 0x20;
  1244. + if (d1 & 0x02)
  1245. + byte |= 0x10;
  1246. + if (d0 & 0x80)
  1247. + byte |= 0x08;
  1248. + if (d0 & 0x20)
  1249. + byte |= 0x04;
  1250. + if (d0 & 0x08)
  1251. + byte |= 0x02;
  1252. + if (d0 & 0x02)
  1253. + byte |= 0x01;
  1254. +
  1255. + if (d2 & 0x80)
  1256. + bit |= 0x04;
  1257. + if (d2 & 0x20)
  1258. + bit |= 0x02;
  1259. + if (d2 & 0x08)
  1260. + bit |= 0x01;
  1261. +
  1262. + data[byte] ^= (1 << bit);
  1263. +
  1264. + return 1; /* Corrected the error */
  1265. + }
  1266. +
  1267. + if ((yaffs_CountBits(d0) +
  1268. + yaffs_CountBits(d1) +
  1269. + yaffs_CountBits(d2)) == 1) {
  1270. + /* Reccoverable error in ecc */
  1271. +
  1272. + read_ecc[0] = test_ecc[0];
  1273. + read_ecc[1] = test_ecc[1];
  1274. + read_ecc[2] = test_ecc[2];
  1275. +
  1276. + return 1; /* Corrected the error */
  1277. + }
  1278. +
  1279. + /* Unrecoverable error */
  1280. +
  1281. + return -1;
  1282. +
  1283. +}
  1284. +
  1285. +
  1286. +/*
  1287. + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  1288. + */
  1289. +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  1290. + yaffs_ECCOther *eccOther)
  1291. +{
  1292. + unsigned int i;
  1293. +
  1294. + unsigned char col_parity = 0;
  1295. + unsigned line_parity = 0;
  1296. + unsigned line_parity_prime = 0;
  1297. + unsigned char b;
  1298. +
  1299. + for (i = 0; i < nBytes; i++) {
  1300. + b = column_parity_table[*data++];
  1301. + col_parity ^= b;
  1302. +
  1303. + if (b & 0x01) {
  1304. + /* odd number of bits in the byte */
  1305. + line_parity ^= i;
  1306. + line_parity_prime ^= ~i;
  1307. + }
  1308. +
  1309. + }
  1310. +
  1311. + eccOther->colParity = (col_parity >> 2) & 0x3f;
  1312. + eccOther->lineParity = line_parity;
  1313. + eccOther->lineParityPrime = line_parity_prime;
  1314. +}
  1315. +
  1316. +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  1317. + yaffs_ECCOther *read_ecc,
  1318. + const yaffs_ECCOther *test_ecc)
  1319. +{
  1320. + unsigned char cDelta; /* column parity delta */
  1321. + unsigned lDelta; /* line parity delta */
  1322. + unsigned lDeltaPrime; /* line parity delta */
  1323. + unsigned bit;
  1324. +
  1325. + cDelta = read_ecc->colParity ^ test_ecc->colParity;
  1326. + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
  1327. + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
  1328. +
  1329. + if ((cDelta | lDelta | lDeltaPrime) == 0)
  1330. + return 0; /* no error */
  1331. +
  1332. + if (lDelta == ~lDeltaPrime &&
  1333. + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) {
  1334. + /* Single bit (recoverable) error in data */
  1335. +
  1336. + bit = 0;
  1337. +
  1338. + if (cDelta & 0x20)
  1339. + bit |= 0x04;
  1340. + if (cDelta & 0x08)
  1341. + bit |= 0x02;
  1342. + if (cDelta & 0x02)
  1343. + bit |= 0x01;
  1344. +
  1345. + if (lDelta >= nBytes)
  1346. + return -1;
  1347. +
  1348. + data[lDelta] ^= (1 << bit);
  1349. +
  1350. + return 1; /* corrected */
  1351. + }
  1352. +
  1353. + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
  1354. + yaffs_CountBits(cDelta)) == 1) {
  1355. + /* Reccoverable error in ecc */
  1356. +
  1357. + *read_ecc = *test_ecc;
  1358. + return 1; /* corrected */
  1359. + }
  1360. +
  1361. + /* Unrecoverable error */
  1362. +
  1363. + return -1;
  1364. +}
  1365. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_ecc.h linux-2.6.34/fs/yaffs2/yaffs_ecc.h
  1366. --- linux-2.6.34.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1367. +++ linux-2.6.34/fs/yaffs2/yaffs_ecc.h 2010-05-20 20:16:22.281614392 +0200
  1368. @@ -0,0 +1,44 @@
  1369. +/*
  1370. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1371. + *
  1372. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1373. + * for Toby Churchill Ltd and Brightstar Engineering
  1374. + *
  1375. + * Created by Charles Manning <charles@aleph1.co.uk>
  1376. + *
  1377. + * This program is free software; you can redistribute it and/or modify
  1378. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1379. + * published by the Free Software Foundation.
  1380. + *
  1381. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1382. + */
  1383. +
  1384. +/*
  1385. + * This code implements the ECC algorithm used in SmartMedia.
  1386. + *
  1387. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1388. + * The two unused bit are set to 1.
  1389. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1390. + * blocks are used on a 512-byte NAND page.
  1391. + *
  1392. + */
  1393. +
  1394. +#ifndef __YAFFS_ECC_H__
  1395. +#define __YAFFS_ECC_H__
  1396. +
  1397. +typedef struct {
  1398. + unsigned char colParity;
  1399. + unsigned lineParity;
  1400. + unsigned lineParityPrime;
  1401. +} yaffs_ECCOther;
  1402. +
  1403. +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
  1404. +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  1405. + const unsigned char *test_ecc);
  1406. +
  1407. +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  1408. + yaffs_ECCOther *ecc);
  1409. +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  1410. + yaffs_ECCOther *read_ecc,
  1411. + const yaffs_ECCOther *test_ecc);
  1412. +#endif
  1413. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_fs.c linux-2.6.34/fs/yaffs2/yaffs_fs.c
  1414. --- linux-2.6.34.orig/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
  1415. +++ linux-2.6.34/fs/yaffs2/yaffs_fs.c 2010-05-20 20:16:22.281614392 +0200
  1416. @@ -0,0 +1,2529 @@
  1417. +/*
  1418. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1419. + *
  1420. + * Copyright (C) 2002-2009 Aleph One Ltd.
  1421. + * for Toby Churchill Ltd and Brightstar Engineering
  1422. + *
  1423. + * Created by Charles Manning <charles@aleph1.co.uk>
  1424. + * Acknowledgements:
  1425. + * Luc van OostenRyck for numerous patches.
  1426. + * Nick Bane for numerous patches.
  1427. + * Nick Bane for 2.5/2.6 integration.
  1428. + * Andras Toth for mknod rdev issue.
  1429. + * Michael Fischer for finding the problem with inode inconsistency.
  1430. + * Some code bodily lifted from JFFS
  1431. + *
  1432. + * This program is free software; you can redistribute it and/or modify
  1433. + * it under the terms of the GNU General Public License version 2 as
  1434. + * published by the Free Software Foundation.
  1435. + */
  1436. +
  1437. +/*
  1438. + *
  1439. + * This is the file system front-end to YAFFS that hooks it up to
  1440. + * the VFS.
  1441. + *
  1442. + * Special notes:
  1443. + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
  1444. + * this superblock
  1445. + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
  1446. + * superblock
  1447. + * >> inode->u.generic_ip points to the associated yaffs_Object.
  1448. + */
  1449. +
  1450. +const char *yaffs_fs_c_version =
  1451. + "$Id: yaffs_fs.c,v 1.79 2009-03-17 01:12:00 wookey Exp $";
  1452. +extern const char *yaffs_guts_c_version;
  1453. +
  1454. +#include <linux/version.h>
  1455. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  1456. +#include <linux/config.h>
  1457. +#endif
  1458. +#include <linux/kernel.h>
  1459. +#include <linux/module.h>
  1460. +#include <linux/slab.h>
  1461. +#include <linux/init.h>
  1462. +#include <linux/fs.h>
  1463. +#include <linux/proc_fs.h>
  1464. +#include <linux/smp_lock.h>
  1465. +#include <linux/pagemap.h>
  1466. +#include <linux/mtd/mtd.h>
  1467. +#include <linux/interrupt.h>
  1468. +#include <linux/string.h>
  1469. +#include <linux/ctype.h>
  1470. +
  1471. +#include "asm/div64.h"
  1472. +
  1473. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1474. +
  1475. +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
  1476. +#include <linux/statfs.h>
  1477. +#define UnlockPage(p) unlock_page(p)
  1478. +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  1479. +
  1480. +/* FIXME: use sb->s_id instead ? */
  1481. +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  1482. +
  1483. +#else
  1484. +
  1485. +#include <linux/locks.h>
  1486. +#define BDEVNAME_SIZE 0
  1487. +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
  1488. +
  1489. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
  1490. +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
  1491. +#define __user
  1492. +#endif
  1493. +
  1494. +#endif
  1495. +
  1496. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  1497. +#define YPROC_ROOT (&proc_root)
  1498. +#else
  1499. +#define YPROC_ROOT NULL
  1500. +#endif
  1501. +
  1502. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1503. +#define WRITE_SIZE_STR "writesize"
  1504. +#define WRITE_SIZE(mtd) ((mtd)->writesize)
  1505. +#else
  1506. +#define WRITE_SIZE_STR "oobblock"
  1507. +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
  1508. +#endif
  1509. +
  1510. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
  1511. +#define YAFFS_USE_WRITE_BEGIN_END 1
  1512. +#else
  1513. +#define YAFFS_USE_WRITE_BEGIN_END 0
  1514. +#endif
  1515. +
  1516. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
  1517. +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  1518. +{
  1519. + uint64_t result = partition_size;
  1520. + do_div(result, block_size);
  1521. + return (uint32_t)result;
  1522. +}
  1523. +#else
  1524. +#define YCALCBLOCKS(s, b) ((s)/(b))
  1525. +#endif
  1526. +
  1527. +#include <linux/uaccess.h>
  1528. +
  1529. +#include "yportenv.h"
  1530. +#include "yaffs_guts.h"
  1531. +
  1532. +#include <linux/mtd/mtd.h>
  1533. +#include "yaffs_mtdif.h"
  1534. +#include "yaffs_mtdif1.h"
  1535. +#include "yaffs_mtdif2.h"
  1536. +
  1537. +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
  1538. +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  1539. +unsigned int yaffs_auto_checkpoint = 1;
  1540. +
  1541. +/* Module Parameters */
  1542. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1543. +module_param(yaffs_traceMask, uint, 0644);
  1544. +module_param(yaffs_wr_attempts, uint, 0644);
  1545. +module_param(yaffs_auto_checkpoint, uint, 0644);
  1546. +#else
  1547. +MODULE_PARM(yaffs_traceMask, "i");
  1548. +MODULE_PARM(yaffs_wr_attempts, "i");
  1549. +MODULE_PARM(yaffs_auto_checkpoint, "i");
  1550. +#endif
  1551. +
  1552. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  1553. +/* use iget and read_inode */
  1554. +#define Y_IGET(sb, inum) iget((sb), (inum))
  1555. +static void yaffs_read_inode(struct inode *inode);
  1556. +
  1557. +#else
  1558. +/* Call local equivalent */
  1559. +#define YAFFS_USE_OWN_IGET
  1560. +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
  1561. +
  1562. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
  1563. +#endif
  1564. +
  1565. +/*#define T(x) printk x */
  1566. +
  1567. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  1568. +#define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
  1569. +#else
  1570. +#define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
  1571. +#endif
  1572. +
  1573. +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
  1574. +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
  1575. +
  1576. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1577. +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
  1578. +#else
  1579. +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
  1580. +#endif
  1581. +
  1582. +static void yaffs_put_super(struct super_block *sb);
  1583. +
  1584. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  1585. + loff_t *pos);
  1586. +static ssize_t yaffs_hold_space(struct file *f);
  1587. +static void yaffs_release_space(struct file *f);
  1588. +
  1589. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1590. +static int yaffs_file_flush(struct file *file, fl_owner_t id);
  1591. +#else
  1592. +static int yaffs_file_flush(struct file *file);
  1593. +#endif
  1594. +
  1595. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  1596. + int datasync);
  1597. +
  1598. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
  1599. +
  1600. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1601. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  1602. + struct nameidata *n);
  1603. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  1604. + struct nameidata *n);
  1605. +#else
  1606. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
  1607. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
  1608. +#endif
  1609. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  1610. + struct dentry *dentry);
  1611. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
  1612. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  1613. + const char *symname);
  1614. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  1615. +
  1616. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1617. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  1618. + dev_t dev);
  1619. +#else
  1620. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  1621. + int dev);
  1622. +#endif
  1623. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1624. + struct inode *new_dir, struct dentry *new_dentry);
  1625. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
  1626. +
  1627. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1628. +static int yaffs_sync_fs(struct super_block *sb, int wait);
  1629. +static void yaffs_write_super(struct super_block *sb);
  1630. +#else
  1631. +static int yaffs_sync_fs(struct super_block *sb);
  1632. +static int yaffs_write_super(struct super_block *sb);
  1633. +#endif
  1634. +
  1635. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1636. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
  1637. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1638. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
  1639. +#else
  1640. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
  1641. +#endif
  1642. +
  1643. +#ifdef YAFFS_HAS_PUT_INODE
  1644. +static void yaffs_put_inode(struct inode *inode);
  1645. +#endif
  1646. +
  1647. +static void yaffs_delete_inode(struct inode *);
  1648. +static void yaffs_clear_inode(struct inode *);
  1649. +
  1650. +static int yaffs_readpage(struct file *file, struct page *page);
  1651. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1652. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
  1653. +#else
  1654. +static int yaffs_writepage(struct page *page);
  1655. +#endif
  1656. +
  1657. +
  1658. +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
  1659. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  1660. + loff_t pos, unsigned len, unsigned flags,
  1661. + struct page **pagep, void **fsdata);
  1662. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  1663. + loff_t pos, unsigned len, unsigned copied,
  1664. + struct page *pg, void *fsdadata);
  1665. +#else
  1666. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  1667. + unsigned offset, unsigned to);
  1668. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  1669. + unsigned to);
  1670. +
  1671. +#endif
  1672. +
  1673. +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
  1674. + int buflen);
  1675. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1676. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  1677. +#else
  1678. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  1679. +#endif
  1680. +
  1681. +static struct address_space_operations yaffs_file_address_operations = {
  1682. + .readpage = yaffs_readpage,
  1683. + .writepage = yaffs_writepage,
  1684. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  1685. + .write_begin = yaffs_write_begin,
  1686. + .write_end = yaffs_write_end,
  1687. +#else
  1688. + .prepare_write = yaffs_prepare_write,
  1689. + .commit_write = yaffs_commit_write,
  1690. +#endif
  1691. +};
  1692. +
  1693. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
  1694. +static const struct file_operations yaffs_file_operations = {
  1695. + .read = do_sync_read,
  1696. + .write = do_sync_write,
  1697. + .aio_read = generic_file_aio_read,
  1698. + .aio_write = generic_file_aio_write,
  1699. + .mmap = generic_file_mmap,
  1700. + .flush = yaffs_file_flush,
  1701. + .fsync = yaffs_sync_object,
  1702. + .splice_read = generic_file_splice_read,
  1703. + .splice_write = generic_file_splice_write,
  1704. + .llseek = generic_file_llseek,
  1705. +};
  1706. +
  1707. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  1708. +
  1709. +static const struct file_operations yaffs_file_operations = {
  1710. + .read = do_sync_read,
  1711. + .write = do_sync_write,
  1712. + .aio_read = generic_file_aio_read,
  1713. + .aio_write = generic_file_aio_write,
  1714. + .mmap = generic_file_mmap,
  1715. + .flush = yaffs_file_flush,
  1716. + .fsync = yaffs_sync_object,
  1717. + .sendfile = generic_file_sendfile,
  1718. +};
  1719. +
  1720. +#else
  1721. +
  1722. +static const struct file_operations yaffs_file_operations = {
  1723. + .read = generic_file_read,
  1724. + .write = generic_file_write,
  1725. + .mmap = generic_file_mmap,
  1726. + .flush = yaffs_file_flush,
  1727. + .fsync = yaffs_sync_object,
  1728. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1729. + .sendfile = generic_file_sendfile,
  1730. +#endif
  1731. +};
  1732. +#endif
  1733. +
  1734. +static const struct inode_operations yaffs_file_inode_operations = {
  1735. + .setattr = yaffs_setattr,
  1736. +};
  1737. +
  1738. +static const struct inode_operations yaffs_symlink_inode_operations = {
  1739. + .readlink = yaffs_readlink,
  1740. + .follow_link = yaffs_follow_link,
  1741. + .setattr = yaffs_setattr,
  1742. +};
  1743. +
  1744. +static const struct inode_operations yaffs_dir_inode_operations = {
  1745. + .create = yaffs_create,
  1746. + .lookup = yaffs_lookup,
  1747. + .link = yaffs_link,
  1748. + .unlink = yaffs_unlink,
  1749. + .symlink = yaffs_symlink,
  1750. + .mkdir = yaffs_mkdir,
  1751. + .rmdir = yaffs_unlink,
  1752. + .mknod = yaffs_mknod,
  1753. + .rename = yaffs_rename,
  1754. + .setattr = yaffs_setattr,
  1755. +};
  1756. +
  1757. +static const struct file_operations yaffs_dir_operations = {
  1758. + .read = generic_read_dir,
  1759. + .readdir = yaffs_readdir,
  1760. + .fsync = yaffs_sync_object,
  1761. +};
  1762. +
  1763. +static const struct super_operations yaffs_super_ops = {
  1764. + .statfs = yaffs_statfs,
  1765. +
  1766. +#ifndef YAFFS_USE_OWN_IGET
  1767. + .read_inode = yaffs_read_inode,
  1768. +#endif
  1769. +#ifdef YAFFS_HAS_PUT_INODE
  1770. + .put_inode = yaffs_put_inode,
  1771. +#endif
  1772. + .put_super = yaffs_put_super,
  1773. + .delete_inode = yaffs_delete_inode,
  1774. + .clear_inode = yaffs_clear_inode,
  1775. + .sync_fs = yaffs_sync_fs,
  1776. + .write_super = yaffs_write_super,
  1777. +};
  1778. +
  1779. +static void yaffs_GrossLock(yaffs_Device *dev)
  1780. +{
  1781. + T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
  1782. + down(&dev->grossLock);
  1783. + T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
  1784. +}
  1785. +
  1786. +static void yaffs_GrossUnlock(yaffs_Device *dev)
  1787. +{
  1788. + T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
  1789. + up(&dev->grossLock);
  1790. +}
  1791. +
  1792. +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
  1793. + int buflen)
  1794. +{
  1795. + unsigned char *alias;
  1796. + int ret;
  1797. +
  1798. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  1799. +
  1800. + yaffs_GrossLock(dev);
  1801. +
  1802. + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
  1803. +
  1804. + yaffs_GrossUnlock(dev);
  1805. +
  1806. + if (!alias)
  1807. + return -ENOMEM;
  1808. +
  1809. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  1810. + kfree(alias);
  1811. + return ret;
  1812. +}
  1813. +
  1814. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1815. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1816. +#else
  1817. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1818. +#endif
  1819. +{
  1820. + unsigned char *alias;
  1821. + int ret;
  1822. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  1823. +
  1824. + yaffs_GrossLock(dev);
  1825. +
  1826. + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
  1827. +
  1828. + yaffs_GrossUnlock(dev);
  1829. +
  1830. + if (!alias) {
  1831. + ret = -ENOMEM;
  1832. + goto out;
  1833. + }
  1834. +
  1835. + ret = vfs_follow_link(nd, alias);
  1836. + kfree(alias);
  1837. +out:
  1838. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1839. + return ERR_PTR(ret);
  1840. +#else
  1841. + return ret;
  1842. +#endif
  1843. +}
  1844. +
  1845. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  1846. + yaffs_Object *obj);
  1847. +
  1848. +/*
  1849. + * Lookup is used to find objects in the fs
  1850. + */
  1851. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1852. +
  1853. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  1854. + struct nameidata *n)
  1855. +#else
  1856. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  1857. +#endif
  1858. +{
  1859. + yaffs_Object *obj;
  1860. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  1861. +
  1862. + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
  1863. +
  1864. + yaffs_GrossLock(dev);
  1865. +
  1866. + T(YAFFS_TRACE_OS,
  1867. + ("yaffs_lookup for %d:%s\n",
  1868. + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
  1869. +
  1870. + obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
  1871. + dentry->d_name.name);
  1872. +
  1873. + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
  1874. +
  1875. + /* Can't hold gross lock when calling yaffs_get_inode() */
  1876. + yaffs_GrossUnlock(dev);
  1877. +
  1878. + if (obj) {
  1879. + T(YAFFS_TRACE_OS,
  1880. + ("yaffs_lookup found %d\n", obj->objectId));
  1881. +
  1882. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  1883. +
  1884. + if (inode) {
  1885. + T(YAFFS_TRACE_OS,
  1886. + ("yaffs_loookup dentry \n"));
  1887. +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
  1888. + * d_add even if NULL inode */
  1889. +#if 0
  1890. + /*dget(dentry); // try to solve directory bug */
  1891. + d_add(dentry, inode);
  1892. +
  1893. + /* return dentry; */
  1894. + return NULL;
  1895. +#endif
  1896. + }
  1897. +
  1898. + } else {
  1899. + T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
  1900. +
  1901. + }
  1902. +
  1903. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  1904. + * NULL which creates dentry hash */
  1905. + d_add(dentry, inode);
  1906. +
  1907. + return NULL;
  1908. +}
  1909. +
  1910. +
  1911. +#ifdef YAFFS_HAS_PUT_INODE
  1912. +
  1913. +/* For now put inode is just for debugging
  1914. + * Put inode is called when the inode **structure** is put.
  1915. + */
  1916. +static void yaffs_put_inode(struct inode *inode)
  1917. +{
  1918. + T(YAFFS_TRACE_OS,
  1919. + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
  1920. + atomic_read(&inode->i_count)));
  1921. +
  1922. +}
  1923. +#endif
  1924. +
  1925. +/* clear is called to tell the fs to release any per-inode data it holds */
  1926. +static void yaffs_clear_inode(struct inode *inode)
  1927. +{
  1928. + yaffs_Object *obj;
  1929. + yaffs_Device *dev;
  1930. +
  1931. + obj = yaffs_InodeToObject(inode);
  1932. +
  1933. + T(YAFFS_TRACE_OS,
  1934. + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
  1935. + atomic_read(&inode->i_count),
  1936. + obj ? "object exists" : "null object"));
  1937. +
  1938. + if (obj) {
  1939. + dev = obj->myDev;
  1940. + yaffs_GrossLock(dev);
  1941. +
  1942. + /* Clear the association between the inode and
  1943. + * the yaffs_Object.
  1944. + */
  1945. + obj->myInode = NULL;
  1946. + yaffs_InodeToObjectLV(inode) = NULL;
  1947. +
  1948. + /* If the object freeing was deferred, then the real
  1949. + * free happens now.
  1950. + * This should fix the inode inconsistency problem.
  1951. + */
  1952. +
  1953. + yaffs_HandleDeferedFree(obj);
  1954. +
  1955. + yaffs_GrossUnlock(dev);
  1956. + }
  1957. +
  1958. +}
  1959. +
  1960. +/* delete is called when the link count is zero and the inode
  1961. + * is put (ie. nobody wants to know about it anymore, time to
  1962. + * delete the file).
  1963. + * NB Must call clear_inode()
  1964. + */
  1965. +static void yaffs_delete_inode(struct inode *inode)
  1966. +{
  1967. + yaffs_Object *obj = yaffs_InodeToObject(inode);
  1968. + yaffs_Device *dev;
  1969. +
  1970. + T(YAFFS_TRACE_OS,
  1971. + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
  1972. + atomic_read(&inode->i_count),
  1973. + obj ? "object exists" : "null object"));
  1974. +
  1975. + if (obj) {
  1976. + dev = obj->myDev;
  1977. + yaffs_GrossLock(dev);
  1978. + yaffs_DeleteObject(obj);
  1979. + yaffs_GrossUnlock(dev);
  1980. + }
  1981. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1982. + truncate_inode_pages(&inode->i_data, 0);
  1983. +#endif
  1984. + clear_inode(inode);
  1985. +}
  1986. +
  1987. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1988. +static int yaffs_file_flush(struct file *file, fl_owner_t id)
  1989. +#else
  1990. +static int yaffs_file_flush(struct file *file)
  1991. +#endif
  1992. +{
  1993. + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
  1994. +
  1995. + yaffs_Device *dev = obj->myDev;
  1996. +
  1997. + T(YAFFS_TRACE_OS,
  1998. + ("yaffs_file_flush object %d (%s)\n", obj->objectId,
  1999. + obj->dirty ? "dirty" : "clean"));
  2000. +
  2001. + yaffs_GrossLock(dev);
  2002. +
  2003. + yaffs_FlushFile(obj, 1);
  2004. +
  2005. + yaffs_GrossUnlock(dev);
  2006. +
  2007. + return 0;
  2008. +}
  2009. +
  2010. +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  2011. +{
  2012. + /* Lifted from jffs2 */
  2013. +
  2014. + yaffs_Object *obj;
  2015. + unsigned char *pg_buf;
  2016. + int ret;
  2017. +
  2018. + yaffs_Device *dev;
  2019. +
  2020. + T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
  2021. + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
  2022. + (unsigned)PAGE_CACHE_SIZE));
  2023. +
  2024. + obj = yaffs_DentryToObject(f->f_dentry);
  2025. +
  2026. + dev = obj->myDev;
  2027. +
  2028. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2029. + BUG_ON(!PageLocked(pg));
  2030. +#else
  2031. + if (!PageLocked(pg))
  2032. + PAGE_BUG(pg);
  2033. +#endif
  2034. +
  2035. + pg_buf = kmap(pg);
  2036. + /* FIXME: Can kmap fail? */
  2037. +
  2038. + yaffs_GrossLock(dev);
  2039. +
  2040. + ret = yaffs_ReadDataFromFile(obj, pg_buf,
  2041. + pg->index << PAGE_CACHE_SHIFT,
  2042. + PAGE_CACHE_SIZE);
  2043. +
  2044. + yaffs_GrossUnlock(dev);
  2045. +
  2046. + if (ret >= 0)
  2047. + ret = 0;
  2048. +
  2049. + if (ret) {
  2050. + ClearPageUptodate(pg);
  2051. + SetPageError(pg);
  2052. + } else {
  2053. + SetPageUptodate(pg);
  2054. + ClearPageError(pg);
  2055. + }
  2056. +
  2057. + flush_dcache_page(pg);
  2058. + kunmap(pg);
  2059. +
  2060. + T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
  2061. + return ret;
  2062. +}
  2063. +
  2064. +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  2065. +{
  2066. + int ret = yaffs_readpage_nolock(f, pg);
  2067. + UnlockPage(pg);
  2068. + return ret;
  2069. +}
  2070. +
  2071. +static int yaffs_readpage(struct file *f, struct page *pg)
  2072. +{
  2073. + return yaffs_readpage_unlock(f, pg);
  2074. +}
  2075. +
  2076. +/* writepage inspired by/stolen from smbfs */
  2077. +
  2078. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2079. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  2080. +#else
  2081. +static int yaffs_writepage(struct page *page)
  2082. +#endif
  2083. +{
  2084. + struct address_space *mapping = page->mapping;
  2085. + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
  2086. + struct inode *inode;
  2087. + unsigned long end_index;
  2088. + char *buffer;
  2089. + yaffs_Object *obj;
  2090. + int nWritten = 0;
  2091. + unsigned nBytes;
  2092. +
  2093. + if (!mapping)
  2094. + BUG();
  2095. + inode = mapping->host;
  2096. + if (!inode)
  2097. + BUG();
  2098. +
  2099. + if (offset > inode->i_size) {
  2100. + T(YAFFS_TRACE_OS,
  2101. + ("yaffs_writepage at %08x, inode size = %08x!!!\n",
  2102. + (unsigned)(page->index << PAGE_CACHE_SHIFT),
  2103. + (unsigned)inode->i_size));
  2104. + T(YAFFS_TRACE_OS,
  2105. + (" -> don't care!!\n"));
  2106. + unlock_page(page);
  2107. + return 0;
  2108. + }
  2109. +
  2110. + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  2111. +
  2112. + /* easy case */
  2113. + if (page->index < end_index)
  2114. + nBytes = PAGE_CACHE_SIZE;
  2115. + else
  2116. + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
  2117. +
  2118. + get_page(page);
  2119. +
  2120. + buffer = kmap(page);
  2121. +
  2122. + obj = yaffs_InodeToObject(inode);
  2123. + yaffs_GrossLock(obj->myDev);
  2124. +
  2125. + T(YAFFS_TRACE_OS,
  2126. + ("yaffs_writepage at %08x, size %08x\n",
  2127. + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
  2128. + T(YAFFS_TRACE_OS,
  2129. + ("writepag0: obj = %05x, ino = %05x\n",
  2130. + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
  2131. +
  2132. + nWritten = yaffs_WriteDataToFile(obj, buffer,
  2133. + page->index << PAGE_CACHE_SHIFT, nBytes, 0);
  2134. +
  2135. + T(YAFFS_TRACE_OS,
  2136. + ("writepag1: obj = %05x, ino = %05x\n",
  2137. + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
  2138. +
  2139. + yaffs_GrossUnlock(obj->myDev);
  2140. +
  2141. + kunmap(page);
  2142. + SetPageUptodate(page);
  2143. + UnlockPage(page);
  2144. + put_page(page);
  2145. +
  2146. + return (nWritten == nBytes) ? 0 : -ENOSPC;
  2147. +}
  2148. +
  2149. +
  2150. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  2151. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  2152. + loff_t pos, unsigned len, unsigned flags,
  2153. + struct page **pagep, void **fsdata)
  2154. +{
  2155. + struct page *pg = NULL;
  2156. + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  2157. + uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
  2158. + uint32_t to = offset + len;
  2159. +
  2160. + int ret = 0;
  2161. + int space_held = 0;
  2162. +
  2163. + T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
  2164. + /* Get a page */
  2165. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28)
  2166. + pg = grab_cache_page_write_begin(mapping, index, flags);
  2167. +#else
  2168. + pg = __grab_cache_page(mapping, index);
  2169. +#endif
  2170. +
  2171. + *pagep = pg;
  2172. + if (!pg) {
  2173. + ret = -ENOMEM;
  2174. + goto out;
  2175. + }
  2176. + /* Get fs space */
  2177. + space_held = yaffs_hold_space(filp);
  2178. +
  2179. + if (!space_held) {
  2180. + ret = -ENOSPC;
  2181. + goto out;
  2182. + }
  2183. +
  2184. + /* Update page if required */
  2185. +
  2186. + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
  2187. + ret = yaffs_readpage_nolock(filp, pg);
  2188. +
  2189. + if (ret)
  2190. + goto out;
  2191. +
  2192. + /* Happy path return */
  2193. + T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
  2194. +
  2195. + return 0;
  2196. +
  2197. +out:
  2198. + T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
  2199. + if (space_held)
  2200. + yaffs_release_space(filp);
  2201. + if (pg) {
  2202. + unlock_page(pg);
  2203. + page_cache_release(pg);
  2204. + }
  2205. + return ret;
  2206. +}
  2207. +
  2208. +#else
  2209. +
  2210. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  2211. + unsigned offset, unsigned to)
  2212. +{
  2213. + T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
  2214. +
  2215. + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
  2216. + return yaffs_readpage_nolock(f, pg);
  2217. + return 0;
  2218. +}
  2219. +#endif
  2220. +
  2221. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  2222. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  2223. + loff_t pos, unsigned len, unsigned copied,
  2224. + struct page *pg, void *fsdadata)
  2225. +{
  2226. + int ret = 0;
  2227. + void *addr, *kva;
  2228. + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  2229. +
  2230. + kva = kmap(pg);
  2231. + addr = kva + offset_into_page;
  2232. +
  2233. + T(YAFFS_TRACE_OS,
  2234. + ("yaffs_write_end addr %x pos %x nBytes %d\n",
  2235. + (unsigned) addr,
  2236. + (int)pos, copied));
  2237. +
  2238. + ret = yaffs_file_write(filp, addr, copied, &pos);
  2239. +
  2240. + if (ret != copied) {
  2241. + T(YAFFS_TRACE_OS,
  2242. + ("yaffs_write_end not same size ret %d copied %d\n",
  2243. + ret, copied));
  2244. + SetPageError(pg);
  2245. + ClearPageUptodate(pg);
  2246. + } else {
  2247. + SetPageUptodate(pg);
  2248. + }
  2249. +
  2250. + kunmap(pg);
  2251. +
  2252. + yaffs_release_space(filp);
  2253. + unlock_page(pg);
  2254. + page_cache_release(pg);
  2255. + return ret;
  2256. +}
  2257. +#else
  2258. +
  2259. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  2260. + unsigned to)
  2261. +{
  2262. + void *addr, *kva;
  2263. +
  2264. + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
  2265. + int nBytes = to - offset;
  2266. + int nWritten;
  2267. +
  2268. + unsigned spos = pos;
  2269. + unsigned saddr;
  2270. +
  2271. + kva = kmap(pg);
  2272. + addr = kva + offset;
  2273. +
  2274. + saddr = (unsigned) addr;
  2275. +
  2276. + T(YAFFS_TRACE_OS,
  2277. + ("yaffs_commit_write addr %x pos %x nBytes %d\n",
  2278. + saddr, spos, nBytes));
  2279. +
  2280. + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
  2281. +
  2282. + if (nWritten != nBytes) {
  2283. + T(YAFFS_TRACE_OS,
  2284. + ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
  2285. + nWritten, nBytes));
  2286. + SetPageError(pg);
  2287. + ClearPageUptodate(pg);
  2288. + } else {
  2289. + SetPageUptodate(pg);
  2290. + }
  2291. +
  2292. + kunmap(pg);
  2293. +
  2294. + T(YAFFS_TRACE_OS,
  2295. + ("yaffs_commit_write returning %d\n",
  2296. + nWritten == nBytes ? 0 : nWritten));
  2297. +
  2298. + return nWritten == nBytes ? 0 : nWritten;
  2299. +}
  2300. +#endif
  2301. +
  2302. +
  2303. +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
  2304. +{
  2305. + if (inode && obj) {
  2306. +
  2307. +
  2308. + /* Check mode against the variant type and attempt to repair if broken. */
  2309. + __u32 mode = obj->yst_mode;
  2310. + switch (obj->variantType) {
  2311. + case YAFFS_OBJECT_TYPE_FILE:
  2312. + if (!S_ISREG(mode)) {
  2313. + obj->yst_mode &= ~S_IFMT;
  2314. + obj->yst_mode |= S_IFREG;
  2315. + }
  2316. +
  2317. + break;
  2318. + case YAFFS_OBJECT_TYPE_SYMLINK:
  2319. + if (!S_ISLNK(mode)) {
  2320. + obj->yst_mode &= ~S_IFMT;
  2321. + obj->yst_mode |= S_IFLNK;
  2322. + }
  2323. +
  2324. + break;
  2325. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  2326. + if (!S_ISDIR(mode)) {
  2327. + obj->yst_mode &= ~S_IFMT;
  2328. + obj->yst_mode |= S_IFDIR;
  2329. + }
  2330. +
  2331. + break;
  2332. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  2333. + case YAFFS_OBJECT_TYPE_HARDLINK:
  2334. + case YAFFS_OBJECT_TYPE_SPECIAL:
  2335. + default:
  2336. + /* TODO? */
  2337. + break;
  2338. + }
  2339. +
  2340. + inode->i_flags |= S_NOATIME;
  2341. +
  2342. + inode->i_ino = obj->objectId;
  2343. + inode->i_mode = obj->yst_mode;
  2344. + inode->i_uid = obj->yst_uid;
  2345. + inode->i_gid = obj->yst_gid;
  2346. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  2347. + inode->i_blksize = inode->i_sb->s_blocksize;
  2348. +#endif
  2349. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2350. +
  2351. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  2352. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  2353. + inode->i_atime.tv_nsec = 0;
  2354. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  2355. + inode->i_mtime.tv_nsec = 0;
  2356. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  2357. + inode->i_ctime.tv_nsec = 0;
  2358. +#else
  2359. + inode->i_rdev = obj->yst_rdev;
  2360. + inode->i_atime = obj->yst_atime;
  2361. + inode->i_mtime = obj->yst_mtime;
  2362. + inode->i_ctime = obj->yst_ctime;
  2363. +#endif
  2364. + inode->i_size = yaffs_GetObjectFileLength(obj);
  2365. + inode->i_blocks = (inode->i_size + 511) >> 9;
  2366. +
  2367. + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
  2368. +
  2369. + T(YAFFS_TRACE_OS,
  2370. + ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
  2371. + inode->i_mode, inode->i_uid, inode->i_gid,
  2372. + (int)inode->i_size, atomic_read(&inode->i_count)));
  2373. +
  2374. + switch (obj->yst_mode & S_IFMT) {
  2375. + default: /* fifo, device or socket */
  2376. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2377. + init_special_inode(inode, obj->yst_mode,
  2378. + old_decode_dev(obj->yst_rdev));
  2379. +#else
  2380. + init_special_inode(inode, obj->yst_mode,
  2381. + (dev_t) (obj->yst_rdev));
  2382. +#endif
  2383. + break;
  2384. + case S_IFREG: /* file */
  2385. + inode->i_op = &yaffs_file_inode_operations;
  2386. + inode->i_fop = &yaffs_file_operations;
  2387. + inode->i_mapping->a_ops =
  2388. + &yaffs_file_address_operations;
  2389. + break;
  2390. + case S_IFDIR: /* directory */
  2391. + inode->i_op = &yaffs_dir_inode_operations;
  2392. + inode->i_fop = &yaffs_dir_operations;
  2393. + break;
  2394. + case S_IFLNK: /* symlink */
  2395. + inode->i_op = &yaffs_symlink_inode_operations;
  2396. + break;
  2397. + }
  2398. +
  2399. + yaffs_InodeToObjectLV(inode) = obj;
  2400. +
  2401. + obj->myInode = inode;
  2402. +
  2403. + } else {
  2404. + T(YAFFS_TRACE_OS,
  2405. + ("yaffs_FileInode invalid parameters\n"));
  2406. + }
  2407. +
  2408. +}
  2409. +
  2410. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  2411. + yaffs_Object *obj)
  2412. +{
  2413. + struct inode *inode;
  2414. +
  2415. + if (!sb) {
  2416. + T(YAFFS_TRACE_OS,
  2417. + ("yaffs_get_inode for NULL super_block!!\n"));
  2418. + return NULL;
  2419. +
  2420. + }
  2421. +
  2422. + if (!obj) {
  2423. + T(YAFFS_TRACE_OS,
  2424. + ("yaffs_get_inode for NULL object!!\n"));
  2425. + return NULL;
  2426. +
  2427. + }
  2428. +
  2429. + T(YAFFS_TRACE_OS,
  2430. + ("yaffs_get_inode for object %d\n", obj->objectId));
  2431. +
  2432. + inode = Y_IGET(sb, obj->objectId);
  2433. + if (IS_ERR(inode))
  2434. + return NULL;
  2435. +
  2436. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  2437. + /* iget also increments the inode's i_count */
  2438. + /* NB You can't be holding grossLock or deadlock will happen! */
  2439. +
  2440. + return inode;
  2441. +}
  2442. +
  2443. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  2444. + loff_t *pos)
  2445. +{
  2446. + yaffs_Object *obj;
  2447. + int nWritten, ipos;
  2448. + struct inode *inode;
  2449. + yaffs_Device *dev;
  2450. +
  2451. + obj = yaffs_DentryToObject(f->f_dentry);
  2452. +
  2453. + dev = obj->myDev;
  2454. +
  2455. + yaffs_GrossLock(dev);
  2456. +
  2457. + inode = f->f_dentry->d_inode;
  2458. +
  2459. + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  2460. + ipos = inode->i_size;
  2461. + else
  2462. + ipos = *pos;
  2463. +
  2464. + if (!obj)
  2465. + T(YAFFS_TRACE_OS,
  2466. + ("yaffs_file_write: hey obj is null!\n"));
  2467. + else
  2468. + T(YAFFS_TRACE_OS,
  2469. + ("yaffs_file_write about to write writing %zu bytes"
  2470. + "to object %d at %d\n",
  2471. + n, obj->objectId, ipos));
  2472. +
  2473. + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
  2474. +
  2475. + T(YAFFS_TRACE_OS,
  2476. + ("yaffs_file_write writing %zu bytes, %d written at %d\n",
  2477. + n, nWritten, ipos));
  2478. +
  2479. + if (nWritten > 0) {
  2480. + ipos += nWritten;
  2481. + *pos = ipos;
  2482. + if (ipos > inode->i_size) {
  2483. + inode->i_size = ipos;
  2484. + inode->i_blocks = (ipos + 511) >> 9;
  2485. +
  2486. + T(YAFFS_TRACE_OS,
  2487. + ("yaffs_file_write size updated to %d bytes, "
  2488. + "%d blocks\n",
  2489. + ipos, (int)(inode->i_blocks)));
  2490. + }
  2491. +
  2492. + }
  2493. + yaffs_GrossUnlock(dev);
  2494. + return nWritten == 0 ? -ENOSPC : nWritten;
  2495. +}
  2496. +
  2497. +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
  2498. +/* For now we just assume few parallel writes and check against a small number. */
  2499. +/* Todo: need to do this with a counter to handle parallel reads better */
  2500. +
  2501. +static ssize_t yaffs_hold_space(struct file *f)
  2502. +{
  2503. + yaffs_Object *obj;
  2504. + yaffs_Device *dev;
  2505. +
  2506. + int nFreeChunks;
  2507. +
  2508. +
  2509. + obj = yaffs_DentryToObject(f->f_dentry);
  2510. +
  2511. + dev = obj->myDev;
  2512. +
  2513. + yaffs_GrossLock(dev);
  2514. +
  2515. + nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
  2516. +
  2517. + yaffs_GrossUnlock(dev);
  2518. +
  2519. + return (nFreeChunks > 20) ? 1 : 0;
  2520. +}
  2521. +
  2522. +static void yaffs_release_space(struct file *f)
  2523. +{
  2524. + yaffs_Object *obj;
  2525. + yaffs_Device *dev;
  2526. +
  2527. +
  2528. + obj = yaffs_DentryToObject(f->f_dentry);
  2529. +
  2530. + dev = obj->myDev;
  2531. +
  2532. + yaffs_GrossLock(dev);
  2533. +
  2534. +
  2535. + yaffs_GrossUnlock(dev);
  2536. +}
  2537. +
  2538. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  2539. +{
  2540. + yaffs_Object *obj;
  2541. + yaffs_Device *dev;
  2542. + struct inode *inode = f->f_dentry->d_inode;
  2543. + unsigned long offset, curoffs;
  2544. + struct ylist_head *i;
  2545. + yaffs_Object *l;
  2546. +
  2547. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  2548. +
  2549. + obj = yaffs_DentryToObject(f->f_dentry);
  2550. + dev = obj->myDev;
  2551. +
  2552. + yaffs_GrossLock(dev);
  2553. +
  2554. + offset = f->f_pos;
  2555. +
  2556. + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
  2557. +
  2558. + if (offset == 0) {
  2559. + T(YAFFS_TRACE_OS,
  2560. + ("yaffs_readdir: entry . ino %d \n",
  2561. + (int)inode->i_ino));
  2562. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
  2563. + goto out;
  2564. + offset++;
  2565. + f->f_pos++;
  2566. + }
  2567. + if (offset == 1) {
  2568. + T(YAFFS_TRACE_OS,
  2569. + ("yaffs_readdir: entry .. ino %d \n",
  2570. + (int)f->f_dentry->d_parent->d_inode->i_ino));
  2571. + if (filldir(dirent, "..", 2, offset,
  2572. + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
  2573. + goto out;
  2574. + offset++;
  2575. + f->f_pos++;
  2576. + }
  2577. +
  2578. + curoffs = 1;
  2579. +
  2580. + /* If the directory has changed since the open or last call to
  2581. + readdir, rewind to after the 2 canned entries. */
  2582. +
  2583. + if (f->f_version != inode->i_version) {
  2584. + offset = 2;
  2585. + f->f_pos = offset;
  2586. + f->f_version = inode->i_version;
  2587. + }
  2588. +
  2589. + ylist_for_each(i, &obj->variant.directoryVariant.children) {
  2590. + curoffs++;
  2591. + if (curoffs >= offset) {
  2592. + l = ylist_entry(i, yaffs_Object, siblings);
  2593. +
  2594. + yaffs_GetObjectName(l, name,
  2595. + YAFFS_MAX_NAME_LENGTH + 1);
  2596. + T(YAFFS_TRACE_OS,
  2597. + ("yaffs_readdir: %s inode %d\n", name,
  2598. + yaffs_GetObjectInode(l)));
  2599. +
  2600. + if (filldir(dirent,
  2601. + name,
  2602. + strlen(name),
  2603. + offset,
  2604. + yaffs_GetObjectInode(l),
  2605. + yaffs_GetObjectType(l)) < 0)
  2606. + goto up_and_out;
  2607. +
  2608. + offset++;
  2609. + f->f_pos++;
  2610. + }
  2611. + }
  2612. +
  2613. +up_and_out:
  2614. +out:
  2615. + yaffs_GrossUnlock(dev);
  2616. +
  2617. + return 0;
  2618. +}
  2619. +
  2620. +/*
  2621. + * File creation. Allocate an inode, and we're done..
  2622. + */
  2623. +
  2624. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  2625. +#define YCRED(x) x
  2626. +#else
  2627. +#define YCRED(x) (x->cred)
  2628. +#endif
  2629. +
  2630. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2631. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  2632. + dev_t rdev)
  2633. +#else
  2634. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  2635. + int rdev)
  2636. +#endif
  2637. +{
  2638. + struct inode *inode;
  2639. +
  2640. + yaffs_Object *obj = NULL;
  2641. + yaffs_Device *dev;
  2642. +
  2643. + yaffs_Object *parent = yaffs_InodeToObject(dir);
  2644. +
  2645. + int error = -ENOSPC;
  2646. + uid_t uid = YCRED(current)->fsuid;
  2647. + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  2648. +
  2649. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  2650. + mode |= S_ISGID;
  2651. +
  2652. + if (parent) {
  2653. + T(YAFFS_TRACE_OS,
  2654. + ("yaffs_mknod: parent object %d type %d\n",
  2655. + parent->objectId, parent->variantType));
  2656. + } else {
  2657. + T(YAFFS_TRACE_OS,
  2658. + ("yaffs_mknod: could not get parent object\n"));
  2659. + return -EPERM;
  2660. + }
  2661. +
  2662. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
  2663. + "mode %x dev %x\n",
  2664. + dentry->d_name.name, mode, rdev));
  2665. +
  2666. + dev = parent->myDev;
  2667. +
  2668. + yaffs_GrossLock(dev);
  2669. +
  2670. + switch (mode & S_IFMT) {
  2671. + default:
  2672. + /* Special (socket, fifo, device...) */
  2673. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
  2674. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2675. + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
  2676. + gid, old_encode_dev(rdev));
  2677. +#else
  2678. + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
  2679. + gid, rdev);
  2680. +#endif
  2681. + break;
  2682. + case S_IFREG: /* file */
  2683. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
  2684. + obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
  2685. + gid);
  2686. + break;
  2687. + case S_IFDIR: /* directory */
  2688. + T(YAFFS_TRACE_OS,
  2689. + ("yaffs_mknod: making directory\n"));
  2690. + obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
  2691. + uid, gid);
  2692. + break;
  2693. + case S_IFLNK: /* symlink */
  2694. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
  2695. + obj = NULL; /* Do we ever get here? */
  2696. + break;
  2697. + }
  2698. +
  2699. + /* Can not call yaffs_get_inode() with gross lock held */
  2700. + yaffs_GrossUnlock(dev);
  2701. +
  2702. + if (obj) {
  2703. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  2704. + d_instantiate(dentry, inode);
  2705. + T(YAFFS_TRACE_OS,
  2706. + ("yaffs_mknod created object %d count = %d\n",
  2707. + obj->objectId, atomic_read(&inode->i_count)));
  2708. + error = 0;
  2709. + } else {
  2710. + T(YAFFS_TRACE_OS,
  2711. + ("yaffs_mknod failed making object\n"));
  2712. + error = -ENOMEM;
  2713. + }
  2714. +
  2715. + return error;
  2716. +}
  2717. +
  2718. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2719. +{
  2720. + int retVal;
  2721. + T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
  2722. + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  2723. + return retVal;
  2724. +}
  2725. +
  2726. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2727. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  2728. + struct nameidata *n)
  2729. +#else
  2730. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  2731. +#endif
  2732. +{
  2733. + T(YAFFS_TRACE_OS, ("yaffs_create\n"));
  2734. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  2735. +}
  2736. +
  2737. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  2738. +{
  2739. + int retVal;
  2740. +
  2741. + yaffs_Device *dev;
  2742. +
  2743. + T(YAFFS_TRACE_OS,
  2744. + ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
  2745. + dentry->d_name.name));
  2746. +
  2747. + dev = yaffs_InodeToObject(dir)->myDev;
  2748. +
  2749. + yaffs_GrossLock(dev);
  2750. +
  2751. + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
  2752. +
  2753. + if (retVal == YAFFS_OK) {
  2754. + dentry->d_inode->i_nlink--;
  2755. + dir->i_version++;
  2756. + yaffs_GrossUnlock(dev);
  2757. + mark_inode_dirty(dentry->d_inode);
  2758. + return 0;
  2759. + }
  2760. + yaffs_GrossUnlock(dev);
  2761. + return -ENOTEMPTY;
  2762. +}
  2763. +
  2764. +/*
  2765. + * Create a link...
  2766. + */
  2767. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  2768. + struct dentry *dentry)
  2769. +{
  2770. + struct inode *inode = old_dentry->d_inode;
  2771. + yaffs_Object *obj = NULL;
  2772. + yaffs_Object *link = NULL;
  2773. + yaffs_Device *dev;
  2774. +
  2775. + T(YAFFS_TRACE_OS, ("yaffs_link\n"));
  2776. +
  2777. + obj = yaffs_InodeToObject(inode);
  2778. + dev = obj->myDev;
  2779. +
  2780. + yaffs_GrossLock(dev);
  2781. +
  2782. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  2783. + link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
  2784. + obj);
  2785. +
  2786. + if (link) {
  2787. + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
  2788. + d_instantiate(dentry, old_dentry->d_inode);
  2789. + atomic_inc(&old_dentry->d_inode->i_count);
  2790. + T(YAFFS_TRACE_OS,
  2791. + ("yaffs_link link count %d i_count %d\n",
  2792. + old_dentry->d_inode->i_nlink,
  2793. + atomic_read(&old_dentry->d_inode->i_count)));
  2794. + }
  2795. +
  2796. + yaffs_GrossUnlock(dev);
  2797. +
  2798. + if (link)
  2799. + return 0;
  2800. +
  2801. + return -EPERM;
  2802. +}
  2803. +
  2804. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  2805. + const char *symname)
  2806. +{
  2807. + yaffs_Object *obj;
  2808. + yaffs_Device *dev;
  2809. + uid_t uid = YCRED(current)->fsuid;
  2810. + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  2811. +
  2812. + T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
  2813. +
  2814. + dev = yaffs_InodeToObject(dir)->myDev;
  2815. + yaffs_GrossLock(dev);
  2816. + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
  2817. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  2818. + yaffs_GrossUnlock(dev);
  2819. +
  2820. + if (obj) {
  2821. + struct inode *inode;
  2822. +
  2823. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  2824. + d_instantiate(dentry, inode);
  2825. + T(YAFFS_TRACE_OS, ("symlink created OK\n"));
  2826. + return 0;
  2827. + } else {
  2828. + T(YAFFS_TRACE_OS, ("symlink not created\n"));
  2829. + }
  2830. +
  2831. + return -ENOMEM;
  2832. +}
  2833. +
  2834. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  2835. + int datasync)
  2836. +{
  2837. +
  2838. + yaffs_Object *obj;
  2839. + yaffs_Device *dev;
  2840. +
  2841. + obj = yaffs_DentryToObject(dentry);
  2842. +
  2843. + dev = obj->myDev;
  2844. +
  2845. + T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
  2846. + yaffs_GrossLock(dev);
  2847. + yaffs_FlushFile(obj, 1);
  2848. + yaffs_GrossUnlock(dev);
  2849. + return 0;
  2850. +}
  2851. +
  2852. +/*
  2853. + * The VFS layer already does all the dentry stuff for rename.
  2854. + *
  2855. + * NB: POSIX says you can rename an object over an old object of the same name
  2856. + */
  2857. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2858. + struct inode *new_dir, struct dentry *new_dentry)
  2859. +{
  2860. + yaffs_Device *dev;
  2861. + int retVal = YAFFS_FAIL;
  2862. + yaffs_Object *target;
  2863. +
  2864. + T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
  2865. + dev = yaffs_InodeToObject(old_dir)->myDev;
  2866. +
  2867. + yaffs_GrossLock(dev);
  2868. +
  2869. + /* Check if the target is an existing directory that is not empty. */
  2870. + target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
  2871. + new_dentry->d_name.name);
  2872. +
  2873. +
  2874. +
  2875. + if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
  2876. + !ylist_empty(&target->variant.directoryVariant.children)) {
  2877. +
  2878. + T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
  2879. +
  2880. + retVal = YAFFS_FAIL;
  2881. + } else {
  2882. + /* Now does unlinking internally using shadowing mechanism */
  2883. + T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
  2884. +
  2885. + retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
  2886. + old_dentry->d_name.name,
  2887. + yaffs_InodeToObject(new_dir),
  2888. + new_dentry->d_name.name);
  2889. + }
  2890. + yaffs_GrossUnlock(dev);
  2891. +
  2892. + if (retVal == YAFFS_OK) {
  2893. + if (target) {
  2894. + new_dentry->d_inode->i_nlink--;
  2895. + mark_inode_dirty(new_dentry->d_inode);
  2896. + }
  2897. +
  2898. + return 0;
  2899. + } else {
  2900. + return -ENOTEMPTY;
  2901. + }
  2902. +}
  2903. +
  2904. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  2905. +{
  2906. + struct inode *inode = dentry->d_inode;
  2907. + int error;
  2908. + yaffs_Device *dev;
  2909. +
  2910. + T(YAFFS_TRACE_OS,
  2911. + ("yaffs_setattr of object %d\n",
  2912. + yaffs_InodeToObject(inode)->objectId));
  2913. +
  2914. + error = inode_change_ok(inode, attr);
  2915. + if (error == 0) {
  2916. + dev = yaffs_InodeToObject(inode)->myDev;
  2917. + yaffs_GrossLock(dev);
  2918. + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
  2919. + YAFFS_OK) {
  2920. + error = 0;
  2921. + } else {
  2922. + error = -EPERM;
  2923. + }
  2924. + yaffs_GrossUnlock(dev);
  2925. + if (!error)
  2926. + error = inode_setattr(inode, attr);
  2927. + }
  2928. + return error;
  2929. +}
  2930. +
  2931. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  2932. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  2933. +{
  2934. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  2935. + struct super_block *sb = dentry->d_sb;
  2936. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2937. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  2938. +{
  2939. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  2940. +#else
  2941. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  2942. +{
  2943. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  2944. +#endif
  2945. +
  2946. + T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
  2947. +
  2948. + yaffs_GrossLock(dev);
  2949. +
  2950. + buf->f_type = YAFFS_MAGIC;
  2951. + buf->f_bsize = sb->s_blocksize;
  2952. + buf->f_namelen = 255;
  2953. +
  2954. + if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
  2955. + /* Do this if chunk size is not a power of 2 */
  2956. +
  2957. + uint64_t bytesInDev;
  2958. + uint64_t bytesFree;
  2959. +
  2960. + bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
  2961. + ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
  2962. +
  2963. + do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
  2964. + buf->f_blocks = bytesInDev;
  2965. +
  2966. + bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
  2967. + ((uint64_t)(dev->nDataBytesPerChunk));
  2968. +
  2969. + do_div(bytesFree, sb->s_blocksize);
  2970. +
  2971. + buf->f_bfree = bytesFree;
  2972. +
  2973. + } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
  2974. +
  2975. + buf->f_blocks =
  2976. + (dev->endBlock - dev->startBlock + 1) *
  2977. + dev->nChunksPerBlock /
  2978. + (sb->s_blocksize / dev->nDataBytesPerChunk);
  2979. + buf->f_bfree =
  2980. + yaffs_GetNumberOfFreeChunks(dev) /
  2981. + (sb->s_blocksize / dev->nDataBytesPerChunk);
  2982. + } else {
  2983. + buf->f_blocks =
  2984. + (dev->endBlock - dev->startBlock + 1) *
  2985. + dev->nChunksPerBlock *
  2986. + (dev->nDataBytesPerChunk / sb->s_blocksize);
  2987. +
  2988. + buf->f_bfree =
  2989. + yaffs_GetNumberOfFreeChunks(dev) *
  2990. + (dev->nDataBytesPerChunk / sb->s_blocksize);
  2991. + }
  2992. +
  2993. + buf->f_files = 0;
  2994. + buf->f_ffree = 0;
  2995. + buf->f_bavail = buf->f_bfree;
  2996. +
  2997. + yaffs_GrossUnlock(dev);
  2998. + return 0;
  2999. +}
  3000. +
  3001. +
  3002. +static int yaffs_do_sync_fs(struct super_block *sb)
  3003. +{
  3004. +
  3005. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3006. + T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
  3007. +
  3008. + if (sb->s_dirt) {
  3009. + yaffs_GrossLock(dev);
  3010. +
  3011. + if (dev) {
  3012. + yaffs_FlushEntireDeviceCache(dev);
  3013. + yaffs_CheckpointSave(dev);
  3014. + }
  3015. +
  3016. + yaffs_GrossUnlock(dev);
  3017. +
  3018. + sb->s_dirt = 0;
  3019. + }
  3020. + return 0;
  3021. +}
  3022. +
  3023. +
  3024. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3025. +static void yaffs_write_super(struct super_block *sb)
  3026. +#else
  3027. +static int yaffs_write_super(struct super_block *sb)
  3028. +#endif
  3029. +{
  3030. +
  3031. + T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
  3032. + if (yaffs_auto_checkpoint >= 2)
  3033. + yaffs_do_sync_fs(sb);
  3034. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  3035. + return 0;
  3036. +#endif
  3037. +}
  3038. +
  3039. +
  3040. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3041. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  3042. +#else
  3043. +static int yaffs_sync_fs(struct super_block *sb)
  3044. +#endif
  3045. +{
  3046. + T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
  3047. +
  3048. + if (yaffs_auto_checkpoint >= 1)
  3049. + yaffs_do_sync_fs(sb);
  3050. +
  3051. + return 0;
  3052. +}
  3053. +
  3054. +#ifdef YAFFS_USE_OWN_IGET
  3055. +
  3056. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  3057. +{
  3058. + struct inode *inode;
  3059. + yaffs_Object *obj;
  3060. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3061. +
  3062. + T(YAFFS_TRACE_OS,
  3063. + ("yaffs_iget for %lu\n", ino));
  3064. +
  3065. + inode = iget_locked(sb, ino);
  3066. + if (!inode)
  3067. + return ERR_PTR(-ENOMEM);
  3068. + if (!(inode->i_state & I_NEW))
  3069. + return inode;
  3070. +
  3071. + /* NB This is called as a side effect of other functions, but
  3072. + * we had to release the lock to prevent deadlocks, so
  3073. + * need to lock again.
  3074. + */
  3075. +
  3076. + yaffs_GrossLock(dev);
  3077. +
  3078. + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
  3079. +
  3080. + yaffs_FillInodeFromObject(inode, obj);
  3081. +
  3082. + yaffs_GrossUnlock(dev);
  3083. +
  3084. + unlock_new_inode(inode);
  3085. + return inode;
  3086. +}
  3087. +
  3088. +#else
  3089. +
  3090. +static void yaffs_read_inode(struct inode *inode)
  3091. +{
  3092. + /* NB This is called as a side effect of other functions, but
  3093. + * we had to release the lock to prevent deadlocks, so
  3094. + * need to lock again.
  3095. + */
  3096. +
  3097. + yaffs_Object *obj;
  3098. + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
  3099. +
  3100. + T(YAFFS_TRACE_OS,
  3101. + ("yaffs_read_inode for %d\n", (int)inode->i_ino));
  3102. +
  3103. + yaffs_GrossLock(dev);
  3104. +
  3105. + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
  3106. +
  3107. + yaffs_FillInodeFromObject(inode, obj);
  3108. +
  3109. + yaffs_GrossUnlock(dev);
  3110. +}
  3111. +
  3112. +#endif
  3113. +
  3114. +static YLIST_HEAD(yaffs_dev_list);
  3115. +
  3116. +#if 0 /* not used */
  3117. +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
  3118. +{
  3119. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3120. +
  3121. + if (*flags & MS_RDONLY) {
  3122. + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
  3123. +
  3124. + T(YAFFS_TRACE_OS,
  3125. + ("yaffs_remount_fs: %s: RO\n", dev->name));
  3126. +
  3127. + yaffs_GrossLock(dev);
  3128. +
  3129. + yaffs_FlushEntireDeviceCache(dev);
  3130. +
  3131. + yaffs_CheckpointSave(dev);
  3132. +
  3133. + if (mtd->sync)
  3134. + mtd->sync(mtd);
  3135. +
  3136. + yaffs_GrossUnlock(dev);
  3137. + } else {
  3138. + T(YAFFS_TRACE_OS,
  3139. + ("yaffs_remount_fs: %s: RW\n", dev->name));
  3140. + }
  3141. +
  3142. + return 0;
  3143. +}
  3144. +#endif
  3145. +
  3146. +static void yaffs_put_super(struct super_block *sb)
  3147. +{
  3148. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3149. +
  3150. + T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
  3151. +
  3152. + yaffs_GrossLock(dev);
  3153. +
  3154. + yaffs_FlushEntireDeviceCache(dev);
  3155. +
  3156. + yaffs_CheckpointSave(dev);
  3157. +
  3158. + if (dev->putSuperFunc)
  3159. + dev->putSuperFunc(sb);
  3160. +
  3161. + yaffs_Deinitialise(dev);
  3162. +
  3163. + yaffs_GrossUnlock(dev);
  3164. +
  3165. + /* we assume this is protected by lock_kernel() in mount/umount */
  3166. + ylist_del(&dev->devList);
  3167. +
  3168. + if (dev->spareBuffer) {
  3169. + YFREE(dev->spareBuffer);
  3170. + dev->spareBuffer = NULL;
  3171. + }
  3172. +
  3173. + kfree(dev);
  3174. +}
  3175. +
  3176. +
  3177. +static void yaffs_MTDPutSuper(struct super_block *sb)
  3178. +{
  3179. + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
  3180. +
  3181. + if (mtd->sync)
  3182. + mtd->sync(mtd);
  3183. +
  3184. + put_mtd_device(mtd);
  3185. +}
  3186. +
  3187. +
  3188. +static void yaffs_MarkSuperBlockDirty(void *vsb)
  3189. +{
  3190. + struct super_block *sb = (struct super_block *)vsb;
  3191. +
  3192. + T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
  3193. + if (sb)
  3194. + sb->s_dirt = 1;
  3195. +}
  3196. +
  3197. +typedef struct {
  3198. + int inband_tags;
  3199. + int skip_checkpoint_read;
  3200. + int skip_checkpoint_write;
  3201. + int no_cache;
  3202. +} yaffs_options;
  3203. +
  3204. +#define MAX_OPT_LEN 20
  3205. +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
  3206. +{
  3207. + char cur_opt[MAX_OPT_LEN + 1];
  3208. + int p;
  3209. + int error = 0;
  3210. +
  3211. + /* Parse through the options which is a comma seperated list */
  3212. +
  3213. + while (options_str && *options_str && !error) {
  3214. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  3215. + p = 0;
  3216. +
  3217. + while (*options_str && *options_str != ',') {
  3218. + if (p < MAX_OPT_LEN) {
  3219. + cur_opt[p] = *options_str;
  3220. + p++;
  3221. + }
  3222. + options_str++;
  3223. + }
  3224. +
  3225. + if (!strcmp(cur_opt, "inband-tags"))
  3226. + options->inband_tags = 1;
  3227. + else if (!strcmp(cur_opt, "no-cache"))
  3228. + options->no_cache = 1;
  3229. + else if (!strcmp(cur_opt, "no-checkpoint-read"))
  3230. + options->skip_checkpoint_read = 1;
  3231. + else if (!strcmp(cur_opt, "no-checkpoint-write"))
  3232. + options->skip_checkpoint_write = 1;
  3233. + else if (!strcmp(cur_opt, "no-checkpoint")) {
  3234. + options->skip_checkpoint_read = 1;
  3235. + options->skip_checkpoint_write = 1;
  3236. + } else {
  3237. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  3238. + cur_opt);
  3239. + error = 1;
  3240. + }
  3241. + }
  3242. +
  3243. + return error;
  3244. +}
  3245. +
  3246. +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
  3247. + struct super_block *sb,
  3248. + void *data, int silent)
  3249. +{
  3250. + int nBlocks;
  3251. + struct inode *inode = NULL;
  3252. + struct dentry *root;
  3253. + yaffs_Device *dev = 0;
  3254. + char devname_buf[BDEVNAME_SIZE + 1];
  3255. + struct mtd_info *mtd;
  3256. + int err;
  3257. + char *data_str = (char *)data;
  3258. +
  3259. + yaffs_options options;
  3260. +
  3261. + sb->s_magic = YAFFS_MAGIC;
  3262. + sb->s_op = &yaffs_super_ops;
  3263. + sb->s_flags |= MS_NOATIME;
  3264. +
  3265. + if (!sb)
  3266. + printk(KERN_INFO "yaffs: sb is NULL\n");
  3267. + else if (!sb->s_dev)
  3268. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  3269. + else if (!yaffs_devname(sb, devname_buf))
  3270. + printk(KERN_INFO "yaffs: devname is NULL\n");
  3271. + else
  3272. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
  3273. + sb->s_dev,
  3274. + yaffs_devname(sb, devname_buf));
  3275. +
  3276. + if (!data_str)
  3277. + data_str = "";
  3278. +
  3279. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  3280. +
  3281. + memset(&options, 0, sizeof(options));
  3282. +
  3283. + if (yaffs_parse_options(&options, data_str)) {
  3284. + /* Option parsing failed */
  3285. + return NULL;
  3286. + }
  3287. +
  3288. +
  3289. + sb->s_blocksize = PAGE_CACHE_SIZE;
  3290. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  3291. + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
  3292. + T(YAFFS_TRACE_OS,
  3293. + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
  3294. +
  3295. +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
  3296. + T(YAFFS_TRACE_OS,
  3297. + ("yaffs: Write verification disabled. All guarantees "
  3298. + "null and void\n"));
  3299. +#endif
  3300. +
  3301. + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
  3302. + "\"%s\"\n",
  3303. + MAJOR(sb->s_dev), MINOR(sb->s_dev),
  3304. + yaffs_devname(sb, devname_buf)));
  3305. +
  3306. + /* Check it's an mtd device..... */
  3307. + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
  3308. + return NULL; /* This isn't an mtd device */
  3309. +
  3310. + /* Get the device */
  3311. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  3312. + if (!mtd) {
  3313. + T(YAFFS_TRACE_ALWAYS,
  3314. + ("yaffs: MTD device #%u doesn't appear to exist\n",
  3315. + MINOR(sb->s_dev)));
  3316. + return NULL;
  3317. + }
  3318. + /* Check it's NAND */
  3319. + if (mtd->type != MTD_NANDFLASH) {
  3320. + T(YAFFS_TRACE_ALWAYS,
  3321. + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
  3322. + return NULL;
  3323. + }
  3324. +
  3325. + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
  3326. + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
  3327. + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
  3328. + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
  3329. + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
  3330. + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
  3331. + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
  3332. + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
  3333. + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
  3334. + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
  3335. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  3336. + T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
  3337. +#else
  3338. + T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
  3339. +#endif
  3340. +
  3341. +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
  3342. +
  3343. + if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
  3344. + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
  3345. + yaffsVersion = 2;
  3346. + }
  3347. +
  3348. + /* Added NCB 26/5/2006 for completeness */
  3349. + if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
  3350. + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
  3351. + yaffsVersion = 1;
  3352. + }
  3353. +
  3354. +#endif
  3355. +
  3356. + if (yaffsVersion == 2) {
  3357. + /* Check for version 2 style functions */
  3358. + if (!mtd->erase ||
  3359. + !mtd->block_isbad ||
  3360. + !mtd->block_markbad ||
  3361. + !mtd->read ||
  3362. + !mtd->write ||
  3363. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3364. + !mtd->read_oob || !mtd->write_oob) {
  3365. +#else
  3366. + !mtd->write_ecc ||
  3367. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  3368. +#endif
  3369. + T(YAFFS_TRACE_ALWAYS,
  3370. + ("yaffs: MTD device does not support required "
  3371. + "functions\n"));;
  3372. + return NULL;
  3373. + }
  3374. +
  3375. + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  3376. + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  3377. + !options.inband_tags) {
  3378. + T(YAFFS_TRACE_ALWAYS,
  3379. + ("yaffs: MTD device does not have the "
  3380. + "right page sizes\n"));
  3381. + return NULL;
  3382. + }
  3383. + } else {
  3384. + /* Check for V1 style functions */
  3385. + if (!mtd->erase ||
  3386. + !mtd->read ||
  3387. + !mtd->write ||
  3388. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3389. + !mtd->read_oob || !mtd->write_oob) {
  3390. +#else
  3391. + !mtd->write_ecc ||
  3392. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  3393. +#endif
  3394. + T(YAFFS_TRACE_ALWAYS,
  3395. + ("yaffs: MTD device does not support required "
  3396. + "functions\n"));;
  3397. + return NULL;
  3398. + }
  3399. +
  3400. + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  3401. + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  3402. + T(YAFFS_TRACE_ALWAYS,
  3403. + ("yaffs: MTD device does not support have the "
  3404. + "right page sizes\n"));
  3405. + return NULL;
  3406. + }
  3407. + }
  3408. +
  3409. + /* OK, so if we got here, we have an MTD that's NAND and looks
  3410. + * like it has the right capabilities
  3411. + * Set the yaffs_Device up for mtd
  3412. + */
  3413. +
  3414. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3415. + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
  3416. +#else
  3417. + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
  3418. +#endif
  3419. + if (!dev) {
  3420. + /* Deep shit could not allocate device structure */
  3421. + T(YAFFS_TRACE_ALWAYS,
  3422. + ("yaffs_read_super: Failed trying to allocate "
  3423. + "yaffs_Device. \n"));
  3424. + return NULL;
  3425. + }
  3426. +
  3427. + memset(dev, 0, sizeof(yaffs_Device));
  3428. + dev->genericDevice = mtd;
  3429. + dev->name = mtd->name;
  3430. +
  3431. + /* Set up the memory size parameters.... */
  3432. +
  3433. + nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
  3434. +
  3435. + dev->startBlock = 0;
  3436. + dev->endBlock = nBlocks - 1;
  3437. + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
  3438. + dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
  3439. + dev->nReservedBlocks = 5;
  3440. + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
  3441. + dev->inbandTags = options.inband_tags;
  3442. +
  3443. + /* ... and the functions. */
  3444. + if (yaffsVersion == 2) {
  3445. + dev->writeChunkWithTagsToNAND =
  3446. + nandmtd2_WriteChunkWithTagsToNAND;
  3447. + dev->readChunkWithTagsFromNAND =
  3448. + nandmtd2_ReadChunkWithTagsFromNAND;
  3449. + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
  3450. + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
  3451. + dev->spareBuffer = YMALLOC(mtd->oobsize);
  3452. + dev->isYaffs2 = 1;
  3453. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3454. + dev->totalBytesPerChunk = mtd->writesize;
  3455. + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
  3456. +#else
  3457. + dev->totalBytesPerChunk = mtd->oobblock;
  3458. + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
  3459. +#endif
  3460. + nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  3461. +
  3462. + dev->startBlock = 0;
  3463. + dev->endBlock = nBlocks - 1;
  3464. + } else {
  3465. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3466. + /* use the MTD interface in yaffs_mtdif1.c */
  3467. + dev->writeChunkWithTagsToNAND =
  3468. + nandmtd1_WriteChunkWithTagsToNAND;
  3469. + dev->readChunkWithTagsFromNAND =
  3470. + nandmtd1_ReadChunkWithTagsFromNAND;
  3471. + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
  3472. + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
  3473. +#else
  3474. + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
  3475. + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
  3476. +#endif
  3477. + dev->isYaffs2 = 0;
  3478. + }
  3479. + /* ... and common functions */
  3480. + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
  3481. + dev->initialiseNAND = nandmtd_InitialiseNAND;
  3482. +
  3483. + dev->putSuperFunc = yaffs_MTDPutSuper;
  3484. +
  3485. + dev->superBlock = (void *)sb;
  3486. + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
  3487. +
  3488. +
  3489. +#ifndef CONFIG_YAFFS_DOES_ECC
  3490. + dev->useNANDECC = 1;
  3491. +#endif
  3492. +
  3493. +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
  3494. + dev->wideTnodesDisabled = 1;
  3495. +#endif
  3496. +
  3497. + dev->skipCheckpointRead = options.skip_checkpoint_read;
  3498. + dev->skipCheckpointWrite = options.skip_checkpoint_write;
  3499. +
  3500. + /* we assume this is protected by lock_kernel() in mount/umount */
  3501. + ylist_add_tail(&dev->devList, &yaffs_dev_list);
  3502. +
  3503. + init_MUTEX(&dev->grossLock);
  3504. +
  3505. + yaffs_GrossLock(dev);
  3506. +
  3507. + err = yaffs_GutsInitialise(dev);
  3508. +
  3509. + T(YAFFS_TRACE_OS,
  3510. + ("yaffs_read_super: guts initialised %s\n",
  3511. + (err == YAFFS_OK) ? "OK" : "FAILED"));
  3512. +
  3513. + /* Release lock before yaffs_get_inode() */
  3514. + yaffs_GrossUnlock(dev);
  3515. +
  3516. + /* Create root inode */
  3517. + if (err == YAFFS_OK)
  3518. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
  3519. + yaffs_Root(dev));
  3520. +
  3521. + if (!inode)
  3522. + return NULL;
  3523. +
  3524. + inode->i_op = &yaffs_dir_inode_operations;
  3525. + inode->i_fop = &yaffs_dir_operations;
  3526. +
  3527. + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
  3528. +
  3529. + root = d_alloc_root(inode);
  3530. +
  3531. + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
  3532. +
  3533. + if (!root) {
  3534. + iput(inode);
  3535. + return NULL;
  3536. + }
  3537. + sb->s_root = root;
  3538. + sb->s_dirt = !dev->isCheckpointed;
  3539. + T(YAFFS_TRACE_ALWAYS,
  3540. + ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
  3541. +
  3542. + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
  3543. + return sb;
  3544. +}
  3545. +
  3546. +
  3547. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3548. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  3549. + int silent)
  3550. +{
  3551. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  3552. +}
  3553. +
  3554. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3555. +static int yaffs_read_super(struct file_system_type *fs,
  3556. + int flags, const char *dev_name,
  3557. + void *data, struct vfsmount *mnt)
  3558. +{
  3559. +
  3560. + return get_sb_bdev(fs, flags, dev_name, data,
  3561. + yaffs_internal_read_super_mtd, mnt);
  3562. +}
  3563. +#else
  3564. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  3565. + int flags, const char *dev_name,
  3566. + void *data)
  3567. +{
  3568. +
  3569. + return get_sb_bdev(fs, flags, dev_name, data,
  3570. + yaffs_internal_read_super_mtd);
  3571. +}
  3572. +#endif
  3573. +
  3574. +static struct file_system_type yaffs_fs_type = {
  3575. + .owner = THIS_MODULE,
  3576. + .name = "yaffs",
  3577. + .get_sb = yaffs_read_super,
  3578. + .kill_sb = kill_block_super,
  3579. + .fs_flags = FS_REQUIRES_DEV,
  3580. +};
  3581. +#else
  3582. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  3583. + int silent)
  3584. +{
  3585. + return yaffs_internal_read_super(1, sb, data, silent);
  3586. +}
  3587. +
  3588. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  3589. + FS_REQUIRES_DEV);
  3590. +#endif
  3591. +
  3592. +
  3593. +#ifdef CONFIG_YAFFS_YAFFS2
  3594. +
  3595. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3596. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  3597. + int silent)
  3598. +{
  3599. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  3600. +}
  3601. +
  3602. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3603. +static int yaffs2_read_super(struct file_system_type *fs,
  3604. + int flags, const char *dev_name, void *data,
  3605. + struct vfsmount *mnt)
  3606. +{
  3607. + return get_sb_bdev(fs, flags, dev_name, data,
  3608. + yaffs2_internal_read_super_mtd, mnt);
  3609. +}
  3610. +#else
  3611. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  3612. + int flags, const char *dev_name,
  3613. + void *data)
  3614. +{
  3615. +
  3616. + return get_sb_bdev(fs, flags, dev_name, data,
  3617. + yaffs2_internal_read_super_mtd);
  3618. +}
  3619. +#endif
  3620. +
  3621. +static struct file_system_type yaffs2_fs_type = {
  3622. + .owner = THIS_MODULE,
  3623. + .name = "yaffs2",
  3624. + .get_sb = yaffs2_read_super,
  3625. + .kill_sb = kill_block_super,
  3626. + .fs_flags = FS_REQUIRES_DEV,
  3627. +};
  3628. +#else
  3629. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  3630. + void *data, int silent)
  3631. +{
  3632. + return yaffs_internal_read_super(2, sb, data, silent);
  3633. +}
  3634. +
  3635. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  3636. + FS_REQUIRES_DEV);
  3637. +#endif
  3638. +
  3639. +#endif /* CONFIG_YAFFS_YAFFS2 */
  3640. +
  3641. +static struct proc_dir_entry *my_proc_entry;
  3642. +
  3643. +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
  3644. +{
  3645. + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
  3646. + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
  3647. + buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
  3648. + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
  3649. + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
  3650. + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
  3651. + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
  3652. + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
  3653. + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
  3654. + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
  3655. + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
  3656. + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
  3657. + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
  3658. + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
  3659. + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
  3660. + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
  3661. + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
  3662. + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
  3663. + buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
  3664. + buf += sprintf(buf, "passiveGCs......... %d\n",
  3665. + dev->passiveGarbageCollections);
  3666. + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
  3667. + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
  3668. + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
  3669. + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
  3670. + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
  3671. + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
  3672. + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
  3673. + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
  3674. + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
  3675. + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
  3676. + buf +=
  3677. + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
  3678. + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
  3679. + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
  3680. + buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
  3681. +
  3682. + return buf;
  3683. +}
  3684. +
  3685. +static int yaffs_proc_read(char *page,
  3686. + char **start,
  3687. + off_t offset, int count, int *eof, void *data)
  3688. +{
  3689. + struct ylist_head *item;
  3690. + char *buf = page;
  3691. + int step = offset;
  3692. + int n = 0;
  3693. +
  3694. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  3695. + * We use 'offset' (*ppos) to indicate where we are in devList.
  3696. + * This also assumes the user has posted a read buffer large
  3697. + * enough to hold the complete output; but that's life in /proc.
  3698. + */
  3699. +
  3700. + *(int *)start = 1;
  3701. +
  3702. + /* Print header first */
  3703. + if (step == 0) {
  3704. + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
  3705. + "\n%s\n%s\n", yaffs_fs_c_version,
  3706. + yaffs_guts_c_version);
  3707. + }
  3708. +
  3709. + /* hold lock_kernel while traversing yaffs_dev_list */
  3710. + lock_kernel();
  3711. +
  3712. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  3713. + ylist_for_each(item, &yaffs_dev_list) {
  3714. + yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
  3715. + if (n < step) {
  3716. + n++;
  3717. + continue;
  3718. + }
  3719. + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
  3720. + buf = yaffs_dump_dev(buf, dev);
  3721. + break;
  3722. + }
  3723. + unlock_kernel();
  3724. +
  3725. + return buf - page < count ? buf - page : count;
  3726. +}
  3727. +
  3728. +/**
  3729. + * Set the verbosity of the warnings and error messages.
  3730. + *
  3731. + * Note that the names can only be a..z or _ with the current code.
  3732. + */
  3733. +
  3734. +static struct {
  3735. + char *mask_name;
  3736. + unsigned mask_bitfield;
  3737. +} mask_flags[] = {
  3738. + {"allocate", YAFFS_TRACE_ALLOCATE},
  3739. + {"always", YAFFS_TRACE_ALWAYS},
  3740. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  3741. + {"buffers", YAFFS_TRACE_BUFFERS},
  3742. + {"bug", YAFFS_TRACE_BUG},
  3743. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  3744. + {"deletion", YAFFS_TRACE_DELETION},
  3745. + {"erase", YAFFS_TRACE_ERASE},
  3746. + {"error", YAFFS_TRACE_ERROR},
  3747. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  3748. + {"gc", YAFFS_TRACE_GC},
  3749. + {"mtd", YAFFS_TRACE_MTD},
  3750. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  3751. + {"os", YAFFS_TRACE_OS},
  3752. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  3753. + {"scan", YAFFS_TRACE_SCAN},
  3754. + {"tracing", YAFFS_TRACE_TRACING},
  3755. +
  3756. + {"verify", YAFFS_TRACE_VERIFY},
  3757. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  3758. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  3759. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  3760. +
  3761. + {"write", YAFFS_TRACE_WRITE},
  3762. + {"all", 0xffffffff},
  3763. + {"none", 0},
  3764. + {NULL, 0},
  3765. +};
  3766. +
  3767. +#define MAX_MASK_NAME_LENGTH 40
  3768. +static int yaffs_proc_write(struct file *file, const char *buf,
  3769. + unsigned long count, void *data)
  3770. +{
  3771. + unsigned rg = 0, mask_bitfield;
  3772. + char *end;
  3773. + char *mask_name;
  3774. + const char *x;
  3775. + char substring[MAX_MASK_NAME_LENGTH + 1];
  3776. + int i;
  3777. + int done = 0;
  3778. + int add, len = 0;
  3779. + int pos = 0;
  3780. +
  3781. + rg = yaffs_traceMask;
  3782. +
  3783. + while (!done && (pos < count)) {
  3784. + done = 1;
  3785. + while ((pos < count) && isspace(buf[pos]))
  3786. + pos++;
  3787. +
  3788. + switch (buf[pos]) {
  3789. + case '+':
  3790. + case '-':
  3791. + case '=':
  3792. + add = buf[pos];
  3793. + pos++;
  3794. + break;
  3795. +
  3796. + default:
  3797. + add = ' ';
  3798. + break;
  3799. + }
  3800. + mask_name = NULL;
  3801. +
  3802. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  3803. +
  3804. + if (end > buf + pos) {
  3805. + mask_name = "numeral";
  3806. + len = end - (buf + pos);
  3807. + pos += len;
  3808. + done = 0;
  3809. + } else {
  3810. + for (x = buf + pos, i = 0;
  3811. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  3812. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  3813. + substring[i] = *x;
  3814. + substring[i] = '\0';
  3815. +
  3816. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  3817. + if (strcmp(substring, mask_flags[i].mask_name) == 0) {
  3818. + mask_name = mask_flags[i].mask_name;
  3819. + mask_bitfield = mask_flags[i].mask_bitfield;
  3820. + done = 0;
  3821. + break;
  3822. + }
  3823. + }
  3824. + }
  3825. +
  3826. + if (mask_name != NULL) {
  3827. + done = 0;
  3828. + switch (add) {
  3829. + case '-':
  3830. + rg &= ~mask_bitfield;
  3831. + break;
  3832. + case '+':
  3833. + rg |= mask_bitfield;
  3834. + break;
  3835. + case '=':
  3836. + rg = mask_bitfield;
  3837. + break;
  3838. + default:
  3839. + rg |= mask_bitfield;
  3840. + break;
  3841. + }
  3842. + }
  3843. + }
  3844. +
  3845. + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
  3846. +
  3847. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
  3848. +
  3849. + if (rg & YAFFS_TRACE_ALWAYS) {
  3850. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  3851. + char flag;
  3852. + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
  3853. + printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
  3854. + }
  3855. + }
  3856. +
  3857. + return count;
  3858. +}
  3859. +
  3860. +/* Stuff to handle installation of file systems */
  3861. +struct file_system_to_install {
  3862. + struct file_system_type *fst;
  3863. + int installed;
  3864. +};
  3865. +
  3866. +static struct file_system_to_install fs_to_install[] = {
  3867. + {&yaffs_fs_type, 0},
  3868. + {&yaffs2_fs_type, 0},
  3869. + {NULL, 0}
  3870. +};
  3871. +
  3872. +static int __init init_yaffs_fs(void)
  3873. +{
  3874. + int error = 0;
  3875. + struct file_system_to_install *fsinst;
  3876. +
  3877. + T(YAFFS_TRACE_ALWAYS,
  3878. + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
  3879. +
  3880. + /* Install the proc_fs entry */
  3881. + my_proc_entry = create_proc_entry("yaffs",
  3882. + S_IRUGO | S_IFREG,
  3883. + YPROC_ROOT);
  3884. +
  3885. + if (my_proc_entry) {
  3886. + my_proc_entry->write_proc = yaffs_proc_write;
  3887. + my_proc_entry->read_proc = yaffs_proc_read;
  3888. + my_proc_entry->data = NULL;
  3889. + } else
  3890. + return -ENOMEM;
  3891. +
  3892. + /* Now add the file system entries */
  3893. +
  3894. + fsinst = fs_to_install;
  3895. +
  3896. + while (fsinst->fst && !error) {
  3897. + error = register_filesystem(fsinst->fst);
  3898. + if (!error)
  3899. + fsinst->installed = 1;
  3900. + fsinst++;
  3901. + }
  3902. +
  3903. + /* Any errors? uninstall */
  3904. + if (error) {
  3905. + fsinst = fs_to_install;
  3906. +
  3907. + while (fsinst->fst) {
  3908. + if (fsinst->installed) {
  3909. + unregister_filesystem(fsinst->fst);
  3910. + fsinst->installed = 0;
  3911. + }
  3912. + fsinst++;
  3913. + }
  3914. + }
  3915. +
  3916. + return error;
  3917. +}
  3918. +
  3919. +static void __exit exit_yaffs_fs(void)
  3920. +{
  3921. +
  3922. + struct file_system_to_install *fsinst;
  3923. +
  3924. + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
  3925. + " removing. \n"));
  3926. +
  3927. + remove_proc_entry("yaffs", YPROC_ROOT);
  3928. +
  3929. + fsinst = fs_to_install;
  3930. +
  3931. + while (fsinst->fst) {
  3932. + if (fsinst->installed) {
  3933. + unregister_filesystem(fsinst->fst);
  3934. + fsinst->installed = 0;
  3935. + }
  3936. + fsinst++;
  3937. + }
  3938. +}
  3939. +
  3940. +module_init(init_yaffs_fs)
  3941. +module_exit(exit_yaffs_fs)
  3942. +
  3943. +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  3944. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
  3945. +MODULE_LICENSE("GPL");
  3946. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_getblockinfo.h linux-2.6.34/fs/yaffs2/yaffs_getblockinfo.h
  3947. --- linux-2.6.34.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  3948. +++ linux-2.6.34/fs/yaffs2/yaffs_getblockinfo.h 2010-05-20 20:16:22.281614392 +0200
  3949. @@ -0,0 +1,34 @@
  3950. +/*
  3951. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  3952. + *
  3953. + * Copyright (C) 2002-2007 Aleph One Ltd.
  3954. + * for Toby Churchill Ltd and Brightstar Engineering
  3955. + *
  3956. + * Created by Charles Manning <charles@aleph1.co.uk>
  3957. + *
  3958. + * This program is free software; you can redistribute it and/or modify
  3959. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  3960. + * published by the Free Software Foundation.
  3961. + *
  3962. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  3963. + */
  3964. +
  3965. +#ifndef __YAFFS_GETBLOCKINFO_H__
  3966. +#define __YAFFS_GETBLOCKINFO_H__
  3967. +
  3968. +#include "yaffs_guts.h"
  3969. +
  3970. +/* Function to manipulate block info */
  3971. +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
  3972. +{
  3973. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  3974. + T(YAFFS_TRACE_ERROR,
  3975. + (TSTR
  3976. + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
  3977. + blk));
  3978. + YBUG();
  3979. + }
  3980. + return &dev->blockInfo[blk - dev->internalStartBlock];
  3981. +}
  3982. +
  3983. +#endif
  3984. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_guts.c linux-2.6.34/fs/yaffs2/yaffs_guts.c
  3985. --- linux-2.6.34.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  3986. +++ linux-2.6.34/fs/yaffs2/yaffs_guts.c 2010-05-20 20:16:22.301615823 +0200
  3987. @@ -0,0 +1,7552 @@
  3988. +/*
  3989. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3990. + *
  3991. + * Copyright (C) 2002-2007 Aleph One Ltd.
  3992. + * for Toby Churchill Ltd and Brightstar Engineering
  3993. + *
  3994. + * Created by Charles Manning <charles@aleph1.co.uk>
  3995. + *
  3996. + * This program is free software; you can redistribute it and/or modify
  3997. + * it under the terms of the GNU General Public License version 2 as
  3998. + * published by the Free Software Foundation.
  3999. + */
  4000. +
  4001. +const char *yaffs_guts_c_version =
  4002. + "$Id: yaffs_guts.c,v 1.82 2009-03-09 04:24:17 charles Exp $";
  4003. +
  4004. +#include "yportenv.h"
  4005. +
  4006. +#include "yaffsinterface.h"
  4007. +#include "yaffs_guts.h"
  4008. +#include "yaffs_tagsvalidity.h"
  4009. +#include "yaffs_getblockinfo.h"
  4010. +
  4011. +#include "yaffs_tagscompat.h"
  4012. +#ifndef CONFIG_YAFFS_USE_OWN_SORT
  4013. +#include "yaffs_qsort.h"
  4014. +#endif
  4015. +#include "yaffs_nand.h"
  4016. +
  4017. +#include "yaffs_checkptrw.h"
  4018. +
  4019. +#include "yaffs_nand.h"
  4020. +#include "yaffs_packedtags2.h"
  4021. +
  4022. +
  4023. +#define YAFFS_PASSIVE_GC_CHUNKS 2
  4024. +
  4025. +#include "yaffs_ecc.h"
  4026. +
  4027. +
  4028. +/* Robustification (if it ever comes about...) */
  4029. +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
  4030. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
  4031. + int erasedOk);
  4032. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  4033. + const __u8 *data,
  4034. + const yaffs_ExtendedTags *tags);
  4035. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  4036. + const yaffs_ExtendedTags *tags);
  4037. +
  4038. +/* Other local prototypes */
  4039. +static int yaffs_UnlinkObject(yaffs_Object *obj);
  4040. +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
  4041. +
  4042. +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
  4043. +
  4044. +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
  4045. + const __u8 *buffer,
  4046. + yaffs_ExtendedTags *tags,
  4047. + int useReserve);
  4048. +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
  4049. + int chunkInNAND, int inScan);
  4050. +
  4051. +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
  4052. + yaffs_ObjectType type);
  4053. +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
  4054. + yaffs_Object *obj);
  4055. +static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
  4056. + int force, int isShrink, int shadows);
  4057. +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
  4058. +static int yaffs_CheckStructures(void);
  4059. +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
  4060. + int chunkOffset, int *limit);
  4061. +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
  4062. +
  4063. +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
  4064. +
  4065. +
  4066. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  4067. + int chunkInNAND);
  4068. +
  4069. +static int yaffs_UnlinkWorker(yaffs_Object *obj);
  4070. +
  4071. +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
  4072. + int chunkInObject);
  4073. +
  4074. +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
  4075. + yaffs_BlockInfo **blockUsedPtr);
  4076. +
  4077. +static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
  4078. +
  4079. +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
  4080. +
  4081. +static void yaffs_VerifyDirectory(yaffs_Object *directory);
  4082. +#ifdef YAFFS_PARANOID
  4083. +static int yaffs_CheckFileSanity(yaffs_Object *in);
  4084. +#else
  4085. +#define yaffs_CheckFileSanity(in)
  4086. +#endif
  4087. +
  4088. +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
  4089. +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
  4090. +
  4091. +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
  4092. +
  4093. +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
  4094. + yaffs_ExtendedTags *tags);
  4095. +
  4096. +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
  4097. + unsigned pos);
  4098. +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
  4099. + yaffs_FileStructure *fStruct,
  4100. + __u32 chunkId);
  4101. +
  4102. +
  4103. +/* Function to calculate chunk and offset */
  4104. +
  4105. +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
  4106. + __u32 *offsetOut)
  4107. +{
  4108. + int chunk;
  4109. + __u32 offset;
  4110. +
  4111. + chunk = (__u32)(addr >> dev->chunkShift);
  4112. +
  4113. + if (dev->chunkDiv == 1) {
  4114. + /* easy power of 2 case */
  4115. + offset = (__u32)(addr & dev->chunkMask);
  4116. + } else {
  4117. + /* Non power-of-2 case */
  4118. +
  4119. + loff_t chunkBase;
  4120. +
  4121. + chunk /= dev->chunkDiv;
  4122. +
  4123. + chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
  4124. + offset = (__u32)(addr - chunkBase);
  4125. + }
  4126. +
  4127. + *chunkOut = chunk;
  4128. + *offsetOut = offset;
  4129. +}
  4130. +
  4131. +/* Function to return the number of shifts for a power of 2 greater than or
  4132. + * equal to the given number
  4133. + * Note we don't try to cater for all possible numbers and this does not have to
  4134. + * be hellishly efficient.
  4135. + */
  4136. +
  4137. +static __u32 ShiftsGE(__u32 x)
  4138. +{
  4139. + int extraBits;
  4140. + int nShifts;
  4141. +
  4142. + nShifts = extraBits = 0;
  4143. +
  4144. + while (x > 1) {
  4145. + if (x & 1)
  4146. + extraBits++;
  4147. + x >>= 1;
  4148. + nShifts++;
  4149. + }
  4150. +
  4151. + if (extraBits)
  4152. + nShifts++;
  4153. +
  4154. + return nShifts;
  4155. +}
  4156. +
  4157. +/* Function to return the number of shifts to get a 1 in bit 0
  4158. + */
  4159. +
  4160. +static __u32 Shifts(__u32 x)
  4161. +{
  4162. + int nShifts;
  4163. +
  4164. + nShifts = 0;
  4165. +
  4166. + if (!x)
  4167. + return 0;
  4168. +
  4169. + while (!(x&1)) {
  4170. + x >>= 1;
  4171. + nShifts++;
  4172. + }
  4173. +
  4174. + return nShifts;
  4175. +}
  4176. +
  4177. +
  4178. +
  4179. +/*
  4180. + * Temporary buffer manipulations.
  4181. + */
  4182. +
  4183. +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
  4184. +{
  4185. + int i;
  4186. + __u8 *buf = (__u8 *)1;
  4187. +
  4188. + memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
  4189. +
  4190. + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  4191. + dev->tempBuffer[i].line = 0; /* not in use */
  4192. + dev->tempBuffer[i].buffer = buf =
  4193. + YMALLOC_DMA(dev->totalBytesPerChunk);
  4194. + }
  4195. +
  4196. + return buf ? YAFFS_OK : YAFFS_FAIL;
  4197. +}
  4198. +
  4199. +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
  4200. +{
  4201. + int i, j;
  4202. +
  4203. + dev->tempInUse++;
  4204. + if (dev->tempInUse > dev->maxTemp)
  4205. + dev->maxTemp = dev->tempInUse;
  4206. +
  4207. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4208. + if (dev->tempBuffer[i].line == 0) {
  4209. + dev->tempBuffer[i].line = lineNo;
  4210. + if ((i + 1) > dev->maxTemp) {
  4211. + dev->maxTemp = i + 1;
  4212. + for (j = 0; j <= i; j++)
  4213. + dev->tempBuffer[j].maxLine =
  4214. + dev->tempBuffer[j].line;
  4215. + }
  4216. +
  4217. + return dev->tempBuffer[i].buffer;
  4218. + }
  4219. + }
  4220. +
  4221. + T(YAFFS_TRACE_BUFFERS,
  4222. + (TSTR("Out of temp buffers at line %d, other held by lines:"),
  4223. + lineNo));
  4224. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  4225. + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
  4226. +
  4227. + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
  4228. +
  4229. + /*
  4230. + * If we got here then we have to allocate an unmanaged one
  4231. + * This is not good.
  4232. + */
  4233. +
  4234. + dev->unmanagedTempAllocations++;
  4235. + return YMALLOC(dev->nDataBytesPerChunk);
  4236. +
  4237. +}
  4238. +
  4239. +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
  4240. + int lineNo)
  4241. +{
  4242. + int i;
  4243. +
  4244. + dev->tempInUse--;
  4245. +
  4246. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4247. + if (dev->tempBuffer[i].buffer == buffer) {
  4248. + dev->tempBuffer[i].line = 0;
  4249. + return;
  4250. + }
  4251. + }
  4252. +
  4253. + if (buffer) {
  4254. + /* assume it is an unmanaged one. */
  4255. + T(YAFFS_TRACE_BUFFERS,
  4256. + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
  4257. + lineNo));
  4258. + YFREE(buffer);
  4259. + dev->unmanagedTempDeallocations++;
  4260. + }
  4261. +
  4262. +}
  4263. +
  4264. +/*
  4265. + * Determine if we have a managed buffer.
  4266. + */
  4267. +int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
  4268. +{
  4269. + int i;
  4270. +
  4271. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4272. + if (dev->tempBuffer[i].buffer == buffer)
  4273. + return 1;
  4274. + }
  4275. +
  4276. + for (i = 0; i < dev->nShortOpCaches; i++) {
  4277. + if (dev->srCache[i].data == buffer)
  4278. + return 1;
  4279. + }
  4280. +
  4281. + if (buffer == dev->checkpointBuffer)
  4282. + return 1;
  4283. +
  4284. + T(YAFFS_TRACE_ALWAYS,
  4285. + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
  4286. + return 0;
  4287. +}
  4288. +
  4289. +
  4290. +
  4291. +/*
  4292. + * Chunk bitmap manipulations
  4293. + */
  4294. +
  4295. +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
  4296. +{
  4297. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  4298. + T(YAFFS_TRACE_ERROR,
  4299. + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
  4300. + blk));
  4301. + YBUG();
  4302. + }
  4303. + return dev->chunkBits +
  4304. + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
  4305. +}
  4306. +
  4307. +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
  4308. +{
  4309. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
  4310. + chunk < 0 || chunk >= dev->nChunksPerBlock) {
  4311. + T(YAFFS_TRACE_ERROR,
  4312. + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
  4313. + blk, chunk));
  4314. + YBUG();
  4315. + }
  4316. +}
  4317. +
  4318. +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
  4319. +{
  4320. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4321. +
  4322. + memset(blkBits, 0, dev->chunkBitmapStride);
  4323. +}
  4324. +
  4325. +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
  4326. +{
  4327. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4328. +
  4329. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4330. +
  4331. + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
  4332. +}
  4333. +
  4334. +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
  4335. +{
  4336. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4337. +
  4338. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4339. +
  4340. + blkBits[chunk / 8] |= (1 << (chunk & 7));
  4341. +}
  4342. +
  4343. +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
  4344. +{
  4345. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4346. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4347. +
  4348. + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  4349. +}
  4350. +
  4351. +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
  4352. +{
  4353. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4354. + int i;
  4355. + for (i = 0; i < dev->chunkBitmapStride; i++) {
  4356. + if (*blkBits)
  4357. + return 1;
  4358. + blkBits++;
  4359. + }
  4360. + return 0;
  4361. +}
  4362. +
  4363. +static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
  4364. +{
  4365. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4366. + int i;
  4367. + int n = 0;
  4368. + for (i = 0; i < dev->chunkBitmapStride; i++) {
  4369. + __u8 x = *blkBits;
  4370. + while (x) {
  4371. + if (x & 1)
  4372. + n++;
  4373. + x >>= 1;
  4374. + }
  4375. +
  4376. + blkBits++;
  4377. + }
  4378. + return n;
  4379. +}
  4380. +
  4381. +/*
  4382. + * Verification code
  4383. + */
  4384. +
  4385. +static int yaffs_SkipVerification(yaffs_Device *dev)
  4386. +{
  4387. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  4388. +}
  4389. +
  4390. +static int yaffs_SkipFullVerification(yaffs_Device *dev)
  4391. +{
  4392. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
  4393. +}
  4394. +
  4395. +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
  4396. +{
  4397. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
  4398. +}
  4399. +
  4400. +static const char *blockStateName[] = {
  4401. +"Unknown",
  4402. +"Needs scanning",
  4403. +"Scanning",
  4404. +"Empty",
  4405. +"Allocating",
  4406. +"Full",
  4407. +"Dirty",
  4408. +"Checkpoint",
  4409. +"Collecting",
  4410. +"Dead"
  4411. +};
  4412. +
  4413. +static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
  4414. +{
  4415. + int actuallyUsed;
  4416. + int inUse;
  4417. +
  4418. + if (yaffs_SkipVerification(dev))
  4419. + return;
  4420. +
  4421. + /* Report illegal runtime states */
  4422. + if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
  4423. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
  4424. +
  4425. + switch (bi->blockState) {
  4426. + case YAFFS_BLOCK_STATE_UNKNOWN:
  4427. + case YAFFS_BLOCK_STATE_SCANNING:
  4428. + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
  4429. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
  4430. + n, blockStateName[bi->blockState]));
  4431. + }
  4432. +
  4433. + /* Check pages in use and soft deletions are legal */
  4434. +
  4435. + actuallyUsed = bi->pagesInUse - bi->softDeletions;
  4436. +
  4437. + if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
  4438. + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
  4439. + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
  4440. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
  4441. + n, bi->pagesInUse, bi->softDeletions));
  4442. +
  4443. +
  4444. + /* Check chunk bitmap legal */
  4445. + inUse = yaffs_CountChunkBits(dev, n);
  4446. + if (inUse != bi->pagesInUse)
  4447. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
  4448. + n, bi->pagesInUse, inUse));
  4449. +
  4450. + /* Check that the sequence number is valid.
  4451. + * Ten million is legal, but is very unlikely
  4452. + */
  4453. + if (dev->isYaffs2 &&
  4454. + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
  4455. + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
  4456. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
  4457. + n, bi->sequenceNumber));
  4458. +}
  4459. +
  4460. +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
  4461. + int n)
  4462. +{
  4463. + yaffs_VerifyBlock(dev, bi, n);
  4464. +
  4465. + /* After collection the block should be in the erased state */
  4466. + /* This will need to change if we do partial gc */
  4467. +
  4468. + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
  4469. + bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
  4470. + T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
  4471. + n, bi->blockState));
  4472. + }
  4473. +}
  4474. +
  4475. +static void yaffs_VerifyBlocks(yaffs_Device *dev)
  4476. +{
  4477. + int i;
  4478. + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
  4479. + int nIllegalBlockStates = 0;
  4480. +
  4481. + if (yaffs_SkipVerification(dev))
  4482. + return;
  4483. +
  4484. + memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
  4485. +
  4486. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  4487. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  4488. + yaffs_VerifyBlock(dev, bi, i);
  4489. +
  4490. + if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
  4491. + nBlocksPerState[bi->blockState]++;
  4492. + else
  4493. + nIllegalBlockStates++;
  4494. + }
  4495. +
  4496. + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
  4497. + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
  4498. +
  4499. + T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
  4500. + if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  4501. + T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
  4502. +
  4503. + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  4504. + T(YAFFS_TRACE_VERIFY,
  4505. + (TSTR("%s %d blocks"TENDSTR),
  4506. + blockStateName[i], nBlocksPerState[i]));
  4507. +
  4508. + if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
  4509. + T(YAFFS_TRACE_VERIFY,
  4510. + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
  4511. + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
  4512. +
  4513. + if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
  4514. + T(YAFFS_TRACE_VERIFY,
  4515. + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
  4516. + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
  4517. +
  4518. + if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  4519. + T(YAFFS_TRACE_VERIFY,
  4520. + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
  4521. + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
  4522. +
  4523. + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
  4524. +
  4525. +}
  4526. +
  4527. +/*
  4528. + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
  4529. + * case those tests will not be performed.
  4530. + */
  4531. +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
  4532. +{
  4533. + if (obj && yaffs_SkipVerification(obj->myDev))
  4534. + return;
  4535. +
  4536. + if (!(tags && obj && oh)) {
  4537. + T(YAFFS_TRACE_VERIFY,
  4538. + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
  4539. + (__u32)tags, (__u32)obj, (__u32)oh));
  4540. + return;
  4541. + }
  4542. +
  4543. + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  4544. + oh->type > YAFFS_OBJECT_TYPE_MAX)
  4545. + T(YAFFS_TRACE_VERIFY,
  4546. + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
  4547. + tags->objectId, oh->type));
  4548. +
  4549. + if (tags->objectId != obj->objectId)
  4550. + T(YAFFS_TRACE_VERIFY,
  4551. + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
  4552. + tags->objectId, obj->objectId));
  4553. +
  4554. +
  4555. + /*
  4556. + * Check that the object's parent ids match if parentCheck requested.
  4557. + *
  4558. + * Tests do not apply to the root object.
  4559. + */
  4560. +
  4561. + if (parentCheck && tags->objectId > 1 && !obj->parent)
  4562. + T(YAFFS_TRACE_VERIFY,
  4563. + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
  4564. + tags->objectId, oh->parentObjectId));
  4565. +
  4566. + if (parentCheck && obj->parent &&
  4567. + oh->parentObjectId != obj->parent->objectId &&
  4568. + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
  4569. + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
  4570. + T(YAFFS_TRACE_VERIFY,
  4571. + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
  4572. + tags->objectId, oh->parentObjectId, obj->parent->objectId));
  4573. +
  4574. + if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
  4575. + T(YAFFS_TRACE_VERIFY,
  4576. + (TSTR("Obj %d header name is NULL"TENDSTR),
  4577. + obj->objectId));
  4578. +
  4579. + if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
  4580. + T(YAFFS_TRACE_VERIFY,
  4581. + (TSTR("Obj %d header name is 0xFF"TENDSTR),
  4582. + obj->objectId));
  4583. +}
  4584. +
  4585. +
  4586. +
  4587. +static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
  4588. + __u32 level, int chunkOffset)
  4589. +{
  4590. + int i;
  4591. + yaffs_Device *dev = obj->myDev;
  4592. + int ok = 1;
  4593. +
  4594. + if (tn) {
  4595. + if (level > 0) {
  4596. +
  4597. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  4598. + if (tn->internal[i]) {
  4599. + ok = yaffs_VerifyTnodeWorker(obj,
  4600. + tn->internal[i],
  4601. + level - 1,
  4602. + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  4603. + }
  4604. + }
  4605. + } else if (level == 0) {
  4606. + yaffs_ExtendedTags tags;
  4607. + __u32 objectId = obj->objectId;
  4608. +
  4609. + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
  4610. +
  4611. + for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
  4612. + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  4613. +
  4614. + if (theChunk > 0) {
  4615. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
  4616. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
  4617. + if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
  4618. + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  4619. + objectId, chunkOffset, theChunk,
  4620. + tags.objectId, tags.chunkId));
  4621. + }
  4622. + }
  4623. + chunkOffset++;
  4624. + }
  4625. + }
  4626. + }
  4627. +
  4628. + return ok;
  4629. +
  4630. +}
  4631. +
  4632. +
  4633. +static void yaffs_VerifyFile(yaffs_Object *obj)
  4634. +{
  4635. + int requiredTallness;
  4636. + int actualTallness;
  4637. + __u32 lastChunk;
  4638. + __u32 x;
  4639. + __u32 i;
  4640. + yaffs_Device *dev;
  4641. + yaffs_ExtendedTags tags;
  4642. + yaffs_Tnode *tn;
  4643. + __u32 objectId;
  4644. +
  4645. + if (!obj)
  4646. + return;
  4647. +
  4648. + if (yaffs_SkipVerification(obj->myDev))
  4649. + return;
  4650. +
  4651. + dev = obj->myDev;
  4652. + objectId = obj->objectId;
  4653. +
  4654. + /* Check file size is consistent with tnode depth */
  4655. + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
  4656. + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
  4657. + requiredTallness = 0;
  4658. + while (x > 0) {
  4659. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  4660. + requiredTallness++;
  4661. + }
  4662. +
  4663. + actualTallness = obj->variant.fileVariant.topLevel;
  4664. +
  4665. + if (requiredTallness > actualTallness)
  4666. + T(YAFFS_TRACE_VERIFY,
  4667. + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
  4668. + obj->objectId, actualTallness, requiredTallness));
  4669. +
  4670. +
  4671. + /* Check that the chunks in the tnode tree are all correct.
  4672. + * We do this by scanning through the tnode tree and
  4673. + * checking the tags for every chunk match.
  4674. + */
  4675. +
  4676. + if (yaffs_SkipNANDVerification(dev))
  4677. + return;
  4678. +
  4679. + for (i = 1; i <= lastChunk; i++) {
  4680. + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
  4681. +
  4682. + if (tn) {
  4683. + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  4684. + if (theChunk > 0) {
  4685. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
  4686. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
  4687. + if (tags.objectId != objectId || tags.chunkId != i) {
  4688. + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  4689. + objectId, i, theChunk,
  4690. + tags.objectId, tags.chunkId));
  4691. + }
  4692. + }
  4693. + }
  4694. + }
  4695. +}
  4696. +
  4697. +
  4698. +static void yaffs_VerifyHardLink(yaffs_Object *obj)
  4699. +{
  4700. + if (obj && yaffs_SkipVerification(obj->myDev))
  4701. + return;
  4702. +
  4703. + /* Verify sane equivalent object */
  4704. +}
  4705. +
  4706. +static void yaffs_VerifySymlink(yaffs_Object *obj)
  4707. +{
  4708. + if (obj && yaffs_SkipVerification(obj->myDev))
  4709. + return;
  4710. +
  4711. + /* Verify symlink string */
  4712. +}
  4713. +
  4714. +static void yaffs_VerifySpecial(yaffs_Object *obj)
  4715. +{
  4716. + if (obj && yaffs_SkipVerification(obj->myDev))
  4717. + return;
  4718. +}
  4719. +
  4720. +static void yaffs_VerifyObject(yaffs_Object *obj)
  4721. +{
  4722. + yaffs_Device *dev;
  4723. +
  4724. + __u32 chunkMin;
  4725. + __u32 chunkMax;
  4726. +
  4727. + __u32 chunkIdOk;
  4728. + __u32 chunkInRange;
  4729. + __u32 chunkShouldNotBeDeleted;
  4730. + __u32 chunkValid;
  4731. +
  4732. + if (!obj)
  4733. + return;
  4734. +
  4735. + if (obj->beingCreated)
  4736. + return;
  4737. +
  4738. + dev = obj->myDev;
  4739. +
  4740. + if (yaffs_SkipVerification(dev))
  4741. + return;
  4742. +
  4743. + /* Check sane object header chunk */
  4744. +
  4745. + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
  4746. + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
  4747. +
  4748. + chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
  4749. + chunkIdOk = chunkInRange || obj->hdrChunk == 0;
  4750. + chunkValid = chunkInRange &&
  4751. + yaffs_CheckChunkBit(dev,
  4752. + obj->hdrChunk / dev->nChunksPerBlock,
  4753. + obj->hdrChunk % dev->nChunksPerBlock);
  4754. + chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
  4755. +
  4756. + if (!obj->fake &&
  4757. + (!chunkIdOk || chunkShouldNotBeDeleted)) {
  4758. + T(YAFFS_TRACE_VERIFY,
  4759. + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
  4760. + obj->objectId, obj->hdrChunk,
  4761. + chunkIdOk ? "" : ",out of range",
  4762. + chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
  4763. + }
  4764. +
  4765. + if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
  4766. + yaffs_ExtendedTags tags;
  4767. + yaffs_ObjectHeader *oh;
  4768. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  4769. +
  4770. + oh = (yaffs_ObjectHeader *)buffer;
  4771. +
  4772. + yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
  4773. + &tags);
  4774. +
  4775. + yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
  4776. +
  4777. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  4778. + }
  4779. +
  4780. + /* Verify it has a parent */
  4781. + if (obj && !obj->fake &&
  4782. + (!obj->parent || obj->parent->myDev != dev)) {
  4783. + T(YAFFS_TRACE_VERIFY,
  4784. + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
  4785. + obj->objectId, obj->parent));
  4786. + }
  4787. +
  4788. + /* Verify parent is a directory */
  4789. + if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  4790. + T(YAFFS_TRACE_VERIFY,
  4791. + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
  4792. + obj->objectId, obj->parent->variantType));
  4793. + }
  4794. +
  4795. + switch (obj->variantType) {
  4796. + case YAFFS_OBJECT_TYPE_FILE:
  4797. + yaffs_VerifyFile(obj);
  4798. + break;
  4799. + case YAFFS_OBJECT_TYPE_SYMLINK:
  4800. + yaffs_VerifySymlink(obj);
  4801. + break;
  4802. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  4803. + yaffs_VerifyDirectory(obj);
  4804. + break;
  4805. + case YAFFS_OBJECT_TYPE_HARDLINK:
  4806. + yaffs_VerifyHardLink(obj);
  4807. + break;
  4808. + case YAFFS_OBJECT_TYPE_SPECIAL:
  4809. + yaffs_VerifySpecial(obj);
  4810. + break;
  4811. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  4812. + default:
  4813. + T(YAFFS_TRACE_VERIFY,
  4814. + (TSTR("Obj %d has illegaltype %d"TENDSTR),
  4815. + obj->objectId, obj->variantType));
  4816. + break;
  4817. + }
  4818. +}
  4819. +
  4820. +static void yaffs_VerifyObjects(yaffs_Device *dev)
  4821. +{
  4822. + yaffs_Object *obj;
  4823. + int i;
  4824. + struct ylist_head *lh;
  4825. +
  4826. + if (yaffs_SkipVerification(dev))
  4827. + return;
  4828. +
  4829. + /* Iterate through the objects in each hash entry */
  4830. +
  4831. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  4832. + ylist_for_each(lh, &dev->objectBucket[i].list) {
  4833. + if (lh) {
  4834. + obj = ylist_entry(lh, yaffs_Object, hashLink);
  4835. + yaffs_VerifyObject(obj);
  4836. + }
  4837. + }
  4838. + }
  4839. +}
  4840. +
  4841. +
  4842. +/*
  4843. + * Simple hash function. Needs to have a reasonable spread
  4844. + */
  4845. +
  4846. +static Y_INLINE int yaffs_HashFunction(int n)
  4847. +{
  4848. + n = abs(n);
  4849. + return n % YAFFS_NOBJECT_BUCKETS;
  4850. +}
  4851. +
  4852. +/*
  4853. + * Access functions to useful fake objects.
  4854. + * Note that root might have a presence in NAND if permissions are set.
  4855. + */
  4856. +
  4857. +yaffs_Object *yaffs_Root(yaffs_Device *dev)
  4858. +{
  4859. + return dev->rootDir;
  4860. +}
  4861. +
  4862. +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
  4863. +{
  4864. + return dev->lostNFoundDir;
  4865. +}
  4866. +
  4867. +
  4868. +/*
  4869. + * Erased NAND checking functions
  4870. + */
  4871. +
  4872. +int yaffs_CheckFF(__u8 *buffer, int nBytes)
  4873. +{
  4874. + /* Horrible, slow implementation */
  4875. + while (nBytes--) {
  4876. + if (*buffer != 0xFF)
  4877. + return 0;
  4878. + buffer++;
  4879. + }
  4880. + return 1;
  4881. +}
  4882. +
  4883. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  4884. + int chunkInNAND)
  4885. +{
  4886. + int retval = YAFFS_OK;
  4887. + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
  4888. + yaffs_ExtendedTags tags;
  4889. + int result;
  4890. +
  4891. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
  4892. +
  4893. + if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
  4894. + retval = YAFFS_FAIL;
  4895. +
  4896. + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
  4897. + T(YAFFS_TRACE_NANDACCESS,
  4898. + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
  4899. + retval = YAFFS_FAIL;
  4900. + }
  4901. +
  4902. + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
  4903. +
  4904. + return retval;
  4905. +
  4906. +}
  4907. +
  4908. +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
  4909. + const __u8 *data,
  4910. + yaffs_ExtendedTags *tags,
  4911. + int useReserve)
  4912. +{
  4913. + int attempts = 0;
  4914. + int writeOk = 0;
  4915. + int chunk;
  4916. +
  4917. + yaffs_InvalidateCheckpoint(dev);
  4918. +
  4919. + do {
  4920. + yaffs_BlockInfo *bi = 0;
  4921. + int erasedOk = 0;
  4922. +
  4923. + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
  4924. + if (chunk < 0) {
  4925. + /* no space */
  4926. + break;
  4927. + }
  4928. +
  4929. + /* First check this chunk is erased, if it needs
  4930. + * checking. The checking policy (unless forced
  4931. + * always on) is as follows:
  4932. + *
  4933. + * Check the first page we try to write in a block.
  4934. + * If the check passes then we don't need to check any
  4935. + * more. If the check fails, we check again...
  4936. + * If the block has been erased, we don't need to check.
  4937. + *
  4938. + * However, if the block has been prioritised for gc,
  4939. + * then we think there might be something odd about
  4940. + * this block and stop using it.
  4941. + *
  4942. + * Rationale: We should only ever see chunks that have
  4943. + * not been erased if there was a partially written
  4944. + * chunk due to power loss. This checking policy should
  4945. + * catch that case with very few checks and thus save a
  4946. + * lot of checks that are most likely not needed.
  4947. + */
  4948. + if (bi->gcPrioritise) {
  4949. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  4950. + /* try another chunk */
  4951. + continue;
  4952. + }
  4953. +
  4954. + /* let's give it a try */
  4955. + attempts++;
  4956. +
  4957. +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  4958. + bi->skipErasedCheck = 0;
  4959. +#endif
  4960. + if (!bi->skipErasedCheck) {
  4961. + erasedOk = yaffs_CheckChunkErased(dev, chunk);
  4962. + if (erasedOk != YAFFS_OK) {
  4963. + T(YAFFS_TRACE_ERROR,
  4964. + (TSTR("**>> yaffs chunk %d was not erased"
  4965. + TENDSTR), chunk));
  4966. +
  4967. + /* try another chunk */
  4968. + continue;
  4969. + }
  4970. + bi->skipErasedCheck = 1;
  4971. + }
  4972. +
  4973. + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
  4974. + data, tags);
  4975. + if (writeOk != YAFFS_OK) {
  4976. + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
  4977. + /* try another chunk */
  4978. + continue;
  4979. + }
  4980. +
  4981. + /* Copy the data into the robustification buffer */
  4982. + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
  4983. +
  4984. + } while (writeOk != YAFFS_OK &&
  4985. + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  4986. +
  4987. + if (!writeOk)
  4988. + chunk = -1;
  4989. +
  4990. + if (attempts > 1) {
  4991. + T(YAFFS_TRACE_ERROR,
  4992. + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
  4993. + attempts));
  4994. +
  4995. + dev->nRetriedWrites += (attempts - 1);
  4996. + }
  4997. +
  4998. + return chunk;
  4999. +}
  5000. +
  5001. +/*
  5002. + * Block retiring for handling a broken block.
  5003. + */
  5004. +
  5005. +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
  5006. +{
  5007. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  5008. +
  5009. + yaffs_InvalidateCheckpoint(dev);
  5010. +
  5011. + if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
  5012. + if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
  5013. + T(YAFFS_TRACE_ALWAYS, (TSTR(
  5014. + "yaffs: Failed to mark bad and erase block %d"
  5015. + TENDSTR), blockInNAND));
  5016. + } else {
  5017. + yaffs_ExtendedTags tags;
  5018. + int chunkId = blockInNAND * dev->nChunksPerBlock;
  5019. +
  5020. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  5021. +
  5022. + memset(buffer, 0xff, dev->nDataBytesPerChunk);
  5023. + yaffs_InitialiseTags(&tags);
  5024. + tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
  5025. + if (dev->writeChunkWithTagsToNAND(dev, chunkId -
  5026. + dev->chunkOffset, buffer, &tags) != YAFFS_OK)
  5027. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
  5028. + TCONT("write bad block marker to block %d")
  5029. + TENDSTR), blockInNAND));
  5030. +
  5031. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  5032. + }
  5033. + }
  5034. +
  5035. + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
  5036. + bi->gcPrioritise = 0;
  5037. + bi->needsRetiring = 0;
  5038. +
  5039. + dev->nRetiredBlocks++;
  5040. +}
  5041. +
  5042. +/*
  5043. + * Functions for robustisizing TODO
  5044. + *
  5045. + */
  5046. +
  5047. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  5048. + const __u8 *data,
  5049. + const yaffs_ExtendedTags *tags)
  5050. +{
  5051. +}
  5052. +
  5053. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  5054. + const yaffs_ExtendedTags *tags)
  5055. +{
  5056. +}
  5057. +
  5058. +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
  5059. +{
  5060. + if (!bi->gcPrioritise) {
  5061. + bi->gcPrioritise = 1;
  5062. + dev->hasPendingPrioritisedGCs = 1;
  5063. + bi->chunkErrorStrikes++;
  5064. +
  5065. + if (bi->chunkErrorStrikes > 3) {
  5066. + bi->needsRetiring = 1; /* Too many stikes, so retire this */
  5067. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
  5068. +
  5069. + }
  5070. + }
  5071. +}
  5072. +
  5073. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
  5074. + int erasedOk)
  5075. +{
  5076. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  5077. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  5078. +
  5079. + yaffs_HandleChunkError(dev, bi);
  5080. +
  5081. + if (erasedOk) {
  5082. + /* Was an actual write failure, so mark the block for retirement */
  5083. + bi->needsRetiring = 1;
  5084. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  5085. + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
  5086. + }
  5087. +
  5088. + /* Delete the chunk */
  5089. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  5090. +}
  5091. +
  5092. +
  5093. +/*---------------- Name handling functions ------------*/
  5094. +
  5095. +static __u16 yaffs_CalcNameSum(const YCHAR *name)
  5096. +{
  5097. + __u16 sum = 0;
  5098. + __u16 i = 1;
  5099. +
  5100. + const YUCHAR *bname = (const YUCHAR *) name;
  5101. + if (bname) {
  5102. + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
  5103. +
  5104. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  5105. + sum += yaffs_toupper(*bname) * i;
  5106. +#else
  5107. + sum += (*bname) * i;
  5108. +#endif
  5109. + i++;
  5110. + bname++;
  5111. + }
  5112. + }
  5113. + return sum;
  5114. +}
  5115. +
  5116. +static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
  5117. +{
  5118. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  5119. + memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
  5120. + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
  5121. + yaffs_strcpy(obj->shortName, name);
  5122. + else
  5123. + obj->shortName[0] = _Y('\0');
  5124. +#endif
  5125. + obj->sum = yaffs_CalcNameSum(name);
  5126. +}
  5127. +
  5128. +/*-------------------- TNODES -------------------
  5129. +
  5130. + * List of spare tnodes
  5131. + * The list is hooked together using the first pointer
  5132. + * in the tnode.
  5133. + */
  5134. +
  5135. +/* yaffs_CreateTnodes creates a bunch more tnodes and
  5136. + * adds them to the tnode free list.
  5137. + * Don't use this function directly
  5138. + */
  5139. +
  5140. +static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
  5141. +{
  5142. + int i;
  5143. + int tnodeSize;
  5144. + yaffs_Tnode *newTnodes;
  5145. + __u8 *mem;
  5146. + yaffs_Tnode *curr;
  5147. + yaffs_Tnode *next;
  5148. + yaffs_TnodeList *tnl;
  5149. +
  5150. + if (nTnodes < 1)
  5151. + return YAFFS_OK;
  5152. +
  5153. + /* Calculate the tnode size in bytes for variable width tnode support.
  5154. + * Must be a multiple of 32-bits */
  5155. + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  5156. +
  5157. + if (tnodeSize < sizeof(yaffs_Tnode))
  5158. + tnodeSize = sizeof(yaffs_Tnode);
  5159. +
  5160. + /* make these things */
  5161. +
  5162. + newTnodes = YMALLOC(nTnodes * tnodeSize);
  5163. + mem = (__u8 *)newTnodes;
  5164. +
  5165. + if (!newTnodes) {
  5166. + T(YAFFS_TRACE_ERROR,
  5167. + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
  5168. + return YAFFS_FAIL;
  5169. + }
  5170. +
  5171. + /* Hook them into the free list */
  5172. +#if 0
  5173. + for (i = 0; i < nTnodes - 1; i++) {
  5174. + newTnodes[i].internal[0] = &newTnodes[i + 1];
  5175. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5176. + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5177. +#endif
  5178. + }
  5179. +
  5180. + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
  5181. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5182. + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5183. +#endif
  5184. + dev->freeTnodes = newTnodes;
  5185. +#else
  5186. + /* New hookup for wide tnodes */
  5187. + for (i = 0; i < nTnodes - 1; i++) {
  5188. + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
  5189. + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
  5190. + curr->internal[0] = next;
  5191. + }
  5192. +
  5193. + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
  5194. + curr->internal[0] = dev->freeTnodes;
  5195. + dev->freeTnodes = (yaffs_Tnode *)mem;
  5196. +
  5197. +#endif
  5198. +
  5199. +
  5200. + dev->nFreeTnodes += nTnodes;
  5201. + dev->nTnodesCreated += nTnodes;
  5202. +
  5203. + /* Now add this bunch of tnodes to a list for freeing up.
  5204. + * NB If we can't add this to the management list it isn't fatal
  5205. + * but it just means we can't free this bunch of tnodes later.
  5206. + */
  5207. +
  5208. + tnl = YMALLOC(sizeof(yaffs_TnodeList));
  5209. + if (!tnl) {
  5210. + T(YAFFS_TRACE_ERROR,
  5211. + (TSTR
  5212. + ("yaffs: Could not add tnodes to management list" TENDSTR)));
  5213. + return YAFFS_FAIL;
  5214. + } else {
  5215. + tnl->tnodes = newTnodes;
  5216. + tnl->next = dev->allocatedTnodeList;
  5217. + dev->allocatedTnodeList = tnl;
  5218. + }
  5219. +
  5220. + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
  5221. +
  5222. + return YAFFS_OK;
  5223. +}
  5224. +
  5225. +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
  5226. +
  5227. +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
  5228. +{
  5229. + yaffs_Tnode *tn = NULL;
  5230. +
  5231. + /* If there are none left make more */
  5232. + if (!dev->freeTnodes)
  5233. + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
  5234. +
  5235. + if (dev->freeTnodes) {
  5236. + tn = dev->freeTnodes;
  5237. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5238. + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
  5239. + /* Hoosterman, this thing looks like it isn't in the list */
  5240. + T(YAFFS_TRACE_ALWAYS,
  5241. + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
  5242. + }
  5243. +#endif
  5244. + dev->freeTnodes = dev->freeTnodes->internal[0];
  5245. + dev->nFreeTnodes--;
  5246. + }
  5247. +
  5248. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5249. +
  5250. + return tn;
  5251. +}
  5252. +
  5253. +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
  5254. +{
  5255. + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
  5256. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  5257. +
  5258. + if (tnodeSize < sizeof(yaffs_Tnode))
  5259. + tnodeSize = sizeof(yaffs_Tnode);
  5260. +
  5261. + if (tn)
  5262. + memset(tn, 0, tnodeSize);
  5263. +
  5264. + return tn;
  5265. +}
  5266. +
  5267. +/* FreeTnode frees up a tnode and puts it back on the free list */
  5268. +static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
  5269. +{
  5270. + if (tn) {
  5271. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5272. + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
  5273. + /* Hoosterman, this thing looks like it is already in the list */
  5274. + T(YAFFS_TRACE_ALWAYS,
  5275. + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
  5276. + }
  5277. + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5278. +#endif
  5279. + tn->internal[0] = dev->freeTnodes;
  5280. + dev->freeTnodes = tn;
  5281. + dev->nFreeTnodes++;
  5282. + }
  5283. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5284. +}
  5285. +
  5286. +static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
  5287. +{
  5288. + /* Free the list of allocated tnodes */
  5289. + yaffs_TnodeList *tmp;
  5290. +
  5291. + while (dev->allocatedTnodeList) {
  5292. + tmp = dev->allocatedTnodeList->next;
  5293. +
  5294. + YFREE(dev->allocatedTnodeList->tnodes);
  5295. + YFREE(dev->allocatedTnodeList);
  5296. + dev->allocatedTnodeList = tmp;
  5297. +
  5298. + }
  5299. +
  5300. + dev->freeTnodes = NULL;
  5301. + dev->nFreeTnodes = 0;
  5302. +}
  5303. +
  5304. +static void yaffs_InitialiseTnodes(yaffs_Device *dev)
  5305. +{
  5306. + dev->allocatedTnodeList = NULL;
  5307. + dev->freeTnodes = NULL;
  5308. + dev->nFreeTnodes = 0;
  5309. + dev->nTnodesCreated = 0;
  5310. +}
  5311. +
  5312. +
  5313. +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
  5314. + unsigned val)
  5315. +{
  5316. + __u32 *map = (__u32 *)tn;
  5317. + __u32 bitInMap;
  5318. + __u32 bitInWord;
  5319. + __u32 wordInMap;
  5320. + __u32 mask;
  5321. +
  5322. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  5323. + val >>= dev->chunkGroupBits;
  5324. +
  5325. + bitInMap = pos * dev->tnodeWidth;
  5326. + wordInMap = bitInMap / 32;
  5327. + bitInWord = bitInMap & (32 - 1);
  5328. +
  5329. + mask = dev->tnodeMask << bitInWord;
  5330. +
  5331. + map[wordInMap] &= ~mask;
  5332. + map[wordInMap] |= (mask & (val << bitInWord));
  5333. +
  5334. + if (dev->tnodeWidth > (32 - bitInWord)) {
  5335. + bitInWord = (32 - bitInWord);
  5336. + wordInMap++;;
  5337. + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
  5338. + map[wordInMap] &= ~mask;
  5339. + map[wordInMap] |= (mask & (val >> bitInWord));
  5340. + }
  5341. +}
  5342. +
  5343. +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
  5344. + unsigned pos)
  5345. +{
  5346. + __u32 *map = (__u32 *)tn;
  5347. + __u32 bitInMap;
  5348. + __u32 bitInWord;
  5349. + __u32 wordInMap;
  5350. + __u32 val;
  5351. +
  5352. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  5353. +
  5354. + bitInMap = pos * dev->tnodeWidth;
  5355. + wordInMap = bitInMap / 32;
  5356. + bitInWord = bitInMap & (32 - 1);
  5357. +
  5358. + val = map[wordInMap] >> bitInWord;
  5359. +
  5360. + if (dev->tnodeWidth > (32 - bitInWord)) {
  5361. + bitInWord = (32 - bitInWord);
  5362. + wordInMap++;;
  5363. + val |= (map[wordInMap] << bitInWord);
  5364. + }
  5365. +
  5366. + val &= dev->tnodeMask;
  5367. + val <<= dev->chunkGroupBits;
  5368. +
  5369. + return val;
  5370. +}
  5371. +
  5372. +/* ------------------- End of individual tnode manipulation -----------------*/
  5373. +
  5374. +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  5375. + * The look up tree is represented by the top tnode and the number of topLevel
  5376. + * in the tree. 0 means only the level 0 tnode is in the tree.
  5377. + */
  5378. +
  5379. +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  5380. +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
  5381. + yaffs_FileStructure *fStruct,
  5382. + __u32 chunkId)
  5383. +{
  5384. + yaffs_Tnode *tn = fStruct->top;
  5385. + __u32 i;
  5386. + int requiredTallness;
  5387. + int level = fStruct->topLevel;
  5388. +
  5389. + /* Check sane level and chunk Id */
  5390. + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  5391. + return NULL;
  5392. +
  5393. + if (chunkId > YAFFS_MAX_CHUNK_ID)
  5394. + return NULL;
  5395. +
  5396. + /* First check we're tall enough (ie enough topLevel) */
  5397. +
  5398. + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  5399. + requiredTallness = 0;
  5400. + while (i) {
  5401. + i >>= YAFFS_TNODES_INTERNAL_BITS;
  5402. + requiredTallness++;
  5403. + }
  5404. +
  5405. + if (requiredTallness > fStruct->topLevel)
  5406. + return NULL; /* Not tall enough, so we can't find it */
  5407. +
  5408. + /* Traverse down to level 0 */
  5409. + while (level > 0 && tn) {
  5410. + tn = tn->internal[(chunkId >>
  5411. + (YAFFS_TNODES_LEVEL0_BITS +
  5412. + (level - 1) *
  5413. + YAFFS_TNODES_INTERNAL_BITS)) &
  5414. + YAFFS_TNODES_INTERNAL_MASK];
  5415. + level--;
  5416. + }
  5417. +
  5418. + return tn;
  5419. +}
  5420. +
  5421. +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  5422. + * This happens in two steps:
  5423. + * 1. If the tree isn't tall enough, then make it taller.
  5424. + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  5425. + *
  5426. + * Used when modifying the tree.
  5427. + *
  5428. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  5429. + * be plugged into the ttree.
  5430. + */
  5431. +
  5432. +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
  5433. + yaffs_FileStructure *fStruct,
  5434. + __u32 chunkId,
  5435. + yaffs_Tnode *passedTn)
  5436. +{
  5437. + int requiredTallness;
  5438. + int i;
  5439. + int l;
  5440. + yaffs_Tnode *tn;
  5441. +
  5442. + __u32 x;
  5443. +
  5444. +
  5445. + /* Check sane level and page Id */
  5446. + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
  5447. + return NULL;
  5448. +
  5449. + if (chunkId > YAFFS_MAX_CHUNK_ID)
  5450. + return NULL;
  5451. +
  5452. + /* First check we're tall enough (ie enough topLevel) */
  5453. +
  5454. + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  5455. + requiredTallness = 0;
  5456. + while (x) {
  5457. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  5458. + requiredTallness++;
  5459. + }
  5460. +
  5461. +
  5462. + if (requiredTallness > fStruct->topLevel) {
  5463. + /* Not tall enough, gotta make the tree taller */
  5464. + for (i = fStruct->topLevel; i < requiredTallness; i++) {
  5465. +
  5466. + tn = yaffs_GetTnode(dev);
  5467. +
  5468. + if (tn) {
  5469. + tn->internal[0] = fStruct->top;
  5470. + fStruct->top = tn;
  5471. + } else {
  5472. + T(YAFFS_TRACE_ERROR,
  5473. + (TSTR("yaffs: no more tnodes" TENDSTR)));
  5474. + }
  5475. + }
  5476. +
  5477. + fStruct->topLevel = requiredTallness;
  5478. + }
  5479. +
  5480. + /* Traverse down to level 0, adding anything we need */
  5481. +
  5482. + l = fStruct->topLevel;
  5483. + tn = fStruct->top;
  5484. +
  5485. + if (l > 0) {
  5486. + while (l > 0 && tn) {
  5487. + x = (chunkId >>
  5488. + (YAFFS_TNODES_LEVEL0_BITS +
  5489. + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  5490. + YAFFS_TNODES_INTERNAL_MASK;
  5491. +
  5492. +
  5493. + if ((l > 1) && !tn->internal[x]) {
  5494. + /* Add missing non-level-zero tnode */
  5495. + tn->internal[x] = yaffs_GetTnode(dev);
  5496. +
  5497. + } else if (l == 1) {
  5498. + /* Looking from level 1 at level 0 */
  5499. + if (passedTn) {
  5500. + /* If we already have one, then release it.*/
  5501. + if (tn->internal[x])
  5502. + yaffs_FreeTnode(dev, tn->internal[x]);
  5503. + tn->internal[x] = passedTn;
  5504. +
  5505. + } else if (!tn->internal[x]) {
  5506. + /* Don't have one, none passed in */
  5507. + tn->internal[x] = yaffs_GetTnode(dev);
  5508. + }
  5509. + }
  5510. +
  5511. + tn = tn->internal[x];
  5512. + l--;
  5513. + }
  5514. + } else {
  5515. + /* top is level 0 */
  5516. + if (passedTn) {
  5517. + memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
  5518. + yaffs_FreeTnode(dev, passedTn);
  5519. + }
  5520. + }
  5521. +
  5522. + return tn;
  5523. +}
  5524. +
  5525. +static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
  5526. + yaffs_ExtendedTags *tags, int objectId,
  5527. + int chunkInInode)
  5528. +{
  5529. + int j;
  5530. +
  5531. + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
  5532. + if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock,
  5533. + theChunk % dev->nChunksPerBlock)) {
  5534. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
  5535. + tags);
  5536. + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
  5537. + /* found it; */
  5538. + return theChunk;
  5539. + }
  5540. + }
  5541. + theChunk++;
  5542. + }
  5543. + return -1;
  5544. +}
  5545. +
  5546. +
  5547. +/* DeleteWorker scans backwards through the tnode tree and deletes all the
  5548. + * chunks and tnodes in the file
  5549. + * Returns 1 if the tree was deleted.
  5550. + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
  5551. + */
  5552. +
  5553. +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
  5554. + int chunkOffset, int *limit)
  5555. +{
  5556. + int i;
  5557. + int chunkInInode;
  5558. + int theChunk;
  5559. + yaffs_ExtendedTags tags;
  5560. + int foundChunk;
  5561. + yaffs_Device *dev = in->myDev;
  5562. +
  5563. + int allDone = 1;
  5564. +
  5565. + if (tn) {
  5566. + if (level > 0) {
  5567. + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  5568. + i--) {
  5569. + if (tn->internal[i]) {
  5570. + if (limit && (*limit) < 0) {
  5571. + allDone = 0;
  5572. + } else {
  5573. + allDone =
  5574. + yaffs_DeleteWorker(in,
  5575. + tn->
  5576. + internal
  5577. + [i],
  5578. + level -
  5579. + 1,
  5580. + (chunkOffset
  5581. + <<
  5582. + YAFFS_TNODES_INTERNAL_BITS)
  5583. + + i,
  5584. + limit);
  5585. + }
  5586. + if (allDone) {
  5587. + yaffs_FreeTnode(dev,
  5588. + tn->
  5589. + internal[i]);
  5590. + tn->internal[i] = NULL;
  5591. + }
  5592. + }
  5593. + }
  5594. + return (allDone) ? 1 : 0;
  5595. + } else if (level == 0) {
  5596. + int hitLimit = 0;
  5597. +
  5598. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
  5599. + i--) {
  5600. + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  5601. + if (theChunk) {
  5602. +
  5603. + chunkInInode = (chunkOffset <<
  5604. + YAFFS_TNODES_LEVEL0_BITS) + i;
  5605. +
  5606. + foundChunk =
  5607. + yaffs_FindChunkInGroup(dev,
  5608. + theChunk,
  5609. + &tags,
  5610. + in->objectId,
  5611. + chunkInInode);
  5612. +
  5613. + if (foundChunk > 0) {
  5614. + yaffs_DeleteChunk(dev,
  5615. + foundChunk, 1,
  5616. + __LINE__);
  5617. + in->nDataChunks--;
  5618. + if (limit) {
  5619. + *limit = *limit - 1;
  5620. + if (*limit <= 0)
  5621. + hitLimit = 1;
  5622. + }
  5623. +
  5624. + }
  5625. +
  5626. + yaffs_PutLevel0Tnode(dev, tn, i, 0);
  5627. + }
  5628. +
  5629. + }
  5630. + return (i < 0) ? 1 : 0;
  5631. +
  5632. + }
  5633. +
  5634. + }
  5635. +
  5636. + return 1;
  5637. +
  5638. +}
  5639. +
  5640. +static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
  5641. +{
  5642. + yaffs_BlockInfo *theBlock;
  5643. +
  5644. + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
  5645. +
  5646. + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
  5647. + if (theBlock) {
  5648. + theBlock->softDeletions++;
  5649. + dev->nFreeChunks++;
  5650. + }
  5651. +}
  5652. +
  5653. +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  5654. + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  5655. + * of the tnode.
  5656. + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  5657. + */
  5658. +
  5659. +static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
  5660. + __u32 level, int chunkOffset)
  5661. +{
  5662. + int i;
  5663. + int theChunk;
  5664. + int allDone = 1;
  5665. + yaffs_Device *dev = in->myDev;
  5666. +
  5667. + if (tn) {
  5668. + if (level > 0) {
  5669. +
  5670. + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  5671. + i--) {
  5672. + if (tn->internal[i]) {
  5673. + allDone =
  5674. + yaffs_SoftDeleteWorker(in,
  5675. + tn->
  5676. + internal[i],
  5677. + level - 1,
  5678. + (chunkOffset
  5679. + <<
  5680. + YAFFS_TNODES_INTERNAL_BITS)
  5681. + + i);
  5682. + if (allDone) {
  5683. + yaffs_FreeTnode(dev,
  5684. + tn->
  5685. + internal[i]);
  5686. + tn->internal[i] = NULL;
  5687. + } else {
  5688. + /* Hoosterman... how could this happen? */
  5689. + }
  5690. + }
  5691. + }
  5692. + return (allDone) ? 1 : 0;
  5693. + } else if (level == 0) {
  5694. +
  5695. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  5696. + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  5697. + if (theChunk) {
  5698. + /* Note this does not find the real chunk, only the chunk group.
  5699. + * We make an assumption that a chunk group is not larger than
  5700. + * a block.
  5701. + */
  5702. + yaffs_SoftDeleteChunk(dev, theChunk);
  5703. + yaffs_PutLevel0Tnode(dev, tn, i, 0);
  5704. + }
  5705. +
  5706. + }
  5707. + return 1;
  5708. +
  5709. + }
  5710. +
  5711. + }
  5712. +
  5713. + return 1;
  5714. +
  5715. +}
  5716. +
  5717. +static void yaffs_SoftDeleteFile(yaffs_Object *obj)
  5718. +{
  5719. + if (obj->deleted &&
  5720. + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
  5721. + if (obj->nDataChunks <= 0) {
  5722. + /* Empty file with no duplicate object headers, just delete it immediately */
  5723. + yaffs_FreeTnode(obj->myDev,
  5724. + obj->variant.fileVariant.top);
  5725. + obj->variant.fileVariant.top = NULL;
  5726. + T(YAFFS_TRACE_TRACING,
  5727. + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
  5728. + obj->objectId));
  5729. + yaffs_DoGenericObjectDeletion(obj);
  5730. + } else {
  5731. + yaffs_SoftDeleteWorker(obj,
  5732. + obj->variant.fileVariant.top,
  5733. + obj->variant.fileVariant.
  5734. + topLevel, 0);
  5735. + obj->softDeleted = 1;
  5736. + }
  5737. + }
  5738. +}
  5739. +
  5740. +/* Pruning removes any part of the file structure tree that is beyond the
  5741. + * bounds of the file (ie that does not point to chunks).
  5742. + *
  5743. + * A file should only get pruned when its size is reduced.
  5744. + *
  5745. + * Before pruning, the chunks must be pulled from the tree and the
  5746. + * level 0 tnode entries must be zeroed out.
  5747. + * Could also use this for file deletion, but that's probably better handled
  5748. + * by a special case.
  5749. + */
  5750. +
  5751. +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
  5752. + __u32 level, int del0)
  5753. +{
  5754. + int i;
  5755. + int hasData;
  5756. +
  5757. + if (tn) {
  5758. + hasData = 0;
  5759. +
  5760. + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  5761. + if (tn->internal[i] && level > 0) {
  5762. + tn->internal[i] =
  5763. + yaffs_PruneWorker(dev, tn->internal[i],
  5764. + level - 1,
  5765. + (i == 0) ? del0 : 1);
  5766. + }
  5767. +
  5768. + if (tn->internal[i])
  5769. + hasData++;
  5770. + }
  5771. +
  5772. + if (hasData == 0 && del0) {
  5773. + /* Free and return NULL */
  5774. +
  5775. + yaffs_FreeTnode(dev, tn);
  5776. + tn = NULL;
  5777. + }
  5778. +
  5779. + }
  5780. +
  5781. + return tn;
  5782. +
  5783. +}
  5784. +
  5785. +static int yaffs_PruneFileStructure(yaffs_Device *dev,
  5786. + yaffs_FileStructure *fStruct)
  5787. +{
  5788. + int i;
  5789. + int hasData;
  5790. + int done = 0;
  5791. + yaffs_Tnode *tn;
  5792. +
  5793. + if (fStruct->topLevel > 0) {
  5794. + fStruct->top =
  5795. + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
  5796. +
  5797. + /* Now we have a tree with all the non-zero branches NULL but the height
  5798. + * is the same as it was.
  5799. + * Let's see if we can trim internal tnodes to shorten the tree.
  5800. + * We can do this if only the 0th element in the tnode is in use
  5801. + * (ie all the non-zero are NULL)
  5802. + */
  5803. +
  5804. + while (fStruct->topLevel && !done) {
  5805. + tn = fStruct->top;
  5806. +
  5807. + hasData = 0;
  5808. + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  5809. + if (tn->internal[i])
  5810. + hasData++;
  5811. + }
  5812. +
  5813. + if (!hasData) {
  5814. + fStruct->top = tn->internal[0];
  5815. + fStruct->topLevel--;
  5816. + yaffs_FreeTnode(dev, tn);
  5817. + } else {
  5818. + done = 1;
  5819. + }
  5820. + }
  5821. + }
  5822. +
  5823. + return YAFFS_OK;
  5824. +}
  5825. +
  5826. +/*-------------------- End of File Structure functions.-------------------*/
  5827. +
  5828. +/* yaffs_CreateFreeObjects creates a bunch more objects and
  5829. + * adds them to the object free list.
  5830. + */
  5831. +static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
  5832. +{
  5833. + int i;
  5834. + yaffs_Object *newObjects;
  5835. + yaffs_ObjectList *list;
  5836. +
  5837. + if (nObjects < 1)
  5838. + return YAFFS_OK;
  5839. +
  5840. + /* make these things */
  5841. + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
  5842. + list = YMALLOC(sizeof(yaffs_ObjectList));
  5843. +
  5844. + if (!newObjects || !list) {
  5845. + if (newObjects)
  5846. + YFREE(newObjects);
  5847. + if (list)
  5848. + YFREE(list);
  5849. + T(YAFFS_TRACE_ALLOCATE,
  5850. + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
  5851. + return YAFFS_FAIL;
  5852. + }
  5853. +
  5854. + /* Hook them into the free list */
  5855. + for (i = 0; i < nObjects - 1; i++) {
  5856. + newObjects[i].siblings.next =
  5857. + (struct ylist_head *)(&newObjects[i + 1]);
  5858. + }
  5859. +
  5860. + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
  5861. + dev->freeObjects = newObjects;
  5862. + dev->nFreeObjects += nObjects;
  5863. + dev->nObjectsCreated += nObjects;
  5864. +
  5865. + /* Now add this bunch of Objects to a list for freeing up. */
  5866. +
  5867. + list->objects = newObjects;
  5868. + list->next = dev->allocatedObjectList;
  5869. + dev->allocatedObjectList = list;
  5870. +
  5871. + return YAFFS_OK;
  5872. +}
  5873. +
  5874. +
  5875. +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  5876. +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
  5877. +{
  5878. + yaffs_Object *tn = NULL;
  5879. +
  5880. +#ifdef VALGRIND_TEST
  5881. + tn = YMALLOC(sizeof(yaffs_Object));
  5882. +#else
  5883. + /* If there are none left make more */
  5884. + if (!dev->freeObjects)
  5885. + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
  5886. +
  5887. + if (dev->freeObjects) {
  5888. + tn = dev->freeObjects;
  5889. + dev->freeObjects =
  5890. + (yaffs_Object *) (dev->freeObjects->siblings.next);
  5891. + dev->nFreeObjects--;
  5892. + }
  5893. +#endif
  5894. + if (tn) {
  5895. + /* Now sweeten it up... */
  5896. +
  5897. + memset(tn, 0, sizeof(yaffs_Object));
  5898. + tn->beingCreated = 1;
  5899. +
  5900. + tn->myDev = dev;
  5901. + tn->hdrChunk = 0;
  5902. + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
  5903. + YINIT_LIST_HEAD(&(tn->hardLinks));
  5904. + YINIT_LIST_HEAD(&(tn->hashLink));
  5905. + YINIT_LIST_HEAD(&tn->siblings);
  5906. +
  5907. +
  5908. + /* Now make the directory sane */
  5909. + if (dev->rootDir) {
  5910. + tn->parent = dev->rootDir;
  5911. + ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
  5912. + }
  5913. +
  5914. + /* Add it to the lost and found directory.
  5915. + * NB Can't put root or lostNFound in lostNFound so
  5916. + * check if lostNFound exists first
  5917. + */
  5918. + if (dev->lostNFoundDir)
  5919. + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
  5920. +
  5921. + tn->beingCreated = 0;
  5922. + }
  5923. +
  5924. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5925. +
  5926. + return tn;
  5927. +}
  5928. +
  5929. +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
  5930. + __u32 mode)
  5931. +{
  5932. +
  5933. + yaffs_Object *obj =
  5934. + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  5935. + if (obj) {
  5936. + obj->fake = 1; /* it is fake so it might have no NAND presence... */
  5937. + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
  5938. + obj->unlinkAllowed = 0; /* ... or unlink it */
  5939. + obj->deleted = 0;
  5940. + obj->unlinked = 0;
  5941. + obj->yst_mode = mode;
  5942. + obj->myDev = dev;
  5943. + obj->hdrChunk = 0; /* Not a valid chunk. */
  5944. + }
  5945. +
  5946. + return obj;
  5947. +
  5948. +}
  5949. +
  5950. +static void yaffs_UnhashObject(yaffs_Object *tn)
  5951. +{
  5952. + int bucket;
  5953. + yaffs_Device *dev = tn->myDev;
  5954. +
  5955. + /* If it is still linked into the bucket list, free from the list */
  5956. + if (!ylist_empty(&tn->hashLink)) {
  5957. + ylist_del_init(&tn->hashLink);
  5958. + bucket = yaffs_HashFunction(tn->objectId);
  5959. + dev->objectBucket[bucket].count--;
  5960. + }
  5961. +}
  5962. +
  5963. +/* FreeObject frees up a Object and puts it back on the free list */
  5964. +static void yaffs_FreeObject(yaffs_Object *tn)
  5965. +{
  5966. + yaffs_Device *dev = tn->myDev;
  5967. +
  5968. +#ifdef __KERNEL__
  5969. + T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
  5970. +#endif
  5971. +
  5972. + if (tn->parent)
  5973. + YBUG();
  5974. + if (!ylist_empty(&tn->siblings))
  5975. + YBUG();
  5976. +
  5977. +
  5978. +#ifdef __KERNEL__
  5979. + if (tn->myInode) {
  5980. + /* We're still hooked up to a cached inode.
  5981. + * Don't delete now, but mark for later deletion
  5982. + */
  5983. + tn->deferedFree = 1;
  5984. + return;
  5985. + }
  5986. +#endif
  5987. +
  5988. + yaffs_UnhashObject(tn);
  5989. +
  5990. +#ifdef VALGRIND_TEST
  5991. + YFREE(tn);
  5992. +#else
  5993. + /* Link into the free list. */
  5994. + tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
  5995. + dev->freeObjects = tn;
  5996. + dev->nFreeObjects++;
  5997. +#endif
  5998. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5999. +}
  6000. +
  6001. +#ifdef __KERNEL__
  6002. +
  6003. +void yaffs_HandleDeferedFree(yaffs_Object *obj)
  6004. +{
  6005. + if (obj->deferedFree)
  6006. + yaffs_FreeObject(obj);
  6007. +}
  6008. +
  6009. +#endif
  6010. +
  6011. +static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
  6012. +{
  6013. + /* Free the list of allocated Objects */
  6014. +
  6015. + yaffs_ObjectList *tmp;
  6016. +
  6017. + while (dev->allocatedObjectList) {
  6018. + tmp = dev->allocatedObjectList->next;
  6019. + YFREE(dev->allocatedObjectList->objects);
  6020. + YFREE(dev->allocatedObjectList);
  6021. +
  6022. + dev->allocatedObjectList = tmp;
  6023. + }
  6024. +
  6025. + dev->freeObjects = NULL;
  6026. + dev->nFreeObjects = 0;
  6027. +}
  6028. +
  6029. +static void yaffs_InitialiseObjects(yaffs_Device *dev)
  6030. +{
  6031. + int i;
  6032. +
  6033. + dev->allocatedObjectList = NULL;
  6034. + dev->freeObjects = NULL;
  6035. + dev->nFreeObjects = 0;
  6036. +
  6037. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  6038. + YINIT_LIST_HEAD(&dev->objectBucket[i].list);
  6039. + dev->objectBucket[i].count = 0;
  6040. + }
  6041. +}
  6042. +
  6043. +static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
  6044. +{
  6045. + static int x;
  6046. + int i;
  6047. + int l = 999;
  6048. + int lowest = 999999;
  6049. +
  6050. + /* First let's see if we can find one that's empty. */
  6051. +
  6052. + for (i = 0; i < 10 && lowest > 0; i++) {
  6053. + x++;
  6054. + x %= YAFFS_NOBJECT_BUCKETS;
  6055. + if (dev->objectBucket[x].count < lowest) {
  6056. + lowest = dev->objectBucket[x].count;
  6057. + l = x;
  6058. + }
  6059. +
  6060. + }
  6061. +
  6062. + /* If we didn't find an empty list, then try
  6063. + * looking a bit further for a short one
  6064. + */
  6065. +
  6066. + for (i = 0; i < 10 && lowest > 3; i++) {
  6067. + x++;
  6068. + x %= YAFFS_NOBJECT_BUCKETS;
  6069. + if (dev->objectBucket[x].count < lowest) {
  6070. + lowest = dev->objectBucket[x].count;
  6071. + l = x;
  6072. + }
  6073. +
  6074. + }
  6075. +
  6076. + return l;
  6077. +}
  6078. +
  6079. +static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
  6080. +{
  6081. + int bucket = yaffs_FindNiceObjectBucket(dev);
  6082. +
  6083. + /* Now find an object value that has not already been taken
  6084. + * by scanning the list.
  6085. + */
  6086. +
  6087. + int found = 0;
  6088. + struct ylist_head *i;
  6089. +
  6090. + __u32 n = (__u32) bucket;
  6091. +
  6092. + /* yaffs_CheckObjectHashSanity(); */
  6093. +
  6094. + while (!found) {
  6095. + found = 1;
  6096. + n += YAFFS_NOBJECT_BUCKETS;
  6097. + if (1 || dev->objectBucket[bucket].count > 0) {
  6098. + ylist_for_each(i, &dev->objectBucket[bucket].list) {
  6099. + /* If there is already one in the list */
  6100. + if (i && ylist_entry(i, yaffs_Object,
  6101. + hashLink)->objectId == n) {
  6102. + found = 0;
  6103. + }
  6104. + }
  6105. + }
  6106. + }
  6107. +
  6108. + return n;
  6109. +}
  6110. +
  6111. +static void yaffs_HashObject(yaffs_Object *in)
  6112. +{
  6113. + int bucket = yaffs_HashFunction(in->objectId);
  6114. + yaffs_Device *dev = in->myDev;
  6115. +
  6116. + ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
  6117. + dev->objectBucket[bucket].count++;
  6118. +}
  6119. +
  6120. +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
  6121. +{
  6122. + int bucket = yaffs_HashFunction(number);
  6123. + struct ylist_head *i;
  6124. + yaffs_Object *in;
  6125. +
  6126. + ylist_for_each(i, &dev->objectBucket[bucket].list) {
  6127. + /* Look if it is in the list */
  6128. + if (i) {
  6129. + in = ylist_entry(i, yaffs_Object, hashLink);
  6130. + if (in->objectId == number) {
  6131. +#ifdef __KERNEL__
  6132. + /* Don't tell the VFS about this one if it is defered free */
  6133. + if (in->deferedFree)
  6134. + return NULL;
  6135. +#endif
  6136. +
  6137. + return in;
  6138. + }
  6139. + }
  6140. + }
  6141. +
  6142. + return NULL;
  6143. +}
  6144. +
  6145. +yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
  6146. + yaffs_ObjectType type)
  6147. +{
  6148. + yaffs_Object *theObject;
  6149. + yaffs_Tnode *tn = NULL;
  6150. +
  6151. + if (number < 0)
  6152. + number = yaffs_CreateNewObjectNumber(dev);
  6153. +
  6154. + theObject = yaffs_AllocateEmptyObject(dev);
  6155. + if (!theObject)
  6156. + return NULL;
  6157. +
  6158. + if (type == YAFFS_OBJECT_TYPE_FILE) {
  6159. + tn = yaffs_GetTnode(dev);
  6160. + if (!tn) {
  6161. + yaffs_FreeObject(theObject);
  6162. + return NULL;
  6163. + }
  6164. + }
  6165. +
  6166. + if (theObject) {
  6167. + theObject->fake = 0;
  6168. + theObject->renameAllowed = 1;
  6169. + theObject->unlinkAllowed = 1;
  6170. + theObject->objectId = number;
  6171. + yaffs_HashObject(theObject);
  6172. + theObject->variantType = type;
  6173. +#ifdef CONFIG_YAFFS_WINCE
  6174. + yfsd_WinFileTimeNow(theObject->win_atime);
  6175. + theObject->win_ctime[0] = theObject->win_mtime[0] =
  6176. + theObject->win_atime[0];
  6177. + theObject->win_ctime[1] = theObject->win_mtime[1] =
  6178. + theObject->win_atime[1];
  6179. +
  6180. +#else
  6181. +
  6182. + theObject->yst_atime = theObject->yst_mtime =
  6183. + theObject->yst_ctime = Y_CURRENT_TIME;
  6184. +#endif
  6185. + switch (type) {
  6186. + case YAFFS_OBJECT_TYPE_FILE:
  6187. + theObject->variant.fileVariant.fileSize = 0;
  6188. + theObject->variant.fileVariant.scannedFileSize = 0;
  6189. + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
  6190. + theObject->variant.fileVariant.topLevel = 0;
  6191. + theObject->variant.fileVariant.top = tn;
  6192. + break;
  6193. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6194. + YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
  6195. + children);
  6196. + break;
  6197. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6198. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6199. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6200. + /* No action required */
  6201. + break;
  6202. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  6203. + /* todo this should not happen */
  6204. + break;
  6205. + }
  6206. + }
  6207. +
  6208. + return theObject;
  6209. +}
  6210. +
  6211. +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
  6212. + int number,
  6213. + yaffs_ObjectType type)
  6214. +{
  6215. + yaffs_Object *theObject = NULL;
  6216. +
  6217. + if (number > 0)
  6218. + theObject = yaffs_FindObjectByNumber(dev, number);
  6219. +
  6220. + if (!theObject)
  6221. + theObject = yaffs_CreateNewObject(dev, number, type);
  6222. +
  6223. + return theObject;
  6224. +
  6225. +}
  6226. +
  6227. +
  6228. +static YCHAR *yaffs_CloneString(const YCHAR *str)
  6229. +{
  6230. + YCHAR *newStr = NULL;
  6231. +
  6232. + if (str && *str) {
  6233. + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
  6234. + if (newStr)
  6235. + yaffs_strcpy(newStr, str);
  6236. + }
  6237. +
  6238. + return newStr;
  6239. +
  6240. +}
  6241. +
  6242. +/*
  6243. + * Mknod (create) a new object.
  6244. + * equivalentObject only has meaning for a hard link;
  6245. + * aliasString only has meaning for a sumlink.
  6246. + * rdev only has meaning for devices (a subset of special objects)
  6247. + */
  6248. +
  6249. +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
  6250. + yaffs_Object *parent,
  6251. + const YCHAR *name,
  6252. + __u32 mode,
  6253. + __u32 uid,
  6254. + __u32 gid,
  6255. + yaffs_Object *equivalentObject,
  6256. + const YCHAR *aliasString, __u32 rdev)
  6257. +{
  6258. + yaffs_Object *in;
  6259. + YCHAR *str = NULL;
  6260. +
  6261. + yaffs_Device *dev = parent->myDev;
  6262. +
  6263. + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
  6264. + if (yaffs_FindObjectByName(parent, name))
  6265. + return NULL;
  6266. +
  6267. + in = yaffs_CreateNewObject(dev, -1, type);
  6268. +
  6269. + if (!in)
  6270. + return YAFFS_FAIL;
  6271. +
  6272. + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6273. + str = yaffs_CloneString(aliasString);
  6274. + if (!str) {
  6275. + yaffs_FreeObject(in);
  6276. + return NULL;
  6277. + }
  6278. + }
  6279. +
  6280. +
  6281. +
  6282. + if (in) {
  6283. + in->hdrChunk = 0;
  6284. + in->valid = 1;
  6285. + in->variantType = type;
  6286. +
  6287. + in->yst_mode = mode;
  6288. +
  6289. +#ifdef CONFIG_YAFFS_WINCE
  6290. + yfsd_WinFileTimeNow(in->win_atime);
  6291. + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
  6292. + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
  6293. +
  6294. +#else
  6295. + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
  6296. +
  6297. + in->yst_rdev = rdev;
  6298. + in->yst_uid = uid;
  6299. + in->yst_gid = gid;
  6300. +#endif
  6301. + in->nDataChunks = 0;
  6302. +
  6303. + yaffs_SetObjectName(in, name);
  6304. + in->dirty = 1;
  6305. +
  6306. + yaffs_AddObjectToDirectory(parent, in);
  6307. +
  6308. + in->myDev = parent->myDev;
  6309. +
  6310. + switch (type) {
  6311. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6312. + in->variant.symLinkVariant.alias = str;
  6313. + break;
  6314. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6315. + in->variant.hardLinkVariant.equivalentObject =
  6316. + equivalentObject;
  6317. + in->variant.hardLinkVariant.equivalentObjectId =
  6318. + equivalentObject->objectId;
  6319. + ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
  6320. + break;
  6321. + case YAFFS_OBJECT_TYPE_FILE:
  6322. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6323. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6324. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  6325. + /* do nothing */
  6326. + break;
  6327. + }
  6328. +
  6329. + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
  6330. + /* Could not create the object header, fail the creation */
  6331. + yaffs_DeleteObject(in);
  6332. + in = NULL;
  6333. + }
  6334. +
  6335. + }
  6336. +
  6337. + return in;
  6338. +}
  6339. +
  6340. +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
  6341. + __u32 mode, __u32 uid, __u32 gid)
  6342. +{
  6343. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  6344. + uid, gid, NULL, NULL, 0);
  6345. +}
  6346. +
  6347. +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
  6348. + __u32 mode, __u32 uid, __u32 gid)
  6349. +{
  6350. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  6351. + mode, uid, gid, NULL, NULL, 0);
  6352. +}
  6353. +
  6354. +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
  6355. + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
  6356. +{
  6357. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  6358. + uid, gid, NULL, NULL, rdev);
  6359. +}
  6360. +
  6361. +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
  6362. + __u32 mode, __u32 uid, __u32 gid,
  6363. + const YCHAR *alias)
  6364. +{
  6365. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  6366. + uid, gid, NULL, alias, 0);
  6367. +}
  6368. +
  6369. +/* yaffs_Link returns the object id of the equivalent object.*/
  6370. +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
  6371. + yaffs_Object *equivalentObject)
  6372. +{
  6373. + /* Get the real object in case we were fed a hard link as an equivalent object */
  6374. + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
  6375. +
  6376. + if (yaffs_MknodObject
  6377. + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  6378. + equivalentObject, NULL, 0)) {
  6379. + return equivalentObject;
  6380. + } else {
  6381. + return NULL;
  6382. + }
  6383. +
  6384. +}
  6385. +
  6386. +static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
  6387. + const YCHAR *newName, int force, int shadows)
  6388. +{
  6389. + int unlinkOp;
  6390. + int deleteOp;
  6391. +
  6392. + yaffs_Object *existingTarget;
  6393. +
  6394. + if (newDir == NULL)
  6395. + newDir = obj->parent; /* use the old directory */
  6396. +
  6397. + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6398. + T(YAFFS_TRACE_ALWAYS,
  6399. + (TSTR
  6400. + ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
  6401. + TENDSTR)));
  6402. + YBUG();
  6403. + }
  6404. +
  6405. + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  6406. + if (obj->myDev->isYaffs2)
  6407. + unlinkOp = (newDir == obj->myDev->unlinkedDir);
  6408. + else
  6409. + unlinkOp = (newDir == obj->myDev->unlinkedDir
  6410. + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
  6411. +
  6412. + deleteOp = (newDir == obj->myDev->deletedDir);
  6413. +
  6414. + existingTarget = yaffs_FindObjectByName(newDir, newName);
  6415. +
  6416. + /* If the object is a file going into the unlinked directory,
  6417. + * then it is OK to just stuff it in since duplicate names are allowed.
  6418. + * else only proceed if the new name does not exist and if we're putting
  6419. + * it into a directory.
  6420. + */
  6421. + if ((unlinkOp ||
  6422. + deleteOp ||
  6423. + force ||
  6424. + (shadows > 0) ||
  6425. + !existingTarget) &&
  6426. + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
  6427. + yaffs_SetObjectName(obj, newName);
  6428. + obj->dirty = 1;
  6429. +
  6430. + yaffs_AddObjectToDirectory(newDir, obj);
  6431. +
  6432. + if (unlinkOp)
  6433. + obj->unlinked = 1;
  6434. +
  6435. + /* If it is a deletion then we mark it as a shrink for gc purposes. */
  6436. + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
  6437. + return YAFFS_OK;
  6438. + }
  6439. +
  6440. + return YAFFS_FAIL;
  6441. +}
  6442. +
  6443. +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
  6444. + yaffs_Object *newDir, const YCHAR *newName)
  6445. +{
  6446. + yaffs_Object *obj = NULL;
  6447. + yaffs_Object *existingTarget = NULL;
  6448. + int force = 0;
  6449. +
  6450. +
  6451. + if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
  6452. + YBUG();
  6453. + if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
  6454. + YBUG();
  6455. +
  6456. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  6457. + /* Special case for case insemsitive systems (eg. WinCE).
  6458. + * While look-up is case insensitive, the name isn't.
  6459. + * Therefore we might want to change x.txt to X.txt
  6460. + */
  6461. + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
  6462. + force = 1;
  6463. +#endif
  6464. +
  6465. + else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
  6466. + /* ENAMETOOLONG */
  6467. + return YAFFS_FAIL;
  6468. +
  6469. + obj = yaffs_FindObjectByName(oldDir, oldName);
  6470. +
  6471. + if (obj && obj->renameAllowed) {
  6472. +
  6473. + /* Now do the handling for an existing target, if there is one */
  6474. +
  6475. + existingTarget = yaffs_FindObjectByName(newDir, newName);
  6476. + if (existingTarget &&
  6477. + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
  6478. + !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
  6479. + /* There is a target that is a non-empty directory, so we fail */
  6480. + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
  6481. + } else if (existingTarget && existingTarget != obj) {
  6482. + /* Nuke the target first, using shadowing,
  6483. + * but only if it isn't the same object
  6484. + */
  6485. + yaffs_ChangeObjectName(obj, newDir, newName, force,
  6486. + existingTarget->objectId);
  6487. + yaffs_UnlinkObject(existingTarget);
  6488. + }
  6489. +
  6490. + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
  6491. + }
  6492. + return YAFFS_FAIL;
  6493. +}
  6494. +
  6495. +/*------------------------- Block Management and Page Allocation ----------------*/
  6496. +
  6497. +static int yaffs_InitialiseBlocks(yaffs_Device *dev)
  6498. +{
  6499. + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  6500. +
  6501. + dev->blockInfo = NULL;
  6502. + dev->chunkBits = NULL;
  6503. +
  6504. + dev->allocationBlock = -1; /* force it to get a new one */
  6505. +
  6506. + /* If the first allocation strategy fails, thry the alternate one */
  6507. + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
  6508. + if (!dev->blockInfo) {
  6509. + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
  6510. + dev->blockInfoAlt = 1;
  6511. + } else
  6512. + dev->blockInfoAlt = 0;
  6513. +
  6514. + if (dev->blockInfo) {
  6515. + /* Set up dynamic blockinfo stuff. */
  6516. + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
  6517. + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
  6518. + if (!dev->chunkBits) {
  6519. + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
  6520. + dev->chunkBitsAlt = 1;
  6521. + } else
  6522. + dev->chunkBitsAlt = 0;
  6523. + }
  6524. +
  6525. + if (dev->blockInfo && dev->chunkBits) {
  6526. + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
  6527. + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
  6528. + return YAFFS_OK;
  6529. + }
  6530. +
  6531. + return YAFFS_FAIL;
  6532. +}
  6533. +
  6534. +static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
  6535. +{
  6536. + if (dev->blockInfoAlt && dev->blockInfo)
  6537. + YFREE_ALT(dev->blockInfo);
  6538. + else if (dev->blockInfo)
  6539. + YFREE(dev->blockInfo);
  6540. +
  6541. + dev->blockInfoAlt = 0;
  6542. +
  6543. + dev->blockInfo = NULL;
  6544. +
  6545. + if (dev->chunkBitsAlt && dev->chunkBits)
  6546. + YFREE_ALT(dev->chunkBits);
  6547. + else if (dev->chunkBits)
  6548. + YFREE(dev->chunkBits);
  6549. + dev->chunkBitsAlt = 0;
  6550. + dev->chunkBits = NULL;
  6551. +}
  6552. +
  6553. +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
  6554. + yaffs_BlockInfo *bi)
  6555. +{
  6556. + int i;
  6557. + __u32 seq;
  6558. + yaffs_BlockInfo *b;
  6559. +
  6560. + if (!dev->isYaffs2)
  6561. + return 1; /* disqualification only applies to yaffs2. */
  6562. +
  6563. + if (!bi->hasShrinkHeader)
  6564. + return 1; /* can gc */
  6565. +
  6566. + /* Find the oldest dirty sequence number if we don't know it and save it
  6567. + * so we don't have to keep recomputing it.
  6568. + */
  6569. + if (!dev->oldestDirtySequence) {
  6570. + seq = dev->sequenceNumber;
  6571. +
  6572. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
  6573. + i++) {
  6574. + b = yaffs_GetBlockInfo(dev, i);
  6575. + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
  6576. + (b->pagesInUse - b->softDeletions) <
  6577. + dev->nChunksPerBlock && b->sequenceNumber < seq) {
  6578. + seq = b->sequenceNumber;
  6579. + }
  6580. + }
  6581. + dev->oldestDirtySequence = seq;
  6582. + }
  6583. +
  6584. + /* Can't do gc of this block if there are any blocks older than this one that have
  6585. + * discarded pages.
  6586. + */
  6587. + return (bi->sequenceNumber <= dev->oldestDirtySequence);
  6588. +}
  6589. +
  6590. +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
  6591. + * for garbage collection.
  6592. + */
  6593. +
  6594. +static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
  6595. + int aggressive)
  6596. +{
  6597. + int b = dev->currentDirtyChecker;
  6598. +
  6599. + int i;
  6600. + int iterations;
  6601. + int dirtiest = -1;
  6602. + int pagesInUse = 0;
  6603. + int prioritised = 0;
  6604. + yaffs_BlockInfo *bi;
  6605. + int pendingPrioritisedExist = 0;
  6606. +
  6607. + /* First let's see if we need to grab a prioritised block */
  6608. + if (dev->hasPendingPrioritisedGCs) {
  6609. + for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
  6610. +
  6611. + bi = yaffs_GetBlockInfo(dev, i);
  6612. + /* yaffs_VerifyBlock(dev,bi,i); */
  6613. +
  6614. + if (bi->gcPrioritise) {
  6615. + pendingPrioritisedExist = 1;
  6616. + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  6617. + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
  6618. + pagesInUse = (bi->pagesInUse - bi->softDeletions);
  6619. + dirtiest = i;
  6620. + prioritised = 1;
  6621. + aggressive = 1; /* Fool the non-aggressive skip logiv below */
  6622. + }
  6623. + }
  6624. + }
  6625. +
  6626. + if (!pendingPrioritisedExist) /* None found, so we can clear this */
  6627. + dev->hasPendingPrioritisedGCs = 0;
  6628. + }
  6629. +
  6630. + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  6631. + * search harder.
  6632. + * else (we're doing a leasurely gc), then we only bother to do this if the
  6633. + * block has only a few pages in use.
  6634. + */
  6635. +
  6636. + dev->nonAggressiveSkip--;
  6637. +
  6638. + if (!aggressive && (dev->nonAggressiveSkip > 0))
  6639. + return -1;
  6640. +
  6641. + if (!prioritised)
  6642. + pagesInUse =
  6643. + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
  6644. +
  6645. + if (aggressive)
  6646. + iterations =
  6647. + dev->internalEndBlock - dev->internalStartBlock + 1;
  6648. + else {
  6649. + iterations =
  6650. + dev->internalEndBlock - dev->internalStartBlock + 1;
  6651. + iterations = iterations / 16;
  6652. + if (iterations > 200)
  6653. + iterations = 200;
  6654. + }
  6655. +
  6656. + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
  6657. + b++;
  6658. + if (b < dev->internalStartBlock || b > dev->internalEndBlock)
  6659. + b = dev->internalStartBlock;
  6660. +
  6661. + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
  6662. + T(YAFFS_TRACE_ERROR,
  6663. + (TSTR("**>> Block %d is not valid" TENDSTR), b));
  6664. + YBUG();
  6665. + }
  6666. +
  6667. + bi = yaffs_GetBlockInfo(dev, b);
  6668. +
  6669. + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  6670. + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
  6671. + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
  6672. + dirtiest = b;
  6673. + pagesInUse = (bi->pagesInUse - bi->softDeletions);
  6674. + }
  6675. + }
  6676. +
  6677. + dev->currentDirtyChecker = b;
  6678. +
  6679. + if (dirtiest > 0) {
  6680. + T(YAFFS_TRACE_GC,
  6681. + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
  6682. + dev->nChunksPerBlock - pagesInUse, prioritised));
  6683. + }
  6684. +
  6685. + dev->oldestDirtySequence = 0;
  6686. +
  6687. + if (dirtiest > 0)
  6688. + dev->nonAggressiveSkip = 4;
  6689. +
  6690. + return dirtiest;
  6691. +}
  6692. +
  6693. +static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
  6694. +{
  6695. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
  6696. +
  6697. + int erasedOk = 0;
  6698. +
  6699. + /* If the block is still healthy erase it and mark as clean.
  6700. + * If the block has had a data failure, then retire it.
  6701. + */
  6702. +
  6703. + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  6704. + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
  6705. + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
  6706. +
  6707. + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
  6708. +
  6709. + if (!bi->needsRetiring) {
  6710. + yaffs_InvalidateCheckpoint(dev);
  6711. + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
  6712. + if (!erasedOk) {
  6713. + dev->nErasureFailures++;
  6714. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  6715. + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
  6716. + }
  6717. + }
  6718. +
  6719. + if (erasedOk &&
  6720. + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
  6721. + int i;
  6722. + for (i = 0; i < dev->nChunksPerBlock; i++) {
  6723. + if (!yaffs_CheckChunkErased
  6724. + (dev, blockNo * dev->nChunksPerBlock + i)) {
  6725. + T(YAFFS_TRACE_ERROR,
  6726. + (TSTR
  6727. + (">>Block %d erasure supposedly OK, but chunk %d not erased"
  6728. + TENDSTR), blockNo, i));
  6729. + }
  6730. + }
  6731. + }
  6732. +
  6733. + if (erasedOk) {
  6734. + /* Clean it up... */
  6735. + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
  6736. + dev->nErasedBlocks++;
  6737. + bi->pagesInUse = 0;
  6738. + bi->softDeletions = 0;
  6739. + bi->hasShrinkHeader = 0;
  6740. + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
  6741. + bi->gcPrioritise = 0;
  6742. + yaffs_ClearChunkBits(dev, blockNo);
  6743. +
  6744. + T(YAFFS_TRACE_ERASE,
  6745. + (TSTR("Erased block %d" TENDSTR), blockNo));
  6746. + } else {
  6747. + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
  6748. +
  6749. + yaffs_RetireBlock(dev, blockNo);
  6750. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  6751. + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
  6752. + }
  6753. +}
  6754. +
  6755. +static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
  6756. +{
  6757. + int i;
  6758. +
  6759. + yaffs_BlockInfo *bi;
  6760. +
  6761. + if (dev->nErasedBlocks < 1) {
  6762. + /* Hoosterman we've got a problem.
  6763. + * Can't get space to gc
  6764. + */
  6765. + T(YAFFS_TRACE_ERROR,
  6766. + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
  6767. +
  6768. + return -1;
  6769. + }
  6770. +
  6771. + /* Find an empty block. */
  6772. +
  6773. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  6774. + dev->allocationBlockFinder++;
  6775. + if (dev->allocationBlockFinder < dev->internalStartBlock
  6776. + || dev->allocationBlockFinder > dev->internalEndBlock) {
  6777. + dev->allocationBlockFinder = dev->internalStartBlock;
  6778. + }
  6779. +
  6780. + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
  6781. +
  6782. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
  6783. + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
  6784. + dev->sequenceNumber++;
  6785. + bi->sequenceNumber = dev->sequenceNumber;
  6786. + dev->nErasedBlocks--;
  6787. + T(YAFFS_TRACE_ALLOCATE,
  6788. + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
  6789. + dev->allocationBlockFinder, dev->sequenceNumber,
  6790. + dev->nErasedBlocks));
  6791. + return dev->allocationBlockFinder;
  6792. + }
  6793. + }
  6794. +
  6795. + T(YAFFS_TRACE_ALWAYS,
  6796. + (TSTR
  6797. + ("yaffs tragedy: no more erased blocks, but there should have been %d"
  6798. + TENDSTR), dev->nErasedBlocks));
  6799. +
  6800. + return -1;
  6801. +}
  6802. +
  6803. +
  6804. +
  6805. +static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
  6806. +{
  6807. + if (!dev->nCheckpointBlocksRequired &&
  6808. + dev->isYaffs2) {
  6809. + /* Not a valid value so recalculate */
  6810. + int nBytes = 0;
  6811. + int nBlocks;
  6812. + int devBlocks = (dev->endBlock - dev->startBlock + 1);
  6813. + int tnodeSize;
  6814. +
  6815. + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  6816. +
  6817. + if (tnodeSize < sizeof(yaffs_Tnode))
  6818. + tnodeSize = sizeof(yaffs_Tnode);
  6819. +
  6820. + nBytes += sizeof(yaffs_CheckpointValidity);
  6821. + nBytes += sizeof(yaffs_CheckpointDevice);
  6822. + nBytes += devBlocks * sizeof(yaffs_BlockInfo);
  6823. + nBytes += devBlocks * dev->chunkBitmapStride;
  6824. + nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
  6825. + nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
  6826. + nBytes += sizeof(yaffs_CheckpointValidity);
  6827. + nBytes += sizeof(__u32); /* checksum*/
  6828. +
  6829. + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
  6830. +
  6831. + nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->nChunksPerBlock)) + 3;
  6832. +
  6833. + dev->nCheckpointBlocksRequired = nBlocks;
  6834. + }
  6835. +
  6836. + return dev->nCheckpointBlocksRequired;
  6837. +}
  6838. +
  6839. +/*
  6840. + * Check if there's space to allocate...
  6841. + * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
  6842. + */
  6843. +static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
  6844. +{
  6845. + int reservedChunks;
  6846. + int reservedBlocks = dev->nReservedBlocks;
  6847. + int checkpointBlocks;
  6848. +
  6849. + if (dev->isYaffs2) {
  6850. + checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
  6851. + dev->blocksInCheckpoint;
  6852. + if (checkpointBlocks < 0)
  6853. + checkpointBlocks = 0;
  6854. + } else {
  6855. + checkpointBlocks = 0;
  6856. + }
  6857. +
  6858. + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
  6859. +
  6860. + return (dev->nFreeChunks > reservedChunks);
  6861. +}
  6862. +
  6863. +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
  6864. + yaffs_BlockInfo **blockUsedPtr)
  6865. +{
  6866. + int retVal;
  6867. + yaffs_BlockInfo *bi;
  6868. +
  6869. + if (dev->allocationBlock < 0) {
  6870. + /* Get next block to allocate off */
  6871. + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
  6872. + dev->allocationPage = 0;
  6873. + }
  6874. +
  6875. + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
  6876. + /* Not enough space to allocate unless we're allowed to use the reserve. */
  6877. + return -1;
  6878. + }
  6879. +
  6880. + if (dev->nErasedBlocks < dev->nReservedBlocks
  6881. + && dev->allocationPage == 0) {
  6882. + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
  6883. + }
  6884. +
  6885. + /* Next page please.... */
  6886. + if (dev->allocationBlock >= 0) {
  6887. + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
  6888. +
  6889. + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
  6890. + dev->allocationPage;
  6891. + bi->pagesInUse++;
  6892. + yaffs_SetChunkBit(dev, dev->allocationBlock,
  6893. + dev->allocationPage);
  6894. +
  6895. + dev->allocationPage++;
  6896. +
  6897. + dev->nFreeChunks--;
  6898. +
  6899. + /* If the block is full set the state to full */
  6900. + if (dev->allocationPage >= dev->nChunksPerBlock) {
  6901. + bi->blockState = YAFFS_BLOCK_STATE_FULL;
  6902. + dev->allocationBlock = -1;
  6903. + }
  6904. +
  6905. + if (blockUsedPtr)
  6906. + *blockUsedPtr = bi;
  6907. +
  6908. + return retVal;
  6909. + }
  6910. +
  6911. + T(YAFFS_TRACE_ERROR,
  6912. + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
  6913. +
  6914. + return -1;
  6915. +}
  6916. +
  6917. +static int yaffs_GetErasedChunks(yaffs_Device *dev)
  6918. +{
  6919. + int n;
  6920. +
  6921. + n = dev->nErasedBlocks * dev->nChunksPerBlock;
  6922. +
  6923. + if (dev->allocationBlock > 0)
  6924. + n += (dev->nChunksPerBlock - dev->allocationPage);
  6925. +
  6926. + return n;
  6927. +
  6928. +}
  6929. +
  6930. +static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
  6931. + int wholeBlock)
  6932. +{
  6933. + int oldChunk;
  6934. + int newChunk;
  6935. + int markNAND;
  6936. + int retVal = YAFFS_OK;
  6937. + int cleanups = 0;
  6938. + int i;
  6939. + int isCheckpointBlock;
  6940. + int matchingChunk;
  6941. + int maxCopies;
  6942. +
  6943. + int chunksBefore = yaffs_GetErasedChunks(dev);
  6944. + int chunksAfter;
  6945. +
  6946. + yaffs_ExtendedTags tags;
  6947. +
  6948. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
  6949. +
  6950. + yaffs_Object *object;
  6951. +
  6952. + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
  6953. +
  6954. + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
  6955. +
  6956. + T(YAFFS_TRACE_TRACING,
  6957. + (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
  6958. + block,
  6959. + bi->pagesInUse,
  6960. + bi->hasShrinkHeader,
  6961. + wholeBlock));
  6962. +
  6963. + /*yaffs_VerifyFreeChunks(dev); */
  6964. +
  6965. + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
  6966. +
  6967. + /* Take off the number of soft deleted entries because
  6968. + * they're going to get really deleted during GC.
  6969. + */
  6970. + dev->nFreeChunks -= bi->softDeletions;
  6971. +
  6972. + dev->isDoingGC = 1;
  6973. +
  6974. + if (isCheckpointBlock ||
  6975. + !yaffs_StillSomeChunkBits(dev, block)) {
  6976. + T(YAFFS_TRACE_TRACING,
  6977. + (TSTR
  6978. + ("Collecting block %d that has no chunks in use" TENDSTR),
  6979. + block));
  6980. + yaffs_BlockBecameDirty(dev, block);
  6981. + } else {
  6982. +
  6983. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  6984. +
  6985. + yaffs_VerifyBlock(dev, bi, block);
  6986. +
  6987. + maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10;
  6988. + oldChunk = block * dev->nChunksPerBlock + dev->gcChunk;
  6989. +
  6990. + for (/* init already done */;
  6991. + retVal == YAFFS_OK &&
  6992. + dev->gcChunk < dev->nChunksPerBlock &&
  6993. + (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
  6994. + maxCopies > 0;
  6995. + dev->gcChunk++, oldChunk++) {
  6996. + if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
  6997. +
  6998. + /* This page is in use and might need to be copied off */
  6999. +
  7000. + maxCopies--;
  7001. +
  7002. + markNAND = 1;
  7003. +
  7004. + yaffs_InitialiseTags(&tags);
  7005. +
  7006. + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
  7007. + buffer, &tags);
  7008. +
  7009. + object =
  7010. + yaffs_FindObjectByNumber(dev,
  7011. + tags.objectId);
  7012. +
  7013. + T(YAFFS_TRACE_GC_DETAIL,
  7014. + (TSTR
  7015. + ("Collecting chunk in block %d, %d %d %d " TENDSTR),
  7016. + dev->gcChunk, tags.objectId, tags.chunkId,
  7017. + tags.byteCount));
  7018. +
  7019. + if (object && !yaffs_SkipVerification(dev)) {
  7020. + if (tags.chunkId == 0)
  7021. + matchingChunk = object->hdrChunk;
  7022. + else if (object->softDeleted)
  7023. + matchingChunk = oldChunk; /* Defeat the test */
  7024. + else
  7025. + matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
  7026. +
  7027. + if (oldChunk != matchingChunk)
  7028. + T(YAFFS_TRACE_ERROR,
  7029. + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
  7030. + oldChunk, matchingChunk, tags.objectId, tags.chunkId));
  7031. +
  7032. + }
  7033. +
  7034. + if (!object) {
  7035. + T(YAFFS_TRACE_ERROR,
  7036. + (TSTR
  7037. + ("page %d in gc has no object: %d %d %d "
  7038. + TENDSTR), oldChunk,
  7039. + tags.objectId, tags.chunkId, tags.byteCount));
  7040. + }
  7041. +
  7042. + if (object &&
  7043. + object->deleted &&
  7044. + object->softDeleted &&
  7045. + tags.chunkId != 0) {
  7046. + /* Data chunk in a soft deleted file, throw it away
  7047. + * It's a soft deleted data chunk,
  7048. + * No need to copy this, just forget about it and
  7049. + * fix up the object.
  7050. + */
  7051. +
  7052. + object->nDataChunks--;
  7053. +
  7054. + if (object->nDataChunks <= 0) {
  7055. + /* remeber to clean up the object */
  7056. + dev->gcCleanupList[cleanups] =
  7057. + tags.objectId;
  7058. + cleanups++;
  7059. + }
  7060. + markNAND = 0;
  7061. + } else if (0) {
  7062. + /* Todo object && object->deleted && object->nDataChunks == 0 */
  7063. + /* Deleted object header with no data chunks.
  7064. + * Can be discarded and the file deleted.
  7065. + */
  7066. + object->hdrChunk = 0;
  7067. + yaffs_FreeTnode(object->myDev,
  7068. + object->variant.
  7069. + fileVariant.top);
  7070. + object->variant.fileVariant.top = NULL;
  7071. + yaffs_DoGenericObjectDeletion(object);
  7072. +
  7073. + } else if (object) {
  7074. + /* It's either a data chunk in a live file or
  7075. + * an ObjectHeader, so we're interested in it.
  7076. + * NB Need to keep the ObjectHeaders of deleted files
  7077. + * until the whole file has been deleted off
  7078. + */
  7079. + tags.serialNumber++;
  7080. +
  7081. + dev->nGCCopies++;
  7082. +
  7083. + if (tags.chunkId == 0) {
  7084. + /* It is an object Id,
  7085. + * We need to nuke the shrinkheader flags first
  7086. + * We no longer want the shrinkHeader flag since its work is done
  7087. + * and if it is left in place it will mess up scanning.
  7088. + */
  7089. +
  7090. + yaffs_ObjectHeader *oh;
  7091. + oh = (yaffs_ObjectHeader *)buffer;
  7092. + oh->isShrink = 0;
  7093. + tags.extraIsShrinkHeader = 0;
  7094. +
  7095. + yaffs_VerifyObjectHeader(object, oh, &tags, 1);
  7096. + }
  7097. +
  7098. + newChunk =
  7099. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
  7100. +
  7101. + if (newChunk < 0) {
  7102. + retVal = YAFFS_FAIL;
  7103. + } else {
  7104. +
  7105. + /* Ok, now fix up the Tnodes etc. */
  7106. +
  7107. + if (tags.chunkId == 0) {
  7108. + /* It's a header */
  7109. + object->hdrChunk = newChunk;
  7110. + object->serial = tags.serialNumber;
  7111. + } else {
  7112. + /* It's a data chunk */
  7113. + yaffs_PutChunkIntoFile
  7114. + (object,
  7115. + tags.chunkId,
  7116. + newChunk, 0);
  7117. + }
  7118. + }
  7119. + }
  7120. +
  7121. + if (retVal == YAFFS_OK)
  7122. + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
  7123. +
  7124. + }
  7125. + }
  7126. +
  7127. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  7128. +
  7129. +
  7130. + /* Do any required cleanups */
  7131. + for (i = 0; i < cleanups; i++) {
  7132. + /* Time to delete the file too */
  7133. + object =
  7134. + yaffs_FindObjectByNumber(dev,
  7135. + dev->gcCleanupList[i]);
  7136. + if (object) {
  7137. + yaffs_FreeTnode(dev,
  7138. + object->variant.fileVariant.
  7139. + top);
  7140. + object->variant.fileVariant.top = NULL;
  7141. + T(YAFFS_TRACE_GC,
  7142. + (TSTR
  7143. + ("yaffs: About to finally delete object %d"
  7144. + TENDSTR), object->objectId));
  7145. + yaffs_DoGenericObjectDeletion(object);
  7146. + object->myDev->nDeletedFiles--;
  7147. + }
  7148. +
  7149. + }
  7150. +
  7151. + }
  7152. +
  7153. + yaffs_VerifyCollectedBlock(dev, bi, block);
  7154. +
  7155. + chunksAfter = yaffs_GetErasedChunks(dev);
  7156. + if (chunksBefore >= chunksAfter) {
  7157. + T(YAFFS_TRACE_GC,
  7158. + (TSTR
  7159. + ("gc did not increase free chunks before %d after %d"
  7160. + TENDSTR), chunksBefore, chunksAfter));
  7161. + }
  7162. +
  7163. + /* If the gc completed then clear the current gcBlock so that we find another. */
  7164. + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
  7165. + dev->gcBlock = -1;
  7166. + dev->gcChunk = 0;
  7167. + }
  7168. +
  7169. + dev->isDoingGC = 0;
  7170. +
  7171. + return retVal;
  7172. +}
  7173. +
  7174. +/* New garbage collector
  7175. + * If we're very low on erased blocks then we do aggressive garbage collection
  7176. + * otherwise we do "leasurely" garbage collection.
  7177. + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  7178. + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  7179. + *
  7180. + * The idea is to help clear out space in a more spread-out manner.
  7181. + * Dunno if it really does anything useful.
  7182. + */
  7183. +static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
  7184. +{
  7185. + int block;
  7186. + int aggressive;
  7187. + int gcOk = YAFFS_OK;
  7188. + int maxTries = 0;
  7189. +
  7190. + int checkpointBlockAdjust;
  7191. +
  7192. + if (dev->isDoingGC) {
  7193. + /* Bail out so we don't get recursive gc */
  7194. + return YAFFS_OK;
  7195. + }
  7196. +
  7197. + /* This loop should pass the first time.
  7198. + * We'll only see looping here if the erase of the collected block fails.
  7199. + */
  7200. +
  7201. + do {
  7202. + maxTries++;
  7203. +
  7204. + checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
  7205. + if (checkpointBlockAdjust < 0)
  7206. + checkpointBlockAdjust = 0;
  7207. +
  7208. + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
  7209. + /* We need a block soon...*/
  7210. + aggressive = 1;
  7211. + } else {
  7212. + /* We're in no hurry */
  7213. + aggressive = 0;
  7214. + }
  7215. +
  7216. + if (dev->gcBlock <= 0) {
  7217. + dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive);
  7218. + dev->gcChunk = 0;
  7219. + }
  7220. +
  7221. + block = dev->gcBlock;
  7222. +
  7223. + if (block > 0) {
  7224. + dev->garbageCollections++;
  7225. + if (!aggressive)
  7226. + dev->passiveGarbageCollections++;
  7227. +
  7228. + T(YAFFS_TRACE_GC,
  7229. + (TSTR
  7230. + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
  7231. + dev->nErasedBlocks, aggressive));
  7232. +
  7233. + gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive);
  7234. + }
  7235. +
  7236. + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
  7237. + T(YAFFS_TRACE_GC,
  7238. + (TSTR
  7239. + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
  7240. + TENDSTR), dev->nErasedBlocks, maxTries, block));
  7241. + }
  7242. + } while ((dev->nErasedBlocks < dev->nReservedBlocks) &&
  7243. + (block > 0) &&
  7244. + (maxTries < 2));
  7245. +
  7246. + return aggressive ? gcOk : YAFFS_OK;
  7247. +}
  7248. +
  7249. +/*------------------------- TAGS --------------------------------*/
  7250. +
  7251. +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
  7252. + int chunkInObject)
  7253. +{
  7254. + return (tags->chunkId == chunkInObject &&
  7255. + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
  7256. +
  7257. +}
  7258. +
  7259. +
  7260. +/*-------------------- Data file manipulation -----------------*/
  7261. +
  7262. +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
  7263. + yaffs_ExtendedTags *tags)
  7264. +{
  7265. + /*Get the Tnode, then get the level 0 offset chunk offset */
  7266. + yaffs_Tnode *tn;
  7267. + int theChunk = -1;
  7268. + yaffs_ExtendedTags localTags;
  7269. + int retVal = -1;
  7270. +
  7271. + yaffs_Device *dev = in->myDev;
  7272. +
  7273. + if (!tags) {
  7274. + /* Passed a NULL, so use our own tags space */
  7275. + tags = &localTags;
  7276. + }
  7277. +
  7278. + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  7279. +
  7280. + if (tn) {
  7281. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7282. +
  7283. + retVal =
  7284. + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  7285. + chunkInInode);
  7286. + }
  7287. + return retVal;
  7288. +}
  7289. +
  7290. +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
  7291. + yaffs_ExtendedTags *tags)
  7292. +{
  7293. + /* Get the Tnode, then get the level 0 offset chunk offset */
  7294. + yaffs_Tnode *tn;
  7295. + int theChunk = -1;
  7296. + yaffs_ExtendedTags localTags;
  7297. +
  7298. + yaffs_Device *dev = in->myDev;
  7299. + int retVal = -1;
  7300. +
  7301. + if (!tags) {
  7302. + /* Passed a NULL, so use our own tags space */
  7303. + tags = &localTags;
  7304. + }
  7305. +
  7306. + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  7307. +
  7308. + if (tn) {
  7309. +
  7310. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7311. +
  7312. + retVal =
  7313. + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  7314. + chunkInInode);
  7315. +
  7316. + /* Delete the entry in the filestructure (if found) */
  7317. + if (retVal != -1)
  7318. + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
  7319. + }
  7320. +
  7321. + return retVal;
  7322. +}
  7323. +
  7324. +#ifdef YAFFS_PARANOID
  7325. +
  7326. +static int yaffs_CheckFileSanity(yaffs_Object *in)
  7327. +{
  7328. + int chunk;
  7329. + int nChunks;
  7330. + int fSize;
  7331. + int failed = 0;
  7332. + int objId;
  7333. + yaffs_Tnode *tn;
  7334. + yaffs_Tags localTags;
  7335. + yaffs_Tags *tags = &localTags;
  7336. + int theChunk;
  7337. + int chunkDeleted;
  7338. +
  7339. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
  7340. + return YAFFS_FAIL;
  7341. +
  7342. + objId = in->objectId;
  7343. + fSize = in->variant.fileVariant.fileSize;
  7344. + nChunks =
  7345. + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
  7346. +
  7347. + for (chunk = 1; chunk <= nChunks; chunk++) {
  7348. + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
  7349. + chunk);
  7350. +
  7351. + if (tn) {
  7352. +
  7353. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
  7354. +
  7355. + if (yaffs_CheckChunkBits
  7356. + (dev, theChunk / dev->nChunksPerBlock,
  7357. + theChunk % dev->nChunksPerBlock)) {
  7358. +
  7359. + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
  7360. + tags,
  7361. + &chunkDeleted);
  7362. + if (yaffs_TagsMatch
  7363. + (tags, in->objectId, chunk, chunkDeleted)) {
  7364. + /* found it; */
  7365. +
  7366. + }
  7367. + } else {
  7368. +
  7369. + failed = 1;
  7370. + }
  7371. +
  7372. + } else {
  7373. + /* T(("No level 0 found for %d\n", chunk)); */
  7374. + }
  7375. + }
  7376. +
  7377. + return failed ? YAFFS_FAIL : YAFFS_OK;
  7378. +}
  7379. +
  7380. +#endif
  7381. +
  7382. +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
  7383. + int chunkInNAND, int inScan)
  7384. +{
  7385. + /* NB inScan is zero unless scanning.
  7386. + * For forward scanning, inScan is > 0;
  7387. + * for backward scanning inScan is < 0
  7388. + */
  7389. +
  7390. + yaffs_Tnode *tn;
  7391. + yaffs_Device *dev = in->myDev;
  7392. + int existingChunk;
  7393. + yaffs_ExtendedTags existingTags;
  7394. + yaffs_ExtendedTags newTags;
  7395. + unsigned existingSerial, newSerial;
  7396. +
  7397. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
  7398. + /* Just ignore an attempt at putting a chunk into a non-file during scanning
  7399. + * If it is not during Scanning then something went wrong!
  7400. + */
  7401. + if (!inScan) {
  7402. + T(YAFFS_TRACE_ERROR,
  7403. + (TSTR
  7404. + ("yaffs tragedy:attempt to put data chunk into a non-file"
  7405. + TENDSTR)));
  7406. + YBUG();
  7407. + }
  7408. +
  7409. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  7410. + return YAFFS_OK;
  7411. + }
  7412. +
  7413. + tn = yaffs_AddOrFindLevel0Tnode(dev,
  7414. + &in->variant.fileVariant,
  7415. + chunkInInode,
  7416. + NULL);
  7417. + if (!tn)
  7418. + return YAFFS_FAIL;
  7419. +
  7420. + existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7421. +
  7422. + if (inScan != 0) {
  7423. + /* If we're scanning then we need to test for duplicates
  7424. + * NB This does not need to be efficient since it should only ever
  7425. + * happen when the power fails during a write, then only one
  7426. + * chunk should ever be affected.
  7427. + *
  7428. + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  7429. + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  7430. + */
  7431. +
  7432. + if (existingChunk > 0) {
  7433. + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
  7434. + * thus we have to do a FindChunkInFile to get the real chunk id.
  7435. + *
  7436. + * We have a duplicate now we need to decide which one to use:
  7437. + *
  7438. + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  7439. + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  7440. + * YAFFS1: Get both sets of tags and compare serial numbers.
  7441. + */
  7442. +
  7443. + if (inScan > 0) {
  7444. + /* Only do this for forward scanning */
  7445. + yaffs_ReadChunkWithTagsFromNAND(dev,
  7446. + chunkInNAND,
  7447. + NULL, &newTags);
  7448. +
  7449. + /* Do a proper find */
  7450. + existingChunk =
  7451. + yaffs_FindChunkInFile(in, chunkInInode,
  7452. + &existingTags);
  7453. + }
  7454. +
  7455. + if (existingChunk <= 0) {
  7456. + /*Hoosterman - how did this happen? */
  7457. +
  7458. + T(YAFFS_TRACE_ERROR,
  7459. + (TSTR
  7460. + ("yaffs tragedy: existing chunk < 0 in scan"
  7461. + TENDSTR)));
  7462. +
  7463. + }
  7464. +
  7465. + /* NB The deleted flags should be false, otherwise the chunks will
  7466. + * not be loaded during a scan
  7467. + */
  7468. +
  7469. + if (inScan > 0) {
  7470. + newSerial = newTags.serialNumber;
  7471. + existingSerial = existingTags.serialNumber;
  7472. + }
  7473. +
  7474. + if ((inScan > 0) &&
  7475. + (in->myDev->isYaffs2 ||
  7476. + existingChunk <= 0 ||
  7477. + ((existingSerial + 1) & 3) == newSerial)) {
  7478. + /* Forward scanning.
  7479. + * Use new
  7480. + * Delete the old one and drop through to update the tnode
  7481. + */
  7482. + yaffs_DeleteChunk(dev, existingChunk, 1,
  7483. + __LINE__);
  7484. + } else {
  7485. + /* Backward scanning or we want to use the existing one
  7486. + * Use existing.
  7487. + * Delete the new one and return early so that the tnode isn't changed
  7488. + */
  7489. + yaffs_DeleteChunk(dev, chunkInNAND, 1,
  7490. + __LINE__);
  7491. + return YAFFS_OK;
  7492. + }
  7493. + }
  7494. +
  7495. + }
  7496. +
  7497. + if (existingChunk == 0)
  7498. + in->nDataChunks++;
  7499. +
  7500. + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
  7501. +
  7502. + return YAFFS_OK;
  7503. +}
  7504. +
  7505. +static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
  7506. + __u8 *buffer)
  7507. +{
  7508. + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
  7509. +
  7510. + if (chunkInNAND >= 0)
  7511. + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
  7512. + buffer, NULL);
  7513. + else {
  7514. + T(YAFFS_TRACE_NANDACCESS,
  7515. + (TSTR("Chunk %d not found zero instead" TENDSTR),
  7516. + chunkInNAND));
  7517. + /* get sane (zero) data if you read a hole */
  7518. + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
  7519. + return 0;
  7520. + }
  7521. +
  7522. +}
  7523. +
  7524. +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
  7525. +{
  7526. + int block;
  7527. + int page;
  7528. + yaffs_ExtendedTags tags;
  7529. + yaffs_BlockInfo *bi;
  7530. +
  7531. + if (chunkId <= 0)
  7532. + return;
  7533. +
  7534. + dev->nDeletions++;
  7535. + block = chunkId / dev->nChunksPerBlock;
  7536. + page = chunkId % dev->nChunksPerBlock;
  7537. +
  7538. +
  7539. + if (!yaffs_CheckChunkBit(dev, block, page))
  7540. + T(YAFFS_TRACE_VERIFY,
  7541. + (TSTR("Deleting invalid chunk %d"TENDSTR),
  7542. + chunkId));
  7543. +
  7544. + bi = yaffs_GetBlockInfo(dev, block);
  7545. +
  7546. + T(YAFFS_TRACE_DELETION,
  7547. + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
  7548. +
  7549. + if (markNAND &&
  7550. + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
  7551. +
  7552. + yaffs_InitialiseTags(&tags);
  7553. +
  7554. + tags.chunkDeleted = 1;
  7555. +
  7556. + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
  7557. + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
  7558. + } else {
  7559. + dev->nUnmarkedDeletions++;
  7560. + }
  7561. +
  7562. + /* Pull out of the management area.
  7563. + * If the whole block became dirty, this will kick off an erasure.
  7564. + */
  7565. + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
  7566. + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
  7567. + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  7568. + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
  7569. + dev->nFreeChunks++;
  7570. +
  7571. + yaffs_ClearChunkBit(dev, block, page);
  7572. +
  7573. + bi->pagesInUse--;
  7574. +
  7575. + if (bi->pagesInUse == 0 &&
  7576. + !bi->hasShrinkHeader &&
  7577. + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
  7578. + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  7579. + yaffs_BlockBecameDirty(dev, block);
  7580. + }
  7581. +
  7582. + }
  7583. +
  7584. +}
  7585. +
  7586. +static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
  7587. + const __u8 *buffer, int nBytes,
  7588. + int useReserve)
  7589. +{
  7590. + /* Find old chunk Need to do this to get serial number
  7591. + * Write new one and patch into tree.
  7592. + * Invalidate old tags.
  7593. + */
  7594. +
  7595. + int prevChunkId;
  7596. + yaffs_ExtendedTags prevTags;
  7597. +
  7598. + int newChunkId;
  7599. + yaffs_ExtendedTags newTags;
  7600. +
  7601. + yaffs_Device *dev = in->myDev;
  7602. +
  7603. + yaffs_CheckGarbageCollection(dev);
  7604. +
  7605. + /* Get the previous chunk at this location in the file if it exists */
  7606. + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
  7607. +
  7608. + /* Set up new tags */
  7609. + yaffs_InitialiseTags(&newTags);
  7610. +
  7611. + newTags.chunkId = chunkInInode;
  7612. + newTags.objectId = in->objectId;
  7613. + newTags.serialNumber =
  7614. + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
  7615. + newTags.byteCount = nBytes;
  7616. +
  7617. + if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) {
  7618. + T(YAFFS_TRACE_ERROR,
  7619. + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
  7620. + YBUG();
  7621. + }
  7622. +
  7623. + newChunkId =
  7624. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  7625. + useReserve);
  7626. +
  7627. + if (newChunkId >= 0) {
  7628. + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
  7629. +
  7630. + if (prevChunkId >= 0)
  7631. + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
  7632. +
  7633. + yaffs_CheckFileSanity(in);
  7634. + }
  7635. + return newChunkId;
  7636. +
  7637. +}
  7638. +
  7639. +/* UpdateObjectHeader updates the header on NAND for an object.
  7640. + * If name is not NULL, then that new name is used.
  7641. + */
  7642. +int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
  7643. + int isShrink, int shadows)
  7644. +{
  7645. +
  7646. + yaffs_BlockInfo *bi;
  7647. +
  7648. + yaffs_Device *dev = in->myDev;
  7649. +
  7650. + int prevChunkId;
  7651. + int retVal = 0;
  7652. + int result = 0;
  7653. +
  7654. + int newChunkId;
  7655. + yaffs_ExtendedTags newTags;
  7656. + yaffs_ExtendedTags oldTags;
  7657. +
  7658. + __u8 *buffer = NULL;
  7659. + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
  7660. +
  7661. + yaffs_ObjectHeader *oh = NULL;
  7662. +
  7663. + yaffs_strcpy(oldName, _Y("silly old name"));
  7664. +
  7665. +
  7666. + if (!in->fake ||
  7667. + in == dev->rootDir || /* The rootDir should also be saved */
  7668. + force) {
  7669. +
  7670. + yaffs_CheckGarbageCollection(dev);
  7671. + yaffs_CheckObjectDetailsLoaded(in);
  7672. +
  7673. + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
  7674. + oh = (yaffs_ObjectHeader *) buffer;
  7675. +
  7676. + prevChunkId = in->hdrChunk;
  7677. +
  7678. + if (prevChunkId > 0) {
  7679. + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
  7680. + buffer, &oldTags);
  7681. +
  7682. + yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
  7683. +
  7684. + memcpy(oldName, oh->name, sizeof(oh->name));
  7685. + }
  7686. +
  7687. + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
  7688. +
  7689. + oh->type = in->variantType;
  7690. + oh->yst_mode = in->yst_mode;
  7691. + oh->shadowsObject = oh->inbandShadowsObject = shadows;
  7692. +
  7693. +#ifdef CONFIG_YAFFS_WINCE
  7694. + oh->win_atime[0] = in->win_atime[0];
  7695. + oh->win_ctime[0] = in->win_ctime[0];
  7696. + oh->win_mtime[0] = in->win_mtime[0];
  7697. + oh->win_atime[1] = in->win_atime[1];
  7698. + oh->win_ctime[1] = in->win_ctime[1];
  7699. + oh->win_mtime[1] = in->win_mtime[1];
  7700. +#else
  7701. + oh->yst_uid = in->yst_uid;
  7702. + oh->yst_gid = in->yst_gid;
  7703. + oh->yst_atime = in->yst_atime;
  7704. + oh->yst_mtime = in->yst_mtime;
  7705. + oh->yst_ctime = in->yst_ctime;
  7706. + oh->yst_rdev = in->yst_rdev;
  7707. +#endif
  7708. + if (in->parent)
  7709. + oh->parentObjectId = in->parent->objectId;
  7710. + else
  7711. + oh->parentObjectId = 0;
  7712. +
  7713. + if (name && *name) {
  7714. + memset(oh->name, 0, sizeof(oh->name));
  7715. + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
  7716. + } else if (prevChunkId >= 0)
  7717. + memcpy(oh->name, oldName, sizeof(oh->name));
  7718. + else
  7719. + memset(oh->name, 0, sizeof(oh->name));
  7720. +
  7721. + oh->isShrink = isShrink;
  7722. +
  7723. + switch (in->variantType) {
  7724. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  7725. + /* Should not happen */
  7726. + break;
  7727. + case YAFFS_OBJECT_TYPE_FILE:
  7728. + oh->fileSize =
  7729. + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
  7730. + || oh->parentObjectId ==
  7731. + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
  7732. + fileVariant.fileSize;
  7733. + break;
  7734. + case YAFFS_OBJECT_TYPE_HARDLINK:
  7735. + oh->equivalentObjectId =
  7736. + in->variant.hardLinkVariant.equivalentObjectId;
  7737. + break;
  7738. + case YAFFS_OBJECT_TYPE_SPECIAL:
  7739. + /* Do nothing */
  7740. + break;
  7741. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  7742. + /* Do nothing */
  7743. + break;
  7744. + case YAFFS_OBJECT_TYPE_SYMLINK:
  7745. + yaffs_strncpy(oh->alias,
  7746. + in->variant.symLinkVariant.alias,
  7747. + YAFFS_MAX_ALIAS_LENGTH);
  7748. + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  7749. + break;
  7750. + }
  7751. +
  7752. + /* Tags */
  7753. + yaffs_InitialiseTags(&newTags);
  7754. + in->serial++;
  7755. + newTags.chunkId = 0;
  7756. + newTags.objectId = in->objectId;
  7757. + newTags.serialNumber = in->serial;
  7758. +
  7759. + /* Add extra info for file header */
  7760. +
  7761. + newTags.extraHeaderInfoAvailable = 1;
  7762. + newTags.extraParentObjectId = oh->parentObjectId;
  7763. + newTags.extraFileLength = oh->fileSize;
  7764. + newTags.extraIsShrinkHeader = oh->isShrink;
  7765. + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
  7766. + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
  7767. + newTags.extraObjectType = in->variantType;
  7768. +
  7769. + yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
  7770. +
  7771. + /* Create new chunk in NAND */
  7772. + newChunkId =
  7773. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  7774. + (prevChunkId >= 0) ? 1 : 0);
  7775. +
  7776. + if (newChunkId >= 0) {
  7777. +
  7778. + in->hdrChunk = newChunkId;
  7779. +
  7780. + if (prevChunkId >= 0) {
  7781. + yaffs_DeleteChunk(dev, prevChunkId, 1,
  7782. + __LINE__);
  7783. + }
  7784. +
  7785. + if (!yaffs_ObjectHasCachedWriteData(in))
  7786. + in->dirty = 0;
  7787. +
  7788. + /* If this was a shrink, then mark the block that the chunk lives on */
  7789. + if (isShrink) {
  7790. + bi = yaffs_GetBlockInfo(in->myDev,
  7791. + newChunkId / in->myDev->nChunksPerBlock);
  7792. + bi->hasShrinkHeader = 1;
  7793. + }
  7794. +
  7795. + }
  7796. +
  7797. + retVal = newChunkId;
  7798. +
  7799. + }
  7800. +
  7801. + if (buffer)
  7802. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  7803. +
  7804. + return retVal;
  7805. +}
  7806. +
  7807. +/*------------------------ Short Operations Cache ----------------------------------------
  7808. + * In many situations where there is no high level buffering (eg WinCE) a lot of
  7809. + * reads might be short sequential reads, and a lot of writes may be short
  7810. + * sequential writes. eg. scanning/writing a jpeg file.
  7811. + * In these cases, a short read/write cache can provide a huge perfomance benefit
  7812. + * with dumb-as-a-rock code.
  7813. + * In Linux, the page cache provides read buffering aand the short op cache provides write
  7814. + * buffering.
  7815. + *
  7816. + * There are a limited number (~10) of cache chunks per device so that we don't
  7817. + * need a very intelligent search.
  7818. + */
  7819. +
  7820. +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
  7821. +{
  7822. + yaffs_Device *dev = obj->myDev;
  7823. + int i;
  7824. + yaffs_ChunkCache *cache;
  7825. + int nCaches = obj->myDev->nShortOpCaches;
  7826. +
  7827. + for (i = 0; i < nCaches; i++) {
  7828. + cache = &dev->srCache[i];
  7829. + if (cache->object == obj &&
  7830. + cache->dirty)
  7831. + return 1;
  7832. + }
  7833. +
  7834. + return 0;
  7835. +}
  7836. +
  7837. +
  7838. +static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
  7839. +{
  7840. + yaffs_Device *dev = obj->myDev;
  7841. + int lowest = -99; /* Stop compiler whining. */
  7842. + int i;
  7843. + yaffs_ChunkCache *cache;
  7844. + int chunkWritten = 0;
  7845. + int nCaches = obj->myDev->nShortOpCaches;
  7846. +
  7847. + if (nCaches > 0) {
  7848. + do {
  7849. + cache = NULL;
  7850. +
  7851. + /* Find the dirty cache for this object with the lowest chunk id. */
  7852. + for (i = 0; i < nCaches; i++) {
  7853. + if (dev->srCache[i].object == obj &&
  7854. + dev->srCache[i].dirty) {
  7855. + if (!cache
  7856. + || dev->srCache[i].chunkId <
  7857. + lowest) {
  7858. + cache = &dev->srCache[i];
  7859. + lowest = cache->chunkId;
  7860. + }
  7861. + }
  7862. + }
  7863. +
  7864. + if (cache && !cache->locked) {
  7865. + /* Write it out and free it up */
  7866. +
  7867. + chunkWritten =
  7868. + yaffs_WriteChunkDataToObject(cache->object,
  7869. + cache->chunkId,
  7870. + cache->data,
  7871. + cache->nBytes,
  7872. + 1);
  7873. + cache->dirty = 0;
  7874. + cache->object = NULL;
  7875. + }
  7876. +
  7877. + } while (cache && chunkWritten > 0);
  7878. +
  7879. + if (cache) {
  7880. + /* Hoosterman, disk full while writing cache out. */
  7881. + T(YAFFS_TRACE_ERROR,
  7882. + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
  7883. +
  7884. + }
  7885. + }
  7886. +
  7887. +}
  7888. +
  7889. +/*yaffs_FlushEntireDeviceCache(dev)
  7890. + *
  7891. + *
  7892. + */
  7893. +
  7894. +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
  7895. +{
  7896. + yaffs_Object *obj;
  7897. + int nCaches = dev->nShortOpCaches;
  7898. + int i;
  7899. +
  7900. + /* Find a dirty object in the cache and flush it...
  7901. + * until there are no further dirty objects.
  7902. + */
  7903. + do {
  7904. + obj = NULL;
  7905. + for (i = 0; i < nCaches && !obj; i++) {
  7906. + if (dev->srCache[i].object &&
  7907. + dev->srCache[i].dirty)
  7908. + obj = dev->srCache[i].object;
  7909. +
  7910. + }
  7911. + if (obj)
  7912. + yaffs_FlushFilesChunkCache(obj);
  7913. +
  7914. + } while (obj);
  7915. +
  7916. +}
  7917. +
  7918. +
  7919. +/* Grab us a cache chunk for use.
  7920. + * First look for an empty one.
  7921. + * Then look for the least recently used non-dirty one.
  7922. + * Then look for the least recently used dirty one...., flush and look again.
  7923. + */
  7924. +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
  7925. +{
  7926. + int i;
  7927. +
  7928. + if (dev->nShortOpCaches > 0) {
  7929. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7930. + if (!dev->srCache[i].object)
  7931. + return &dev->srCache[i];
  7932. + }
  7933. + }
  7934. +
  7935. + return NULL;
  7936. +}
  7937. +
  7938. +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
  7939. +{
  7940. + yaffs_ChunkCache *cache;
  7941. + yaffs_Object *theObj;
  7942. + int usage;
  7943. + int i;
  7944. + int pushout;
  7945. +
  7946. + if (dev->nShortOpCaches > 0) {
  7947. + /* Try find a non-dirty one... */
  7948. +
  7949. + cache = yaffs_GrabChunkCacheWorker(dev);
  7950. +
  7951. + if (!cache) {
  7952. + /* They were all dirty, find the last recently used object and flush
  7953. + * its cache, then find again.
  7954. + * NB what's here is not very accurate, we actually flush the object
  7955. + * the last recently used page.
  7956. + */
  7957. +
  7958. + /* With locking we can't assume we can use entry zero */
  7959. +
  7960. + theObj = NULL;
  7961. + usage = -1;
  7962. + cache = NULL;
  7963. + pushout = -1;
  7964. +
  7965. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7966. + if (dev->srCache[i].object &&
  7967. + !dev->srCache[i].locked &&
  7968. + (dev->srCache[i].lastUse < usage || !cache)) {
  7969. + usage = dev->srCache[i].lastUse;
  7970. + theObj = dev->srCache[i].object;
  7971. + cache = &dev->srCache[i];
  7972. + pushout = i;
  7973. + }
  7974. + }
  7975. +
  7976. + if (!cache || cache->dirty) {
  7977. + /* Flush and try again */
  7978. + yaffs_FlushFilesChunkCache(theObj);
  7979. + cache = yaffs_GrabChunkCacheWorker(dev);
  7980. + }
  7981. +
  7982. + }
  7983. + return cache;
  7984. + } else
  7985. + return NULL;
  7986. +
  7987. +}
  7988. +
  7989. +/* Find a cached chunk */
  7990. +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
  7991. + int chunkId)
  7992. +{
  7993. + yaffs_Device *dev = obj->myDev;
  7994. + int i;
  7995. + if (dev->nShortOpCaches > 0) {
  7996. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7997. + if (dev->srCache[i].object == obj &&
  7998. + dev->srCache[i].chunkId == chunkId) {
  7999. + dev->cacheHits++;
  8000. +
  8001. + return &dev->srCache[i];
  8002. + }
  8003. + }
  8004. + }
  8005. + return NULL;
  8006. +}
  8007. +
  8008. +/* Mark the chunk for the least recently used algorithym */
  8009. +static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
  8010. + int isAWrite)
  8011. +{
  8012. +
  8013. + if (dev->nShortOpCaches > 0) {
  8014. + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
  8015. + /* Reset the cache usages */
  8016. + int i;
  8017. + for (i = 1; i < dev->nShortOpCaches; i++)
  8018. + dev->srCache[i].lastUse = 0;
  8019. +
  8020. + dev->srLastUse = 0;
  8021. + }
  8022. +
  8023. + dev->srLastUse++;
  8024. +
  8025. + cache->lastUse = dev->srLastUse;
  8026. +
  8027. + if (isAWrite)
  8028. + cache->dirty = 1;
  8029. + }
  8030. +}
  8031. +
  8032. +/* Invalidate a single cache page.
  8033. + * Do this when a whole page gets written,
  8034. + * ie the short cache for this page is no longer valid.
  8035. + */
  8036. +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
  8037. +{
  8038. + if (object->myDev->nShortOpCaches > 0) {
  8039. + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
  8040. +
  8041. + if (cache)
  8042. + cache->object = NULL;
  8043. + }
  8044. +}
  8045. +
  8046. +/* Invalidate all the cache pages associated with this object
  8047. + * Do this whenever ther file is deleted or resized.
  8048. + */
  8049. +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
  8050. +{
  8051. + int i;
  8052. + yaffs_Device *dev = in->myDev;
  8053. +
  8054. + if (dev->nShortOpCaches > 0) {
  8055. + /* Invalidate it. */
  8056. + for (i = 0; i < dev->nShortOpCaches; i++) {
  8057. + if (dev->srCache[i].object == in)
  8058. + dev->srCache[i].object = NULL;
  8059. + }
  8060. + }
  8061. +}
  8062. +
  8063. +/*--------------------- Checkpointing --------------------*/
  8064. +
  8065. +
  8066. +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
  8067. +{
  8068. + yaffs_CheckpointValidity cp;
  8069. +
  8070. + memset(&cp, 0, sizeof(cp));
  8071. +
  8072. + cp.structType = sizeof(cp);
  8073. + cp.magic = YAFFS_MAGIC;
  8074. + cp.version = YAFFS_CHECKPOINT_VERSION;
  8075. + cp.head = (head) ? 1 : 0;
  8076. +
  8077. + return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
  8078. + 1 : 0;
  8079. +}
  8080. +
  8081. +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
  8082. +{
  8083. + yaffs_CheckpointValidity cp;
  8084. + int ok;
  8085. +
  8086. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8087. +
  8088. + if (ok)
  8089. + ok = (cp.structType == sizeof(cp)) &&
  8090. + (cp.magic == YAFFS_MAGIC) &&
  8091. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  8092. + (cp.head == ((head) ? 1 : 0));
  8093. + return ok ? 1 : 0;
  8094. +}
  8095. +
  8096. +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
  8097. + yaffs_Device *dev)
  8098. +{
  8099. + cp->nErasedBlocks = dev->nErasedBlocks;
  8100. + cp->allocationBlock = dev->allocationBlock;
  8101. + cp->allocationPage = dev->allocationPage;
  8102. + cp->nFreeChunks = dev->nFreeChunks;
  8103. +
  8104. + cp->nDeletedFiles = dev->nDeletedFiles;
  8105. + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
  8106. + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
  8107. + cp->sequenceNumber = dev->sequenceNumber;
  8108. + cp->oldestDirtySequence = dev->oldestDirtySequence;
  8109. +
  8110. +}
  8111. +
  8112. +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
  8113. + yaffs_CheckpointDevice *cp)
  8114. +{
  8115. + dev->nErasedBlocks = cp->nErasedBlocks;
  8116. + dev->allocationBlock = cp->allocationBlock;
  8117. + dev->allocationPage = cp->allocationPage;
  8118. + dev->nFreeChunks = cp->nFreeChunks;
  8119. +
  8120. + dev->nDeletedFiles = cp->nDeletedFiles;
  8121. + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
  8122. + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
  8123. + dev->sequenceNumber = cp->sequenceNumber;
  8124. + dev->oldestDirtySequence = cp->oldestDirtySequence;
  8125. +}
  8126. +
  8127. +
  8128. +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
  8129. +{
  8130. + yaffs_CheckpointDevice cp;
  8131. + __u32 nBytes;
  8132. + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  8133. +
  8134. + int ok;
  8135. +
  8136. + /* Write device runtime values*/
  8137. + yaffs_DeviceToCheckpointDevice(&cp, dev);
  8138. + cp.structType = sizeof(cp);
  8139. +
  8140. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8141. +
  8142. + /* Write block info */
  8143. + if (ok) {
  8144. + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  8145. + ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
  8146. + }
  8147. +
  8148. + /* Write chunk bits */
  8149. + if (ok) {
  8150. + nBytes = nBlocks * dev->chunkBitmapStride;
  8151. + ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
  8152. + }
  8153. + return ok ? 1 : 0;
  8154. +
  8155. +}
  8156. +
  8157. +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
  8158. +{
  8159. + yaffs_CheckpointDevice cp;
  8160. + __u32 nBytes;
  8161. + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  8162. +
  8163. + int ok;
  8164. +
  8165. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8166. + if (!ok)
  8167. + return 0;
  8168. +
  8169. + if (cp.structType != sizeof(cp))
  8170. + return 0;
  8171. +
  8172. +
  8173. + yaffs_CheckpointDeviceToDevice(dev, &cp);
  8174. +
  8175. + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  8176. +
  8177. + ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
  8178. +
  8179. + if (!ok)
  8180. + return 0;
  8181. + nBytes = nBlocks * dev->chunkBitmapStride;
  8182. +
  8183. + ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
  8184. +
  8185. + return ok ? 1 : 0;
  8186. +}
  8187. +
  8188. +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
  8189. + yaffs_Object *obj)
  8190. +{
  8191. +
  8192. + cp->objectId = obj->objectId;
  8193. + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
  8194. + cp->hdrChunk = obj->hdrChunk;
  8195. + cp->variantType = obj->variantType;
  8196. + cp->deleted = obj->deleted;
  8197. + cp->softDeleted = obj->softDeleted;
  8198. + cp->unlinked = obj->unlinked;
  8199. + cp->fake = obj->fake;
  8200. + cp->renameAllowed = obj->renameAllowed;
  8201. + cp->unlinkAllowed = obj->unlinkAllowed;
  8202. + cp->serial = obj->serial;
  8203. + cp->nDataChunks = obj->nDataChunks;
  8204. +
  8205. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8206. + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
  8207. + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  8208. + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
  8209. +}
  8210. +
  8211. +static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
  8212. +{
  8213. +
  8214. + yaffs_Object *parent;
  8215. +
  8216. + if (obj->variantType != cp->variantType) {
  8217. + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
  8218. + TCONT("chunk %d does not match existing object type %d")
  8219. + TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
  8220. + obj->variantType));
  8221. + return 0;
  8222. + }
  8223. +
  8224. + obj->objectId = cp->objectId;
  8225. +
  8226. + if (cp->parentId)
  8227. + parent = yaffs_FindOrCreateObjectByNumber(
  8228. + obj->myDev,
  8229. + cp->parentId,
  8230. + YAFFS_OBJECT_TYPE_DIRECTORY);
  8231. + else
  8232. + parent = NULL;
  8233. +
  8234. + if (parent) {
  8235. + if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  8236. + T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
  8237. + TCONT(" chunk %d Parent type, %d, not directory")
  8238. + TENDSTR),
  8239. + cp->objectId, cp->parentId, cp->variantType,
  8240. + cp->hdrChunk, parent->variantType));
  8241. + return 0;
  8242. + }
  8243. + yaffs_AddObjectToDirectory(parent, obj);
  8244. + }
  8245. +
  8246. + obj->hdrChunk = cp->hdrChunk;
  8247. + obj->variantType = cp->variantType;
  8248. + obj->deleted = cp->deleted;
  8249. + obj->softDeleted = cp->softDeleted;
  8250. + obj->unlinked = cp->unlinked;
  8251. + obj->fake = cp->fake;
  8252. + obj->renameAllowed = cp->renameAllowed;
  8253. + obj->unlinkAllowed = cp->unlinkAllowed;
  8254. + obj->serial = cp->serial;
  8255. + obj->nDataChunks = cp->nDataChunks;
  8256. +
  8257. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8258. + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
  8259. + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  8260. + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
  8261. +
  8262. + if (obj->hdrChunk > 0)
  8263. + obj->lazyLoaded = 1;
  8264. + return 1;
  8265. +}
  8266. +
  8267. +
  8268. +
  8269. +static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
  8270. + __u32 level, int chunkOffset)
  8271. +{
  8272. + int i;
  8273. + yaffs_Device *dev = in->myDev;
  8274. + int ok = 1;
  8275. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  8276. +
  8277. + if (tnodeSize < sizeof(yaffs_Tnode))
  8278. + tnodeSize = sizeof(yaffs_Tnode);
  8279. +
  8280. +
  8281. + if (tn) {
  8282. + if (level > 0) {
  8283. +
  8284. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  8285. + if (tn->internal[i]) {
  8286. + ok = yaffs_CheckpointTnodeWorker(in,
  8287. + tn->internal[i],
  8288. + level - 1,
  8289. + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  8290. + }
  8291. + }
  8292. + } else if (level == 0) {
  8293. + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
  8294. + ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
  8295. + if (ok)
  8296. + ok = (yaffs_CheckpointWrite(dev, tn, tnodeSize) == tnodeSize);
  8297. + }
  8298. + }
  8299. +
  8300. + return ok;
  8301. +
  8302. +}
  8303. +
  8304. +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
  8305. +{
  8306. + __u32 endMarker = ~0;
  8307. + int ok = 1;
  8308. +
  8309. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  8310. + ok = yaffs_CheckpointTnodeWorker(obj,
  8311. + obj->variant.fileVariant.top,
  8312. + obj->variant.fileVariant.topLevel,
  8313. + 0);
  8314. + if (ok)
  8315. + ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
  8316. + sizeof(endMarker));
  8317. + }
  8318. +
  8319. + return ok ? 1 : 0;
  8320. +}
  8321. +
  8322. +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
  8323. +{
  8324. + __u32 baseChunk;
  8325. + int ok = 1;
  8326. + yaffs_Device *dev = obj->myDev;
  8327. + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
  8328. + yaffs_Tnode *tn;
  8329. + int nread = 0;
  8330. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  8331. +
  8332. + if (tnodeSize < sizeof(yaffs_Tnode))
  8333. + tnodeSize = sizeof(yaffs_Tnode);
  8334. +
  8335. + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
  8336. +
  8337. + while (ok && (~baseChunk)) {
  8338. + nread++;
  8339. + /* Read level 0 tnode */
  8340. +
  8341. +
  8342. + tn = yaffs_GetTnodeRaw(dev);
  8343. + if (tn)
  8344. + ok = (yaffs_CheckpointRead(dev, tn, tnodeSize) == tnodeSize);
  8345. + else
  8346. + ok = 0;
  8347. +
  8348. + if (tn && ok)
  8349. + ok = yaffs_AddOrFindLevel0Tnode(dev,
  8350. + fileStructPtr,
  8351. + baseChunk,
  8352. + tn) ? 1 : 0;
  8353. +
  8354. + if (ok)
  8355. + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
  8356. +
  8357. + }
  8358. +
  8359. + T(YAFFS_TRACE_CHECKPOINT, (
  8360. + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
  8361. + nread, baseChunk, ok));
  8362. +
  8363. + return ok ? 1 : 0;
  8364. +}
  8365. +
  8366. +
  8367. +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
  8368. +{
  8369. + yaffs_Object *obj;
  8370. + yaffs_CheckpointObject cp;
  8371. + int i;
  8372. + int ok = 1;
  8373. + struct ylist_head *lh;
  8374. +
  8375. +
  8376. + /* Iterate through the objects in each hash entry,
  8377. + * dumping them to the checkpointing stream.
  8378. + */
  8379. +
  8380. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  8381. + ylist_for_each(lh, &dev->objectBucket[i].list) {
  8382. + if (lh) {
  8383. + obj = ylist_entry(lh, yaffs_Object, hashLink);
  8384. + if (!obj->deferedFree) {
  8385. + yaffs_ObjectToCheckpointObject(&cp, obj);
  8386. + cp.structType = sizeof(cp);
  8387. +
  8388. + T(YAFFS_TRACE_CHECKPOINT, (
  8389. + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
  8390. + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, (unsigned) obj));
  8391. +
  8392. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8393. +
  8394. + if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8395. + ok = yaffs_WriteCheckpointTnodes(obj);
  8396. + }
  8397. + }
  8398. + }
  8399. + }
  8400. +
  8401. + /* Dump end of list */
  8402. + memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
  8403. + cp.structType = sizeof(cp);
  8404. +
  8405. + if (ok)
  8406. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8407. +
  8408. + return ok ? 1 : 0;
  8409. +}
  8410. +
  8411. +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
  8412. +{
  8413. + yaffs_Object *obj;
  8414. + yaffs_CheckpointObject cp;
  8415. + int ok = 1;
  8416. + int done = 0;
  8417. + yaffs_Object *hardList = NULL;
  8418. +
  8419. + while (ok && !done) {
  8420. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8421. + if (cp.structType != sizeof(cp)) {
  8422. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
  8423. + cp.structType, sizeof(cp), ok));
  8424. + ok = 0;
  8425. + }
  8426. +
  8427. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
  8428. + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
  8429. +
  8430. + if (ok && cp.objectId == ~0)
  8431. + done = 1;
  8432. + else if (ok) {
  8433. + obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
  8434. + if (obj) {
  8435. + ok = yaffs_CheckpointObjectToObject(obj, &cp);
  8436. + if (!ok)
  8437. + break;
  8438. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  8439. + ok = yaffs_ReadCheckpointTnodes(obj);
  8440. + } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  8441. + obj->hardLinks.next =
  8442. + (struct ylist_head *) hardList;
  8443. + hardList = obj;
  8444. + }
  8445. + } else
  8446. + ok = 0;
  8447. + }
  8448. + }
  8449. +
  8450. + if (ok)
  8451. + yaffs_HardlinkFixup(dev, hardList);
  8452. +
  8453. + return ok ? 1 : 0;
  8454. +}
  8455. +
  8456. +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
  8457. +{
  8458. + __u32 checkpointSum;
  8459. + int ok;
  8460. +
  8461. + yaffs_GetCheckpointSum(dev, &checkpointSum);
  8462. +
  8463. + ok = (yaffs_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
  8464. +
  8465. + if (!ok)
  8466. + return 0;
  8467. +
  8468. + return 1;
  8469. +}
  8470. +
  8471. +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
  8472. +{
  8473. + __u32 checkpointSum0;
  8474. + __u32 checkpointSum1;
  8475. + int ok;
  8476. +
  8477. + yaffs_GetCheckpointSum(dev, &checkpointSum0);
  8478. +
  8479. + ok = (yaffs_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
  8480. +
  8481. + if (!ok)
  8482. + return 0;
  8483. +
  8484. + if (checkpointSum0 != checkpointSum1)
  8485. + return 0;
  8486. +
  8487. + return 1;
  8488. +}
  8489. +
  8490. +
  8491. +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
  8492. +{
  8493. + int ok = 1;
  8494. +
  8495. + if (dev->skipCheckpointWrite || !dev->isYaffs2) {
  8496. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
  8497. + ok = 0;
  8498. + }
  8499. +
  8500. + if (ok)
  8501. + ok = yaffs_CheckpointOpen(dev, 1);
  8502. +
  8503. + if (ok) {
  8504. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
  8505. + ok = yaffs_WriteCheckpointValidityMarker(dev, 1);
  8506. + }
  8507. + if (ok) {
  8508. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
  8509. + ok = yaffs_WriteCheckpointDevice(dev);
  8510. + }
  8511. + if (ok) {
  8512. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
  8513. + ok = yaffs_WriteCheckpointObjects(dev);
  8514. + }
  8515. + if (ok) {
  8516. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
  8517. + ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
  8518. + }
  8519. +
  8520. + if (ok)
  8521. + ok = yaffs_WriteCheckpointSum(dev);
  8522. +
  8523. + if (!yaffs_CheckpointClose(dev))
  8524. + ok = 0;
  8525. +
  8526. + if (ok)
  8527. + dev->isCheckpointed = 1;
  8528. + else
  8529. + dev->isCheckpointed = 0;
  8530. +
  8531. + return dev->isCheckpointed;
  8532. +}
  8533. +
  8534. +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
  8535. +{
  8536. + int ok = 1;
  8537. +
  8538. + if (dev->skipCheckpointRead || !dev->isYaffs2) {
  8539. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
  8540. + ok = 0;
  8541. + }
  8542. +
  8543. + if (ok)
  8544. + ok = yaffs_CheckpointOpen(dev, 0); /* open for read */
  8545. +
  8546. + if (ok) {
  8547. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
  8548. + ok = yaffs_ReadCheckpointValidityMarker(dev, 1);
  8549. + }
  8550. + if (ok) {
  8551. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
  8552. + ok = yaffs_ReadCheckpointDevice(dev);
  8553. + }
  8554. + if (ok) {
  8555. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
  8556. + ok = yaffs_ReadCheckpointObjects(dev);
  8557. + }
  8558. + if (ok) {
  8559. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
  8560. + ok = yaffs_ReadCheckpointValidityMarker(dev, 0);
  8561. + }
  8562. +
  8563. + if (ok) {
  8564. + ok = yaffs_ReadCheckpointSum(dev);
  8565. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
  8566. + }
  8567. +
  8568. + if (!yaffs_CheckpointClose(dev))
  8569. + ok = 0;
  8570. +
  8571. + if (ok)
  8572. + dev->isCheckpointed = 1;
  8573. + else
  8574. + dev->isCheckpointed = 0;
  8575. +
  8576. + return ok ? 1 : 0;
  8577. +
  8578. +}
  8579. +
  8580. +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
  8581. +{
  8582. + if (dev->isCheckpointed ||
  8583. + dev->blocksInCheckpoint > 0) {
  8584. + dev->isCheckpointed = 0;
  8585. + yaffs_CheckpointInvalidateStream(dev);
  8586. + if (dev->superBlock && dev->markSuperBlockDirty)
  8587. + dev->markSuperBlockDirty(dev->superBlock);
  8588. + }
  8589. +}
  8590. +
  8591. +
  8592. +int yaffs_CheckpointSave(yaffs_Device *dev)
  8593. +{
  8594. +
  8595. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8596. +
  8597. + yaffs_VerifyObjects(dev);
  8598. + yaffs_VerifyBlocks(dev);
  8599. + yaffs_VerifyFreeChunks(dev);
  8600. +
  8601. + if (!dev->isCheckpointed) {
  8602. + yaffs_InvalidateCheckpoint(dev);
  8603. + yaffs_WriteCheckpointData(dev);
  8604. + }
  8605. +
  8606. + T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8607. +
  8608. + return dev->isCheckpointed;
  8609. +}
  8610. +
  8611. +int yaffs_CheckpointRestore(yaffs_Device *dev)
  8612. +{
  8613. + int retval;
  8614. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8615. +
  8616. + retval = yaffs_ReadCheckpointData(dev);
  8617. +
  8618. + if (dev->isCheckpointed) {
  8619. + yaffs_VerifyObjects(dev);
  8620. + yaffs_VerifyBlocks(dev);
  8621. + yaffs_VerifyFreeChunks(dev);
  8622. + }
  8623. +
  8624. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8625. +
  8626. + return retval;
  8627. +}
  8628. +
  8629. +/*--------------------- File read/write ------------------------
  8630. + * Read and write have very similar structures.
  8631. + * In general the read/write has three parts to it
  8632. + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  8633. + * Some complete chunks
  8634. + * An incomplete chunk to end off with
  8635. + *
  8636. + * Curve-balls: the first chunk might also be the last chunk.
  8637. + */
  8638. +
  8639. +int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
  8640. + int nBytes)
  8641. +{
  8642. +
  8643. + int chunk;
  8644. + __u32 start;
  8645. + int nToCopy;
  8646. + int n = nBytes;
  8647. + int nDone = 0;
  8648. + yaffs_ChunkCache *cache;
  8649. +
  8650. + yaffs_Device *dev;
  8651. +
  8652. + dev = in->myDev;
  8653. +
  8654. + while (n > 0) {
  8655. + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
  8656. + /* start = offset % dev->nDataBytesPerChunk; */
  8657. + yaffs_AddrToChunk(dev, offset, &chunk, &start);
  8658. + chunk++;
  8659. +
  8660. + /* OK now check for the curveball where the start and end are in
  8661. + * the same chunk.
  8662. + */
  8663. + if ((start + n) < dev->nDataBytesPerChunk)
  8664. + nToCopy = n;
  8665. + else
  8666. + nToCopy = dev->nDataBytesPerChunk - start;
  8667. +
  8668. + cache = yaffs_FindChunkCache(in, chunk);
  8669. +
  8670. + /* If the chunk is already in the cache or it is less than a whole chunk
  8671. + * or we're using inband tags then use the cache (if there is caching)
  8672. + * else bypass the cache.
  8673. + */
  8674. + if (cache || nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
  8675. + if (dev->nShortOpCaches > 0) {
  8676. +
  8677. + /* If we can't find the data in the cache, then load it up. */
  8678. +
  8679. + if (!cache) {
  8680. + cache = yaffs_GrabChunkCache(in->myDev);
  8681. + cache->object = in;
  8682. + cache->chunkId = chunk;
  8683. + cache->dirty = 0;
  8684. + cache->locked = 0;
  8685. + yaffs_ReadChunkDataFromObject(in, chunk,
  8686. + cache->
  8687. + data);
  8688. + cache->nBytes = 0;
  8689. + }
  8690. +
  8691. + yaffs_UseChunkCache(dev, cache, 0);
  8692. +
  8693. + cache->locked = 1;
  8694. +
  8695. +
  8696. + memcpy(buffer, &cache->data[start], nToCopy);
  8697. +
  8698. + cache->locked = 0;
  8699. + } else {
  8700. + /* Read into the local buffer then copy..*/
  8701. +
  8702. + __u8 *localBuffer =
  8703. + yaffs_GetTempBuffer(dev, __LINE__);
  8704. + yaffs_ReadChunkDataFromObject(in, chunk,
  8705. + localBuffer);
  8706. +
  8707. + memcpy(buffer, &localBuffer[start], nToCopy);
  8708. +
  8709. +
  8710. + yaffs_ReleaseTempBuffer(dev, localBuffer,
  8711. + __LINE__);
  8712. + }
  8713. +
  8714. + } else {
  8715. +
  8716. + /* A full chunk. Read directly into the supplied buffer. */
  8717. + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
  8718. +
  8719. + }
  8720. +
  8721. + n -= nToCopy;
  8722. + offset += nToCopy;
  8723. + buffer += nToCopy;
  8724. + nDone += nToCopy;
  8725. +
  8726. + }
  8727. +
  8728. + return nDone;
  8729. +}
  8730. +
  8731. +int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
  8732. + int nBytes, int writeThrough)
  8733. +{
  8734. +
  8735. + int chunk;
  8736. + __u32 start;
  8737. + int nToCopy;
  8738. + int n = nBytes;
  8739. + int nDone = 0;
  8740. + int nToWriteBack;
  8741. + int startOfWrite = offset;
  8742. + int chunkWritten = 0;
  8743. + __u32 nBytesRead;
  8744. + __u32 chunkStart;
  8745. +
  8746. + yaffs_Device *dev;
  8747. +
  8748. + dev = in->myDev;
  8749. +
  8750. + while (n > 0 && chunkWritten >= 0) {
  8751. + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
  8752. + /* start = offset % dev->nDataBytesPerChunk; */
  8753. + yaffs_AddrToChunk(dev, offset, &chunk, &start);
  8754. +
  8755. + if (chunk * dev->nDataBytesPerChunk + start != offset ||
  8756. + start >= dev->nDataBytesPerChunk) {
  8757. + T(YAFFS_TRACE_ERROR, (
  8758. + TSTR("AddrToChunk of offset %d gives chunk %d start %d"
  8759. + TENDSTR),
  8760. + (int)offset, chunk, start));
  8761. + }
  8762. + chunk++;
  8763. +
  8764. + /* OK now check for the curveball where the start and end are in
  8765. + * the same chunk.
  8766. + */
  8767. +
  8768. + if ((start + n) < dev->nDataBytesPerChunk) {
  8769. + nToCopy = n;
  8770. +
  8771. + /* Now folks, to calculate how many bytes to write back....
  8772. + * If we're overwriting and not writing to then end of file then
  8773. + * we need to write back as much as was there before.
  8774. + */
  8775. +
  8776. + chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
  8777. +
  8778. + if (chunkStart > in->variant.fileVariant.fileSize)
  8779. + nBytesRead = 0; /* Past end of file */
  8780. + else
  8781. + nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
  8782. +
  8783. + if (nBytesRead > dev->nDataBytesPerChunk)
  8784. + nBytesRead = dev->nDataBytesPerChunk;
  8785. +
  8786. + nToWriteBack =
  8787. + (nBytesRead >
  8788. + (start + n)) ? nBytesRead : (start + n);
  8789. +
  8790. + if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
  8791. + YBUG();
  8792. +
  8793. + } else {
  8794. + nToCopy = dev->nDataBytesPerChunk - start;
  8795. + nToWriteBack = dev->nDataBytesPerChunk;
  8796. + }
  8797. +
  8798. + if (nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
  8799. + /* An incomplete start or end chunk (or maybe both start and end chunk),
  8800. + * or we're using inband tags, so we want to use the cache buffers.
  8801. + */
  8802. + if (dev->nShortOpCaches > 0) {
  8803. + yaffs_ChunkCache *cache;
  8804. + /* If we can't find the data in the cache, then load the cache */
  8805. + cache = yaffs_FindChunkCache(in, chunk);
  8806. +
  8807. + if (!cache
  8808. + && yaffs_CheckSpaceForAllocation(in->
  8809. + myDev)) {
  8810. + cache = yaffs_GrabChunkCache(in->myDev);
  8811. + cache->object = in;
  8812. + cache->chunkId = chunk;
  8813. + cache->dirty = 0;
  8814. + cache->locked = 0;
  8815. + yaffs_ReadChunkDataFromObject(in, chunk,
  8816. + cache->
  8817. + data);
  8818. + } else if (cache &&
  8819. + !cache->dirty &&
  8820. + !yaffs_CheckSpaceForAllocation(in->myDev)) {
  8821. + /* Drop the cache if it was a read cache item and
  8822. + * no space check has been made for it.
  8823. + */
  8824. + cache = NULL;
  8825. + }
  8826. +
  8827. + if (cache) {
  8828. + yaffs_UseChunkCache(dev, cache, 1);
  8829. + cache->locked = 1;
  8830. +
  8831. +
  8832. + memcpy(&cache->data[start], buffer,
  8833. + nToCopy);
  8834. +
  8835. +
  8836. + cache->locked = 0;
  8837. + cache->nBytes = nToWriteBack;
  8838. +
  8839. + if (writeThrough) {
  8840. + chunkWritten =
  8841. + yaffs_WriteChunkDataToObject
  8842. + (cache->object,
  8843. + cache->chunkId,
  8844. + cache->data, cache->nBytes,
  8845. + 1);
  8846. + cache->dirty = 0;
  8847. + }
  8848. +
  8849. + } else {
  8850. + chunkWritten = -1; /* fail the write */
  8851. + }
  8852. + } else {
  8853. + /* An incomplete start or end chunk (or maybe both start and end chunk)
  8854. + * Read into the local buffer then copy, then copy over and write back.
  8855. + */
  8856. +
  8857. + __u8 *localBuffer =
  8858. + yaffs_GetTempBuffer(dev, __LINE__);
  8859. +
  8860. + yaffs_ReadChunkDataFromObject(in, chunk,
  8861. + localBuffer);
  8862. +
  8863. +
  8864. +
  8865. + memcpy(&localBuffer[start], buffer, nToCopy);
  8866. +
  8867. + chunkWritten =
  8868. + yaffs_WriteChunkDataToObject(in, chunk,
  8869. + localBuffer,
  8870. + nToWriteBack,
  8871. + 0);
  8872. +
  8873. + yaffs_ReleaseTempBuffer(dev, localBuffer,
  8874. + __LINE__);
  8875. +
  8876. + }
  8877. +
  8878. + } else {
  8879. + /* A full chunk. Write directly from the supplied buffer. */
  8880. +
  8881. +
  8882. +
  8883. + chunkWritten =
  8884. + yaffs_WriteChunkDataToObject(in, chunk, buffer,
  8885. + dev->nDataBytesPerChunk,
  8886. + 0);
  8887. +
  8888. + /* Since we've overwritten the cached data, we better invalidate it. */
  8889. + yaffs_InvalidateChunkCache(in, chunk);
  8890. + }
  8891. +
  8892. + if (chunkWritten >= 0) {
  8893. + n -= nToCopy;
  8894. + offset += nToCopy;
  8895. + buffer += nToCopy;
  8896. + nDone += nToCopy;
  8897. + }
  8898. +
  8899. + }
  8900. +
  8901. + /* Update file object */
  8902. +
  8903. + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
  8904. + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
  8905. +
  8906. + in->dirty = 1;
  8907. +
  8908. + return nDone;
  8909. +}
  8910. +
  8911. +
  8912. +/* ---------------------- File resizing stuff ------------------ */
  8913. +
  8914. +static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
  8915. +{
  8916. +
  8917. + yaffs_Device *dev = in->myDev;
  8918. + int oldFileSize = in->variant.fileVariant.fileSize;
  8919. +
  8920. + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
  8921. +
  8922. + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
  8923. + dev->nDataBytesPerChunk;
  8924. + int i;
  8925. + int chunkId;
  8926. +
  8927. + /* Delete backwards so that we don't end up with holes if
  8928. + * power is lost part-way through the operation.
  8929. + */
  8930. + for (i = lastDel; i >= startDel; i--) {
  8931. + /* NB this could be optimised somewhat,
  8932. + * eg. could retrieve the tags and write them without
  8933. + * using yaffs_DeleteChunk
  8934. + */
  8935. +
  8936. + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
  8937. + if (chunkId > 0) {
  8938. + if (chunkId <
  8939. + (dev->internalStartBlock * dev->nChunksPerBlock)
  8940. + || chunkId >=
  8941. + ((dev->internalEndBlock +
  8942. + 1) * dev->nChunksPerBlock)) {
  8943. + T(YAFFS_TRACE_ALWAYS,
  8944. + (TSTR("Found daft chunkId %d for %d" TENDSTR),
  8945. + chunkId, i));
  8946. + } else {
  8947. + in->nDataChunks--;
  8948. + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
  8949. + }
  8950. + }
  8951. + }
  8952. +
  8953. +}
  8954. +
  8955. +int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
  8956. +{
  8957. +
  8958. + int oldFileSize = in->variant.fileVariant.fileSize;
  8959. + __u32 newSizeOfPartialChunk;
  8960. + int newFullChunks;
  8961. +
  8962. + yaffs_Device *dev = in->myDev;
  8963. +
  8964. + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
  8965. +
  8966. + yaffs_FlushFilesChunkCache(in);
  8967. + yaffs_InvalidateWholeChunkCache(in);
  8968. +
  8969. + yaffs_CheckGarbageCollection(dev);
  8970. +
  8971. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
  8972. + return YAFFS_FAIL;
  8973. +
  8974. + if (newSize == oldFileSize)
  8975. + return YAFFS_OK;
  8976. +
  8977. + if (newSize < oldFileSize) {
  8978. +
  8979. + yaffs_PruneResizedChunks(in, newSize);
  8980. +
  8981. + if (newSizeOfPartialChunk != 0) {
  8982. + int lastChunk = 1 + newFullChunks;
  8983. +
  8984. + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
  8985. +
  8986. + /* Got to read and rewrite the last chunk with its new size and zero pad */
  8987. + yaffs_ReadChunkDataFromObject(in, lastChunk,
  8988. + localBuffer);
  8989. +
  8990. + memset(localBuffer + newSizeOfPartialChunk, 0,
  8991. + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
  8992. +
  8993. + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
  8994. + newSizeOfPartialChunk, 1);
  8995. +
  8996. + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
  8997. + }
  8998. +
  8999. + in->variant.fileVariant.fileSize = newSize;
  9000. +
  9001. + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
  9002. + } else {
  9003. + /* newsSize > oldFileSize */
  9004. + in->variant.fileVariant.fileSize = newSize;
  9005. + }
  9006. +
  9007. +
  9008. + /* Write a new object header.
  9009. + * show we've shrunk the file, if need be
  9010. + * Do this only if the file is not in the deleted directories.
  9011. + */
  9012. + if (in->parent &&
  9013. + in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
  9014. + in->parent->objectId != YAFFS_OBJECTID_DELETED)
  9015. + yaffs_UpdateObjectHeader(in, NULL, 0,
  9016. + (newSize < oldFileSize) ? 1 : 0, 0);
  9017. +
  9018. + return YAFFS_OK;
  9019. +}
  9020. +
  9021. +loff_t yaffs_GetFileSize(yaffs_Object *obj)
  9022. +{
  9023. + obj = yaffs_GetEquivalentObject(obj);
  9024. +
  9025. + switch (obj->variantType) {
  9026. + case YAFFS_OBJECT_TYPE_FILE:
  9027. + return obj->variant.fileVariant.fileSize;
  9028. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9029. + return yaffs_strlen(obj->variant.symLinkVariant.alias);
  9030. + default:
  9031. + return 0;
  9032. + }
  9033. +}
  9034. +
  9035. +
  9036. +
  9037. +int yaffs_FlushFile(yaffs_Object *in, int updateTime)
  9038. +{
  9039. + int retVal;
  9040. + if (in->dirty) {
  9041. + yaffs_FlushFilesChunkCache(in);
  9042. + if (updateTime) {
  9043. +#ifdef CONFIG_YAFFS_WINCE
  9044. + yfsd_WinFileTimeNow(in->win_mtime);
  9045. +#else
  9046. +
  9047. + in->yst_mtime = Y_CURRENT_TIME;
  9048. +
  9049. +#endif
  9050. + }
  9051. +
  9052. + retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
  9053. + 0) ? YAFFS_OK : YAFFS_FAIL;
  9054. + } else {
  9055. + retVal = YAFFS_OK;
  9056. + }
  9057. +
  9058. + return retVal;
  9059. +
  9060. +}
  9061. +
  9062. +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
  9063. +{
  9064. +
  9065. + /* First off, invalidate the file's data in the cache, without flushing. */
  9066. + yaffs_InvalidateWholeChunkCache(in);
  9067. +
  9068. + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
  9069. + /* Move to the unlinked directory so we have a record that it was deleted. */
  9070. + yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
  9071. +
  9072. + }
  9073. +
  9074. + yaffs_RemoveObjectFromDirectory(in);
  9075. + yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
  9076. + in->hdrChunk = 0;
  9077. +
  9078. + yaffs_FreeObject(in);
  9079. + return YAFFS_OK;
  9080. +
  9081. +}
  9082. +
  9083. +/* yaffs_DeleteFile deletes the whole file data
  9084. + * and the inode associated with the file.
  9085. + * It does not delete the links associated with the file.
  9086. + */
  9087. +static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
  9088. +{
  9089. +
  9090. + int retVal;
  9091. + int immediateDeletion = 0;
  9092. +
  9093. +#ifdef __KERNEL__
  9094. + if (!in->myInode)
  9095. + immediateDeletion = 1;
  9096. +#else
  9097. + if (in->inUse <= 0)
  9098. + immediateDeletion = 1;
  9099. +#endif
  9100. +
  9101. + if (immediateDeletion) {
  9102. + retVal =
  9103. + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
  9104. + _Y("deleted"), 0, 0);
  9105. + T(YAFFS_TRACE_TRACING,
  9106. + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
  9107. + in->objectId));
  9108. + in->deleted = 1;
  9109. + in->myDev->nDeletedFiles++;
  9110. + if (1 || in->myDev->isYaffs2)
  9111. + yaffs_ResizeFile(in, 0);
  9112. + yaffs_SoftDeleteFile(in);
  9113. + } else {
  9114. + retVal =
  9115. + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
  9116. + _Y("unlinked"), 0, 0);
  9117. + }
  9118. +
  9119. +
  9120. + return retVal;
  9121. +}
  9122. +
  9123. +int yaffs_DeleteFile(yaffs_Object *in)
  9124. +{
  9125. + int retVal = YAFFS_OK;
  9126. + int deleted = in->deleted;
  9127. +
  9128. + yaffs_ResizeFile(in, 0);
  9129. +
  9130. + if (in->nDataChunks > 0) {
  9131. + /* Use soft deletion if there is data in the file.
  9132. + * That won't be the case if it has been resized to zero.
  9133. + */
  9134. + if (!in->unlinked)
  9135. + retVal = yaffs_UnlinkFileIfNeeded(in);
  9136. +
  9137. + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
  9138. + in->deleted = 1;
  9139. + deleted = 1;
  9140. + in->myDev->nDeletedFiles++;
  9141. + yaffs_SoftDeleteFile(in);
  9142. + }
  9143. + return deleted ? YAFFS_OK : YAFFS_FAIL;
  9144. + } else {
  9145. + /* The file has no data chunks so we toss it immediately */
  9146. + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
  9147. + in->variant.fileVariant.top = NULL;
  9148. + yaffs_DoGenericObjectDeletion(in);
  9149. +
  9150. + return YAFFS_OK;
  9151. + }
  9152. +}
  9153. +
  9154. +static int yaffs_DeleteDirectory(yaffs_Object *in)
  9155. +{
  9156. + /* First check that the directory is empty. */
  9157. + if (ylist_empty(&in->variant.directoryVariant.children))
  9158. + return yaffs_DoGenericObjectDeletion(in);
  9159. +
  9160. + return YAFFS_FAIL;
  9161. +
  9162. +}
  9163. +
  9164. +static int yaffs_DeleteSymLink(yaffs_Object *in)
  9165. +{
  9166. + YFREE(in->variant.symLinkVariant.alias);
  9167. +
  9168. + return yaffs_DoGenericObjectDeletion(in);
  9169. +}
  9170. +
  9171. +static int yaffs_DeleteHardLink(yaffs_Object *in)
  9172. +{
  9173. + /* remove this hardlink from the list assocaited with the equivalent
  9174. + * object
  9175. + */
  9176. + ylist_del_init(&in->hardLinks);
  9177. + return yaffs_DoGenericObjectDeletion(in);
  9178. +}
  9179. +
  9180. +int yaffs_DeleteObject(yaffs_Object *obj)
  9181. +{
  9182. +int retVal = -1;
  9183. + switch (obj->variantType) {
  9184. + case YAFFS_OBJECT_TYPE_FILE:
  9185. + retVal = yaffs_DeleteFile(obj);
  9186. + break;
  9187. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9188. + return yaffs_DeleteDirectory(obj);
  9189. + break;
  9190. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9191. + retVal = yaffs_DeleteSymLink(obj);
  9192. + break;
  9193. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9194. + retVal = yaffs_DeleteHardLink(obj);
  9195. + break;
  9196. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9197. + retVal = yaffs_DoGenericObjectDeletion(obj);
  9198. + break;
  9199. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9200. + retVal = 0;
  9201. + break; /* should not happen. */
  9202. + }
  9203. +
  9204. + return retVal;
  9205. +}
  9206. +
  9207. +static int yaffs_UnlinkWorker(yaffs_Object *obj)
  9208. +{
  9209. +
  9210. + int immediateDeletion = 0;
  9211. +
  9212. +#ifdef __KERNEL__
  9213. + if (!obj->myInode)
  9214. + immediateDeletion = 1;
  9215. +#else
  9216. + if (obj->inUse <= 0)
  9217. + immediateDeletion = 1;
  9218. +#endif
  9219. +
  9220. + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  9221. + return yaffs_DeleteHardLink(obj);
  9222. + } else if (!ylist_empty(&obj->hardLinks)) {
  9223. + /* Curve ball: We're unlinking an object that has a hardlink.
  9224. + *
  9225. + * This problem arises because we are not strictly following
  9226. + * The Linux link/inode model.
  9227. + *
  9228. + * We can't really delete the object.
  9229. + * Instead, we do the following:
  9230. + * - Select a hardlink.
  9231. + * - Unhook it from the hard links
  9232. + * - Unhook it from its parent directory (so that the rename can work)
  9233. + * - Rename the object to the hardlink's name.
  9234. + * - Delete the hardlink
  9235. + */
  9236. +
  9237. + yaffs_Object *hl;
  9238. + int retVal;
  9239. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  9240. +
  9241. + hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
  9242. +
  9243. + ylist_del_init(&hl->hardLinks);
  9244. + ylist_del_init(&hl->siblings);
  9245. +
  9246. + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  9247. +
  9248. + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
  9249. +
  9250. + if (retVal == YAFFS_OK)
  9251. + retVal = yaffs_DoGenericObjectDeletion(hl);
  9252. +
  9253. + return retVal;
  9254. +
  9255. + } else if (immediateDeletion) {
  9256. + switch (obj->variantType) {
  9257. + case YAFFS_OBJECT_TYPE_FILE:
  9258. + return yaffs_DeleteFile(obj);
  9259. + break;
  9260. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9261. + return yaffs_DeleteDirectory(obj);
  9262. + break;
  9263. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9264. + return yaffs_DeleteSymLink(obj);
  9265. + break;
  9266. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9267. + return yaffs_DoGenericObjectDeletion(obj);
  9268. + break;
  9269. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9270. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9271. + default:
  9272. + return YAFFS_FAIL;
  9273. + }
  9274. + } else
  9275. + return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
  9276. + _Y("unlinked"), 0, 0);
  9277. +}
  9278. +
  9279. +
  9280. +static int yaffs_UnlinkObject(yaffs_Object *obj)
  9281. +{
  9282. +
  9283. + if (obj && obj->unlinkAllowed)
  9284. + return yaffs_UnlinkWorker(obj);
  9285. +
  9286. + return YAFFS_FAIL;
  9287. +
  9288. +}
  9289. +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
  9290. +{
  9291. + yaffs_Object *obj;
  9292. +
  9293. + obj = yaffs_FindObjectByName(dir, name);
  9294. + return yaffs_UnlinkObject(obj);
  9295. +}
  9296. +
  9297. +/*----------------------- Initialisation Scanning ---------------------- */
  9298. +
  9299. +static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
  9300. + int backwardScanning)
  9301. +{
  9302. + yaffs_Object *obj;
  9303. +
  9304. + if (!backwardScanning) {
  9305. + /* Handle YAFFS1 forward scanning case
  9306. + * For YAFFS1 we always do the deletion
  9307. + */
  9308. +
  9309. + } else {
  9310. + /* Handle YAFFS2 case (backward scanning)
  9311. + * If the shadowed object exists then ignore.
  9312. + */
  9313. + if (yaffs_FindObjectByNumber(dev, objId))
  9314. + return;
  9315. + }
  9316. +
  9317. + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  9318. + * We put it in unlinked dir to be cleaned up after the scanning
  9319. + */
  9320. + obj =
  9321. + yaffs_FindOrCreateObjectByNumber(dev, objId,
  9322. + YAFFS_OBJECT_TYPE_FILE);
  9323. + if (!obj)
  9324. + return;
  9325. + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
  9326. + obj->variant.fileVariant.shrinkSize = 0;
  9327. + obj->valid = 1; /* So that we don't read any other info for this file */
  9328. +
  9329. +}
  9330. +
  9331. +typedef struct {
  9332. + int seq;
  9333. + int block;
  9334. +} yaffs_BlockIndex;
  9335. +
  9336. +
  9337. +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
  9338. +{
  9339. + yaffs_Object *hl;
  9340. + yaffs_Object *in;
  9341. +
  9342. + while (hardList) {
  9343. + hl = hardList;
  9344. + hardList = (yaffs_Object *) (hardList->hardLinks.next);
  9345. +
  9346. + in = yaffs_FindObjectByNumber(dev,
  9347. + hl->variant.hardLinkVariant.
  9348. + equivalentObjectId);
  9349. +
  9350. + if (in) {
  9351. + /* Add the hardlink pointers */
  9352. + hl->variant.hardLinkVariant.equivalentObject = in;
  9353. + ylist_add(&hl->hardLinks, &in->hardLinks);
  9354. + } else {
  9355. + /* Todo Need to report/handle this better.
  9356. + * Got a problem... hardlink to a non-existant object
  9357. + */
  9358. + hl->variant.hardLinkVariant.equivalentObject = NULL;
  9359. + YINIT_LIST_HEAD(&hl->hardLinks);
  9360. +
  9361. + }
  9362. + }
  9363. +}
  9364. +
  9365. +
  9366. +
  9367. +
  9368. +
  9369. +static int ybicmp(const void *a, const void *b)
  9370. +{
  9371. + register int aseq = ((yaffs_BlockIndex *)a)->seq;
  9372. + register int bseq = ((yaffs_BlockIndex *)b)->seq;
  9373. + register int ablock = ((yaffs_BlockIndex *)a)->block;
  9374. + register int bblock = ((yaffs_BlockIndex *)b)->block;
  9375. + if (aseq == bseq)
  9376. + return ablock - bblock;
  9377. + else
  9378. + return aseq - bseq;
  9379. +}
  9380. +
  9381. +
  9382. +struct yaffs_ShadowFixerStruct {
  9383. + int objectId;
  9384. + int shadowedId;
  9385. + struct yaffs_ShadowFixerStruct *next;
  9386. +};
  9387. +
  9388. +
  9389. +static void yaffs_StripDeletedObjects(yaffs_Device *dev)
  9390. +{
  9391. + /*
  9392. + * Sort out state of unlinked and deleted objects after scanning.
  9393. + */
  9394. + struct ylist_head *i;
  9395. + struct ylist_head *n;
  9396. + yaffs_Object *l;
  9397. +
  9398. + /* Soft delete all the unlinked files */
  9399. + ylist_for_each_safe(i, n,
  9400. + &dev->unlinkedDir->variant.directoryVariant.children) {
  9401. + if (i) {
  9402. + l = ylist_entry(i, yaffs_Object, siblings);
  9403. + yaffs_DeleteObject(l);
  9404. + }
  9405. + }
  9406. +
  9407. + ylist_for_each_safe(i, n,
  9408. + &dev->deletedDir->variant.directoryVariant.children) {
  9409. + if (i) {
  9410. + l = ylist_entry(i, yaffs_Object, siblings);
  9411. + yaffs_DeleteObject(l);
  9412. + }
  9413. + }
  9414. +
  9415. +}
  9416. +
  9417. +static int yaffs_Scan(yaffs_Device *dev)
  9418. +{
  9419. + yaffs_ExtendedTags tags;
  9420. + int blk;
  9421. + int blockIterator;
  9422. + int startIterator;
  9423. + int endIterator;
  9424. + int result;
  9425. +
  9426. + int chunk;
  9427. + int c;
  9428. + int deleted;
  9429. + yaffs_BlockState state;
  9430. + yaffs_Object *hardList = NULL;
  9431. + yaffs_BlockInfo *bi;
  9432. + __u32 sequenceNumber;
  9433. + yaffs_ObjectHeader *oh;
  9434. + yaffs_Object *in;
  9435. + yaffs_Object *parent;
  9436. +
  9437. + int alloc_failed = 0;
  9438. +
  9439. + struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
  9440. +
  9441. +
  9442. + __u8 *chunkData;
  9443. +
  9444. +
  9445. +
  9446. + T(YAFFS_TRACE_SCAN,
  9447. + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
  9448. + dev->internalStartBlock, dev->internalEndBlock));
  9449. +
  9450. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9451. +
  9452. + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  9453. +
  9454. + /* Scan all the blocks to determine their state */
  9455. + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  9456. + bi = yaffs_GetBlockInfo(dev, blk);
  9457. + yaffs_ClearChunkBits(dev, blk);
  9458. + bi->pagesInUse = 0;
  9459. + bi->softDeletions = 0;
  9460. +
  9461. + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  9462. +
  9463. + bi->blockState = state;
  9464. + bi->sequenceNumber = sequenceNumber;
  9465. +
  9466. + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
  9467. + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
  9468. +
  9469. + T(YAFFS_TRACE_SCAN_DEBUG,
  9470. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  9471. + state, sequenceNumber));
  9472. +
  9473. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  9474. + T(YAFFS_TRACE_BAD_BLOCKS,
  9475. + (TSTR("block %d is bad" TENDSTR), blk));
  9476. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  9477. + T(YAFFS_TRACE_SCAN_DEBUG,
  9478. + (TSTR("Block empty " TENDSTR)));
  9479. + dev->nErasedBlocks++;
  9480. + dev->nFreeChunks += dev->nChunksPerBlock;
  9481. + }
  9482. + }
  9483. +
  9484. + startIterator = dev->internalStartBlock;
  9485. + endIterator = dev->internalEndBlock;
  9486. +
  9487. + /* For each block.... */
  9488. + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
  9489. + blockIterator++) {
  9490. +
  9491. + YYIELD();
  9492. +
  9493. + YYIELD();
  9494. +
  9495. + blk = blockIterator;
  9496. +
  9497. + bi = yaffs_GetBlockInfo(dev, blk);
  9498. + state = bi->blockState;
  9499. +
  9500. + deleted = 0;
  9501. +
  9502. + /* For each chunk in each block that needs scanning....*/
  9503. + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
  9504. + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  9505. + /* Read the tags and decide what to do */
  9506. + chunk = blk * dev->nChunksPerBlock + c;
  9507. +
  9508. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  9509. + &tags);
  9510. +
  9511. + /* Let's have a good look at this chunk... */
  9512. +
  9513. + if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
  9514. + /* YAFFS1 only...
  9515. + * A deleted chunk
  9516. + */
  9517. + deleted++;
  9518. + dev->nFreeChunks++;
  9519. + /*T((" %d %d deleted\n",blk,c)); */
  9520. + } else if (!tags.chunkUsed) {
  9521. + /* An unassigned chunk in the block
  9522. + * This means that either the block is empty or
  9523. + * this is the one being allocated from
  9524. + */
  9525. +
  9526. + if (c == 0) {
  9527. + /* We're looking at the first chunk in the block so the block is unused */
  9528. + state = YAFFS_BLOCK_STATE_EMPTY;
  9529. + dev->nErasedBlocks++;
  9530. + } else {
  9531. + /* this is the block being allocated from */
  9532. + T(YAFFS_TRACE_SCAN,
  9533. + (TSTR
  9534. + (" Allocating from %d %d" TENDSTR),
  9535. + blk, c));
  9536. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  9537. + dev->allocationBlock = blk;
  9538. + dev->allocationPage = c;
  9539. + dev->allocationBlockFinder = blk;
  9540. + /* Set it to here to encourage the allocator to go forth from here. */
  9541. +
  9542. + }
  9543. +
  9544. + dev->nFreeChunks += (dev->nChunksPerBlock - c);
  9545. + } else if (tags.chunkId > 0) {
  9546. + /* chunkId > 0 so it is a data chunk... */
  9547. + unsigned int endpos;
  9548. +
  9549. + yaffs_SetChunkBit(dev, blk, c);
  9550. + bi->pagesInUse++;
  9551. +
  9552. + in = yaffs_FindOrCreateObjectByNumber(dev,
  9553. + tags.
  9554. + objectId,
  9555. + YAFFS_OBJECT_TYPE_FILE);
  9556. + /* PutChunkIntoFile checks for a clash (two data chunks with
  9557. + * the same chunkId).
  9558. + */
  9559. +
  9560. + if (!in)
  9561. + alloc_failed = 1;
  9562. +
  9563. + if (in) {
  9564. + if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
  9565. + alloc_failed = 1;
  9566. + }
  9567. +
  9568. + endpos =
  9569. + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
  9570. + tags.byteCount;
  9571. + if (in &&
  9572. + in->variantType == YAFFS_OBJECT_TYPE_FILE
  9573. + && in->variant.fileVariant.scannedFileSize <
  9574. + endpos) {
  9575. + in->variant.fileVariant.
  9576. + scannedFileSize = endpos;
  9577. + if (!dev->useHeaderFileSize) {
  9578. + in->variant.fileVariant.
  9579. + fileSize =
  9580. + in->variant.fileVariant.
  9581. + scannedFileSize;
  9582. + }
  9583. +
  9584. + }
  9585. + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
  9586. + } else {
  9587. + /* chunkId == 0, so it is an ObjectHeader.
  9588. + * Thus, we read in the object header and make the object
  9589. + */
  9590. + yaffs_SetChunkBit(dev, blk, c);
  9591. + bi->pagesInUse++;
  9592. +
  9593. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
  9594. + chunkData,
  9595. + NULL);
  9596. +
  9597. + oh = (yaffs_ObjectHeader *) chunkData;
  9598. +
  9599. + in = yaffs_FindObjectByNumber(dev,
  9600. + tags.objectId);
  9601. + if (in && in->variantType != oh->type) {
  9602. + /* This should not happen, but somehow
  9603. + * Wev'e ended up with an objectId that has been reused but not yet
  9604. + * deleted, and worse still it has changed type. Delete the old object.
  9605. + */
  9606. +
  9607. + yaffs_DeleteObject(in);
  9608. +
  9609. + in = 0;
  9610. + }
  9611. +
  9612. + in = yaffs_FindOrCreateObjectByNumber(dev,
  9613. + tags.
  9614. + objectId,
  9615. + oh->type);
  9616. +
  9617. + if (!in)
  9618. + alloc_failed = 1;
  9619. +
  9620. + if (in && oh->shadowsObject > 0) {
  9621. +
  9622. + struct yaffs_ShadowFixerStruct *fixer;
  9623. + fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
  9624. + if (fixer) {
  9625. + fixer->next = shadowFixerList;
  9626. + shadowFixerList = fixer;
  9627. + fixer->objectId = tags.objectId;
  9628. + fixer->shadowedId = oh->shadowsObject;
  9629. + }
  9630. +
  9631. + }
  9632. +
  9633. + if (in && in->valid) {
  9634. + /* We have already filled this one. We have a duplicate and need to resolve it. */
  9635. +
  9636. + unsigned existingSerial = in->serial;
  9637. + unsigned newSerial = tags.serialNumber;
  9638. +
  9639. + if (((existingSerial + 1) & 3) == newSerial) {
  9640. + /* Use new one - destroy the exisiting one */
  9641. + yaffs_DeleteChunk(dev,
  9642. + in->hdrChunk,
  9643. + 1, __LINE__);
  9644. + in->valid = 0;
  9645. + } else {
  9646. + /* Use existing - destroy this one. */
  9647. + yaffs_DeleteChunk(dev, chunk, 1,
  9648. + __LINE__);
  9649. + }
  9650. + }
  9651. +
  9652. + if (in && !in->valid &&
  9653. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  9654. + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
  9655. + /* We only load some info, don't fiddle with directory structure */
  9656. + in->valid = 1;
  9657. + in->variantType = oh->type;
  9658. +
  9659. + in->yst_mode = oh->yst_mode;
  9660. +#ifdef CONFIG_YAFFS_WINCE
  9661. + in->win_atime[0] = oh->win_atime[0];
  9662. + in->win_ctime[0] = oh->win_ctime[0];
  9663. + in->win_mtime[0] = oh->win_mtime[0];
  9664. + in->win_atime[1] = oh->win_atime[1];
  9665. + in->win_ctime[1] = oh->win_ctime[1];
  9666. + in->win_mtime[1] = oh->win_mtime[1];
  9667. +#else
  9668. + in->yst_uid = oh->yst_uid;
  9669. + in->yst_gid = oh->yst_gid;
  9670. + in->yst_atime = oh->yst_atime;
  9671. + in->yst_mtime = oh->yst_mtime;
  9672. + in->yst_ctime = oh->yst_ctime;
  9673. + in->yst_rdev = oh->yst_rdev;
  9674. +#endif
  9675. + in->hdrChunk = chunk;
  9676. + in->serial = tags.serialNumber;
  9677. +
  9678. + } else if (in && !in->valid) {
  9679. + /* we need to load this info */
  9680. +
  9681. + in->valid = 1;
  9682. + in->variantType = oh->type;
  9683. +
  9684. + in->yst_mode = oh->yst_mode;
  9685. +#ifdef CONFIG_YAFFS_WINCE
  9686. + in->win_atime[0] = oh->win_atime[0];
  9687. + in->win_ctime[0] = oh->win_ctime[0];
  9688. + in->win_mtime[0] = oh->win_mtime[0];
  9689. + in->win_atime[1] = oh->win_atime[1];
  9690. + in->win_ctime[1] = oh->win_ctime[1];
  9691. + in->win_mtime[1] = oh->win_mtime[1];
  9692. +#else
  9693. + in->yst_uid = oh->yst_uid;
  9694. + in->yst_gid = oh->yst_gid;
  9695. + in->yst_atime = oh->yst_atime;
  9696. + in->yst_mtime = oh->yst_mtime;
  9697. + in->yst_ctime = oh->yst_ctime;
  9698. + in->yst_rdev = oh->yst_rdev;
  9699. +#endif
  9700. + in->hdrChunk = chunk;
  9701. + in->serial = tags.serialNumber;
  9702. +
  9703. + yaffs_SetObjectName(in, oh->name);
  9704. + in->dirty = 0;
  9705. +
  9706. + /* directory stuff...
  9707. + * hook up to parent
  9708. + */
  9709. +
  9710. + parent =
  9711. + yaffs_FindOrCreateObjectByNumber
  9712. + (dev, oh->parentObjectId,
  9713. + YAFFS_OBJECT_TYPE_DIRECTORY);
  9714. + if (!parent)
  9715. + alloc_failed = 1;
  9716. + if (parent && parent->variantType ==
  9717. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  9718. + /* Set up as a directory */
  9719. + parent->variantType =
  9720. + YAFFS_OBJECT_TYPE_DIRECTORY;
  9721. + YINIT_LIST_HEAD(&parent->variant.
  9722. + directoryVariant.
  9723. + children);
  9724. + } else if (!parent || parent->variantType !=
  9725. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  9726. + /* Hoosterman, another problem....
  9727. + * We're trying to use a non-directory as a directory
  9728. + */
  9729. +
  9730. + T(YAFFS_TRACE_ERROR,
  9731. + (TSTR
  9732. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  9733. + TENDSTR)));
  9734. + parent = dev->lostNFoundDir;
  9735. + }
  9736. +
  9737. + yaffs_AddObjectToDirectory(parent, in);
  9738. +
  9739. + if (0 && (parent == dev->deletedDir ||
  9740. + parent == dev->unlinkedDir)) {
  9741. + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  9742. + dev->nDeletedFiles++;
  9743. + }
  9744. + /* Note re hardlinks.
  9745. + * Since we might scan a hardlink before its equivalent object is scanned
  9746. + * we put them all in a list.
  9747. + * After scanning is complete, we should have all the objects, so we run through this
  9748. + * list and fix up all the chains.
  9749. + */
  9750. +
  9751. + switch (in->variantType) {
  9752. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9753. + /* Todo got a problem */
  9754. + break;
  9755. + case YAFFS_OBJECT_TYPE_FILE:
  9756. + if (dev->useHeaderFileSize)
  9757. +
  9758. + in->variant.fileVariant.
  9759. + fileSize =
  9760. + oh->fileSize;
  9761. +
  9762. + break;
  9763. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9764. + in->variant.hardLinkVariant.
  9765. + equivalentObjectId =
  9766. + oh->equivalentObjectId;
  9767. + in->hardLinks.next =
  9768. + (struct ylist_head *)
  9769. + hardList;
  9770. + hardList = in;
  9771. + break;
  9772. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9773. + /* Do nothing */
  9774. + break;
  9775. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9776. + /* Do nothing */
  9777. + break;
  9778. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9779. + in->variant.symLinkVariant.alias =
  9780. + yaffs_CloneString(oh->alias);
  9781. + if (!in->variant.symLinkVariant.alias)
  9782. + alloc_failed = 1;
  9783. + break;
  9784. + }
  9785. +
  9786. +/*
  9787. + if (parent == dev->deletedDir) {
  9788. + yaffs_DestroyObject(in);
  9789. + bi->hasShrinkHeader = 1;
  9790. + }
  9791. +*/
  9792. + }
  9793. + }
  9794. + }
  9795. +
  9796. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  9797. + /* If we got this far while scanning, then the block is fully allocated.*/
  9798. + state = YAFFS_BLOCK_STATE_FULL;
  9799. + }
  9800. +
  9801. + bi->blockState = state;
  9802. +
  9803. + /* Now let's see if it was dirty */
  9804. + if (bi->pagesInUse == 0 &&
  9805. + !bi->hasShrinkHeader &&
  9806. + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  9807. + yaffs_BlockBecameDirty(dev, blk);
  9808. + }
  9809. +
  9810. + }
  9811. +
  9812. +
  9813. + /* Ok, we've done all the scanning.
  9814. + * Fix up the hard link chains.
  9815. + * We should now have scanned all the objects, now it's time to add these
  9816. + * hardlinks.
  9817. + */
  9818. +
  9819. + yaffs_HardlinkFixup(dev, hardList);
  9820. +
  9821. + /* Fix up any shadowed objects */
  9822. + {
  9823. + struct yaffs_ShadowFixerStruct *fixer;
  9824. + yaffs_Object *obj;
  9825. +
  9826. + while (shadowFixerList) {
  9827. + fixer = shadowFixerList;
  9828. + shadowFixerList = fixer->next;
  9829. + /* Complete the rename transaction by deleting the shadowed object
  9830. + * then setting the object header to unshadowed.
  9831. + */
  9832. + obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
  9833. + if (obj)
  9834. + yaffs_DeleteObject(obj);
  9835. +
  9836. + obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
  9837. +
  9838. + if (obj)
  9839. + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
  9840. +
  9841. + YFREE(fixer);
  9842. + }
  9843. + }
  9844. +
  9845. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  9846. +
  9847. + if (alloc_failed)
  9848. + return YAFFS_FAIL;
  9849. +
  9850. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
  9851. +
  9852. +
  9853. + return YAFFS_OK;
  9854. +}
  9855. +
  9856. +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
  9857. +{
  9858. + __u8 *chunkData;
  9859. + yaffs_ObjectHeader *oh;
  9860. + yaffs_Device *dev;
  9861. + yaffs_ExtendedTags tags;
  9862. + int result;
  9863. + int alloc_failed = 0;
  9864. +
  9865. + if (!in)
  9866. + return;
  9867. +
  9868. + dev = in->myDev;
  9869. +
  9870. +#if 0
  9871. + T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
  9872. + in->objectId,
  9873. + in->lazyLoaded ? "not yet" : "already"));
  9874. +#endif
  9875. +
  9876. + if (in->lazyLoaded && in->hdrChunk > 0) {
  9877. + in->lazyLoaded = 0;
  9878. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9879. +
  9880. + result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
  9881. + oh = (yaffs_ObjectHeader *) chunkData;
  9882. +
  9883. + in->yst_mode = oh->yst_mode;
  9884. +#ifdef CONFIG_YAFFS_WINCE
  9885. + in->win_atime[0] = oh->win_atime[0];
  9886. + in->win_ctime[0] = oh->win_ctime[0];
  9887. + in->win_mtime[0] = oh->win_mtime[0];
  9888. + in->win_atime[1] = oh->win_atime[1];
  9889. + in->win_ctime[1] = oh->win_ctime[1];
  9890. + in->win_mtime[1] = oh->win_mtime[1];
  9891. +#else
  9892. + in->yst_uid = oh->yst_uid;
  9893. + in->yst_gid = oh->yst_gid;
  9894. + in->yst_atime = oh->yst_atime;
  9895. + in->yst_mtime = oh->yst_mtime;
  9896. + in->yst_ctime = oh->yst_ctime;
  9897. + in->yst_rdev = oh->yst_rdev;
  9898. +
  9899. +#endif
  9900. + yaffs_SetObjectName(in, oh->name);
  9901. +
  9902. + if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
  9903. + in->variant.symLinkVariant.alias =
  9904. + yaffs_CloneString(oh->alias);
  9905. + if (!in->variant.symLinkVariant.alias)
  9906. + alloc_failed = 1; /* Not returned to caller */
  9907. + }
  9908. +
  9909. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  9910. + }
  9911. +}
  9912. +
  9913. +static int yaffs_ScanBackwards(yaffs_Device *dev)
  9914. +{
  9915. + yaffs_ExtendedTags tags;
  9916. + int blk;
  9917. + int blockIterator;
  9918. + int startIterator;
  9919. + int endIterator;
  9920. + int nBlocksToScan = 0;
  9921. +
  9922. + int chunk;
  9923. + int result;
  9924. + int c;
  9925. + int deleted;
  9926. + yaffs_BlockState state;
  9927. + yaffs_Object *hardList = NULL;
  9928. + yaffs_BlockInfo *bi;
  9929. + __u32 sequenceNumber;
  9930. + yaffs_ObjectHeader *oh;
  9931. + yaffs_Object *in;
  9932. + yaffs_Object *parent;
  9933. + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  9934. + int itsUnlinked;
  9935. + __u8 *chunkData;
  9936. +
  9937. + int fileSize;
  9938. + int isShrink;
  9939. + int foundChunksInBlock;
  9940. + int equivalentObjectId;
  9941. + int alloc_failed = 0;
  9942. +
  9943. +
  9944. + yaffs_BlockIndex *blockIndex = NULL;
  9945. + int altBlockIndex = 0;
  9946. +
  9947. + if (!dev->isYaffs2) {
  9948. + T(YAFFS_TRACE_SCAN,
  9949. + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
  9950. + return YAFFS_FAIL;
  9951. + }
  9952. +
  9953. + T(YAFFS_TRACE_SCAN,
  9954. + (TSTR
  9955. + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
  9956. + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
  9957. +
  9958. +
  9959. + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  9960. +
  9961. + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
  9962. +
  9963. + if (!blockIndex) {
  9964. + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
  9965. + altBlockIndex = 1;
  9966. + }
  9967. +
  9968. + if (!blockIndex) {
  9969. + T(YAFFS_TRACE_SCAN,
  9970. + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
  9971. + return YAFFS_FAIL;
  9972. + }
  9973. +
  9974. + dev->blocksInCheckpoint = 0;
  9975. +
  9976. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9977. +
  9978. + /* Scan all the blocks to determine their state */
  9979. + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  9980. + bi = yaffs_GetBlockInfo(dev, blk);
  9981. + yaffs_ClearChunkBits(dev, blk);
  9982. + bi->pagesInUse = 0;
  9983. + bi->softDeletions = 0;
  9984. +
  9985. + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  9986. +
  9987. + bi->blockState = state;
  9988. + bi->sequenceNumber = sequenceNumber;
  9989. +
  9990. + if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  9991. + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
  9992. + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
  9993. + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
  9994. +
  9995. + T(YAFFS_TRACE_SCAN_DEBUG,
  9996. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  9997. + state, sequenceNumber));
  9998. +
  9999. +
  10000. + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  10001. + dev->blocksInCheckpoint++;
  10002. +
  10003. + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
  10004. + T(YAFFS_TRACE_BAD_BLOCKS,
  10005. + (TSTR("block %d is bad" TENDSTR), blk));
  10006. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  10007. + T(YAFFS_TRACE_SCAN_DEBUG,
  10008. + (TSTR("Block empty " TENDSTR)));
  10009. + dev->nErasedBlocks++;
  10010. + dev->nFreeChunks += dev->nChunksPerBlock;
  10011. + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  10012. +
  10013. + /* Determine the highest sequence number */
  10014. + if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  10015. + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  10016. +
  10017. + blockIndex[nBlocksToScan].seq = sequenceNumber;
  10018. + blockIndex[nBlocksToScan].block = blk;
  10019. +
  10020. + nBlocksToScan++;
  10021. +
  10022. + if (sequenceNumber >= dev->sequenceNumber)
  10023. + dev->sequenceNumber = sequenceNumber;
  10024. + } else {
  10025. + /* TODO: Nasty sequence number! */
  10026. + T(YAFFS_TRACE_SCAN,
  10027. + (TSTR
  10028. + ("Block scanning block %d has bad sequence number %d"
  10029. + TENDSTR), blk, sequenceNumber));
  10030. +
  10031. + }
  10032. + }
  10033. + }
  10034. +
  10035. + T(YAFFS_TRACE_SCAN,
  10036. + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
  10037. +
  10038. +
  10039. +
  10040. + YYIELD();
  10041. +
  10042. + /* Sort the blocks */
  10043. +#ifndef CONFIG_YAFFS_USE_OWN_SORT
  10044. + {
  10045. + /* Use qsort now. */
  10046. + yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
  10047. + }
  10048. +#else
  10049. + {
  10050. + /* Dungy old bubble sort... */
  10051. +
  10052. + yaffs_BlockIndex temp;
  10053. + int i;
  10054. + int j;
  10055. +
  10056. + for (i = 0; i < nBlocksToScan; i++)
  10057. + for (j = i + 1; j < nBlocksToScan; j++)
  10058. + if (blockIndex[i].seq > blockIndex[j].seq) {
  10059. + temp = blockIndex[j];
  10060. + blockIndex[j] = blockIndex[i];
  10061. + blockIndex[i] = temp;
  10062. + }
  10063. + }
  10064. +#endif
  10065. +
  10066. + YYIELD();
  10067. +
  10068. + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
  10069. +
  10070. + /* Now scan the blocks looking at the data. */
  10071. + startIterator = 0;
  10072. + endIterator = nBlocksToScan - 1;
  10073. + T(YAFFS_TRACE_SCAN_DEBUG,
  10074. + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
  10075. +
  10076. + /* For each block.... backwards */
  10077. + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
  10078. + blockIterator--) {
  10079. + /* Cooperative multitasking! This loop can run for so
  10080. + long that watchdog timers expire. */
  10081. + YYIELD();
  10082. +
  10083. + /* get the block to scan in the correct order */
  10084. + blk = blockIndex[blockIterator].block;
  10085. +
  10086. + bi = yaffs_GetBlockInfo(dev, blk);
  10087. +
  10088. +
  10089. + state = bi->blockState;
  10090. +
  10091. + deleted = 0;
  10092. +
  10093. + /* For each chunk in each block that needs scanning.... */
  10094. + foundChunksInBlock = 0;
  10095. + for (c = dev->nChunksPerBlock - 1;
  10096. + !alloc_failed && c >= 0 &&
  10097. + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  10098. + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
  10099. + /* Scan backwards...
  10100. + * Read the tags and decide what to do
  10101. + */
  10102. +
  10103. + chunk = blk * dev->nChunksPerBlock + c;
  10104. +
  10105. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  10106. + &tags);
  10107. +
  10108. + /* Let's have a good look at this chunk... */
  10109. +
  10110. + if (!tags.chunkUsed) {
  10111. + /* An unassigned chunk in the block.
  10112. + * If there are used chunks after this one, then
  10113. + * it is a chunk that was skipped due to failing the erased
  10114. + * check. Just skip it so that it can be deleted.
  10115. + * But, more typically, We get here when this is an unallocated
  10116. + * chunk and his means that either the block is empty or
  10117. + * this is the one being allocated from
  10118. + */
  10119. +
  10120. + if (foundChunksInBlock) {
  10121. + /* This is a chunk that was skipped due to failing the erased check */
  10122. + } else if (c == 0) {
  10123. + /* We're looking at the first chunk in the block so the block is unused */
  10124. + state = YAFFS_BLOCK_STATE_EMPTY;
  10125. + dev->nErasedBlocks++;
  10126. + } else {
  10127. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  10128. + state == YAFFS_BLOCK_STATE_ALLOCATING) {
  10129. + if (dev->sequenceNumber == bi->sequenceNumber) {
  10130. + /* this is the block being allocated from */
  10131. +
  10132. + T(YAFFS_TRACE_SCAN,
  10133. + (TSTR
  10134. + (" Allocating from %d %d"
  10135. + TENDSTR), blk, c));
  10136. +
  10137. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  10138. + dev->allocationBlock = blk;
  10139. + dev->allocationPage = c;
  10140. + dev->allocationBlockFinder = blk;
  10141. + } else {
  10142. + /* This is a partially written block that is not
  10143. + * the current allocation block. This block must have
  10144. + * had a write failure, so set up for retirement.
  10145. + */
  10146. +
  10147. + /* bi->needsRetiring = 1; ??? TODO */
  10148. + bi->gcPrioritise = 1;
  10149. +
  10150. + T(YAFFS_TRACE_ALWAYS,
  10151. + (TSTR("Partially written block %d detected" TENDSTR),
  10152. + blk));
  10153. + }
  10154. + }
  10155. + }
  10156. +
  10157. + dev->nFreeChunks++;
  10158. +
  10159. + } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
  10160. + T(YAFFS_TRACE_SCAN,
  10161. + (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
  10162. + blk, c));
  10163. +
  10164. + dev->nFreeChunks++;
  10165. +
  10166. + } else if (tags.chunkId > 0) {
  10167. + /* chunkId > 0 so it is a data chunk... */
  10168. + unsigned int endpos;
  10169. + __u32 chunkBase =
  10170. + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
  10171. +
  10172. + foundChunksInBlock = 1;
  10173. +
  10174. +
  10175. + yaffs_SetChunkBit(dev, blk, c);
  10176. + bi->pagesInUse++;
  10177. +
  10178. + in = yaffs_FindOrCreateObjectByNumber(dev,
  10179. + tags.
  10180. + objectId,
  10181. + YAFFS_OBJECT_TYPE_FILE);
  10182. + if (!in) {
  10183. + /* Out of memory */
  10184. + alloc_failed = 1;
  10185. + }
  10186. +
  10187. + if (in &&
  10188. + in->variantType == YAFFS_OBJECT_TYPE_FILE
  10189. + && chunkBase <
  10190. + in->variant.fileVariant.shrinkSize) {
  10191. + /* This has not been invalidated by a resize */
  10192. + if (!yaffs_PutChunkIntoFile(in, tags.chunkId,
  10193. + chunk, -1)) {
  10194. + alloc_failed = 1;
  10195. + }
  10196. +
  10197. + /* File size is calculated by looking at the data chunks if we have not
  10198. + * seen an object header yet. Stop this practice once we find an object header.
  10199. + */
  10200. + endpos =
  10201. + (tags.chunkId -
  10202. + 1) * dev->nDataBytesPerChunk +
  10203. + tags.byteCount;
  10204. +
  10205. + if (!in->valid && /* have not got an object header yet */
  10206. + in->variant.fileVariant.
  10207. + scannedFileSize < endpos) {
  10208. + in->variant.fileVariant.
  10209. + scannedFileSize = endpos;
  10210. + in->variant.fileVariant.
  10211. + fileSize =
  10212. + in->variant.fileVariant.
  10213. + scannedFileSize;
  10214. + }
  10215. +
  10216. + } else if (in) {
  10217. + /* This chunk has been invalidated by a resize, so delete */
  10218. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  10219. +
  10220. + }
  10221. + } else {
  10222. + /* chunkId == 0, so it is an ObjectHeader.
  10223. + * Thus, we read in the object header and make the object
  10224. + */
  10225. + foundChunksInBlock = 1;
  10226. +
  10227. + yaffs_SetChunkBit(dev, blk, c);
  10228. + bi->pagesInUse++;
  10229. +
  10230. + oh = NULL;
  10231. + in = NULL;
  10232. +
  10233. + if (tags.extraHeaderInfoAvailable) {
  10234. + in = yaffs_FindOrCreateObjectByNumber
  10235. + (dev, tags.objectId,
  10236. + tags.extraObjectType);
  10237. + if (!in)
  10238. + alloc_failed = 1;
  10239. + }
  10240. +
  10241. + if (!in ||
  10242. +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  10243. + !in->valid ||
  10244. +#endif
  10245. + tags.extraShadows ||
  10246. + (!in->valid &&
  10247. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  10248. + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
  10249. +
  10250. + /* If we don't have valid info then we need to read the chunk
  10251. + * TODO In future we can probably defer reading the chunk and
  10252. + * living with invalid data until needed.
  10253. + */
  10254. +
  10255. + result = yaffs_ReadChunkWithTagsFromNAND(dev,
  10256. + chunk,
  10257. + chunkData,
  10258. + NULL);
  10259. +
  10260. + oh = (yaffs_ObjectHeader *) chunkData;
  10261. +
  10262. + if (dev->inbandTags) {
  10263. + /* Fix up the header if they got corrupted by inband tags */
  10264. + oh->shadowsObject = oh->inbandShadowsObject;
  10265. + oh->isShrink = oh->inbandIsShrink;
  10266. + }
  10267. +
  10268. + if (!in) {
  10269. + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
  10270. + if (!in)
  10271. + alloc_failed = 1;
  10272. + }
  10273. +
  10274. + }
  10275. +
  10276. + if (!in) {
  10277. + /* TODO Hoosterman we have a problem! */
  10278. + T(YAFFS_TRACE_ERROR,
  10279. + (TSTR
  10280. + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
  10281. + TENDSTR), tags.objectId, chunk));
  10282. + continue;
  10283. + }
  10284. +
  10285. + if (in->valid) {
  10286. + /* We have already filled this one.
  10287. + * We have a duplicate that will be discarded, but
  10288. + * we first have to suck out resize info if it is a file.
  10289. + */
  10290. +
  10291. + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
  10292. + ((oh &&
  10293. + oh->type == YAFFS_OBJECT_TYPE_FILE) ||
  10294. + (tags.extraHeaderInfoAvailable &&
  10295. + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
  10296. + __u32 thisSize =
  10297. + (oh) ? oh->fileSize : tags.
  10298. + extraFileLength;
  10299. + __u32 parentObjectId =
  10300. + (oh) ? oh->
  10301. + parentObjectId : tags.
  10302. + extraParentObjectId;
  10303. +
  10304. +
  10305. + isShrink =
  10306. + (oh) ? oh->isShrink : tags.
  10307. + extraIsShrinkHeader;
  10308. +
  10309. + /* If it is deleted (unlinked at start also means deleted)
  10310. + * we treat the file size as being zeroed at this point.
  10311. + */
  10312. + if (parentObjectId ==
  10313. + YAFFS_OBJECTID_DELETED
  10314. + || parentObjectId ==
  10315. + YAFFS_OBJECTID_UNLINKED) {
  10316. + thisSize = 0;
  10317. + isShrink = 1;
  10318. + }
  10319. +
  10320. + if (isShrink &&
  10321. + in->variant.fileVariant.
  10322. + shrinkSize > thisSize) {
  10323. + in->variant.fileVariant.
  10324. + shrinkSize =
  10325. + thisSize;
  10326. + }
  10327. +
  10328. + if (isShrink)
  10329. + bi->hasShrinkHeader = 1;
  10330. +
  10331. + }
  10332. + /* Use existing - destroy this one. */
  10333. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  10334. +
  10335. + }
  10336. +
  10337. + if (!in->valid && in->variantType !=
  10338. + (oh ? oh->type : tags.extraObjectType))
  10339. + T(YAFFS_TRACE_ERROR, (
  10340. + TSTR("yaffs tragedy: Bad object type, "
  10341. + TCONT("%d != %d, for object %d at chunk ")
  10342. + TCONT("%d during scan")
  10343. + TENDSTR), oh ?
  10344. + oh->type : tags.extraObjectType,
  10345. + in->variantType, tags.objectId,
  10346. + chunk));
  10347. +
  10348. + if (!in->valid &&
  10349. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  10350. + tags.objectId ==
  10351. + YAFFS_OBJECTID_LOSTNFOUND)) {
  10352. + /* We only load some info, don't fiddle with directory structure */
  10353. + in->valid = 1;
  10354. +
  10355. + if (oh) {
  10356. + in->variantType = oh->type;
  10357. +
  10358. + in->yst_mode = oh->yst_mode;
  10359. +#ifdef CONFIG_YAFFS_WINCE
  10360. + in->win_atime[0] = oh->win_atime[0];
  10361. + in->win_ctime[0] = oh->win_ctime[0];
  10362. + in->win_mtime[0] = oh->win_mtime[0];
  10363. + in->win_atime[1] = oh->win_atime[1];
  10364. + in->win_ctime[1] = oh->win_ctime[1];
  10365. + in->win_mtime[1] = oh->win_mtime[1];
  10366. +#else
  10367. + in->yst_uid = oh->yst_uid;
  10368. + in->yst_gid = oh->yst_gid;
  10369. + in->yst_atime = oh->yst_atime;
  10370. + in->yst_mtime = oh->yst_mtime;
  10371. + in->yst_ctime = oh->yst_ctime;
  10372. + in->yst_rdev = oh->yst_rdev;
  10373. +
  10374. +#endif
  10375. + } else {
  10376. + in->variantType = tags.extraObjectType;
  10377. + in->lazyLoaded = 1;
  10378. + }
  10379. +
  10380. + in->hdrChunk = chunk;
  10381. +
  10382. + } else if (!in->valid) {
  10383. + /* we need to load this info */
  10384. +
  10385. + in->valid = 1;
  10386. + in->hdrChunk = chunk;
  10387. +
  10388. + if (oh) {
  10389. + in->variantType = oh->type;
  10390. +
  10391. + in->yst_mode = oh->yst_mode;
  10392. +#ifdef CONFIG_YAFFS_WINCE
  10393. + in->win_atime[0] = oh->win_atime[0];
  10394. + in->win_ctime[0] = oh->win_ctime[0];
  10395. + in->win_mtime[0] = oh->win_mtime[0];
  10396. + in->win_atime[1] = oh->win_atime[1];
  10397. + in->win_ctime[1] = oh->win_ctime[1];
  10398. + in->win_mtime[1] = oh->win_mtime[1];
  10399. +#else
  10400. + in->yst_uid = oh->yst_uid;
  10401. + in->yst_gid = oh->yst_gid;
  10402. + in->yst_atime = oh->yst_atime;
  10403. + in->yst_mtime = oh->yst_mtime;
  10404. + in->yst_ctime = oh->yst_ctime;
  10405. + in->yst_rdev = oh->yst_rdev;
  10406. +#endif
  10407. +
  10408. + if (oh->shadowsObject > 0)
  10409. + yaffs_HandleShadowedObject(dev,
  10410. + oh->
  10411. + shadowsObject,
  10412. + 1);
  10413. +
  10414. +
  10415. + yaffs_SetObjectName(in, oh->name);
  10416. + parent =
  10417. + yaffs_FindOrCreateObjectByNumber
  10418. + (dev, oh->parentObjectId,
  10419. + YAFFS_OBJECT_TYPE_DIRECTORY);
  10420. +
  10421. + fileSize = oh->fileSize;
  10422. + isShrink = oh->isShrink;
  10423. + equivalentObjectId = oh->equivalentObjectId;
  10424. +
  10425. + } else {
  10426. + in->variantType = tags.extraObjectType;
  10427. + parent =
  10428. + yaffs_FindOrCreateObjectByNumber
  10429. + (dev, tags.extraParentObjectId,
  10430. + YAFFS_OBJECT_TYPE_DIRECTORY);
  10431. + fileSize = tags.extraFileLength;
  10432. + isShrink = tags.extraIsShrinkHeader;
  10433. + equivalentObjectId = tags.extraEquivalentObjectId;
  10434. + in->lazyLoaded = 1;
  10435. +
  10436. + }
  10437. + in->dirty = 0;
  10438. +
  10439. + if (!parent)
  10440. + alloc_failed = 1;
  10441. +
  10442. + /* directory stuff...
  10443. + * hook up to parent
  10444. + */
  10445. +
  10446. + if (parent && parent->variantType ==
  10447. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  10448. + /* Set up as a directory */
  10449. + parent->variantType =
  10450. + YAFFS_OBJECT_TYPE_DIRECTORY;
  10451. + YINIT_LIST_HEAD(&parent->variant.
  10452. + directoryVariant.
  10453. + children);
  10454. + } else if (!parent || parent->variantType !=
  10455. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  10456. + /* Hoosterman, another problem....
  10457. + * We're trying to use a non-directory as a directory
  10458. + */
  10459. +
  10460. + T(YAFFS_TRACE_ERROR,
  10461. + (TSTR
  10462. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  10463. + TENDSTR)));
  10464. + parent = dev->lostNFoundDir;
  10465. + }
  10466. +
  10467. + yaffs_AddObjectToDirectory(parent, in);
  10468. +
  10469. + itsUnlinked = (parent == dev->deletedDir) ||
  10470. + (parent == dev->unlinkedDir);
  10471. +
  10472. + if (isShrink) {
  10473. + /* Mark the block as having a shrinkHeader */
  10474. + bi->hasShrinkHeader = 1;
  10475. + }
  10476. +
  10477. + /* Note re hardlinks.
  10478. + * Since we might scan a hardlink before its equivalent object is scanned
  10479. + * we put them all in a list.
  10480. + * After scanning is complete, we should have all the objects, so we run
  10481. + * through this list and fix up all the chains.
  10482. + */
  10483. +
  10484. + switch (in->variantType) {
  10485. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  10486. + /* Todo got a problem */
  10487. + break;
  10488. + case YAFFS_OBJECT_TYPE_FILE:
  10489. +
  10490. + if (in->variant.fileVariant.
  10491. + scannedFileSize < fileSize) {
  10492. + /* This covers the case where the file size is greater
  10493. + * than where the data is
  10494. + * This will happen if the file is resized to be larger
  10495. + * than its current data extents.
  10496. + */
  10497. + in->variant.fileVariant.fileSize = fileSize;
  10498. + in->variant.fileVariant.scannedFileSize =
  10499. + in->variant.fileVariant.fileSize;
  10500. + }
  10501. +
  10502. + if (isShrink &&
  10503. + in->variant.fileVariant.shrinkSize > fileSize) {
  10504. + in->variant.fileVariant.shrinkSize = fileSize;
  10505. + }
  10506. +
  10507. + break;
  10508. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10509. + if (!itsUnlinked) {
  10510. + in->variant.hardLinkVariant.equivalentObjectId =
  10511. + equivalentObjectId;
  10512. + in->hardLinks.next =
  10513. + (struct ylist_head *) hardList;
  10514. + hardList = in;
  10515. + }
  10516. + break;
  10517. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10518. + /* Do nothing */
  10519. + break;
  10520. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10521. + /* Do nothing */
  10522. + break;
  10523. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10524. + if (oh) {
  10525. + in->variant.symLinkVariant.alias =
  10526. + yaffs_CloneString(oh->alias);
  10527. + if (!in->variant.symLinkVariant.alias)
  10528. + alloc_failed = 1;
  10529. + }
  10530. + break;
  10531. + }
  10532. +
  10533. + }
  10534. +
  10535. + }
  10536. +
  10537. + } /* End of scanning for each chunk */
  10538. +
  10539. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  10540. + /* If we got this far while scanning, then the block is fully allocated. */
  10541. + state = YAFFS_BLOCK_STATE_FULL;
  10542. + }
  10543. +
  10544. + bi->blockState = state;
  10545. +
  10546. + /* Now let's see if it was dirty */
  10547. + if (bi->pagesInUse == 0 &&
  10548. + !bi->hasShrinkHeader &&
  10549. + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  10550. + yaffs_BlockBecameDirty(dev, blk);
  10551. + }
  10552. +
  10553. + }
  10554. +
  10555. + if (altBlockIndex)
  10556. + YFREE_ALT(blockIndex);
  10557. + else
  10558. + YFREE(blockIndex);
  10559. +
  10560. + /* Ok, we've done all the scanning.
  10561. + * Fix up the hard link chains.
  10562. + * We should now have scanned all the objects, now it's time to add these
  10563. + * hardlinks.
  10564. + */
  10565. + yaffs_HardlinkFixup(dev, hardList);
  10566. +
  10567. +
  10568. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  10569. +
  10570. + if (alloc_failed)
  10571. + return YAFFS_FAIL;
  10572. +
  10573. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
  10574. +
  10575. + return YAFFS_OK;
  10576. +}
  10577. +
  10578. +/*------------------------------ Directory Functions ----------------------------- */
  10579. +
  10580. +static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
  10581. +{
  10582. + struct ylist_head *lh;
  10583. + yaffs_Object *listObj;
  10584. +
  10585. + int count = 0;
  10586. +
  10587. + if (!obj) {
  10588. + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
  10589. + YBUG();
  10590. + return;
  10591. + }
  10592. +
  10593. + if (yaffs_SkipVerification(obj->myDev))
  10594. + return;
  10595. +
  10596. + if (!obj->parent) {
  10597. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
  10598. + YBUG();
  10599. + return;
  10600. + }
  10601. +
  10602. + if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10603. + T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
  10604. + YBUG();
  10605. + }
  10606. +
  10607. + /* Iterate through the objects in each hash entry */
  10608. +
  10609. + ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
  10610. + if (lh) {
  10611. + listObj = ylist_entry(lh, yaffs_Object, siblings);
  10612. + yaffs_VerifyObject(listObj);
  10613. + if (obj == listObj)
  10614. + count++;
  10615. + }
  10616. + }
  10617. +
  10618. + if (count != 1) {
  10619. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
  10620. + YBUG();
  10621. + }
  10622. +}
  10623. +
  10624. +static void yaffs_VerifyDirectory(yaffs_Object *directory)
  10625. +{
  10626. + struct ylist_head *lh;
  10627. + yaffs_Object *listObj;
  10628. +
  10629. + if (!directory) {
  10630. + YBUG();
  10631. + return;
  10632. + }
  10633. +
  10634. + if (yaffs_SkipFullVerification(directory->myDev))
  10635. + return;
  10636. +
  10637. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10638. + T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
  10639. + YBUG();
  10640. + }
  10641. +
  10642. + /* Iterate through the objects in each hash entry */
  10643. +
  10644. + ylist_for_each(lh, &directory->variant.directoryVariant.children) {
  10645. + if (lh) {
  10646. + listObj = ylist_entry(lh, yaffs_Object, siblings);
  10647. + if (listObj->parent != directory) {
  10648. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
  10649. + YBUG();
  10650. + }
  10651. + yaffs_VerifyObjectInDirectory(listObj);
  10652. + }
  10653. + }
  10654. +}
  10655. +
  10656. +
  10657. +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
  10658. +{
  10659. + yaffs_Device *dev = obj->myDev;
  10660. + yaffs_Object *parent;
  10661. +
  10662. + yaffs_VerifyObjectInDirectory(obj);
  10663. + parent = obj->parent;
  10664. +
  10665. + yaffs_VerifyDirectory(parent);
  10666. +
  10667. + if (dev && dev->removeObjectCallback)
  10668. + dev->removeObjectCallback(obj);
  10669. +
  10670. +
  10671. + ylist_del_init(&obj->siblings);
  10672. + obj->parent = NULL;
  10673. +
  10674. + yaffs_VerifyDirectory(parent);
  10675. +}
  10676. +
  10677. +
  10678. +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
  10679. + yaffs_Object *obj)
  10680. +{
  10681. + if (!directory) {
  10682. + T(YAFFS_TRACE_ALWAYS,
  10683. + (TSTR
  10684. + ("tragedy: Trying to add an object to a null pointer directory"
  10685. + TENDSTR)));
  10686. + YBUG();
  10687. + return;
  10688. + }
  10689. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10690. + T(YAFFS_TRACE_ALWAYS,
  10691. + (TSTR
  10692. + ("tragedy: Trying to add an object to a non-directory"
  10693. + TENDSTR)));
  10694. + YBUG();
  10695. + }
  10696. +
  10697. + if (obj->siblings.prev == NULL) {
  10698. + /* Not initialised */
  10699. + YBUG();
  10700. + }
  10701. +
  10702. +
  10703. + yaffs_VerifyDirectory(directory);
  10704. +
  10705. + yaffs_RemoveObjectFromDirectory(obj);
  10706. +
  10707. +
  10708. + /* Now add it */
  10709. + ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
  10710. + obj->parent = directory;
  10711. +
  10712. + if (directory == obj->myDev->unlinkedDir
  10713. + || directory == obj->myDev->deletedDir) {
  10714. + obj->unlinked = 1;
  10715. + obj->myDev->nUnlinkedFiles++;
  10716. + obj->renameAllowed = 0;
  10717. + }
  10718. +
  10719. + yaffs_VerifyDirectory(directory);
  10720. + yaffs_VerifyObjectInDirectory(obj);
  10721. +}
  10722. +
  10723. +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
  10724. + const YCHAR *name)
  10725. +{
  10726. + int sum;
  10727. +
  10728. + struct ylist_head *i;
  10729. + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  10730. +
  10731. + yaffs_Object *l;
  10732. +
  10733. + if (!name)
  10734. + return NULL;
  10735. +
  10736. + if (!directory) {
  10737. + T(YAFFS_TRACE_ALWAYS,
  10738. + (TSTR
  10739. + ("tragedy: yaffs_FindObjectByName: null pointer directory"
  10740. + TENDSTR)));
  10741. + YBUG();
  10742. + return NULL;
  10743. + }
  10744. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10745. + T(YAFFS_TRACE_ALWAYS,
  10746. + (TSTR
  10747. + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  10748. + YBUG();
  10749. + }
  10750. +
  10751. + sum = yaffs_CalcNameSum(name);
  10752. +
  10753. + ylist_for_each(i, &directory->variant.directoryVariant.children) {
  10754. + if (i) {
  10755. + l = ylist_entry(i, yaffs_Object, siblings);
  10756. +
  10757. + if (l->parent != directory)
  10758. + YBUG();
  10759. +
  10760. + yaffs_CheckObjectDetailsLoaded(l);
  10761. +
  10762. + /* Special case for lost-n-found */
  10763. + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  10764. + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
  10765. + return l;
  10766. + } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
  10767. + /* LostnFound chunk called Objxxx
  10768. + * Do a real check
  10769. + */
  10770. + yaffs_GetObjectName(l, buffer,
  10771. + YAFFS_MAX_NAME_LENGTH);
  10772. + if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
  10773. + return l;
  10774. + }
  10775. + }
  10776. + }
  10777. +
  10778. + return NULL;
  10779. +}
  10780. +
  10781. +
  10782. +#if 0
  10783. +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
  10784. + int (*fn) (yaffs_Object *))
  10785. +{
  10786. + struct ylist_head *i;
  10787. + yaffs_Object *l;
  10788. +
  10789. + if (!theDir) {
  10790. + T(YAFFS_TRACE_ALWAYS,
  10791. + (TSTR
  10792. + ("tragedy: yaffs_FindObjectByName: null pointer directory"
  10793. + TENDSTR)));
  10794. + YBUG();
  10795. + return YAFFS_FAIL;
  10796. + }
  10797. + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10798. + T(YAFFS_TRACE_ALWAYS,
  10799. + (TSTR
  10800. + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  10801. + YBUG();
  10802. + return YAFFS_FAIL;
  10803. + }
  10804. +
  10805. + ylist_for_each(i, &theDir->variant.directoryVariant.children) {
  10806. + if (i) {
  10807. + l = ylist_entry(i, yaffs_Object, siblings);
  10808. + if (l && !fn(l))
  10809. + return YAFFS_FAIL;
  10810. + }
  10811. + }
  10812. +
  10813. + return YAFFS_OK;
  10814. +
  10815. +}
  10816. +#endif
  10817. +
  10818. +/* GetEquivalentObject dereferences any hard links to get to the
  10819. + * actual object.
  10820. + */
  10821. +
  10822. +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
  10823. +{
  10824. + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  10825. + /* We want the object id of the equivalent object, not this one */
  10826. + obj = obj->variant.hardLinkVariant.equivalentObject;
  10827. + yaffs_CheckObjectDetailsLoaded(obj);
  10828. + }
  10829. + return obj;
  10830. +}
  10831. +
  10832. +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
  10833. +{
  10834. + memset(name, 0, buffSize * sizeof(YCHAR));
  10835. +
  10836. + yaffs_CheckObjectDetailsLoaded(obj);
  10837. +
  10838. + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  10839. + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
  10840. + } else if (obj->hdrChunk <= 0) {
  10841. + YCHAR locName[20];
  10842. + YCHAR numString[20];
  10843. + YCHAR *x = &numString[19];
  10844. + unsigned v = obj->objectId;
  10845. + numString[19] = 0;
  10846. + while (v > 0) {
  10847. + x--;
  10848. + *x = '0' + (v % 10);
  10849. + v /= 10;
  10850. + }
  10851. + /* make up a name */
  10852. + yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
  10853. + yaffs_strcat(locName, x);
  10854. + yaffs_strncpy(name, locName, buffSize - 1);
  10855. +
  10856. + }
  10857. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  10858. + else if (obj->shortName[0])
  10859. + yaffs_strcpy(name, obj->shortName);
  10860. +#endif
  10861. + else {
  10862. + int result;
  10863. + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
  10864. +
  10865. + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
  10866. +
  10867. + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
  10868. +
  10869. + if (obj->hdrChunk > 0) {
  10870. + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
  10871. + obj->hdrChunk, buffer,
  10872. + NULL);
  10873. + }
  10874. + yaffs_strncpy(name, oh->name, buffSize - 1);
  10875. +
  10876. + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
  10877. + }
  10878. +
  10879. + return yaffs_strlen(name);
  10880. +}
  10881. +
  10882. +int yaffs_GetObjectFileLength(yaffs_Object *obj)
  10883. +{
  10884. + /* Dereference any hard linking */
  10885. + obj = yaffs_GetEquivalentObject(obj);
  10886. +
  10887. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  10888. + return obj->variant.fileVariant.fileSize;
  10889. + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
  10890. + return yaffs_strlen(obj->variant.symLinkVariant.alias);
  10891. + else {
  10892. + /* Only a directory should drop through to here */
  10893. + return obj->myDev->nDataBytesPerChunk;
  10894. + }
  10895. +}
  10896. +
  10897. +int yaffs_GetObjectLinkCount(yaffs_Object *obj)
  10898. +{
  10899. + int count = 0;
  10900. + struct ylist_head *i;
  10901. +
  10902. + if (!obj->unlinked)
  10903. + count++; /* the object itself */
  10904. +
  10905. + ylist_for_each(i, &obj->hardLinks)
  10906. + count++; /* add the hard links; */
  10907. +
  10908. + return count;
  10909. +}
  10910. +
  10911. +int yaffs_GetObjectInode(yaffs_Object *obj)
  10912. +{
  10913. + obj = yaffs_GetEquivalentObject(obj);
  10914. +
  10915. + return obj->objectId;
  10916. +}
  10917. +
  10918. +unsigned yaffs_GetObjectType(yaffs_Object *obj)
  10919. +{
  10920. + obj = yaffs_GetEquivalentObject(obj);
  10921. +
  10922. + switch (obj->variantType) {
  10923. + case YAFFS_OBJECT_TYPE_FILE:
  10924. + return DT_REG;
  10925. + break;
  10926. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10927. + return DT_DIR;
  10928. + break;
  10929. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10930. + return DT_LNK;
  10931. + break;
  10932. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10933. + return DT_REG;
  10934. + break;
  10935. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10936. + if (S_ISFIFO(obj->yst_mode))
  10937. + return DT_FIFO;
  10938. + if (S_ISCHR(obj->yst_mode))
  10939. + return DT_CHR;
  10940. + if (S_ISBLK(obj->yst_mode))
  10941. + return DT_BLK;
  10942. + if (S_ISSOCK(obj->yst_mode))
  10943. + return DT_SOCK;
  10944. + default:
  10945. + return DT_REG;
  10946. + break;
  10947. + }
  10948. +}
  10949. +
  10950. +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
  10951. +{
  10952. + obj = yaffs_GetEquivalentObject(obj);
  10953. + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
  10954. + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
  10955. + else
  10956. + return yaffs_CloneString(_Y(""));
  10957. +}
  10958. +
  10959. +#ifndef CONFIG_YAFFS_WINCE
  10960. +
  10961. +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
  10962. +{
  10963. + unsigned int valid = attr->ia_valid;
  10964. +
  10965. + if (valid & ATTR_MODE)
  10966. + obj->yst_mode = attr->ia_mode;
  10967. + if (valid & ATTR_UID)
  10968. + obj->yst_uid = attr->ia_uid;
  10969. + if (valid & ATTR_GID)
  10970. + obj->yst_gid = attr->ia_gid;
  10971. +
  10972. + if (valid & ATTR_ATIME)
  10973. + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  10974. + if (valid & ATTR_CTIME)
  10975. + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  10976. + if (valid & ATTR_MTIME)
  10977. + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  10978. +
  10979. + if (valid & ATTR_SIZE)
  10980. + yaffs_ResizeFile(obj, attr->ia_size);
  10981. +
  10982. + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
  10983. +
  10984. + return YAFFS_OK;
  10985. +
  10986. +}
  10987. +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
  10988. +{
  10989. + unsigned int valid = 0;
  10990. +
  10991. + attr->ia_mode = obj->yst_mode;
  10992. + valid |= ATTR_MODE;
  10993. + attr->ia_uid = obj->yst_uid;
  10994. + valid |= ATTR_UID;
  10995. + attr->ia_gid = obj->yst_gid;
  10996. + valid |= ATTR_GID;
  10997. +
  10998. + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  10999. + valid |= ATTR_ATIME;
  11000. + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  11001. + valid |= ATTR_CTIME;
  11002. + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  11003. + valid |= ATTR_MTIME;
  11004. +
  11005. + attr->ia_size = yaffs_GetFileSize(obj);
  11006. + valid |= ATTR_SIZE;
  11007. +
  11008. + attr->ia_valid = valid;
  11009. +
  11010. + return YAFFS_OK;
  11011. +}
  11012. +
  11013. +#endif
  11014. +
  11015. +#if 0
  11016. +int yaffs_DumpObject(yaffs_Object *obj)
  11017. +{
  11018. + YCHAR name[257];
  11019. +
  11020. + yaffs_GetObjectName(obj, name, 256);
  11021. +
  11022. + T(YAFFS_TRACE_ALWAYS,
  11023. + (TSTR
  11024. + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
  11025. + " chunk %d type %d size %d\n"
  11026. + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
  11027. + obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
  11028. + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
  11029. +
  11030. + return YAFFS_OK;
  11031. +}
  11032. +#endif
  11033. +
  11034. +/*---------------------------- Initialisation code -------------------------------------- */
  11035. +
  11036. +static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
  11037. +{
  11038. +
  11039. + /* Common functions, gotta have */
  11040. + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
  11041. + return 0;
  11042. +
  11043. +#ifdef CONFIG_YAFFS_YAFFS2
  11044. +
  11045. + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  11046. + if (dev->writeChunkWithTagsToNAND &&
  11047. + dev->readChunkWithTagsFromNAND &&
  11048. + !dev->writeChunkToNAND &&
  11049. + !dev->readChunkFromNAND &&
  11050. + dev->markNANDBlockBad && dev->queryNANDBlock)
  11051. + return 1;
  11052. +#endif
  11053. +
  11054. + /* Can use the "spare" style interface for yaffs1 */
  11055. + if (!dev->isYaffs2 &&
  11056. + !dev->writeChunkWithTagsToNAND &&
  11057. + !dev->readChunkWithTagsFromNAND &&
  11058. + dev->writeChunkToNAND &&
  11059. + dev->readChunkFromNAND &&
  11060. + !dev->markNANDBlockBad && !dev->queryNANDBlock)
  11061. + return 1;
  11062. +
  11063. + return 0; /* bad */
  11064. +}
  11065. +
  11066. +
  11067. +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
  11068. +{
  11069. + /* Initialise the unlinked, deleted, root and lost and found directories */
  11070. +
  11071. + dev->lostNFoundDir = dev->rootDir = NULL;
  11072. + dev->unlinkedDir = dev->deletedDir = NULL;
  11073. +
  11074. + dev->unlinkedDir =
  11075. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  11076. +
  11077. + dev->deletedDir =
  11078. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  11079. +
  11080. + dev->rootDir =
  11081. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
  11082. + YAFFS_ROOT_MODE | S_IFDIR);
  11083. + dev->lostNFoundDir =
  11084. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
  11085. + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  11086. +
  11087. + if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
  11088. + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
  11089. + return YAFFS_OK;
  11090. + }
  11091. +
  11092. + return YAFFS_FAIL;
  11093. +}
  11094. +
  11095. +int yaffs_GutsInitialise(yaffs_Device *dev)
  11096. +{
  11097. + int init_failed = 0;
  11098. + unsigned x;
  11099. + int bits;
  11100. +
  11101. + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
  11102. +
  11103. + /* Check stuff that must be set */
  11104. +
  11105. + if (!dev) {
  11106. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
  11107. + return YAFFS_FAIL;
  11108. + }
  11109. +
  11110. + dev->internalStartBlock = dev->startBlock;
  11111. + dev->internalEndBlock = dev->endBlock;
  11112. + dev->blockOffset = 0;
  11113. + dev->chunkOffset = 0;
  11114. + dev->nFreeChunks = 0;
  11115. +
  11116. + dev->gcBlock = -1;
  11117. +
  11118. + if (dev->startBlock == 0) {
  11119. + dev->internalStartBlock = dev->startBlock + 1;
  11120. + dev->internalEndBlock = dev->endBlock + 1;
  11121. + dev->blockOffset = 1;
  11122. + dev->chunkOffset = dev->nChunksPerBlock;
  11123. + }
  11124. +
  11125. + /* Check geometry parameters. */
  11126. +
  11127. + if ((!dev->inbandTags && dev->isYaffs2 && dev->totalBytesPerChunk < 1024) ||
  11128. + (!dev->isYaffs2 && dev->totalBytesPerChunk < 512) ||
  11129. + (dev->inbandTags && !dev->isYaffs2) ||
  11130. + dev->nChunksPerBlock < 2 ||
  11131. + dev->nReservedBlocks < 2 ||
  11132. + dev->internalStartBlock <= 0 ||
  11133. + dev->internalEndBlock <= 0 ||
  11134. + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2)) { /* otherwise it is too small */
  11135. + T(YAFFS_TRACE_ALWAYS,
  11136. + (TSTR
  11137. + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
  11138. + TENDSTR), dev->totalBytesPerChunk, dev->isYaffs2 ? "2" : "", dev->inbandTags));
  11139. + return YAFFS_FAIL;
  11140. + }
  11141. +
  11142. + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
  11143. + T(YAFFS_TRACE_ALWAYS,
  11144. + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
  11145. + return YAFFS_FAIL;
  11146. + }
  11147. +
  11148. + /* Sort out space for inband tags, if required */
  11149. + if (dev->inbandTags)
  11150. + dev->nDataBytesPerChunk = dev->totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
  11151. + else
  11152. + dev->nDataBytesPerChunk = dev->totalBytesPerChunk;
  11153. +
  11154. + /* Got the right mix of functions? */
  11155. + if (!yaffs_CheckDevFunctions(dev)) {
  11156. + /* Function missing */
  11157. + T(YAFFS_TRACE_ALWAYS,
  11158. + (TSTR
  11159. + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
  11160. +
  11161. + return YAFFS_FAIL;
  11162. + }
  11163. +
  11164. + /* This is really a compilation check. */
  11165. + if (!yaffs_CheckStructures()) {
  11166. + T(YAFFS_TRACE_ALWAYS,
  11167. + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
  11168. + return YAFFS_FAIL;
  11169. + }
  11170. +
  11171. + if (dev->isMounted) {
  11172. + T(YAFFS_TRACE_ALWAYS,
  11173. + (TSTR("yaffs: device already mounted\n" TENDSTR)));
  11174. + return YAFFS_FAIL;
  11175. + }
  11176. +
  11177. + /* Finished with most checks. One or two more checks happen later on too. */
  11178. +
  11179. + dev->isMounted = 1;
  11180. +
  11181. + /* OK now calculate a few things for the device */
  11182. +
  11183. + /*
  11184. + * Calculate all the chunk size manipulation numbers:
  11185. + */
  11186. + x = dev->nDataBytesPerChunk;
  11187. + /* We always use dev->chunkShift and dev->chunkDiv */
  11188. + dev->chunkShift = Shifts(x);
  11189. + x >>= dev->chunkShift;
  11190. + dev->chunkDiv = x;
  11191. + /* We only use chunk mask if chunkDiv is 1 */
  11192. + dev->chunkMask = (1<<dev->chunkShift) - 1;
  11193. +
  11194. + /*
  11195. + * Calculate chunkGroupBits.
  11196. + * We need to find the next power of 2 > than internalEndBlock
  11197. + */
  11198. +
  11199. + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
  11200. +
  11201. + bits = ShiftsGE(x);
  11202. +
  11203. + /* Set up tnode width if wide tnodes are enabled. */
  11204. + if (!dev->wideTnodesDisabled) {
  11205. + /* bits must be even so that we end up with 32-bit words */
  11206. + if (bits & 1)
  11207. + bits++;
  11208. + if (bits < 16)
  11209. + dev->tnodeWidth = 16;
  11210. + else
  11211. + dev->tnodeWidth = bits;
  11212. + } else
  11213. + dev->tnodeWidth = 16;
  11214. +
  11215. + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
  11216. +
  11217. + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  11218. + * so if the bitwidth of the
  11219. + * chunk range we're using is greater than 16 we need
  11220. + * to figure out chunk shift and chunkGroupSize
  11221. + */
  11222. +
  11223. + if (bits <= dev->tnodeWidth)
  11224. + dev->chunkGroupBits = 0;
  11225. + else
  11226. + dev->chunkGroupBits = bits - dev->tnodeWidth;
  11227. +
  11228. +
  11229. + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
  11230. +
  11231. + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
  11232. + /* We have a problem because the soft delete won't work if
  11233. + * the chunk group size > chunks per block.
  11234. + * This can be remedied by using larger "virtual blocks".
  11235. + */
  11236. + T(YAFFS_TRACE_ALWAYS,
  11237. + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
  11238. +
  11239. + return YAFFS_FAIL;
  11240. + }
  11241. +
  11242. + /* OK, we've finished verifying the device, lets continue with initialisation */
  11243. +
  11244. + /* More device initialisation */
  11245. + dev->garbageCollections = 0;
  11246. + dev->passiveGarbageCollections = 0;
  11247. + dev->currentDirtyChecker = 0;
  11248. + dev->bufferedBlock = -1;
  11249. + dev->doingBufferedBlockRewrite = 0;
  11250. + dev->nDeletedFiles = 0;
  11251. + dev->nBackgroundDeletions = 0;
  11252. + dev->nUnlinkedFiles = 0;
  11253. + dev->eccFixed = 0;
  11254. + dev->eccUnfixed = 0;
  11255. + dev->tagsEccFixed = 0;
  11256. + dev->tagsEccUnfixed = 0;
  11257. + dev->nErasureFailures = 0;
  11258. + dev->nErasedBlocks = 0;
  11259. + dev->isDoingGC = 0;
  11260. + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
  11261. +
  11262. + /* Initialise temporary buffers and caches. */
  11263. + if (!yaffs_InitialiseTempBuffers(dev))
  11264. + init_failed = 1;
  11265. +
  11266. + dev->srCache = NULL;
  11267. + dev->gcCleanupList = NULL;
  11268. +
  11269. +
  11270. + if (!init_failed &&
  11271. + dev->nShortOpCaches > 0) {
  11272. + int i;
  11273. + void *buf;
  11274. + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
  11275. +
  11276. + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
  11277. + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
  11278. +
  11279. + dev->srCache = YMALLOC(srCacheBytes);
  11280. +
  11281. + buf = (__u8 *) dev->srCache;
  11282. +
  11283. + if (dev->srCache)
  11284. + memset(dev->srCache, 0, srCacheBytes);
  11285. +
  11286. + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
  11287. + dev->srCache[i].object = NULL;
  11288. + dev->srCache[i].lastUse = 0;
  11289. + dev->srCache[i].dirty = 0;
  11290. + dev->srCache[i].data = buf = YMALLOC_DMA(dev->totalBytesPerChunk);
  11291. + }
  11292. + if (!buf)
  11293. + init_failed = 1;
  11294. +
  11295. + dev->srLastUse = 0;
  11296. + }
  11297. +
  11298. + dev->cacheHits = 0;
  11299. +
  11300. + if (!init_failed) {
  11301. + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
  11302. + if (!dev->gcCleanupList)
  11303. + init_failed = 1;
  11304. + }
  11305. +
  11306. + if (dev->isYaffs2)
  11307. + dev->useHeaderFileSize = 1;
  11308. +
  11309. + if (!init_failed && !yaffs_InitialiseBlocks(dev))
  11310. + init_failed = 1;
  11311. +
  11312. + yaffs_InitialiseTnodes(dev);
  11313. + yaffs_InitialiseObjects(dev);
  11314. +
  11315. + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
  11316. + init_failed = 1;
  11317. +
  11318. +
  11319. + if (!init_failed) {
  11320. + /* Now scan the flash. */
  11321. + if (dev->isYaffs2) {
  11322. + if (yaffs_CheckpointRestore(dev)) {
  11323. + yaffs_CheckObjectDetailsLoaded(dev->rootDir);
  11324. + T(YAFFS_TRACE_ALWAYS,
  11325. + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
  11326. + } else {
  11327. +
  11328. + /* Clean up the mess caused by an aborted checkpoint load
  11329. + * and scan backwards.
  11330. + */
  11331. + yaffs_DeinitialiseBlocks(dev);
  11332. + yaffs_DeinitialiseTnodes(dev);
  11333. + yaffs_DeinitialiseObjects(dev);
  11334. +
  11335. +
  11336. + dev->nErasedBlocks = 0;
  11337. + dev->nFreeChunks = 0;
  11338. + dev->allocationBlock = -1;
  11339. + dev->allocationPage = -1;
  11340. + dev->nDeletedFiles = 0;
  11341. + dev->nUnlinkedFiles = 0;
  11342. + dev->nBackgroundDeletions = 0;
  11343. + dev->oldestDirtySequence = 0;
  11344. +
  11345. + if (!init_failed && !yaffs_InitialiseBlocks(dev))
  11346. + init_failed = 1;
  11347. +
  11348. + yaffs_InitialiseTnodes(dev);
  11349. + yaffs_InitialiseObjects(dev);
  11350. +
  11351. + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
  11352. + init_failed = 1;
  11353. +
  11354. + if (!init_failed && !yaffs_ScanBackwards(dev))
  11355. + init_failed = 1;
  11356. + }
  11357. + } else if (!yaffs_Scan(dev))
  11358. + init_failed = 1;
  11359. +
  11360. + yaffs_StripDeletedObjects(dev);
  11361. + }
  11362. +
  11363. + if (init_failed) {
  11364. + /* Clean up the mess */
  11365. + T(YAFFS_TRACE_TRACING,
  11366. + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
  11367. +
  11368. + yaffs_Deinitialise(dev);
  11369. + return YAFFS_FAIL;
  11370. + }
  11371. +
  11372. + /* Zero out stats */
  11373. + dev->nPageReads = 0;
  11374. + dev->nPageWrites = 0;
  11375. + dev->nBlockErasures = 0;
  11376. + dev->nGCCopies = 0;
  11377. + dev->nRetriedWrites = 0;
  11378. +
  11379. + dev->nRetiredBlocks = 0;
  11380. +
  11381. + yaffs_VerifyFreeChunks(dev);
  11382. + yaffs_VerifyBlocks(dev);
  11383. +
  11384. +
  11385. + T(YAFFS_TRACE_TRACING,
  11386. + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
  11387. + return YAFFS_OK;
  11388. +
  11389. +}
  11390. +
  11391. +void yaffs_Deinitialise(yaffs_Device *dev)
  11392. +{
  11393. + if (dev->isMounted) {
  11394. + int i;
  11395. +
  11396. + yaffs_DeinitialiseBlocks(dev);
  11397. + yaffs_DeinitialiseTnodes(dev);
  11398. + yaffs_DeinitialiseObjects(dev);
  11399. + if (dev->nShortOpCaches > 0 &&
  11400. + dev->srCache) {
  11401. +
  11402. + for (i = 0; i < dev->nShortOpCaches; i++) {
  11403. + if (dev->srCache[i].data)
  11404. + YFREE(dev->srCache[i].data);
  11405. + dev->srCache[i].data = NULL;
  11406. + }
  11407. +
  11408. + YFREE(dev->srCache);
  11409. + dev->srCache = NULL;
  11410. + }
  11411. +
  11412. + YFREE(dev->gcCleanupList);
  11413. +
  11414. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  11415. + YFREE(dev->tempBuffer[i].buffer);
  11416. +
  11417. + dev->isMounted = 0;
  11418. +
  11419. + if (dev->deinitialiseNAND)
  11420. + dev->deinitialiseNAND(dev);
  11421. + }
  11422. +}
  11423. +
  11424. +static int yaffs_CountFreeChunks(yaffs_Device *dev)
  11425. +{
  11426. + int nFree;
  11427. + int b;
  11428. +
  11429. + yaffs_BlockInfo *blk;
  11430. +
  11431. + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
  11432. + b++) {
  11433. + blk = yaffs_GetBlockInfo(dev, b);
  11434. +
  11435. + switch (blk->blockState) {
  11436. + case YAFFS_BLOCK_STATE_EMPTY:
  11437. + case YAFFS_BLOCK_STATE_ALLOCATING:
  11438. + case YAFFS_BLOCK_STATE_COLLECTING:
  11439. + case YAFFS_BLOCK_STATE_FULL:
  11440. + nFree +=
  11441. + (dev->nChunksPerBlock - blk->pagesInUse +
  11442. + blk->softDeletions);
  11443. + break;
  11444. + default:
  11445. + break;
  11446. + }
  11447. + }
  11448. +
  11449. + return nFree;
  11450. +}
  11451. +
  11452. +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
  11453. +{
  11454. + /* This is what we report to the outside world */
  11455. +
  11456. + int nFree;
  11457. + int nDirtyCacheChunks;
  11458. + int blocksForCheckpoint;
  11459. + int i;
  11460. +
  11461. +#if 1
  11462. + nFree = dev->nFreeChunks;
  11463. +#else
  11464. + nFree = yaffs_CountFreeChunks(dev);
  11465. +#endif
  11466. +
  11467. + nFree += dev->nDeletedFiles;
  11468. +
  11469. + /* Now count the number of dirty chunks in the cache and subtract those */
  11470. +
  11471. + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
  11472. + if (dev->srCache[i].dirty)
  11473. + nDirtyCacheChunks++;
  11474. + }
  11475. +
  11476. + nFree -= nDirtyCacheChunks;
  11477. +
  11478. + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
  11479. +
  11480. + /* Now we figure out how much to reserve for the checkpoint and report that... */
  11481. + blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
  11482. + if (blocksForCheckpoint < 0)
  11483. + blocksForCheckpoint = 0;
  11484. +
  11485. + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
  11486. +
  11487. + if (nFree < 0)
  11488. + nFree = 0;
  11489. +
  11490. + return nFree;
  11491. +
  11492. +}
  11493. +
  11494. +static int yaffs_freeVerificationFailures;
  11495. +
  11496. +static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
  11497. +{
  11498. + int counted;
  11499. + int difference;
  11500. +
  11501. + if (yaffs_SkipVerification(dev))
  11502. + return;
  11503. +
  11504. + counted = yaffs_CountFreeChunks(dev);
  11505. +
  11506. + difference = dev->nFreeChunks - counted;
  11507. +
  11508. + if (difference) {
  11509. + T(YAFFS_TRACE_ALWAYS,
  11510. + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
  11511. + dev->nFreeChunks, counted, difference));
  11512. + yaffs_freeVerificationFailures++;
  11513. + }
  11514. +}
  11515. +
  11516. +/*---------------------------------------- YAFFS test code ----------------------*/
  11517. +
  11518. +#define yaffs_CheckStruct(structure, syze, name) \
  11519. + do { \
  11520. + if (sizeof(structure) != syze) { \
  11521. + T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
  11522. + name, syze, sizeof(structure))); \
  11523. + return YAFFS_FAIL; \
  11524. + } \
  11525. + } while (0)
  11526. +
  11527. +static int yaffs_CheckStructures(void)
  11528. +{
  11529. +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
  11530. +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
  11531. +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
  11532. +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
  11533. + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode");
  11534. +#endif
  11535. +#ifndef CONFIG_YAFFS_WINCE
  11536. + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");
  11537. +#endif
  11538. + return YAFFS_OK;
  11539. +}
  11540. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_guts.h linux-2.6.34/fs/yaffs2/yaffs_guts.h
  11541. --- linux-2.6.34.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  11542. +++ linux-2.6.34/fs/yaffs2/yaffs_guts.h 2010-05-20 20:16:22.301615823 +0200
  11543. @@ -0,0 +1,904 @@
  11544. +/*
  11545. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  11546. + *
  11547. + * Copyright (C) 2002-2007 Aleph One Ltd.
  11548. + * for Toby Churchill Ltd and Brightstar Engineering
  11549. + *
  11550. + * Created by Charles Manning <charles@aleph1.co.uk>
  11551. + *
  11552. + * This program is free software; you can redistribute it and/or modify
  11553. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  11554. + * published by the Free Software Foundation.
  11555. + *
  11556. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  11557. + */
  11558. +
  11559. +#ifndef __YAFFS_GUTS_H__
  11560. +#define __YAFFS_GUTS_H__
  11561. +
  11562. +#include "devextras.h"
  11563. +#include "yportenv.h"
  11564. +
  11565. +#define YAFFS_OK 1
  11566. +#define YAFFS_FAIL 0
  11567. +
  11568. +/* Give us a Y=0x59,
  11569. + * Give us an A=0x41,
  11570. + * Give us an FF=0xFF
  11571. + * Give us an S=0x53
  11572. + * And what have we got...
  11573. + */
  11574. +#define YAFFS_MAGIC 0x5941FF53
  11575. +
  11576. +#define YAFFS_NTNODES_LEVEL0 16
  11577. +#define YAFFS_TNODES_LEVEL0_BITS 4
  11578. +#define YAFFS_TNODES_LEVEL0_MASK 0xf
  11579. +
  11580. +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  11581. +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  11582. +#define YAFFS_TNODES_INTERNAL_MASK 0x7
  11583. +#define YAFFS_TNODES_MAX_LEVEL 6
  11584. +
  11585. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  11586. +#define YAFFS_BYTES_PER_SPARE 16
  11587. +#define YAFFS_BYTES_PER_CHUNK 512
  11588. +#define YAFFS_CHUNK_SIZE_SHIFT 9
  11589. +#define YAFFS_CHUNKS_PER_BLOCK 32
  11590. +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  11591. +#endif
  11592. +
  11593. +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  11594. +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  11595. +
  11596. +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
  11597. +
  11598. +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
  11599. +
  11600. +#define YAFFS_ALLOCATION_NOBJECTS 100
  11601. +#define YAFFS_ALLOCATION_NTNODES 100
  11602. +#define YAFFS_ALLOCATION_NLINKS 100
  11603. +
  11604. +#define YAFFS_NOBJECT_BUCKETS 256
  11605. +
  11606. +
  11607. +#define YAFFS_OBJECT_SPACE 0x40000
  11608. +
  11609. +#define YAFFS_CHECKPOINT_VERSION 3
  11610. +
  11611. +#ifdef CONFIG_YAFFS_UNICODE
  11612. +#define YAFFS_MAX_NAME_LENGTH 127
  11613. +#define YAFFS_MAX_ALIAS_LENGTH 79
  11614. +#else
  11615. +#define YAFFS_MAX_NAME_LENGTH 255
  11616. +#define YAFFS_MAX_ALIAS_LENGTH 159
  11617. +#endif
  11618. +
  11619. +#define YAFFS_SHORT_NAME_LENGTH 15
  11620. +
  11621. +/* Some special object ids for pseudo objects */
  11622. +#define YAFFS_OBJECTID_ROOT 1
  11623. +#define YAFFS_OBJECTID_LOSTNFOUND 2
  11624. +#define YAFFS_OBJECTID_UNLINKED 3
  11625. +#define YAFFS_OBJECTID_DELETED 4
  11626. +
  11627. +/* Sseudo object ids for checkpointing */
  11628. +#define YAFFS_OBJECTID_SB_HEADER 0x10
  11629. +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  11630. +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  11631. +
  11632. +/* */
  11633. +
  11634. +#define YAFFS_MAX_SHORT_OP_CACHES 20
  11635. +
  11636. +#define YAFFS_N_TEMP_BUFFERS 6
  11637. +
  11638. +/* We limit the number attempts at sucessfully saving a chunk of data.
  11639. + * Small-page devices have 32 pages per block; large-page devices have 64.
  11640. + * Default to something in the order of 5 to 10 blocks worth of chunks.
  11641. + */
  11642. +#define YAFFS_WR_ATTEMPTS (5*64)
  11643. +
  11644. +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
  11645. + * The range is limited slightly to help distinguish bad numbers from good.
  11646. + * This also allows us to perhaps in the future use special numbers for
  11647. + * special purposes.
  11648. + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
  11649. + * and is a larger number than the lifetime of a 2GB device.
  11650. + */
  11651. +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  11652. +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
  11653. +
  11654. +/* Special sequence number for bad block that failed to be marked bad */
  11655. +#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
  11656. +
  11657. +/* ChunkCache is used for short read/write operations.*/
  11658. +typedef struct {
  11659. + struct yaffs_ObjectStruct *object;
  11660. + int chunkId;
  11661. + int lastUse;
  11662. + int dirty;
  11663. + int nBytes; /* Only valid if the cache is dirty */
  11664. + int locked; /* Can't push out or flush while locked. */
  11665. +#ifdef CONFIG_YAFFS_YAFFS2
  11666. + __u8 *data;
  11667. +#else
  11668. + __u8 data[YAFFS_BYTES_PER_CHUNK];
  11669. +#endif
  11670. +} yaffs_ChunkCache;
  11671. +
  11672. +
  11673. +
  11674. +/* Tags structures in RAM
  11675. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
  11676. + * the structure size will get blown out.
  11677. + */
  11678. +
  11679. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  11680. +typedef struct {
  11681. + unsigned chunkId:20;
  11682. + unsigned serialNumber:2;
  11683. + unsigned byteCountLSB:10;
  11684. + unsigned objectId:18;
  11685. + unsigned ecc:12;
  11686. + unsigned byteCountMSB:2;
  11687. +} yaffs_Tags;
  11688. +
  11689. +typedef union {
  11690. + yaffs_Tags asTags;
  11691. + __u8 asBytes[8];
  11692. +} yaffs_TagsUnion;
  11693. +
  11694. +#endif
  11695. +
  11696. +/* Stuff used for extended tags in YAFFS2 */
  11697. +
  11698. +typedef enum {
  11699. + YAFFS_ECC_RESULT_UNKNOWN,
  11700. + YAFFS_ECC_RESULT_NO_ERROR,
  11701. + YAFFS_ECC_RESULT_FIXED,
  11702. + YAFFS_ECC_RESULT_UNFIXED
  11703. +} yaffs_ECCResult;
  11704. +
  11705. +typedef enum {
  11706. + YAFFS_OBJECT_TYPE_UNKNOWN,
  11707. + YAFFS_OBJECT_TYPE_FILE,
  11708. + YAFFS_OBJECT_TYPE_SYMLINK,
  11709. + YAFFS_OBJECT_TYPE_DIRECTORY,
  11710. + YAFFS_OBJECT_TYPE_HARDLINK,
  11711. + YAFFS_OBJECT_TYPE_SPECIAL
  11712. +} yaffs_ObjectType;
  11713. +
  11714. +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  11715. +
  11716. +typedef struct {
  11717. +
  11718. + unsigned validMarker0;
  11719. + unsigned chunkUsed; /* Status of the chunk: used or unused */
  11720. + unsigned objectId; /* If 0 then this is not part of an object (unused) */
  11721. + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
  11722. + unsigned byteCount; /* Only valid for data chunks */
  11723. +
  11724. + /* The following stuff only has meaning when we read */
  11725. + yaffs_ECCResult eccResult;
  11726. + unsigned blockBad;
  11727. +
  11728. + /* YAFFS 1 stuff */
  11729. + unsigned chunkDeleted; /* The chunk is marked deleted */
  11730. + unsigned serialNumber; /* Yaffs1 2-bit serial number */
  11731. +
  11732. + /* YAFFS2 stuff */
  11733. + unsigned sequenceNumber; /* The sequence number of this block */
  11734. +
  11735. + /* Extra info if this is an object header (YAFFS2 only) */
  11736. +
  11737. + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
  11738. + unsigned extraParentObjectId; /* The parent object */
  11739. + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
  11740. + unsigned extraShadows; /* Does this shadow another object? */
  11741. +
  11742. + yaffs_ObjectType extraObjectType; /* What object type? */
  11743. +
  11744. + unsigned extraFileLength; /* Length if it is a file */
  11745. + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
  11746. +
  11747. + unsigned validMarker1;
  11748. +
  11749. +} yaffs_ExtendedTags;
  11750. +
  11751. +/* Spare structure for YAFFS1 */
  11752. +typedef struct {
  11753. + __u8 tagByte0;
  11754. + __u8 tagByte1;
  11755. + __u8 tagByte2;
  11756. + __u8 tagByte3;
  11757. + __u8 pageStatus; /* set to 0 to delete the chunk */
  11758. + __u8 blockStatus;
  11759. + __u8 tagByte4;
  11760. + __u8 tagByte5;
  11761. + __u8 ecc1[3];
  11762. + __u8 tagByte6;
  11763. + __u8 tagByte7;
  11764. + __u8 ecc2[3];
  11765. +} yaffs_Spare;
  11766. +
  11767. +/*Special structure for passing through to mtd */
  11768. +struct yaffs_NANDSpare {
  11769. + yaffs_Spare spare;
  11770. + int eccres1;
  11771. + int eccres2;
  11772. +};
  11773. +
  11774. +/* Block data in RAM */
  11775. +
  11776. +typedef enum {
  11777. + YAFFS_BLOCK_STATE_UNKNOWN = 0,
  11778. +
  11779. + YAFFS_BLOCK_STATE_SCANNING,
  11780. + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
  11781. + /* The block might have something on it (ie it is allocating or full, perhaps empty)
  11782. + * but it needs to be scanned to determine its true state.
  11783. + * This state is only valid during yaffs_Scan.
  11784. + * NB We tolerate empty because the pre-scanner might be incapable of deciding
  11785. + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
  11786. + */
  11787. +
  11788. + YAFFS_BLOCK_STATE_EMPTY,
  11789. + /* This block is empty */
  11790. +
  11791. + YAFFS_BLOCK_STATE_ALLOCATING,
  11792. + /* This block is partially allocated.
  11793. + * At least one page holds valid data.
  11794. + * This is the one currently being used for page
  11795. + * allocation. Should never be more than one of these
  11796. + */
  11797. +
  11798. + YAFFS_BLOCK_STATE_FULL,
  11799. + /* All the pages in this block have been allocated.
  11800. + */
  11801. +
  11802. + YAFFS_BLOCK_STATE_DIRTY,
  11803. + /* All pages have been allocated and deleted.
  11804. + * Erase me, reuse me.
  11805. + */
  11806. +
  11807. + YAFFS_BLOCK_STATE_CHECKPOINT,
  11808. + /* This block is assigned to holding checkpoint data.
  11809. + */
  11810. +
  11811. + YAFFS_BLOCK_STATE_COLLECTING,
  11812. + /* This block is being garbage collected */
  11813. +
  11814. + YAFFS_BLOCK_STATE_DEAD
  11815. + /* This block has failed and is not in use */
  11816. +} yaffs_BlockState;
  11817. +
  11818. +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  11819. +
  11820. +
  11821. +typedef struct {
  11822. +
  11823. + int softDeletions:10; /* number of soft deleted pages */
  11824. + int pagesInUse:10; /* number of pages in use */
  11825. + unsigned blockState:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
  11826. + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
  11827. + /* and retire the block. */
  11828. + __u32 skipErasedCheck:1; /* If this is set we can skip the erased check on this block */
  11829. + __u32 gcPrioritise:1; /* An ECC check or blank check has failed on this block.
  11830. + It should be prioritised for GC */
  11831. + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
  11832. +
  11833. +#ifdef CONFIG_YAFFS_YAFFS2
  11834. + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
  11835. + __u32 sequenceNumber; /* block sequence number for yaffs2 */
  11836. +#endif
  11837. +
  11838. +} yaffs_BlockInfo;
  11839. +
  11840. +/* -------------------------- Object structure -------------------------------*/
  11841. +/* This is the object structure as stored on NAND */
  11842. +
  11843. +typedef struct {
  11844. + yaffs_ObjectType type;
  11845. +
  11846. + /* Apply to everything */
  11847. + int parentObjectId;
  11848. + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
  11849. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  11850. +
  11851. + /* The following apply to directories, files, symlinks - not hard links */
  11852. + __u32 yst_mode; /* protection */
  11853. +
  11854. +#ifdef CONFIG_YAFFS_WINCE
  11855. + __u32 notForWinCE[5];
  11856. +#else
  11857. + __u32 yst_uid;
  11858. + __u32 yst_gid;
  11859. + __u32 yst_atime;
  11860. + __u32 yst_mtime;
  11861. + __u32 yst_ctime;
  11862. +#endif
  11863. +
  11864. + /* File size applies to files only */
  11865. + int fileSize;
  11866. +
  11867. + /* Equivalent object id applies to hard links only. */
  11868. + int equivalentObjectId;
  11869. +
  11870. + /* Alias is for symlinks only. */
  11871. + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  11872. +
  11873. + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
  11874. +
  11875. +#ifdef CONFIG_YAFFS_WINCE
  11876. + __u32 win_ctime[2];
  11877. + __u32 win_atime[2];
  11878. + __u32 win_mtime[2];
  11879. +#else
  11880. + __u32 roomToGrow[6];
  11881. +
  11882. +#endif
  11883. + __u32 inbandShadowsObject;
  11884. + __u32 inbandIsShrink;
  11885. +
  11886. + __u32 reservedSpace[2];
  11887. + int shadowsObject; /* This object header shadows the specified object if > 0 */
  11888. +
  11889. + /* isShrink applies to object headers written when we shrink the file (ie resize) */
  11890. + __u32 isShrink;
  11891. +
  11892. +} yaffs_ObjectHeader;
  11893. +
  11894. +/*--------------------------- Tnode -------------------------- */
  11895. +
  11896. +union yaffs_Tnode_union {
  11897. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  11898. + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
  11899. +#else
  11900. + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
  11901. +#endif
  11902. +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
  11903. +
  11904. +};
  11905. +
  11906. +typedef union yaffs_Tnode_union yaffs_Tnode;
  11907. +
  11908. +struct yaffs_TnodeList_struct {
  11909. + struct yaffs_TnodeList_struct *next;
  11910. + yaffs_Tnode *tnodes;
  11911. +};
  11912. +
  11913. +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
  11914. +
  11915. +/*------------------------ Object -----------------------------*/
  11916. +/* An object can be one of:
  11917. + * - a directory (no data, has children links
  11918. + * - a regular file (data.... not prunes :->).
  11919. + * - a symlink [symbolic link] (the alias).
  11920. + * - a hard link
  11921. + */
  11922. +
  11923. +typedef struct {
  11924. + __u32 fileSize;
  11925. + __u32 scannedFileSize;
  11926. + __u32 shrinkSize;
  11927. + int topLevel;
  11928. + yaffs_Tnode *top;
  11929. +} yaffs_FileStructure;
  11930. +
  11931. +typedef struct {
  11932. + struct ylist_head children; /* list of child links */
  11933. +} yaffs_DirectoryStructure;
  11934. +
  11935. +typedef struct {
  11936. + YCHAR *alias;
  11937. +} yaffs_SymLinkStructure;
  11938. +
  11939. +typedef struct {
  11940. + struct yaffs_ObjectStruct *equivalentObject;
  11941. + __u32 equivalentObjectId;
  11942. +} yaffs_HardLinkStructure;
  11943. +
  11944. +typedef union {
  11945. + yaffs_FileStructure fileVariant;
  11946. + yaffs_DirectoryStructure directoryVariant;
  11947. + yaffs_SymLinkStructure symLinkVariant;
  11948. + yaffs_HardLinkStructure hardLinkVariant;
  11949. +} yaffs_ObjectVariant;
  11950. +
  11951. +struct yaffs_ObjectStruct {
  11952. + __u8 deleted:1; /* This should only apply to unlinked files. */
  11953. + __u8 softDeleted:1; /* it has also been soft deleted */
  11954. + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
  11955. + __u8 fake:1; /* A fake object has no presence on NAND. */
  11956. + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
  11957. + __u8 unlinkAllowed:1;
  11958. + __u8 dirty:1; /* the object needs to be written to flash */
  11959. + __u8 valid:1; /* When the file system is being loaded up, this
  11960. + * object might be created before the data
  11961. + * is available (ie. file data records appear before the header).
  11962. + */
  11963. + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
  11964. +
  11965. + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
  11966. + * still in the inode cache. Free of object is defered.
  11967. + * until the inode is released.
  11968. + */
  11969. + __u8 beingCreated:1; /* This object is still being created so skip some checks. */
  11970. +
  11971. + __u8 serial; /* serial number of chunk in NAND. Cached here */
  11972. + __u16 sum; /* sum of the name to speed searching */
  11973. +
  11974. + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
  11975. +
  11976. + struct ylist_head hashLink; /* list of objects in this hash bucket */
  11977. +
  11978. + struct ylist_head hardLinks; /* all the equivalent hard linked objects */
  11979. +
  11980. + /* directory structure stuff */
  11981. + /* also used for linking up the free list */
  11982. + struct yaffs_ObjectStruct *parent;
  11983. + struct ylist_head siblings;
  11984. +
  11985. + /* Where's my object header in NAND? */
  11986. + int hdrChunk;
  11987. +
  11988. + int nDataChunks; /* Number of data chunks attached to the file. */
  11989. +
  11990. + __u32 objectId; /* the object id value */
  11991. +
  11992. + __u32 yst_mode;
  11993. +
  11994. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  11995. + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
  11996. +#endif
  11997. +
  11998. +#ifndef __KERNEL__
  11999. + __u32 inUse;
  12000. +#endif
  12001. +
  12002. +#ifdef CONFIG_YAFFS_WINCE
  12003. + __u32 win_ctime[2];
  12004. + __u32 win_mtime[2];
  12005. + __u32 win_atime[2];
  12006. +#else
  12007. + __u32 yst_uid;
  12008. + __u32 yst_gid;
  12009. + __u32 yst_atime;
  12010. + __u32 yst_mtime;
  12011. + __u32 yst_ctime;
  12012. +#endif
  12013. +
  12014. + __u32 yst_rdev;
  12015. +
  12016. +#ifdef __KERNEL__
  12017. + struct inode *myInode;
  12018. +
  12019. +#endif
  12020. +
  12021. + yaffs_ObjectType variantType;
  12022. +
  12023. + yaffs_ObjectVariant variant;
  12024. +
  12025. +};
  12026. +
  12027. +typedef struct yaffs_ObjectStruct yaffs_Object;
  12028. +
  12029. +struct yaffs_ObjectList_struct {
  12030. + yaffs_Object *objects;
  12031. + struct yaffs_ObjectList_struct *next;
  12032. +};
  12033. +
  12034. +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
  12035. +
  12036. +typedef struct {
  12037. + struct ylist_head list;
  12038. + int count;
  12039. +} yaffs_ObjectBucket;
  12040. +
  12041. +
  12042. +/* yaffs_CheckpointObject holds the definition of an object as dumped
  12043. + * by checkpointing.
  12044. + */
  12045. +
  12046. +typedef struct {
  12047. + int structType;
  12048. + __u32 objectId;
  12049. + __u32 parentId;
  12050. + int hdrChunk;
  12051. + yaffs_ObjectType variantType:3;
  12052. + __u8 deleted:1;
  12053. + __u8 softDeleted:1;
  12054. + __u8 unlinked:1;
  12055. + __u8 fake:1;
  12056. + __u8 renameAllowed:1;
  12057. + __u8 unlinkAllowed:1;
  12058. + __u8 serial;
  12059. +
  12060. + int nDataChunks;
  12061. + __u32 fileSizeOrEquivalentObjectId;
  12062. +} yaffs_CheckpointObject;
  12063. +
  12064. +/*--------------------- Temporary buffers ----------------
  12065. + *
  12066. + * These are chunk-sized working buffers. Each device has a few
  12067. + */
  12068. +
  12069. +typedef struct {
  12070. + __u8 *buffer;
  12071. + int line; /* track from whence this buffer was allocated */
  12072. + int maxLine;
  12073. +} yaffs_TempBuffer;
  12074. +
  12075. +/*----------------- Device ---------------------------------*/
  12076. +
  12077. +struct yaffs_DeviceStruct {
  12078. + struct ylist_head devList;
  12079. + const char *name;
  12080. +
  12081. + /* Entry parameters set up way early. Yaffs sets up the rest.*/
  12082. + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
  12083. + int nChunksPerBlock; /* does not need to be a power of 2 */
  12084. + int spareBytesPerChunk; /* spare area size */
  12085. + int startBlock; /* Start block we're allowed to use */
  12086. + int endBlock; /* End block we're allowed to use */
  12087. + int nReservedBlocks; /* We want this tuneable so that we can reduce */
  12088. + /* reserved blocks on NOR and RAM. */
  12089. +
  12090. +
  12091. + /* Stuff used by the shared space checkpointing mechanism */
  12092. + /* If this value is zero, then this mechanism is disabled */
  12093. +
  12094. +/* int nCheckpointReservedBlocks; */ /* Blocks to reserve for checkpoint data */
  12095. +
  12096. +
  12097. + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
  12098. + * the number of short op caches (don't use too many)
  12099. + */
  12100. +
  12101. + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
  12102. +
  12103. + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
  12104. +
  12105. + void *genericDevice; /* Pointer to device context
  12106. + * On an mtd this holds the mtd pointer.
  12107. + */
  12108. + void *superBlock;
  12109. +
  12110. + /* NAND access functions (Must be set before calling YAFFS)*/
  12111. +
  12112. + int (*writeChunkToNAND) (struct yaffs_DeviceStruct *dev,
  12113. + int chunkInNAND, const __u8 *data,
  12114. + const yaffs_Spare *spare);
  12115. + int (*readChunkFromNAND) (struct yaffs_DeviceStruct *dev,
  12116. + int chunkInNAND, __u8 *data,
  12117. + yaffs_Spare *spare);
  12118. + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct *dev,
  12119. + int blockInNAND);
  12120. + int (*initialiseNAND) (struct yaffs_DeviceStruct *dev);
  12121. + int (*deinitialiseNAND) (struct yaffs_DeviceStruct *dev);
  12122. +
  12123. +#ifdef CONFIG_YAFFS_YAFFS2
  12124. + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct *dev,
  12125. + int chunkInNAND, const __u8 *data,
  12126. + const yaffs_ExtendedTags *tags);
  12127. + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct *dev,
  12128. + int chunkInNAND, __u8 *data,
  12129. + yaffs_ExtendedTags *tags);
  12130. + int (*markNANDBlockBad) (struct yaffs_DeviceStruct *dev, int blockNo);
  12131. + int (*queryNANDBlock) (struct yaffs_DeviceStruct *dev, int blockNo,
  12132. + yaffs_BlockState *state, __u32 *sequenceNumber);
  12133. +#endif
  12134. +
  12135. + int isYaffs2;
  12136. +
  12137. + /* The removeObjectCallback function must be supplied by OS flavours that
  12138. + * need it. The Linux kernel does not use this, but yaffs direct does use
  12139. + * it to implement the faster readdir
  12140. + */
  12141. + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
  12142. +
  12143. + /* Callback to mark the superblock dirsty */
  12144. + void (*markSuperBlockDirty)(void *superblock);
  12145. +
  12146. + int wideTnodesDisabled; /* Set to disable wide tnodes */
  12147. +
  12148. + YCHAR *pathDividers; /* String of legal path dividers */
  12149. +
  12150. +
  12151. + /* End of stuff that must be set before initialisation. */
  12152. +
  12153. + /* Checkpoint control. Can be set before or after initialisation */
  12154. + __u8 skipCheckpointRead;
  12155. + __u8 skipCheckpointWrite;
  12156. +
  12157. + /* Runtime parameters. Set up by YAFFS. */
  12158. +
  12159. + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
  12160. + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
  12161. +
  12162. + /* Stuff to support wide tnodes */
  12163. + __u32 tnodeWidth;
  12164. + __u32 tnodeMask;
  12165. +
  12166. + /* Stuff for figuring out file offset to chunk conversions */
  12167. + __u32 chunkShift; /* Shift value */
  12168. + __u32 chunkDiv; /* Divisor after shifting: 1 for power-of-2 sizes */
  12169. + __u32 chunkMask; /* Mask to use for power-of-2 case */
  12170. +
  12171. + /* Stuff to handle inband tags */
  12172. + int inbandTags;
  12173. + __u32 totalBytesPerChunk;
  12174. +
  12175. +#ifdef __KERNEL__
  12176. +
  12177. + struct semaphore sem; /* Semaphore for waiting on erasure.*/
  12178. + struct semaphore grossLock; /* Gross locking semaphore */
  12179. + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
  12180. + * at compile time so we have to allocate it.
  12181. + */
  12182. + void (*putSuperFunc) (struct super_block *sb);
  12183. +#endif
  12184. +
  12185. + int isMounted;
  12186. +
  12187. + int isCheckpointed;
  12188. +
  12189. +
  12190. + /* Stuff to support block offsetting to support start block zero */
  12191. + int internalStartBlock;
  12192. + int internalEndBlock;
  12193. + int blockOffset;
  12194. + int chunkOffset;
  12195. +
  12196. +
  12197. + /* Runtime checkpointing stuff */
  12198. + int checkpointPageSequence; /* running sequence number of checkpoint pages */
  12199. + int checkpointByteCount;
  12200. + int checkpointByteOffset;
  12201. + __u8 *checkpointBuffer;
  12202. + int checkpointOpenForWrite;
  12203. + int blocksInCheckpoint;
  12204. + int checkpointCurrentChunk;
  12205. + int checkpointCurrentBlock;
  12206. + int checkpointNextBlock;
  12207. + int *checkpointBlockList;
  12208. + int checkpointMaxBlocks;
  12209. + __u32 checkpointSum;
  12210. + __u32 checkpointXor;
  12211. +
  12212. + int nCheckpointBlocksRequired; /* Number of blocks needed to store current checkpoint set */
  12213. +
  12214. + /* Block Info */
  12215. + yaffs_BlockInfo *blockInfo;
  12216. + __u8 *chunkBits; /* bitmap of chunks in use */
  12217. + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
  12218. + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
  12219. + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
  12220. + * Must be consistent with nChunksPerBlock.
  12221. + */
  12222. +
  12223. + int nErasedBlocks;
  12224. + int allocationBlock; /* Current block being allocated off */
  12225. + __u32 allocationPage;
  12226. + int allocationBlockFinder; /* Used to search for next allocation block */
  12227. +
  12228. + /* Runtime state */
  12229. + int nTnodesCreated;
  12230. + yaffs_Tnode *freeTnodes;
  12231. + int nFreeTnodes;
  12232. + yaffs_TnodeList *allocatedTnodeList;
  12233. +
  12234. + int isDoingGC;
  12235. + int gcBlock;
  12236. + int gcChunk;
  12237. +
  12238. + int nObjectsCreated;
  12239. + yaffs_Object *freeObjects;
  12240. + int nFreeObjects;
  12241. +
  12242. + int nHardLinks;
  12243. +
  12244. + yaffs_ObjectList *allocatedObjectList;
  12245. +
  12246. + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
  12247. +
  12248. + int nFreeChunks;
  12249. +
  12250. + int currentDirtyChecker; /* Used to find current dirtiest block */
  12251. +
  12252. + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
  12253. + int nonAggressiveSkip; /* GC state/mode */
  12254. +
  12255. + /* Statistcs */
  12256. + int nPageWrites;
  12257. + int nPageReads;
  12258. + int nBlockErasures;
  12259. + int nErasureFailures;
  12260. + int nGCCopies;
  12261. + int garbageCollections;
  12262. + int passiveGarbageCollections;
  12263. + int nRetriedWrites;
  12264. + int nRetiredBlocks;
  12265. + int eccFixed;
  12266. + int eccUnfixed;
  12267. + int tagsEccFixed;
  12268. + int tagsEccUnfixed;
  12269. + int nDeletions;
  12270. + int nUnmarkedDeletions;
  12271. +
  12272. + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
  12273. +
  12274. + /* Special directories */
  12275. + yaffs_Object *rootDir;
  12276. + yaffs_Object *lostNFoundDir;
  12277. +
  12278. + /* Buffer areas for storing data to recover from write failures TODO
  12279. + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
  12280. + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
  12281. + */
  12282. +
  12283. + int bufferedBlock; /* Which block is buffered here? */
  12284. + int doingBufferedBlockRewrite;
  12285. +
  12286. + yaffs_ChunkCache *srCache;
  12287. + int srLastUse;
  12288. +
  12289. + int cacheHits;
  12290. +
  12291. + /* Stuff for background deletion and unlinked files.*/
  12292. + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
  12293. + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
  12294. + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
  12295. + int nDeletedFiles; /* Count of files awaiting deletion;*/
  12296. + int nUnlinkedFiles; /* Count of unlinked files. */
  12297. + int nBackgroundDeletions; /* Count of background deletions. */
  12298. +
  12299. +
  12300. + /* Temporary buffer management */
  12301. + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
  12302. + int maxTemp;
  12303. + int tempInUse;
  12304. + int unmanagedTempAllocations;
  12305. + int unmanagedTempDeallocations;
  12306. +
  12307. + /* yaffs2 runtime stuff */
  12308. + unsigned sequenceNumber; /* Sequence number of currently allocating block */
  12309. + unsigned oldestDirtySequence;
  12310. +
  12311. +};
  12312. +
  12313. +typedef struct yaffs_DeviceStruct yaffs_Device;
  12314. +
  12315. +/* The static layout of block usage etc is stored in the super block header */
  12316. +typedef struct {
  12317. + int StructType;
  12318. + int version;
  12319. + int checkpointStartBlock;
  12320. + int checkpointEndBlock;
  12321. + int startBlock;
  12322. + int endBlock;
  12323. + int rfu[100];
  12324. +} yaffs_SuperBlockHeader;
  12325. +
  12326. +/* The CheckpointDevice structure holds the device information that changes at runtime and
  12327. + * must be preserved over unmount/mount cycles.
  12328. + */
  12329. +typedef struct {
  12330. + int structType;
  12331. + int nErasedBlocks;
  12332. + int allocationBlock; /* Current block being allocated off */
  12333. + __u32 allocationPage;
  12334. + int nFreeChunks;
  12335. +
  12336. + int nDeletedFiles; /* Count of files awaiting deletion;*/
  12337. + int nUnlinkedFiles; /* Count of unlinked files. */
  12338. + int nBackgroundDeletions; /* Count of background deletions. */
  12339. +
  12340. + /* yaffs2 runtime stuff */
  12341. + unsigned sequenceNumber; /* Sequence number of currently allocating block */
  12342. + unsigned oldestDirtySequence;
  12343. +
  12344. +} yaffs_CheckpointDevice;
  12345. +
  12346. +
  12347. +typedef struct {
  12348. + int structType;
  12349. + __u32 magic;
  12350. + __u32 version;
  12351. + __u32 head;
  12352. +} yaffs_CheckpointValidity;
  12353. +
  12354. +
  12355. +/*----------------------- YAFFS Functions -----------------------*/
  12356. +
  12357. +int yaffs_GutsInitialise(yaffs_Device *dev);
  12358. +void yaffs_Deinitialise(yaffs_Device *dev);
  12359. +
  12360. +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
  12361. +
  12362. +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
  12363. + yaffs_Object *newDir, const YCHAR *newName);
  12364. +
  12365. +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name);
  12366. +int yaffs_DeleteObject(yaffs_Object *obj);
  12367. +
  12368. +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize);
  12369. +int yaffs_GetObjectFileLength(yaffs_Object *obj);
  12370. +int yaffs_GetObjectInode(yaffs_Object *obj);
  12371. +unsigned yaffs_GetObjectType(yaffs_Object *obj);
  12372. +int yaffs_GetObjectLinkCount(yaffs_Object *obj);
  12373. +
  12374. +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
  12375. +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
  12376. +
  12377. +/* File operations */
  12378. +int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, loff_t offset,
  12379. + int nBytes);
  12380. +int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, loff_t offset,
  12381. + int nBytes, int writeThrough);
  12382. +int yaffs_ResizeFile(yaffs_Object *obj, loff_t newSize);
  12383. +
  12384. +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
  12385. + __u32 mode, __u32 uid, __u32 gid);
  12386. +int yaffs_FlushFile(yaffs_Object *obj, int updateTime);
  12387. +
  12388. +/* Flushing and checkpointing */
  12389. +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
  12390. +
  12391. +int yaffs_CheckpointSave(yaffs_Device *dev);
  12392. +int yaffs_CheckpointRestore(yaffs_Device *dev);
  12393. +
  12394. +/* Directory operations */
  12395. +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
  12396. + __u32 mode, __u32 uid, __u32 gid);
  12397. +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir, const YCHAR *name);
  12398. +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
  12399. + int (*fn) (yaffs_Object *));
  12400. +
  12401. +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number);
  12402. +
  12403. +/* Link operations */
  12404. +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
  12405. + yaffs_Object *equivalentObject);
  12406. +
  12407. +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
  12408. +
  12409. +/* Symlink operations */
  12410. +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
  12411. + __u32 mode, __u32 uid, __u32 gid,
  12412. + const YCHAR *alias);
  12413. +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj);
  12414. +
  12415. +/* Special inodes (fifos, sockets and devices) */
  12416. +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
  12417. + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
  12418. +
  12419. +/* Special directories */
  12420. +yaffs_Object *yaffs_Root(yaffs_Device *dev);
  12421. +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
  12422. +
  12423. +#ifdef CONFIG_YAFFS_WINCE
  12424. +/* CONFIG_YAFFS_WINCE special stuff */
  12425. +void yfsd_WinFileTimeNow(__u32 target[2]);
  12426. +#endif
  12427. +
  12428. +#ifdef __KERNEL__
  12429. +
  12430. +void yaffs_HandleDeferedFree(yaffs_Object *obj);
  12431. +#endif
  12432. +
  12433. +/* Debug dump */
  12434. +int yaffs_DumpObject(yaffs_Object *obj);
  12435. +
  12436. +void yaffs_GutsTest(yaffs_Device *dev);
  12437. +
  12438. +/* A few useful functions */
  12439. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
  12440. +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn);
  12441. +int yaffs_CheckFF(__u8 *buffer, int nBytes);
  12442. +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
  12443. +
  12444. +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo);
  12445. +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
  12446. +
  12447. +#endif
  12448. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffsinterface.h linux-2.6.34/fs/yaffs2/yaffsinterface.h
  12449. --- linux-2.6.34.orig/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
  12450. +++ linux-2.6.34/fs/yaffs2/yaffsinterface.h 2010-05-20 20:16:22.301615823 +0200
  12451. @@ -0,0 +1,21 @@
  12452. +/*
  12453. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  12454. + *
  12455. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12456. + * for Toby Churchill Ltd and Brightstar Engineering
  12457. + *
  12458. + * Created by Charles Manning <charles@aleph1.co.uk>
  12459. + *
  12460. + * This program is free software; you can redistribute it and/or modify
  12461. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  12462. + * published by the Free Software Foundation.
  12463. + *
  12464. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  12465. + */
  12466. +
  12467. +#ifndef __YAFFSINTERFACE_H__
  12468. +#define __YAFFSINTERFACE_H__
  12469. +
  12470. +int yaffs_Initialise(unsigned nBlocks);
  12471. +
  12472. +#endif
  12473. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif1.c linux-2.6.34/fs/yaffs2/yaffs_mtdif1.c
  12474. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
  12475. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif1.c 2010-05-20 20:16:22.301615823 +0200
  12476. @@ -0,0 +1,365 @@
  12477. +/*
  12478. + * YAFFS: Yet another FFS. A NAND-flash specific file system.
  12479. + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
  12480. + *
  12481. + * Copyright (C) 2002 Aleph One Ltd.
  12482. + * for Toby Churchill Ltd and Brightstar Engineering
  12483. + *
  12484. + * This program is free software; you can redistribute it and/or modify
  12485. + * it under the terms of the GNU General Public License version 2 as
  12486. + * published by the Free Software Foundation.
  12487. + */
  12488. +
  12489. +/*
  12490. + * This module provides the interface between yaffs_nand.c and the
  12491. + * MTD API. This version is used when the MTD interface supports the
  12492. + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
  12493. + * and we have small-page NAND device.
  12494. + *
  12495. + * These functions are invoked via function pointers in yaffs_nand.c.
  12496. + * This replaces functionality provided by functions in yaffs_mtdif.c
  12497. + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
  12498. + * called in yaffs_mtdif.c when the function pointers are NULL.
  12499. + * We assume the MTD layer is performing ECC (useNANDECC is true).
  12500. + */
  12501. +
  12502. +#include "yportenv.h"
  12503. +#include "yaffs_guts.h"
  12504. +#include "yaffs_packedtags1.h"
  12505. +#include "yaffs_tagscompat.h" /* for yaffs_CalcTagsECC */
  12506. +
  12507. +#include "linux/kernel.h"
  12508. +#include "linux/version.h"
  12509. +#include "linux/types.h"
  12510. +#include "linux/mtd/mtd.h"
  12511. +
  12512. +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
  12513. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12514. +
  12515. +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.10 2009-03-09 07:41:10 charles Exp $";
  12516. +
  12517. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12518. +# define YTAG1_SIZE 8
  12519. +#else
  12520. +# define YTAG1_SIZE 9
  12521. +#endif
  12522. +
  12523. +#if 0
  12524. +/* Use the following nand_ecclayout with MTD when using
  12525. + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
  12526. + * If you have existing Yaffs images and the byte order differs from this,
  12527. + * adjust 'oobfree' to match your existing Yaffs data.
  12528. + *
  12529. + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
  12530. + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
  12531. + * the 9th byte.
  12532. + *
  12533. + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
  12534. + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
  12535. + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
  12536. + * byte and B is the small-page bad-block indicator byte.
  12537. + */
  12538. +static struct nand_ecclayout nand_oob_16 = {
  12539. + .eccbytes = 6,
  12540. + .eccpos = { 8, 9, 10, 13, 14, 15 },
  12541. + .oobavail = 9,
  12542. + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  12543. +};
  12544. +#endif
  12545. +
  12546. +/* Write a chunk (page) of data to NAND.
  12547. + *
  12548. + * Caller always provides ExtendedTags data which are converted to a more
  12549. + * compact (packed) form for storage in NAND. A mini-ECC runs over the
  12550. + * contents of the tags meta-data; used to valid the tags when read.
  12551. + *
  12552. + * - Pack ExtendedTags to PackedTags1 form
  12553. + * - Compute mini-ECC for PackedTags1
  12554. + * - Write data and packed tags to NAND.
  12555. + *
  12556. + * Note: Due to the use of the PackedTags1 meta-data which does not include
  12557. + * a full sequence number (as found in the larger PackedTags2 form) it is
  12558. + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
  12559. + * discarded and dirty. This is not ideal: newer NAND parts are supposed
  12560. + * to be written just once. When Yaffs performs this operation, this
  12561. + * function is called with a NULL data pointer -- calling MTD write_oob
  12562. + * without data is valid usage (2.6.17).
  12563. + *
  12564. + * Any underlying MTD error results in YAFFS_FAIL.
  12565. + * Returns YAFFS_OK or YAFFS_FAIL.
  12566. + */
  12567. +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  12568. + int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
  12569. +{
  12570. + struct mtd_info *mtd = dev->genericDevice;
  12571. + int chunkBytes = dev->nDataBytesPerChunk;
  12572. + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
  12573. + struct mtd_oob_ops ops;
  12574. + yaffs_PackedTags1 pt1;
  12575. + int retval;
  12576. +
  12577. + /* we assume that PackedTags1 and yaffs_Tags are compatible */
  12578. + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
  12579. + compile_time_assertion(sizeof(yaffs_Tags) == 8);
  12580. +
  12581. + dev->nPageWrites++;
  12582. +
  12583. + yaffs_PackTags1(&pt1, etags);
  12584. + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
  12585. +
  12586. + /* When deleting a chunk, the upper layer provides only skeletal
  12587. + * etags, one with chunkDeleted set. However, we need to update the
  12588. + * tags, not erase them completely. So we use the NAND write property
  12589. + * that only zeroed-bits stick and set tag bytes to all-ones and
  12590. + * zero just the (not) deleted bit.
  12591. + */
  12592. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12593. + if (etags->chunkDeleted) {
  12594. + memset(&pt1, 0xff, 8);
  12595. + /* clear delete status bit to indicate deleted */
  12596. + pt1.deleted = 0;
  12597. + }
  12598. +#else
  12599. + ((__u8 *)&pt1)[8] = 0xff;
  12600. + if (etags->chunkDeleted) {
  12601. + memset(&pt1, 0xff, 8);
  12602. + /* zero pageStatus byte to indicate deleted */
  12603. + ((__u8 *)&pt1)[8] = 0;
  12604. + }
  12605. +#endif
  12606. +
  12607. + memset(&ops, 0, sizeof(ops));
  12608. + ops.mode = MTD_OOB_AUTO;
  12609. + ops.len = (data) ? chunkBytes : 0;
  12610. + ops.ooblen = YTAG1_SIZE;
  12611. + ops.datbuf = (__u8 *)data;
  12612. + ops.oobbuf = (__u8 *)&pt1;
  12613. +
  12614. + retval = mtd->write_oob(mtd, addr, &ops);
  12615. + if (retval) {
  12616. + yaffs_trace(YAFFS_TRACE_MTD,
  12617. + "write_oob failed, chunk %d, mtd error %d\n",
  12618. + chunkInNAND, retval);
  12619. + }
  12620. + return retval ? YAFFS_FAIL : YAFFS_OK;
  12621. +}
  12622. +
  12623. +/* Return with empty ExtendedTags but add eccResult.
  12624. + */
  12625. +static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
  12626. +{
  12627. + if (etags) {
  12628. + memset(etags, 0, sizeof(*etags));
  12629. + etags->eccResult = eccResult;
  12630. + }
  12631. + return retval;
  12632. +}
  12633. +
  12634. +/* Read a chunk (page) from NAND.
  12635. + *
  12636. + * Caller expects ExtendedTags data to be usable even on error; that is,
  12637. + * all members except eccResult and blockBad are zeroed.
  12638. + *
  12639. + * - Check ECC results for data (if applicable)
  12640. + * - Check for blank/erased block (return empty ExtendedTags if blank)
  12641. + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
  12642. + * - Convert PackedTags1 to ExtendedTags
  12643. + * - Update eccResult and blockBad members to refect state.
  12644. + *
  12645. + * Returns YAFFS_OK or YAFFS_FAIL.
  12646. + */
  12647. +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
  12648. + int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
  12649. +{
  12650. + struct mtd_info *mtd = dev->genericDevice;
  12651. + int chunkBytes = dev->nDataBytesPerChunk;
  12652. + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
  12653. + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
  12654. + struct mtd_oob_ops ops;
  12655. + yaffs_PackedTags1 pt1;
  12656. + int retval;
  12657. + int deleted;
  12658. +
  12659. + dev->nPageReads++;
  12660. +
  12661. + memset(&ops, 0, sizeof(ops));
  12662. + ops.mode = MTD_OOB_AUTO;
  12663. + ops.len = (data) ? chunkBytes : 0;
  12664. + ops.ooblen = YTAG1_SIZE;
  12665. + ops.datbuf = data;
  12666. + ops.oobbuf = (__u8 *)&pt1;
  12667. +
  12668. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
  12669. + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
  12670. + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
  12671. + */
  12672. + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
  12673. +#endif
  12674. + /* Read page and oob using MTD.
  12675. + * Check status and determine ECC result.
  12676. + */
  12677. + retval = mtd->read_oob(mtd, addr, &ops);
  12678. + if (retval) {
  12679. + yaffs_trace(YAFFS_TRACE_MTD,
  12680. + "read_oob failed, chunk %d, mtd error %d\n",
  12681. + chunkInNAND, retval);
  12682. + }
  12683. +
  12684. + switch (retval) {
  12685. + case 0:
  12686. + /* no error */
  12687. + break;
  12688. +
  12689. + case -EUCLEAN:
  12690. + /* MTD's ECC fixed the data */
  12691. + eccres = YAFFS_ECC_RESULT_FIXED;
  12692. + dev->eccFixed++;
  12693. + break;
  12694. +
  12695. + case -EBADMSG:
  12696. + /* MTD's ECC could not fix the data */
  12697. + dev->eccUnfixed++;
  12698. + /* fall into... */
  12699. + default:
  12700. + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
  12701. + etags->blockBad = (mtd->block_isbad)(mtd, addr);
  12702. + return YAFFS_FAIL;
  12703. + }
  12704. +
  12705. + /* Check for a blank/erased chunk.
  12706. + */
  12707. + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
  12708. + /* when blank, upper layers want eccResult to be <= NO_ERROR */
  12709. + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
  12710. + }
  12711. +
  12712. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12713. + /* Read deleted status (bit) then return it to it's non-deleted
  12714. + * state before performing tags mini-ECC check. pt1.deleted is
  12715. + * inverted.
  12716. + */
  12717. + deleted = !pt1.deleted;
  12718. + pt1.deleted = 1;
  12719. +#else
  12720. + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
  12721. +#endif
  12722. +
  12723. + /* Check the packed tags mini-ECC and correct if necessary/possible.
  12724. + */
  12725. + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
  12726. + switch (retval) {
  12727. + case 0:
  12728. + /* no tags error, use MTD result */
  12729. + break;
  12730. + case 1:
  12731. + /* recovered tags-ECC error */
  12732. + dev->tagsEccFixed++;
  12733. + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
  12734. + eccres = YAFFS_ECC_RESULT_FIXED;
  12735. + break;
  12736. + default:
  12737. + /* unrecovered tags-ECC error */
  12738. + dev->tagsEccUnfixed++;
  12739. + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
  12740. + }
  12741. +
  12742. + /* Unpack the tags to extended form and set ECC result.
  12743. + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
  12744. + */
  12745. + pt1.shouldBeFF = 0xFFFFFFFF;
  12746. + yaffs_UnpackTags1(etags, &pt1);
  12747. + etags->eccResult = eccres;
  12748. +
  12749. + /* Set deleted state */
  12750. + etags->chunkDeleted = deleted;
  12751. + return YAFFS_OK;
  12752. +}
  12753. +
  12754. +/* Mark a block bad.
  12755. + *
  12756. + * This is a persistant state.
  12757. + * Use of this function should be rare.
  12758. + *
  12759. + * Returns YAFFS_OK or YAFFS_FAIL.
  12760. + */
  12761. +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
  12762. +{
  12763. + struct mtd_info *mtd = dev->genericDevice;
  12764. + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
  12765. + int retval;
  12766. +
  12767. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad\n", blockNo);
  12768. +
  12769. + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
  12770. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  12771. +}
  12772. +
  12773. +/* Check any MTD prerequists.
  12774. + *
  12775. + * Returns YAFFS_OK or YAFFS_FAIL.
  12776. + */
  12777. +static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
  12778. +{
  12779. + /* 2.6.18 has mtd->ecclayout->oobavail */
  12780. + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
  12781. + int oobavail = mtd->ecclayout->oobavail;
  12782. +
  12783. + if (oobavail < YTAG1_SIZE) {
  12784. + yaffs_trace(YAFFS_TRACE_ERROR,
  12785. + "mtd device has only %d bytes for tags, need %d\n",
  12786. + oobavail, YTAG1_SIZE);
  12787. + return YAFFS_FAIL;
  12788. + }
  12789. + return YAFFS_OK;
  12790. +}
  12791. +
  12792. +/* Query for the current state of a specific block.
  12793. + *
  12794. + * Examine the tags of the first chunk of the block and return the state:
  12795. + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
  12796. + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
  12797. + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
  12798. + *
  12799. + * Always returns YAFFS_OK.
  12800. + */
  12801. +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  12802. + yaffs_BlockState *pState, __u32 *pSequenceNumber)
  12803. +{
  12804. + struct mtd_info *mtd = dev->genericDevice;
  12805. + int chunkNo = blockNo * dev->nChunksPerBlock;
  12806. + loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
  12807. + yaffs_ExtendedTags etags;
  12808. + int state = YAFFS_BLOCK_STATE_DEAD;
  12809. + int seqnum = 0;
  12810. + int retval;
  12811. +
  12812. + /* We don't yet have a good place to test for MTD config prerequists.
  12813. + * Do it here as we are called during the initial scan.
  12814. + */
  12815. + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK)
  12816. + return YAFFS_FAIL;
  12817. +
  12818. + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
  12819. + etags.blockBad = (mtd->block_isbad)(mtd, addr);
  12820. + if (etags.blockBad) {
  12821. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  12822. + "block %d is marked bad\n", blockNo);
  12823. + state = YAFFS_BLOCK_STATE_DEAD;
  12824. + } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
  12825. + /* bad tags, need to look more closely */
  12826. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  12827. + } else if (etags.chunkUsed) {
  12828. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  12829. + seqnum = etags.sequenceNumber;
  12830. + } else {
  12831. + state = YAFFS_BLOCK_STATE_EMPTY;
  12832. + }
  12833. +
  12834. + *pState = state;
  12835. + *pSequenceNumber = seqnum;
  12836. +
  12837. + /* query always succeeds */
  12838. + return YAFFS_OK;
  12839. +}
  12840. +
  12841. +#endif /*MTD_VERSION*/
  12842. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif1.h linux-2.6.34/fs/yaffs2/yaffs_mtdif1.h
  12843. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
  12844. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif1.h 2010-05-20 20:16:22.311614862 +0200
  12845. @@ -0,0 +1,28 @@
  12846. +/*
  12847. + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
  12848. + *
  12849. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12850. + * for Toby Churchill Ltd and Brightstar Engineering
  12851. + *
  12852. + * This program is free software; you can redistribute it and/or modify
  12853. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  12854. + * published by the Free Software Foundation.
  12855. + *
  12856. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  12857. + */
  12858. +
  12859. +#ifndef __YAFFS_MTDIF1_H__
  12860. +#define __YAFFS_MTDIF1_H__
  12861. +
  12862. +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  12863. + const __u8 *data, const yaffs_ExtendedTags *tags);
  12864. +
  12865. +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  12866. + __u8 *data, yaffs_ExtendedTags *tags);
  12867. +
  12868. +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  12869. +
  12870. +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  12871. + yaffs_BlockState *state, __u32 *sequenceNumber);
  12872. +
  12873. +#endif
  12874. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif2.c linux-2.6.34/fs/yaffs2/yaffs_mtdif2.c
  12875. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
  12876. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif2.c 2010-05-20 20:16:22.311614862 +0200
  12877. @@ -0,0 +1,246 @@
  12878. +/*
  12879. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  12880. + *
  12881. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12882. + * for Toby Churchill Ltd and Brightstar Engineering
  12883. + *
  12884. + * Created by Charles Manning <charles@aleph1.co.uk>
  12885. + *
  12886. + * This program is free software; you can redistribute it and/or modify
  12887. + * it under the terms of the GNU General Public License version 2 as
  12888. + * published by the Free Software Foundation.
  12889. + */
  12890. +
  12891. +/* mtd interface for YAFFS2 */
  12892. +
  12893. +const char *yaffs_mtdif2_c_version =
  12894. + "$Id: yaffs_mtdif2.c,v 1.23 2009-03-06 17:20:53 wookey Exp $";
  12895. +
  12896. +#include "yportenv.h"
  12897. +
  12898. +
  12899. +#include "yaffs_mtdif2.h"
  12900. +
  12901. +#include "linux/mtd/mtd.h"
  12902. +#include "linux/types.h"
  12903. +#include "linux/time.h"
  12904. +
  12905. +#include "yaffs_packedtags2.h"
  12906. +
  12907. +/* NB For use with inband tags....
  12908. + * We assume that the data buffer is of size totalBytersPerChunk so that we can also
  12909. + * use it to load the tags.
  12910. + */
  12911. +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  12912. + const __u8 *data,
  12913. + const yaffs_ExtendedTags *tags)
  12914. +{
  12915. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  12916. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12917. + struct mtd_oob_ops ops;
  12918. +#else
  12919. + size_t dummy;
  12920. +#endif
  12921. + int retval = 0;
  12922. +
  12923. + loff_t addr;
  12924. +
  12925. + yaffs_PackedTags2 pt;
  12926. +
  12927. + T(YAFFS_TRACE_MTD,
  12928. + (TSTR
  12929. + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
  12930. + TENDSTR), chunkInNAND, data, tags));
  12931. +
  12932. +
  12933. + addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
  12934. +
  12935. + /* For yaffs2 writing there must be both data and tags.
  12936. + * If we're using inband tags, then the tags are stuffed into
  12937. + * the end of the data buffer.
  12938. + */
  12939. + if (!data || !tags)
  12940. + BUG();
  12941. + else if (dev->inbandTags) {
  12942. + yaffs_PackedTags2TagsPart *pt2tp;
  12943. + pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
  12944. + yaffs_PackTags2TagsPart(pt2tp, tags);
  12945. + } else
  12946. + yaffs_PackTags2(&pt, tags);
  12947. +
  12948. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  12949. + ops.mode = MTD_OOB_AUTO;
  12950. + ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
  12951. + ops.len = dev->totalBytesPerChunk;
  12952. + ops.ooboffs = 0;
  12953. + ops.datbuf = (__u8 *)data;
  12954. + ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
  12955. + retval = mtd->write_oob(mtd, addr, &ops);
  12956. +
  12957. +#else
  12958. + if (!dev->inbandTags) {
  12959. + retval =
  12960. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  12961. + &dummy, data, (__u8 *) &pt, NULL);
  12962. + } else {
  12963. + retval =
  12964. + mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
  12965. + data);
  12966. + }
  12967. +#endif
  12968. +
  12969. + if (retval == 0)
  12970. + return YAFFS_OK;
  12971. + else
  12972. + return YAFFS_FAIL;
  12973. +}
  12974. +
  12975. +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  12976. + __u8 *data, yaffs_ExtendedTags *tags)
  12977. +{
  12978. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  12979. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12980. + struct mtd_oob_ops ops;
  12981. +#endif
  12982. + size_t dummy;
  12983. + int retval = 0;
  12984. + int localData = 0;
  12985. +
  12986. + loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
  12987. +
  12988. + yaffs_PackedTags2 pt;
  12989. +
  12990. + T(YAFFS_TRACE_MTD,
  12991. + (TSTR
  12992. + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
  12993. + TENDSTR), chunkInNAND, data, tags));
  12994. +
  12995. + if (dev->inbandTags) {
  12996. +
  12997. + if (!data) {
  12998. + localData = 1;
  12999. + data = yaffs_GetTempBuffer(dev, __LINE__);
  13000. + }
  13001. +
  13002. +
  13003. + }
  13004. +
  13005. +
  13006. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13007. + if (dev->inbandTags || (data && !tags))
  13008. + retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
  13009. + &dummy, data);
  13010. + else if (tags) {
  13011. + ops.mode = MTD_OOB_AUTO;
  13012. + ops.ooblen = sizeof(pt);
  13013. + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
  13014. + ops.ooboffs = 0;
  13015. + ops.datbuf = data;
  13016. + ops.oobbuf = dev->spareBuffer;
  13017. + retval = mtd->read_oob(mtd, addr, &ops);
  13018. + }
  13019. +#else
  13020. + if (!dev->inbandTags && data && tags) {
  13021. +
  13022. + retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13023. + &dummy, data, dev->spareBuffer,
  13024. + NULL);
  13025. + } else {
  13026. + if (data)
  13027. + retval =
  13028. + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13029. + data);
  13030. + if (!dev->inbandTags && tags)
  13031. + retval =
  13032. + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
  13033. + dev->spareBuffer);
  13034. + }
  13035. +#endif
  13036. +
  13037. +
  13038. + if (dev->inbandTags) {
  13039. + if (tags) {
  13040. + yaffs_PackedTags2TagsPart *pt2tp;
  13041. + pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
  13042. + yaffs_UnpackTags2TagsPart(tags, pt2tp);
  13043. + }
  13044. + } else {
  13045. + if (tags) {
  13046. + memcpy(&pt, dev->spareBuffer, sizeof(pt));
  13047. + yaffs_UnpackTags2(tags, &pt);
  13048. + }
  13049. + }
  13050. +
  13051. + if (localData)
  13052. + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
  13053. +
  13054. + if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
  13055. + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
  13056. + if (retval == 0)
  13057. + return YAFFS_OK;
  13058. + else
  13059. + return YAFFS_FAIL;
  13060. +}
  13061. +
  13062. +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
  13063. +{
  13064. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13065. + int retval;
  13066. + T(YAFFS_TRACE_MTD,
  13067. + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
  13068. +
  13069. + retval =
  13070. + mtd->block_markbad(mtd,
  13071. + blockNo * dev->nChunksPerBlock *
  13072. + dev->totalBytesPerChunk);
  13073. +
  13074. + if (retval == 0)
  13075. + return YAFFS_OK;
  13076. + else
  13077. + return YAFFS_FAIL;
  13078. +
  13079. +}
  13080. +
  13081. +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13082. + yaffs_BlockState *state, __u32 *sequenceNumber)
  13083. +{
  13084. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13085. + int retval;
  13086. +
  13087. + T(YAFFS_TRACE_MTD,
  13088. + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
  13089. + retval =
  13090. + mtd->block_isbad(mtd,
  13091. + blockNo * dev->nChunksPerBlock *
  13092. + dev->totalBytesPerChunk);
  13093. +
  13094. + if (retval) {
  13095. + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
  13096. +
  13097. + *state = YAFFS_BLOCK_STATE_DEAD;
  13098. + *sequenceNumber = 0;
  13099. + } else {
  13100. + yaffs_ExtendedTags t;
  13101. + nandmtd2_ReadChunkWithTagsFromNAND(dev,
  13102. + blockNo *
  13103. + dev->nChunksPerBlock, NULL,
  13104. + &t);
  13105. +
  13106. + if (t.chunkUsed) {
  13107. + *sequenceNumber = t.sequenceNumber;
  13108. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  13109. + } else {
  13110. + *sequenceNumber = 0;
  13111. + *state = YAFFS_BLOCK_STATE_EMPTY;
  13112. + }
  13113. + }
  13114. + T(YAFFS_TRACE_MTD,
  13115. + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
  13116. + *state));
  13117. +
  13118. + if (retval == 0)
  13119. + return YAFFS_OK;
  13120. + else
  13121. + return YAFFS_FAIL;
  13122. +}
  13123. +
  13124. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif2.h linux-2.6.34/fs/yaffs2/yaffs_mtdif2.h
  13125. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
  13126. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif2.h 2010-05-20 20:16:22.311614862 +0200
  13127. @@ -0,0 +1,29 @@
  13128. +/*
  13129. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13130. + *
  13131. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13132. + * for Toby Churchill Ltd and Brightstar Engineering
  13133. + *
  13134. + * Created by Charles Manning <charles@aleph1.co.uk>
  13135. + *
  13136. + * This program is free software; you can redistribute it and/or modify
  13137. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13138. + * published by the Free Software Foundation.
  13139. + *
  13140. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13141. + */
  13142. +
  13143. +#ifndef __YAFFS_MTDIF2_H__
  13144. +#define __YAFFS_MTDIF2_H__
  13145. +
  13146. +#include "yaffs_guts.h"
  13147. +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  13148. + const __u8 *data,
  13149. + const yaffs_ExtendedTags *tags);
  13150. +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13151. + __u8 *data, yaffs_ExtendedTags *tags);
  13152. +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  13153. +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13154. + yaffs_BlockState *state, __u32 *sequenceNumber);
  13155. +
  13156. +#endif
  13157. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif.c linux-2.6.34/fs/yaffs2/yaffs_mtdif.c
  13158. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  13159. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif.c 2010-05-20 20:16:22.311614862 +0200
  13160. @@ -0,0 +1,241 @@
  13161. +/*
  13162. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13163. + *
  13164. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13165. + * for Toby Churchill Ltd and Brightstar Engineering
  13166. + *
  13167. + * Created by Charles Manning <charles@aleph1.co.uk>
  13168. + *
  13169. + * This program is free software; you can redistribute it and/or modify
  13170. + * it under the terms of the GNU General Public License version 2 as
  13171. + * published by the Free Software Foundation.
  13172. + */
  13173. +
  13174. +const char *yaffs_mtdif_c_version =
  13175. + "$Id: yaffs_mtdif.c,v 1.22 2009-03-06 17:20:51 wookey Exp $";
  13176. +
  13177. +#include "yportenv.h"
  13178. +
  13179. +
  13180. +#include "yaffs_mtdif.h"
  13181. +
  13182. +#include "linux/mtd/mtd.h"
  13183. +#include "linux/types.h"
  13184. +#include "linux/time.h"
  13185. +#include "linux/mtd/nand.h"
  13186. +
  13187. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
  13188. +static struct nand_oobinfo yaffs_oobinfo = {
  13189. + .useecc = 1,
  13190. + .eccbytes = 6,
  13191. + .eccpos = {8, 9, 10, 13, 14, 15}
  13192. +};
  13193. +
  13194. +static struct nand_oobinfo yaffs_noeccinfo = {
  13195. + .useecc = 0,
  13196. +};
  13197. +#endif
  13198. +
  13199. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13200. +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
  13201. +{
  13202. + oob[0] = spare->tagByte0;
  13203. + oob[1] = spare->tagByte1;
  13204. + oob[2] = spare->tagByte2;
  13205. + oob[3] = spare->tagByte3;
  13206. + oob[4] = spare->tagByte4;
  13207. + oob[5] = spare->tagByte5 & 0x3f;
  13208. + oob[5] |= spare->blockStatus == 'Y' ? 0 : 0x80;
  13209. + oob[5] |= spare->pageStatus == 0 ? 0 : 0x40;
  13210. + oob[6] = spare->tagByte6;
  13211. + oob[7] = spare->tagByte7;
  13212. +}
  13213. +
  13214. +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
  13215. +{
  13216. + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
  13217. + spare->tagByte0 = oob[0];
  13218. + spare->tagByte1 = oob[1];
  13219. + spare->tagByte2 = oob[2];
  13220. + spare->tagByte3 = oob[3];
  13221. + spare->tagByte4 = oob[4];
  13222. + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
  13223. + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
  13224. + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
  13225. + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
  13226. + spare->tagByte6 = oob[6];
  13227. + spare->tagByte7 = oob[7];
  13228. + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
  13229. +
  13230. + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
  13231. +}
  13232. +#endif
  13233. +
  13234. +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
  13235. + const __u8 *data, const yaffs_Spare *spare)
  13236. +{
  13237. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13238. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13239. + struct mtd_oob_ops ops;
  13240. +#endif
  13241. + size_t dummy;
  13242. + int retval = 0;
  13243. +
  13244. + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
  13245. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13246. + __u8 spareAsBytes[8]; /* OOB */
  13247. +
  13248. + if (data && !spare)
  13249. + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
  13250. + &dummy, data);
  13251. + else if (spare) {
  13252. + if (dev->useNANDECC) {
  13253. + translate_spare2oob(spare, spareAsBytes);
  13254. + ops.mode = MTD_OOB_AUTO;
  13255. + ops.ooblen = 8; /* temp hack */
  13256. + } else {
  13257. + ops.mode = MTD_OOB_RAW;
  13258. + ops.ooblen = YAFFS_BYTES_PER_SPARE;
  13259. + }
  13260. + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
  13261. + ops.datbuf = (u8 *)data;
  13262. + ops.ooboffs = 0;
  13263. + ops.oobbuf = spareAsBytes;
  13264. + retval = mtd->write_oob(mtd, addr, &ops);
  13265. + }
  13266. +#else
  13267. + __u8 *spareAsBytes = (__u8 *) spare;
  13268. +
  13269. + if (data && spare) {
  13270. + if (dev->useNANDECC)
  13271. + retval =
  13272. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13273. + &dummy, data, spareAsBytes,
  13274. + &yaffs_oobinfo);
  13275. + else
  13276. + retval =
  13277. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13278. + &dummy, data, spareAsBytes,
  13279. + &yaffs_noeccinfo);
  13280. + } else {
  13281. + if (data)
  13282. + retval =
  13283. + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13284. + data);
  13285. + if (spare)
  13286. + retval =
  13287. + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
  13288. + &dummy, spareAsBytes);
  13289. + }
  13290. +#endif
  13291. +
  13292. + if (retval == 0)
  13293. + return YAFFS_OK;
  13294. + else
  13295. + return YAFFS_FAIL;
  13296. +}
  13297. +
  13298. +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
  13299. + yaffs_Spare *spare)
  13300. +{
  13301. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13302. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13303. + struct mtd_oob_ops ops;
  13304. +#endif
  13305. + size_t dummy;
  13306. + int retval = 0;
  13307. +
  13308. + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
  13309. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13310. + __u8 spareAsBytes[8]; /* OOB */
  13311. +
  13312. + if (data && !spare)
  13313. + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
  13314. + &dummy, data);
  13315. + else if (spare) {
  13316. + if (dev->useNANDECC) {
  13317. + ops.mode = MTD_OOB_AUTO;
  13318. + ops.ooblen = 8; /* temp hack */
  13319. + } else {
  13320. + ops.mode = MTD_OOB_RAW;
  13321. + ops.ooblen = YAFFS_BYTES_PER_SPARE;
  13322. + }
  13323. + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
  13324. + ops.datbuf = data;
  13325. + ops.ooboffs = 0;
  13326. + ops.oobbuf = spareAsBytes;
  13327. + retval = mtd->read_oob(mtd, addr, &ops);
  13328. + if (dev->useNANDECC)
  13329. + translate_oob2spare(spare, spareAsBytes);
  13330. + }
  13331. +#else
  13332. + __u8 *spareAsBytes = (__u8 *) spare;
  13333. +
  13334. + if (data && spare) {
  13335. + if (dev->useNANDECC) {
  13336. + /* Careful, this call adds 2 ints */
  13337. + /* to the end of the spare data. Calling function */
  13338. + /* should allocate enough memory for spare, */
  13339. + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
  13340. + retval =
  13341. + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13342. + &dummy, data, spareAsBytes,
  13343. + &yaffs_oobinfo);
  13344. + } else {
  13345. + retval =
  13346. + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13347. + &dummy, data, spareAsBytes,
  13348. + &yaffs_noeccinfo);
  13349. + }
  13350. + } else {
  13351. + if (data)
  13352. + retval =
  13353. + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13354. + data);
  13355. + if (spare)
  13356. + retval =
  13357. + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
  13358. + &dummy, spareAsBytes);
  13359. + }
  13360. +#endif
  13361. +
  13362. + if (retval == 0)
  13363. + return YAFFS_OK;
  13364. + else
  13365. + return YAFFS_FAIL;
  13366. +}
  13367. +
  13368. +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
  13369. +{
  13370. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13371. + __u32 addr =
  13372. + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
  13373. + * dev->nChunksPerBlock;
  13374. + struct erase_info ei;
  13375. + int retval = 0;
  13376. +
  13377. + ei.mtd = mtd;
  13378. + ei.addr = addr;
  13379. + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
  13380. + ei.time = 1000;
  13381. + ei.retries = 2;
  13382. + ei.callback = NULL;
  13383. + ei.priv = (u_long) dev;
  13384. +
  13385. + /* Todo finish off the ei if required */
  13386. +
  13387. + sema_init(&dev->sem, 0);
  13388. +
  13389. + retval = mtd->erase(mtd, &ei);
  13390. +
  13391. + if (retval == 0)
  13392. + return YAFFS_OK;
  13393. + else
  13394. + return YAFFS_FAIL;
  13395. +}
  13396. +
  13397. +int nandmtd_InitialiseNAND(yaffs_Device *dev)
  13398. +{
  13399. + return YAFFS_OK;
  13400. +}
  13401. +
  13402. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif.h linux-2.6.34/fs/yaffs2/yaffs_mtdif.h
  13403. --- linux-2.6.34.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  13404. +++ linux-2.6.34/fs/yaffs2/yaffs_mtdif.h 2010-05-20 20:16:22.311614862 +0200
  13405. @@ -0,0 +1,32 @@
  13406. +/*
  13407. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13408. + *
  13409. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13410. + * for Toby Churchill Ltd and Brightstar Engineering
  13411. + *
  13412. + * Created by Charles Manning <charles@aleph1.co.uk>
  13413. + *
  13414. + * This program is free software; you can redistribute it and/or modify
  13415. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13416. + * published by the Free Software Foundation.
  13417. + *
  13418. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13419. + */
  13420. +
  13421. +#ifndef __YAFFS_MTDIF_H__
  13422. +#define __YAFFS_MTDIF_H__
  13423. +
  13424. +#include "yaffs_guts.h"
  13425. +
  13426. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
  13427. +extern struct nand_oobinfo yaffs_oobinfo;
  13428. +extern struct nand_oobinfo yaffs_noeccinfo;
  13429. +#endif
  13430. +
  13431. +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
  13432. + const __u8 *data, const yaffs_Spare *spare);
  13433. +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
  13434. + yaffs_Spare *spare);
  13435. +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber);
  13436. +int nandmtd_InitialiseNAND(yaffs_Device *dev);
  13437. +#endif
  13438. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_nand.c linux-2.6.34/fs/yaffs2/yaffs_nand.c
  13439. --- linux-2.6.34.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  13440. +++ linux-2.6.34/fs/yaffs2/yaffs_nand.c 2010-05-20 20:16:22.311614862 +0200
  13441. @@ -0,0 +1,135 @@
  13442. +/*
  13443. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13444. + *
  13445. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13446. + * for Toby Churchill Ltd and Brightstar Engineering
  13447. + *
  13448. + * Created by Charles Manning <charles@aleph1.co.uk>
  13449. + *
  13450. + * This program is free software; you can redistribute it and/or modify
  13451. + * it under the terms of the GNU General Public License version 2 as
  13452. + * published by the Free Software Foundation.
  13453. + */
  13454. +
  13455. +const char *yaffs_nand_c_version =
  13456. + "$Id: yaffs_nand.c,v 1.10 2009-03-06 17:20:54 wookey Exp $";
  13457. +
  13458. +#include "yaffs_nand.h"
  13459. +#include "yaffs_tagscompat.h"
  13460. +#include "yaffs_tagsvalidity.h"
  13461. +
  13462. +#include "yaffs_getblockinfo.h"
  13463. +
  13464. +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13465. + __u8 *buffer,
  13466. + yaffs_ExtendedTags *tags)
  13467. +{
  13468. + int result;
  13469. + yaffs_ExtendedTags localTags;
  13470. +
  13471. + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
  13472. +
  13473. + /* If there are no tags provided, use local tags to get prioritised gc working */
  13474. + if (!tags)
  13475. + tags = &localTags;
  13476. +
  13477. + if (dev->readChunkWithTagsFromNAND)
  13478. + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
  13479. + tags);
  13480. + else
  13481. + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
  13482. + realignedChunkInNAND,
  13483. + buffer,
  13484. + tags);
  13485. + if (tags &&
  13486. + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR) {
  13487. +
  13488. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
  13489. + yaffs_HandleChunkError(dev, bi);
  13490. + }
  13491. +
  13492. + return result;
  13493. +}
  13494. +
  13495. +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  13496. + int chunkInNAND,
  13497. + const __u8 *buffer,
  13498. + yaffs_ExtendedTags *tags)
  13499. +{
  13500. + chunkInNAND -= dev->chunkOffset;
  13501. +
  13502. +
  13503. + if (tags) {
  13504. + tags->sequenceNumber = dev->sequenceNumber;
  13505. + tags->chunkUsed = 1;
  13506. + if (!yaffs_ValidateTags(tags)) {
  13507. + T(YAFFS_TRACE_ERROR,
  13508. + (TSTR("Writing uninitialised tags" TENDSTR)));
  13509. + YBUG();
  13510. + }
  13511. + T(YAFFS_TRACE_WRITE,
  13512. + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
  13513. + tags->objectId, tags->chunkId));
  13514. + } else {
  13515. + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
  13516. + YBUG();
  13517. + }
  13518. +
  13519. + if (dev->writeChunkWithTagsToNAND)
  13520. + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
  13521. + tags);
  13522. + else
  13523. + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
  13524. + chunkInNAND,
  13525. + buffer,
  13526. + tags);
  13527. +}
  13528. +
  13529. +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
  13530. +{
  13531. + blockNo -= dev->blockOffset;
  13532. +
  13533. +;
  13534. + if (dev->markNANDBlockBad)
  13535. + return dev->markNANDBlockBad(dev, blockNo);
  13536. + else
  13537. + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
  13538. +}
  13539. +
  13540. +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
  13541. + int blockNo,
  13542. + yaffs_BlockState *state,
  13543. + __u32 *sequenceNumber)
  13544. +{
  13545. + blockNo -= dev->blockOffset;
  13546. +
  13547. + if (dev->queryNANDBlock)
  13548. + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
  13549. + else
  13550. + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
  13551. + state,
  13552. + sequenceNumber);
  13553. +}
  13554. +
  13555. +
  13556. +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13557. + int blockInNAND)
  13558. +{
  13559. + int result;
  13560. +
  13561. + blockInNAND -= dev->blockOffset;
  13562. +
  13563. +
  13564. + dev->nBlockErasures++;
  13565. + result = dev->eraseBlockInNAND(dev, blockInNAND);
  13566. +
  13567. + return result;
  13568. +}
  13569. +
  13570. +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
  13571. +{
  13572. + return dev->initialiseNAND(dev);
  13573. +}
  13574. +
  13575. +
  13576. +
  13577. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.34/fs/yaffs2/yaffs_nandemul2k.h
  13578. --- linux-2.6.34.orig/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
  13579. +++ linux-2.6.34/fs/yaffs2/yaffs_nandemul2k.h 2010-05-20 20:16:22.311614862 +0200
  13580. @@ -0,0 +1,39 @@
  13581. +/*
  13582. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13583. + *
  13584. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13585. + * for Toby Churchill Ltd and Brightstar Engineering
  13586. + *
  13587. + * Created by Charles Manning <charles@aleph1.co.uk>
  13588. + *
  13589. + * This program is free software; you can redistribute it and/or modify
  13590. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13591. + * published by the Free Software Foundation.
  13592. + *
  13593. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13594. + */
  13595. +
  13596. +/* Interface to emulated NAND functions (2k page size) */
  13597. +
  13598. +#ifndef __YAFFS_NANDEMUL2K_H__
  13599. +#define __YAFFS_NANDEMUL2K_H__
  13600. +
  13601. +#include "yaffs_guts.h"
  13602. +
  13603. +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
  13604. + int chunkInNAND, const __u8 *data,
  13605. + const yaffs_ExtendedTags *tags);
  13606. +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
  13607. + int chunkInNAND, __u8 *data,
  13608. + yaffs_ExtendedTags *tags);
  13609. +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  13610. +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13611. + yaffs_BlockState *state, __u32 *sequenceNumber);
  13612. +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13613. + int blockInNAND);
  13614. +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
  13615. +int nandemul2k_GetBytesPerChunk(void);
  13616. +int nandemul2k_GetChunksPerBlock(void);
  13617. +int nandemul2k_GetNumberOfBlocks(void);
  13618. +
  13619. +#endif
  13620. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_nand.h linux-2.6.34/fs/yaffs2/yaffs_nand.h
  13621. --- linux-2.6.34.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  13622. +++ linux-2.6.34/fs/yaffs2/yaffs_nand.h 2010-05-20 20:16:22.311614862 +0200
  13623. @@ -0,0 +1,44 @@
  13624. +/*
  13625. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13626. + *
  13627. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13628. + * for Toby Churchill Ltd and Brightstar Engineering
  13629. + *
  13630. + * Created by Charles Manning <charles@aleph1.co.uk>
  13631. + *
  13632. + * This program is free software; you can redistribute it and/or modify
  13633. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13634. + * published by the Free Software Foundation.
  13635. + *
  13636. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13637. + */
  13638. +
  13639. +#ifndef __YAFFS_NAND_H__
  13640. +#define __YAFFS_NAND_H__
  13641. +#include "yaffs_guts.h"
  13642. +
  13643. +
  13644. +
  13645. +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13646. + __u8 *buffer,
  13647. + yaffs_ExtendedTags *tags);
  13648. +
  13649. +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  13650. + int chunkInNAND,
  13651. + const __u8 *buffer,
  13652. + yaffs_ExtendedTags *tags);
  13653. +
  13654. +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
  13655. +
  13656. +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
  13657. + int blockNo,
  13658. + yaffs_BlockState *state,
  13659. + unsigned *sequenceNumber);
  13660. +
  13661. +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13662. + int blockInNAND);
  13663. +
  13664. +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
  13665. +
  13666. +#endif
  13667. +
  13668. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags1.c linux-2.6.34/fs/yaffs2/yaffs_packedtags1.c
  13669. --- linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  13670. +++ linux-2.6.34/fs/yaffs2/yaffs_packedtags1.c 2010-05-20 20:16:22.311614862 +0200
  13671. @@ -0,0 +1,50 @@
  13672. +/*
  13673. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13674. + *
  13675. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13676. + * for Toby Churchill Ltd and Brightstar Engineering
  13677. + *
  13678. + * Created by Charles Manning <charles@aleph1.co.uk>
  13679. + *
  13680. + * This program is free software; you can redistribute it and/or modify
  13681. + * it under the terms of the GNU General Public License version 2 as
  13682. + * published by the Free Software Foundation.
  13683. + */
  13684. +
  13685. +#include "yaffs_packedtags1.h"
  13686. +#include "yportenv.h"
  13687. +
  13688. +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t)
  13689. +{
  13690. + pt->chunkId = t->chunkId;
  13691. + pt->serialNumber = t->serialNumber;
  13692. + pt->byteCount = t->byteCount;
  13693. + pt->objectId = t->objectId;
  13694. + pt->ecc = 0;
  13695. + pt->deleted = (t->chunkDeleted) ? 0 : 1;
  13696. + pt->unusedStuff = 0;
  13697. + pt->shouldBeFF = 0xFFFFFFFF;
  13698. +
  13699. +}
  13700. +
  13701. +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt)
  13702. +{
  13703. + static const __u8 allFF[] =
  13704. + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  13705. +0xff };
  13706. +
  13707. + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
  13708. + t->blockBad = 0;
  13709. + if (pt->shouldBeFF != 0xFFFFFFFF)
  13710. + t->blockBad = 1;
  13711. + t->chunkUsed = 1;
  13712. + t->objectId = pt->objectId;
  13713. + t->chunkId = pt->chunkId;
  13714. + t->byteCount = pt->byteCount;
  13715. + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13716. + t->chunkDeleted = (pt->deleted) ? 0 : 1;
  13717. + t->serialNumber = pt->serialNumber;
  13718. + } else {
  13719. + memset(t, 0, sizeof(yaffs_ExtendedTags));
  13720. + }
  13721. +}
  13722. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags1.h linux-2.6.34/fs/yaffs2/yaffs_packedtags1.h
  13723. --- linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  13724. +++ linux-2.6.34/fs/yaffs2/yaffs_packedtags1.h 2010-05-20 20:16:22.311614862 +0200
  13725. @@ -0,0 +1,37 @@
  13726. +/*
  13727. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13728. + *
  13729. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13730. + * for Toby Churchill Ltd and Brightstar Engineering
  13731. + *
  13732. + * Created by Charles Manning <charles@aleph1.co.uk>
  13733. + *
  13734. + * This program is free software; you can redistribute it and/or modify
  13735. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13736. + * published by the Free Software Foundation.
  13737. + *
  13738. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13739. + */
  13740. +
  13741. +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
  13742. +
  13743. +#ifndef __YAFFS_PACKEDTAGS1_H__
  13744. +#define __YAFFS_PACKEDTAGS1_H__
  13745. +
  13746. +#include "yaffs_guts.h"
  13747. +
  13748. +typedef struct {
  13749. + unsigned chunkId:20;
  13750. + unsigned serialNumber:2;
  13751. + unsigned byteCount:10;
  13752. + unsigned objectId:18;
  13753. + unsigned ecc:12;
  13754. + unsigned deleted:1;
  13755. + unsigned unusedStuff:1;
  13756. + unsigned shouldBeFF;
  13757. +
  13758. +} yaffs_PackedTags1;
  13759. +
  13760. +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t);
  13761. +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt);
  13762. +#endif
  13763. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags2.c linux-2.6.34/fs/yaffs2/yaffs_packedtags2.c
  13764. --- linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  13765. +++ linux-2.6.34/fs/yaffs2/yaffs_packedtags2.c 2010-05-20 20:16:22.311614862 +0200
  13766. @@ -0,0 +1,206 @@
  13767. +/*
  13768. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13769. + *
  13770. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13771. + * for Toby Churchill Ltd and Brightstar Engineering
  13772. + *
  13773. + * Created by Charles Manning <charles@aleph1.co.uk>
  13774. + *
  13775. + * This program is free software; you can redistribute it and/or modify
  13776. + * it under the terms of the GNU General Public License version 2 as
  13777. + * published by the Free Software Foundation.
  13778. + */
  13779. +
  13780. +#include "yaffs_packedtags2.h"
  13781. +#include "yportenv.h"
  13782. +#include "yaffs_tagsvalidity.h"
  13783. +
  13784. +/* This code packs a set of extended tags into a binary structure for
  13785. + * NAND storage
  13786. + */
  13787. +
  13788. +/* Some of the information is "extra" struff which can be packed in to
  13789. + * speed scanning
  13790. + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
  13791. + */
  13792. +
  13793. +/* Extra flags applied to chunkId */
  13794. +
  13795. +#define EXTRA_HEADER_INFO_FLAG 0x80000000
  13796. +#define EXTRA_SHRINK_FLAG 0x40000000
  13797. +#define EXTRA_SHADOWS_FLAG 0x20000000
  13798. +#define EXTRA_SPARE_FLAGS 0x10000000
  13799. +
  13800. +#define ALL_EXTRA_FLAGS 0xF0000000
  13801. +
  13802. +/* Also, the top 4 bits of the object Id are set to the object type. */
  13803. +#define EXTRA_OBJECT_TYPE_SHIFT (28)
  13804. +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
  13805. +
  13806. +
  13807. +static void yaffs_DumpPackedTags2TagsPart(const yaffs_PackedTags2TagsPart *ptt)
  13808. +{
  13809. + T(YAFFS_TRACE_MTD,
  13810. + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
  13811. + ptt->objectId, ptt->chunkId, ptt->byteCount,
  13812. + ptt->sequenceNumber));
  13813. +}
  13814. +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
  13815. +{
  13816. + yaffs_DumpPackedTags2TagsPart(&pt->t);
  13817. +}
  13818. +
  13819. +static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
  13820. +{
  13821. + T(YAFFS_TRACE_MTD,
  13822. + (TSTR
  13823. + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
  13824. + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
  13825. + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
  13826. + t->sequenceNumber));
  13827. +
  13828. +}
  13829. +
  13830. +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
  13831. + const yaffs_ExtendedTags *t)
  13832. +{
  13833. + ptt->chunkId = t->chunkId;
  13834. + ptt->sequenceNumber = t->sequenceNumber;
  13835. + ptt->byteCount = t->byteCount;
  13836. + ptt->objectId = t->objectId;
  13837. +
  13838. + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
  13839. + /* Store the extra header info instead */
  13840. + /* We save the parent object in the chunkId */
  13841. + ptt->chunkId = EXTRA_HEADER_INFO_FLAG
  13842. + | t->extraParentObjectId;
  13843. + if (t->extraIsShrinkHeader)
  13844. + ptt->chunkId |= EXTRA_SHRINK_FLAG;
  13845. + if (t->extraShadows)
  13846. + ptt->chunkId |= EXTRA_SHADOWS_FLAG;
  13847. +
  13848. + ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
  13849. + ptt->objectId |=
  13850. + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
  13851. +
  13852. + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
  13853. + ptt->byteCount = t->extraEquivalentObjectId;
  13854. + else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
  13855. + ptt->byteCount = t->extraFileLength;
  13856. + else
  13857. + ptt->byteCount = 0;
  13858. + }
  13859. +
  13860. + yaffs_DumpPackedTags2TagsPart(ptt);
  13861. + yaffs_DumpTags2(t);
  13862. +}
  13863. +
  13864. +
  13865. +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
  13866. +{
  13867. + yaffs_PackTags2TagsPart(&pt->t, t);
  13868. +
  13869. +#ifndef YAFFS_IGNORE_TAGS_ECC
  13870. + {
  13871. + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
  13872. + sizeof(yaffs_PackedTags2TagsPart),
  13873. + &pt->ecc);
  13874. + }
  13875. +#endif
  13876. +}
  13877. +
  13878. +
  13879. +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
  13880. + yaffs_PackedTags2TagsPart *ptt)
  13881. +{
  13882. +
  13883. + memset(t, 0, sizeof(yaffs_ExtendedTags));
  13884. +
  13885. + yaffs_InitialiseTags(t);
  13886. +
  13887. + if (ptt->sequenceNumber != 0xFFFFFFFF) {
  13888. + t->blockBad = 0;
  13889. + t->chunkUsed = 1;
  13890. + t->objectId = ptt->objectId;
  13891. + t->chunkId = ptt->chunkId;
  13892. + t->byteCount = ptt->byteCount;
  13893. + t->chunkDeleted = 0;
  13894. + t->serialNumber = 0;
  13895. + t->sequenceNumber = ptt->sequenceNumber;
  13896. +
  13897. + /* Do extra header info stuff */
  13898. +
  13899. + if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
  13900. + t->chunkId = 0;
  13901. + t->byteCount = 0;
  13902. +
  13903. + t->extraHeaderInfoAvailable = 1;
  13904. + t->extraParentObjectId =
  13905. + ptt->chunkId & (~(ALL_EXTRA_FLAGS));
  13906. + t->extraIsShrinkHeader =
  13907. + (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
  13908. + t->extraShadows =
  13909. + (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
  13910. + t->extraObjectType =
  13911. + ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
  13912. + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
  13913. +
  13914. + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
  13915. + t->extraEquivalentObjectId = ptt->byteCount;
  13916. + else
  13917. + t->extraFileLength = ptt->byteCount;
  13918. + }
  13919. + }
  13920. +
  13921. + yaffs_DumpPackedTags2TagsPart(ptt);
  13922. + yaffs_DumpTags2(t);
  13923. +
  13924. +}
  13925. +
  13926. +
  13927. +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
  13928. +{
  13929. +
  13930. + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13931. +
  13932. + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
  13933. + /* Page is in use */
  13934. +#ifndef YAFFS_IGNORE_TAGS_ECC
  13935. + {
  13936. + yaffs_ECCOther ecc;
  13937. + int result;
  13938. + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
  13939. + sizeof
  13940. + (yaffs_PackedTags2TagsPart),
  13941. + &ecc);
  13942. + result =
  13943. + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
  13944. + sizeof
  13945. + (yaffs_PackedTags2TagsPart),
  13946. + &pt->ecc, &ecc);
  13947. + switch (result) {
  13948. + case 0:
  13949. + eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13950. + break;
  13951. + case 1:
  13952. + eccResult = YAFFS_ECC_RESULT_FIXED;
  13953. + break;
  13954. + case -1:
  13955. + eccResult = YAFFS_ECC_RESULT_UNFIXED;
  13956. + break;
  13957. + default:
  13958. + eccResult = YAFFS_ECC_RESULT_UNKNOWN;
  13959. + }
  13960. + }
  13961. +#endif
  13962. + }
  13963. +
  13964. + yaffs_UnpackTags2TagsPart(t, &pt->t);
  13965. +
  13966. + t->eccResult = eccResult;
  13967. +
  13968. + yaffs_DumpPackedTags2(pt);
  13969. + yaffs_DumpTags2(t);
  13970. +
  13971. +}
  13972. +
  13973. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags2.h linux-2.6.34/fs/yaffs2/yaffs_packedtags2.h
  13974. --- linux-2.6.34.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  13975. +++ linux-2.6.34/fs/yaffs2/yaffs_packedtags2.h 2010-05-20 20:16:22.311614862 +0200
  13976. @@ -0,0 +1,43 @@
  13977. +/*
  13978. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13979. + *
  13980. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13981. + * for Toby Churchill Ltd and Brightstar Engineering
  13982. + *
  13983. + * Created by Charles Manning <charles@aleph1.co.uk>
  13984. + *
  13985. + * This program is free software; you can redistribute it and/or modify
  13986. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13987. + * published by the Free Software Foundation.
  13988. + *
  13989. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13990. + */
  13991. +
  13992. +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
  13993. +
  13994. +#ifndef __YAFFS_PACKEDTAGS2_H__
  13995. +#define __YAFFS_PACKEDTAGS2_H__
  13996. +
  13997. +#include "yaffs_guts.h"
  13998. +#include "yaffs_ecc.h"
  13999. +
  14000. +typedef struct {
  14001. + unsigned sequenceNumber;
  14002. + unsigned objectId;
  14003. + unsigned chunkId;
  14004. + unsigned byteCount;
  14005. +} yaffs_PackedTags2TagsPart;
  14006. +
  14007. +typedef struct {
  14008. + yaffs_PackedTags2TagsPart t;
  14009. + yaffs_ECCOther ecc;
  14010. +} yaffs_PackedTags2;
  14011. +
  14012. +/* Full packed tags with ECC, used for oob tags */
  14013. +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t);
  14014. +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt);
  14015. +
  14016. +/* Only the tags part (no ECC for use with inband tags */
  14017. +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *pt, const yaffs_ExtendedTags *t);
  14018. +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t, yaffs_PackedTags2TagsPart *pt);
  14019. +#endif
  14020. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_qsort.c linux-2.6.34/fs/yaffs2/yaffs_qsort.c
  14021. --- linux-2.6.34.orig/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
  14022. +++ linux-2.6.34/fs/yaffs2/yaffs_qsort.c 2010-05-20 20:16:22.311614862 +0200
  14023. @@ -0,0 +1,163 @@
  14024. +/*
  14025. + * Copyright (c) 1992, 1993
  14026. + * The Regents of the University of California. All rights reserved.
  14027. + *
  14028. + * Redistribution and use in source and binary forms, with or without
  14029. + * modification, are permitted provided that the following conditions
  14030. + * are met:
  14031. + * 1. Redistributions of source code must retain the above copyright
  14032. + * notice, this list of conditions and the following disclaimer.
  14033. + * 2. Redistributions in binary form must reproduce the above copyright
  14034. + * notice, this list of conditions and the following disclaimer in the
  14035. + * documentation and/or other materials provided with the distribution.
  14036. + * 3. Neither the name of the University nor the names of its contributors
  14037. + * may be used to endorse or promote products derived from this software
  14038. + * without specific prior written permission.
  14039. + *
  14040. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  14041. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14042. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14043. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  14044. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  14045. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  14046. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  14047. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14048. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  14049. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  14050. + * SUCH DAMAGE.
  14051. + */
  14052. +
  14053. +#include "yportenv.h"
  14054. +/* #include <linux/string.h> */
  14055. +
  14056. +/*
  14057. + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
  14058. + */
  14059. +#define swapcode(TYPE, parmi, parmj, n) do { \
  14060. + long i = (n) / sizeof (TYPE); \
  14061. + register TYPE *pi = (TYPE *) (parmi); \
  14062. + register TYPE *pj = (TYPE *) (parmj); \
  14063. + do { \
  14064. + register TYPE t = *pi; \
  14065. + *pi++ = *pj; \
  14066. + *pj++ = t; \
  14067. + } while (--i > 0); \
  14068. +} while (0)
  14069. +
  14070. +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
  14071. + es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
  14072. +
  14073. +static __inline void
  14074. +swapfunc(char *a, char *b, int n, int swaptype)
  14075. +{
  14076. + if (swaptype <= 1)
  14077. + swapcode(long, a, b, n);
  14078. + else
  14079. + swapcode(char, a, b, n);
  14080. +}
  14081. +
  14082. +#define yswap(a, b) do { \
  14083. + if (swaptype == 0) { \
  14084. + long t = *(long *)(a); \
  14085. + *(long *)(a) = *(long *)(b); \
  14086. + *(long *)(b) = t; \
  14087. + } else \
  14088. + swapfunc(a, b, es, swaptype); \
  14089. +} while (0)
  14090. +
  14091. +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
  14092. +
  14093. +static __inline char *
  14094. +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
  14095. +{
  14096. + return cmp(a, b) < 0 ?
  14097. + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
  14098. + : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
  14099. +}
  14100. +
  14101. +#ifndef min
  14102. +#define min(a, b) (((a) < (b)) ? (a) : (b))
  14103. +#endif
  14104. +
  14105. +void
  14106. +yaffs_qsort(void *aa, size_t n, size_t es,
  14107. + int (*cmp)(const void *, const void *))
  14108. +{
  14109. + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  14110. + int d, r, swaptype, swap_cnt;
  14111. + register char *a = aa;
  14112. +
  14113. +loop: SWAPINIT(a, es);
  14114. + swap_cnt = 0;
  14115. + if (n < 7) {
  14116. + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
  14117. + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  14118. + pl -= es)
  14119. + yswap(pl, pl - es);
  14120. + return;
  14121. + }
  14122. + pm = (char *)a + (n / 2) * es;
  14123. + if (n > 7) {
  14124. + pl = (char *)a;
  14125. + pn = (char *)a + (n - 1) * es;
  14126. + if (n > 40) {
  14127. + d = (n / 8) * es;
  14128. + pl = med3(pl, pl + d, pl + 2 * d, cmp);
  14129. + pm = med3(pm - d, pm, pm + d, cmp);
  14130. + pn = med3(pn - 2 * d, pn - d, pn, cmp);
  14131. + }
  14132. + pm = med3(pl, pm, pn, cmp);
  14133. + }
  14134. + yswap(a, pm);
  14135. + pa = pb = (char *)a + es;
  14136. +
  14137. + pc = pd = (char *)a + (n - 1) * es;
  14138. + for (;;) {
  14139. + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
  14140. + if (r == 0) {
  14141. + swap_cnt = 1;
  14142. + yswap(pa, pb);
  14143. + pa += es;
  14144. + }
  14145. + pb += es;
  14146. + }
  14147. + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
  14148. + if (r == 0) {
  14149. + swap_cnt = 1;
  14150. + yswap(pc, pd);
  14151. + pd -= es;
  14152. + }
  14153. + pc -= es;
  14154. + }
  14155. + if (pb > pc)
  14156. + break;
  14157. + yswap(pb, pc);
  14158. + swap_cnt = 1;
  14159. + pb += es;
  14160. + pc -= es;
  14161. + }
  14162. + if (swap_cnt == 0) { /* Switch to insertion sort */
  14163. + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
  14164. + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  14165. + pl -= es)
  14166. + yswap(pl, pl - es);
  14167. + return;
  14168. + }
  14169. +
  14170. + pn = (char *)a + n * es;
  14171. + r = min(pa - (char *)a, pb - pa);
  14172. + vecswap(a, pb - r, r);
  14173. + r = min((long)(pd - pc), (long)(pn - pd - es));
  14174. + vecswap(pb, pn - r, r);
  14175. + r = pb - pa;
  14176. + if (r > es)
  14177. + yaffs_qsort(a, r / es, es, cmp);
  14178. + r = pd - pc;
  14179. + if (r > es) {
  14180. + /* Iterate rather than recurse to save stack space */
  14181. + a = pn - r;
  14182. + n = r / es;
  14183. + goto loop;
  14184. + }
  14185. +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
  14186. +}
  14187. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_qsort.h linux-2.6.34/fs/yaffs2/yaffs_qsort.h
  14188. --- linux-2.6.34.orig/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
  14189. +++ linux-2.6.34/fs/yaffs2/yaffs_qsort.h 2010-05-20 20:16:22.311614862 +0200
  14190. @@ -0,0 +1,23 @@
  14191. +/*
  14192. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14193. + *
  14194. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14195. + * for Toby Churchill Ltd and Brightstar Engineering
  14196. + *
  14197. + * Created by Charles Manning <charles@aleph1.co.uk>
  14198. + *
  14199. + * This program is free software; you can redistribute it and/or modify
  14200. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14201. + * published by the Free Software Foundation.
  14202. + *
  14203. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14204. + */
  14205. +
  14206. +
  14207. +#ifndef __YAFFS_QSORT_H__
  14208. +#define __YAFFS_QSORT_H__
  14209. +
  14210. +extern void yaffs_qsort(void *const base, size_t total_elems, size_t size,
  14211. + int (*cmp)(const void *, const void *));
  14212. +
  14213. +#endif
  14214. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_tagscompat.c linux-2.6.34/fs/yaffs2/yaffs_tagscompat.c
  14215. --- linux-2.6.34.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  14216. +++ linux-2.6.34/fs/yaffs2/yaffs_tagscompat.c 2010-05-20 20:16:22.311614862 +0200
  14217. @@ -0,0 +1,541 @@
  14218. +/*
  14219. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14220. + *
  14221. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14222. + * for Toby Churchill Ltd and Brightstar Engineering
  14223. + *
  14224. + * Created by Charles Manning <charles@aleph1.co.uk>
  14225. + *
  14226. + * This program is free software; you can redistribute it and/or modify
  14227. + * it under the terms of the GNU General Public License version 2 as
  14228. + * published by the Free Software Foundation.
  14229. + */
  14230. +
  14231. +#include "yaffs_guts.h"
  14232. +#include "yaffs_tagscompat.h"
  14233. +#include "yaffs_ecc.h"
  14234. +#include "yaffs_getblockinfo.h"
  14235. +
  14236. +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
  14237. +#ifdef NOTYET
  14238. +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
  14239. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  14240. + const __u8 *data,
  14241. + const yaffs_Spare *spare);
  14242. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  14243. + const yaffs_Spare *spare);
  14244. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
  14245. +#endif
  14246. +
  14247. +static const char yaffs_countBitsTable[256] = {
  14248. + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  14249. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14250. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14251. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14252. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14253. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14254. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14255. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14256. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14257. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14258. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14259. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14260. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14261. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14262. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14263. + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  14264. +};
  14265. +
  14266. +int yaffs_CountBits(__u8 x)
  14267. +{
  14268. + int retVal;
  14269. + retVal = yaffs_countBitsTable[x];
  14270. + return retVal;
  14271. +}
  14272. +
  14273. +/********** Tags ECC calculations *********/
  14274. +
  14275. +void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
  14276. +{
  14277. + yaffs_ECCCalculate(data, spare->ecc1);
  14278. + yaffs_ECCCalculate(&data[256], spare->ecc2);
  14279. +}
  14280. +
  14281. +void yaffs_CalcTagsECC(yaffs_Tags *tags)
  14282. +{
  14283. + /* Calculate an ecc */
  14284. +
  14285. + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
  14286. + unsigned i, j;
  14287. + unsigned ecc = 0;
  14288. + unsigned bit = 0;
  14289. +
  14290. + tags->ecc = 0;
  14291. +
  14292. + for (i = 0; i < 8; i++) {
  14293. + for (j = 1; j & 0xff; j <<= 1) {
  14294. + bit++;
  14295. + if (b[i] & j)
  14296. + ecc ^= bit;
  14297. + }
  14298. + }
  14299. +
  14300. + tags->ecc = ecc;
  14301. +
  14302. +}
  14303. +
  14304. +int yaffs_CheckECCOnTags(yaffs_Tags *tags)
  14305. +{
  14306. + unsigned ecc = tags->ecc;
  14307. +
  14308. + yaffs_CalcTagsECC(tags);
  14309. +
  14310. + ecc ^= tags->ecc;
  14311. +
  14312. + if (ecc && ecc <= 64) {
  14313. + /* TODO: Handle the failure better. Retire? */
  14314. + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
  14315. +
  14316. + ecc--;
  14317. +
  14318. + b[ecc / 8] ^= (1 << (ecc & 7));
  14319. +
  14320. + /* Now recvalc the ecc */
  14321. + yaffs_CalcTagsECC(tags);
  14322. +
  14323. + return 1; /* recovered error */
  14324. + } else if (ecc) {
  14325. + /* Wierd ecc failure value */
  14326. + /* TODO Need to do somethiong here */
  14327. + return -1; /* unrecovered error */
  14328. + }
  14329. +
  14330. + return 0;
  14331. +}
  14332. +
  14333. +/********** Tags **********/
  14334. +
  14335. +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
  14336. + yaffs_Tags *tagsPtr)
  14337. +{
  14338. + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
  14339. +
  14340. + yaffs_CalcTagsECC(tagsPtr);
  14341. +
  14342. + sparePtr->tagByte0 = tu->asBytes[0];
  14343. + sparePtr->tagByte1 = tu->asBytes[1];
  14344. + sparePtr->tagByte2 = tu->asBytes[2];
  14345. + sparePtr->tagByte3 = tu->asBytes[3];
  14346. + sparePtr->tagByte4 = tu->asBytes[4];
  14347. + sparePtr->tagByte5 = tu->asBytes[5];
  14348. + sparePtr->tagByte6 = tu->asBytes[6];
  14349. + sparePtr->tagByte7 = tu->asBytes[7];
  14350. +}
  14351. +
  14352. +static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
  14353. + yaffs_Tags *tagsPtr)
  14354. +{
  14355. + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
  14356. + int result;
  14357. +
  14358. + tu->asBytes[0] = sparePtr->tagByte0;
  14359. + tu->asBytes[1] = sparePtr->tagByte1;
  14360. + tu->asBytes[2] = sparePtr->tagByte2;
  14361. + tu->asBytes[3] = sparePtr->tagByte3;
  14362. + tu->asBytes[4] = sparePtr->tagByte4;
  14363. + tu->asBytes[5] = sparePtr->tagByte5;
  14364. + tu->asBytes[6] = sparePtr->tagByte6;
  14365. + tu->asBytes[7] = sparePtr->tagByte7;
  14366. +
  14367. + result = yaffs_CheckECCOnTags(tagsPtr);
  14368. + if (result > 0)
  14369. + dev->tagsEccFixed++;
  14370. + else if (result < 0)
  14371. + dev->tagsEccUnfixed++;
  14372. +}
  14373. +
  14374. +static void yaffs_SpareInitialise(yaffs_Spare *spare)
  14375. +{
  14376. + memset(spare, 0xFF, sizeof(yaffs_Spare));
  14377. +}
  14378. +
  14379. +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
  14380. + int chunkInNAND, const __u8 *data,
  14381. + yaffs_Spare *spare)
  14382. +{
  14383. + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
  14384. + T(YAFFS_TRACE_ERROR,
  14385. + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
  14386. + chunkInNAND));
  14387. + return YAFFS_FAIL;
  14388. + }
  14389. +
  14390. + dev->nPageWrites++;
  14391. + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
  14392. +}
  14393. +
  14394. +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
  14395. + int chunkInNAND,
  14396. + __u8 *data,
  14397. + yaffs_Spare *spare,
  14398. + yaffs_ECCResult *eccResult,
  14399. + int doErrorCorrection)
  14400. +{
  14401. + int retVal;
  14402. + yaffs_Spare localSpare;
  14403. +
  14404. + dev->nPageReads++;
  14405. +
  14406. + if (!spare && data) {
  14407. + /* If we don't have a real spare, then we use a local one. */
  14408. + /* Need this for the calculation of the ecc */
  14409. + spare = &localSpare;
  14410. + }
  14411. +
  14412. + if (!dev->useNANDECC) {
  14413. + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
  14414. + if (data && doErrorCorrection) {
  14415. + /* Do ECC correction */
  14416. + /* Todo handle any errors */
  14417. + int eccResult1, eccResult2;
  14418. + __u8 calcEcc[3];
  14419. +
  14420. + yaffs_ECCCalculate(data, calcEcc);
  14421. + eccResult1 =
  14422. + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
  14423. + yaffs_ECCCalculate(&data[256], calcEcc);
  14424. + eccResult2 =
  14425. + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
  14426. +
  14427. + if (eccResult1 > 0) {
  14428. + T(YAFFS_TRACE_ERROR,
  14429. + (TSTR
  14430. + ("**>>yaffs ecc error fix performed on chunk %d:0"
  14431. + TENDSTR), chunkInNAND));
  14432. + dev->eccFixed++;
  14433. + } else if (eccResult1 < 0) {
  14434. + T(YAFFS_TRACE_ERROR,
  14435. + (TSTR
  14436. + ("**>>yaffs ecc error unfixed on chunk %d:0"
  14437. + TENDSTR), chunkInNAND));
  14438. + dev->eccUnfixed++;
  14439. + }
  14440. +
  14441. + if (eccResult2 > 0) {
  14442. + T(YAFFS_TRACE_ERROR,
  14443. + (TSTR
  14444. + ("**>>yaffs ecc error fix performed on chunk %d:1"
  14445. + TENDSTR), chunkInNAND));
  14446. + dev->eccFixed++;
  14447. + } else if (eccResult2 < 0) {
  14448. + T(YAFFS_TRACE_ERROR,
  14449. + (TSTR
  14450. + ("**>>yaffs ecc error unfixed on chunk %d:1"
  14451. + TENDSTR), chunkInNAND));
  14452. + dev->eccUnfixed++;
  14453. + }
  14454. +
  14455. + if (eccResult1 || eccResult2) {
  14456. + /* We had a data problem on this page */
  14457. + yaffs_HandleReadDataError(dev, chunkInNAND);
  14458. + }
  14459. +
  14460. + if (eccResult1 < 0 || eccResult2 < 0)
  14461. + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
  14462. + else if (eccResult1 > 0 || eccResult2 > 0)
  14463. + *eccResult = YAFFS_ECC_RESULT_FIXED;
  14464. + else
  14465. + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  14466. + }
  14467. + } else {
  14468. + /* Must allocate enough memory for spare+2*sizeof(int) */
  14469. + /* for ecc results from device. */
  14470. + struct yaffs_NANDSpare nspare;
  14471. +
  14472. + memset(&nspare, 0, sizeof(nspare));
  14473. +
  14474. + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
  14475. + (yaffs_Spare *) &nspare);
  14476. + memcpy(spare, &nspare, sizeof(yaffs_Spare));
  14477. + if (data && doErrorCorrection) {
  14478. + if (nspare.eccres1 > 0) {
  14479. + T(YAFFS_TRACE_ERROR,
  14480. + (TSTR
  14481. + ("**>>mtd ecc error fix performed on chunk %d:0"
  14482. + TENDSTR), chunkInNAND));
  14483. + } else if (nspare.eccres1 < 0) {
  14484. + T(YAFFS_TRACE_ERROR,
  14485. + (TSTR
  14486. + ("**>>mtd ecc error unfixed on chunk %d:0"
  14487. + TENDSTR), chunkInNAND));
  14488. + }
  14489. +
  14490. + if (nspare.eccres2 > 0) {
  14491. + T(YAFFS_TRACE_ERROR,
  14492. + (TSTR
  14493. + ("**>>mtd ecc error fix performed on chunk %d:1"
  14494. + TENDSTR), chunkInNAND));
  14495. + } else if (nspare.eccres2 < 0) {
  14496. + T(YAFFS_TRACE_ERROR,
  14497. + (TSTR
  14498. + ("**>>mtd ecc error unfixed on chunk %d:1"
  14499. + TENDSTR), chunkInNAND));
  14500. + }
  14501. +
  14502. + if (nspare.eccres1 || nspare.eccres2) {
  14503. + /* We had a data problem on this page */
  14504. + yaffs_HandleReadDataError(dev, chunkInNAND);
  14505. + }
  14506. +
  14507. + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
  14508. + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
  14509. + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
  14510. + *eccResult = YAFFS_ECC_RESULT_FIXED;
  14511. + else
  14512. + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  14513. +
  14514. + }
  14515. + }
  14516. + return retVal;
  14517. +}
  14518. +
  14519. +#ifdef NOTYET
  14520. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  14521. + int chunkInNAND)
  14522. +{
  14523. + static int init;
  14524. + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
  14525. + static __u8 data[YAFFS_BYTES_PER_CHUNK];
  14526. + /* Might as well always allocate the larger size for */
  14527. + /* dev->useNANDECC == true; */
  14528. + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
  14529. +
  14530. + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
  14531. +
  14532. + if (!init) {
  14533. + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
  14534. + init = 1;
  14535. + }
  14536. +
  14537. + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
  14538. + return YAFFS_FAIL;
  14539. + if (memcmp(cmpbuf, spare, 16))
  14540. + return YAFFS_FAIL;
  14541. +
  14542. + return YAFFS_OK;
  14543. +
  14544. +}
  14545. +#endif
  14546. +
  14547. +/*
  14548. + * Functions for robustisizing
  14549. + */
  14550. +
  14551. +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
  14552. +{
  14553. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  14554. +
  14555. + /* Mark the block for retirement */
  14556. + yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
  14557. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  14558. + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
  14559. +
  14560. + /* TODO:
  14561. + * Just do a garbage collection on the affected block
  14562. + * then retire the block
  14563. + * NB recursion
  14564. + */
  14565. +}
  14566. +
  14567. +#ifdef NOTYET
  14568. +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
  14569. +{
  14570. +}
  14571. +
  14572. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  14573. + const __u8 *data,
  14574. + const yaffs_Spare *spare)
  14575. +{
  14576. +}
  14577. +
  14578. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  14579. + const yaffs_Spare *spare)
  14580. +{
  14581. +}
  14582. +
  14583. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
  14584. +{
  14585. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  14586. +
  14587. + /* Mark the block for retirement */
  14588. + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
  14589. + /* Delete the chunk */
  14590. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  14591. +}
  14592. +
  14593. +static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
  14594. + const yaffs_Spare *s0, const yaffs_Spare *s1)
  14595. +{
  14596. +
  14597. + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
  14598. + s0->tagByte0 != s1->tagByte0 ||
  14599. + s0->tagByte1 != s1->tagByte1 ||
  14600. + s0->tagByte2 != s1->tagByte2 ||
  14601. + s0->tagByte3 != s1->tagByte3 ||
  14602. + s0->tagByte4 != s1->tagByte4 ||
  14603. + s0->tagByte5 != s1->tagByte5 ||
  14604. + s0->tagByte6 != s1->tagByte6 ||
  14605. + s0->tagByte7 != s1->tagByte7 ||
  14606. + s0->ecc1[0] != s1->ecc1[0] ||
  14607. + s0->ecc1[1] != s1->ecc1[1] ||
  14608. + s0->ecc1[2] != s1->ecc1[2] ||
  14609. + s0->ecc2[0] != s1->ecc2[0] ||
  14610. + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
  14611. + return 0;
  14612. + }
  14613. +
  14614. + return 1;
  14615. +}
  14616. +#endif /* NOTYET */
  14617. +
  14618. +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
  14619. + int chunkInNAND,
  14620. + const __u8 *data,
  14621. + const yaffs_ExtendedTags *eTags)
  14622. +{
  14623. + yaffs_Spare spare;
  14624. + yaffs_Tags tags;
  14625. +
  14626. + yaffs_SpareInitialise(&spare);
  14627. +
  14628. + if (eTags->chunkDeleted)
  14629. + spare.pageStatus = 0;
  14630. + else {
  14631. + tags.objectId = eTags->objectId;
  14632. + tags.chunkId = eTags->chunkId;
  14633. +
  14634. + tags.byteCountLSB = eTags->byteCount & 0x3ff;
  14635. +
  14636. + if (dev->nDataBytesPerChunk >= 1024)
  14637. + tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
  14638. + else
  14639. + tags.byteCountMSB = 3;
  14640. +
  14641. +
  14642. + tags.serialNumber = eTags->serialNumber;
  14643. +
  14644. + if (!dev->useNANDECC && data)
  14645. + yaffs_CalcECC(data, &spare);
  14646. +
  14647. + yaffs_LoadTagsIntoSpare(&spare, &tags);
  14648. +
  14649. + }
  14650. +
  14651. + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
  14652. +}
  14653. +
  14654. +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
  14655. + int chunkInNAND,
  14656. + __u8 *data,
  14657. + yaffs_ExtendedTags *eTags)
  14658. +{
  14659. +
  14660. + yaffs_Spare spare;
  14661. + yaffs_Tags tags;
  14662. + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
  14663. +
  14664. + static yaffs_Spare spareFF;
  14665. + static int init;
  14666. +
  14667. + if (!init) {
  14668. + memset(&spareFF, 0xFF, sizeof(spareFF));
  14669. + init = 1;
  14670. + }
  14671. +
  14672. + if (yaffs_ReadChunkFromNAND
  14673. + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
  14674. + /* eTags may be NULL */
  14675. + if (eTags) {
  14676. +
  14677. + int deleted =
  14678. + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
  14679. +
  14680. + eTags->chunkDeleted = deleted;
  14681. + eTags->eccResult = eccResult;
  14682. + eTags->blockBad = 0; /* We're reading it */
  14683. + /* therefore it is not a bad block */
  14684. + eTags->chunkUsed =
  14685. + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
  14686. + 0) ? 1 : 0;
  14687. +
  14688. + if (eTags->chunkUsed) {
  14689. + yaffs_GetTagsFromSpare(dev, &spare, &tags);
  14690. +
  14691. + eTags->objectId = tags.objectId;
  14692. + eTags->chunkId = tags.chunkId;
  14693. + eTags->byteCount = tags.byteCountLSB;
  14694. +
  14695. + if (dev->nDataBytesPerChunk >= 1024)
  14696. + eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
  14697. +
  14698. + eTags->serialNumber = tags.serialNumber;
  14699. + }
  14700. + }
  14701. +
  14702. + return YAFFS_OK;
  14703. + } else {
  14704. + return YAFFS_FAIL;
  14705. + }
  14706. +}
  14707. +
  14708. +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
  14709. + int blockInNAND)
  14710. +{
  14711. +
  14712. + yaffs_Spare spare;
  14713. +
  14714. + memset(&spare, 0xff, sizeof(yaffs_Spare));
  14715. +
  14716. + spare.blockStatus = 'Y';
  14717. +
  14718. + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
  14719. + &spare);
  14720. + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
  14721. + NULL, &spare);
  14722. +
  14723. + return YAFFS_OK;
  14724. +
  14725. +}
  14726. +
  14727. +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
  14728. + int blockNo,
  14729. + yaffs_BlockState *state,
  14730. + __u32 *sequenceNumber)
  14731. +{
  14732. +
  14733. + yaffs_Spare spare0, spare1;
  14734. + static yaffs_Spare spareFF;
  14735. + static int init;
  14736. + yaffs_ECCResult dummy;
  14737. +
  14738. + if (!init) {
  14739. + memset(&spareFF, 0xFF, sizeof(spareFF));
  14740. + init = 1;
  14741. + }
  14742. +
  14743. + *sequenceNumber = 0;
  14744. +
  14745. + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
  14746. + &spare0, &dummy, 1);
  14747. + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
  14748. + &spare1, &dummy, 1);
  14749. +
  14750. + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
  14751. + *state = YAFFS_BLOCK_STATE_DEAD;
  14752. + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
  14753. + *state = YAFFS_BLOCK_STATE_EMPTY;
  14754. + else
  14755. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  14756. +
  14757. + return YAFFS_OK;
  14758. +}
  14759. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_tagscompat.h linux-2.6.34/fs/yaffs2/yaffs_tagscompat.h
  14760. --- linux-2.6.34.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  14761. +++ linux-2.6.34/fs/yaffs2/yaffs_tagscompat.h 2010-05-20 20:16:22.311614862 +0200
  14762. @@ -0,0 +1,39 @@
  14763. +/*
  14764. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14765. + *
  14766. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14767. + * for Toby Churchill Ltd and Brightstar Engineering
  14768. + *
  14769. + * Created by Charles Manning <charles@aleph1.co.uk>
  14770. + *
  14771. + * This program is free software; you can redistribute it and/or modify
  14772. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14773. + * published by the Free Software Foundation.
  14774. + *
  14775. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14776. + */
  14777. +
  14778. +#ifndef __YAFFS_TAGSCOMPAT_H__
  14779. +#define __YAFFS_TAGSCOMPAT_H__
  14780. +
  14781. +#include "yaffs_guts.h"
  14782. +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
  14783. + int chunkInNAND,
  14784. + const __u8 *data,
  14785. + const yaffs_ExtendedTags *tags);
  14786. +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
  14787. + int chunkInNAND,
  14788. + __u8 *data,
  14789. + yaffs_ExtendedTags *tags);
  14790. +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
  14791. + int blockNo);
  14792. +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
  14793. + int blockNo,
  14794. + yaffs_BlockState *state,
  14795. + __u32 *sequenceNumber);
  14796. +
  14797. +void yaffs_CalcTagsECC(yaffs_Tags *tags);
  14798. +int yaffs_CheckECCOnTags(yaffs_Tags *tags);
  14799. +int yaffs_CountBits(__u8 byte);
  14800. +
  14801. +#endif
  14802. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.34/fs/yaffs2/yaffs_tagsvalidity.c
  14803. --- linux-2.6.34.orig/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
  14804. +++ linux-2.6.34/fs/yaffs2/yaffs_tagsvalidity.c 2010-05-20 20:16:22.311614862 +0200
  14805. @@ -0,0 +1,28 @@
  14806. +/*
  14807. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14808. + *
  14809. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14810. + * for Toby Churchill Ltd and Brightstar Engineering
  14811. + *
  14812. + * Created by Charles Manning <charles@aleph1.co.uk>
  14813. + *
  14814. + * This program is free software; you can redistribute it and/or modify
  14815. + * it under the terms of the GNU General Public License version 2 as
  14816. + * published by the Free Software Foundation.
  14817. + */
  14818. +
  14819. +#include "yaffs_tagsvalidity.h"
  14820. +
  14821. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
  14822. +{
  14823. + memset(tags, 0, sizeof(yaffs_ExtendedTags));
  14824. + tags->validMarker0 = 0xAAAAAAAA;
  14825. + tags->validMarker1 = 0x55555555;
  14826. +}
  14827. +
  14828. +int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
  14829. +{
  14830. + return (tags->validMarker0 == 0xAAAAAAAA &&
  14831. + tags->validMarker1 == 0x55555555);
  14832. +
  14833. +}
  14834. diff -Nur linux-2.6.34.orig/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.34/fs/yaffs2/yaffs_tagsvalidity.h
  14835. --- linux-2.6.34.orig/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
  14836. +++ linux-2.6.34/fs/yaffs2/yaffs_tagsvalidity.h 2010-05-20 20:16:22.311614862 +0200
  14837. @@ -0,0 +1,24 @@
  14838. +/*
  14839. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14840. + *
  14841. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14842. + * for Toby Churchill Ltd and Brightstar Engineering
  14843. + *
  14844. + * Created by Charles Manning <charles@aleph1.co.uk>
  14845. + *
  14846. + * This program is free software; you can redistribute it and/or modify
  14847. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14848. + * published by the Free Software Foundation.
  14849. + *
  14850. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14851. + */
  14852. +
  14853. +
  14854. +#ifndef __YAFFS_TAGS_VALIDITY_H__
  14855. +#define __YAFFS_TAGS_VALIDITY_H__
  14856. +
  14857. +#include "yaffs_guts.h"
  14858. +
  14859. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
  14860. +int yaffs_ValidateTags(yaffs_ExtendedTags *tags);
  14861. +#endif
  14862. diff -Nur linux-2.6.34.orig/fs/yaffs2/yportenv.h linux-2.6.34/fs/yaffs2/yportenv.h
  14863. --- linux-2.6.34.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  14864. +++ linux-2.6.34/fs/yaffs2/yportenv.h 2010-05-20 20:16:22.311614862 +0200
  14865. @@ -0,0 +1,203 @@
  14866. +/*
  14867. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14868. + *
  14869. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14870. + * for Toby Churchill Ltd and Brightstar Engineering
  14871. + *
  14872. + * Created by Charles Manning <charles@aleph1.co.uk>
  14873. + *
  14874. + * This program is free software; you can redistribute it and/or modify
  14875. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14876. + * published by the Free Software Foundation.
  14877. + *
  14878. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14879. + */
  14880. +
  14881. +
  14882. +#ifndef __YPORTENV_H__
  14883. +#define __YPORTENV_H__
  14884. +
  14885. +/*
  14886. + * Define the MTD version in terms of Linux Kernel versions
  14887. + * This allows yaffs to be used independantly of the kernel
  14888. + * as well as with it.
  14889. + */
  14890. +
  14891. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  14892. +
  14893. +#if defined CONFIG_YAFFS_WINCE
  14894. +
  14895. +#include "ywinceenv.h"
  14896. +
  14897. +#elif defined __KERNEL__
  14898. +
  14899. +#include "moduleconfig.h"
  14900. +
  14901. +/* Linux kernel */
  14902. +
  14903. +#include <linux/version.h>
  14904. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  14905. +
  14906. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  14907. +#include <linux/config.h>
  14908. +#endif
  14909. +#include <linux/kernel.h>
  14910. +#include <linux/mm.h>
  14911. +#include <linux/sched.h>
  14912. +#include <linux/string.h>
  14913. +#include <linux/slab.h>
  14914. +#include <linux/vmalloc.h>
  14915. +
  14916. +#define YCHAR char
  14917. +#define YUCHAR unsigned char
  14918. +#define _Y(x) x
  14919. +#define yaffs_strcat(a, b) strcat(a, b)
  14920. +#define yaffs_strcpy(a, b) strcpy(a, b)
  14921. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  14922. +#define yaffs_strncmp(a, b, c) strncmp(a, b, c)
  14923. +#define yaffs_strlen(s) strlen(s)
  14924. +#define yaffs_sprintf sprintf
  14925. +#define yaffs_toupper(a) toupper(a)
  14926. +
  14927. +#define Y_INLINE inline
  14928. +
  14929. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  14930. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  14931. +
  14932. +/* #define YPRINTF(x) printk x */
  14933. +#define YMALLOC(x) kmalloc(x, GFP_NOFS)
  14934. +#define YFREE(x) kfree(x)
  14935. +#define YMALLOC_ALT(x) vmalloc(x)
  14936. +#define YFREE_ALT(x) vfree(x)
  14937. +#define YMALLOC_DMA(x) YMALLOC(x)
  14938. +
  14939. +/* KR - added for use in scan so processes aren't blocked indefinitely. */
  14940. +#define YYIELD() schedule()
  14941. +
  14942. +#define YAFFS_ROOT_MODE 0666
  14943. +#define YAFFS_LOSTNFOUND_MODE 0666
  14944. +
  14945. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  14946. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  14947. +#define Y_TIME_CONVERT(x) (x).tv_sec
  14948. +#else
  14949. +#define Y_CURRENT_TIME CURRENT_TIME
  14950. +#define Y_TIME_CONVERT(x) (x)
  14951. +#endif
  14952. +
  14953. +#define yaffs_SumCompare(x, y) ((x) == (y))
  14954. +#define yaffs_strcmp(a, b) strcmp(a, b)
  14955. +
  14956. +#define TENDSTR "\n"
  14957. +#define TSTR(x) KERN_WARNING x
  14958. +#define TCONT(x) x
  14959. +#define TOUT(p) printk p
  14960. +
  14961. +#define yaffs_trace(mask, fmt, args...) \
  14962. + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
  14963. + printk(KERN_WARNING "yaffs: " fmt, ## args); \
  14964. + } while (0)
  14965. +
  14966. +#define compile_time_assertion(assertion) \
  14967. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  14968. +
  14969. +#elif defined CONFIG_YAFFS_DIRECT
  14970. +
  14971. +#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22)
  14972. +
  14973. +/* Direct interface */
  14974. +#include "ydirectenv.h"
  14975. +
  14976. +#elif defined CONFIG_YAFFS_UTIL
  14977. +
  14978. +/* Stuff for YAFFS utilities */
  14979. +
  14980. +#include "stdlib.h"
  14981. +#include "stdio.h"
  14982. +#include "string.h"
  14983. +
  14984. +#include "devextras.h"
  14985. +
  14986. +#define YMALLOC(x) malloc(x)
  14987. +#define YFREE(x) free(x)
  14988. +#define YMALLOC_ALT(x) malloc(x)
  14989. +#define YFREE_ALT(x) free(x)
  14990. +
  14991. +#define YCHAR char
  14992. +#define YUCHAR unsigned char
  14993. +#define _Y(x) x
  14994. +#define yaffs_strcat(a, b) strcat(a, b)
  14995. +#define yaffs_strcpy(a, b) strcpy(a, b)
  14996. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  14997. +#define yaffs_strlen(s) strlen(s)
  14998. +#define yaffs_sprintf sprintf
  14999. +#define yaffs_toupper(a) toupper(a)
  15000. +
  15001. +#define Y_INLINE inline
  15002. +
  15003. +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
  15004. +/* #define YALERT(s) YINFO(s) */
  15005. +
  15006. +#define TENDSTR "\n"
  15007. +#define TSTR(x) x
  15008. +#define TOUT(p) printf p
  15009. +
  15010. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  15011. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  15012. +/* #define YPRINTF(x) printf x */
  15013. +
  15014. +#define YAFFS_ROOT_MODE 0666
  15015. +#define YAFFS_LOSTNFOUND_MODE 0666
  15016. +
  15017. +#define yaffs_SumCompare(x, y) ((x) == (y))
  15018. +#define yaffs_strcmp(a, b) strcmp(a, b)
  15019. +
  15020. +#else
  15021. +/* Should have specified a configuration type */
  15022. +#error Unknown configuration
  15023. +
  15024. +#endif
  15025. +
  15026. +/* see yaffs_fs.c */
  15027. +extern unsigned int yaffs_traceMask;
  15028. +extern unsigned int yaffs_wr_attempts;
  15029. +
  15030. +/*
  15031. + * Tracing flags.
  15032. + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
  15033. + */
  15034. +
  15035. +#define YAFFS_TRACE_OS 0x00000002
  15036. +#define YAFFS_TRACE_ALLOCATE 0x00000004
  15037. +#define YAFFS_TRACE_SCAN 0x00000008
  15038. +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
  15039. +#define YAFFS_TRACE_ERASE 0x00000020
  15040. +#define YAFFS_TRACE_GC 0x00000040
  15041. +#define YAFFS_TRACE_WRITE 0x00000080
  15042. +#define YAFFS_TRACE_TRACING 0x00000100
  15043. +#define YAFFS_TRACE_DELETION 0x00000200
  15044. +#define YAFFS_TRACE_BUFFERS 0x00000400
  15045. +#define YAFFS_TRACE_NANDACCESS 0x00000800
  15046. +#define YAFFS_TRACE_GC_DETAIL 0x00001000
  15047. +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
  15048. +#define YAFFS_TRACE_MTD 0x00004000
  15049. +#define YAFFS_TRACE_CHECKPOINT 0x00008000
  15050. +
  15051. +#define YAFFS_TRACE_VERIFY 0x00010000
  15052. +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
  15053. +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
  15054. +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
  15055. +
  15056. +
  15057. +#define YAFFS_TRACE_ERROR 0x40000000
  15058. +#define YAFFS_TRACE_BUG 0x80000000
  15059. +#define YAFFS_TRACE_ALWAYS 0xF0000000
  15060. +
  15061. +
  15062. +#define T(mask, p) do { if ((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
  15063. +
  15064. +#ifndef YBUG
  15065. +#define YBUG() do {T(YAFFS_TRACE_BUG, (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR), __LINE__)); } while (0)
  15066. +#endif
  15067. +
  15068. +#endif