yaffs2.patch 396 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066
  1. diff -Nur linux-2.6.32.orig/fs/Kconfig linux-2.6.32/fs/Kconfig
  2. --- linux-2.6.32.orig/fs/Kconfig 2009-12-03 04:51:21.000000000 +0100
  3. +++ linux-2.6.32/fs/Kconfig 2010-01-30 20:35:00.921899692 +0100
  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.32.orig/fs/Makefile linux-2.6.32/fs/Makefile
  16. --- linux-2.6.32.orig/fs/Makefile 2009-12-03 04:51:21.000000000 +0100
  17. +++ linux-2.6.32/fs/Makefile 2010-01-30 20:35:00.933084814 +0100
  18. @@ -124,3 +124,4 @@
  19. obj-$(CONFIG_BTRFS_FS) += btrfs/
  20. obj-$(CONFIG_GFS2_FS) += gfs2/
  21. obj-$(CONFIG_EXOFS_FS) += exofs/
  22. +obj-$(CONFIG_YAFFS_FS) += yaffs2/
  23. diff -Nur linux-2.6.32.orig/fs/Makefile.pre.yaffs linux-2.6.32/fs/Makefile.pre.yaffs
  24. --- linux-2.6.32.orig/fs/Makefile.pre.yaffs 1970-01-01 01:00:00.000000000 +0100
  25. +++ linux-2.6.32/fs/Makefile.pre.yaffs 2010-01-30 20:35:00.983076819 +0100
  26. @@ -0,0 +1,126 @@
  27. +#
  28. +# Makefile for the Linux filesystems.
  29. +#
  30. +# 14 Sep 2000, Christoph Hellwig <hch@infradead.org>
  31. +# Rewritten to use lists instead of if-statements.
  32. +#
  33. +
  34. +obj-y := open.o read_write.o file_table.o super.o \
  35. + char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \
  36. + ioctl.o readdir.o select.o fifo.o dcache.o inode.o \
  37. + attr.o bad_inode.o file.o filesystems.o namespace.o \
  38. + seq_file.o xattr.o libfs.o fs-writeback.o \
  39. + pnode.o drop_caches.o splice.o sync.o utimes.o \
  40. + stack.o
  41. +
  42. +ifeq ($(CONFIG_BLOCK),y)
  43. +obj-y += buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
  44. +else
  45. +obj-y += no-block.o
  46. +endif
  47. +
  48. +obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o
  49. +obj-y += notify/
  50. +obj-$(CONFIG_EPOLL) += eventpoll.o
  51. +obj-$(CONFIG_ANON_INODES) += anon_inodes.o
  52. +obj-$(CONFIG_SIGNALFD) += signalfd.o
  53. +obj-$(CONFIG_TIMERFD) += timerfd.o
  54. +obj-$(CONFIG_EVENTFD) += eventfd.o
  55. +obj-$(CONFIG_AIO) += aio.o
  56. +obj-$(CONFIG_FILE_LOCKING) += locks.o
  57. +obj-$(CONFIG_COMPAT) += compat.o compat_ioctl.o
  58. +
  59. +nfsd-$(CONFIG_NFSD) := nfsctl.o
  60. +obj-y += $(nfsd-y) $(nfsd-m)
  61. +
  62. +obj-$(CONFIG_BINFMT_AOUT) += binfmt_aout.o
  63. +obj-$(CONFIG_BINFMT_EM86) += binfmt_em86.o
  64. +obj-$(CONFIG_BINFMT_MISC) += binfmt_misc.o
  65. +
  66. +# binfmt_script is always there
  67. +obj-y += binfmt_script.o
  68. +
  69. +obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o
  70. +obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o
  71. +obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o
  72. +obj-$(CONFIG_BINFMT_SOM) += binfmt_som.o
  73. +obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o
  74. +
  75. +obj-$(CONFIG_FS_MBCACHE) += mbcache.o
  76. +obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.o xattr_acl.o
  77. +obj-$(CONFIG_NFS_COMMON) += nfs_common/
  78. +obj-$(CONFIG_GENERIC_ACL) += generic_acl.o
  79. +
  80. +obj-$(CONFIG_QUOTA) += dquot.o
  81. +obj-$(CONFIG_QFMT_V1) += quota_v1.o
  82. +obj-$(CONFIG_QFMT_V2) += quota_v2.o
  83. +obj-$(CONFIG_QUOTA_TREE) += quota_tree.o
  84. +obj-$(CONFIG_QUOTACTL) += quota.o
  85. +
  86. +obj-$(CONFIG_PROC_FS) += proc/
  87. +obj-y += partitions/
  88. +obj-$(CONFIG_SYSFS) += sysfs/
  89. +obj-$(CONFIG_CONFIGFS_FS) += configfs/
  90. +obj-y += devpts/
  91. +
  92. +obj-$(CONFIG_PROFILING) += dcookies.o
  93. +obj-$(CONFIG_DLM) += dlm/
  94. +
  95. +# Do not add any filesystems before this line
  96. +obj-$(CONFIG_REISERFS_FS) += reiserfs/
  97. +obj-$(CONFIG_EXT3_FS) += ext3/ # Before ext2 so root fs can be ext3
  98. +obj-$(CONFIG_EXT2_FS) += ext2/
  99. +# We place ext4 after ext2 so plain ext2 root fs's are mounted using ext2
  100. +# unless explicitly requested by rootfstype
  101. +obj-$(CONFIG_EXT4_FS) += ext4/
  102. +obj-$(CONFIG_JBD) += jbd/
  103. +obj-$(CONFIG_JBD2) += jbd2/
  104. +obj-$(CONFIG_CRAMFS) += cramfs/
  105. +obj-$(CONFIG_SQUASHFS) += squashfs/
  106. +obj-y += ramfs/
  107. +obj-$(CONFIG_HUGETLBFS) += hugetlbfs/
  108. +obj-$(CONFIG_CODA_FS) += coda/
  109. +obj-$(CONFIG_MINIX_FS) += minix/
  110. +obj-$(CONFIG_FAT_FS) += fat/
  111. +obj-$(CONFIG_BFS_FS) += bfs/
  112. +obj-$(CONFIG_ISO9660_FS) += isofs/
  113. +obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
  114. +obj-$(CONFIG_HFS_FS) += hfs/
  115. +obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
  116. +obj-$(CONFIG_VXFS_FS) += freevxfs/
  117. +obj-$(CONFIG_NFS_FS) += nfs/
  118. +obj-$(CONFIG_EXPORTFS) += exportfs/
  119. +obj-$(CONFIG_NFSD) += nfsd/
  120. +obj-$(CONFIG_LOCKD) += lockd/
  121. +obj-$(CONFIG_NLS) += nls/
  122. +obj-$(CONFIG_SYSV_FS) += sysv/
  123. +obj-$(CONFIG_SMB_FS) += smbfs/
  124. +obj-$(CONFIG_CIFS) += cifs/
  125. +obj-$(CONFIG_NCP_FS) += ncpfs/
  126. +obj-$(CONFIG_HPFS_FS) += hpfs/
  127. +obj-$(CONFIG_NTFS_FS) += ntfs/
  128. +obj-$(CONFIG_UFS_FS) += ufs/
  129. +obj-$(CONFIG_EFS_FS) += efs/
  130. +obj-$(CONFIG_JFFS2_FS) += jffs2/
  131. +obj-$(CONFIG_UBIFS_FS) += ubifs/
  132. +obj-$(CONFIG_AFFS_FS) += affs/
  133. +obj-$(CONFIG_ROMFS_FS) += romfs/
  134. +obj-$(CONFIG_QNX4FS_FS) += qnx4/
  135. +obj-$(CONFIG_AUTOFS_FS) += autofs/
  136. +obj-$(CONFIG_AUTOFS4_FS) += autofs4/
  137. +obj-$(CONFIG_ADFS_FS) += adfs/
  138. +obj-$(CONFIG_FUSE_FS) += fuse/
  139. +obj-$(CONFIG_UDF_FS) += udf/
  140. +obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
  141. +obj-$(CONFIG_OMFS_FS) += omfs/
  142. +obj-$(CONFIG_JFS_FS) += jfs/
  143. +obj-$(CONFIG_XFS_FS) += xfs/
  144. +obj-$(CONFIG_9P_FS) += 9p/
  145. +obj-$(CONFIG_AFS_FS) += afs/
  146. +obj-$(CONFIG_BEFS_FS) += befs/
  147. +obj-$(CONFIG_HOSTFS) += hostfs/
  148. +obj-$(CONFIG_HPPFS) += hppfs/
  149. +obj-$(CONFIG_DEBUG_FS) += debugfs/
  150. +obj-$(CONFIG_OCFS2_FS) += ocfs2/
  151. +obj-$(CONFIG_BTRFS_FS) += btrfs/
  152. +obj-$(CONFIG_GFS2_FS) += gfs2/
  153. diff -Nur linux-2.6.32.orig/fs/yaffs2/devextras.h linux-2.6.32/fs/yaffs2/devextras.h
  154. --- linux-2.6.32.orig/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
  155. +++ linux-2.6.32/fs/yaffs2/devextras.h 2010-01-30 20:35:01.021829008 +0100
  156. @@ -0,0 +1,196 @@
  157. +/*
  158. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  159. + *
  160. + * Copyright (C) 2002-2007 Aleph One Ltd.
  161. + * for Toby Churchill Ltd and Brightstar Engineering
  162. + *
  163. + * Created by Charles Manning <charles@aleph1.co.uk>
  164. + *
  165. + * This program is free software; you can redistribute it and/or modify
  166. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  167. + * published by the Free Software Foundation.
  168. + *
  169. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  170. + */
  171. +
  172. +/*
  173. + * This file is just holds extra declarations of macros that would normally
  174. + * be providesd in the Linux kernel. These macros have been written from
  175. + * scratch but are functionally equivalent to the Linux ones.
  176. + *
  177. + */
  178. +
  179. +#ifndef __EXTRAS_H__
  180. +#define __EXTRAS_H__
  181. +
  182. +
  183. +#if !(defined __KERNEL__)
  184. +
  185. +/* Definition of types */
  186. +typedef unsigned char __u8;
  187. +typedef unsigned short __u16;
  188. +typedef unsigned __u32;
  189. +
  190. +#endif
  191. +
  192. +/*
  193. + * This is a simple doubly linked list implementation that matches the
  194. + * way the Linux kernel doubly linked list implementation works.
  195. + */
  196. +
  197. +struct ylist_head {
  198. + struct ylist_head *next; /* next in chain */
  199. + struct ylist_head *prev; /* previous in chain */
  200. +};
  201. +
  202. +
  203. +/* Initialise a static list */
  204. +#define YLIST_HEAD(name) \
  205. +struct ylist_head name = { &(name), &(name)}
  206. +
  207. +
  208. +
  209. +/* Initialise a list head to an empty list */
  210. +#define YINIT_LIST_HEAD(p) \
  211. +do { \
  212. + (p)->next = (p);\
  213. + (p)->prev = (p); \
  214. +} while (0)
  215. +
  216. +
  217. +/* Add an element to a list */
  218. +static __inline__ void ylist_add(struct ylist_head *newEntry,
  219. + struct ylist_head *list)
  220. +{
  221. + struct ylist_head *listNext = list->next;
  222. +
  223. + list->next = newEntry;
  224. + newEntry->prev = list;
  225. + newEntry->next = listNext;
  226. + listNext->prev = newEntry;
  227. +
  228. +}
  229. +
  230. +static __inline__ void ylist_add_tail(struct ylist_head *newEntry,
  231. + struct ylist_head *list)
  232. +{
  233. + struct ylist_head *listPrev = list->prev;
  234. +
  235. + list->prev = newEntry;
  236. + newEntry->next = list;
  237. + newEntry->prev = listPrev;
  238. + listPrev->next = newEntry;
  239. +
  240. +}
  241. +
  242. +
  243. +/* Take an element out of its current list, with or without
  244. + * reinitialising the links.of the entry*/
  245. +static __inline__ void ylist_del(struct ylist_head *entry)
  246. +{
  247. + struct ylist_head *listNext = entry->next;
  248. + struct ylist_head *listPrev = entry->prev;
  249. +
  250. + listNext->prev = listPrev;
  251. + listPrev->next = listNext;
  252. +
  253. +}
  254. +
  255. +static __inline__ void ylist_del_init(struct ylist_head *entry)
  256. +{
  257. + ylist_del(entry);
  258. + entry->next = entry->prev = entry;
  259. +}
  260. +
  261. +
  262. +/* Test if the list is empty */
  263. +static __inline__ int ylist_empty(struct ylist_head *entry)
  264. +{
  265. + return (entry->next == entry);
  266. +}
  267. +
  268. +
  269. +/* ylist_entry takes a pointer to a list entry and offsets it to that
  270. + * we can find a pointer to the object it is embedded in.
  271. + */
  272. +
  273. +
  274. +#define ylist_entry(entry, type, member) \
  275. + ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
  276. +
  277. +
  278. +/* ylist_for_each and list_for_each_safe iterate over lists.
  279. + * ylist_for_each_safe uses temporary storage to make the list delete safe
  280. + */
  281. +
  282. +#define ylist_for_each(itervar, list) \
  283. + for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
  284. +
  285. +#define ylist_for_each_safe(itervar, saveVar, list) \
  286. + for (itervar = (list)->next, saveVar = (list)->next->next; \
  287. + itervar != (list); itervar = saveVar, saveVar = saveVar->next)
  288. +
  289. +
  290. +#if !(defined __KERNEL__)
  291. +
  292. +
  293. +#ifndef WIN32
  294. +#include <sys/stat.h>
  295. +#endif
  296. +
  297. +
  298. +#ifdef CONFIG_YAFFS_PROVIDE_DEFS
  299. +/* File types */
  300. +
  301. +
  302. +#define DT_UNKNOWN 0
  303. +#define DT_FIFO 1
  304. +#define DT_CHR 2
  305. +#define DT_DIR 4
  306. +#define DT_BLK 6
  307. +#define DT_REG 8
  308. +#define DT_LNK 10
  309. +#define DT_SOCK 12
  310. +#define DT_WHT 14
  311. +
  312. +
  313. +#ifndef WIN32
  314. +#include <sys/stat.h>
  315. +#endif
  316. +
  317. +/*
  318. + * Attribute flags. These should be or-ed together to figure out what
  319. + * has been changed!
  320. + */
  321. +#define ATTR_MODE 1
  322. +#define ATTR_UID 2
  323. +#define ATTR_GID 4
  324. +#define ATTR_SIZE 8
  325. +#define ATTR_ATIME 16
  326. +#define ATTR_MTIME 32
  327. +#define ATTR_CTIME 64
  328. +
  329. +struct iattr {
  330. + unsigned int ia_valid;
  331. + unsigned ia_mode;
  332. + unsigned ia_uid;
  333. + unsigned ia_gid;
  334. + unsigned ia_size;
  335. + unsigned ia_atime;
  336. + unsigned ia_mtime;
  337. + unsigned ia_ctime;
  338. + unsigned int ia_attr_flags;
  339. +};
  340. +
  341. +#endif
  342. +
  343. +#else
  344. +
  345. +#include <linux/types.h>
  346. +#include <linux/fs.h>
  347. +#include <linux/stat.h>
  348. +
  349. +#endif
  350. +
  351. +
  352. +#endif
  353. diff -Nur linux-2.6.32.orig/fs/yaffs2/Kconfig linux-2.6.32/fs/yaffs2/Kconfig
  354. --- linux-2.6.32.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  355. +++ linux-2.6.32/fs/yaffs2/Kconfig 2010-01-30 20:35:01.053081300 +0100
  356. @@ -0,0 +1,156 @@
  357. +#
  358. +# YAFFS file system configurations
  359. +#
  360. +
  361. +config YAFFS_FS
  362. + tristate "YAFFS2 file system support"
  363. + default n
  364. + depends on MTD_BLOCK
  365. + select YAFFS_YAFFS1
  366. + select YAFFS_YAFFS2
  367. + help
  368. + YAFFS2, or Yet Another Flash Filing System, is a filing system
  369. + optimised for NAND Flash chips.
  370. +
  371. + To compile the YAFFS2 file system support as a module, choose M
  372. + here: the module will be called yaffs2.
  373. +
  374. + If unsure, say N.
  375. +
  376. + Further information on YAFFS2 is available at
  377. + <http://www.aleph1.co.uk/yaffs/>.
  378. +
  379. +config YAFFS_YAFFS1
  380. + bool "512 byte / page devices"
  381. + depends on YAFFS_FS
  382. + default y
  383. + help
  384. + Enable YAFFS1 support -- yaffs for 512 byte / page devices
  385. +
  386. + Not needed for 2K-page devices.
  387. +
  388. + If unsure, say Y.
  389. +
  390. +config YAFFS_9BYTE_TAGS
  391. + bool "Use older-style on-NAND data format with pageStatus byte"
  392. + depends on YAFFS_YAFFS1
  393. + default n
  394. + help
  395. +
  396. + Older-style on-NAND data format has a "pageStatus" byte to record
  397. + chunk/page state. This byte is zero when the page is discarded.
  398. + Choose this option if you have existing on-NAND data using this
  399. + format that you need to continue to support. New data written
  400. + also uses the older-style format. Note: Use of this option
  401. + generally requires that MTD's oob layout be adjusted to use the
  402. + older-style format. See notes on tags formats and MTD versions
  403. + in yaffs_mtdif1.c.
  404. +
  405. + If unsure, say N.
  406. +
  407. +config YAFFS_DOES_ECC
  408. + bool "Lets Yaffs do its own ECC"
  409. + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
  410. + default n
  411. + help
  412. + This enables Yaffs to use its own ECC functions instead of using
  413. + the ones from the generic MTD-NAND driver.
  414. +
  415. + If unsure, say N.
  416. +
  417. +config YAFFS_ECC_WRONG_ORDER
  418. + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
  419. + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
  420. + default n
  421. + help
  422. + This makes yaffs_ecc.c use the same ecc byte order as Steven
  423. + Hill's nand_ecc.c. If not set, then you get the same ecc byte
  424. + order as SmartMedia.
  425. +
  426. + If unsure, say N.
  427. +
  428. +config YAFFS_YAFFS2
  429. + bool "2048 byte (or larger) / page devices"
  430. + depends on YAFFS_FS
  431. + default y
  432. + help
  433. + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
  434. +
  435. + If unsure, say Y.
  436. +
  437. +config YAFFS_AUTO_YAFFS2
  438. + bool "Autoselect yaffs2 format"
  439. + depends on YAFFS_YAFFS2
  440. + default y
  441. + help
  442. + Without this, you need to explicitely use yaffs2 as the file
  443. + system type. With this, you can say "yaffs" and yaffs or yaffs2
  444. + will be used depending on the device page size (yaffs on
  445. + 512-byte page devices, yaffs2 on 2K page devices).
  446. +
  447. + If unsure, say Y.
  448. +
  449. +config YAFFS_DISABLE_LAZY_LOAD
  450. + bool "Disable lazy loading"
  451. + depends on YAFFS_YAFFS2
  452. + default n
  453. + help
  454. + "Lazy loading" defers loading file details until they are
  455. + required. This saves mount time, but makes the first look-up
  456. + a bit longer.
  457. +
  458. + Lazy loading will only happen if enabled by this option being 'n'
  459. + and if the appropriate tags are available, else yaffs2 will
  460. + automatically fall back to immediate loading and do the right
  461. + thing.
  462. +
  463. + Lazy laoding will be required by checkpointing.
  464. +
  465. + Setting this to 'y' will disable lazy loading.
  466. +
  467. + If unsure, say N.
  468. +
  469. +
  470. +config YAFFS_DISABLE_WIDE_TNODES
  471. + bool "Turn off wide tnodes"
  472. + depends on YAFFS_FS
  473. + default n
  474. + help
  475. + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
  476. + page devices and >=128MB for 2k page devices. They use slightly
  477. + more RAM but are faster since they eliminate chunk group
  478. + searching.
  479. +
  480. + Setting this to 'y' will force tnode width to 16 bits and save
  481. + memory but make large arrays slower.
  482. +
  483. + If unsure, say N.
  484. +
  485. +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  486. + bool "Force chunk erase check"
  487. + depends on YAFFS_FS
  488. + default n
  489. + help
  490. + Normally YAFFS only checks chunks before writing until an erased
  491. + chunk is found. This helps to detect any partially written
  492. + chunks that might have happened due to power loss.
  493. +
  494. + Enabling this forces on the test that chunks are erased in flash
  495. + before writing to them. This takes more time but is potentially
  496. + a bit more secure.
  497. +
  498. + Suggest setting Y during development and ironing out driver
  499. + issues etc. Suggest setting to N if you want faster writing.
  500. +
  501. + If unsure, say Y.
  502. +
  503. +config YAFFS_SHORT_NAMES_IN_RAM
  504. + bool "Cache short names in RAM"
  505. + depends on YAFFS_FS
  506. + default y
  507. + help
  508. + If this config is set, then short names are stored with the
  509. + yaffs_Object. This costs an extra 16 bytes of RAM per object,
  510. + but makes look-ups faster.
  511. +
  512. + If unsure, say Y.
  513. diff -Nur linux-2.6.32.orig/fs/yaffs2/Makefile linux-2.6.32/fs/yaffs2/Makefile
  514. --- linux-2.6.32.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  515. +++ linux-2.6.32/fs/yaffs2/Makefile 2010-01-30 20:35:01.093074881 +0100
  516. @@ -0,0 +1,10 @@
  517. +#
  518. +# Makefile for the linux YAFFS filesystem routines.
  519. +#
  520. +
  521. +obj-$(CONFIG_YAFFS_FS) += yaffs.o
  522. +
  523. +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
  524. +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
  525. +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
  526. +yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
  527. diff -Nur linux-2.6.32.orig/fs/yaffs2/moduleconfig.h linux-2.6.32/fs/yaffs2/moduleconfig.h
  528. --- linux-2.6.32.orig/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
  529. +++ linux-2.6.32/fs/yaffs2/moduleconfig.h 2010-01-30 20:35:01.131828051 +0100
  530. @@ -0,0 +1,65 @@
  531. +/*
  532. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  533. + *
  534. + * Copyright (C) 2002-2007 Aleph One Ltd.
  535. + * for Toby Churchill Ltd and Brightstar Engineering
  536. + *
  537. + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
  538. + *
  539. + * This program is free software; you can redistribute it and/or modify
  540. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  541. + * published by the Free Software Foundation.
  542. + *
  543. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  544. + */
  545. +
  546. +#ifndef __YAFFS_CONFIG_H__
  547. +#define __YAFFS_CONFIG_H__
  548. +
  549. +#ifdef YAFFS_OUT_OF_TREE
  550. +
  551. +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
  552. +#define CONFIG_YAFFS_FS
  553. +#define CONFIG_YAFFS_YAFFS1
  554. +#define CONFIG_YAFFS_YAFFS2
  555. +
  556. +/* These options are independent of each other. Select those that matter. */
  557. +
  558. +/* Default: Not selected */
  559. +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
  560. +/* #define CONFIG_YAFFS_DOES_ECC */
  561. +
  562. +/* Default: Not selected */
  563. +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
  564. +/* CONFIG_YAFFS_DOES_ECC is set */
  565. +/* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
  566. +
  567. +/* Default: Selected */
  568. +/* Meaning: Disables testing whether chunks are erased before writing to them*/
  569. +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
  570. +
  571. +/* Default: Selected */
  572. +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
  573. +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  574. +
  575. +/* Default: 10 */
  576. +/* Meaning: set the count of blocks to reserve for checkpointing */
  577. +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
  578. +
  579. +/*
  580. +Older-style on-NAND data format has a "pageStatus" byte to record
  581. +chunk/page state. This byte is zeroed when the page is discarded.
  582. +Choose this option if you have existing on-NAND data in this format
  583. +that you need to continue to support. New data written also uses the
  584. +older-style format.
  585. +Note: Use of this option generally requires that MTD's oob layout be
  586. +adjusted to use the older-style format. See notes on tags formats and
  587. +MTD versions in yaffs_mtdif1.c.
  588. +*/
  589. +/* Default: Not selected */
  590. +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
  591. +/* #define CONFIG_YAFFS_9BYTE_TAGS */
  592. +
  593. +#endif /* YAFFS_OUT_OF_TREE */
  594. +
  595. +#endif /* __YAFFS_CONFIG_H__ */
  596. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_checkptrw.c linux-2.6.32/fs/yaffs2/yaffs_checkptrw.c
  597. --- linux-2.6.32.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  598. +++ linux-2.6.32/fs/yaffs2/yaffs_checkptrw.c 2010-01-30 20:35:01.171829690 +0100
  599. @@ -0,0 +1,394 @@
  600. +/*
  601. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  602. + *
  603. + * Copyright (C) 2002-2007 Aleph One Ltd.
  604. + * for Toby Churchill Ltd and Brightstar Engineering
  605. + *
  606. + * Created by Charles Manning <charles@aleph1.co.uk>
  607. + *
  608. + * This program is free software; you can redistribute it and/or modify
  609. + * it under the terms of the GNU General Public License version 2 as
  610. + * published by the Free Software Foundation.
  611. + */
  612. +
  613. +const char *yaffs_checkptrw_c_version =
  614. + "$Id: yaffs_checkptrw.c,v 1.18 2009-03-06 17:20:49 wookey Exp $";
  615. +
  616. +
  617. +#include "yaffs_checkptrw.h"
  618. +#include "yaffs_getblockinfo.h"
  619. +
  620. +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
  621. +{
  622. + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
  623. +
  624. + T(YAFFS_TRACE_CHECKPOINT,
  625. + (TSTR("checkpt blocks available = %d" TENDSTR),
  626. + blocksAvailable));
  627. +
  628. + return (blocksAvailable <= 0) ? 0 : 1;
  629. +}
  630. +
  631. +
  632. +static int yaffs_CheckpointErase(yaffs_Device *dev)
  633. +{
  634. + int i;
  635. +
  636. + if (!dev->eraseBlockInNAND)
  637. + return 0;
  638. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
  639. + dev->internalStartBlock, dev->internalEndBlock));
  640. +
  641. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  642. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  643. + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
  644. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("erasing checkpt block %d"TENDSTR), i));
  645. + if (dev->eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
  646. + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
  647. + dev->nErasedBlocks++;
  648. + dev->nFreeChunks += dev->nChunksPerBlock;
  649. + } else {
  650. + dev->markNANDBlockBad(dev, i);
  651. + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
  652. + }
  653. + }
  654. + }
  655. +
  656. + dev->blocksInCheckpoint = 0;
  657. +
  658. + return 1;
  659. +}
  660. +
  661. +
  662. +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
  663. +{
  664. + int i;
  665. + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
  666. + T(YAFFS_TRACE_CHECKPOINT,
  667. + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
  668. + dev->nErasedBlocks, dev->nReservedBlocks, blocksAvailable, dev->checkpointNextBlock));
  669. +
  670. + if (dev->checkpointNextBlock >= 0 &&
  671. + dev->checkpointNextBlock <= dev->internalEndBlock &&
  672. + blocksAvailable > 0) {
  673. +
  674. + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
  675. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  676. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
  677. + dev->checkpointNextBlock = i + 1;
  678. + dev->checkpointCurrentBlock = i;
  679. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("allocating checkpt block %d"TENDSTR), i));
  680. + return;
  681. + }
  682. + }
  683. + }
  684. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
  685. +
  686. + dev->checkpointNextBlock = -1;
  687. + dev->checkpointCurrentBlock = -1;
  688. +}
  689. +
  690. +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
  691. +{
  692. + int i;
  693. + yaffs_ExtendedTags tags;
  694. +
  695. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
  696. + dev->blocksInCheckpoint, dev->checkpointNextBlock));
  697. +
  698. + if (dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
  699. + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
  700. + int chunk = i * dev->nChunksPerBlock;
  701. + int realignedChunk = chunk - dev->chunkOffset;
  702. +
  703. + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
  704. + NULL, &tags);
  705. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
  706. + i, tags.objectId, tags.sequenceNumber, tags.eccResult));
  707. +
  708. + if (tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  709. + /* Right kind of block */
  710. + dev->checkpointNextBlock = tags.objectId;
  711. + dev->checkpointCurrentBlock = i;
  712. + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
  713. + dev->blocksInCheckpoint++;
  714. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found checkpt block %d"TENDSTR), i));
  715. + return;
  716. + }
  717. + }
  718. +
  719. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
  720. +
  721. + dev->checkpointNextBlock = -1;
  722. + dev->checkpointCurrentBlock = -1;
  723. +}
  724. +
  725. +
  726. +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
  727. +{
  728. +
  729. + /* Got the functions we need? */
  730. + if (!dev->writeChunkWithTagsToNAND ||
  731. + !dev->readChunkWithTagsFromNAND ||
  732. + !dev->eraseBlockInNAND ||
  733. + !dev->markNANDBlockBad)
  734. + return 0;
  735. +
  736. + if (forWriting && !yaffs_CheckpointSpaceOk(dev))
  737. + return 0;
  738. +
  739. + if (!dev->checkpointBuffer)
  740. + dev->checkpointBuffer = YMALLOC_DMA(dev->totalBytesPerChunk);
  741. + if (!dev->checkpointBuffer)
  742. + return 0;
  743. +
  744. +
  745. + dev->checkpointPageSequence = 0;
  746. +
  747. + dev->checkpointOpenForWrite = forWriting;
  748. +
  749. + dev->checkpointByteCount = 0;
  750. + dev->checkpointSum = 0;
  751. + dev->checkpointXor = 0;
  752. + dev->checkpointCurrentBlock = -1;
  753. + dev->checkpointCurrentChunk = -1;
  754. + dev->checkpointNextBlock = dev->internalStartBlock;
  755. +
  756. + /* Erase all the blocks in the checkpoint area */
  757. + if (forWriting) {
  758. + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
  759. + dev->checkpointByteOffset = 0;
  760. + return yaffs_CheckpointErase(dev);
  761. + } else {
  762. + int i;
  763. + /* Set to a value that will kick off a read */
  764. + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
  765. + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
  766. + * going to be way more than we need */
  767. + dev->blocksInCheckpoint = 0;
  768. + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
  769. + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
  770. + for (i = 0; i < dev->checkpointMaxBlocks; i++)
  771. + dev->checkpointBlockList[i] = -1;
  772. + }
  773. +
  774. + return 1;
  775. +}
  776. +
  777. +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
  778. +{
  779. + __u32 compositeSum;
  780. + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
  781. + *sum = compositeSum;
  782. + return 1;
  783. +}
  784. +
  785. +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
  786. +{
  787. + int chunk;
  788. + int realignedChunk;
  789. +
  790. + yaffs_ExtendedTags tags;
  791. +
  792. + if (dev->checkpointCurrentBlock < 0) {
  793. + yaffs_CheckpointFindNextErasedBlock(dev);
  794. + dev->checkpointCurrentChunk = 0;
  795. + }
  796. +
  797. + if (dev->checkpointCurrentBlock < 0)
  798. + return 0;
  799. +
  800. + tags.chunkDeleted = 0;
  801. + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
  802. + tags.chunkId = dev->checkpointPageSequence + 1;
  803. + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  804. + tags.byteCount = dev->nDataBytesPerChunk;
  805. + if (dev->checkpointCurrentChunk == 0) {
  806. + /* First chunk we write for the block? Set block state to
  807. + checkpoint */
  808. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
  809. + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
  810. + dev->blocksInCheckpoint++;
  811. + }
  812. +
  813. + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
  814. +
  815. +
  816. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
  817. + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk, tags.objectId, tags.chunkId));
  818. +
  819. + realignedChunk = chunk - dev->chunkOffset;
  820. +
  821. + dev->writeChunkWithTagsToNAND(dev, realignedChunk,
  822. + dev->checkpointBuffer, &tags);
  823. + dev->checkpointByteOffset = 0;
  824. + dev->checkpointPageSequence++;
  825. + dev->checkpointCurrentChunk++;
  826. + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock) {
  827. + dev->checkpointCurrentChunk = 0;
  828. + dev->checkpointCurrentBlock = -1;
  829. + }
  830. + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
  831. +
  832. + return 1;
  833. +}
  834. +
  835. +
  836. +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
  837. +{
  838. + int i = 0;
  839. + int ok = 1;
  840. +
  841. +
  842. + __u8 * dataBytes = (__u8 *)data;
  843. +
  844. +
  845. +
  846. + if (!dev->checkpointBuffer)
  847. + return 0;
  848. +
  849. + if (!dev->checkpointOpenForWrite)
  850. + return -1;
  851. +
  852. + while (i < nBytes && ok) {
  853. + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
  854. + dev->checkpointSum += *dataBytes;
  855. + dev->checkpointXor ^= *dataBytes;
  856. +
  857. + dev->checkpointByteOffset++;
  858. + i++;
  859. + dataBytes++;
  860. + dev->checkpointByteCount++;
  861. +
  862. +
  863. + if (dev->checkpointByteOffset < 0 ||
  864. + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
  865. + ok = yaffs_CheckpointFlushBuffer(dev);
  866. + }
  867. +
  868. + return i;
  869. +}
  870. +
  871. +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
  872. +{
  873. + int i = 0;
  874. + int ok = 1;
  875. + yaffs_ExtendedTags tags;
  876. +
  877. +
  878. + int chunk;
  879. + int realignedChunk;
  880. +
  881. + __u8 *dataBytes = (__u8 *)data;
  882. +
  883. + if (!dev->checkpointBuffer)
  884. + return 0;
  885. +
  886. + if (dev->checkpointOpenForWrite)
  887. + return -1;
  888. +
  889. + while (i < nBytes && ok) {
  890. +
  891. +
  892. + if (dev->checkpointByteOffset < 0 ||
  893. + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
  894. +
  895. + if (dev->checkpointCurrentBlock < 0) {
  896. + yaffs_CheckpointFindNextCheckpointBlock(dev);
  897. + dev->checkpointCurrentChunk = 0;
  898. + }
  899. +
  900. + if (dev->checkpointCurrentBlock < 0)
  901. + ok = 0;
  902. + else {
  903. + chunk = dev->checkpointCurrentBlock *
  904. + dev->nChunksPerBlock +
  905. + dev->checkpointCurrentChunk;
  906. +
  907. + realignedChunk = chunk - dev->chunkOffset;
  908. +
  909. + /* read in the next chunk */
  910. + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
  911. + dev->readChunkWithTagsFromNAND(dev,
  912. + realignedChunk,
  913. + dev->checkpointBuffer,
  914. + &tags);
  915. +
  916. + if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
  917. + tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
  918. + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
  919. + ok = 0;
  920. +
  921. + dev->checkpointByteOffset = 0;
  922. + dev->checkpointPageSequence++;
  923. + dev->checkpointCurrentChunk++;
  924. +
  925. + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
  926. + dev->checkpointCurrentBlock = -1;
  927. + }
  928. + }
  929. +
  930. + if (ok) {
  931. + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
  932. + dev->checkpointSum += *dataBytes;
  933. + dev->checkpointXor ^= *dataBytes;
  934. + dev->checkpointByteOffset++;
  935. + i++;
  936. + dataBytes++;
  937. + dev->checkpointByteCount++;
  938. + }
  939. + }
  940. +
  941. + return i;
  942. +}
  943. +
  944. +int yaffs_CheckpointClose(yaffs_Device *dev)
  945. +{
  946. +
  947. + if (dev->checkpointOpenForWrite) {
  948. + if (dev->checkpointByteOffset != 0)
  949. + yaffs_CheckpointFlushBuffer(dev);
  950. + } else {
  951. + int i;
  952. + for (i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++) {
  953. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointBlockList[i]);
  954. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
  955. + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
  956. + else {
  957. + /* Todo this looks odd... */
  958. + }
  959. + }
  960. + YFREE(dev->checkpointBlockList);
  961. + dev->checkpointBlockList = NULL;
  962. + }
  963. +
  964. + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
  965. + dev->nErasedBlocks -= dev->blocksInCheckpoint;
  966. +
  967. +
  968. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
  969. + dev->checkpointByteCount));
  970. +
  971. + if (dev->checkpointBuffer) {
  972. + /* free the buffer */
  973. + YFREE(dev->checkpointBuffer);
  974. + dev->checkpointBuffer = NULL;
  975. + return 1;
  976. + } else
  977. + return 0;
  978. +}
  979. +
  980. +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
  981. +{
  982. + /* Erase the first checksum block */
  983. +
  984. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate"TENDSTR)));
  985. +
  986. + if (!yaffs_CheckpointSpaceOk(dev))
  987. + return 0;
  988. +
  989. + return yaffs_CheckpointErase(dev);
  990. +}
  991. +
  992. +
  993. +
  994. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_checkptrw.h linux-2.6.32/fs/yaffs2/yaffs_checkptrw.h
  995. --- linux-2.6.32.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  996. +++ linux-2.6.32/fs/yaffs2/yaffs_checkptrw.h 2010-01-30 20:35:01.213084831 +0100
  997. @@ -0,0 +1,35 @@
  998. +/*
  999. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1000. + *
  1001. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1002. + * for Toby Churchill Ltd and Brightstar Engineering
  1003. + *
  1004. + * Created by Charles Manning <charles@aleph1.co.uk>
  1005. + *
  1006. + * This program is free software; you can redistribute it and/or modify
  1007. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1008. + * published by the Free Software Foundation.
  1009. + *
  1010. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1011. + */
  1012. +
  1013. +#ifndef __YAFFS_CHECKPTRW_H__
  1014. +#define __YAFFS_CHECKPTRW_H__
  1015. +
  1016. +#include "yaffs_guts.h"
  1017. +
  1018. +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
  1019. +
  1020. +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes);
  1021. +
  1022. +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes);
  1023. +
  1024. +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
  1025. +
  1026. +int yaffs_CheckpointClose(yaffs_Device *dev);
  1027. +
  1028. +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
  1029. +
  1030. +
  1031. +#endif
  1032. +
  1033. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_ecc.c linux-2.6.32/fs/yaffs2/yaffs_ecc.c
  1034. --- linux-2.6.32.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1035. +++ linux-2.6.32/fs/yaffs2/yaffs_ecc.c 2010-01-30 20:35:01.251829837 +0100
  1036. @@ -0,0 +1,326 @@
  1037. +/*
  1038. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1039. + *
  1040. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1041. + * for Toby Churchill Ltd and Brightstar Engineering
  1042. + *
  1043. + * Created by Charles Manning <charles@aleph1.co.uk>
  1044. + *
  1045. + * This program is free software; you can redistribute it and/or modify
  1046. + * it under the terms of the GNU General Public License version 2 as
  1047. + * published by the Free Software Foundation.
  1048. + */
  1049. +
  1050. +/*
  1051. + * This code implements the ECC algorithm used in SmartMedia.
  1052. + *
  1053. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1054. + * The two unused bit are set to 1.
  1055. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1056. + * blocks are used on a 512-byte NAND page.
  1057. + *
  1058. + */
  1059. +
  1060. +/* Table generated by gen-ecc.c
  1061. + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  1062. + * for each byte of data. These are instead provided in a table in bits7..2.
  1063. + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  1064. + * this bytes influence on the line parity.
  1065. + */
  1066. +
  1067. +const char *yaffs_ecc_c_version =
  1068. + "$Id: yaffs_ecc.c,v 1.11 2009-03-06 17:20:50 wookey Exp $";
  1069. +
  1070. +#include "yportenv.h"
  1071. +
  1072. +#include "yaffs_ecc.h"
  1073. +
  1074. +static const unsigned char column_parity_table[] = {
  1075. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1076. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1077. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1078. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1079. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1080. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1081. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1082. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1083. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1084. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1085. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1086. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1087. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1088. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1089. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1090. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1091. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1092. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1093. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1094. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1095. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1096. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1097. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1098. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1099. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1100. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1101. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1102. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1103. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1104. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1105. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1106. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1107. +};
  1108. +
  1109. +/* Count the bits in an unsigned char or a U32 */
  1110. +
  1111. +static int yaffs_CountBits(unsigned char x)
  1112. +{
  1113. + int r = 0;
  1114. + while (x) {
  1115. + if (x & 1)
  1116. + r++;
  1117. + x >>= 1;
  1118. + }
  1119. + return r;
  1120. +}
  1121. +
  1122. +static int yaffs_CountBits32(unsigned x)
  1123. +{
  1124. + int r = 0;
  1125. + while (x) {
  1126. + if (x & 1)
  1127. + r++;
  1128. + x >>= 1;
  1129. + }
  1130. + return r;
  1131. +}
  1132. +
  1133. +/* Calculate the ECC for a 256-byte block of data */
  1134. +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
  1135. +{
  1136. + unsigned int i;
  1137. +
  1138. + unsigned char col_parity = 0;
  1139. + unsigned char line_parity = 0;
  1140. + unsigned char line_parity_prime = 0;
  1141. + unsigned char t;
  1142. + unsigned char b;
  1143. +
  1144. + for (i = 0; i < 256; i++) {
  1145. + b = column_parity_table[*data++];
  1146. + col_parity ^= b;
  1147. +
  1148. + if (b & 0x01) { /* odd number of bits in the byte */
  1149. + line_parity ^= i;
  1150. + line_parity_prime ^= ~i;
  1151. + }
  1152. + }
  1153. +
  1154. + ecc[2] = (~col_parity) | 0x03;
  1155. +
  1156. + t = 0;
  1157. + if (line_parity & 0x80)
  1158. + t |= 0x80;
  1159. + if (line_parity_prime & 0x80)
  1160. + t |= 0x40;
  1161. + if (line_parity & 0x40)
  1162. + t |= 0x20;
  1163. + if (line_parity_prime & 0x40)
  1164. + t |= 0x10;
  1165. + if (line_parity & 0x20)
  1166. + t |= 0x08;
  1167. + if (line_parity_prime & 0x20)
  1168. + t |= 0x04;
  1169. + if (line_parity & 0x10)
  1170. + t |= 0x02;
  1171. + if (line_parity_prime & 0x10)
  1172. + t |= 0x01;
  1173. + ecc[1] = ~t;
  1174. +
  1175. + t = 0;
  1176. + if (line_parity & 0x08)
  1177. + t |= 0x80;
  1178. + if (line_parity_prime & 0x08)
  1179. + t |= 0x40;
  1180. + if (line_parity & 0x04)
  1181. + t |= 0x20;
  1182. + if (line_parity_prime & 0x04)
  1183. + t |= 0x10;
  1184. + if (line_parity & 0x02)
  1185. + t |= 0x08;
  1186. + if (line_parity_prime & 0x02)
  1187. + t |= 0x04;
  1188. + if (line_parity & 0x01)
  1189. + t |= 0x02;
  1190. + if (line_parity_prime & 0x01)
  1191. + t |= 0x01;
  1192. + ecc[0] = ~t;
  1193. +
  1194. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1195. + /* Swap the bytes into the wrong order */
  1196. + t = ecc[0];
  1197. + ecc[0] = ecc[1];
  1198. + ecc[1] = t;
  1199. +#endif
  1200. +}
  1201. +
  1202. +
  1203. +/* Correct the ECC on a 256 byte block of data */
  1204. +
  1205. +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  1206. + const unsigned char *test_ecc)
  1207. +{
  1208. + unsigned char d0, d1, d2; /* deltas */
  1209. +
  1210. + d0 = read_ecc[0] ^ test_ecc[0];
  1211. + d1 = read_ecc[1] ^ test_ecc[1];
  1212. + d2 = read_ecc[2] ^ test_ecc[2];
  1213. +
  1214. + if ((d0 | d1 | d2) == 0)
  1215. + return 0; /* no error */
  1216. +
  1217. + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  1218. + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  1219. + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  1220. + /* Single bit (recoverable) error in data */
  1221. +
  1222. + unsigned byte;
  1223. + unsigned bit;
  1224. +
  1225. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1226. + /* swap the bytes to correct for the wrong order */
  1227. + unsigned char t;
  1228. +
  1229. + t = d0;
  1230. + d0 = d1;
  1231. + d1 = t;
  1232. +#endif
  1233. +
  1234. + bit = byte = 0;
  1235. +
  1236. + if (d1 & 0x80)
  1237. + byte |= 0x80;
  1238. + if (d1 & 0x20)
  1239. + byte |= 0x40;
  1240. + if (d1 & 0x08)
  1241. + byte |= 0x20;
  1242. + if (d1 & 0x02)
  1243. + byte |= 0x10;
  1244. + if (d0 & 0x80)
  1245. + byte |= 0x08;
  1246. + if (d0 & 0x20)
  1247. + byte |= 0x04;
  1248. + if (d0 & 0x08)
  1249. + byte |= 0x02;
  1250. + if (d0 & 0x02)
  1251. + byte |= 0x01;
  1252. +
  1253. + if (d2 & 0x80)
  1254. + bit |= 0x04;
  1255. + if (d2 & 0x20)
  1256. + bit |= 0x02;
  1257. + if (d2 & 0x08)
  1258. + bit |= 0x01;
  1259. +
  1260. + data[byte] ^= (1 << bit);
  1261. +
  1262. + return 1; /* Corrected the error */
  1263. + }
  1264. +
  1265. + if ((yaffs_CountBits(d0) +
  1266. + yaffs_CountBits(d1) +
  1267. + yaffs_CountBits(d2)) == 1) {
  1268. + /* Reccoverable error in ecc */
  1269. +
  1270. + read_ecc[0] = test_ecc[0];
  1271. + read_ecc[1] = test_ecc[1];
  1272. + read_ecc[2] = test_ecc[2];
  1273. +
  1274. + return 1; /* Corrected the error */
  1275. + }
  1276. +
  1277. + /* Unrecoverable error */
  1278. +
  1279. + return -1;
  1280. +
  1281. +}
  1282. +
  1283. +
  1284. +/*
  1285. + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  1286. + */
  1287. +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  1288. + yaffs_ECCOther *eccOther)
  1289. +{
  1290. + unsigned int i;
  1291. +
  1292. + unsigned char col_parity = 0;
  1293. + unsigned line_parity = 0;
  1294. + unsigned line_parity_prime = 0;
  1295. + unsigned char b;
  1296. +
  1297. + for (i = 0; i < nBytes; i++) {
  1298. + b = column_parity_table[*data++];
  1299. + col_parity ^= b;
  1300. +
  1301. + if (b & 0x01) {
  1302. + /* odd number of bits in the byte */
  1303. + line_parity ^= i;
  1304. + line_parity_prime ^= ~i;
  1305. + }
  1306. +
  1307. + }
  1308. +
  1309. + eccOther->colParity = (col_parity >> 2) & 0x3f;
  1310. + eccOther->lineParity = line_parity;
  1311. + eccOther->lineParityPrime = line_parity_prime;
  1312. +}
  1313. +
  1314. +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  1315. + yaffs_ECCOther *read_ecc,
  1316. + const yaffs_ECCOther *test_ecc)
  1317. +{
  1318. + unsigned char cDelta; /* column parity delta */
  1319. + unsigned lDelta; /* line parity delta */
  1320. + unsigned lDeltaPrime; /* line parity delta */
  1321. + unsigned bit;
  1322. +
  1323. + cDelta = read_ecc->colParity ^ test_ecc->colParity;
  1324. + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
  1325. + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
  1326. +
  1327. + if ((cDelta | lDelta | lDeltaPrime) == 0)
  1328. + return 0; /* no error */
  1329. +
  1330. + if (lDelta == ~lDeltaPrime &&
  1331. + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) {
  1332. + /* Single bit (recoverable) error in data */
  1333. +
  1334. + bit = 0;
  1335. +
  1336. + if (cDelta & 0x20)
  1337. + bit |= 0x04;
  1338. + if (cDelta & 0x08)
  1339. + bit |= 0x02;
  1340. + if (cDelta & 0x02)
  1341. + bit |= 0x01;
  1342. +
  1343. + if (lDelta >= nBytes)
  1344. + return -1;
  1345. +
  1346. + data[lDelta] ^= (1 << bit);
  1347. +
  1348. + return 1; /* corrected */
  1349. + }
  1350. +
  1351. + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
  1352. + yaffs_CountBits(cDelta)) == 1) {
  1353. + /* Reccoverable error in ecc */
  1354. +
  1355. + *read_ecc = *test_ecc;
  1356. + return 1; /* corrected */
  1357. + }
  1358. +
  1359. + /* Unrecoverable error */
  1360. +
  1361. + return -1;
  1362. +}
  1363. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_ecc.h linux-2.6.32/fs/yaffs2/yaffs_ecc.h
  1364. --- linux-2.6.32.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1365. +++ linux-2.6.32/fs/yaffs2/yaffs_ecc.h 2010-01-30 20:35:01.292693842 +0100
  1366. @@ -0,0 +1,44 @@
  1367. +/*
  1368. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1369. + *
  1370. + * Copyright (C) 2002-2007 Aleph One Ltd.
  1371. + * for Toby Churchill Ltd and Brightstar Engineering
  1372. + *
  1373. + * Created by Charles Manning <charles@aleph1.co.uk>
  1374. + *
  1375. + * This program is free software; you can redistribute it and/or modify
  1376. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1377. + * published by the Free Software Foundation.
  1378. + *
  1379. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1380. + */
  1381. +
  1382. +/*
  1383. + * This code implements the ECC algorithm used in SmartMedia.
  1384. + *
  1385. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1386. + * The two unused bit are set to 1.
  1387. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1388. + * blocks are used on a 512-byte NAND page.
  1389. + *
  1390. + */
  1391. +
  1392. +#ifndef __YAFFS_ECC_H__
  1393. +#define __YAFFS_ECC_H__
  1394. +
  1395. +typedef struct {
  1396. + unsigned char colParity;
  1397. + unsigned lineParity;
  1398. + unsigned lineParityPrime;
  1399. +} yaffs_ECCOther;
  1400. +
  1401. +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
  1402. +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  1403. + const unsigned char *test_ecc);
  1404. +
  1405. +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  1406. + yaffs_ECCOther *ecc);
  1407. +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  1408. + yaffs_ECCOther *read_ecc,
  1409. + const yaffs_ECCOther *test_ecc);
  1410. +#endif
  1411. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_fs.c linux-2.6.32/fs/yaffs2/yaffs_fs.c
  1412. --- linux-2.6.32.orig/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
  1413. +++ linux-2.6.32/fs/yaffs2/yaffs_fs.c 2010-01-30 20:35:01.331845579 +0100
  1414. @@ -0,0 +1,2529 @@
  1415. +/*
  1416. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1417. + *
  1418. + * Copyright (C) 2002-2009 Aleph One Ltd.
  1419. + * for Toby Churchill Ltd and Brightstar Engineering
  1420. + *
  1421. + * Created by Charles Manning <charles@aleph1.co.uk>
  1422. + * Acknowledgements:
  1423. + * Luc van OostenRyck for numerous patches.
  1424. + * Nick Bane for numerous patches.
  1425. + * Nick Bane for 2.5/2.6 integration.
  1426. + * Andras Toth for mknod rdev issue.
  1427. + * Michael Fischer for finding the problem with inode inconsistency.
  1428. + * Some code bodily lifted from JFFS
  1429. + *
  1430. + * This program is free software; you can redistribute it and/or modify
  1431. + * it under the terms of the GNU General Public License version 2 as
  1432. + * published by the Free Software Foundation.
  1433. + */
  1434. +
  1435. +/*
  1436. + *
  1437. + * This is the file system front-end to YAFFS that hooks it up to
  1438. + * the VFS.
  1439. + *
  1440. + * Special notes:
  1441. + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
  1442. + * this superblock
  1443. + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
  1444. + * superblock
  1445. + * >> inode->u.generic_ip points to the associated yaffs_Object.
  1446. + */
  1447. +
  1448. +const char *yaffs_fs_c_version =
  1449. + "$Id: yaffs_fs.c,v 1.79 2009-03-17 01:12:00 wookey Exp $";
  1450. +extern const char *yaffs_guts_c_version;
  1451. +
  1452. +#include <linux/version.h>
  1453. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  1454. +#include <linux/config.h>
  1455. +#endif
  1456. +#include <linux/kernel.h>
  1457. +#include <linux/module.h>
  1458. +#include <linux/slab.h>
  1459. +#include <linux/init.h>
  1460. +#include <linux/fs.h>
  1461. +#include <linux/proc_fs.h>
  1462. +#include <linux/smp_lock.h>
  1463. +#include <linux/pagemap.h>
  1464. +#include <linux/mtd/mtd.h>
  1465. +#include <linux/interrupt.h>
  1466. +#include <linux/string.h>
  1467. +#include <linux/ctype.h>
  1468. +
  1469. +#include "asm/div64.h"
  1470. +
  1471. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1472. +
  1473. +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
  1474. +#include <linux/statfs.h>
  1475. +#define UnlockPage(p) unlock_page(p)
  1476. +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  1477. +
  1478. +/* FIXME: use sb->s_id instead ? */
  1479. +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  1480. +
  1481. +#else
  1482. +
  1483. +#include <linux/locks.h>
  1484. +#define BDEVNAME_SIZE 0
  1485. +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
  1486. +
  1487. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
  1488. +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
  1489. +#define __user
  1490. +#endif
  1491. +
  1492. +#endif
  1493. +
  1494. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  1495. +#define YPROC_ROOT (&proc_root)
  1496. +#else
  1497. +#define YPROC_ROOT NULL
  1498. +#endif
  1499. +
  1500. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1501. +#define WRITE_SIZE_STR "writesize"
  1502. +#define WRITE_SIZE(mtd) ((mtd)->writesize)
  1503. +#else
  1504. +#define WRITE_SIZE_STR "oobblock"
  1505. +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
  1506. +#endif
  1507. +
  1508. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
  1509. +#define YAFFS_USE_WRITE_BEGIN_END 1
  1510. +#else
  1511. +#define YAFFS_USE_WRITE_BEGIN_END 0
  1512. +#endif
  1513. +
  1514. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
  1515. +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  1516. +{
  1517. + uint64_t result = partition_size;
  1518. + do_div(result, block_size);
  1519. + return (uint32_t)result;
  1520. +}
  1521. +#else
  1522. +#define YCALCBLOCKS(s, b) ((s)/(b))
  1523. +#endif
  1524. +
  1525. +#include <linux/uaccess.h>
  1526. +
  1527. +#include "yportenv.h"
  1528. +#include "yaffs_guts.h"
  1529. +
  1530. +#include <linux/mtd/mtd.h>
  1531. +#include "yaffs_mtdif.h"
  1532. +#include "yaffs_mtdif1.h"
  1533. +#include "yaffs_mtdif2.h"
  1534. +
  1535. +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
  1536. +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  1537. +unsigned int yaffs_auto_checkpoint = 1;
  1538. +
  1539. +/* Module Parameters */
  1540. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1541. +module_param(yaffs_traceMask, uint, 0644);
  1542. +module_param(yaffs_wr_attempts, uint, 0644);
  1543. +module_param(yaffs_auto_checkpoint, uint, 0644);
  1544. +#else
  1545. +MODULE_PARM(yaffs_traceMask, "i");
  1546. +MODULE_PARM(yaffs_wr_attempts, "i");
  1547. +MODULE_PARM(yaffs_auto_checkpoint, "i");
  1548. +#endif
  1549. +
  1550. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  1551. +/* use iget and read_inode */
  1552. +#define Y_IGET(sb, inum) iget((sb), (inum))
  1553. +static void yaffs_read_inode(struct inode *inode);
  1554. +
  1555. +#else
  1556. +/* Call local equivalent */
  1557. +#define YAFFS_USE_OWN_IGET
  1558. +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
  1559. +
  1560. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
  1561. +#endif
  1562. +
  1563. +/*#define T(x) printk x */
  1564. +
  1565. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  1566. +#define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
  1567. +#else
  1568. +#define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
  1569. +#endif
  1570. +
  1571. +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
  1572. +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
  1573. +
  1574. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1575. +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
  1576. +#else
  1577. +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
  1578. +#endif
  1579. +
  1580. +static void yaffs_put_super(struct super_block *sb);
  1581. +
  1582. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  1583. + loff_t *pos);
  1584. +static ssize_t yaffs_hold_space(struct file *f);
  1585. +static void yaffs_release_space(struct file *f);
  1586. +
  1587. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1588. +static int yaffs_file_flush(struct file *file, fl_owner_t id);
  1589. +#else
  1590. +static int yaffs_file_flush(struct file *file);
  1591. +#endif
  1592. +
  1593. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  1594. + int datasync);
  1595. +
  1596. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
  1597. +
  1598. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1599. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  1600. + struct nameidata *n);
  1601. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  1602. + struct nameidata *n);
  1603. +#else
  1604. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
  1605. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
  1606. +#endif
  1607. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  1608. + struct dentry *dentry);
  1609. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
  1610. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  1611. + const char *symname);
  1612. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  1613. +
  1614. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1615. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  1616. + dev_t dev);
  1617. +#else
  1618. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  1619. + int dev);
  1620. +#endif
  1621. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1622. + struct inode *new_dir, struct dentry *new_dentry);
  1623. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
  1624. +
  1625. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1626. +static int yaffs_sync_fs(struct super_block *sb, int wait);
  1627. +static void yaffs_write_super(struct super_block *sb);
  1628. +#else
  1629. +static int yaffs_sync_fs(struct super_block *sb);
  1630. +static int yaffs_write_super(struct super_block *sb);
  1631. +#endif
  1632. +
  1633. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1634. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
  1635. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1636. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
  1637. +#else
  1638. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
  1639. +#endif
  1640. +
  1641. +#ifdef YAFFS_HAS_PUT_INODE
  1642. +static void yaffs_put_inode(struct inode *inode);
  1643. +#endif
  1644. +
  1645. +static void yaffs_delete_inode(struct inode *);
  1646. +static void yaffs_clear_inode(struct inode *);
  1647. +
  1648. +static int yaffs_readpage(struct file *file, struct page *page);
  1649. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1650. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
  1651. +#else
  1652. +static int yaffs_writepage(struct page *page);
  1653. +#endif
  1654. +
  1655. +
  1656. +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
  1657. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  1658. + loff_t pos, unsigned len, unsigned flags,
  1659. + struct page **pagep, void **fsdata);
  1660. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  1661. + loff_t pos, unsigned len, unsigned copied,
  1662. + struct page *pg, void *fsdadata);
  1663. +#else
  1664. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  1665. + unsigned offset, unsigned to);
  1666. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  1667. + unsigned to);
  1668. +
  1669. +#endif
  1670. +
  1671. +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
  1672. + int buflen);
  1673. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1674. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  1675. +#else
  1676. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  1677. +#endif
  1678. +
  1679. +static struct address_space_operations yaffs_file_address_operations = {
  1680. + .readpage = yaffs_readpage,
  1681. + .writepage = yaffs_writepage,
  1682. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  1683. + .write_begin = yaffs_write_begin,
  1684. + .write_end = yaffs_write_end,
  1685. +#else
  1686. + .prepare_write = yaffs_prepare_write,
  1687. + .commit_write = yaffs_commit_write,
  1688. +#endif
  1689. +};
  1690. +
  1691. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
  1692. +static const struct file_operations yaffs_file_operations = {
  1693. + .read = do_sync_read,
  1694. + .write = do_sync_write,
  1695. + .aio_read = generic_file_aio_read,
  1696. + .aio_write = generic_file_aio_write,
  1697. + .mmap = generic_file_mmap,
  1698. + .flush = yaffs_file_flush,
  1699. + .fsync = yaffs_sync_object,
  1700. + .splice_read = generic_file_splice_read,
  1701. + .splice_write = generic_file_splice_write,
  1702. + .llseek = generic_file_llseek,
  1703. +};
  1704. +
  1705. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  1706. +
  1707. +static const struct file_operations yaffs_file_operations = {
  1708. + .read = do_sync_read,
  1709. + .write = do_sync_write,
  1710. + .aio_read = generic_file_aio_read,
  1711. + .aio_write = generic_file_aio_write,
  1712. + .mmap = generic_file_mmap,
  1713. + .flush = yaffs_file_flush,
  1714. + .fsync = yaffs_sync_object,
  1715. + .sendfile = generic_file_sendfile,
  1716. +};
  1717. +
  1718. +#else
  1719. +
  1720. +static const struct file_operations yaffs_file_operations = {
  1721. + .read = generic_file_read,
  1722. + .write = generic_file_write,
  1723. + .mmap = generic_file_mmap,
  1724. + .flush = yaffs_file_flush,
  1725. + .fsync = yaffs_sync_object,
  1726. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1727. + .sendfile = generic_file_sendfile,
  1728. +#endif
  1729. +};
  1730. +#endif
  1731. +
  1732. +static const struct inode_operations yaffs_file_inode_operations = {
  1733. + .setattr = yaffs_setattr,
  1734. +};
  1735. +
  1736. +static const struct inode_operations yaffs_symlink_inode_operations = {
  1737. + .readlink = yaffs_readlink,
  1738. + .follow_link = yaffs_follow_link,
  1739. + .setattr = yaffs_setattr,
  1740. +};
  1741. +
  1742. +static const struct inode_operations yaffs_dir_inode_operations = {
  1743. + .create = yaffs_create,
  1744. + .lookup = yaffs_lookup,
  1745. + .link = yaffs_link,
  1746. + .unlink = yaffs_unlink,
  1747. + .symlink = yaffs_symlink,
  1748. + .mkdir = yaffs_mkdir,
  1749. + .rmdir = yaffs_unlink,
  1750. + .mknod = yaffs_mknod,
  1751. + .rename = yaffs_rename,
  1752. + .setattr = yaffs_setattr,
  1753. +};
  1754. +
  1755. +static const struct file_operations yaffs_dir_operations = {
  1756. + .read = generic_read_dir,
  1757. + .readdir = yaffs_readdir,
  1758. + .fsync = yaffs_sync_object,
  1759. +};
  1760. +
  1761. +static const struct super_operations yaffs_super_ops = {
  1762. + .statfs = yaffs_statfs,
  1763. +
  1764. +#ifndef YAFFS_USE_OWN_IGET
  1765. + .read_inode = yaffs_read_inode,
  1766. +#endif
  1767. +#ifdef YAFFS_HAS_PUT_INODE
  1768. + .put_inode = yaffs_put_inode,
  1769. +#endif
  1770. + .put_super = yaffs_put_super,
  1771. + .delete_inode = yaffs_delete_inode,
  1772. + .clear_inode = yaffs_clear_inode,
  1773. + .sync_fs = yaffs_sync_fs,
  1774. + .write_super = yaffs_write_super,
  1775. +};
  1776. +
  1777. +static void yaffs_GrossLock(yaffs_Device *dev)
  1778. +{
  1779. + T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
  1780. + down(&dev->grossLock);
  1781. + T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
  1782. +}
  1783. +
  1784. +static void yaffs_GrossUnlock(yaffs_Device *dev)
  1785. +{
  1786. + T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
  1787. + up(&dev->grossLock);
  1788. +}
  1789. +
  1790. +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
  1791. + int buflen)
  1792. +{
  1793. + unsigned char *alias;
  1794. + int ret;
  1795. +
  1796. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  1797. +
  1798. + yaffs_GrossLock(dev);
  1799. +
  1800. + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
  1801. +
  1802. + yaffs_GrossUnlock(dev);
  1803. +
  1804. + if (!alias)
  1805. + return -ENOMEM;
  1806. +
  1807. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  1808. + kfree(alias);
  1809. + return ret;
  1810. +}
  1811. +
  1812. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1813. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1814. +#else
  1815. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1816. +#endif
  1817. +{
  1818. + unsigned char *alias;
  1819. + int ret;
  1820. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  1821. +
  1822. + yaffs_GrossLock(dev);
  1823. +
  1824. + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
  1825. +
  1826. + yaffs_GrossUnlock(dev);
  1827. +
  1828. + if (!alias) {
  1829. + ret = -ENOMEM;
  1830. + goto out;
  1831. + }
  1832. +
  1833. + ret = vfs_follow_link(nd, alias);
  1834. + kfree(alias);
  1835. +out:
  1836. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1837. + return ERR_PTR(ret);
  1838. +#else
  1839. + return ret;
  1840. +#endif
  1841. +}
  1842. +
  1843. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  1844. + yaffs_Object *obj);
  1845. +
  1846. +/*
  1847. + * Lookup is used to find objects in the fs
  1848. + */
  1849. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  1850. +
  1851. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  1852. + struct nameidata *n)
  1853. +#else
  1854. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  1855. +#endif
  1856. +{
  1857. + yaffs_Object *obj;
  1858. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  1859. +
  1860. + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
  1861. +
  1862. + yaffs_GrossLock(dev);
  1863. +
  1864. + T(YAFFS_TRACE_OS,
  1865. + ("yaffs_lookup for %d:%s\n",
  1866. + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
  1867. +
  1868. + obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
  1869. + dentry->d_name.name);
  1870. +
  1871. + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
  1872. +
  1873. + /* Can't hold gross lock when calling yaffs_get_inode() */
  1874. + yaffs_GrossUnlock(dev);
  1875. +
  1876. + if (obj) {
  1877. + T(YAFFS_TRACE_OS,
  1878. + ("yaffs_lookup found %d\n", obj->objectId));
  1879. +
  1880. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  1881. +
  1882. + if (inode) {
  1883. + T(YAFFS_TRACE_OS,
  1884. + ("yaffs_loookup dentry \n"));
  1885. +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
  1886. + * d_add even if NULL inode */
  1887. +#if 0
  1888. + /*dget(dentry); // try to solve directory bug */
  1889. + d_add(dentry, inode);
  1890. +
  1891. + /* return dentry; */
  1892. + return NULL;
  1893. +#endif
  1894. + }
  1895. +
  1896. + } else {
  1897. + T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
  1898. +
  1899. + }
  1900. +
  1901. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  1902. + * NULL which creates dentry hash */
  1903. + d_add(dentry, inode);
  1904. +
  1905. + return NULL;
  1906. +}
  1907. +
  1908. +
  1909. +#ifdef YAFFS_HAS_PUT_INODE
  1910. +
  1911. +/* For now put inode is just for debugging
  1912. + * Put inode is called when the inode **structure** is put.
  1913. + */
  1914. +static void yaffs_put_inode(struct inode *inode)
  1915. +{
  1916. + T(YAFFS_TRACE_OS,
  1917. + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
  1918. + atomic_read(&inode->i_count)));
  1919. +
  1920. +}
  1921. +#endif
  1922. +
  1923. +/* clear is called to tell the fs to release any per-inode data it holds */
  1924. +static void yaffs_clear_inode(struct inode *inode)
  1925. +{
  1926. + yaffs_Object *obj;
  1927. + yaffs_Device *dev;
  1928. +
  1929. + obj = yaffs_InodeToObject(inode);
  1930. +
  1931. + T(YAFFS_TRACE_OS,
  1932. + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
  1933. + atomic_read(&inode->i_count),
  1934. + obj ? "object exists" : "null object"));
  1935. +
  1936. + if (obj) {
  1937. + dev = obj->myDev;
  1938. + yaffs_GrossLock(dev);
  1939. +
  1940. + /* Clear the association between the inode and
  1941. + * the yaffs_Object.
  1942. + */
  1943. + obj->myInode = NULL;
  1944. + yaffs_InodeToObjectLV(inode) = NULL;
  1945. +
  1946. + /* If the object freeing was deferred, then the real
  1947. + * free happens now.
  1948. + * This should fix the inode inconsistency problem.
  1949. + */
  1950. +
  1951. + yaffs_HandleDeferedFree(obj);
  1952. +
  1953. + yaffs_GrossUnlock(dev);
  1954. + }
  1955. +
  1956. +}
  1957. +
  1958. +/* delete is called when the link count is zero and the inode
  1959. + * is put (ie. nobody wants to know about it anymore, time to
  1960. + * delete the file).
  1961. + * NB Must call clear_inode()
  1962. + */
  1963. +static void yaffs_delete_inode(struct inode *inode)
  1964. +{
  1965. + yaffs_Object *obj = yaffs_InodeToObject(inode);
  1966. + yaffs_Device *dev;
  1967. +
  1968. + T(YAFFS_TRACE_OS,
  1969. + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
  1970. + atomic_read(&inode->i_count),
  1971. + obj ? "object exists" : "null object"));
  1972. +
  1973. + if (obj) {
  1974. + dev = obj->myDev;
  1975. + yaffs_GrossLock(dev);
  1976. + yaffs_DeleteObject(obj);
  1977. + yaffs_GrossUnlock(dev);
  1978. + }
  1979. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  1980. + truncate_inode_pages(&inode->i_data, 0);
  1981. +#endif
  1982. + clear_inode(inode);
  1983. +}
  1984. +
  1985. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  1986. +static int yaffs_file_flush(struct file *file, fl_owner_t id)
  1987. +#else
  1988. +static int yaffs_file_flush(struct file *file)
  1989. +#endif
  1990. +{
  1991. + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
  1992. +
  1993. + yaffs_Device *dev = obj->myDev;
  1994. +
  1995. + T(YAFFS_TRACE_OS,
  1996. + ("yaffs_file_flush object %d (%s)\n", obj->objectId,
  1997. + obj->dirty ? "dirty" : "clean"));
  1998. +
  1999. + yaffs_GrossLock(dev);
  2000. +
  2001. + yaffs_FlushFile(obj, 1);
  2002. +
  2003. + yaffs_GrossUnlock(dev);
  2004. +
  2005. + return 0;
  2006. +}
  2007. +
  2008. +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  2009. +{
  2010. + /* Lifted from jffs2 */
  2011. +
  2012. + yaffs_Object *obj;
  2013. + unsigned char *pg_buf;
  2014. + int ret;
  2015. +
  2016. + yaffs_Device *dev;
  2017. +
  2018. + T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
  2019. + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
  2020. + (unsigned)PAGE_CACHE_SIZE));
  2021. +
  2022. + obj = yaffs_DentryToObject(f->f_dentry);
  2023. +
  2024. + dev = obj->myDev;
  2025. +
  2026. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2027. + BUG_ON(!PageLocked(pg));
  2028. +#else
  2029. + if (!PageLocked(pg))
  2030. + PAGE_BUG(pg);
  2031. +#endif
  2032. +
  2033. + pg_buf = kmap(pg);
  2034. + /* FIXME: Can kmap fail? */
  2035. +
  2036. + yaffs_GrossLock(dev);
  2037. +
  2038. + ret = yaffs_ReadDataFromFile(obj, pg_buf,
  2039. + pg->index << PAGE_CACHE_SHIFT,
  2040. + PAGE_CACHE_SIZE);
  2041. +
  2042. + yaffs_GrossUnlock(dev);
  2043. +
  2044. + if (ret >= 0)
  2045. + ret = 0;
  2046. +
  2047. + if (ret) {
  2048. + ClearPageUptodate(pg);
  2049. + SetPageError(pg);
  2050. + } else {
  2051. + SetPageUptodate(pg);
  2052. + ClearPageError(pg);
  2053. + }
  2054. +
  2055. + flush_dcache_page(pg);
  2056. + kunmap(pg);
  2057. +
  2058. + T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
  2059. + return ret;
  2060. +}
  2061. +
  2062. +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  2063. +{
  2064. + int ret = yaffs_readpage_nolock(f, pg);
  2065. + UnlockPage(pg);
  2066. + return ret;
  2067. +}
  2068. +
  2069. +static int yaffs_readpage(struct file *f, struct page *pg)
  2070. +{
  2071. + return yaffs_readpage_unlock(f, pg);
  2072. +}
  2073. +
  2074. +/* writepage inspired by/stolen from smbfs */
  2075. +
  2076. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2077. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  2078. +#else
  2079. +static int yaffs_writepage(struct page *page)
  2080. +#endif
  2081. +{
  2082. + struct address_space *mapping = page->mapping;
  2083. + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
  2084. + struct inode *inode;
  2085. + unsigned long end_index;
  2086. + char *buffer;
  2087. + yaffs_Object *obj;
  2088. + int nWritten = 0;
  2089. + unsigned nBytes;
  2090. +
  2091. + if (!mapping)
  2092. + BUG();
  2093. + inode = mapping->host;
  2094. + if (!inode)
  2095. + BUG();
  2096. +
  2097. + if (offset > inode->i_size) {
  2098. + T(YAFFS_TRACE_OS,
  2099. + ("yaffs_writepage at %08x, inode size = %08x!!!\n",
  2100. + (unsigned)(page->index << PAGE_CACHE_SHIFT),
  2101. + (unsigned)inode->i_size));
  2102. + T(YAFFS_TRACE_OS,
  2103. + (" -> don't care!!\n"));
  2104. + unlock_page(page);
  2105. + return 0;
  2106. + }
  2107. +
  2108. + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  2109. +
  2110. + /* easy case */
  2111. + if (page->index < end_index)
  2112. + nBytes = PAGE_CACHE_SIZE;
  2113. + else
  2114. + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
  2115. +
  2116. + get_page(page);
  2117. +
  2118. + buffer = kmap(page);
  2119. +
  2120. + obj = yaffs_InodeToObject(inode);
  2121. + yaffs_GrossLock(obj->myDev);
  2122. +
  2123. + T(YAFFS_TRACE_OS,
  2124. + ("yaffs_writepage at %08x, size %08x\n",
  2125. + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
  2126. + T(YAFFS_TRACE_OS,
  2127. + ("writepag0: obj = %05x, ino = %05x\n",
  2128. + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
  2129. +
  2130. + nWritten = yaffs_WriteDataToFile(obj, buffer,
  2131. + page->index << PAGE_CACHE_SHIFT, nBytes, 0);
  2132. +
  2133. + T(YAFFS_TRACE_OS,
  2134. + ("writepag1: obj = %05x, ino = %05x\n",
  2135. + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
  2136. +
  2137. + yaffs_GrossUnlock(obj->myDev);
  2138. +
  2139. + kunmap(page);
  2140. + SetPageUptodate(page);
  2141. + UnlockPage(page);
  2142. + put_page(page);
  2143. +
  2144. + return (nWritten == nBytes) ? 0 : -ENOSPC;
  2145. +}
  2146. +
  2147. +
  2148. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  2149. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  2150. + loff_t pos, unsigned len, unsigned flags,
  2151. + struct page **pagep, void **fsdata)
  2152. +{
  2153. + struct page *pg = NULL;
  2154. + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  2155. + uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
  2156. + uint32_t to = offset + len;
  2157. +
  2158. + int ret = 0;
  2159. + int space_held = 0;
  2160. +
  2161. + T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
  2162. + /* Get a page */
  2163. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28)
  2164. + pg = grab_cache_page_write_begin(mapping, index, flags);
  2165. +#else
  2166. + pg = __grab_cache_page(mapping, index);
  2167. +#endif
  2168. +
  2169. + *pagep = pg;
  2170. + if (!pg) {
  2171. + ret = -ENOMEM;
  2172. + goto out;
  2173. + }
  2174. + /* Get fs space */
  2175. + space_held = yaffs_hold_space(filp);
  2176. +
  2177. + if (!space_held) {
  2178. + ret = -ENOSPC;
  2179. + goto out;
  2180. + }
  2181. +
  2182. + /* Update page if required */
  2183. +
  2184. + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
  2185. + ret = yaffs_readpage_nolock(filp, pg);
  2186. +
  2187. + if (ret)
  2188. + goto out;
  2189. +
  2190. + /* Happy path return */
  2191. + T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
  2192. +
  2193. + return 0;
  2194. +
  2195. +out:
  2196. + T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
  2197. + if (space_held)
  2198. + yaffs_release_space(filp);
  2199. + if (pg) {
  2200. + unlock_page(pg);
  2201. + page_cache_release(pg);
  2202. + }
  2203. + return ret;
  2204. +}
  2205. +
  2206. +#else
  2207. +
  2208. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  2209. + unsigned offset, unsigned to)
  2210. +{
  2211. + T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
  2212. +
  2213. + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
  2214. + return yaffs_readpage_nolock(f, pg);
  2215. + return 0;
  2216. +}
  2217. +#endif
  2218. +
  2219. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  2220. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  2221. + loff_t pos, unsigned len, unsigned copied,
  2222. + struct page *pg, void *fsdadata)
  2223. +{
  2224. + int ret = 0;
  2225. + void *addr, *kva;
  2226. + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  2227. +
  2228. + kva = kmap(pg);
  2229. + addr = kva + offset_into_page;
  2230. +
  2231. + T(YAFFS_TRACE_OS,
  2232. + ("yaffs_write_end addr %x pos %x nBytes %d\n",
  2233. + (unsigned) addr,
  2234. + (int)pos, copied));
  2235. +
  2236. + ret = yaffs_file_write(filp, addr, copied, &pos);
  2237. +
  2238. + if (ret != copied) {
  2239. + T(YAFFS_TRACE_OS,
  2240. + ("yaffs_write_end not same size ret %d copied %d\n",
  2241. + ret, copied));
  2242. + SetPageError(pg);
  2243. + ClearPageUptodate(pg);
  2244. + } else {
  2245. + SetPageUptodate(pg);
  2246. + }
  2247. +
  2248. + kunmap(pg);
  2249. +
  2250. + yaffs_release_space(filp);
  2251. + unlock_page(pg);
  2252. + page_cache_release(pg);
  2253. + return ret;
  2254. +}
  2255. +#else
  2256. +
  2257. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  2258. + unsigned to)
  2259. +{
  2260. + void *addr, *kva;
  2261. +
  2262. + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
  2263. + int nBytes = to - offset;
  2264. + int nWritten;
  2265. +
  2266. + unsigned spos = pos;
  2267. + unsigned saddr;
  2268. +
  2269. + kva = kmap(pg);
  2270. + addr = kva + offset;
  2271. +
  2272. + saddr = (unsigned) addr;
  2273. +
  2274. + T(YAFFS_TRACE_OS,
  2275. + ("yaffs_commit_write addr %x pos %x nBytes %d\n",
  2276. + saddr, spos, nBytes));
  2277. +
  2278. + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
  2279. +
  2280. + if (nWritten != nBytes) {
  2281. + T(YAFFS_TRACE_OS,
  2282. + ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
  2283. + nWritten, nBytes));
  2284. + SetPageError(pg);
  2285. + ClearPageUptodate(pg);
  2286. + } else {
  2287. + SetPageUptodate(pg);
  2288. + }
  2289. +
  2290. + kunmap(pg);
  2291. +
  2292. + T(YAFFS_TRACE_OS,
  2293. + ("yaffs_commit_write returning %d\n",
  2294. + nWritten == nBytes ? 0 : nWritten));
  2295. +
  2296. + return nWritten == nBytes ? 0 : nWritten;
  2297. +}
  2298. +#endif
  2299. +
  2300. +
  2301. +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
  2302. +{
  2303. + if (inode && obj) {
  2304. +
  2305. +
  2306. + /* Check mode against the variant type and attempt to repair if broken. */
  2307. + __u32 mode = obj->yst_mode;
  2308. + switch (obj->variantType) {
  2309. + case YAFFS_OBJECT_TYPE_FILE:
  2310. + if (!S_ISREG(mode)) {
  2311. + obj->yst_mode &= ~S_IFMT;
  2312. + obj->yst_mode |= S_IFREG;
  2313. + }
  2314. +
  2315. + break;
  2316. + case YAFFS_OBJECT_TYPE_SYMLINK:
  2317. + if (!S_ISLNK(mode)) {
  2318. + obj->yst_mode &= ~S_IFMT;
  2319. + obj->yst_mode |= S_IFLNK;
  2320. + }
  2321. +
  2322. + break;
  2323. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  2324. + if (!S_ISDIR(mode)) {
  2325. + obj->yst_mode &= ~S_IFMT;
  2326. + obj->yst_mode |= S_IFDIR;
  2327. + }
  2328. +
  2329. + break;
  2330. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  2331. + case YAFFS_OBJECT_TYPE_HARDLINK:
  2332. + case YAFFS_OBJECT_TYPE_SPECIAL:
  2333. + default:
  2334. + /* TODO? */
  2335. + break;
  2336. + }
  2337. +
  2338. + inode->i_flags |= S_NOATIME;
  2339. +
  2340. + inode->i_ino = obj->objectId;
  2341. + inode->i_mode = obj->yst_mode;
  2342. + inode->i_uid = obj->yst_uid;
  2343. + inode->i_gid = obj->yst_gid;
  2344. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  2345. + inode->i_blksize = inode->i_sb->s_blocksize;
  2346. +#endif
  2347. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2348. +
  2349. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  2350. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  2351. + inode->i_atime.tv_nsec = 0;
  2352. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  2353. + inode->i_mtime.tv_nsec = 0;
  2354. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  2355. + inode->i_ctime.tv_nsec = 0;
  2356. +#else
  2357. + inode->i_rdev = obj->yst_rdev;
  2358. + inode->i_atime = obj->yst_atime;
  2359. + inode->i_mtime = obj->yst_mtime;
  2360. + inode->i_ctime = obj->yst_ctime;
  2361. +#endif
  2362. + inode->i_size = yaffs_GetObjectFileLength(obj);
  2363. + inode->i_blocks = (inode->i_size + 511) >> 9;
  2364. +
  2365. + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
  2366. +
  2367. + T(YAFFS_TRACE_OS,
  2368. + ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
  2369. + inode->i_mode, inode->i_uid, inode->i_gid,
  2370. + (int)inode->i_size, atomic_read(&inode->i_count)));
  2371. +
  2372. + switch (obj->yst_mode & S_IFMT) {
  2373. + default: /* fifo, device or socket */
  2374. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2375. + init_special_inode(inode, obj->yst_mode,
  2376. + old_decode_dev(obj->yst_rdev));
  2377. +#else
  2378. + init_special_inode(inode, obj->yst_mode,
  2379. + (dev_t) (obj->yst_rdev));
  2380. +#endif
  2381. + break;
  2382. + case S_IFREG: /* file */
  2383. + inode->i_op = &yaffs_file_inode_operations;
  2384. + inode->i_fop = &yaffs_file_operations;
  2385. + inode->i_mapping->a_ops =
  2386. + &yaffs_file_address_operations;
  2387. + break;
  2388. + case S_IFDIR: /* directory */
  2389. + inode->i_op = &yaffs_dir_inode_operations;
  2390. + inode->i_fop = &yaffs_dir_operations;
  2391. + break;
  2392. + case S_IFLNK: /* symlink */
  2393. + inode->i_op = &yaffs_symlink_inode_operations;
  2394. + break;
  2395. + }
  2396. +
  2397. + yaffs_InodeToObjectLV(inode) = obj;
  2398. +
  2399. + obj->myInode = inode;
  2400. +
  2401. + } else {
  2402. + T(YAFFS_TRACE_OS,
  2403. + ("yaffs_FileInode invalid parameters\n"));
  2404. + }
  2405. +
  2406. +}
  2407. +
  2408. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  2409. + yaffs_Object *obj)
  2410. +{
  2411. + struct inode *inode;
  2412. +
  2413. + if (!sb) {
  2414. + T(YAFFS_TRACE_OS,
  2415. + ("yaffs_get_inode for NULL super_block!!\n"));
  2416. + return NULL;
  2417. +
  2418. + }
  2419. +
  2420. + if (!obj) {
  2421. + T(YAFFS_TRACE_OS,
  2422. + ("yaffs_get_inode for NULL object!!\n"));
  2423. + return NULL;
  2424. +
  2425. + }
  2426. +
  2427. + T(YAFFS_TRACE_OS,
  2428. + ("yaffs_get_inode for object %d\n", obj->objectId));
  2429. +
  2430. + inode = Y_IGET(sb, obj->objectId);
  2431. + if (IS_ERR(inode))
  2432. + return NULL;
  2433. +
  2434. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  2435. + /* iget also increments the inode's i_count */
  2436. + /* NB You can't be holding grossLock or deadlock will happen! */
  2437. +
  2438. + return inode;
  2439. +}
  2440. +
  2441. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  2442. + loff_t *pos)
  2443. +{
  2444. + yaffs_Object *obj;
  2445. + int nWritten, ipos;
  2446. + struct inode *inode;
  2447. + yaffs_Device *dev;
  2448. +
  2449. + obj = yaffs_DentryToObject(f->f_dentry);
  2450. +
  2451. + dev = obj->myDev;
  2452. +
  2453. + yaffs_GrossLock(dev);
  2454. +
  2455. + inode = f->f_dentry->d_inode;
  2456. +
  2457. + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  2458. + ipos = inode->i_size;
  2459. + else
  2460. + ipos = *pos;
  2461. +
  2462. + if (!obj)
  2463. + T(YAFFS_TRACE_OS,
  2464. + ("yaffs_file_write: hey obj is null!\n"));
  2465. + else
  2466. + T(YAFFS_TRACE_OS,
  2467. + ("yaffs_file_write about to write writing %zu bytes"
  2468. + "to object %d at %d\n",
  2469. + n, obj->objectId, ipos));
  2470. +
  2471. + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
  2472. +
  2473. + T(YAFFS_TRACE_OS,
  2474. + ("yaffs_file_write writing %zu bytes, %d written at %d\n",
  2475. + n, nWritten, ipos));
  2476. +
  2477. + if (nWritten > 0) {
  2478. + ipos += nWritten;
  2479. + *pos = ipos;
  2480. + if (ipos > inode->i_size) {
  2481. + inode->i_size = ipos;
  2482. + inode->i_blocks = (ipos + 511) >> 9;
  2483. +
  2484. + T(YAFFS_TRACE_OS,
  2485. + ("yaffs_file_write size updated to %d bytes, "
  2486. + "%d blocks\n",
  2487. + ipos, (int)(inode->i_blocks)));
  2488. + }
  2489. +
  2490. + }
  2491. + yaffs_GrossUnlock(dev);
  2492. + return nWritten == 0 ? -ENOSPC : nWritten;
  2493. +}
  2494. +
  2495. +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
  2496. +/* For now we just assume few parallel writes and check against a small number. */
  2497. +/* Todo: need to do this with a counter to handle parallel reads better */
  2498. +
  2499. +static ssize_t yaffs_hold_space(struct file *f)
  2500. +{
  2501. + yaffs_Object *obj;
  2502. + yaffs_Device *dev;
  2503. +
  2504. + int nFreeChunks;
  2505. +
  2506. +
  2507. + obj = yaffs_DentryToObject(f->f_dentry);
  2508. +
  2509. + dev = obj->myDev;
  2510. +
  2511. + yaffs_GrossLock(dev);
  2512. +
  2513. + nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
  2514. +
  2515. + yaffs_GrossUnlock(dev);
  2516. +
  2517. + return (nFreeChunks > 20) ? 1 : 0;
  2518. +}
  2519. +
  2520. +static void yaffs_release_space(struct file *f)
  2521. +{
  2522. + yaffs_Object *obj;
  2523. + yaffs_Device *dev;
  2524. +
  2525. +
  2526. + obj = yaffs_DentryToObject(f->f_dentry);
  2527. +
  2528. + dev = obj->myDev;
  2529. +
  2530. + yaffs_GrossLock(dev);
  2531. +
  2532. +
  2533. + yaffs_GrossUnlock(dev);
  2534. +}
  2535. +
  2536. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  2537. +{
  2538. + yaffs_Object *obj;
  2539. + yaffs_Device *dev;
  2540. + struct inode *inode = f->f_dentry->d_inode;
  2541. + unsigned long offset, curoffs;
  2542. + struct ylist_head *i;
  2543. + yaffs_Object *l;
  2544. +
  2545. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  2546. +
  2547. + obj = yaffs_DentryToObject(f->f_dentry);
  2548. + dev = obj->myDev;
  2549. +
  2550. + yaffs_GrossLock(dev);
  2551. +
  2552. + offset = f->f_pos;
  2553. +
  2554. + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
  2555. +
  2556. + if (offset == 0) {
  2557. + T(YAFFS_TRACE_OS,
  2558. + ("yaffs_readdir: entry . ino %d \n",
  2559. + (int)inode->i_ino));
  2560. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
  2561. + goto out;
  2562. + offset++;
  2563. + f->f_pos++;
  2564. + }
  2565. + if (offset == 1) {
  2566. + T(YAFFS_TRACE_OS,
  2567. + ("yaffs_readdir: entry .. ino %d \n",
  2568. + (int)f->f_dentry->d_parent->d_inode->i_ino));
  2569. + if (filldir(dirent, "..", 2, offset,
  2570. + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
  2571. + goto out;
  2572. + offset++;
  2573. + f->f_pos++;
  2574. + }
  2575. +
  2576. + curoffs = 1;
  2577. +
  2578. + /* If the directory has changed since the open or last call to
  2579. + readdir, rewind to after the 2 canned entries. */
  2580. +
  2581. + if (f->f_version != inode->i_version) {
  2582. + offset = 2;
  2583. + f->f_pos = offset;
  2584. + f->f_version = inode->i_version;
  2585. + }
  2586. +
  2587. + ylist_for_each(i, &obj->variant.directoryVariant.children) {
  2588. + curoffs++;
  2589. + if (curoffs >= offset) {
  2590. + l = ylist_entry(i, yaffs_Object, siblings);
  2591. +
  2592. + yaffs_GetObjectName(l, name,
  2593. + YAFFS_MAX_NAME_LENGTH + 1);
  2594. + T(YAFFS_TRACE_OS,
  2595. + ("yaffs_readdir: %s inode %d\n", name,
  2596. + yaffs_GetObjectInode(l)));
  2597. +
  2598. + if (filldir(dirent,
  2599. + name,
  2600. + strlen(name),
  2601. + offset,
  2602. + yaffs_GetObjectInode(l),
  2603. + yaffs_GetObjectType(l)) < 0)
  2604. + goto up_and_out;
  2605. +
  2606. + offset++;
  2607. + f->f_pos++;
  2608. + }
  2609. + }
  2610. +
  2611. +up_and_out:
  2612. +out:
  2613. + yaffs_GrossUnlock(dev);
  2614. +
  2615. + return 0;
  2616. +}
  2617. +
  2618. +/*
  2619. + * File creation. Allocate an inode, and we're done..
  2620. + */
  2621. +
  2622. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  2623. +#define YCRED(x) x
  2624. +#else
  2625. +#define YCRED(x) (x->cred)
  2626. +#endif
  2627. +
  2628. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2629. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  2630. + dev_t rdev)
  2631. +#else
  2632. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  2633. + int rdev)
  2634. +#endif
  2635. +{
  2636. + struct inode *inode;
  2637. +
  2638. + yaffs_Object *obj = NULL;
  2639. + yaffs_Device *dev;
  2640. +
  2641. + yaffs_Object *parent = yaffs_InodeToObject(dir);
  2642. +
  2643. + int error = -ENOSPC;
  2644. + uid_t uid = YCRED(current)->fsuid;
  2645. + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  2646. +
  2647. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  2648. + mode |= S_ISGID;
  2649. +
  2650. + if (parent) {
  2651. + T(YAFFS_TRACE_OS,
  2652. + ("yaffs_mknod: parent object %d type %d\n",
  2653. + parent->objectId, parent->variantType));
  2654. + } else {
  2655. + T(YAFFS_TRACE_OS,
  2656. + ("yaffs_mknod: could not get parent object\n"));
  2657. + return -EPERM;
  2658. + }
  2659. +
  2660. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
  2661. + "mode %x dev %x\n",
  2662. + dentry->d_name.name, mode, rdev));
  2663. +
  2664. + dev = parent->myDev;
  2665. +
  2666. + yaffs_GrossLock(dev);
  2667. +
  2668. + switch (mode & S_IFMT) {
  2669. + default:
  2670. + /* Special (socket, fifo, device...) */
  2671. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
  2672. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2673. + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
  2674. + gid, old_encode_dev(rdev));
  2675. +#else
  2676. + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
  2677. + gid, rdev);
  2678. +#endif
  2679. + break;
  2680. + case S_IFREG: /* file */
  2681. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
  2682. + obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
  2683. + gid);
  2684. + break;
  2685. + case S_IFDIR: /* directory */
  2686. + T(YAFFS_TRACE_OS,
  2687. + ("yaffs_mknod: making directory\n"));
  2688. + obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
  2689. + uid, gid);
  2690. + break;
  2691. + case S_IFLNK: /* symlink */
  2692. + T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
  2693. + obj = NULL; /* Do we ever get here? */
  2694. + break;
  2695. + }
  2696. +
  2697. + /* Can not call yaffs_get_inode() with gross lock held */
  2698. + yaffs_GrossUnlock(dev);
  2699. +
  2700. + if (obj) {
  2701. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  2702. + d_instantiate(dentry, inode);
  2703. + T(YAFFS_TRACE_OS,
  2704. + ("yaffs_mknod created object %d count = %d\n",
  2705. + obj->objectId, atomic_read(&inode->i_count)));
  2706. + error = 0;
  2707. + } else {
  2708. + T(YAFFS_TRACE_OS,
  2709. + ("yaffs_mknod failed making object\n"));
  2710. + error = -ENOMEM;
  2711. + }
  2712. +
  2713. + return error;
  2714. +}
  2715. +
  2716. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2717. +{
  2718. + int retVal;
  2719. + T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
  2720. + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  2721. + return retVal;
  2722. +}
  2723. +
  2724. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2725. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  2726. + struct nameidata *n)
  2727. +#else
  2728. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  2729. +#endif
  2730. +{
  2731. + T(YAFFS_TRACE_OS, ("yaffs_create\n"));
  2732. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  2733. +}
  2734. +
  2735. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  2736. +{
  2737. + int retVal;
  2738. +
  2739. + yaffs_Device *dev;
  2740. +
  2741. + T(YAFFS_TRACE_OS,
  2742. + ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
  2743. + dentry->d_name.name));
  2744. +
  2745. + dev = yaffs_InodeToObject(dir)->myDev;
  2746. +
  2747. + yaffs_GrossLock(dev);
  2748. +
  2749. + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
  2750. +
  2751. + if (retVal == YAFFS_OK) {
  2752. + dentry->d_inode->i_nlink--;
  2753. + dir->i_version++;
  2754. + yaffs_GrossUnlock(dev);
  2755. + mark_inode_dirty(dentry->d_inode);
  2756. + return 0;
  2757. + }
  2758. + yaffs_GrossUnlock(dev);
  2759. + return -ENOTEMPTY;
  2760. +}
  2761. +
  2762. +/*
  2763. + * Create a link...
  2764. + */
  2765. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  2766. + struct dentry *dentry)
  2767. +{
  2768. + struct inode *inode = old_dentry->d_inode;
  2769. + yaffs_Object *obj = NULL;
  2770. + yaffs_Object *link = NULL;
  2771. + yaffs_Device *dev;
  2772. +
  2773. + T(YAFFS_TRACE_OS, ("yaffs_link\n"));
  2774. +
  2775. + obj = yaffs_InodeToObject(inode);
  2776. + dev = obj->myDev;
  2777. +
  2778. + yaffs_GrossLock(dev);
  2779. +
  2780. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  2781. + link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
  2782. + obj);
  2783. +
  2784. + if (link) {
  2785. + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
  2786. + d_instantiate(dentry, old_dentry->d_inode);
  2787. + atomic_inc(&old_dentry->d_inode->i_count);
  2788. + T(YAFFS_TRACE_OS,
  2789. + ("yaffs_link link count %d i_count %d\n",
  2790. + old_dentry->d_inode->i_nlink,
  2791. + atomic_read(&old_dentry->d_inode->i_count)));
  2792. + }
  2793. +
  2794. + yaffs_GrossUnlock(dev);
  2795. +
  2796. + if (link)
  2797. + return 0;
  2798. +
  2799. + return -EPERM;
  2800. +}
  2801. +
  2802. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  2803. + const char *symname)
  2804. +{
  2805. + yaffs_Object *obj;
  2806. + yaffs_Device *dev;
  2807. + uid_t uid = YCRED(current)->fsuid;
  2808. + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  2809. +
  2810. + T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
  2811. +
  2812. + dev = yaffs_InodeToObject(dir)->myDev;
  2813. + yaffs_GrossLock(dev);
  2814. + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
  2815. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  2816. + yaffs_GrossUnlock(dev);
  2817. +
  2818. + if (obj) {
  2819. + struct inode *inode;
  2820. +
  2821. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  2822. + d_instantiate(dentry, inode);
  2823. + T(YAFFS_TRACE_OS, ("symlink created OK\n"));
  2824. + return 0;
  2825. + } else {
  2826. + T(YAFFS_TRACE_OS, ("symlink not created\n"));
  2827. + }
  2828. +
  2829. + return -ENOMEM;
  2830. +}
  2831. +
  2832. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  2833. + int datasync)
  2834. +{
  2835. +
  2836. + yaffs_Object *obj;
  2837. + yaffs_Device *dev;
  2838. +
  2839. + obj = yaffs_DentryToObject(dentry);
  2840. +
  2841. + dev = obj->myDev;
  2842. +
  2843. + T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
  2844. + yaffs_GrossLock(dev);
  2845. + yaffs_FlushFile(obj, 1);
  2846. + yaffs_GrossUnlock(dev);
  2847. + return 0;
  2848. +}
  2849. +
  2850. +/*
  2851. + * The VFS layer already does all the dentry stuff for rename.
  2852. + *
  2853. + * NB: POSIX says you can rename an object over an old object of the same name
  2854. + */
  2855. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2856. + struct inode *new_dir, struct dentry *new_dentry)
  2857. +{
  2858. + yaffs_Device *dev;
  2859. + int retVal = YAFFS_FAIL;
  2860. + yaffs_Object *target;
  2861. +
  2862. + T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
  2863. + dev = yaffs_InodeToObject(old_dir)->myDev;
  2864. +
  2865. + yaffs_GrossLock(dev);
  2866. +
  2867. + /* Check if the target is an existing directory that is not empty. */
  2868. + target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
  2869. + new_dentry->d_name.name);
  2870. +
  2871. +
  2872. +
  2873. + if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
  2874. + !ylist_empty(&target->variant.directoryVariant.children)) {
  2875. +
  2876. + T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
  2877. +
  2878. + retVal = YAFFS_FAIL;
  2879. + } else {
  2880. + /* Now does unlinking internally using shadowing mechanism */
  2881. + T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
  2882. +
  2883. + retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
  2884. + old_dentry->d_name.name,
  2885. + yaffs_InodeToObject(new_dir),
  2886. + new_dentry->d_name.name);
  2887. + }
  2888. + yaffs_GrossUnlock(dev);
  2889. +
  2890. + if (retVal == YAFFS_OK) {
  2891. + if (target) {
  2892. + new_dentry->d_inode->i_nlink--;
  2893. + mark_inode_dirty(new_dentry->d_inode);
  2894. + }
  2895. +
  2896. + return 0;
  2897. + } else {
  2898. + return -ENOTEMPTY;
  2899. + }
  2900. +}
  2901. +
  2902. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  2903. +{
  2904. + struct inode *inode = dentry->d_inode;
  2905. + int error;
  2906. + yaffs_Device *dev;
  2907. +
  2908. + T(YAFFS_TRACE_OS,
  2909. + ("yaffs_setattr of object %d\n",
  2910. + yaffs_InodeToObject(inode)->objectId));
  2911. +
  2912. + error = inode_change_ok(inode, attr);
  2913. + if (error == 0) {
  2914. + dev = yaffs_InodeToObject(inode)->myDev;
  2915. + yaffs_GrossLock(dev);
  2916. + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
  2917. + YAFFS_OK) {
  2918. + error = 0;
  2919. + } else {
  2920. + error = -EPERM;
  2921. + }
  2922. + yaffs_GrossUnlock(dev);
  2923. + if (!error)
  2924. + error = inode_setattr(inode, attr);
  2925. + }
  2926. + return error;
  2927. +}
  2928. +
  2929. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  2930. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  2931. +{
  2932. + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
  2933. + struct super_block *sb = dentry->d_sb;
  2934. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  2935. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  2936. +{
  2937. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  2938. +#else
  2939. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  2940. +{
  2941. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  2942. +#endif
  2943. +
  2944. + T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
  2945. +
  2946. + yaffs_GrossLock(dev);
  2947. +
  2948. + buf->f_type = YAFFS_MAGIC;
  2949. + buf->f_bsize = sb->s_blocksize;
  2950. + buf->f_namelen = 255;
  2951. +
  2952. + if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
  2953. + /* Do this if chunk size is not a power of 2 */
  2954. +
  2955. + uint64_t bytesInDev;
  2956. + uint64_t bytesFree;
  2957. +
  2958. + bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
  2959. + ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
  2960. +
  2961. + do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
  2962. + buf->f_blocks = bytesInDev;
  2963. +
  2964. + bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
  2965. + ((uint64_t)(dev->nDataBytesPerChunk));
  2966. +
  2967. + do_div(bytesFree, sb->s_blocksize);
  2968. +
  2969. + buf->f_bfree = bytesFree;
  2970. +
  2971. + } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
  2972. +
  2973. + buf->f_blocks =
  2974. + (dev->endBlock - dev->startBlock + 1) *
  2975. + dev->nChunksPerBlock /
  2976. + (sb->s_blocksize / dev->nDataBytesPerChunk);
  2977. + buf->f_bfree =
  2978. + yaffs_GetNumberOfFreeChunks(dev) /
  2979. + (sb->s_blocksize / dev->nDataBytesPerChunk);
  2980. + } else {
  2981. + buf->f_blocks =
  2982. + (dev->endBlock - dev->startBlock + 1) *
  2983. + dev->nChunksPerBlock *
  2984. + (dev->nDataBytesPerChunk / sb->s_blocksize);
  2985. +
  2986. + buf->f_bfree =
  2987. + yaffs_GetNumberOfFreeChunks(dev) *
  2988. + (dev->nDataBytesPerChunk / sb->s_blocksize);
  2989. + }
  2990. +
  2991. + buf->f_files = 0;
  2992. + buf->f_ffree = 0;
  2993. + buf->f_bavail = buf->f_bfree;
  2994. +
  2995. + yaffs_GrossUnlock(dev);
  2996. + return 0;
  2997. +}
  2998. +
  2999. +
  3000. +static int yaffs_do_sync_fs(struct super_block *sb)
  3001. +{
  3002. +
  3003. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3004. + T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
  3005. +
  3006. + if (sb->s_dirt) {
  3007. + yaffs_GrossLock(dev);
  3008. +
  3009. + if (dev) {
  3010. + yaffs_FlushEntireDeviceCache(dev);
  3011. + yaffs_CheckpointSave(dev);
  3012. + }
  3013. +
  3014. + yaffs_GrossUnlock(dev);
  3015. +
  3016. + sb->s_dirt = 0;
  3017. + }
  3018. + return 0;
  3019. +}
  3020. +
  3021. +
  3022. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3023. +static void yaffs_write_super(struct super_block *sb)
  3024. +#else
  3025. +static int yaffs_write_super(struct super_block *sb)
  3026. +#endif
  3027. +{
  3028. +
  3029. + T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
  3030. + if (yaffs_auto_checkpoint >= 2)
  3031. + yaffs_do_sync_fs(sb);
  3032. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  3033. + return 0;
  3034. +#endif
  3035. +}
  3036. +
  3037. +
  3038. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3039. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  3040. +#else
  3041. +static int yaffs_sync_fs(struct super_block *sb)
  3042. +#endif
  3043. +{
  3044. + T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
  3045. +
  3046. + if (yaffs_auto_checkpoint >= 1)
  3047. + yaffs_do_sync_fs(sb);
  3048. +
  3049. + return 0;
  3050. +}
  3051. +
  3052. +#ifdef YAFFS_USE_OWN_IGET
  3053. +
  3054. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  3055. +{
  3056. + struct inode *inode;
  3057. + yaffs_Object *obj;
  3058. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3059. +
  3060. + T(YAFFS_TRACE_OS,
  3061. + ("yaffs_iget for %lu\n", ino));
  3062. +
  3063. + inode = iget_locked(sb, ino);
  3064. + if (!inode)
  3065. + return ERR_PTR(-ENOMEM);
  3066. + if (!(inode->i_state & I_NEW))
  3067. + return inode;
  3068. +
  3069. + /* NB This is called as a side effect of other functions, but
  3070. + * we had to release the lock to prevent deadlocks, so
  3071. + * need to lock again.
  3072. + */
  3073. +
  3074. + yaffs_GrossLock(dev);
  3075. +
  3076. + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
  3077. +
  3078. + yaffs_FillInodeFromObject(inode, obj);
  3079. +
  3080. + yaffs_GrossUnlock(dev);
  3081. +
  3082. + unlock_new_inode(inode);
  3083. + return inode;
  3084. +}
  3085. +
  3086. +#else
  3087. +
  3088. +static void yaffs_read_inode(struct inode *inode)
  3089. +{
  3090. + /* NB This is called as a side effect of other functions, but
  3091. + * we had to release the lock to prevent deadlocks, so
  3092. + * need to lock again.
  3093. + */
  3094. +
  3095. + yaffs_Object *obj;
  3096. + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
  3097. +
  3098. + T(YAFFS_TRACE_OS,
  3099. + ("yaffs_read_inode for %d\n", (int)inode->i_ino));
  3100. +
  3101. + yaffs_GrossLock(dev);
  3102. +
  3103. + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
  3104. +
  3105. + yaffs_FillInodeFromObject(inode, obj);
  3106. +
  3107. + yaffs_GrossUnlock(dev);
  3108. +}
  3109. +
  3110. +#endif
  3111. +
  3112. +static YLIST_HEAD(yaffs_dev_list);
  3113. +
  3114. +#if 0 /* not used */
  3115. +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
  3116. +{
  3117. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3118. +
  3119. + if (*flags & MS_RDONLY) {
  3120. + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
  3121. +
  3122. + T(YAFFS_TRACE_OS,
  3123. + ("yaffs_remount_fs: %s: RO\n", dev->name));
  3124. +
  3125. + yaffs_GrossLock(dev);
  3126. +
  3127. + yaffs_FlushEntireDeviceCache(dev);
  3128. +
  3129. + yaffs_CheckpointSave(dev);
  3130. +
  3131. + if (mtd->sync)
  3132. + mtd->sync(mtd);
  3133. +
  3134. + yaffs_GrossUnlock(dev);
  3135. + } else {
  3136. + T(YAFFS_TRACE_OS,
  3137. + ("yaffs_remount_fs: %s: RW\n", dev->name));
  3138. + }
  3139. +
  3140. + return 0;
  3141. +}
  3142. +#endif
  3143. +
  3144. +static void yaffs_put_super(struct super_block *sb)
  3145. +{
  3146. + yaffs_Device *dev = yaffs_SuperToDevice(sb);
  3147. +
  3148. + T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
  3149. +
  3150. + yaffs_GrossLock(dev);
  3151. +
  3152. + yaffs_FlushEntireDeviceCache(dev);
  3153. +
  3154. + yaffs_CheckpointSave(dev);
  3155. +
  3156. + if (dev->putSuperFunc)
  3157. + dev->putSuperFunc(sb);
  3158. +
  3159. + yaffs_Deinitialise(dev);
  3160. +
  3161. + yaffs_GrossUnlock(dev);
  3162. +
  3163. + /* we assume this is protected by lock_kernel() in mount/umount */
  3164. + ylist_del(&dev->devList);
  3165. +
  3166. + if (dev->spareBuffer) {
  3167. + YFREE(dev->spareBuffer);
  3168. + dev->spareBuffer = NULL;
  3169. + }
  3170. +
  3171. + kfree(dev);
  3172. +}
  3173. +
  3174. +
  3175. +static void yaffs_MTDPutSuper(struct super_block *sb)
  3176. +{
  3177. + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
  3178. +
  3179. + if (mtd->sync)
  3180. + mtd->sync(mtd);
  3181. +
  3182. + put_mtd_device(mtd);
  3183. +}
  3184. +
  3185. +
  3186. +static void yaffs_MarkSuperBlockDirty(void *vsb)
  3187. +{
  3188. + struct super_block *sb = (struct super_block *)vsb;
  3189. +
  3190. + T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
  3191. + if (sb)
  3192. + sb->s_dirt = 1;
  3193. +}
  3194. +
  3195. +typedef struct {
  3196. + int inband_tags;
  3197. + int skip_checkpoint_read;
  3198. + int skip_checkpoint_write;
  3199. + int no_cache;
  3200. +} yaffs_options;
  3201. +
  3202. +#define MAX_OPT_LEN 20
  3203. +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
  3204. +{
  3205. + char cur_opt[MAX_OPT_LEN + 1];
  3206. + int p;
  3207. + int error = 0;
  3208. +
  3209. + /* Parse through the options which is a comma seperated list */
  3210. +
  3211. + while (options_str && *options_str && !error) {
  3212. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  3213. + p = 0;
  3214. +
  3215. + while (*options_str && *options_str != ',') {
  3216. + if (p < MAX_OPT_LEN) {
  3217. + cur_opt[p] = *options_str;
  3218. + p++;
  3219. + }
  3220. + options_str++;
  3221. + }
  3222. +
  3223. + if (!strcmp(cur_opt, "inband-tags"))
  3224. + options->inband_tags = 1;
  3225. + else if (!strcmp(cur_opt, "no-cache"))
  3226. + options->no_cache = 1;
  3227. + else if (!strcmp(cur_opt, "no-checkpoint-read"))
  3228. + options->skip_checkpoint_read = 1;
  3229. + else if (!strcmp(cur_opt, "no-checkpoint-write"))
  3230. + options->skip_checkpoint_write = 1;
  3231. + else if (!strcmp(cur_opt, "no-checkpoint")) {
  3232. + options->skip_checkpoint_read = 1;
  3233. + options->skip_checkpoint_write = 1;
  3234. + } else {
  3235. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  3236. + cur_opt);
  3237. + error = 1;
  3238. + }
  3239. + }
  3240. +
  3241. + return error;
  3242. +}
  3243. +
  3244. +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
  3245. + struct super_block *sb,
  3246. + void *data, int silent)
  3247. +{
  3248. + int nBlocks;
  3249. + struct inode *inode = NULL;
  3250. + struct dentry *root;
  3251. + yaffs_Device *dev = 0;
  3252. + char devname_buf[BDEVNAME_SIZE + 1];
  3253. + struct mtd_info *mtd;
  3254. + int err;
  3255. + char *data_str = (char *)data;
  3256. +
  3257. + yaffs_options options;
  3258. +
  3259. + sb->s_magic = YAFFS_MAGIC;
  3260. + sb->s_op = &yaffs_super_ops;
  3261. + sb->s_flags |= MS_NOATIME;
  3262. +
  3263. + if (!sb)
  3264. + printk(KERN_INFO "yaffs: sb is NULL\n");
  3265. + else if (!sb->s_dev)
  3266. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  3267. + else if (!yaffs_devname(sb, devname_buf))
  3268. + printk(KERN_INFO "yaffs: devname is NULL\n");
  3269. + else
  3270. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
  3271. + sb->s_dev,
  3272. + yaffs_devname(sb, devname_buf));
  3273. +
  3274. + if (!data_str)
  3275. + data_str = "";
  3276. +
  3277. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  3278. +
  3279. + memset(&options, 0, sizeof(options));
  3280. +
  3281. + if (yaffs_parse_options(&options, data_str)) {
  3282. + /* Option parsing failed */
  3283. + return NULL;
  3284. + }
  3285. +
  3286. +
  3287. + sb->s_blocksize = PAGE_CACHE_SIZE;
  3288. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  3289. + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
  3290. + T(YAFFS_TRACE_OS,
  3291. + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
  3292. +
  3293. +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
  3294. + T(YAFFS_TRACE_OS,
  3295. + ("yaffs: Write verification disabled. All guarantees "
  3296. + "null and void\n"));
  3297. +#endif
  3298. +
  3299. + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
  3300. + "\"%s\"\n",
  3301. + MAJOR(sb->s_dev), MINOR(sb->s_dev),
  3302. + yaffs_devname(sb, devname_buf)));
  3303. +
  3304. + /* Check it's an mtd device..... */
  3305. + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
  3306. + return NULL; /* This isn't an mtd device */
  3307. +
  3308. + /* Get the device */
  3309. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  3310. + if (!mtd) {
  3311. + T(YAFFS_TRACE_ALWAYS,
  3312. + ("yaffs: MTD device #%u doesn't appear to exist\n",
  3313. + MINOR(sb->s_dev)));
  3314. + return NULL;
  3315. + }
  3316. + /* Check it's NAND */
  3317. + if (mtd->type != MTD_NANDFLASH) {
  3318. + T(YAFFS_TRACE_ALWAYS,
  3319. + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
  3320. + return NULL;
  3321. + }
  3322. +
  3323. + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
  3324. + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
  3325. + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
  3326. + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
  3327. + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
  3328. + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
  3329. + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
  3330. + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
  3331. + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
  3332. + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
  3333. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  3334. + T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
  3335. +#else
  3336. + T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
  3337. +#endif
  3338. +
  3339. +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
  3340. +
  3341. + if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
  3342. + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
  3343. + yaffsVersion = 2;
  3344. + }
  3345. +
  3346. + /* Added NCB 26/5/2006 for completeness */
  3347. + if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
  3348. + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
  3349. + yaffsVersion = 1;
  3350. + }
  3351. +
  3352. +#endif
  3353. +
  3354. + if (yaffsVersion == 2) {
  3355. + /* Check for version 2 style functions */
  3356. + if (!mtd->erase ||
  3357. + !mtd->block_isbad ||
  3358. + !mtd->block_markbad ||
  3359. + !mtd->read ||
  3360. + !mtd->write ||
  3361. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3362. + !mtd->read_oob || !mtd->write_oob) {
  3363. +#else
  3364. + !mtd->write_ecc ||
  3365. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  3366. +#endif
  3367. + T(YAFFS_TRACE_ALWAYS,
  3368. + ("yaffs: MTD device does not support required "
  3369. + "functions\n"));;
  3370. + return NULL;
  3371. + }
  3372. +
  3373. + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  3374. + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  3375. + !options.inband_tags) {
  3376. + T(YAFFS_TRACE_ALWAYS,
  3377. + ("yaffs: MTD device does not have the "
  3378. + "right page sizes\n"));
  3379. + return NULL;
  3380. + }
  3381. + } else {
  3382. + /* Check for V1 style functions */
  3383. + if (!mtd->erase ||
  3384. + !mtd->read ||
  3385. + !mtd->write ||
  3386. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3387. + !mtd->read_oob || !mtd->write_oob) {
  3388. +#else
  3389. + !mtd->write_ecc ||
  3390. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  3391. +#endif
  3392. + T(YAFFS_TRACE_ALWAYS,
  3393. + ("yaffs: MTD device does not support required "
  3394. + "functions\n"));;
  3395. + return NULL;
  3396. + }
  3397. +
  3398. + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  3399. + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  3400. + T(YAFFS_TRACE_ALWAYS,
  3401. + ("yaffs: MTD device does not support have the "
  3402. + "right page sizes\n"));
  3403. + return NULL;
  3404. + }
  3405. + }
  3406. +
  3407. + /* OK, so if we got here, we have an MTD that's NAND and looks
  3408. + * like it has the right capabilities
  3409. + * Set the yaffs_Device up for mtd
  3410. + */
  3411. +
  3412. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3413. + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
  3414. +#else
  3415. + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
  3416. +#endif
  3417. + if (!dev) {
  3418. + /* Deep shit could not allocate device structure */
  3419. + T(YAFFS_TRACE_ALWAYS,
  3420. + ("yaffs_read_super: Failed trying to allocate "
  3421. + "yaffs_Device. \n"));
  3422. + return NULL;
  3423. + }
  3424. +
  3425. + memset(dev, 0, sizeof(yaffs_Device));
  3426. + dev->genericDevice = mtd;
  3427. + dev->name = mtd->name;
  3428. +
  3429. + /* Set up the memory size parameters.... */
  3430. +
  3431. + nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
  3432. +
  3433. + dev->startBlock = 0;
  3434. + dev->endBlock = nBlocks - 1;
  3435. + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
  3436. + dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
  3437. + dev->nReservedBlocks = 5;
  3438. + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
  3439. + dev->inbandTags = options.inband_tags;
  3440. +
  3441. + /* ... and the functions. */
  3442. + if (yaffsVersion == 2) {
  3443. + dev->writeChunkWithTagsToNAND =
  3444. + nandmtd2_WriteChunkWithTagsToNAND;
  3445. + dev->readChunkWithTagsFromNAND =
  3446. + nandmtd2_ReadChunkWithTagsFromNAND;
  3447. + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
  3448. + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
  3449. + dev->spareBuffer = YMALLOC(mtd->oobsize);
  3450. + dev->isYaffs2 = 1;
  3451. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3452. + dev->totalBytesPerChunk = mtd->writesize;
  3453. + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
  3454. +#else
  3455. + dev->totalBytesPerChunk = mtd->oobblock;
  3456. + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
  3457. +#endif
  3458. + nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  3459. +
  3460. + dev->startBlock = 0;
  3461. + dev->endBlock = nBlocks - 1;
  3462. + } else {
  3463. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3464. + /* use the MTD interface in yaffs_mtdif1.c */
  3465. + dev->writeChunkWithTagsToNAND =
  3466. + nandmtd1_WriteChunkWithTagsToNAND;
  3467. + dev->readChunkWithTagsFromNAND =
  3468. + nandmtd1_ReadChunkWithTagsFromNAND;
  3469. + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
  3470. + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
  3471. +#else
  3472. + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
  3473. + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
  3474. +#endif
  3475. + dev->isYaffs2 = 0;
  3476. + }
  3477. + /* ... and common functions */
  3478. + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
  3479. + dev->initialiseNAND = nandmtd_InitialiseNAND;
  3480. +
  3481. + dev->putSuperFunc = yaffs_MTDPutSuper;
  3482. +
  3483. + dev->superBlock = (void *)sb;
  3484. + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
  3485. +
  3486. +
  3487. +#ifndef CONFIG_YAFFS_DOES_ECC
  3488. + dev->useNANDECC = 1;
  3489. +#endif
  3490. +
  3491. +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
  3492. + dev->wideTnodesDisabled = 1;
  3493. +#endif
  3494. +
  3495. + dev->skipCheckpointRead = options.skip_checkpoint_read;
  3496. + dev->skipCheckpointWrite = options.skip_checkpoint_write;
  3497. +
  3498. + /* we assume this is protected by lock_kernel() in mount/umount */
  3499. + ylist_add_tail(&dev->devList, &yaffs_dev_list);
  3500. +
  3501. + init_MUTEX(&dev->grossLock);
  3502. +
  3503. + yaffs_GrossLock(dev);
  3504. +
  3505. + err = yaffs_GutsInitialise(dev);
  3506. +
  3507. + T(YAFFS_TRACE_OS,
  3508. + ("yaffs_read_super: guts initialised %s\n",
  3509. + (err == YAFFS_OK) ? "OK" : "FAILED"));
  3510. +
  3511. + /* Release lock before yaffs_get_inode() */
  3512. + yaffs_GrossUnlock(dev);
  3513. +
  3514. + /* Create root inode */
  3515. + if (err == YAFFS_OK)
  3516. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
  3517. + yaffs_Root(dev));
  3518. +
  3519. + if (!inode)
  3520. + return NULL;
  3521. +
  3522. + inode->i_op = &yaffs_dir_inode_operations;
  3523. + inode->i_fop = &yaffs_dir_operations;
  3524. +
  3525. + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
  3526. +
  3527. + root = d_alloc_root(inode);
  3528. +
  3529. + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
  3530. +
  3531. + if (!root) {
  3532. + iput(inode);
  3533. + return NULL;
  3534. + }
  3535. + sb->s_root = root;
  3536. + sb->s_dirt = !dev->isCheckpointed;
  3537. + T(YAFFS_TRACE_ALWAYS,
  3538. + ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
  3539. +
  3540. + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
  3541. + return sb;
  3542. +}
  3543. +
  3544. +
  3545. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3546. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  3547. + int silent)
  3548. +{
  3549. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  3550. +}
  3551. +
  3552. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3553. +static int yaffs_read_super(struct file_system_type *fs,
  3554. + int flags, const char *dev_name,
  3555. + void *data, struct vfsmount *mnt)
  3556. +{
  3557. +
  3558. + return get_sb_bdev(fs, flags, dev_name, data,
  3559. + yaffs_internal_read_super_mtd, mnt);
  3560. +}
  3561. +#else
  3562. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  3563. + int flags, const char *dev_name,
  3564. + void *data)
  3565. +{
  3566. +
  3567. + return get_sb_bdev(fs, flags, dev_name, data,
  3568. + yaffs_internal_read_super_mtd);
  3569. +}
  3570. +#endif
  3571. +
  3572. +static struct file_system_type yaffs_fs_type = {
  3573. + .owner = THIS_MODULE,
  3574. + .name = "yaffs",
  3575. + .get_sb = yaffs_read_super,
  3576. + .kill_sb = kill_block_super,
  3577. + .fs_flags = FS_REQUIRES_DEV,
  3578. +};
  3579. +#else
  3580. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  3581. + int silent)
  3582. +{
  3583. + return yaffs_internal_read_super(1, sb, data, silent);
  3584. +}
  3585. +
  3586. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  3587. + FS_REQUIRES_DEV);
  3588. +#endif
  3589. +
  3590. +
  3591. +#ifdef CONFIG_YAFFS_YAFFS2
  3592. +
  3593. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  3594. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  3595. + int silent)
  3596. +{
  3597. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  3598. +}
  3599. +
  3600. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  3601. +static int yaffs2_read_super(struct file_system_type *fs,
  3602. + int flags, const char *dev_name, void *data,
  3603. + struct vfsmount *mnt)
  3604. +{
  3605. + return get_sb_bdev(fs, flags, dev_name, data,
  3606. + yaffs2_internal_read_super_mtd, mnt);
  3607. +}
  3608. +#else
  3609. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  3610. + int flags, const char *dev_name,
  3611. + void *data)
  3612. +{
  3613. +
  3614. + return get_sb_bdev(fs, flags, dev_name, data,
  3615. + yaffs2_internal_read_super_mtd);
  3616. +}
  3617. +#endif
  3618. +
  3619. +static struct file_system_type yaffs2_fs_type = {
  3620. + .owner = THIS_MODULE,
  3621. + .name = "yaffs2",
  3622. + .get_sb = yaffs2_read_super,
  3623. + .kill_sb = kill_block_super,
  3624. + .fs_flags = FS_REQUIRES_DEV,
  3625. +};
  3626. +#else
  3627. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  3628. + void *data, int silent)
  3629. +{
  3630. + return yaffs_internal_read_super(2, sb, data, silent);
  3631. +}
  3632. +
  3633. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  3634. + FS_REQUIRES_DEV);
  3635. +#endif
  3636. +
  3637. +#endif /* CONFIG_YAFFS_YAFFS2 */
  3638. +
  3639. +static struct proc_dir_entry *my_proc_entry;
  3640. +
  3641. +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
  3642. +{
  3643. + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
  3644. + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
  3645. + buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
  3646. + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
  3647. + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
  3648. + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
  3649. + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
  3650. + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
  3651. + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
  3652. + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
  3653. + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
  3654. + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
  3655. + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
  3656. + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
  3657. + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
  3658. + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
  3659. + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
  3660. + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
  3661. + buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
  3662. + buf += sprintf(buf, "passiveGCs......... %d\n",
  3663. + dev->passiveGarbageCollections);
  3664. + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
  3665. + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
  3666. + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
  3667. + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
  3668. + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
  3669. + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
  3670. + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
  3671. + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
  3672. + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
  3673. + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
  3674. + buf +=
  3675. + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
  3676. + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
  3677. + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
  3678. + buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
  3679. +
  3680. + return buf;
  3681. +}
  3682. +
  3683. +static int yaffs_proc_read(char *page,
  3684. + char **start,
  3685. + off_t offset, int count, int *eof, void *data)
  3686. +{
  3687. + struct ylist_head *item;
  3688. + char *buf = page;
  3689. + int step = offset;
  3690. + int n = 0;
  3691. +
  3692. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  3693. + * We use 'offset' (*ppos) to indicate where we are in devList.
  3694. + * This also assumes the user has posted a read buffer large
  3695. + * enough to hold the complete output; but that's life in /proc.
  3696. + */
  3697. +
  3698. + *(int *)start = 1;
  3699. +
  3700. + /* Print header first */
  3701. + if (step == 0) {
  3702. + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
  3703. + "\n%s\n%s\n", yaffs_fs_c_version,
  3704. + yaffs_guts_c_version);
  3705. + }
  3706. +
  3707. + /* hold lock_kernel while traversing yaffs_dev_list */
  3708. + lock_kernel();
  3709. +
  3710. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  3711. + ylist_for_each(item, &yaffs_dev_list) {
  3712. + yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
  3713. + if (n < step) {
  3714. + n++;
  3715. + continue;
  3716. + }
  3717. + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
  3718. + buf = yaffs_dump_dev(buf, dev);
  3719. + break;
  3720. + }
  3721. + unlock_kernel();
  3722. +
  3723. + return buf - page < count ? buf - page : count;
  3724. +}
  3725. +
  3726. +/**
  3727. + * Set the verbosity of the warnings and error messages.
  3728. + *
  3729. + * Note that the names can only be a..z or _ with the current code.
  3730. + */
  3731. +
  3732. +static struct {
  3733. + char *mask_name;
  3734. + unsigned mask_bitfield;
  3735. +} mask_flags[] = {
  3736. + {"allocate", YAFFS_TRACE_ALLOCATE},
  3737. + {"always", YAFFS_TRACE_ALWAYS},
  3738. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  3739. + {"buffers", YAFFS_TRACE_BUFFERS},
  3740. + {"bug", YAFFS_TRACE_BUG},
  3741. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  3742. + {"deletion", YAFFS_TRACE_DELETION},
  3743. + {"erase", YAFFS_TRACE_ERASE},
  3744. + {"error", YAFFS_TRACE_ERROR},
  3745. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  3746. + {"gc", YAFFS_TRACE_GC},
  3747. + {"mtd", YAFFS_TRACE_MTD},
  3748. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  3749. + {"os", YAFFS_TRACE_OS},
  3750. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  3751. + {"scan", YAFFS_TRACE_SCAN},
  3752. + {"tracing", YAFFS_TRACE_TRACING},
  3753. +
  3754. + {"verify", YAFFS_TRACE_VERIFY},
  3755. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  3756. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  3757. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  3758. +
  3759. + {"write", YAFFS_TRACE_WRITE},
  3760. + {"all", 0xffffffff},
  3761. + {"none", 0},
  3762. + {NULL, 0},
  3763. +};
  3764. +
  3765. +#define MAX_MASK_NAME_LENGTH 40
  3766. +static int yaffs_proc_write(struct file *file, const char *buf,
  3767. + unsigned long count, void *data)
  3768. +{
  3769. + unsigned rg = 0, mask_bitfield;
  3770. + char *end;
  3771. + char *mask_name;
  3772. + const char *x;
  3773. + char substring[MAX_MASK_NAME_LENGTH + 1];
  3774. + int i;
  3775. + int done = 0;
  3776. + int add, len = 0;
  3777. + int pos = 0;
  3778. +
  3779. + rg = yaffs_traceMask;
  3780. +
  3781. + while (!done && (pos < count)) {
  3782. + done = 1;
  3783. + while ((pos < count) && isspace(buf[pos]))
  3784. + pos++;
  3785. +
  3786. + switch (buf[pos]) {
  3787. + case '+':
  3788. + case '-':
  3789. + case '=':
  3790. + add = buf[pos];
  3791. + pos++;
  3792. + break;
  3793. +
  3794. + default:
  3795. + add = ' ';
  3796. + break;
  3797. + }
  3798. + mask_name = NULL;
  3799. +
  3800. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  3801. +
  3802. + if (end > buf + pos) {
  3803. + mask_name = "numeral";
  3804. + len = end - (buf + pos);
  3805. + pos += len;
  3806. + done = 0;
  3807. + } else {
  3808. + for (x = buf + pos, i = 0;
  3809. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  3810. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  3811. + substring[i] = *x;
  3812. + substring[i] = '\0';
  3813. +
  3814. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  3815. + if (strcmp(substring, mask_flags[i].mask_name) == 0) {
  3816. + mask_name = mask_flags[i].mask_name;
  3817. + mask_bitfield = mask_flags[i].mask_bitfield;
  3818. + done = 0;
  3819. + break;
  3820. + }
  3821. + }
  3822. + }
  3823. +
  3824. + if (mask_name != NULL) {
  3825. + done = 0;
  3826. + switch (add) {
  3827. + case '-':
  3828. + rg &= ~mask_bitfield;
  3829. + break;
  3830. + case '+':
  3831. + rg |= mask_bitfield;
  3832. + break;
  3833. + case '=':
  3834. + rg = mask_bitfield;
  3835. + break;
  3836. + default:
  3837. + rg |= mask_bitfield;
  3838. + break;
  3839. + }
  3840. + }
  3841. + }
  3842. +
  3843. + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
  3844. +
  3845. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
  3846. +
  3847. + if (rg & YAFFS_TRACE_ALWAYS) {
  3848. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  3849. + char flag;
  3850. + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
  3851. + printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
  3852. + }
  3853. + }
  3854. +
  3855. + return count;
  3856. +}
  3857. +
  3858. +/* Stuff to handle installation of file systems */
  3859. +struct file_system_to_install {
  3860. + struct file_system_type *fst;
  3861. + int installed;
  3862. +};
  3863. +
  3864. +static struct file_system_to_install fs_to_install[] = {
  3865. + {&yaffs_fs_type, 0},
  3866. + {&yaffs2_fs_type, 0},
  3867. + {NULL, 0}
  3868. +};
  3869. +
  3870. +static int __init init_yaffs_fs(void)
  3871. +{
  3872. + int error = 0;
  3873. + struct file_system_to_install *fsinst;
  3874. +
  3875. + T(YAFFS_TRACE_ALWAYS,
  3876. + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
  3877. +
  3878. + /* Install the proc_fs entry */
  3879. + my_proc_entry = create_proc_entry("yaffs",
  3880. + S_IRUGO | S_IFREG,
  3881. + YPROC_ROOT);
  3882. +
  3883. + if (my_proc_entry) {
  3884. + my_proc_entry->write_proc = yaffs_proc_write;
  3885. + my_proc_entry->read_proc = yaffs_proc_read;
  3886. + my_proc_entry->data = NULL;
  3887. + } else
  3888. + return -ENOMEM;
  3889. +
  3890. + /* Now add the file system entries */
  3891. +
  3892. + fsinst = fs_to_install;
  3893. +
  3894. + while (fsinst->fst && !error) {
  3895. + error = register_filesystem(fsinst->fst);
  3896. + if (!error)
  3897. + fsinst->installed = 1;
  3898. + fsinst++;
  3899. + }
  3900. +
  3901. + /* Any errors? uninstall */
  3902. + if (error) {
  3903. + fsinst = fs_to_install;
  3904. +
  3905. + while (fsinst->fst) {
  3906. + if (fsinst->installed) {
  3907. + unregister_filesystem(fsinst->fst);
  3908. + fsinst->installed = 0;
  3909. + }
  3910. + fsinst++;
  3911. + }
  3912. + }
  3913. +
  3914. + return error;
  3915. +}
  3916. +
  3917. +static void __exit exit_yaffs_fs(void)
  3918. +{
  3919. +
  3920. + struct file_system_to_install *fsinst;
  3921. +
  3922. + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
  3923. + " removing. \n"));
  3924. +
  3925. + remove_proc_entry("yaffs", YPROC_ROOT);
  3926. +
  3927. + fsinst = fs_to_install;
  3928. +
  3929. + while (fsinst->fst) {
  3930. + if (fsinst->installed) {
  3931. + unregister_filesystem(fsinst->fst);
  3932. + fsinst->installed = 0;
  3933. + }
  3934. + fsinst++;
  3935. + }
  3936. +}
  3937. +
  3938. +module_init(init_yaffs_fs)
  3939. +module_exit(exit_yaffs_fs)
  3940. +
  3941. +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  3942. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
  3943. +MODULE_LICENSE("GPL");
  3944. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_getblockinfo.h linux-2.6.32/fs/yaffs2/yaffs_getblockinfo.h
  3945. --- linux-2.6.32.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  3946. +++ linux-2.6.32/fs/yaffs2/yaffs_getblockinfo.h 2010-01-30 20:35:01.373074589 +0100
  3947. @@ -0,0 +1,34 @@
  3948. +/*
  3949. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  3950. + *
  3951. + * Copyright (C) 2002-2007 Aleph One Ltd.
  3952. + * for Toby Churchill Ltd and Brightstar Engineering
  3953. + *
  3954. + * Created by Charles Manning <charles@aleph1.co.uk>
  3955. + *
  3956. + * This program is free software; you can redistribute it and/or modify
  3957. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  3958. + * published by the Free Software Foundation.
  3959. + *
  3960. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  3961. + */
  3962. +
  3963. +#ifndef __YAFFS_GETBLOCKINFO_H__
  3964. +#define __YAFFS_GETBLOCKINFO_H__
  3965. +
  3966. +#include "yaffs_guts.h"
  3967. +
  3968. +/* Function to manipulate block info */
  3969. +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
  3970. +{
  3971. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  3972. + T(YAFFS_TRACE_ERROR,
  3973. + (TSTR
  3974. + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
  3975. + blk));
  3976. + YBUG();
  3977. + }
  3978. + return &dev->blockInfo[blk - dev->internalStartBlock];
  3979. +}
  3980. +
  3981. +#endif
  3982. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_guts.c linux-2.6.32/fs/yaffs2/yaffs_guts.c
  3983. --- linux-2.6.32.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  3984. +++ linux-2.6.32/fs/yaffs2/yaffs_guts.c 2010-01-30 20:35:01.393075518 +0100
  3985. @@ -0,0 +1,7552 @@
  3986. +/*
  3987. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3988. + *
  3989. + * Copyright (C) 2002-2007 Aleph One Ltd.
  3990. + * for Toby Churchill Ltd and Brightstar Engineering
  3991. + *
  3992. + * Created by Charles Manning <charles@aleph1.co.uk>
  3993. + *
  3994. + * This program is free software; you can redistribute it and/or modify
  3995. + * it under the terms of the GNU General Public License version 2 as
  3996. + * published by the Free Software Foundation.
  3997. + */
  3998. +
  3999. +const char *yaffs_guts_c_version =
  4000. + "$Id: yaffs_guts.c,v 1.82 2009-03-09 04:24:17 charles Exp $";
  4001. +
  4002. +#include "yportenv.h"
  4003. +
  4004. +#include "yaffsinterface.h"
  4005. +#include "yaffs_guts.h"
  4006. +#include "yaffs_tagsvalidity.h"
  4007. +#include "yaffs_getblockinfo.h"
  4008. +
  4009. +#include "yaffs_tagscompat.h"
  4010. +#ifndef CONFIG_YAFFS_USE_OWN_SORT
  4011. +#include "yaffs_qsort.h"
  4012. +#endif
  4013. +#include "yaffs_nand.h"
  4014. +
  4015. +#include "yaffs_checkptrw.h"
  4016. +
  4017. +#include "yaffs_nand.h"
  4018. +#include "yaffs_packedtags2.h"
  4019. +
  4020. +
  4021. +#define YAFFS_PASSIVE_GC_CHUNKS 2
  4022. +
  4023. +#include "yaffs_ecc.h"
  4024. +
  4025. +
  4026. +/* Robustification (if it ever comes about...) */
  4027. +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
  4028. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
  4029. + int erasedOk);
  4030. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  4031. + const __u8 *data,
  4032. + const yaffs_ExtendedTags *tags);
  4033. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  4034. + const yaffs_ExtendedTags *tags);
  4035. +
  4036. +/* Other local prototypes */
  4037. +static int yaffs_UnlinkObject(yaffs_Object *obj);
  4038. +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
  4039. +
  4040. +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
  4041. +
  4042. +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
  4043. + const __u8 *buffer,
  4044. + yaffs_ExtendedTags *tags,
  4045. + int useReserve);
  4046. +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
  4047. + int chunkInNAND, int inScan);
  4048. +
  4049. +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
  4050. + yaffs_ObjectType type);
  4051. +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
  4052. + yaffs_Object *obj);
  4053. +static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
  4054. + int force, int isShrink, int shadows);
  4055. +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
  4056. +static int yaffs_CheckStructures(void);
  4057. +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
  4058. + int chunkOffset, int *limit);
  4059. +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
  4060. +
  4061. +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
  4062. +
  4063. +
  4064. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  4065. + int chunkInNAND);
  4066. +
  4067. +static int yaffs_UnlinkWorker(yaffs_Object *obj);
  4068. +
  4069. +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
  4070. + int chunkInObject);
  4071. +
  4072. +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
  4073. + yaffs_BlockInfo **blockUsedPtr);
  4074. +
  4075. +static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
  4076. +
  4077. +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
  4078. +
  4079. +static void yaffs_VerifyDirectory(yaffs_Object *directory);
  4080. +#ifdef YAFFS_PARANOID
  4081. +static int yaffs_CheckFileSanity(yaffs_Object *in);
  4082. +#else
  4083. +#define yaffs_CheckFileSanity(in)
  4084. +#endif
  4085. +
  4086. +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
  4087. +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
  4088. +
  4089. +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
  4090. +
  4091. +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
  4092. + yaffs_ExtendedTags *tags);
  4093. +
  4094. +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
  4095. + unsigned pos);
  4096. +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
  4097. + yaffs_FileStructure *fStruct,
  4098. + __u32 chunkId);
  4099. +
  4100. +
  4101. +/* Function to calculate chunk and offset */
  4102. +
  4103. +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
  4104. + __u32 *offsetOut)
  4105. +{
  4106. + int chunk;
  4107. + __u32 offset;
  4108. +
  4109. + chunk = (__u32)(addr >> dev->chunkShift);
  4110. +
  4111. + if (dev->chunkDiv == 1) {
  4112. + /* easy power of 2 case */
  4113. + offset = (__u32)(addr & dev->chunkMask);
  4114. + } else {
  4115. + /* Non power-of-2 case */
  4116. +
  4117. + loff_t chunkBase;
  4118. +
  4119. + chunk /= dev->chunkDiv;
  4120. +
  4121. + chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
  4122. + offset = (__u32)(addr - chunkBase);
  4123. + }
  4124. +
  4125. + *chunkOut = chunk;
  4126. + *offsetOut = offset;
  4127. +}
  4128. +
  4129. +/* Function to return the number of shifts for a power of 2 greater than or
  4130. + * equal to the given number
  4131. + * Note we don't try to cater for all possible numbers and this does not have to
  4132. + * be hellishly efficient.
  4133. + */
  4134. +
  4135. +static __u32 ShiftsGE(__u32 x)
  4136. +{
  4137. + int extraBits;
  4138. + int nShifts;
  4139. +
  4140. + nShifts = extraBits = 0;
  4141. +
  4142. + while (x > 1) {
  4143. + if (x & 1)
  4144. + extraBits++;
  4145. + x >>= 1;
  4146. + nShifts++;
  4147. + }
  4148. +
  4149. + if (extraBits)
  4150. + nShifts++;
  4151. +
  4152. + return nShifts;
  4153. +}
  4154. +
  4155. +/* Function to return the number of shifts to get a 1 in bit 0
  4156. + */
  4157. +
  4158. +static __u32 Shifts(__u32 x)
  4159. +{
  4160. + int nShifts;
  4161. +
  4162. + nShifts = 0;
  4163. +
  4164. + if (!x)
  4165. + return 0;
  4166. +
  4167. + while (!(x&1)) {
  4168. + x >>= 1;
  4169. + nShifts++;
  4170. + }
  4171. +
  4172. + return nShifts;
  4173. +}
  4174. +
  4175. +
  4176. +
  4177. +/*
  4178. + * Temporary buffer manipulations.
  4179. + */
  4180. +
  4181. +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
  4182. +{
  4183. + int i;
  4184. + __u8 *buf = (__u8 *)1;
  4185. +
  4186. + memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
  4187. +
  4188. + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  4189. + dev->tempBuffer[i].line = 0; /* not in use */
  4190. + dev->tempBuffer[i].buffer = buf =
  4191. + YMALLOC_DMA(dev->totalBytesPerChunk);
  4192. + }
  4193. +
  4194. + return buf ? YAFFS_OK : YAFFS_FAIL;
  4195. +}
  4196. +
  4197. +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
  4198. +{
  4199. + int i, j;
  4200. +
  4201. + dev->tempInUse++;
  4202. + if (dev->tempInUse > dev->maxTemp)
  4203. + dev->maxTemp = dev->tempInUse;
  4204. +
  4205. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4206. + if (dev->tempBuffer[i].line == 0) {
  4207. + dev->tempBuffer[i].line = lineNo;
  4208. + if ((i + 1) > dev->maxTemp) {
  4209. + dev->maxTemp = i + 1;
  4210. + for (j = 0; j <= i; j++)
  4211. + dev->tempBuffer[j].maxLine =
  4212. + dev->tempBuffer[j].line;
  4213. + }
  4214. +
  4215. + return dev->tempBuffer[i].buffer;
  4216. + }
  4217. + }
  4218. +
  4219. + T(YAFFS_TRACE_BUFFERS,
  4220. + (TSTR("Out of temp buffers at line %d, other held by lines:"),
  4221. + lineNo));
  4222. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  4223. + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
  4224. +
  4225. + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
  4226. +
  4227. + /*
  4228. + * If we got here then we have to allocate an unmanaged one
  4229. + * This is not good.
  4230. + */
  4231. +
  4232. + dev->unmanagedTempAllocations++;
  4233. + return YMALLOC(dev->nDataBytesPerChunk);
  4234. +
  4235. +}
  4236. +
  4237. +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
  4238. + int lineNo)
  4239. +{
  4240. + int i;
  4241. +
  4242. + dev->tempInUse--;
  4243. +
  4244. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4245. + if (dev->tempBuffer[i].buffer == buffer) {
  4246. + dev->tempBuffer[i].line = 0;
  4247. + return;
  4248. + }
  4249. + }
  4250. +
  4251. + if (buffer) {
  4252. + /* assume it is an unmanaged one. */
  4253. + T(YAFFS_TRACE_BUFFERS,
  4254. + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
  4255. + lineNo));
  4256. + YFREE(buffer);
  4257. + dev->unmanagedTempDeallocations++;
  4258. + }
  4259. +
  4260. +}
  4261. +
  4262. +/*
  4263. + * Determine if we have a managed buffer.
  4264. + */
  4265. +int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
  4266. +{
  4267. + int i;
  4268. +
  4269. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  4270. + if (dev->tempBuffer[i].buffer == buffer)
  4271. + return 1;
  4272. + }
  4273. +
  4274. + for (i = 0; i < dev->nShortOpCaches; i++) {
  4275. + if (dev->srCache[i].data == buffer)
  4276. + return 1;
  4277. + }
  4278. +
  4279. + if (buffer == dev->checkpointBuffer)
  4280. + return 1;
  4281. +
  4282. + T(YAFFS_TRACE_ALWAYS,
  4283. + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
  4284. + return 0;
  4285. +}
  4286. +
  4287. +
  4288. +
  4289. +/*
  4290. + * Chunk bitmap manipulations
  4291. + */
  4292. +
  4293. +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
  4294. +{
  4295. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  4296. + T(YAFFS_TRACE_ERROR,
  4297. + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
  4298. + blk));
  4299. + YBUG();
  4300. + }
  4301. + return dev->chunkBits +
  4302. + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
  4303. +}
  4304. +
  4305. +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
  4306. +{
  4307. + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
  4308. + chunk < 0 || chunk >= dev->nChunksPerBlock) {
  4309. + T(YAFFS_TRACE_ERROR,
  4310. + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
  4311. + blk, chunk));
  4312. + YBUG();
  4313. + }
  4314. +}
  4315. +
  4316. +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
  4317. +{
  4318. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4319. +
  4320. + memset(blkBits, 0, dev->chunkBitmapStride);
  4321. +}
  4322. +
  4323. +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
  4324. +{
  4325. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4326. +
  4327. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4328. +
  4329. + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
  4330. +}
  4331. +
  4332. +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
  4333. +{
  4334. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4335. +
  4336. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4337. +
  4338. + blkBits[chunk / 8] |= (1 << (chunk & 7));
  4339. +}
  4340. +
  4341. +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
  4342. +{
  4343. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4344. + yaffs_VerifyChunkBitId(dev, blk, chunk);
  4345. +
  4346. + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  4347. +}
  4348. +
  4349. +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
  4350. +{
  4351. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4352. + int i;
  4353. + for (i = 0; i < dev->chunkBitmapStride; i++) {
  4354. + if (*blkBits)
  4355. + return 1;
  4356. + blkBits++;
  4357. + }
  4358. + return 0;
  4359. +}
  4360. +
  4361. +static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
  4362. +{
  4363. + __u8 *blkBits = yaffs_BlockBits(dev, blk);
  4364. + int i;
  4365. + int n = 0;
  4366. + for (i = 0; i < dev->chunkBitmapStride; i++) {
  4367. + __u8 x = *blkBits;
  4368. + while (x) {
  4369. + if (x & 1)
  4370. + n++;
  4371. + x >>= 1;
  4372. + }
  4373. +
  4374. + blkBits++;
  4375. + }
  4376. + return n;
  4377. +}
  4378. +
  4379. +/*
  4380. + * Verification code
  4381. + */
  4382. +
  4383. +static int yaffs_SkipVerification(yaffs_Device *dev)
  4384. +{
  4385. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  4386. +}
  4387. +
  4388. +static int yaffs_SkipFullVerification(yaffs_Device *dev)
  4389. +{
  4390. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
  4391. +}
  4392. +
  4393. +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
  4394. +{
  4395. + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
  4396. +}
  4397. +
  4398. +static const char *blockStateName[] = {
  4399. +"Unknown",
  4400. +"Needs scanning",
  4401. +"Scanning",
  4402. +"Empty",
  4403. +"Allocating",
  4404. +"Full",
  4405. +"Dirty",
  4406. +"Checkpoint",
  4407. +"Collecting",
  4408. +"Dead"
  4409. +};
  4410. +
  4411. +static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
  4412. +{
  4413. + int actuallyUsed;
  4414. + int inUse;
  4415. +
  4416. + if (yaffs_SkipVerification(dev))
  4417. + return;
  4418. +
  4419. + /* Report illegal runtime states */
  4420. + if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
  4421. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
  4422. +
  4423. + switch (bi->blockState) {
  4424. + case YAFFS_BLOCK_STATE_UNKNOWN:
  4425. + case YAFFS_BLOCK_STATE_SCANNING:
  4426. + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
  4427. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
  4428. + n, blockStateName[bi->blockState]));
  4429. + }
  4430. +
  4431. + /* Check pages in use and soft deletions are legal */
  4432. +
  4433. + actuallyUsed = bi->pagesInUse - bi->softDeletions;
  4434. +
  4435. + if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
  4436. + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
  4437. + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
  4438. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
  4439. + n, bi->pagesInUse, bi->softDeletions));
  4440. +
  4441. +
  4442. + /* Check chunk bitmap legal */
  4443. + inUse = yaffs_CountChunkBits(dev, n);
  4444. + if (inUse != bi->pagesInUse)
  4445. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
  4446. + n, bi->pagesInUse, inUse));
  4447. +
  4448. + /* Check that the sequence number is valid.
  4449. + * Ten million is legal, but is very unlikely
  4450. + */
  4451. + if (dev->isYaffs2 &&
  4452. + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
  4453. + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
  4454. + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
  4455. + n, bi->sequenceNumber));
  4456. +}
  4457. +
  4458. +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
  4459. + int n)
  4460. +{
  4461. + yaffs_VerifyBlock(dev, bi, n);
  4462. +
  4463. + /* After collection the block should be in the erased state */
  4464. + /* This will need to change if we do partial gc */
  4465. +
  4466. + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
  4467. + bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
  4468. + T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
  4469. + n, bi->blockState));
  4470. + }
  4471. +}
  4472. +
  4473. +static void yaffs_VerifyBlocks(yaffs_Device *dev)
  4474. +{
  4475. + int i;
  4476. + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
  4477. + int nIllegalBlockStates = 0;
  4478. +
  4479. + if (yaffs_SkipVerification(dev))
  4480. + return;
  4481. +
  4482. + memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
  4483. +
  4484. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  4485. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
  4486. + yaffs_VerifyBlock(dev, bi, i);
  4487. +
  4488. + if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
  4489. + nBlocksPerState[bi->blockState]++;
  4490. + else
  4491. + nIllegalBlockStates++;
  4492. + }
  4493. +
  4494. + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
  4495. + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
  4496. +
  4497. + T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
  4498. + if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  4499. + T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
  4500. +
  4501. + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  4502. + T(YAFFS_TRACE_VERIFY,
  4503. + (TSTR("%s %d blocks"TENDSTR),
  4504. + blockStateName[i], nBlocksPerState[i]));
  4505. +
  4506. + if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
  4507. + T(YAFFS_TRACE_VERIFY,
  4508. + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
  4509. + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
  4510. +
  4511. + if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
  4512. + T(YAFFS_TRACE_VERIFY,
  4513. + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
  4514. + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
  4515. +
  4516. + if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  4517. + T(YAFFS_TRACE_VERIFY,
  4518. + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
  4519. + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
  4520. +
  4521. + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
  4522. +
  4523. +}
  4524. +
  4525. +/*
  4526. + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
  4527. + * case those tests will not be performed.
  4528. + */
  4529. +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
  4530. +{
  4531. + if (obj && yaffs_SkipVerification(obj->myDev))
  4532. + return;
  4533. +
  4534. + if (!(tags && obj && oh)) {
  4535. + T(YAFFS_TRACE_VERIFY,
  4536. + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
  4537. + (__u32)tags, (__u32)obj, (__u32)oh));
  4538. + return;
  4539. + }
  4540. +
  4541. + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  4542. + oh->type > YAFFS_OBJECT_TYPE_MAX)
  4543. + T(YAFFS_TRACE_VERIFY,
  4544. + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
  4545. + tags->objectId, oh->type));
  4546. +
  4547. + if (tags->objectId != obj->objectId)
  4548. + T(YAFFS_TRACE_VERIFY,
  4549. + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
  4550. + tags->objectId, obj->objectId));
  4551. +
  4552. +
  4553. + /*
  4554. + * Check that the object's parent ids match if parentCheck requested.
  4555. + *
  4556. + * Tests do not apply to the root object.
  4557. + */
  4558. +
  4559. + if (parentCheck && tags->objectId > 1 && !obj->parent)
  4560. + T(YAFFS_TRACE_VERIFY,
  4561. + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
  4562. + tags->objectId, oh->parentObjectId));
  4563. +
  4564. + if (parentCheck && obj->parent &&
  4565. + oh->parentObjectId != obj->parent->objectId &&
  4566. + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
  4567. + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
  4568. + T(YAFFS_TRACE_VERIFY,
  4569. + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
  4570. + tags->objectId, oh->parentObjectId, obj->parent->objectId));
  4571. +
  4572. + if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
  4573. + T(YAFFS_TRACE_VERIFY,
  4574. + (TSTR("Obj %d header name is NULL"TENDSTR),
  4575. + obj->objectId));
  4576. +
  4577. + if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
  4578. + T(YAFFS_TRACE_VERIFY,
  4579. + (TSTR("Obj %d header name is 0xFF"TENDSTR),
  4580. + obj->objectId));
  4581. +}
  4582. +
  4583. +
  4584. +
  4585. +static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
  4586. + __u32 level, int chunkOffset)
  4587. +{
  4588. + int i;
  4589. + yaffs_Device *dev = obj->myDev;
  4590. + int ok = 1;
  4591. +
  4592. + if (tn) {
  4593. + if (level > 0) {
  4594. +
  4595. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  4596. + if (tn->internal[i]) {
  4597. + ok = yaffs_VerifyTnodeWorker(obj,
  4598. + tn->internal[i],
  4599. + level - 1,
  4600. + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  4601. + }
  4602. + }
  4603. + } else if (level == 0) {
  4604. + yaffs_ExtendedTags tags;
  4605. + __u32 objectId = obj->objectId;
  4606. +
  4607. + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
  4608. +
  4609. + for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
  4610. + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  4611. +
  4612. + if (theChunk > 0) {
  4613. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
  4614. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
  4615. + if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
  4616. + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  4617. + objectId, chunkOffset, theChunk,
  4618. + tags.objectId, tags.chunkId));
  4619. + }
  4620. + }
  4621. + chunkOffset++;
  4622. + }
  4623. + }
  4624. + }
  4625. +
  4626. + return ok;
  4627. +
  4628. +}
  4629. +
  4630. +
  4631. +static void yaffs_VerifyFile(yaffs_Object *obj)
  4632. +{
  4633. + int requiredTallness;
  4634. + int actualTallness;
  4635. + __u32 lastChunk;
  4636. + __u32 x;
  4637. + __u32 i;
  4638. + yaffs_Device *dev;
  4639. + yaffs_ExtendedTags tags;
  4640. + yaffs_Tnode *tn;
  4641. + __u32 objectId;
  4642. +
  4643. + if (!obj)
  4644. + return;
  4645. +
  4646. + if (yaffs_SkipVerification(obj->myDev))
  4647. + return;
  4648. +
  4649. + dev = obj->myDev;
  4650. + objectId = obj->objectId;
  4651. +
  4652. + /* Check file size is consistent with tnode depth */
  4653. + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
  4654. + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
  4655. + requiredTallness = 0;
  4656. + while (x > 0) {
  4657. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  4658. + requiredTallness++;
  4659. + }
  4660. +
  4661. + actualTallness = obj->variant.fileVariant.topLevel;
  4662. +
  4663. + if (requiredTallness > actualTallness)
  4664. + T(YAFFS_TRACE_VERIFY,
  4665. + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
  4666. + obj->objectId, actualTallness, requiredTallness));
  4667. +
  4668. +
  4669. + /* Check that the chunks in the tnode tree are all correct.
  4670. + * We do this by scanning through the tnode tree and
  4671. + * checking the tags for every chunk match.
  4672. + */
  4673. +
  4674. + if (yaffs_SkipNANDVerification(dev))
  4675. + return;
  4676. +
  4677. + for (i = 1; i <= lastChunk; i++) {
  4678. + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
  4679. +
  4680. + if (tn) {
  4681. + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  4682. + if (theChunk > 0) {
  4683. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
  4684. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
  4685. + if (tags.objectId != objectId || tags.chunkId != i) {
  4686. + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
  4687. + objectId, i, theChunk,
  4688. + tags.objectId, tags.chunkId));
  4689. + }
  4690. + }
  4691. + }
  4692. + }
  4693. +}
  4694. +
  4695. +
  4696. +static void yaffs_VerifyHardLink(yaffs_Object *obj)
  4697. +{
  4698. + if (obj && yaffs_SkipVerification(obj->myDev))
  4699. + return;
  4700. +
  4701. + /* Verify sane equivalent object */
  4702. +}
  4703. +
  4704. +static void yaffs_VerifySymlink(yaffs_Object *obj)
  4705. +{
  4706. + if (obj && yaffs_SkipVerification(obj->myDev))
  4707. + return;
  4708. +
  4709. + /* Verify symlink string */
  4710. +}
  4711. +
  4712. +static void yaffs_VerifySpecial(yaffs_Object *obj)
  4713. +{
  4714. + if (obj && yaffs_SkipVerification(obj->myDev))
  4715. + return;
  4716. +}
  4717. +
  4718. +static void yaffs_VerifyObject(yaffs_Object *obj)
  4719. +{
  4720. + yaffs_Device *dev;
  4721. +
  4722. + __u32 chunkMin;
  4723. + __u32 chunkMax;
  4724. +
  4725. + __u32 chunkIdOk;
  4726. + __u32 chunkInRange;
  4727. + __u32 chunkShouldNotBeDeleted;
  4728. + __u32 chunkValid;
  4729. +
  4730. + if (!obj)
  4731. + return;
  4732. +
  4733. + if (obj->beingCreated)
  4734. + return;
  4735. +
  4736. + dev = obj->myDev;
  4737. +
  4738. + if (yaffs_SkipVerification(dev))
  4739. + return;
  4740. +
  4741. + /* Check sane object header chunk */
  4742. +
  4743. + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
  4744. + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
  4745. +
  4746. + chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
  4747. + chunkIdOk = chunkInRange || obj->hdrChunk == 0;
  4748. + chunkValid = chunkInRange &&
  4749. + yaffs_CheckChunkBit(dev,
  4750. + obj->hdrChunk / dev->nChunksPerBlock,
  4751. + obj->hdrChunk % dev->nChunksPerBlock);
  4752. + chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
  4753. +
  4754. + if (!obj->fake &&
  4755. + (!chunkIdOk || chunkShouldNotBeDeleted)) {
  4756. + T(YAFFS_TRACE_VERIFY,
  4757. + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
  4758. + obj->objectId, obj->hdrChunk,
  4759. + chunkIdOk ? "" : ",out of range",
  4760. + chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
  4761. + }
  4762. +
  4763. + if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
  4764. + yaffs_ExtendedTags tags;
  4765. + yaffs_ObjectHeader *oh;
  4766. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  4767. +
  4768. + oh = (yaffs_ObjectHeader *)buffer;
  4769. +
  4770. + yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
  4771. + &tags);
  4772. +
  4773. + yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
  4774. +
  4775. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  4776. + }
  4777. +
  4778. + /* Verify it has a parent */
  4779. + if (obj && !obj->fake &&
  4780. + (!obj->parent || obj->parent->myDev != dev)) {
  4781. + T(YAFFS_TRACE_VERIFY,
  4782. + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
  4783. + obj->objectId, obj->parent));
  4784. + }
  4785. +
  4786. + /* Verify parent is a directory */
  4787. + if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  4788. + T(YAFFS_TRACE_VERIFY,
  4789. + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
  4790. + obj->objectId, obj->parent->variantType));
  4791. + }
  4792. +
  4793. + switch (obj->variantType) {
  4794. + case YAFFS_OBJECT_TYPE_FILE:
  4795. + yaffs_VerifyFile(obj);
  4796. + break;
  4797. + case YAFFS_OBJECT_TYPE_SYMLINK:
  4798. + yaffs_VerifySymlink(obj);
  4799. + break;
  4800. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  4801. + yaffs_VerifyDirectory(obj);
  4802. + break;
  4803. + case YAFFS_OBJECT_TYPE_HARDLINK:
  4804. + yaffs_VerifyHardLink(obj);
  4805. + break;
  4806. + case YAFFS_OBJECT_TYPE_SPECIAL:
  4807. + yaffs_VerifySpecial(obj);
  4808. + break;
  4809. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  4810. + default:
  4811. + T(YAFFS_TRACE_VERIFY,
  4812. + (TSTR("Obj %d has illegaltype %d"TENDSTR),
  4813. + obj->objectId, obj->variantType));
  4814. + break;
  4815. + }
  4816. +}
  4817. +
  4818. +static void yaffs_VerifyObjects(yaffs_Device *dev)
  4819. +{
  4820. + yaffs_Object *obj;
  4821. + int i;
  4822. + struct ylist_head *lh;
  4823. +
  4824. + if (yaffs_SkipVerification(dev))
  4825. + return;
  4826. +
  4827. + /* Iterate through the objects in each hash entry */
  4828. +
  4829. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  4830. + ylist_for_each(lh, &dev->objectBucket[i].list) {
  4831. + if (lh) {
  4832. + obj = ylist_entry(lh, yaffs_Object, hashLink);
  4833. + yaffs_VerifyObject(obj);
  4834. + }
  4835. + }
  4836. + }
  4837. +}
  4838. +
  4839. +
  4840. +/*
  4841. + * Simple hash function. Needs to have a reasonable spread
  4842. + */
  4843. +
  4844. +static Y_INLINE int yaffs_HashFunction(int n)
  4845. +{
  4846. + n = abs(n);
  4847. + return n % YAFFS_NOBJECT_BUCKETS;
  4848. +}
  4849. +
  4850. +/*
  4851. + * Access functions to useful fake objects.
  4852. + * Note that root might have a presence in NAND if permissions are set.
  4853. + */
  4854. +
  4855. +yaffs_Object *yaffs_Root(yaffs_Device *dev)
  4856. +{
  4857. + return dev->rootDir;
  4858. +}
  4859. +
  4860. +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
  4861. +{
  4862. + return dev->lostNFoundDir;
  4863. +}
  4864. +
  4865. +
  4866. +/*
  4867. + * Erased NAND checking functions
  4868. + */
  4869. +
  4870. +int yaffs_CheckFF(__u8 *buffer, int nBytes)
  4871. +{
  4872. + /* Horrible, slow implementation */
  4873. + while (nBytes--) {
  4874. + if (*buffer != 0xFF)
  4875. + return 0;
  4876. + buffer++;
  4877. + }
  4878. + return 1;
  4879. +}
  4880. +
  4881. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  4882. + int chunkInNAND)
  4883. +{
  4884. + int retval = YAFFS_OK;
  4885. + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
  4886. + yaffs_ExtendedTags tags;
  4887. + int result;
  4888. +
  4889. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
  4890. +
  4891. + if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
  4892. + retval = YAFFS_FAIL;
  4893. +
  4894. + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
  4895. + T(YAFFS_TRACE_NANDACCESS,
  4896. + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
  4897. + retval = YAFFS_FAIL;
  4898. + }
  4899. +
  4900. + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
  4901. +
  4902. + return retval;
  4903. +
  4904. +}
  4905. +
  4906. +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
  4907. + const __u8 *data,
  4908. + yaffs_ExtendedTags *tags,
  4909. + int useReserve)
  4910. +{
  4911. + int attempts = 0;
  4912. + int writeOk = 0;
  4913. + int chunk;
  4914. +
  4915. + yaffs_InvalidateCheckpoint(dev);
  4916. +
  4917. + do {
  4918. + yaffs_BlockInfo *bi = 0;
  4919. + int erasedOk = 0;
  4920. +
  4921. + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
  4922. + if (chunk < 0) {
  4923. + /* no space */
  4924. + break;
  4925. + }
  4926. +
  4927. + /* First check this chunk is erased, if it needs
  4928. + * checking. The checking policy (unless forced
  4929. + * always on) is as follows:
  4930. + *
  4931. + * Check the first page we try to write in a block.
  4932. + * If the check passes then we don't need to check any
  4933. + * more. If the check fails, we check again...
  4934. + * If the block has been erased, we don't need to check.
  4935. + *
  4936. + * However, if the block has been prioritised for gc,
  4937. + * then we think there might be something odd about
  4938. + * this block and stop using it.
  4939. + *
  4940. + * Rationale: We should only ever see chunks that have
  4941. + * not been erased if there was a partially written
  4942. + * chunk due to power loss. This checking policy should
  4943. + * catch that case with very few checks and thus save a
  4944. + * lot of checks that are most likely not needed.
  4945. + */
  4946. + if (bi->gcPrioritise) {
  4947. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  4948. + /* try another chunk */
  4949. + continue;
  4950. + }
  4951. +
  4952. + /* let's give it a try */
  4953. + attempts++;
  4954. +
  4955. +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  4956. + bi->skipErasedCheck = 0;
  4957. +#endif
  4958. + if (!bi->skipErasedCheck) {
  4959. + erasedOk = yaffs_CheckChunkErased(dev, chunk);
  4960. + if (erasedOk != YAFFS_OK) {
  4961. + T(YAFFS_TRACE_ERROR,
  4962. + (TSTR("**>> yaffs chunk %d was not erased"
  4963. + TENDSTR), chunk));
  4964. +
  4965. + /* try another chunk */
  4966. + continue;
  4967. + }
  4968. + bi->skipErasedCheck = 1;
  4969. + }
  4970. +
  4971. + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
  4972. + data, tags);
  4973. + if (writeOk != YAFFS_OK) {
  4974. + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
  4975. + /* try another chunk */
  4976. + continue;
  4977. + }
  4978. +
  4979. + /* Copy the data into the robustification buffer */
  4980. + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
  4981. +
  4982. + } while (writeOk != YAFFS_OK &&
  4983. + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  4984. +
  4985. + if (!writeOk)
  4986. + chunk = -1;
  4987. +
  4988. + if (attempts > 1) {
  4989. + T(YAFFS_TRACE_ERROR,
  4990. + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
  4991. + attempts));
  4992. +
  4993. + dev->nRetriedWrites += (attempts - 1);
  4994. + }
  4995. +
  4996. + return chunk;
  4997. +}
  4998. +
  4999. +/*
  5000. + * Block retiring for handling a broken block.
  5001. + */
  5002. +
  5003. +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
  5004. +{
  5005. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  5006. +
  5007. + yaffs_InvalidateCheckpoint(dev);
  5008. +
  5009. + if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
  5010. + if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
  5011. + T(YAFFS_TRACE_ALWAYS, (TSTR(
  5012. + "yaffs: Failed to mark bad and erase block %d"
  5013. + TENDSTR), blockInNAND));
  5014. + } else {
  5015. + yaffs_ExtendedTags tags;
  5016. + int chunkId = blockInNAND * dev->nChunksPerBlock;
  5017. +
  5018. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  5019. +
  5020. + memset(buffer, 0xff, dev->nDataBytesPerChunk);
  5021. + yaffs_InitialiseTags(&tags);
  5022. + tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
  5023. + if (dev->writeChunkWithTagsToNAND(dev, chunkId -
  5024. + dev->chunkOffset, buffer, &tags) != YAFFS_OK)
  5025. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
  5026. + TCONT("write bad block marker to block %d")
  5027. + TENDSTR), blockInNAND));
  5028. +
  5029. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  5030. + }
  5031. + }
  5032. +
  5033. + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
  5034. + bi->gcPrioritise = 0;
  5035. + bi->needsRetiring = 0;
  5036. +
  5037. + dev->nRetiredBlocks++;
  5038. +}
  5039. +
  5040. +/*
  5041. + * Functions for robustisizing TODO
  5042. + *
  5043. + */
  5044. +
  5045. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  5046. + const __u8 *data,
  5047. + const yaffs_ExtendedTags *tags)
  5048. +{
  5049. +}
  5050. +
  5051. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  5052. + const yaffs_ExtendedTags *tags)
  5053. +{
  5054. +}
  5055. +
  5056. +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
  5057. +{
  5058. + if (!bi->gcPrioritise) {
  5059. + bi->gcPrioritise = 1;
  5060. + dev->hasPendingPrioritisedGCs = 1;
  5061. + bi->chunkErrorStrikes++;
  5062. +
  5063. + if (bi->chunkErrorStrikes > 3) {
  5064. + bi->needsRetiring = 1; /* Too many stikes, so retire this */
  5065. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
  5066. +
  5067. + }
  5068. + }
  5069. +}
  5070. +
  5071. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
  5072. + int erasedOk)
  5073. +{
  5074. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  5075. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
  5076. +
  5077. + yaffs_HandleChunkError(dev, bi);
  5078. +
  5079. + if (erasedOk) {
  5080. + /* Was an actual write failure, so mark the block for retirement */
  5081. + bi->needsRetiring = 1;
  5082. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  5083. + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
  5084. + }
  5085. +
  5086. + /* Delete the chunk */
  5087. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  5088. +}
  5089. +
  5090. +
  5091. +/*---------------- Name handling functions ------------*/
  5092. +
  5093. +static __u16 yaffs_CalcNameSum(const YCHAR *name)
  5094. +{
  5095. + __u16 sum = 0;
  5096. + __u16 i = 1;
  5097. +
  5098. + const YUCHAR *bname = (const YUCHAR *) name;
  5099. + if (bname) {
  5100. + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
  5101. +
  5102. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  5103. + sum += yaffs_toupper(*bname) * i;
  5104. +#else
  5105. + sum += (*bname) * i;
  5106. +#endif
  5107. + i++;
  5108. + bname++;
  5109. + }
  5110. + }
  5111. + return sum;
  5112. +}
  5113. +
  5114. +static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
  5115. +{
  5116. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  5117. + memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
  5118. + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
  5119. + yaffs_strcpy(obj->shortName, name);
  5120. + else
  5121. + obj->shortName[0] = _Y('\0');
  5122. +#endif
  5123. + obj->sum = yaffs_CalcNameSum(name);
  5124. +}
  5125. +
  5126. +/*-------------------- TNODES -------------------
  5127. +
  5128. + * List of spare tnodes
  5129. + * The list is hooked together using the first pointer
  5130. + * in the tnode.
  5131. + */
  5132. +
  5133. +/* yaffs_CreateTnodes creates a bunch more tnodes and
  5134. + * adds them to the tnode free list.
  5135. + * Don't use this function directly
  5136. + */
  5137. +
  5138. +static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
  5139. +{
  5140. + int i;
  5141. + int tnodeSize;
  5142. + yaffs_Tnode *newTnodes;
  5143. + __u8 *mem;
  5144. + yaffs_Tnode *curr;
  5145. + yaffs_Tnode *next;
  5146. + yaffs_TnodeList *tnl;
  5147. +
  5148. + if (nTnodes < 1)
  5149. + return YAFFS_OK;
  5150. +
  5151. + /* Calculate the tnode size in bytes for variable width tnode support.
  5152. + * Must be a multiple of 32-bits */
  5153. + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  5154. +
  5155. + if (tnodeSize < sizeof(yaffs_Tnode))
  5156. + tnodeSize = sizeof(yaffs_Tnode);
  5157. +
  5158. + /* make these things */
  5159. +
  5160. + newTnodes = YMALLOC(nTnodes * tnodeSize);
  5161. + mem = (__u8 *)newTnodes;
  5162. +
  5163. + if (!newTnodes) {
  5164. + T(YAFFS_TRACE_ERROR,
  5165. + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
  5166. + return YAFFS_FAIL;
  5167. + }
  5168. +
  5169. + /* Hook them into the free list */
  5170. +#if 0
  5171. + for (i = 0; i < nTnodes - 1; i++) {
  5172. + newTnodes[i].internal[0] = &newTnodes[i + 1];
  5173. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5174. + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5175. +#endif
  5176. + }
  5177. +
  5178. + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
  5179. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5180. + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5181. +#endif
  5182. + dev->freeTnodes = newTnodes;
  5183. +#else
  5184. + /* New hookup for wide tnodes */
  5185. + for (i = 0; i < nTnodes - 1; i++) {
  5186. + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
  5187. + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
  5188. + curr->internal[0] = next;
  5189. + }
  5190. +
  5191. + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
  5192. + curr->internal[0] = dev->freeTnodes;
  5193. + dev->freeTnodes = (yaffs_Tnode *)mem;
  5194. +
  5195. +#endif
  5196. +
  5197. +
  5198. + dev->nFreeTnodes += nTnodes;
  5199. + dev->nTnodesCreated += nTnodes;
  5200. +
  5201. + /* Now add this bunch of tnodes to a list for freeing up.
  5202. + * NB If we can't add this to the management list it isn't fatal
  5203. + * but it just means we can't free this bunch of tnodes later.
  5204. + */
  5205. +
  5206. + tnl = YMALLOC(sizeof(yaffs_TnodeList));
  5207. + if (!tnl) {
  5208. + T(YAFFS_TRACE_ERROR,
  5209. + (TSTR
  5210. + ("yaffs: Could not add tnodes to management list" TENDSTR)));
  5211. + return YAFFS_FAIL;
  5212. + } else {
  5213. + tnl->tnodes = newTnodes;
  5214. + tnl->next = dev->allocatedTnodeList;
  5215. + dev->allocatedTnodeList = tnl;
  5216. + }
  5217. +
  5218. + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
  5219. +
  5220. + return YAFFS_OK;
  5221. +}
  5222. +
  5223. +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
  5224. +
  5225. +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
  5226. +{
  5227. + yaffs_Tnode *tn = NULL;
  5228. +
  5229. + /* If there are none left make more */
  5230. + if (!dev->freeTnodes)
  5231. + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
  5232. +
  5233. + if (dev->freeTnodes) {
  5234. + tn = dev->freeTnodes;
  5235. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5236. + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
  5237. + /* Hoosterman, this thing looks like it isn't in the list */
  5238. + T(YAFFS_TRACE_ALWAYS,
  5239. + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
  5240. + }
  5241. +#endif
  5242. + dev->freeTnodes = dev->freeTnodes->internal[0];
  5243. + dev->nFreeTnodes--;
  5244. + }
  5245. +
  5246. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5247. +
  5248. + return tn;
  5249. +}
  5250. +
  5251. +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
  5252. +{
  5253. + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
  5254. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  5255. +
  5256. + if (tnodeSize < sizeof(yaffs_Tnode))
  5257. + tnodeSize = sizeof(yaffs_Tnode);
  5258. +
  5259. + if (tn)
  5260. + memset(tn, 0, tnodeSize);
  5261. +
  5262. + return tn;
  5263. +}
  5264. +
  5265. +/* FreeTnode frees up a tnode and puts it back on the free list */
  5266. +static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
  5267. +{
  5268. + if (tn) {
  5269. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  5270. + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
  5271. + /* Hoosterman, this thing looks like it is already in the list */
  5272. + T(YAFFS_TRACE_ALWAYS,
  5273. + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
  5274. + }
  5275. + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
  5276. +#endif
  5277. + tn->internal[0] = dev->freeTnodes;
  5278. + dev->freeTnodes = tn;
  5279. + dev->nFreeTnodes++;
  5280. + }
  5281. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5282. +}
  5283. +
  5284. +static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
  5285. +{
  5286. + /* Free the list of allocated tnodes */
  5287. + yaffs_TnodeList *tmp;
  5288. +
  5289. + while (dev->allocatedTnodeList) {
  5290. + tmp = dev->allocatedTnodeList->next;
  5291. +
  5292. + YFREE(dev->allocatedTnodeList->tnodes);
  5293. + YFREE(dev->allocatedTnodeList);
  5294. + dev->allocatedTnodeList = tmp;
  5295. +
  5296. + }
  5297. +
  5298. + dev->freeTnodes = NULL;
  5299. + dev->nFreeTnodes = 0;
  5300. +}
  5301. +
  5302. +static void yaffs_InitialiseTnodes(yaffs_Device *dev)
  5303. +{
  5304. + dev->allocatedTnodeList = NULL;
  5305. + dev->freeTnodes = NULL;
  5306. + dev->nFreeTnodes = 0;
  5307. + dev->nTnodesCreated = 0;
  5308. +}
  5309. +
  5310. +
  5311. +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
  5312. + unsigned val)
  5313. +{
  5314. + __u32 *map = (__u32 *)tn;
  5315. + __u32 bitInMap;
  5316. + __u32 bitInWord;
  5317. + __u32 wordInMap;
  5318. + __u32 mask;
  5319. +
  5320. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  5321. + val >>= dev->chunkGroupBits;
  5322. +
  5323. + bitInMap = pos * dev->tnodeWidth;
  5324. + wordInMap = bitInMap / 32;
  5325. + bitInWord = bitInMap & (32 - 1);
  5326. +
  5327. + mask = dev->tnodeMask << bitInWord;
  5328. +
  5329. + map[wordInMap] &= ~mask;
  5330. + map[wordInMap] |= (mask & (val << bitInWord));
  5331. +
  5332. + if (dev->tnodeWidth > (32 - bitInWord)) {
  5333. + bitInWord = (32 - bitInWord);
  5334. + wordInMap++;;
  5335. + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
  5336. + map[wordInMap] &= ~mask;
  5337. + map[wordInMap] |= (mask & (val >> bitInWord));
  5338. + }
  5339. +}
  5340. +
  5341. +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
  5342. + unsigned pos)
  5343. +{
  5344. + __u32 *map = (__u32 *)tn;
  5345. + __u32 bitInMap;
  5346. + __u32 bitInWord;
  5347. + __u32 wordInMap;
  5348. + __u32 val;
  5349. +
  5350. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  5351. +
  5352. + bitInMap = pos * dev->tnodeWidth;
  5353. + wordInMap = bitInMap / 32;
  5354. + bitInWord = bitInMap & (32 - 1);
  5355. +
  5356. + val = map[wordInMap] >> bitInWord;
  5357. +
  5358. + if (dev->tnodeWidth > (32 - bitInWord)) {
  5359. + bitInWord = (32 - bitInWord);
  5360. + wordInMap++;;
  5361. + val |= (map[wordInMap] << bitInWord);
  5362. + }
  5363. +
  5364. + val &= dev->tnodeMask;
  5365. + val <<= dev->chunkGroupBits;
  5366. +
  5367. + return val;
  5368. +}
  5369. +
  5370. +/* ------------------- End of individual tnode manipulation -----------------*/
  5371. +
  5372. +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  5373. + * The look up tree is represented by the top tnode and the number of topLevel
  5374. + * in the tree. 0 means only the level 0 tnode is in the tree.
  5375. + */
  5376. +
  5377. +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  5378. +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
  5379. + yaffs_FileStructure *fStruct,
  5380. + __u32 chunkId)
  5381. +{
  5382. + yaffs_Tnode *tn = fStruct->top;
  5383. + __u32 i;
  5384. + int requiredTallness;
  5385. + int level = fStruct->topLevel;
  5386. +
  5387. + /* Check sane level and chunk Id */
  5388. + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  5389. + return NULL;
  5390. +
  5391. + if (chunkId > YAFFS_MAX_CHUNK_ID)
  5392. + return NULL;
  5393. +
  5394. + /* First check we're tall enough (ie enough topLevel) */
  5395. +
  5396. + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  5397. + requiredTallness = 0;
  5398. + while (i) {
  5399. + i >>= YAFFS_TNODES_INTERNAL_BITS;
  5400. + requiredTallness++;
  5401. + }
  5402. +
  5403. + if (requiredTallness > fStruct->topLevel)
  5404. + return NULL; /* Not tall enough, so we can't find it */
  5405. +
  5406. + /* Traverse down to level 0 */
  5407. + while (level > 0 && tn) {
  5408. + tn = tn->internal[(chunkId >>
  5409. + (YAFFS_TNODES_LEVEL0_BITS +
  5410. + (level - 1) *
  5411. + YAFFS_TNODES_INTERNAL_BITS)) &
  5412. + YAFFS_TNODES_INTERNAL_MASK];
  5413. + level--;
  5414. + }
  5415. +
  5416. + return tn;
  5417. +}
  5418. +
  5419. +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  5420. + * This happens in two steps:
  5421. + * 1. If the tree isn't tall enough, then make it taller.
  5422. + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  5423. + *
  5424. + * Used when modifying the tree.
  5425. + *
  5426. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  5427. + * be plugged into the ttree.
  5428. + */
  5429. +
  5430. +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
  5431. + yaffs_FileStructure *fStruct,
  5432. + __u32 chunkId,
  5433. + yaffs_Tnode *passedTn)
  5434. +{
  5435. + int requiredTallness;
  5436. + int i;
  5437. + int l;
  5438. + yaffs_Tnode *tn;
  5439. +
  5440. + __u32 x;
  5441. +
  5442. +
  5443. + /* Check sane level and page Id */
  5444. + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
  5445. + return NULL;
  5446. +
  5447. + if (chunkId > YAFFS_MAX_CHUNK_ID)
  5448. + return NULL;
  5449. +
  5450. + /* First check we're tall enough (ie enough topLevel) */
  5451. +
  5452. + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
  5453. + requiredTallness = 0;
  5454. + while (x) {
  5455. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  5456. + requiredTallness++;
  5457. + }
  5458. +
  5459. +
  5460. + if (requiredTallness > fStruct->topLevel) {
  5461. + /* Not tall enough, gotta make the tree taller */
  5462. + for (i = fStruct->topLevel; i < requiredTallness; i++) {
  5463. +
  5464. + tn = yaffs_GetTnode(dev);
  5465. +
  5466. + if (tn) {
  5467. + tn->internal[0] = fStruct->top;
  5468. + fStruct->top = tn;
  5469. + } else {
  5470. + T(YAFFS_TRACE_ERROR,
  5471. + (TSTR("yaffs: no more tnodes" TENDSTR)));
  5472. + }
  5473. + }
  5474. +
  5475. + fStruct->topLevel = requiredTallness;
  5476. + }
  5477. +
  5478. + /* Traverse down to level 0, adding anything we need */
  5479. +
  5480. + l = fStruct->topLevel;
  5481. + tn = fStruct->top;
  5482. +
  5483. + if (l > 0) {
  5484. + while (l > 0 && tn) {
  5485. + x = (chunkId >>
  5486. + (YAFFS_TNODES_LEVEL0_BITS +
  5487. + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  5488. + YAFFS_TNODES_INTERNAL_MASK;
  5489. +
  5490. +
  5491. + if ((l > 1) && !tn->internal[x]) {
  5492. + /* Add missing non-level-zero tnode */
  5493. + tn->internal[x] = yaffs_GetTnode(dev);
  5494. +
  5495. + } else if (l == 1) {
  5496. + /* Looking from level 1 at level 0 */
  5497. + if (passedTn) {
  5498. + /* If we already have one, then release it.*/
  5499. + if (tn->internal[x])
  5500. + yaffs_FreeTnode(dev, tn->internal[x]);
  5501. + tn->internal[x] = passedTn;
  5502. +
  5503. + } else if (!tn->internal[x]) {
  5504. + /* Don't have one, none passed in */
  5505. + tn->internal[x] = yaffs_GetTnode(dev);
  5506. + }
  5507. + }
  5508. +
  5509. + tn = tn->internal[x];
  5510. + l--;
  5511. + }
  5512. + } else {
  5513. + /* top is level 0 */
  5514. + if (passedTn) {
  5515. + memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
  5516. + yaffs_FreeTnode(dev, passedTn);
  5517. + }
  5518. + }
  5519. +
  5520. + return tn;
  5521. +}
  5522. +
  5523. +static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
  5524. + yaffs_ExtendedTags *tags, int objectId,
  5525. + int chunkInInode)
  5526. +{
  5527. + int j;
  5528. +
  5529. + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
  5530. + if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock,
  5531. + theChunk % dev->nChunksPerBlock)) {
  5532. + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
  5533. + tags);
  5534. + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
  5535. + /* found it; */
  5536. + return theChunk;
  5537. + }
  5538. + }
  5539. + theChunk++;
  5540. + }
  5541. + return -1;
  5542. +}
  5543. +
  5544. +
  5545. +/* DeleteWorker scans backwards through the tnode tree and deletes all the
  5546. + * chunks and tnodes in the file
  5547. + * Returns 1 if the tree was deleted.
  5548. + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
  5549. + */
  5550. +
  5551. +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
  5552. + int chunkOffset, int *limit)
  5553. +{
  5554. + int i;
  5555. + int chunkInInode;
  5556. + int theChunk;
  5557. + yaffs_ExtendedTags tags;
  5558. + int foundChunk;
  5559. + yaffs_Device *dev = in->myDev;
  5560. +
  5561. + int allDone = 1;
  5562. +
  5563. + if (tn) {
  5564. + if (level > 0) {
  5565. + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  5566. + i--) {
  5567. + if (tn->internal[i]) {
  5568. + if (limit && (*limit) < 0) {
  5569. + allDone = 0;
  5570. + } else {
  5571. + allDone =
  5572. + yaffs_DeleteWorker(in,
  5573. + tn->
  5574. + internal
  5575. + [i],
  5576. + level -
  5577. + 1,
  5578. + (chunkOffset
  5579. + <<
  5580. + YAFFS_TNODES_INTERNAL_BITS)
  5581. + + i,
  5582. + limit);
  5583. + }
  5584. + if (allDone) {
  5585. + yaffs_FreeTnode(dev,
  5586. + tn->
  5587. + internal[i]);
  5588. + tn->internal[i] = NULL;
  5589. + }
  5590. + }
  5591. + }
  5592. + return (allDone) ? 1 : 0;
  5593. + } else if (level == 0) {
  5594. + int hitLimit = 0;
  5595. +
  5596. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
  5597. + i--) {
  5598. + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  5599. + if (theChunk) {
  5600. +
  5601. + chunkInInode = (chunkOffset <<
  5602. + YAFFS_TNODES_LEVEL0_BITS) + i;
  5603. +
  5604. + foundChunk =
  5605. + yaffs_FindChunkInGroup(dev,
  5606. + theChunk,
  5607. + &tags,
  5608. + in->objectId,
  5609. + chunkInInode);
  5610. +
  5611. + if (foundChunk > 0) {
  5612. + yaffs_DeleteChunk(dev,
  5613. + foundChunk, 1,
  5614. + __LINE__);
  5615. + in->nDataChunks--;
  5616. + if (limit) {
  5617. + *limit = *limit - 1;
  5618. + if (*limit <= 0)
  5619. + hitLimit = 1;
  5620. + }
  5621. +
  5622. + }
  5623. +
  5624. + yaffs_PutLevel0Tnode(dev, tn, i, 0);
  5625. + }
  5626. +
  5627. + }
  5628. + return (i < 0) ? 1 : 0;
  5629. +
  5630. + }
  5631. +
  5632. + }
  5633. +
  5634. + return 1;
  5635. +
  5636. +}
  5637. +
  5638. +static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
  5639. +{
  5640. + yaffs_BlockInfo *theBlock;
  5641. +
  5642. + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
  5643. +
  5644. + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
  5645. + if (theBlock) {
  5646. + theBlock->softDeletions++;
  5647. + dev->nFreeChunks++;
  5648. + }
  5649. +}
  5650. +
  5651. +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  5652. + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  5653. + * of the tnode.
  5654. + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  5655. + */
  5656. +
  5657. +static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
  5658. + __u32 level, int chunkOffset)
  5659. +{
  5660. + int i;
  5661. + int theChunk;
  5662. + int allDone = 1;
  5663. + yaffs_Device *dev = in->myDev;
  5664. +
  5665. + if (tn) {
  5666. + if (level > 0) {
  5667. +
  5668. + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
  5669. + i--) {
  5670. + if (tn->internal[i]) {
  5671. + allDone =
  5672. + yaffs_SoftDeleteWorker(in,
  5673. + tn->
  5674. + internal[i],
  5675. + level - 1,
  5676. + (chunkOffset
  5677. + <<
  5678. + YAFFS_TNODES_INTERNAL_BITS)
  5679. + + i);
  5680. + if (allDone) {
  5681. + yaffs_FreeTnode(dev,
  5682. + tn->
  5683. + internal[i]);
  5684. + tn->internal[i] = NULL;
  5685. + } else {
  5686. + /* Hoosterman... how could this happen? */
  5687. + }
  5688. + }
  5689. + }
  5690. + return (allDone) ? 1 : 0;
  5691. + } else if (level == 0) {
  5692. +
  5693. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  5694. + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
  5695. + if (theChunk) {
  5696. + /* Note this does not find the real chunk, only the chunk group.
  5697. + * We make an assumption that a chunk group is not larger than
  5698. + * a block.
  5699. + */
  5700. + yaffs_SoftDeleteChunk(dev, theChunk);
  5701. + yaffs_PutLevel0Tnode(dev, tn, i, 0);
  5702. + }
  5703. +
  5704. + }
  5705. + return 1;
  5706. +
  5707. + }
  5708. +
  5709. + }
  5710. +
  5711. + return 1;
  5712. +
  5713. +}
  5714. +
  5715. +static void yaffs_SoftDeleteFile(yaffs_Object *obj)
  5716. +{
  5717. + if (obj->deleted &&
  5718. + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
  5719. + if (obj->nDataChunks <= 0) {
  5720. + /* Empty file with no duplicate object headers, just delete it immediately */
  5721. + yaffs_FreeTnode(obj->myDev,
  5722. + obj->variant.fileVariant.top);
  5723. + obj->variant.fileVariant.top = NULL;
  5724. + T(YAFFS_TRACE_TRACING,
  5725. + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
  5726. + obj->objectId));
  5727. + yaffs_DoGenericObjectDeletion(obj);
  5728. + } else {
  5729. + yaffs_SoftDeleteWorker(obj,
  5730. + obj->variant.fileVariant.top,
  5731. + obj->variant.fileVariant.
  5732. + topLevel, 0);
  5733. + obj->softDeleted = 1;
  5734. + }
  5735. + }
  5736. +}
  5737. +
  5738. +/* Pruning removes any part of the file structure tree that is beyond the
  5739. + * bounds of the file (ie that does not point to chunks).
  5740. + *
  5741. + * A file should only get pruned when its size is reduced.
  5742. + *
  5743. + * Before pruning, the chunks must be pulled from the tree and the
  5744. + * level 0 tnode entries must be zeroed out.
  5745. + * Could also use this for file deletion, but that's probably better handled
  5746. + * by a special case.
  5747. + */
  5748. +
  5749. +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
  5750. + __u32 level, int del0)
  5751. +{
  5752. + int i;
  5753. + int hasData;
  5754. +
  5755. + if (tn) {
  5756. + hasData = 0;
  5757. +
  5758. + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  5759. + if (tn->internal[i] && level > 0) {
  5760. + tn->internal[i] =
  5761. + yaffs_PruneWorker(dev, tn->internal[i],
  5762. + level - 1,
  5763. + (i == 0) ? del0 : 1);
  5764. + }
  5765. +
  5766. + if (tn->internal[i])
  5767. + hasData++;
  5768. + }
  5769. +
  5770. + if (hasData == 0 && del0) {
  5771. + /* Free and return NULL */
  5772. +
  5773. + yaffs_FreeTnode(dev, tn);
  5774. + tn = NULL;
  5775. + }
  5776. +
  5777. + }
  5778. +
  5779. + return tn;
  5780. +
  5781. +}
  5782. +
  5783. +static int yaffs_PruneFileStructure(yaffs_Device *dev,
  5784. + yaffs_FileStructure *fStruct)
  5785. +{
  5786. + int i;
  5787. + int hasData;
  5788. + int done = 0;
  5789. + yaffs_Tnode *tn;
  5790. +
  5791. + if (fStruct->topLevel > 0) {
  5792. + fStruct->top =
  5793. + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
  5794. +
  5795. + /* Now we have a tree with all the non-zero branches NULL but the height
  5796. + * is the same as it was.
  5797. + * Let's see if we can trim internal tnodes to shorten the tree.
  5798. + * We can do this if only the 0th element in the tnode is in use
  5799. + * (ie all the non-zero are NULL)
  5800. + */
  5801. +
  5802. + while (fStruct->topLevel && !done) {
  5803. + tn = fStruct->top;
  5804. +
  5805. + hasData = 0;
  5806. + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  5807. + if (tn->internal[i])
  5808. + hasData++;
  5809. + }
  5810. +
  5811. + if (!hasData) {
  5812. + fStruct->top = tn->internal[0];
  5813. + fStruct->topLevel--;
  5814. + yaffs_FreeTnode(dev, tn);
  5815. + } else {
  5816. + done = 1;
  5817. + }
  5818. + }
  5819. + }
  5820. +
  5821. + return YAFFS_OK;
  5822. +}
  5823. +
  5824. +/*-------------------- End of File Structure functions.-------------------*/
  5825. +
  5826. +/* yaffs_CreateFreeObjects creates a bunch more objects and
  5827. + * adds them to the object free list.
  5828. + */
  5829. +static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
  5830. +{
  5831. + int i;
  5832. + yaffs_Object *newObjects;
  5833. + yaffs_ObjectList *list;
  5834. +
  5835. + if (nObjects < 1)
  5836. + return YAFFS_OK;
  5837. +
  5838. + /* make these things */
  5839. + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
  5840. + list = YMALLOC(sizeof(yaffs_ObjectList));
  5841. +
  5842. + if (!newObjects || !list) {
  5843. + if (newObjects)
  5844. + YFREE(newObjects);
  5845. + if (list)
  5846. + YFREE(list);
  5847. + T(YAFFS_TRACE_ALLOCATE,
  5848. + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
  5849. + return YAFFS_FAIL;
  5850. + }
  5851. +
  5852. + /* Hook them into the free list */
  5853. + for (i = 0; i < nObjects - 1; i++) {
  5854. + newObjects[i].siblings.next =
  5855. + (struct ylist_head *)(&newObjects[i + 1]);
  5856. + }
  5857. +
  5858. + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
  5859. + dev->freeObjects = newObjects;
  5860. + dev->nFreeObjects += nObjects;
  5861. + dev->nObjectsCreated += nObjects;
  5862. +
  5863. + /* Now add this bunch of Objects to a list for freeing up. */
  5864. +
  5865. + list->objects = newObjects;
  5866. + list->next = dev->allocatedObjectList;
  5867. + dev->allocatedObjectList = list;
  5868. +
  5869. + return YAFFS_OK;
  5870. +}
  5871. +
  5872. +
  5873. +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  5874. +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
  5875. +{
  5876. + yaffs_Object *tn = NULL;
  5877. +
  5878. +#ifdef VALGRIND_TEST
  5879. + tn = YMALLOC(sizeof(yaffs_Object));
  5880. +#else
  5881. + /* If there are none left make more */
  5882. + if (!dev->freeObjects)
  5883. + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
  5884. +
  5885. + if (dev->freeObjects) {
  5886. + tn = dev->freeObjects;
  5887. + dev->freeObjects =
  5888. + (yaffs_Object *) (dev->freeObjects->siblings.next);
  5889. + dev->nFreeObjects--;
  5890. + }
  5891. +#endif
  5892. + if (tn) {
  5893. + /* Now sweeten it up... */
  5894. +
  5895. + memset(tn, 0, sizeof(yaffs_Object));
  5896. + tn->beingCreated = 1;
  5897. +
  5898. + tn->myDev = dev;
  5899. + tn->hdrChunk = 0;
  5900. + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
  5901. + YINIT_LIST_HEAD(&(tn->hardLinks));
  5902. + YINIT_LIST_HEAD(&(tn->hashLink));
  5903. + YINIT_LIST_HEAD(&tn->siblings);
  5904. +
  5905. +
  5906. + /* Now make the directory sane */
  5907. + if (dev->rootDir) {
  5908. + tn->parent = dev->rootDir;
  5909. + ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
  5910. + }
  5911. +
  5912. + /* Add it to the lost and found directory.
  5913. + * NB Can't put root or lostNFound in lostNFound so
  5914. + * check if lostNFound exists first
  5915. + */
  5916. + if (dev->lostNFoundDir)
  5917. + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
  5918. +
  5919. + tn->beingCreated = 0;
  5920. + }
  5921. +
  5922. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5923. +
  5924. + return tn;
  5925. +}
  5926. +
  5927. +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
  5928. + __u32 mode)
  5929. +{
  5930. +
  5931. + yaffs_Object *obj =
  5932. + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  5933. + if (obj) {
  5934. + obj->fake = 1; /* it is fake so it might have no NAND presence... */
  5935. + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
  5936. + obj->unlinkAllowed = 0; /* ... or unlink it */
  5937. + obj->deleted = 0;
  5938. + obj->unlinked = 0;
  5939. + obj->yst_mode = mode;
  5940. + obj->myDev = dev;
  5941. + obj->hdrChunk = 0; /* Not a valid chunk. */
  5942. + }
  5943. +
  5944. + return obj;
  5945. +
  5946. +}
  5947. +
  5948. +static void yaffs_UnhashObject(yaffs_Object *tn)
  5949. +{
  5950. + int bucket;
  5951. + yaffs_Device *dev = tn->myDev;
  5952. +
  5953. + /* If it is still linked into the bucket list, free from the list */
  5954. + if (!ylist_empty(&tn->hashLink)) {
  5955. + ylist_del_init(&tn->hashLink);
  5956. + bucket = yaffs_HashFunction(tn->objectId);
  5957. + dev->objectBucket[bucket].count--;
  5958. + }
  5959. +}
  5960. +
  5961. +/* FreeObject frees up a Object and puts it back on the free list */
  5962. +static void yaffs_FreeObject(yaffs_Object *tn)
  5963. +{
  5964. + yaffs_Device *dev = tn->myDev;
  5965. +
  5966. +#ifdef __KERNEL__
  5967. + T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
  5968. +#endif
  5969. +
  5970. + if (tn->parent)
  5971. + YBUG();
  5972. + if (!ylist_empty(&tn->siblings))
  5973. + YBUG();
  5974. +
  5975. +
  5976. +#ifdef __KERNEL__
  5977. + if (tn->myInode) {
  5978. + /* We're still hooked up to a cached inode.
  5979. + * Don't delete now, but mark for later deletion
  5980. + */
  5981. + tn->deferedFree = 1;
  5982. + return;
  5983. + }
  5984. +#endif
  5985. +
  5986. + yaffs_UnhashObject(tn);
  5987. +
  5988. +#ifdef VALGRIND_TEST
  5989. + YFREE(tn);
  5990. +#else
  5991. + /* Link into the free list. */
  5992. + tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
  5993. + dev->freeObjects = tn;
  5994. + dev->nFreeObjects++;
  5995. +#endif
  5996. + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
  5997. +}
  5998. +
  5999. +#ifdef __KERNEL__
  6000. +
  6001. +void yaffs_HandleDeferedFree(yaffs_Object *obj)
  6002. +{
  6003. + if (obj->deferedFree)
  6004. + yaffs_FreeObject(obj);
  6005. +}
  6006. +
  6007. +#endif
  6008. +
  6009. +static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
  6010. +{
  6011. + /* Free the list of allocated Objects */
  6012. +
  6013. + yaffs_ObjectList *tmp;
  6014. +
  6015. + while (dev->allocatedObjectList) {
  6016. + tmp = dev->allocatedObjectList->next;
  6017. + YFREE(dev->allocatedObjectList->objects);
  6018. + YFREE(dev->allocatedObjectList);
  6019. +
  6020. + dev->allocatedObjectList = tmp;
  6021. + }
  6022. +
  6023. + dev->freeObjects = NULL;
  6024. + dev->nFreeObjects = 0;
  6025. +}
  6026. +
  6027. +static void yaffs_InitialiseObjects(yaffs_Device *dev)
  6028. +{
  6029. + int i;
  6030. +
  6031. + dev->allocatedObjectList = NULL;
  6032. + dev->freeObjects = NULL;
  6033. + dev->nFreeObjects = 0;
  6034. +
  6035. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  6036. + YINIT_LIST_HEAD(&dev->objectBucket[i].list);
  6037. + dev->objectBucket[i].count = 0;
  6038. + }
  6039. +}
  6040. +
  6041. +static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
  6042. +{
  6043. + static int x;
  6044. + int i;
  6045. + int l = 999;
  6046. + int lowest = 999999;
  6047. +
  6048. + /* First let's see if we can find one that's empty. */
  6049. +
  6050. + for (i = 0; i < 10 && lowest > 0; i++) {
  6051. + x++;
  6052. + x %= YAFFS_NOBJECT_BUCKETS;
  6053. + if (dev->objectBucket[x].count < lowest) {
  6054. + lowest = dev->objectBucket[x].count;
  6055. + l = x;
  6056. + }
  6057. +
  6058. + }
  6059. +
  6060. + /* If we didn't find an empty list, then try
  6061. + * looking a bit further for a short one
  6062. + */
  6063. +
  6064. + for (i = 0; i < 10 && lowest > 3; i++) {
  6065. + x++;
  6066. + x %= YAFFS_NOBJECT_BUCKETS;
  6067. + if (dev->objectBucket[x].count < lowest) {
  6068. + lowest = dev->objectBucket[x].count;
  6069. + l = x;
  6070. + }
  6071. +
  6072. + }
  6073. +
  6074. + return l;
  6075. +}
  6076. +
  6077. +static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
  6078. +{
  6079. + int bucket = yaffs_FindNiceObjectBucket(dev);
  6080. +
  6081. + /* Now find an object value that has not already been taken
  6082. + * by scanning the list.
  6083. + */
  6084. +
  6085. + int found = 0;
  6086. + struct ylist_head *i;
  6087. +
  6088. + __u32 n = (__u32) bucket;
  6089. +
  6090. + /* yaffs_CheckObjectHashSanity(); */
  6091. +
  6092. + while (!found) {
  6093. + found = 1;
  6094. + n += YAFFS_NOBJECT_BUCKETS;
  6095. + if (1 || dev->objectBucket[bucket].count > 0) {
  6096. + ylist_for_each(i, &dev->objectBucket[bucket].list) {
  6097. + /* If there is already one in the list */
  6098. + if (i && ylist_entry(i, yaffs_Object,
  6099. + hashLink)->objectId == n) {
  6100. + found = 0;
  6101. + }
  6102. + }
  6103. + }
  6104. + }
  6105. +
  6106. + return n;
  6107. +}
  6108. +
  6109. +static void yaffs_HashObject(yaffs_Object *in)
  6110. +{
  6111. + int bucket = yaffs_HashFunction(in->objectId);
  6112. + yaffs_Device *dev = in->myDev;
  6113. +
  6114. + ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
  6115. + dev->objectBucket[bucket].count++;
  6116. +}
  6117. +
  6118. +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
  6119. +{
  6120. + int bucket = yaffs_HashFunction(number);
  6121. + struct ylist_head *i;
  6122. + yaffs_Object *in;
  6123. +
  6124. + ylist_for_each(i, &dev->objectBucket[bucket].list) {
  6125. + /* Look if it is in the list */
  6126. + if (i) {
  6127. + in = ylist_entry(i, yaffs_Object, hashLink);
  6128. + if (in->objectId == number) {
  6129. +#ifdef __KERNEL__
  6130. + /* Don't tell the VFS about this one if it is defered free */
  6131. + if (in->deferedFree)
  6132. + return NULL;
  6133. +#endif
  6134. +
  6135. + return in;
  6136. + }
  6137. + }
  6138. + }
  6139. +
  6140. + return NULL;
  6141. +}
  6142. +
  6143. +yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
  6144. + yaffs_ObjectType type)
  6145. +{
  6146. + yaffs_Object *theObject;
  6147. + yaffs_Tnode *tn = NULL;
  6148. +
  6149. + if (number < 0)
  6150. + number = yaffs_CreateNewObjectNumber(dev);
  6151. +
  6152. + theObject = yaffs_AllocateEmptyObject(dev);
  6153. + if (!theObject)
  6154. + return NULL;
  6155. +
  6156. + if (type == YAFFS_OBJECT_TYPE_FILE) {
  6157. + tn = yaffs_GetTnode(dev);
  6158. + if (!tn) {
  6159. + yaffs_FreeObject(theObject);
  6160. + return NULL;
  6161. + }
  6162. + }
  6163. +
  6164. + if (theObject) {
  6165. + theObject->fake = 0;
  6166. + theObject->renameAllowed = 1;
  6167. + theObject->unlinkAllowed = 1;
  6168. + theObject->objectId = number;
  6169. + yaffs_HashObject(theObject);
  6170. + theObject->variantType = type;
  6171. +#ifdef CONFIG_YAFFS_WINCE
  6172. + yfsd_WinFileTimeNow(theObject->win_atime);
  6173. + theObject->win_ctime[0] = theObject->win_mtime[0] =
  6174. + theObject->win_atime[0];
  6175. + theObject->win_ctime[1] = theObject->win_mtime[1] =
  6176. + theObject->win_atime[1];
  6177. +
  6178. +#else
  6179. +
  6180. + theObject->yst_atime = theObject->yst_mtime =
  6181. + theObject->yst_ctime = Y_CURRENT_TIME;
  6182. +#endif
  6183. + switch (type) {
  6184. + case YAFFS_OBJECT_TYPE_FILE:
  6185. + theObject->variant.fileVariant.fileSize = 0;
  6186. + theObject->variant.fileVariant.scannedFileSize = 0;
  6187. + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
  6188. + theObject->variant.fileVariant.topLevel = 0;
  6189. + theObject->variant.fileVariant.top = tn;
  6190. + break;
  6191. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6192. + YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
  6193. + children);
  6194. + break;
  6195. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6196. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6197. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6198. + /* No action required */
  6199. + break;
  6200. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  6201. + /* todo this should not happen */
  6202. + break;
  6203. + }
  6204. + }
  6205. +
  6206. + return theObject;
  6207. +}
  6208. +
  6209. +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
  6210. + int number,
  6211. + yaffs_ObjectType type)
  6212. +{
  6213. + yaffs_Object *theObject = NULL;
  6214. +
  6215. + if (number > 0)
  6216. + theObject = yaffs_FindObjectByNumber(dev, number);
  6217. +
  6218. + if (!theObject)
  6219. + theObject = yaffs_CreateNewObject(dev, number, type);
  6220. +
  6221. + return theObject;
  6222. +
  6223. +}
  6224. +
  6225. +
  6226. +static YCHAR *yaffs_CloneString(const YCHAR *str)
  6227. +{
  6228. + YCHAR *newStr = NULL;
  6229. +
  6230. + if (str && *str) {
  6231. + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
  6232. + if (newStr)
  6233. + yaffs_strcpy(newStr, str);
  6234. + }
  6235. +
  6236. + return newStr;
  6237. +
  6238. +}
  6239. +
  6240. +/*
  6241. + * Mknod (create) a new object.
  6242. + * equivalentObject only has meaning for a hard link;
  6243. + * aliasString only has meaning for a sumlink.
  6244. + * rdev only has meaning for devices (a subset of special objects)
  6245. + */
  6246. +
  6247. +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
  6248. + yaffs_Object *parent,
  6249. + const YCHAR *name,
  6250. + __u32 mode,
  6251. + __u32 uid,
  6252. + __u32 gid,
  6253. + yaffs_Object *equivalentObject,
  6254. + const YCHAR *aliasString, __u32 rdev)
  6255. +{
  6256. + yaffs_Object *in;
  6257. + YCHAR *str = NULL;
  6258. +
  6259. + yaffs_Device *dev = parent->myDev;
  6260. +
  6261. + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
  6262. + if (yaffs_FindObjectByName(parent, name))
  6263. + return NULL;
  6264. +
  6265. + in = yaffs_CreateNewObject(dev, -1, type);
  6266. +
  6267. + if (!in)
  6268. + return YAFFS_FAIL;
  6269. +
  6270. + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6271. + str = yaffs_CloneString(aliasString);
  6272. + if (!str) {
  6273. + yaffs_FreeObject(in);
  6274. + return NULL;
  6275. + }
  6276. + }
  6277. +
  6278. +
  6279. +
  6280. + if (in) {
  6281. + in->hdrChunk = 0;
  6282. + in->valid = 1;
  6283. + in->variantType = type;
  6284. +
  6285. + in->yst_mode = mode;
  6286. +
  6287. +#ifdef CONFIG_YAFFS_WINCE
  6288. + yfsd_WinFileTimeNow(in->win_atime);
  6289. + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
  6290. + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
  6291. +
  6292. +#else
  6293. + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
  6294. +
  6295. + in->yst_rdev = rdev;
  6296. + in->yst_uid = uid;
  6297. + in->yst_gid = gid;
  6298. +#endif
  6299. + in->nDataChunks = 0;
  6300. +
  6301. + yaffs_SetObjectName(in, name);
  6302. + in->dirty = 1;
  6303. +
  6304. + yaffs_AddObjectToDirectory(parent, in);
  6305. +
  6306. + in->myDev = parent->myDev;
  6307. +
  6308. + switch (type) {
  6309. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6310. + in->variant.symLinkVariant.alias = str;
  6311. + break;
  6312. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6313. + in->variant.hardLinkVariant.equivalentObject =
  6314. + equivalentObject;
  6315. + in->variant.hardLinkVariant.equivalentObjectId =
  6316. + equivalentObject->objectId;
  6317. + ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
  6318. + break;
  6319. + case YAFFS_OBJECT_TYPE_FILE:
  6320. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6321. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6322. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  6323. + /* do nothing */
  6324. + break;
  6325. + }
  6326. +
  6327. + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
  6328. + /* Could not create the object header, fail the creation */
  6329. + yaffs_DeleteObject(in);
  6330. + in = NULL;
  6331. + }
  6332. +
  6333. + }
  6334. +
  6335. + return in;
  6336. +}
  6337. +
  6338. +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
  6339. + __u32 mode, __u32 uid, __u32 gid)
  6340. +{
  6341. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  6342. + uid, gid, NULL, NULL, 0);
  6343. +}
  6344. +
  6345. +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
  6346. + __u32 mode, __u32 uid, __u32 gid)
  6347. +{
  6348. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  6349. + mode, uid, gid, NULL, NULL, 0);
  6350. +}
  6351. +
  6352. +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
  6353. + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
  6354. +{
  6355. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  6356. + uid, gid, NULL, NULL, rdev);
  6357. +}
  6358. +
  6359. +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
  6360. + __u32 mode, __u32 uid, __u32 gid,
  6361. + const YCHAR *alias)
  6362. +{
  6363. + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  6364. + uid, gid, NULL, alias, 0);
  6365. +}
  6366. +
  6367. +/* yaffs_Link returns the object id of the equivalent object.*/
  6368. +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
  6369. + yaffs_Object *equivalentObject)
  6370. +{
  6371. + /* Get the real object in case we were fed a hard link as an equivalent object */
  6372. + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
  6373. +
  6374. + if (yaffs_MknodObject
  6375. + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  6376. + equivalentObject, NULL, 0)) {
  6377. + return equivalentObject;
  6378. + } else {
  6379. + return NULL;
  6380. + }
  6381. +
  6382. +}
  6383. +
  6384. +static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
  6385. + const YCHAR *newName, int force, int shadows)
  6386. +{
  6387. + int unlinkOp;
  6388. + int deleteOp;
  6389. +
  6390. + yaffs_Object *existingTarget;
  6391. +
  6392. + if (newDir == NULL)
  6393. + newDir = obj->parent; /* use the old directory */
  6394. +
  6395. + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6396. + T(YAFFS_TRACE_ALWAYS,
  6397. + (TSTR
  6398. + ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
  6399. + TENDSTR)));
  6400. + YBUG();
  6401. + }
  6402. +
  6403. + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  6404. + if (obj->myDev->isYaffs2)
  6405. + unlinkOp = (newDir == obj->myDev->unlinkedDir);
  6406. + else
  6407. + unlinkOp = (newDir == obj->myDev->unlinkedDir
  6408. + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
  6409. +
  6410. + deleteOp = (newDir == obj->myDev->deletedDir);
  6411. +
  6412. + existingTarget = yaffs_FindObjectByName(newDir, newName);
  6413. +
  6414. + /* If the object is a file going into the unlinked directory,
  6415. + * then it is OK to just stuff it in since duplicate names are allowed.
  6416. + * else only proceed if the new name does not exist and if we're putting
  6417. + * it into a directory.
  6418. + */
  6419. + if ((unlinkOp ||
  6420. + deleteOp ||
  6421. + force ||
  6422. + (shadows > 0) ||
  6423. + !existingTarget) &&
  6424. + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
  6425. + yaffs_SetObjectName(obj, newName);
  6426. + obj->dirty = 1;
  6427. +
  6428. + yaffs_AddObjectToDirectory(newDir, obj);
  6429. +
  6430. + if (unlinkOp)
  6431. + obj->unlinked = 1;
  6432. +
  6433. + /* If it is a deletion then we mark it as a shrink for gc purposes. */
  6434. + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
  6435. + return YAFFS_OK;
  6436. + }
  6437. +
  6438. + return YAFFS_FAIL;
  6439. +}
  6440. +
  6441. +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
  6442. + yaffs_Object *newDir, const YCHAR *newName)
  6443. +{
  6444. + yaffs_Object *obj = NULL;
  6445. + yaffs_Object *existingTarget = NULL;
  6446. + int force = 0;
  6447. +
  6448. +
  6449. + if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
  6450. + YBUG();
  6451. + if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
  6452. + YBUG();
  6453. +
  6454. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  6455. + /* Special case for case insemsitive systems (eg. WinCE).
  6456. + * While look-up is case insensitive, the name isn't.
  6457. + * Therefore we might want to change x.txt to X.txt
  6458. + */
  6459. + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
  6460. + force = 1;
  6461. +#endif
  6462. +
  6463. + else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
  6464. + /* ENAMETOOLONG */
  6465. + return YAFFS_FAIL;
  6466. +
  6467. + obj = yaffs_FindObjectByName(oldDir, oldName);
  6468. +
  6469. + if (obj && obj->renameAllowed) {
  6470. +
  6471. + /* Now do the handling for an existing target, if there is one */
  6472. +
  6473. + existingTarget = yaffs_FindObjectByName(newDir, newName);
  6474. + if (existingTarget &&
  6475. + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
  6476. + !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
  6477. + /* There is a target that is a non-empty directory, so we fail */
  6478. + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
  6479. + } else if (existingTarget && existingTarget != obj) {
  6480. + /* Nuke the target first, using shadowing,
  6481. + * but only if it isn't the same object
  6482. + */
  6483. + yaffs_ChangeObjectName(obj, newDir, newName, force,
  6484. + existingTarget->objectId);
  6485. + yaffs_UnlinkObject(existingTarget);
  6486. + }
  6487. +
  6488. + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
  6489. + }
  6490. + return YAFFS_FAIL;
  6491. +}
  6492. +
  6493. +/*------------------------- Block Management and Page Allocation ----------------*/
  6494. +
  6495. +static int yaffs_InitialiseBlocks(yaffs_Device *dev)
  6496. +{
  6497. + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  6498. +
  6499. + dev->blockInfo = NULL;
  6500. + dev->chunkBits = NULL;
  6501. +
  6502. + dev->allocationBlock = -1; /* force it to get a new one */
  6503. +
  6504. + /* If the first allocation strategy fails, thry the alternate one */
  6505. + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
  6506. + if (!dev->blockInfo) {
  6507. + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
  6508. + dev->blockInfoAlt = 1;
  6509. + } else
  6510. + dev->blockInfoAlt = 0;
  6511. +
  6512. + if (dev->blockInfo) {
  6513. + /* Set up dynamic blockinfo stuff. */
  6514. + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
  6515. + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
  6516. + if (!dev->chunkBits) {
  6517. + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
  6518. + dev->chunkBitsAlt = 1;
  6519. + } else
  6520. + dev->chunkBitsAlt = 0;
  6521. + }
  6522. +
  6523. + if (dev->blockInfo && dev->chunkBits) {
  6524. + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
  6525. + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
  6526. + return YAFFS_OK;
  6527. + }
  6528. +
  6529. + return YAFFS_FAIL;
  6530. +}
  6531. +
  6532. +static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
  6533. +{
  6534. + if (dev->blockInfoAlt && dev->blockInfo)
  6535. + YFREE_ALT(dev->blockInfo);
  6536. + else if (dev->blockInfo)
  6537. + YFREE(dev->blockInfo);
  6538. +
  6539. + dev->blockInfoAlt = 0;
  6540. +
  6541. + dev->blockInfo = NULL;
  6542. +
  6543. + if (dev->chunkBitsAlt && dev->chunkBits)
  6544. + YFREE_ALT(dev->chunkBits);
  6545. + else if (dev->chunkBits)
  6546. + YFREE(dev->chunkBits);
  6547. + dev->chunkBitsAlt = 0;
  6548. + dev->chunkBits = NULL;
  6549. +}
  6550. +
  6551. +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
  6552. + yaffs_BlockInfo *bi)
  6553. +{
  6554. + int i;
  6555. + __u32 seq;
  6556. + yaffs_BlockInfo *b;
  6557. +
  6558. + if (!dev->isYaffs2)
  6559. + return 1; /* disqualification only applies to yaffs2. */
  6560. +
  6561. + if (!bi->hasShrinkHeader)
  6562. + return 1; /* can gc */
  6563. +
  6564. + /* Find the oldest dirty sequence number if we don't know it and save it
  6565. + * so we don't have to keep recomputing it.
  6566. + */
  6567. + if (!dev->oldestDirtySequence) {
  6568. + seq = dev->sequenceNumber;
  6569. +
  6570. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
  6571. + i++) {
  6572. + b = yaffs_GetBlockInfo(dev, i);
  6573. + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
  6574. + (b->pagesInUse - b->softDeletions) <
  6575. + dev->nChunksPerBlock && b->sequenceNumber < seq) {
  6576. + seq = b->sequenceNumber;
  6577. + }
  6578. + }
  6579. + dev->oldestDirtySequence = seq;
  6580. + }
  6581. +
  6582. + /* Can't do gc of this block if there are any blocks older than this one that have
  6583. + * discarded pages.
  6584. + */
  6585. + return (bi->sequenceNumber <= dev->oldestDirtySequence);
  6586. +}
  6587. +
  6588. +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
  6589. + * for garbage collection.
  6590. + */
  6591. +
  6592. +static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
  6593. + int aggressive)
  6594. +{
  6595. + int b = dev->currentDirtyChecker;
  6596. +
  6597. + int i;
  6598. + int iterations;
  6599. + int dirtiest = -1;
  6600. + int pagesInUse = 0;
  6601. + int prioritised = 0;
  6602. + yaffs_BlockInfo *bi;
  6603. + int pendingPrioritisedExist = 0;
  6604. +
  6605. + /* First let's see if we need to grab a prioritised block */
  6606. + if (dev->hasPendingPrioritisedGCs) {
  6607. + for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
  6608. +
  6609. + bi = yaffs_GetBlockInfo(dev, i);
  6610. + /* yaffs_VerifyBlock(dev,bi,i); */
  6611. +
  6612. + if (bi->gcPrioritise) {
  6613. + pendingPrioritisedExist = 1;
  6614. + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  6615. + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
  6616. + pagesInUse = (bi->pagesInUse - bi->softDeletions);
  6617. + dirtiest = i;
  6618. + prioritised = 1;
  6619. + aggressive = 1; /* Fool the non-aggressive skip logiv below */
  6620. + }
  6621. + }
  6622. + }
  6623. +
  6624. + if (!pendingPrioritisedExist) /* None found, so we can clear this */
  6625. + dev->hasPendingPrioritisedGCs = 0;
  6626. + }
  6627. +
  6628. + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  6629. + * search harder.
  6630. + * else (we're doing a leasurely gc), then we only bother to do this if the
  6631. + * block has only a few pages in use.
  6632. + */
  6633. +
  6634. + dev->nonAggressiveSkip--;
  6635. +
  6636. + if (!aggressive && (dev->nonAggressiveSkip > 0))
  6637. + return -1;
  6638. +
  6639. + if (!prioritised)
  6640. + pagesInUse =
  6641. + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
  6642. +
  6643. + if (aggressive)
  6644. + iterations =
  6645. + dev->internalEndBlock - dev->internalStartBlock + 1;
  6646. + else {
  6647. + iterations =
  6648. + dev->internalEndBlock - dev->internalStartBlock + 1;
  6649. + iterations = iterations / 16;
  6650. + if (iterations > 200)
  6651. + iterations = 200;
  6652. + }
  6653. +
  6654. + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
  6655. + b++;
  6656. + if (b < dev->internalStartBlock || b > dev->internalEndBlock)
  6657. + b = dev->internalStartBlock;
  6658. +
  6659. + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
  6660. + T(YAFFS_TRACE_ERROR,
  6661. + (TSTR("**>> Block %d is not valid" TENDSTR), b));
  6662. + YBUG();
  6663. + }
  6664. +
  6665. + bi = yaffs_GetBlockInfo(dev, b);
  6666. +
  6667. + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
  6668. + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
  6669. + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
  6670. + dirtiest = b;
  6671. + pagesInUse = (bi->pagesInUse - bi->softDeletions);
  6672. + }
  6673. + }
  6674. +
  6675. + dev->currentDirtyChecker = b;
  6676. +
  6677. + if (dirtiest > 0) {
  6678. + T(YAFFS_TRACE_GC,
  6679. + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
  6680. + dev->nChunksPerBlock - pagesInUse, prioritised));
  6681. + }
  6682. +
  6683. + dev->oldestDirtySequence = 0;
  6684. +
  6685. + if (dirtiest > 0)
  6686. + dev->nonAggressiveSkip = 4;
  6687. +
  6688. + return dirtiest;
  6689. +}
  6690. +
  6691. +static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
  6692. +{
  6693. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
  6694. +
  6695. + int erasedOk = 0;
  6696. +
  6697. + /* If the block is still healthy erase it and mark as clean.
  6698. + * If the block has had a data failure, then retire it.
  6699. + */
  6700. +
  6701. + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  6702. + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
  6703. + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
  6704. +
  6705. + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
  6706. +
  6707. + if (!bi->needsRetiring) {
  6708. + yaffs_InvalidateCheckpoint(dev);
  6709. + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
  6710. + if (!erasedOk) {
  6711. + dev->nErasureFailures++;
  6712. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  6713. + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
  6714. + }
  6715. + }
  6716. +
  6717. + if (erasedOk &&
  6718. + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
  6719. + int i;
  6720. + for (i = 0; i < dev->nChunksPerBlock; i++) {
  6721. + if (!yaffs_CheckChunkErased
  6722. + (dev, blockNo * dev->nChunksPerBlock + i)) {
  6723. + T(YAFFS_TRACE_ERROR,
  6724. + (TSTR
  6725. + (">>Block %d erasure supposedly OK, but chunk %d not erased"
  6726. + TENDSTR), blockNo, i));
  6727. + }
  6728. + }
  6729. + }
  6730. +
  6731. + if (erasedOk) {
  6732. + /* Clean it up... */
  6733. + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
  6734. + dev->nErasedBlocks++;
  6735. + bi->pagesInUse = 0;
  6736. + bi->softDeletions = 0;
  6737. + bi->hasShrinkHeader = 0;
  6738. + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
  6739. + bi->gcPrioritise = 0;
  6740. + yaffs_ClearChunkBits(dev, blockNo);
  6741. +
  6742. + T(YAFFS_TRACE_ERASE,
  6743. + (TSTR("Erased block %d" TENDSTR), blockNo));
  6744. + } else {
  6745. + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
  6746. +
  6747. + yaffs_RetireBlock(dev, blockNo);
  6748. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  6749. + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
  6750. + }
  6751. +}
  6752. +
  6753. +static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
  6754. +{
  6755. + int i;
  6756. +
  6757. + yaffs_BlockInfo *bi;
  6758. +
  6759. + if (dev->nErasedBlocks < 1) {
  6760. + /* Hoosterman we've got a problem.
  6761. + * Can't get space to gc
  6762. + */
  6763. + T(YAFFS_TRACE_ERROR,
  6764. + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
  6765. +
  6766. + return -1;
  6767. + }
  6768. +
  6769. + /* Find an empty block. */
  6770. +
  6771. + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
  6772. + dev->allocationBlockFinder++;
  6773. + if (dev->allocationBlockFinder < dev->internalStartBlock
  6774. + || dev->allocationBlockFinder > dev->internalEndBlock) {
  6775. + dev->allocationBlockFinder = dev->internalStartBlock;
  6776. + }
  6777. +
  6778. + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
  6779. +
  6780. + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
  6781. + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
  6782. + dev->sequenceNumber++;
  6783. + bi->sequenceNumber = dev->sequenceNumber;
  6784. + dev->nErasedBlocks--;
  6785. + T(YAFFS_TRACE_ALLOCATE,
  6786. + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
  6787. + dev->allocationBlockFinder, dev->sequenceNumber,
  6788. + dev->nErasedBlocks));
  6789. + return dev->allocationBlockFinder;
  6790. + }
  6791. + }
  6792. +
  6793. + T(YAFFS_TRACE_ALWAYS,
  6794. + (TSTR
  6795. + ("yaffs tragedy: no more erased blocks, but there should have been %d"
  6796. + TENDSTR), dev->nErasedBlocks));
  6797. +
  6798. + return -1;
  6799. +}
  6800. +
  6801. +
  6802. +
  6803. +static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
  6804. +{
  6805. + if (!dev->nCheckpointBlocksRequired &&
  6806. + dev->isYaffs2) {
  6807. + /* Not a valid value so recalculate */
  6808. + int nBytes = 0;
  6809. + int nBlocks;
  6810. + int devBlocks = (dev->endBlock - dev->startBlock + 1);
  6811. + int tnodeSize;
  6812. +
  6813. + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  6814. +
  6815. + if (tnodeSize < sizeof(yaffs_Tnode))
  6816. + tnodeSize = sizeof(yaffs_Tnode);
  6817. +
  6818. + nBytes += sizeof(yaffs_CheckpointValidity);
  6819. + nBytes += sizeof(yaffs_CheckpointDevice);
  6820. + nBytes += devBlocks * sizeof(yaffs_BlockInfo);
  6821. + nBytes += devBlocks * dev->chunkBitmapStride;
  6822. + nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
  6823. + nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
  6824. + nBytes += sizeof(yaffs_CheckpointValidity);
  6825. + nBytes += sizeof(__u32); /* checksum*/
  6826. +
  6827. + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
  6828. +
  6829. + nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->nChunksPerBlock)) + 3;
  6830. +
  6831. + dev->nCheckpointBlocksRequired = nBlocks;
  6832. + }
  6833. +
  6834. + return dev->nCheckpointBlocksRequired;
  6835. +}
  6836. +
  6837. +/*
  6838. + * Check if there's space to allocate...
  6839. + * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
  6840. + */
  6841. +static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
  6842. +{
  6843. + int reservedChunks;
  6844. + int reservedBlocks = dev->nReservedBlocks;
  6845. + int checkpointBlocks;
  6846. +
  6847. + if (dev->isYaffs2) {
  6848. + checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
  6849. + dev->blocksInCheckpoint;
  6850. + if (checkpointBlocks < 0)
  6851. + checkpointBlocks = 0;
  6852. + } else {
  6853. + checkpointBlocks = 0;
  6854. + }
  6855. +
  6856. + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
  6857. +
  6858. + return (dev->nFreeChunks > reservedChunks);
  6859. +}
  6860. +
  6861. +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
  6862. + yaffs_BlockInfo **blockUsedPtr)
  6863. +{
  6864. + int retVal;
  6865. + yaffs_BlockInfo *bi;
  6866. +
  6867. + if (dev->allocationBlock < 0) {
  6868. + /* Get next block to allocate off */
  6869. + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
  6870. + dev->allocationPage = 0;
  6871. + }
  6872. +
  6873. + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
  6874. + /* Not enough space to allocate unless we're allowed to use the reserve. */
  6875. + return -1;
  6876. + }
  6877. +
  6878. + if (dev->nErasedBlocks < dev->nReservedBlocks
  6879. + && dev->allocationPage == 0) {
  6880. + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
  6881. + }
  6882. +
  6883. + /* Next page please.... */
  6884. + if (dev->allocationBlock >= 0) {
  6885. + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
  6886. +
  6887. + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
  6888. + dev->allocationPage;
  6889. + bi->pagesInUse++;
  6890. + yaffs_SetChunkBit(dev, dev->allocationBlock,
  6891. + dev->allocationPage);
  6892. +
  6893. + dev->allocationPage++;
  6894. +
  6895. + dev->nFreeChunks--;
  6896. +
  6897. + /* If the block is full set the state to full */
  6898. + if (dev->allocationPage >= dev->nChunksPerBlock) {
  6899. + bi->blockState = YAFFS_BLOCK_STATE_FULL;
  6900. + dev->allocationBlock = -1;
  6901. + }
  6902. +
  6903. + if (blockUsedPtr)
  6904. + *blockUsedPtr = bi;
  6905. +
  6906. + return retVal;
  6907. + }
  6908. +
  6909. + T(YAFFS_TRACE_ERROR,
  6910. + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
  6911. +
  6912. + return -1;
  6913. +}
  6914. +
  6915. +static int yaffs_GetErasedChunks(yaffs_Device *dev)
  6916. +{
  6917. + int n;
  6918. +
  6919. + n = dev->nErasedBlocks * dev->nChunksPerBlock;
  6920. +
  6921. + if (dev->allocationBlock > 0)
  6922. + n += (dev->nChunksPerBlock - dev->allocationPage);
  6923. +
  6924. + return n;
  6925. +
  6926. +}
  6927. +
  6928. +static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
  6929. + int wholeBlock)
  6930. +{
  6931. + int oldChunk;
  6932. + int newChunk;
  6933. + int markNAND;
  6934. + int retVal = YAFFS_OK;
  6935. + int cleanups = 0;
  6936. + int i;
  6937. + int isCheckpointBlock;
  6938. + int matchingChunk;
  6939. + int maxCopies;
  6940. +
  6941. + int chunksBefore = yaffs_GetErasedChunks(dev);
  6942. + int chunksAfter;
  6943. +
  6944. + yaffs_ExtendedTags tags;
  6945. +
  6946. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
  6947. +
  6948. + yaffs_Object *object;
  6949. +
  6950. + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
  6951. +
  6952. + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
  6953. +
  6954. + T(YAFFS_TRACE_TRACING,
  6955. + (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
  6956. + block,
  6957. + bi->pagesInUse,
  6958. + bi->hasShrinkHeader,
  6959. + wholeBlock));
  6960. +
  6961. + /*yaffs_VerifyFreeChunks(dev); */
  6962. +
  6963. + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
  6964. +
  6965. + /* Take off the number of soft deleted entries because
  6966. + * they're going to get really deleted during GC.
  6967. + */
  6968. + dev->nFreeChunks -= bi->softDeletions;
  6969. +
  6970. + dev->isDoingGC = 1;
  6971. +
  6972. + if (isCheckpointBlock ||
  6973. + !yaffs_StillSomeChunkBits(dev, block)) {
  6974. + T(YAFFS_TRACE_TRACING,
  6975. + (TSTR
  6976. + ("Collecting block %d that has no chunks in use" TENDSTR),
  6977. + block));
  6978. + yaffs_BlockBecameDirty(dev, block);
  6979. + } else {
  6980. +
  6981. + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
  6982. +
  6983. + yaffs_VerifyBlock(dev, bi, block);
  6984. +
  6985. + maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10;
  6986. + oldChunk = block * dev->nChunksPerBlock + dev->gcChunk;
  6987. +
  6988. + for (/* init already done */;
  6989. + retVal == YAFFS_OK &&
  6990. + dev->gcChunk < dev->nChunksPerBlock &&
  6991. + (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
  6992. + maxCopies > 0;
  6993. + dev->gcChunk++, oldChunk++) {
  6994. + if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
  6995. +
  6996. + /* This page is in use and might need to be copied off */
  6997. +
  6998. + maxCopies--;
  6999. +
  7000. + markNAND = 1;
  7001. +
  7002. + yaffs_InitialiseTags(&tags);
  7003. +
  7004. + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
  7005. + buffer, &tags);
  7006. +
  7007. + object =
  7008. + yaffs_FindObjectByNumber(dev,
  7009. + tags.objectId);
  7010. +
  7011. + T(YAFFS_TRACE_GC_DETAIL,
  7012. + (TSTR
  7013. + ("Collecting chunk in block %d, %d %d %d " TENDSTR),
  7014. + dev->gcChunk, tags.objectId, tags.chunkId,
  7015. + tags.byteCount));
  7016. +
  7017. + if (object && !yaffs_SkipVerification(dev)) {
  7018. + if (tags.chunkId == 0)
  7019. + matchingChunk = object->hdrChunk;
  7020. + else if (object->softDeleted)
  7021. + matchingChunk = oldChunk; /* Defeat the test */
  7022. + else
  7023. + matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
  7024. +
  7025. + if (oldChunk != matchingChunk)
  7026. + T(YAFFS_TRACE_ERROR,
  7027. + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
  7028. + oldChunk, matchingChunk, tags.objectId, tags.chunkId));
  7029. +
  7030. + }
  7031. +
  7032. + if (!object) {
  7033. + T(YAFFS_TRACE_ERROR,
  7034. + (TSTR
  7035. + ("page %d in gc has no object: %d %d %d "
  7036. + TENDSTR), oldChunk,
  7037. + tags.objectId, tags.chunkId, tags.byteCount));
  7038. + }
  7039. +
  7040. + if (object &&
  7041. + object->deleted &&
  7042. + object->softDeleted &&
  7043. + tags.chunkId != 0) {
  7044. + /* Data chunk in a soft deleted file, throw it away
  7045. + * It's a soft deleted data chunk,
  7046. + * No need to copy this, just forget about it and
  7047. + * fix up the object.
  7048. + */
  7049. +
  7050. + object->nDataChunks--;
  7051. +
  7052. + if (object->nDataChunks <= 0) {
  7053. + /* remeber to clean up the object */
  7054. + dev->gcCleanupList[cleanups] =
  7055. + tags.objectId;
  7056. + cleanups++;
  7057. + }
  7058. + markNAND = 0;
  7059. + } else if (0) {
  7060. + /* Todo object && object->deleted && object->nDataChunks == 0 */
  7061. + /* Deleted object header with no data chunks.
  7062. + * Can be discarded and the file deleted.
  7063. + */
  7064. + object->hdrChunk = 0;
  7065. + yaffs_FreeTnode(object->myDev,
  7066. + object->variant.
  7067. + fileVariant.top);
  7068. + object->variant.fileVariant.top = NULL;
  7069. + yaffs_DoGenericObjectDeletion(object);
  7070. +
  7071. + } else if (object) {
  7072. + /* It's either a data chunk in a live file or
  7073. + * an ObjectHeader, so we're interested in it.
  7074. + * NB Need to keep the ObjectHeaders of deleted files
  7075. + * until the whole file has been deleted off
  7076. + */
  7077. + tags.serialNumber++;
  7078. +
  7079. + dev->nGCCopies++;
  7080. +
  7081. + if (tags.chunkId == 0) {
  7082. + /* It is an object Id,
  7083. + * We need to nuke the shrinkheader flags first
  7084. + * We no longer want the shrinkHeader flag since its work is done
  7085. + * and if it is left in place it will mess up scanning.
  7086. + */
  7087. +
  7088. + yaffs_ObjectHeader *oh;
  7089. + oh = (yaffs_ObjectHeader *)buffer;
  7090. + oh->isShrink = 0;
  7091. + tags.extraIsShrinkHeader = 0;
  7092. +
  7093. + yaffs_VerifyObjectHeader(object, oh, &tags, 1);
  7094. + }
  7095. +
  7096. + newChunk =
  7097. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
  7098. +
  7099. + if (newChunk < 0) {
  7100. + retVal = YAFFS_FAIL;
  7101. + } else {
  7102. +
  7103. + /* Ok, now fix up the Tnodes etc. */
  7104. +
  7105. + if (tags.chunkId == 0) {
  7106. + /* It's a header */
  7107. + object->hdrChunk = newChunk;
  7108. + object->serial = tags.serialNumber;
  7109. + } else {
  7110. + /* It's a data chunk */
  7111. + yaffs_PutChunkIntoFile
  7112. + (object,
  7113. + tags.chunkId,
  7114. + newChunk, 0);
  7115. + }
  7116. + }
  7117. + }
  7118. +
  7119. + if (retVal == YAFFS_OK)
  7120. + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
  7121. +
  7122. + }
  7123. + }
  7124. +
  7125. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  7126. +
  7127. +
  7128. + /* Do any required cleanups */
  7129. + for (i = 0; i < cleanups; i++) {
  7130. + /* Time to delete the file too */
  7131. + object =
  7132. + yaffs_FindObjectByNumber(dev,
  7133. + dev->gcCleanupList[i]);
  7134. + if (object) {
  7135. + yaffs_FreeTnode(dev,
  7136. + object->variant.fileVariant.
  7137. + top);
  7138. + object->variant.fileVariant.top = NULL;
  7139. + T(YAFFS_TRACE_GC,
  7140. + (TSTR
  7141. + ("yaffs: About to finally delete object %d"
  7142. + TENDSTR), object->objectId));
  7143. + yaffs_DoGenericObjectDeletion(object);
  7144. + object->myDev->nDeletedFiles--;
  7145. + }
  7146. +
  7147. + }
  7148. +
  7149. + }
  7150. +
  7151. + yaffs_VerifyCollectedBlock(dev, bi, block);
  7152. +
  7153. + chunksAfter = yaffs_GetErasedChunks(dev);
  7154. + if (chunksBefore >= chunksAfter) {
  7155. + T(YAFFS_TRACE_GC,
  7156. + (TSTR
  7157. + ("gc did not increase free chunks before %d after %d"
  7158. + TENDSTR), chunksBefore, chunksAfter));
  7159. + }
  7160. +
  7161. + /* If the gc completed then clear the current gcBlock so that we find another. */
  7162. + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
  7163. + dev->gcBlock = -1;
  7164. + dev->gcChunk = 0;
  7165. + }
  7166. +
  7167. + dev->isDoingGC = 0;
  7168. +
  7169. + return retVal;
  7170. +}
  7171. +
  7172. +/* New garbage collector
  7173. + * If we're very low on erased blocks then we do aggressive garbage collection
  7174. + * otherwise we do "leasurely" garbage collection.
  7175. + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  7176. + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  7177. + *
  7178. + * The idea is to help clear out space in a more spread-out manner.
  7179. + * Dunno if it really does anything useful.
  7180. + */
  7181. +static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
  7182. +{
  7183. + int block;
  7184. + int aggressive;
  7185. + int gcOk = YAFFS_OK;
  7186. + int maxTries = 0;
  7187. +
  7188. + int checkpointBlockAdjust;
  7189. +
  7190. + if (dev->isDoingGC) {
  7191. + /* Bail out so we don't get recursive gc */
  7192. + return YAFFS_OK;
  7193. + }
  7194. +
  7195. + /* This loop should pass the first time.
  7196. + * We'll only see looping here if the erase of the collected block fails.
  7197. + */
  7198. +
  7199. + do {
  7200. + maxTries++;
  7201. +
  7202. + checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
  7203. + if (checkpointBlockAdjust < 0)
  7204. + checkpointBlockAdjust = 0;
  7205. +
  7206. + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
  7207. + /* We need a block soon...*/
  7208. + aggressive = 1;
  7209. + } else {
  7210. + /* We're in no hurry */
  7211. + aggressive = 0;
  7212. + }
  7213. +
  7214. + if (dev->gcBlock <= 0) {
  7215. + dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive);
  7216. + dev->gcChunk = 0;
  7217. + }
  7218. +
  7219. + block = dev->gcBlock;
  7220. +
  7221. + if (block > 0) {
  7222. + dev->garbageCollections++;
  7223. + if (!aggressive)
  7224. + dev->passiveGarbageCollections++;
  7225. +
  7226. + T(YAFFS_TRACE_GC,
  7227. + (TSTR
  7228. + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
  7229. + dev->nErasedBlocks, aggressive));
  7230. +
  7231. + gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive);
  7232. + }
  7233. +
  7234. + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
  7235. + T(YAFFS_TRACE_GC,
  7236. + (TSTR
  7237. + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
  7238. + TENDSTR), dev->nErasedBlocks, maxTries, block));
  7239. + }
  7240. + } while ((dev->nErasedBlocks < dev->nReservedBlocks) &&
  7241. + (block > 0) &&
  7242. + (maxTries < 2));
  7243. +
  7244. + return aggressive ? gcOk : YAFFS_OK;
  7245. +}
  7246. +
  7247. +/*------------------------- TAGS --------------------------------*/
  7248. +
  7249. +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
  7250. + int chunkInObject)
  7251. +{
  7252. + return (tags->chunkId == chunkInObject &&
  7253. + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
  7254. +
  7255. +}
  7256. +
  7257. +
  7258. +/*-------------------- Data file manipulation -----------------*/
  7259. +
  7260. +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
  7261. + yaffs_ExtendedTags *tags)
  7262. +{
  7263. + /*Get the Tnode, then get the level 0 offset chunk offset */
  7264. + yaffs_Tnode *tn;
  7265. + int theChunk = -1;
  7266. + yaffs_ExtendedTags localTags;
  7267. + int retVal = -1;
  7268. +
  7269. + yaffs_Device *dev = in->myDev;
  7270. +
  7271. + if (!tags) {
  7272. + /* Passed a NULL, so use our own tags space */
  7273. + tags = &localTags;
  7274. + }
  7275. +
  7276. + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  7277. +
  7278. + if (tn) {
  7279. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7280. +
  7281. + retVal =
  7282. + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  7283. + chunkInInode);
  7284. + }
  7285. + return retVal;
  7286. +}
  7287. +
  7288. +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
  7289. + yaffs_ExtendedTags *tags)
  7290. +{
  7291. + /* Get the Tnode, then get the level 0 offset chunk offset */
  7292. + yaffs_Tnode *tn;
  7293. + int theChunk = -1;
  7294. + yaffs_ExtendedTags localTags;
  7295. +
  7296. + yaffs_Device *dev = in->myDev;
  7297. + int retVal = -1;
  7298. +
  7299. + if (!tags) {
  7300. + /* Passed a NULL, so use our own tags space */
  7301. + tags = &localTags;
  7302. + }
  7303. +
  7304. + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
  7305. +
  7306. + if (tn) {
  7307. +
  7308. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7309. +
  7310. + retVal =
  7311. + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
  7312. + chunkInInode);
  7313. +
  7314. + /* Delete the entry in the filestructure (if found) */
  7315. + if (retVal != -1)
  7316. + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
  7317. + }
  7318. +
  7319. + return retVal;
  7320. +}
  7321. +
  7322. +#ifdef YAFFS_PARANOID
  7323. +
  7324. +static int yaffs_CheckFileSanity(yaffs_Object *in)
  7325. +{
  7326. + int chunk;
  7327. + int nChunks;
  7328. + int fSize;
  7329. + int failed = 0;
  7330. + int objId;
  7331. + yaffs_Tnode *tn;
  7332. + yaffs_Tags localTags;
  7333. + yaffs_Tags *tags = &localTags;
  7334. + int theChunk;
  7335. + int chunkDeleted;
  7336. +
  7337. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
  7338. + return YAFFS_FAIL;
  7339. +
  7340. + objId = in->objectId;
  7341. + fSize = in->variant.fileVariant.fileSize;
  7342. + nChunks =
  7343. + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
  7344. +
  7345. + for (chunk = 1; chunk <= nChunks; chunk++) {
  7346. + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
  7347. + chunk);
  7348. +
  7349. + if (tn) {
  7350. +
  7351. + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
  7352. +
  7353. + if (yaffs_CheckChunkBits
  7354. + (dev, theChunk / dev->nChunksPerBlock,
  7355. + theChunk % dev->nChunksPerBlock)) {
  7356. +
  7357. + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
  7358. + tags,
  7359. + &chunkDeleted);
  7360. + if (yaffs_TagsMatch
  7361. + (tags, in->objectId, chunk, chunkDeleted)) {
  7362. + /* found it; */
  7363. +
  7364. + }
  7365. + } else {
  7366. +
  7367. + failed = 1;
  7368. + }
  7369. +
  7370. + } else {
  7371. + /* T(("No level 0 found for %d\n", chunk)); */
  7372. + }
  7373. + }
  7374. +
  7375. + return failed ? YAFFS_FAIL : YAFFS_OK;
  7376. +}
  7377. +
  7378. +#endif
  7379. +
  7380. +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
  7381. + int chunkInNAND, int inScan)
  7382. +{
  7383. + /* NB inScan is zero unless scanning.
  7384. + * For forward scanning, inScan is > 0;
  7385. + * for backward scanning inScan is < 0
  7386. + */
  7387. +
  7388. + yaffs_Tnode *tn;
  7389. + yaffs_Device *dev = in->myDev;
  7390. + int existingChunk;
  7391. + yaffs_ExtendedTags existingTags;
  7392. + yaffs_ExtendedTags newTags;
  7393. + unsigned existingSerial, newSerial;
  7394. +
  7395. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
  7396. + /* Just ignore an attempt at putting a chunk into a non-file during scanning
  7397. + * If it is not during Scanning then something went wrong!
  7398. + */
  7399. + if (!inScan) {
  7400. + T(YAFFS_TRACE_ERROR,
  7401. + (TSTR
  7402. + ("yaffs tragedy:attempt to put data chunk into a non-file"
  7403. + TENDSTR)));
  7404. + YBUG();
  7405. + }
  7406. +
  7407. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  7408. + return YAFFS_OK;
  7409. + }
  7410. +
  7411. + tn = yaffs_AddOrFindLevel0Tnode(dev,
  7412. + &in->variant.fileVariant,
  7413. + chunkInInode,
  7414. + NULL);
  7415. + if (!tn)
  7416. + return YAFFS_FAIL;
  7417. +
  7418. + existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
  7419. +
  7420. + if (inScan != 0) {
  7421. + /* If we're scanning then we need to test for duplicates
  7422. + * NB This does not need to be efficient since it should only ever
  7423. + * happen when the power fails during a write, then only one
  7424. + * chunk should ever be affected.
  7425. + *
  7426. + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  7427. + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  7428. + */
  7429. +
  7430. + if (existingChunk > 0) {
  7431. + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
  7432. + * thus we have to do a FindChunkInFile to get the real chunk id.
  7433. + *
  7434. + * We have a duplicate now we need to decide which one to use:
  7435. + *
  7436. + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  7437. + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  7438. + * YAFFS1: Get both sets of tags and compare serial numbers.
  7439. + */
  7440. +
  7441. + if (inScan > 0) {
  7442. + /* Only do this for forward scanning */
  7443. + yaffs_ReadChunkWithTagsFromNAND(dev,
  7444. + chunkInNAND,
  7445. + NULL, &newTags);
  7446. +
  7447. + /* Do a proper find */
  7448. + existingChunk =
  7449. + yaffs_FindChunkInFile(in, chunkInInode,
  7450. + &existingTags);
  7451. + }
  7452. +
  7453. + if (existingChunk <= 0) {
  7454. + /*Hoosterman - how did this happen? */
  7455. +
  7456. + T(YAFFS_TRACE_ERROR,
  7457. + (TSTR
  7458. + ("yaffs tragedy: existing chunk < 0 in scan"
  7459. + TENDSTR)));
  7460. +
  7461. + }
  7462. +
  7463. + /* NB The deleted flags should be false, otherwise the chunks will
  7464. + * not be loaded during a scan
  7465. + */
  7466. +
  7467. + if (inScan > 0) {
  7468. + newSerial = newTags.serialNumber;
  7469. + existingSerial = existingTags.serialNumber;
  7470. + }
  7471. +
  7472. + if ((inScan > 0) &&
  7473. + (in->myDev->isYaffs2 ||
  7474. + existingChunk <= 0 ||
  7475. + ((existingSerial + 1) & 3) == newSerial)) {
  7476. + /* Forward scanning.
  7477. + * Use new
  7478. + * Delete the old one and drop through to update the tnode
  7479. + */
  7480. + yaffs_DeleteChunk(dev, existingChunk, 1,
  7481. + __LINE__);
  7482. + } else {
  7483. + /* Backward scanning or we want to use the existing one
  7484. + * Use existing.
  7485. + * Delete the new one and return early so that the tnode isn't changed
  7486. + */
  7487. + yaffs_DeleteChunk(dev, chunkInNAND, 1,
  7488. + __LINE__);
  7489. + return YAFFS_OK;
  7490. + }
  7491. + }
  7492. +
  7493. + }
  7494. +
  7495. + if (existingChunk == 0)
  7496. + in->nDataChunks++;
  7497. +
  7498. + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
  7499. +
  7500. + return YAFFS_OK;
  7501. +}
  7502. +
  7503. +static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
  7504. + __u8 *buffer)
  7505. +{
  7506. + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
  7507. +
  7508. + if (chunkInNAND >= 0)
  7509. + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
  7510. + buffer, NULL);
  7511. + else {
  7512. + T(YAFFS_TRACE_NANDACCESS,
  7513. + (TSTR("Chunk %d not found zero instead" TENDSTR),
  7514. + chunkInNAND));
  7515. + /* get sane (zero) data if you read a hole */
  7516. + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
  7517. + return 0;
  7518. + }
  7519. +
  7520. +}
  7521. +
  7522. +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
  7523. +{
  7524. + int block;
  7525. + int page;
  7526. + yaffs_ExtendedTags tags;
  7527. + yaffs_BlockInfo *bi;
  7528. +
  7529. + if (chunkId <= 0)
  7530. + return;
  7531. +
  7532. + dev->nDeletions++;
  7533. + block = chunkId / dev->nChunksPerBlock;
  7534. + page = chunkId % dev->nChunksPerBlock;
  7535. +
  7536. +
  7537. + if (!yaffs_CheckChunkBit(dev, block, page))
  7538. + T(YAFFS_TRACE_VERIFY,
  7539. + (TSTR("Deleting invalid chunk %d"TENDSTR),
  7540. + chunkId));
  7541. +
  7542. + bi = yaffs_GetBlockInfo(dev, block);
  7543. +
  7544. + T(YAFFS_TRACE_DELETION,
  7545. + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
  7546. +
  7547. + if (markNAND &&
  7548. + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
  7549. +
  7550. + yaffs_InitialiseTags(&tags);
  7551. +
  7552. + tags.chunkDeleted = 1;
  7553. +
  7554. + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
  7555. + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
  7556. + } else {
  7557. + dev->nUnmarkedDeletions++;
  7558. + }
  7559. +
  7560. + /* Pull out of the management area.
  7561. + * If the whole block became dirty, this will kick off an erasure.
  7562. + */
  7563. + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
  7564. + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
  7565. + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  7566. + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
  7567. + dev->nFreeChunks++;
  7568. +
  7569. + yaffs_ClearChunkBit(dev, block, page);
  7570. +
  7571. + bi->pagesInUse--;
  7572. +
  7573. + if (bi->pagesInUse == 0 &&
  7574. + !bi->hasShrinkHeader &&
  7575. + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
  7576. + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  7577. + yaffs_BlockBecameDirty(dev, block);
  7578. + }
  7579. +
  7580. + }
  7581. +
  7582. +}
  7583. +
  7584. +static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
  7585. + const __u8 *buffer, int nBytes,
  7586. + int useReserve)
  7587. +{
  7588. + /* Find old chunk Need to do this to get serial number
  7589. + * Write new one and patch into tree.
  7590. + * Invalidate old tags.
  7591. + */
  7592. +
  7593. + int prevChunkId;
  7594. + yaffs_ExtendedTags prevTags;
  7595. +
  7596. + int newChunkId;
  7597. + yaffs_ExtendedTags newTags;
  7598. +
  7599. + yaffs_Device *dev = in->myDev;
  7600. +
  7601. + yaffs_CheckGarbageCollection(dev);
  7602. +
  7603. + /* Get the previous chunk at this location in the file if it exists */
  7604. + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
  7605. +
  7606. + /* Set up new tags */
  7607. + yaffs_InitialiseTags(&newTags);
  7608. +
  7609. + newTags.chunkId = chunkInInode;
  7610. + newTags.objectId = in->objectId;
  7611. + newTags.serialNumber =
  7612. + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
  7613. + newTags.byteCount = nBytes;
  7614. +
  7615. + if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) {
  7616. + T(YAFFS_TRACE_ERROR,
  7617. + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
  7618. + YBUG();
  7619. + }
  7620. +
  7621. + newChunkId =
  7622. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  7623. + useReserve);
  7624. +
  7625. + if (newChunkId >= 0) {
  7626. + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
  7627. +
  7628. + if (prevChunkId >= 0)
  7629. + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
  7630. +
  7631. + yaffs_CheckFileSanity(in);
  7632. + }
  7633. + return newChunkId;
  7634. +
  7635. +}
  7636. +
  7637. +/* UpdateObjectHeader updates the header on NAND for an object.
  7638. + * If name is not NULL, then that new name is used.
  7639. + */
  7640. +int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
  7641. + int isShrink, int shadows)
  7642. +{
  7643. +
  7644. + yaffs_BlockInfo *bi;
  7645. +
  7646. + yaffs_Device *dev = in->myDev;
  7647. +
  7648. + int prevChunkId;
  7649. + int retVal = 0;
  7650. + int result = 0;
  7651. +
  7652. + int newChunkId;
  7653. + yaffs_ExtendedTags newTags;
  7654. + yaffs_ExtendedTags oldTags;
  7655. +
  7656. + __u8 *buffer = NULL;
  7657. + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
  7658. +
  7659. + yaffs_ObjectHeader *oh = NULL;
  7660. +
  7661. + yaffs_strcpy(oldName, _Y("silly old name"));
  7662. +
  7663. +
  7664. + if (!in->fake ||
  7665. + in == dev->rootDir || /* The rootDir should also be saved */
  7666. + force) {
  7667. +
  7668. + yaffs_CheckGarbageCollection(dev);
  7669. + yaffs_CheckObjectDetailsLoaded(in);
  7670. +
  7671. + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
  7672. + oh = (yaffs_ObjectHeader *) buffer;
  7673. +
  7674. + prevChunkId = in->hdrChunk;
  7675. +
  7676. + if (prevChunkId > 0) {
  7677. + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
  7678. + buffer, &oldTags);
  7679. +
  7680. + yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
  7681. +
  7682. + memcpy(oldName, oh->name, sizeof(oh->name));
  7683. + }
  7684. +
  7685. + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
  7686. +
  7687. + oh->type = in->variantType;
  7688. + oh->yst_mode = in->yst_mode;
  7689. + oh->shadowsObject = oh->inbandShadowsObject = shadows;
  7690. +
  7691. +#ifdef CONFIG_YAFFS_WINCE
  7692. + oh->win_atime[0] = in->win_atime[0];
  7693. + oh->win_ctime[0] = in->win_ctime[0];
  7694. + oh->win_mtime[0] = in->win_mtime[0];
  7695. + oh->win_atime[1] = in->win_atime[1];
  7696. + oh->win_ctime[1] = in->win_ctime[1];
  7697. + oh->win_mtime[1] = in->win_mtime[1];
  7698. +#else
  7699. + oh->yst_uid = in->yst_uid;
  7700. + oh->yst_gid = in->yst_gid;
  7701. + oh->yst_atime = in->yst_atime;
  7702. + oh->yst_mtime = in->yst_mtime;
  7703. + oh->yst_ctime = in->yst_ctime;
  7704. + oh->yst_rdev = in->yst_rdev;
  7705. +#endif
  7706. + if (in->parent)
  7707. + oh->parentObjectId = in->parent->objectId;
  7708. + else
  7709. + oh->parentObjectId = 0;
  7710. +
  7711. + if (name && *name) {
  7712. + memset(oh->name, 0, sizeof(oh->name));
  7713. + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
  7714. + } else if (prevChunkId >= 0)
  7715. + memcpy(oh->name, oldName, sizeof(oh->name));
  7716. + else
  7717. + memset(oh->name, 0, sizeof(oh->name));
  7718. +
  7719. + oh->isShrink = isShrink;
  7720. +
  7721. + switch (in->variantType) {
  7722. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  7723. + /* Should not happen */
  7724. + break;
  7725. + case YAFFS_OBJECT_TYPE_FILE:
  7726. + oh->fileSize =
  7727. + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
  7728. + || oh->parentObjectId ==
  7729. + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
  7730. + fileVariant.fileSize;
  7731. + break;
  7732. + case YAFFS_OBJECT_TYPE_HARDLINK:
  7733. + oh->equivalentObjectId =
  7734. + in->variant.hardLinkVariant.equivalentObjectId;
  7735. + break;
  7736. + case YAFFS_OBJECT_TYPE_SPECIAL:
  7737. + /* Do nothing */
  7738. + break;
  7739. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  7740. + /* Do nothing */
  7741. + break;
  7742. + case YAFFS_OBJECT_TYPE_SYMLINK:
  7743. + yaffs_strncpy(oh->alias,
  7744. + in->variant.symLinkVariant.alias,
  7745. + YAFFS_MAX_ALIAS_LENGTH);
  7746. + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  7747. + break;
  7748. + }
  7749. +
  7750. + /* Tags */
  7751. + yaffs_InitialiseTags(&newTags);
  7752. + in->serial++;
  7753. + newTags.chunkId = 0;
  7754. + newTags.objectId = in->objectId;
  7755. + newTags.serialNumber = in->serial;
  7756. +
  7757. + /* Add extra info for file header */
  7758. +
  7759. + newTags.extraHeaderInfoAvailable = 1;
  7760. + newTags.extraParentObjectId = oh->parentObjectId;
  7761. + newTags.extraFileLength = oh->fileSize;
  7762. + newTags.extraIsShrinkHeader = oh->isShrink;
  7763. + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
  7764. + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
  7765. + newTags.extraObjectType = in->variantType;
  7766. +
  7767. + yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
  7768. +
  7769. + /* Create new chunk in NAND */
  7770. + newChunkId =
  7771. + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
  7772. + (prevChunkId >= 0) ? 1 : 0);
  7773. +
  7774. + if (newChunkId >= 0) {
  7775. +
  7776. + in->hdrChunk = newChunkId;
  7777. +
  7778. + if (prevChunkId >= 0) {
  7779. + yaffs_DeleteChunk(dev, prevChunkId, 1,
  7780. + __LINE__);
  7781. + }
  7782. +
  7783. + if (!yaffs_ObjectHasCachedWriteData(in))
  7784. + in->dirty = 0;
  7785. +
  7786. + /* If this was a shrink, then mark the block that the chunk lives on */
  7787. + if (isShrink) {
  7788. + bi = yaffs_GetBlockInfo(in->myDev,
  7789. + newChunkId / in->myDev->nChunksPerBlock);
  7790. + bi->hasShrinkHeader = 1;
  7791. + }
  7792. +
  7793. + }
  7794. +
  7795. + retVal = newChunkId;
  7796. +
  7797. + }
  7798. +
  7799. + if (buffer)
  7800. + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
  7801. +
  7802. + return retVal;
  7803. +}
  7804. +
  7805. +/*------------------------ Short Operations Cache ----------------------------------------
  7806. + * In many situations where there is no high level buffering (eg WinCE) a lot of
  7807. + * reads might be short sequential reads, and a lot of writes may be short
  7808. + * sequential writes. eg. scanning/writing a jpeg file.
  7809. + * In these cases, a short read/write cache can provide a huge perfomance benefit
  7810. + * with dumb-as-a-rock code.
  7811. + * In Linux, the page cache provides read buffering aand the short op cache provides write
  7812. + * buffering.
  7813. + *
  7814. + * There are a limited number (~10) of cache chunks per device so that we don't
  7815. + * need a very intelligent search.
  7816. + */
  7817. +
  7818. +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
  7819. +{
  7820. + yaffs_Device *dev = obj->myDev;
  7821. + int i;
  7822. + yaffs_ChunkCache *cache;
  7823. + int nCaches = obj->myDev->nShortOpCaches;
  7824. +
  7825. + for (i = 0; i < nCaches; i++) {
  7826. + cache = &dev->srCache[i];
  7827. + if (cache->object == obj &&
  7828. + cache->dirty)
  7829. + return 1;
  7830. + }
  7831. +
  7832. + return 0;
  7833. +}
  7834. +
  7835. +
  7836. +static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
  7837. +{
  7838. + yaffs_Device *dev = obj->myDev;
  7839. + int lowest = -99; /* Stop compiler whining. */
  7840. + int i;
  7841. + yaffs_ChunkCache *cache;
  7842. + int chunkWritten = 0;
  7843. + int nCaches = obj->myDev->nShortOpCaches;
  7844. +
  7845. + if (nCaches > 0) {
  7846. + do {
  7847. + cache = NULL;
  7848. +
  7849. + /* Find the dirty cache for this object with the lowest chunk id. */
  7850. + for (i = 0; i < nCaches; i++) {
  7851. + if (dev->srCache[i].object == obj &&
  7852. + dev->srCache[i].dirty) {
  7853. + if (!cache
  7854. + || dev->srCache[i].chunkId <
  7855. + lowest) {
  7856. + cache = &dev->srCache[i];
  7857. + lowest = cache->chunkId;
  7858. + }
  7859. + }
  7860. + }
  7861. +
  7862. + if (cache && !cache->locked) {
  7863. + /* Write it out and free it up */
  7864. +
  7865. + chunkWritten =
  7866. + yaffs_WriteChunkDataToObject(cache->object,
  7867. + cache->chunkId,
  7868. + cache->data,
  7869. + cache->nBytes,
  7870. + 1);
  7871. + cache->dirty = 0;
  7872. + cache->object = NULL;
  7873. + }
  7874. +
  7875. + } while (cache && chunkWritten > 0);
  7876. +
  7877. + if (cache) {
  7878. + /* Hoosterman, disk full while writing cache out. */
  7879. + T(YAFFS_TRACE_ERROR,
  7880. + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
  7881. +
  7882. + }
  7883. + }
  7884. +
  7885. +}
  7886. +
  7887. +/*yaffs_FlushEntireDeviceCache(dev)
  7888. + *
  7889. + *
  7890. + */
  7891. +
  7892. +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
  7893. +{
  7894. + yaffs_Object *obj;
  7895. + int nCaches = dev->nShortOpCaches;
  7896. + int i;
  7897. +
  7898. + /* Find a dirty object in the cache and flush it...
  7899. + * until there are no further dirty objects.
  7900. + */
  7901. + do {
  7902. + obj = NULL;
  7903. + for (i = 0; i < nCaches && !obj; i++) {
  7904. + if (dev->srCache[i].object &&
  7905. + dev->srCache[i].dirty)
  7906. + obj = dev->srCache[i].object;
  7907. +
  7908. + }
  7909. + if (obj)
  7910. + yaffs_FlushFilesChunkCache(obj);
  7911. +
  7912. + } while (obj);
  7913. +
  7914. +}
  7915. +
  7916. +
  7917. +/* Grab us a cache chunk for use.
  7918. + * First look for an empty one.
  7919. + * Then look for the least recently used non-dirty one.
  7920. + * Then look for the least recently used dirty one...., flush and look again.
  7921. + */
  7922. +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
  7923. +{
  7924. + int i;
  7925. +
  7926. + if (dev->nShortOpCaches > 0) {
  7927. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7928. + if (!dev->srCache[i].object)
  7929. + return &dev->srCache[i];
  7930. + }
  7931. + }
  7932. +
  7933. + return NULL;
  7934. +}
  7935. +
  7936. +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
  7937. +{
  7938. + yaffs_ChunkCache *cache;
  7939. + yaffs_Object *theObj;
  7940. + int usage;
  7941. + int i;
  7942. + int pushout;
  7943. +
  7944. + if (dev->nShortOpCaches > 0) {
  7945. + /* Try find a non-dirty one... */
  7946. +
  7947. + cache = yaffs_GrabChunkCacheWorker(dev);
  7948. +
  7949. + if (!cache) {
  7950. + /* They were all dirty, find the last recently used object and flush
  7951. + * its cache, then find again.
  7952. + * NB what's here is not very accurate, we actually flush the object
  7953. + * the last recently used page.
  7954. + */
  7955. +
  7956. + /* With locking we can't assume we can use entry zero */
  7957. +
  7958. + theObj = NULL;
  7959. + usage = -1;
  7960. + cache = NULL;
  7961. + pushout = -1;
  7962. +
  7963. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7964. + if (dev->srCache[i].object &&
  7965. + !dev->srCache[i].locked &&
  7966. + (dev->srCache[i].lastUse < usage || !cache)) {
  7967. + usage = dev->srCache[i].lastUse;
  7968. + theObj = dev->srCache[i].object;
  7969. + cache = &dev->srCache[i];
  7970. + pushout = i;
  7971. + }
  7972. + }
  7973. +
  7974. + if (!cache || cache->dirty) {
  7975. + /* Flush and try again */
  7976. + yaffs_FlushFilesChunkCache(theObj);
  7977. + cache = yaffs_GrabChunkCacheWorker(dev);
  7978. + }
  7979. +
  7980. + }
  7981. + return cache;
  7982. + } else
  7983. + return NULL;
  7984. +
  7985. +}
  7986. +
  7987. +/* Find a cached chunk */
  7988. +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
  7989. + int chunkId)
  7990. +{
  7991. + yaffs_Device *dev = obj->myDev;
  7992. + int i;
  7993. + if (dev->nShortOpCaches > 0) {
  7994. + for (i = 0; i < dev->nShortOpCaches; i++) {
  7995. + if (dev->srCache[i].object == obj &&
  7996. + dev->srCache[i].chunkId == chunkId) {
  7997. + dev->cacheHits++;
  7998. +
  7999. + return &dev->srCache[i];
  8000. + }
  8001. + }
  8002. + }
  8003. + return NULL;
  8004. +}
  8005. +
  8006. +/* Mark the chunk for the least recently used algorithym */
  8007. +static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
  8008. + int isAWrite)
  8009. +{
  8010. +
  8011. + if (dev->nShortOpCaches > 0) {
  8012. + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
  8013. + /* Reset the cache usages */
  8014. + int i;
  8015. + for (i = 1; i < dev->nShortOpCaches; i++)
  8016. + dev->srCache[i].lastUse = 0;
  8017. +
  8018. + dev->srLastUse = 0;
  8019. + }
  8020. +
  8021. + dev->srLastUse++;
  8022. +
  8023. + cache->lastUse = dev->srLastUse;
  8024. +
  8025. + if (isAWrite)
  8026. + cache->dirty = 1;
  8027. + }
  8028. +}
  8029. +
  8030. +/* Invalidate a single cache page.
  8031. + * Do this when a whole page gets written,
  8032. + * ie the short cache for this page is no longer valid.
  8033. + */
  8034. +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
  8035. +{
  8036. + if (object->myDev->nShortOpCaches > 0) {
  8037. + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
  8038. +
  8039. + if (cache)
  8040. + cache->object = NULL;
  8041. + }
  8042. +}
  8043. +
  8044. +/* Invalidate all the cache pages associated with this object
  8045. + * Do this whenever ther file is deleted or resized.
  8046. + */
  8047. +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
  8048. +{
  8049. + int i;
  8050. + yaffs_Device *dev = in->myDev;
  8051. +
  8052. + if (dev->nShortOpCaches > 0) {
  8053. + /* Invalidate it. */
  8054. + for (i = 0; i < dev->nShortOpCaches; i++) {
  8055. + if (dev->srCache[i].object == in)
  8056. + dev->srCache[i].object = NULL;
  8057. + }
  8058. + }
  8059. +}
  8060. +
  8061. +/*--------------------- Checkpointing --------------------*/
  8062. +
  8063. +
  8064. +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
  8065. +{
  8066. + yaffs_CheckpointValidity cp;
  8067. +
  8068. + memset(&cp, 0, sizeof(cp));
  8069. +
  8070. + cp.structType = sizeof(cp);
  8071. + cp.magic = YAFFS_MAGIC;
  8072. + cp.version = YAFFS_CHECKPOINT_VERSION;
  8073. + cp.head = (head) ? 1 : 0;
  8074. +
  8075. + return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
  8076. + 1 : 0;
  8077. +}
  8078. +
  8079. +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
  8080. +{
  8081. + yaffs_CheckpointValidity cp;
  8082. + int ok;
  8083. +
  8084. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8085. +
  8086. + if (ok)
  8087. + ok = (cp.structType == sizeof(cp)) &&
  8088. + (cp.magic == YAFFS_MAGIC) &&
  8089. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  8090. + (cp.head == ((head) ? 1 : 0));
  8091. + return ok ? 1 : 0;
  8092. +}
  8093. +
  8094. +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
  8095. + yaffs_Device *dev)
  8096. +{
  8097. + cp->nErasedBlocks = dev->nErasedBlocks;
  8098. + cp->allocationBlock = dev->allocationBlock;
  8099. + cp->allocationPage = dev->allocationPage;
  8100. + cp->nFreeChunks = dev->nFreeChunks;
  8101. +
  8102. + cp->nDeletedFiles = dev->nDeletedFiles;
  8103. + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
  8104. + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
  8105. + cp->sequenceNumber = dev->sequenceNumber;
  8106. + cp->oldestDirtySequence = dev->oldestDirtySequence;
  8107. +
  8108. +}
  8109. +
  8110. +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
  8111. + yaffs_CheckpointDevice *cp)
  8112. +{
  8113. + dev->nErasedBlocks = cp->nErasedBlocks;
  8114. + dev->allocationBlock = cp->allocationBlock;
  8115. + dev->allocationPage = cp->allocationPage;
  8116. + dev->nFreeChunks = cp->nFreeChunks;
  8117. +
  8118. + dev->nDeletedFiles = cp->nDeletedFiles;
  8119. + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
  8120. + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
  8121. + dev->sequenceNumber = cp->sequenceNumber;
  8122. + dev->oldestDirtySequence = cp->oldestDirtySequence;
  8123. +}
  8124. +
  8125. +
  8126. +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
  8127. +{
  8128. + yaffs_CheckpointDevice cp;
  8129. + __u32 nBytes;
  8130. + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  8131. +
  8132. + int ok;
  8133. +
  8134. + /* Write device runtime values*/
  8135. + yaffs_DeviceToCheckpointDevice(&cp, dev);
  8136. + cp.structType = sizeof(cp);
  8137. +
  8138. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8139. +
  8140. + /* Write block info */
  8141. + if (ok) {
  8142. + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  8143. + ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
  8144. + }
  8145. +
  8146. + /* Write chunk bits */
  8147. + if (ok) {
  8148. + nBytes = nBlocks * dev->chunkBitmapStride;
  8149. + ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
  8150. + }
  8151. + return ok ? 1 : 0;
  8152. +
  8153. +}
  8154. +
  8155. +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
  8156. +{
  8157. + yaffs_CheckpointDevice cp;
  8158. + __u32 nBytes;
  8159. + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
  8160. +
  8161. + int ok;
  8162. +
  8163. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8164. + if (!ok)
  8165. + return 0;
  8166. +
  8167. + if (cp.structType != sizeof(cp))
  8168. + return 0;
  8169. +
  8170. +
  8171. + yaffs_CheckpointDeviceToDevice(dev, &cp);
  8172. +
  8173. + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
  8174. +
  8175. + ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
  8176. +
  8177. + if (!ok)
  8178. + return 0;
  8179. + nBytes = nBlocks * dev->chunkBitmapStride;
  8180. +
  8181. + ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
  8182. +
  8183. + return ok ? 1 : 0;
  8184. +}
  8185. +
  8186. +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
  8187. + yaffs_Object *obj)
  8188. +{
  8189. +
  8190. + cp->objectId = obj->objectId;
  8191. + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
  8192. + cp->hdrChunk = obj->hdrChunk;
  8193. + cp->variantType = obj->variantType;
  8194. + cp->deleted = obj->deleted;
  8195. + cp->softDeleted = obj->softDeleted;
  8196. + cp->unlinked = obj->unlinked;
  8197. + cp->fake = obj->fake;
  8198. + cp->renameAllowed = obj->renameAllowed;
  8199. + cp->unlinkAllowed = obj->unlinkAllowed;
  8200. + cp->serial = obj->serial;
  8201. + cp->nDataChunks = obj->nDataChunks;
  8202. +
  8203. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8204. + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
  8205. + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  8206. + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
  8207. +}
  8208. +
  8209. +static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
  8210. +{
  8211. +
  8212. + yaffs_Object *parent;
  8213. +
  8214. + if (obj->variantType != cp->variantType) {
  8215. + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
  8216. + TCONT("chunk %d does not match existing object type %d")
  8217. + TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
  8218. + obj->variantType));
  8219. + return 0;
  8220. + }
  8221. +
  8222. + obj->objectId = cp->objectId;
  8223. +
  8224. + if (cp->parentId)
  8225. + parent = yaffs_FindOrCreateObjectByNumber(
  8226. + obj->myDev,
  8227. + cp->parentId,
  8228. + YAFFS_OBJECT_TYPE_DIRECTORY);
  8229. + else
  8230. + parent = NULL;
  8231. +
  8232. + if (parent) {
  8233. + if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  8234. + T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
  8235. + TCONT(" chunk %d Parent type, %d, not directory")
  8236. + TENDSTR),
  8237. + cp->objectId, cp->parentId, cp->variantType,
  8238. + cp->hdrChunk, parent->variantType));
  8239. + return 0;
  8240. + }
  8241. + yaffs_AddObjectToDirectory(parent, obj);
  8242. + }
  8243. +
  8244. + obj->hdrChunk = cp->hdrChunk;
  8245. + obj->variantType = cp->variantType;
  8246. + obj->deleted = cp->deleted;
  8247. + obj->softDeleted = cp->softDeleted;
  8248. + obj->unlinked = cp->unlinked;
  8249. + obj->fake = cp->fake;
  8250. + obj->renameAllowed = cp->renameAllowed;
  8251. + obj->unlinkAllowed = cp->unlinkAllowed;
  8252. + obj->serial = cp->serial;
  8253. + obj->nDataChunks = cp->nDataChunks;
  8254. +
  8255. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8256. + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
  8257. + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
  8258. + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
  8259. +
  8260. + if (obj->hdrChunk > 0)
  8261. + obj->lazyLoaded = 1;
  8262. + return 1;
  8263. +}
  8264. +
  8265. +
  8266. +
  8267. +static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
  8268. + __u32 level, int chunkOffset)
  8269. +{
  8270. + int i;
  8271. + yaffs_Device *dev = in->myDev;
  8272. + int ok = 1;
  8273. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  8274. +
  8275. + if (tnodeSize < sizeof(yaffs_Tnode))
  8276. + tnodeSize = sizeof(yaffs_Tnode);
  8277. +
  8278. +
  8279. + if (tn) {
  8280. + if (level > 0) {
  8281. +
  8282. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  8283. + if (tn->internal[i]) {
  8284. + ok = yaffs_CheckpointTnodeWorker(in,
  8285. + tn->internal[i],
  8286. + level - 1,
  8287. + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
  8288. + }
  8289. + }
  8290. + } else if (level == 0) {
  8291. + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
  8292. + ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
  8293. + if (ok)
  8294. + ok = (yaffs_CheckpointWrite(dev, tn, tnodeSize) == tnodeSize);
  8295. + }
  8296. + }
  8297. +
  8298. + return ok;
  8299. +
  8300. +}
  8301. +
  8302. +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
  8303. +{
  8304. + __u32 endMarker = ~0;
  8305. + int ok = 1;
  8306. +
  8307. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  8308. + ok = yaffs_CheckpointTnodeWorker(obj,
  8309. + obj->variant.fileVariant.top,
  8310. + obj->variant.fileVariant.topLevel,
  8311. + 0);
  8312. + if (ok)
  8313. + ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
  8314. + sizeof(endMarker));
  8315. + }
  8316. +
  8317. + return ok ? 1 : 0;
  8318. +}
  8319. +
  8320. +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
  8321. +{
  8322. + __u32 baseChunk;
  8323. + int ok = 1;
  8324. + yaffs_Device *dev = obj->myDev;
  8325. + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
  8326. + yaffs_Tnode *tn;
  8327. + int nread = 0;
  8328. + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
  8329. +
  8330. + if (tnodeSize < sizeof(yaffs_Tnode))
  8331. + tnodeSize = sizeof(yaffs_Tnode);
  8332. +
  8333. + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
  8334. +
  8335. + while (ok && (~baseChunk)) {
  8336. + nread++;
  8337. + /* Read level 0 tnode */
  8338. +
  8339. +
  8340. + tn = yaffs_GetTnodeRaw(dev);
  8341. + if (tn)
  8342. + ok = (yaffs_CheckpointRead(dev, tn, tnodeSize) == tnodeSize);
  8343. + else
  8344. + ok = 0;
  8345. +
  8346. + if (tn && ok)
  8347. + ok = yaffs_AddOrFindLevel0Tnode(dev,
  8348. + fileStructPtr,
  8349. + baseChunk,
  8350. + tn) ? 1 : 0;
  8351. +
  8352. + if (ok)
  8353. + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
  8354. +
  8355. + }
  8356. +
  8357. + T(YAFFS_TRACE_CHECKPOINT, (
  8358. + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
  8359. + nread, baseChunk, ok));
  8360. +
  8361. + return ok ? 1 : 0;
  8362. +}
  8363. +
  8364. +
  8365. +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
  8366. +{
  8367. + yaffs_Object *obj;
  8368. + yaffs_CheckpointObject cp;
  8369. + int i;
  8370. + int ok = 1;
  8371. + struct ylist_head *lh;
  8372. +
  8373. +
  8374. + /* Iterate through the objects in each hash entry,
  8375. + * dumping them to the checkpointing stream.
  8376. + */
  8377. +
  8378. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  8379. + ylist_for_each(lh, &dev->objectBucket[i].list) {
  8380. + if (lh) {
  8381. + obj = ylist_entry(lh, yaffs_Object, hashLink);
  8382. + if (!obj->deferedFree) {
  8383. + yaffs_ObjectToCheckpointObject(&cp, obj);
  8384. + cp.structType = sizeof(cp);
  8385. +
  8386. + T(YAFFS_TRACE_CHECKPOINT, (
  8387. + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
  8388. + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, (unsigned) obj));
  8389. +
  8390. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8391. +
  8392. + if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  8393. + ok = yaffs_WriteCheckpointTnodes(obj);
  8394. + }
  8395. + }
  8396. + }
  8397. + }
  8398. +
  8399. + /* Dump end of list */
  8400. + memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
  8401. + cp.structType = sizeof(cp);
  8402. +
  8403. + if (ok)
  8404. + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
  8405. +
  8406. + return ok ? 1 : 0;
  8407. +}
  8408. +
  8409. +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
  8410. +{
  8411. + yaffs_Object *obj;
  8412. + yaffs_CheckpointObject cp;
  8413. + int ok = 1;
  8414. + int done = 0;
  8415. + yaffs_Object *hardList = NULL;
  8416. +
  8417. + while (ok && !done) {
  8418. + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
  8419. + if (cp.structType != sizeof(cp)) {
  8420. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
  8421. + cp.structType, sizeof(cp), ok));
  8422. + ok = 0;
  8423. + }
  8424. +
  8425. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
  8426. + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
  8427. +
  8428. + if (ok && cp.objectId == ~0)
  8429. + done = 1;
  8430. + else if (ok) {
  8431. + obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
  8432. + if (obj) {
  8433. + ok = yaffs_CheckpointObjectToObject(obj, &cp);
  8434. + if (!ok)
  8435. + break;
  8436. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
  8437. + ok = yaffs_ReadCheckpointTnodes(obj);
  8438. + } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  8439. + obj->hardLinks.next =
  8440. + (struct ylist_head *) hardList;
  8441. + hardList = obj;
  8442. + }
  8443. + } else
  8444. + ok = 0;
  8445. + }
  8446. + }
  8447. +
  8448. + if (ok)
  8449. + yaffs_HardlinkFixup(dev, hardList);
  8450. +
  8451. + return ok ? 1 : 0;
  8452. +}
  8453. +
  8454. +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
  8455. +{
  8456. + __u32 checkpointSum;
  8457. + int ok;
  8458. +
  8459. + yaffs_GetCheckpointSum(dev, &checkpointSum);
  8460. +
  8461. + ok = (yaffs_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
  8462. +
  8463. + if (!ok)
  8464. + return 0;
  8465. +
  8466. + return 1;
  8467. +}
  8468. +
  8469. +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
  8470. +{
  8471. + __u32 checkpointSum0;
  8472. + __u32 checkpointSum1;
  8473. + int ok;
  8474. +
  8475. + yaffs_GetCheckpointSum(dev, &checkpointSum0);
  8476. +
  8477. + ok = (yaffs_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
  8478. +
  8479. + if (!ok)
  8480. + return 0;
  8481. +
  8482. + if (checkpointSum0 != checkpointSum1)
  8483. + return 0;
  8484. +
  8485. + return 1;
  8486. +}
  8487. +
  8488. +
  8489. +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
  8490. +{
  8491. + int ok = 1;
  8492. +
  8493. + if (dev->skipCheckpointWrite || !dev->isYaffs2) {
  8494. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
  8495. + ok = 0;
  8496. + }
  8497. +
  8498. + if (ok)
  8499. + ok = yaffs_CheckpointOpen(dev, 1);
  8500. +
  8501. + if (ok) {
  8502. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
  8503. + ok = yaffs_WriteCheckpointValidityMarker(dev, 1);
  8504. + }
  8505. + if (ok) {
  8506. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
  8507. + ok = yaffs_WriteCheckpointDevice(dev);
  8508. + }
  8509. + if (ok) {
  8510. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
  8511. + ok = yaffs_WriteCheckpointObjects(dev);
  8512. + }
  8513. + if (ok) {
  8514. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
  8515. + ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
  8516. + }
  8517. +
  8518. + if (ok)
  8519. + ok = yaffs_WriteCheckpointSum(dev);
  8520. +
  8521. + if (!yaffs_CheckpointClose(dev))
  8522. + ok = 0;
  8523. +
  8524. + if (ok)
  8525. + dev->isCheckpointed = 1;
  8526. + else
  8527. + dev->isCheckpointed = 0;
  8528. +
  8529. + return dev->isCheckpointed;
  8530. +}
  8531. +
  8532. +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
  8533. +{
  8534. + int ok = 1;
  8535. +
  8536. + if (dev->skipCheckpointRead || !dev->isYaffs2) {
  8537. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
  8538. + ok = 0;
  8539. + }
  8540. +
  8541. + if (ok)
  8542. + ok = yaffs_CheckpointOpen(dev, 0); /* open for read */
  8543. +
  8544. + if (ok) {
  8545. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
  8546. + ok = yaffs_ReadCheckpointValidityMarker(dev, 1);
  8547. + }
  8548. + if (ok) {
  8549. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
  8550. + ok = yaffs_ReadCheckpointDevice(dev);
  8551. + }
  8552. + if (ok) {
  8553. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
  8554. + ok = yaffs_ReadCheckpointObjects(dev);
  8555. + }
  8556. + if (ok) {
  8557. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
  8558. + ok = yaffs_ReadCheckpointValidityMarker(dev, 0);
  8559. + }
  8560. +
  8561. + if (ok) {
  8562. + ok = yaffs_ReadCheckpointSum(dev);
  8563. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
  8564. + }
  8565. +
  8566. + if (!yaffs_CheckpointClose(dev))
  8567. + ok = 0;
  8568. +
  8569. + if (ok)
  8570. + dev->isCheckpointed = 1;
  8571. + else
  8572. + dev->isCheckpointed = 0;
  8573. +
  8574. + return ok ? 1 : 0;
  8575. +
  8576. +}
  8577. +
  8578. +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
  8579. +{
  8580. + if (dev->isCheckpointed ||
  8581. + dev->blocksInCheckpoint > 0) {
  8582. + dev->isCheckpointed = 0;
  8583. + yaffs_CheckpointInvalidateStream(dev);
  8584. + if (dev->superBlock && dev->markSuperBlockDirty)
  8585. + dev->markSuperBlockDirty(dev->superBlock);
  8586. + }
  8587. +}
  8588. +
  8589. +
  8590. +int yaffs_CheckpointSave(yaffs_Device *dev)
  8591. +{
  8592. +
  8593. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8594. +
  8595. + yaffs_VerifyObjects(dev);
  8596. + yaffs_VerifyBlocks(dev);
  8597. + yaffs_VerifyFreeChunks(dev);
  8598. +
  8599. + if (!dev->isCheckpointed) {
  8600. + yaffs_InvalidateCheckpoint(dev);
  8601. + yaffs_WriteCheckpointData(dev);
  8602. + }
  8603. +
  8604. + T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8605. +
  8606. + return dev->isCheckpointed;
  8607. +}
  8608. +
  8609. +int yaffs_CheckpointRestore(yaffs_Device *dev)
  8610. +{
  8611. + int retval;
  8612. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8613. +
  8614. + retval = yaffs_ReadCheckpointData(dev);
  8615. +
  8616. + if (dev->isCheckpointed) {
  8617. + yaffs_VerifyObjects(dev);
  8618. + yaffs_VerifyBlocks(dev);
  8619. + yaffs_VerifyFreeChunks(dev);
  8620. + }
  8621. +
  8622. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
  8623. +
  8624. + return retval;
  8625. +}
  8626. +
  8627. +/*--------------------- File read/write ------------------------
  8628. + * Read and write have very similar structures.
  8629. + * In general the read/write has three parts to it
  8630. + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  8631. + * Some complete chunks
  8632. + * An incomplete chunk to end off with
  8633. + *
  8634. + * Curve-balls: the first chunk might also be the last chunk.
  8635. + */
  8636. +
  8637. +int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
  8638. + int nBytes)
  8639. +{
  8640. +
  8641. + int chunk;
  8642. + __u32 start;
  8643. + int nToCopy;
  8644. + int n = nBytes;
  8645. + int nDone = 0;
  8646. + yaffs_ChunkCache *cache;
  8647. +
  8648. + yaffs_Device *dev;
  8649. +
  8650. + dev = in->myDev;
  8651. +
  8652. + while (n > 0) {
  8653. + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
  8654. + /* start = offset % dev->nDataBytesPerChunk; */
  8655. + yaffs_AddrToChunk(dev, offset, &chunk, &start);
  8656. + chunk++;
  8657. +
  8658. + /* OK now check for the curveball where the start and end are in
  8659. + * the same chunk.
  8660. + */
  8661. + if ((start + n) < dev->nDataBytesPerChunk)
  8662. + nToCopy = n;
  8663. + else
  8664. + nToCopy = dev->nDataBytesPerChunk - start;
  8665. +
  8666. + cache = yaffs_FindChunkCache(in, chunk);
  8667. +
  8668. + /* If the chunk is already in the cache or it is less than a whole chunk
  8669. + * or we're using inband tags then use the cache (if there is caching)
  8670. + * else bypass the cache.
  8671. + */
  8672. + if (cache || nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
  8673. + if (dev->nShortOpCaches > 0) {
  8674. +
  8675. + /* If we can't find the data in the cache, then load it up. */
  8676. +
  8677. + if (!cache) {
  8678. + cache = yaffs_GrabChunkCache(in->myDev);
  8679. + cache->object = in;
  8680. + cache->chunkId = chunk;
  8681. + cache->dirty = 0;
  8682. + cache->locked = 0;
  8683. + yaffs_ReadChunkDataFromObject(in, chunk,
  8684. + cache->
  8685. + data);
  8686. + cache->nBytes = 0;
  8687. + }
  8688. +
  8689. + yaffs_UseChunkCache(dev, cache, 0);
  8690. +
  8691. + cache->locked = 1;
  8692. +
  8693. +
  8694. + memcpy(buffer, &cache->data[start], nToCopy);
  8695. +
  8696. + cache->locked = 0;
  8697. + } else {
  8698. + /* Read into the local buffer then copy..*/
  8699. +
  8700. + __u8 *localBuffer =
  8701. + yaffs_GetTempBuffer(dev, __LINE__);
  8702. + yaffs_ReadChunkDataFromObject(in, chunk,
  8703. + localBuffer);
  8704. +
  8705. + memcpy(buffer, &localBuffer[start], nToCopy);
  8706. +
  8707. +
  8708. + yaffs_ReleaseTempBuffer(dev, localBuffer,
  8709. + __LINE__);
  8710. + }
  8711. +
  8712. + } else {
  8713. +
  8714. + /* A full chunk. Read directly into the supplied buffer. */
  8715. + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
  8716. +
  8717. + }
  8718. +
  8719. + n -= nToCopy;
  8720. + offset += nToCopy;
  8721. + buffer += nToCopy;
  8722. + nDone += nToCopy;
  8723. +
  8724. + }
  8725. +
  8726. + return nDone;
  8727. +}
  8728. +
  8729. +int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
  8730. + int nBytes, int writeThrough)
  8731. +{
  8732. +
  8733. + int chunk;
  8734. + __u32 start;
  8735. + int nToCopy;
  8736. + int n = nBytes;
  8737. + int nDone = 0;
  8738. + int nToWriteBack;
  8739. + int startOfWrite = offset;
  8740. + int chunkWritten = 0;
  8741. + __u32 nBytesRead;
  8742. + __u32 chunkStart;
  8743. +
  8744. + yaffs_Device *dev;
  8745. +
  8746. + dev = in->myDev;
  8747. +
  8748. + while (n > 0 && chunkWritten >= 0) {
  8749. + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
  8750. + /* start = offset % dev->nDataBytesPerChunk; */
  8751. + yaffs_AddrToChunk(dev, offset, &chunk, &start);
  8752. +
  8753. + if (chunk * dev->nDataBytesPerChunk + start != offset ||
  8754. + start >= dev->nDataBytesPerChunk) {
  8755. + T(YAFFS_TRACE_ERROR, (
  8756. + TSTR("AddrToChunk of offset %d gives chunk %d start %d"
  8757. + TENDSTR),
  8758. + (int)offset, chunk, start));
  8759. + }
  8760. + chunk++;
  8761. +
  8762. + /* OK now check for the curveball where the start and end are in
  8763. + * the same chunk.
  8764. + */
  8765. +
  8766. + if ((start + n) < dev->nDataBytesPerChunk) {
  8767. + nToCopy = n;
  8768. +
  8769. + /* Now folks, to calculate how many bytes to write back....
  8770. + * If we're overwriting and not writing to then end of file then
  8771. + * we need to write back as much as was there before.
  8772. + */
  8773. +
  8774. + chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
  8775. +
  8776. + if (chunkStart > in->variant.fileVariant.fileSize)
  8777. + nBytesRead = 0; /* Past end of file */
  8778. + else
  8779. + nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
  8780. +
  8781. + if (nBytesRead > dev->nDataBytesPerChunk)
  8782. + nBytesRead = dev->nDataBytesPerChunk;
  8783. +
  8784. + nToWriteBack =
  8785. + (nBytesRead >
  8786. + (start + n)) ? nBytesRead : (start + n);
  8787. +
  8788. + if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
  8789. + YBUG();
  8790. +
  8791. + } else {
  8792. + nToCopy = dev->nDataBytesPerChunk - start;
  8793. + nToWriteBack = dev->nDataBytesPerChunk;
  8794. + }
  8795. +
  8796. + if (nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
  8797. + /* An incomplete start or end chunk (or maybe both start and end chunk),
  8798. + * or we're using inband tags, so we want to use the cache buffers.
  8799. + */
  8800. + if (dev->nShortOpCaches > 0) {
  8801. + yaffs_ChunkCache *cache;
  8802. + /* If we can't find the data in the cache, then load the cache */
  8803. + cache = yaffs_FindChunkCache(in, chunk);
  8804. +
  8805. + if (!cache
  8806. + && yaffs_CheckSpaceForAllocation(in->
  8807. + myDev)) {
  8808. + cache = yaffs_GrabChunkCache(in->myDev);
  8809. + cache->object = in;
  8810. + cache->chunkId = chunk;
  8811. + cache->dirty = 0;
  8812. + cache->locked = 0;
  8813. + yaffs_ReadChunkDataFromObject(in, chunk,
  8814. + cache->
  8815. + data);
  8816. + } else if (cache &&
  8817. + !cache->dirty &&
  8818. + !yaffs_CheckSpaceForAllocation(in->myDev)) {
  8819. + /* Drop the cache if it was a read cache item and
  8820. + * no space check has been made for it.
  8821. + */
  8822. + cache = NULL;
  8823. + }
  8824. +
  8825. + if (cache) {
  8826. + yaffs_UseChunkCache(dev, cache, 1);
  8827. + cache->locked = 1;
  8828. +
  8829. +
  8830. + memcpy(&cache->data[start], buffer,
  8831. + nToCopy);
  8832. +
  8833. +
  8834. + cache->locked = 0;
  8835. + cache->nBytes = nToWriteBack;
  8836. +
  8837. + if (writeThrough) {
  8838. + chunkWritten =
  8839. + yaffs_WriteChunkDataToObject
  8840. + (cache->object,
  8841. + cache->chunkId,
  8842. + cache->data, cache->nBytes,
  8843. + 1);
  8844. + cache->dirty = 0;
  8845. + }
  8846. +
  8847. + } else {
  8848. + chunkWritten = -1; /* fail the write */
  8849. + }
  8850. + } else {
  8851. + /* An incomplete start or end chunk (or maybe both start and end chunk)
  8852. + * Read into the local buffer then copy, then copy over and write back.
  8853. + */
  8854. +
  8855. + __u8 *localBuffer =
  8856. + yaffs_GetTempBuffer(dev, __LINE__);
  8857. +
  8858. + yaffs_ReadChunkDataFromObject(in, chunk,
  8859. + localBuffer);
  8860. +
  8861. +
  8862. +
  8863. + memcpy(&localBuffer[start], buffer, nToCopy);
  8864. +
  8865. + chunkWritten =
  8866. + yaffs_WriteChunkDataToObject(in, chunk,
  8867. + localBuffer,
  8868. + nToWriteBack,
  8869. + 0);
  8870. +
  8871. + yaffs_ReleaseTempBuffer(dev, localBuffer,
  8872. + __LINE__);
  8873. +
  8874. + }
  8875. +
  8876. + } else {
  8877. + /* A full chunk. Write directly from the supplied buffer. */
  8878. +
  8879. +
  8880. +
  8881. + chunkWritten =
  8882. + yaffs_WriteChunkDataToObject(in, chunk, buffer,
  8883. + dev->nDataBytesPerChunk,
  8884. + 0);
  8885. +
  8886. + /* Since we've overwritten the cached data, we better invalidate it. */
  8887. + yaffs_InvalidateChunkCache(in, chunk);
  8888. + }
  8889. +
  8890. + if (chunkWritten >= 0) {
  8891. + n -= nToCopy;
  8892. + offset += nToCopy;
  8893. + buffer += nToCopy;
  8894. + nDone += nToCopy;
  8895. + }
  8896. +
  8897. + }
  8898. +
  8899. + /* Update file object */
  8900. +
  8901. + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
  8902. + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
  8903. +
  8904. + in->dirty = 1;
  8905. +
  8906. + return nDone;
  8907. +}
  8908. +
  8909. +
  8910. +/* ---------------------- File resizing stuff ------------------ */
  8911. +
  8912. +static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
  8913. +{
  8914. +
  8915. + yaffs_Device *dev = in->myDev;
  8916. + int oldFileSize = in->variant.fileVariant.fileSize;
  8917. +
  8918. + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
  8919. +
  8920. + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
  8921. + dev->nDataBytesPerChunk;
  8922. + int i;
  8923. + int chunkId;
  8924. +
  8925. + /* Delete backwards so that we don't end up with holes if
  8926. + * power is lost part-way through the operation.
  8927. + */
  8928. + for (i = lastDel; i >= startDel; i--) {
  8929. + /* NB this could be optimised somewhat,
  8930. + * eg. could retrieve the tags and write them without
  8931. + * using yaffs_DeleteChunk
  8932. + */
  8933. +
  8934. + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
  8935. + if (chunkId > 0) {
  8936. + if (chunkId <
  8937. + (dev->internalStartBlock * dev->nChunksPerBlock)
  8938. + || chunkId >=
  8939. + ((dev->internalEndBlock +
  8940. + 1) * dev->nChunksPerBlock)) {
  8941. + T(YAFFS_TRACE_ALWAYS,
  8942. + (TSTR("Found daft chunkId %d for %d" TENDSTR),
  8943. + chunkId, i));
  8944. + } else {
  8945. + in->nDataChunks--;
  8946. + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
  8947. + }
  8948. + }
  8949. + }
  8950. +
  8951. +}
  8952. +
  8953. +int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
  8954. +{
  8955. +
  8956. + int oldFileSize = in->variant.fileVariant.fileSize;
  8957. + __u32 newSizeOfPartialChunk;
  8958. + int newFullChunks;
  8959. +
  8960. + yaffs_Device *dev = in->myDev;
  8961. +
  8962. + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
  8963. +
  8964. + yaffs_FlushFilesChunkCache(in);
  8965. + yaffs_InvalidateWholeChunkCache(in);
  8966. +
  8967. + yaffs_CheckGarbageCollection(dev);
  8968. +
  8969. + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
  8970. + return YAFFS_FAIL;
  8971. +
  8972. + if (newSize == oldFileSize)
  8973. + return YAFFS_OK;
  8974. +
  8975. + if (newSize < oldFileSize) {
  8976. +
  8977. + yaffs_PruneResizedChunks(in, newSize);
  8978. +
  8979. + if (newSizeOfPartialChunk != 0) {
  8980. + int lastChunk = 1 + newFullChunks;
  8981. +
  8982. + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
  8983. +
  8984. + /* Got to read and rewrite the last chunk with its new size and zero pad */
  8985. + yaffs_ReadChunkDataFromObject(in, lastChunk,
  8986. + localBuffer);
  8987. +
  8988. + memset(localBuffer + newSizeOfPartialChunk, 0,
  8989. + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
  8990. +
  8991. + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
  8992. + newSizeOfPartialChunk, 1);
  8993. +
  8994. + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
  8995. + }
  8996. +
  8997. + in->variant.fileVariant.fileSize = newSize;
  8998. +
  8999. + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
  9000. + } else {
  9001. + /* newsSize > oldFileSize */
  9002. + in->variant.fileVariant.fileSize = newSize;
  9003. + }
  9004. +
  9005. +
  9006. + /* Write a new object header.
  9007. + * show we've shrunk the file, if need be
  9008. + * Do this only if the file is not in the deleted directories.
  9009. + */
  9010. + if (in->parent &&
  9011. + in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
  9012. + in->parent->objectId != YAFFS_OBJECTID_DELETED)
  9013. + yaffs_UpdateObjectHeader(in, NULL, 0,
  9014. + (newSize < oldFileSize) ? 1 : 0, 0);
  9015. +
  9016. + return YAFFS_OK;
  9017. +}
  9018. +
  9019. +loff_t yaffs_GetFileSize(yaffs_Object *obj)
  9020. +{
  9021. + obj = yaffs_GetEquivalentObject(obj);
  9022. +
  9023. + switch (obj->variantType) {
  9024. + case YAFFS_OBJECT_TYPE_FILE:
  9025. + return obj->variant.fileVariant.fileSize;
  9026. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9027. + return yaffs_strlen(obj->variant.symLinkVariant.alias);
  9028. + default:
  9029. + return 0;
  9030. + }
  9031. +}
  9032. +
  9033. +
  9034. +
  9035. +int yaffs_FlushFile(yaffs_Object *in, int updateTime)
  9036. +{
  9037. + int retVal;
  9038. + if (in->dirty) {
  9039. + yaffs_FlushFilesChunkCache(in);
  9040. + if (updateTime) {
  9041. +#ifdef CONFIG_YAFFS_WINCE
  9042. + yfsd_WinFileTimeNow(in->win_mtime);
  9043. +#else
  9044. +
  9045. + in->yst_mtime = Y_CURRENT_TIME;
  9046. +
  9047. +#endif
  9048. + }
  9049. +
  9050. + retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
  9051. + 0) ? YAFFS_OK : YAFFS_FAIL;
  9052. + } else {
  9053. + retVal = YAFFS_OK;
  9054. + }
  9055. +
  9056. + return retVal;
  9057. +
  9058. +}
  9059. +
  9060. +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
  9061. +{
  9062. +
  9063. + /* First off, invalidate the file's data in the cache, without flushing. */
  9064. + yaffs_InvalidateWholeChunkCache(in);
  9065. +
  9066. + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
  9067. + /* Move to the unlinked directory so we have a record that it was deleted. */
  9068. + yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
  9069. +
  9070. + }
  9071. +
  9072. + yaffs_RemoveObjectFromDirectory(in);
  9073. + yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
  9074. + in->hdrChunk = 0;
  9075. +
  9076. + yaffs_FreeObject(in);
  9077. + return YAFFS_OK;
  9078. +
  9079. +}
  9080. +
  9081. +/* yaffs_DeleteFile deletes the whole file data
  9082. + * and the inode associated with the file.
  9083. + * It does not delete the links associated with the file.
  9084. + */
  9085. +static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
  9086. +{
  9087. +
  9088. + int retVal;
  9089. + int immediateDeletion = 0;
  9090. +
  9091. +#ifdef __KERNEL__
  9092. + if (!in->myInode)
  9093. + immediateDeletion = 1;
  9094. +#else
  9095. + if (in->inUse <= 0)
  9096. + immediateDeletion = 1;
  9097. +#endif
  9098. +
  9099. + if (immediateDeletion) {
  9100. + retVal =
  9101. + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
  9102. + _Y("deleted"), 0, 0);
  9103. + T(YAFFS_TRACE_TRACING,
  9104. + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
  9105. + in->objectId));
  9106. + in->deleted = 1;
  9107. + in->myDev->nDeletedFiles++;
  9108. + if (1 || in->myDev->isYaffs2)
  9109. + yaffs_ResizeFile(in, 0);
  9110. + yaffs_SoftDeleteFile(in);
  9111. + } else {
  9112. + retVal =
  9113. + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
  9114. + _Y("unlinked"), 0, 0);
  9115. + }
  9116. +
  9117. +
  9118. + return retVal;
  9119. +}
  9120. +
  9121. +int yaffs_DeleteFile(yaffs_Object *in)
  9122. +{
  9123. + int retVal = YAFFS_OK;
  9124. + int deleted = in->deleted;
  9125. +
  9126. + yaffs_ResizeFile(in, 0);
  9127. +
  9128. + if (in->nDataChunks > 0) {
  9129. + /* Use soft deletion if there is data in the file.
  9130. + * That won't be the case if it has been resized to zero.
  9131. + */
  9132. + if (!in->unlinked)
  9133. + retVal = yaffs_UnlinkFileIfNeeded(in);
  9134. +
  9135. + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
  9136. + in->deleted = 1;
  9137. + deleted = 1;
  9138. + in->myDev->nDeletedFiles++;
  9139. + yaffs_SoftDeleteFile(in);
  9140. + }
  9141. + return deleted ? YAFFS_OK : YAFFS_FAIL;
  9142. + } else {
  9143. + /* The file has no data chunks so we toss it immediately */
  9144. + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
  9145. + in->variant.fileVariant.top = NULL;
  9146. + yaffs_DoGenericObjectDeletion(in);
  9147. +
  9148. + return YAFFS_OK;
  9149. + }
  9150. +}
  9151. +
  9152. +static int yaffs_DeleteDirectory(yaffs_Object *in)
  9153. +{
  9154. + /* First check that the directory is empty. */
  9155. + if (ylist_empty(&in->variant.directoryVariant.children))
  9156. + return yaffs_DoGenericObjectDeletion(in);
  9157. +
  9158. + return YAFFS_FAIL;
  9159. +
  9160. +}
  9161. +
  9162. +static int yaffs_DeleteSymLink(yaffs_Object *in)
  9163. +{
  9164. + YFREE(in->variant.symLinkVariant.alias);
  9165. +
  9166. + return yaffs_DoGenericObjectDeletion(in);
  9167. +}
  9168. +
  9169. +static int yaffs_DeleteHardLink(yaffs_Object *in)
  9170. +{
  9171. + /* remove this hardlink from the list assocaited with the equivalent
  9172. + * object
  9173. + */
  9174. + ylist_del_init(&in->hardLinks);
  9175. + return yaffs_DoGenericObjectDeletion(in);
  9176. +}
  9177. +
  9178. +int yaffs_DeleteObject(yaffs_Object *obj)
  9179. +{
  9180. +int retVal = -1;
  9181. + switch (obj->variantType) {
  9182. + case YAFFS_OBJECT_TYPE_FILE:
  9183. + retVal = yaffs_DeleteFile(obj);
  9184. + break;
  9185. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9186. + return yaffs_DeleteDirectory(obj);
  9187. + break;
  9188. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9189. + retVal = yaffs_DeleteSymLink(obj);
  9190. + break;
  9191. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9192. + retVal = yaffs_DeleteHardLink(obj);
  9193. + break;
  9194. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9195. + retVal = yaffs_DoGenericObjectDeletion(obj);
  9196. + break;
  9197. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9198. + retVal = 0;
  9199. + break; /* should not happen. */
  9200. + }
  9201. +
  9202. + return retVal;
  9203. +}
  9204. +
  9205. +static int yaffs_UnlinkWorker(yaffs_Object *obj)
  9206. +{
  9207. +
  9208. + int immediateDeletion = 0;
  9209. +
  9210. +#ifdef __KERNEL__
  9211. + if (!obj->myInode)
  9212. + immediateDeletion = 1;
  9213. +#else
  9214. + if (obj->inUse <= 0)
  9215. + immediateDeletion = 1;
  9216. +#endif
  9217. +
  9218. + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  9219. + return yaffs_DeleteHardLink(obj);
  9220. + } else if (!ylist_empty(&obj->hardLinks)) {
  9221. + /* Curve ball: We're unlinking an object that has a hardlink.
  9222. + *
  9223. + * This problem arises because we are not strictly following
  9224. + * The Linux link/inode model.
  9225. + *
  9226. + * We can't really delete the object.
  9227. + * Instead, we do the following:
  9228. + * - Select a hardlink.
  9229. + * - Unhook it from the hard links
  9230. + * - Unhook it from its parent directory (so that the rename can work)
  9231. + * - Rename the object to the hardlink's name.
  9232. + * - Delete the hardlink
  9233. + */
  9234. +
  9235. + yaffs_Object *hl;
  9236. + int retVal;
  9237. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  9238. +
  9239. + hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
  9240. +
  9241. + ylist_del_init(&hl->hardLinks);
  9242. + ylist_del_init(&hl->siblings);
  9243. +
  9244. + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  9245. +
  9246. + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
  9247. +
  9248. + if (retVal == YAFFS_OK)
  9249. + retVal = yaffs_DoGenericObjectDeletion(hl);
  9250. +
  9251. + return retVal;
  9252. +
  9253. + } else if (immediateDeletion) {
  9254. + switch (obj->variantType) {
  9255. + case YAFFS_OBJECT_TYPE_FILE:
  9256. + return yaffs_DeleteFile(obj);
  9257. + break;
  9258. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9259. + return yaffs_DeleteDirectory(obj);
  9260. + break;
  9261. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9262. + return yaffs_DeleteSymLink(obj);
  9263. + break;
  9264. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9265. + return yaffs_DoGenericObjectDeletion(obj);
  9266. + break;
  9267. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9268. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9269. + default:
  9270. + return YAFFS_FAIL;
  9271. + }
  9272. + } else
  9273. + return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
  9274. + _Y("unlinked"), 0, 0);
  9275. +}
  9276. +
  9277. +
  9278. +static int yaffs_UnlinkObject(yaffs_Object *obj)
  9279. +{
  9280. +
  9281. + if (obj && obj->unlinkAllowed)
  9282. + return yaffs_UnlinkWorker(obj);
  9283. +
  9284. + return YAFFS_FAIL;
  9285. +
  9286. +}
  9287. +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
  9288. +{
  9289. + yaffs_Object *obj;
  9290. +
  9291. + obj = yaffs_FindObjectByName(dir, name);
  9292. + return yaffs_UnlinkObject(obj);
  9293. +}
  9294. +
  9295. +/*----------------------- Initialisation Scanning ---------------------- */
  9296. +
  9297. +static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
  9298. + int backwardScanning)
  9299. +{
  9300. + yaffs_Object *obj;
  9301. +
  9302. + if (!backwardScanning) {
  9303. + /* Handle YAFFS1 forward scanning case
  9304. + * For YAFFS1 we always do the deletion
  9305. + */
  9306. +
  9307. + } else {
  9308. + /* Handle YAFFS2 case (backward scanning)
  9309. + * If the shadowed object exists then ignore.
  9310. + */
  9311. + if (yaffs_FindObjectByNumber(dev, objId))
  9312. + return;
  9313. + }
  9314. +
  9315. + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  9316. + * We put it in unlinked dir to be cleaned up after the scanning
  9317. + */
  9318. + obj =
  9319. + yaffs_FindOrCreateObjectByNumber(dev, objId,
  9320. + YAFFS_OBJECT_TYPE_FILE);
  9321. + if (!obj)
  9322. + return;
  9323. + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
  9324. + obj->variant.fileVariant.shrinkSize = 0;
  9325. + obj->valid = 1; /* So that we don't read any other info for this file */
  9326. +
  9327. +}
  9328. +
  9329. +typedef struct {
  9330. + int seq;
  9331. + int block;
  9332. +} yaffs_BlockIndex;
  9333. +
  9334. +
  9335. +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
  9336. +{
  9337. + yaffs_Object *hl;
  9338. + yaffs_Object *in;
  9339. +
  9340. + while (hardList) {
  9341. + hl = hardList;
  9342. + hardList = (yaffs_Object *) (hardList->hardLinks.next);
  9343. +
  9344. + in = yaffs_FindObjectByNumber(dev,
  9345. + hl->variant.hardLinkVariant.
  9346. + equivalentObjectId);
  9347. +
  9348. + if (in) {
  9349. + /* Add the hardlink pointers */
  9350. + hl->variant.hardLinkVariant.equivalentObject = in;
  9351. + ylist_add(&hl->hardLinks, &in->hardLinks);
  9352. + } else {
  9353. + /* Todo Need to report/handle this better.
  9354. + * Got a problem... hardlink to a non-existant object
  9355. + */
  9356. + hl->variant.hardLinkVariant.equivalentObject = NULL;
  9357. + YINIT_LIST_HEAD(&hl->hardLinks);
  9358. +
  9359. + }
  9360. + }
  9361. +}
  9362. +
  9363. +
  9364. +
  9365. +
  9366. +
  9367. +static int ybicmp(const void *a, const void *b)
  9368. +{
  9369. + register int aseq = ((yaffs_BlockIndex *)a)->seq;
  9370. + register int bseq = ((yaffs_BlockIndex *)b)->seq;
  9371. + register int ablock = ((yaffs_BlockIndex *)a)->block;
  9372. + register int bblock = ((yaffs_BlockIndex *)b)->block;
  9373. + if (aseq == bseq)
  9374. + return ablock - bblock;
  9375. + else
  9376. + return aseq - bseq;
  9377. +}
  9378. +
  9379. +
  9380. +struct yaffs_ShadowFixerStruct {
  9381. + int objectId;
  9382. + int shadowedId;
  9383. + struct yaffs_ShadowFixerStruct *next;
  9384. +};
  9385. +
  9386. +
  9387. +static void yaffs_StripDeletedObjects(yaffs_Device *dev)
  9388. +{
  9389. + /*
  9390. + * Sort out state of unlinked and deleted objects after scanning.
  9391. + */
  9392. + struct ylist_head *i;
  9393. + struct ylist_head *n;
  9394. + yaffs_Object *l;
  9395. +
  9396. + /* Soft delete all the unlinked files */
  9397. + ylist_for_each_safe(i, n,
  9398. + &dev->unlinkedDir->variant.directoryVariant.children) {
  9399. + if (i) {
  9400. + l = ylist_entry(i, yaffs_Object, siblings);
  9401. + yaffs_DeleteObject(l);
  9402. + }
  9403. + }
  9404. +
  9405. + ylist_for_each_safe(i, n,
  9406. + &dev->deletedDir->variant.directoryVariant.children) {
  9407. + if (i) {
  9408. + l = ylist_entry(i, yaffs_Object, siblings);
  9409. + yaffs_DeleteObject(l);
  9410. + }
  9411. + }
  9412. +
  9413. +}
  9414. +
  9415. +static int yaffs_Scan(yaffs_Device *dev)
  9416. +{
  9417. + yaffs_ExtendedTags tags;
  9418. + int blk;
  9419. + int blockIterator;
  9420. + int startIterator;
  9421. + int endIterator;
  9422. + int result;
  9423. +
  9424. + int chunk;
  9425. + int c;
  9426. + int deleted;
  9427. + yaffs_BlockState state;
  9428. + yaffs_Object *hardList = NULL;
  9429. + yaffs_BlockInfo *bi;
  9430. + __u32 sequenceNumber;
  9431. + yaffs_ObjectHeader *oh;
  9432. + yaffs_Object *in;
  9433. + yaffs_Object *parent;
  9434. +
  9435. + int alloc_failed = 0;
  9436. +
  9437. + struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
  9438. +
  9439. +
  9440. + __u8 *chunkData;
  9441. +
  9442. +
  9443. +
  9444. + T(YAFFS_TRACE_SCAN,
  9445. + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
  9446. + dev->internalStartBlock, dev->internalEndBlock));
  9447. +
  9448. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9449. +
  9450. + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  9451. +
  9452. + /* Scan all the blocks to determine their state */
  9453. + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  9454. + bi = yaffs_GetBlockInfo(dev, blk);
  9455. + yaffs_ClearChunkBits(dev, blk);
  9456. + bi->pagesInUse = 0;
  9457. + bi->softDeletions = 0;
  9458. +
  9459. + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  9460. +
  9461. + bi->blockState = state;
  9462. + bi->sequenceNumber = sequenceNumber;
  9463. +
  9464. + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
  9465. + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
  9466. +
  9467. + T(YAFFS_TRACE_SCAN_DEBUG,
  9468. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  9469. + state, sequenceNumber));
  9470. +
  9471. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  9472. + T(YAFFS_TRACE_BAD_BLOCKS,
  9473. + (TSTR("block %d is bad" TENDSTR), blk));
  9474. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  9475. + T(YAFFS_TRACE_SCAN_DEBUG,
  9476. + (TSTR("Block empty " TENDSTR)));
  9477. + dev->nErasedBlocks++;
  9478. + dev->nFreeChunks += dev->nChunksPerBlock;
  9479. + }
  9480. + }
  9481. +
  9482. + startIterator = dev->internalStartBlock;
  9483. + endIterator = dev->internalEndBlock;
  9484. +
  9485. + /* For each block.... */
  9486. + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
  9487. + blockIterator++) {
  9488. +
  9489. + YYIELD();
  9490. +
  9491. + YYIELD();
  9492. +
  9493. + blk = blockIterator;
  9494. +
  9495. + bi = yaffs_GetBlockInfo(dev, blk);
  9496. + state = bi->blockState;
  9497. +
  9498. + deleted = 0;
  9499. +
  9500. + /* For each chunk in each block that needs scanning....*/
  9501. + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
  9502. + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  9503. + /* Read the tags and decide what to do */
  9504. + chunk = blk * dev->nChunksPerBlock + c;
  9505. +
  9506. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  9507. + &tags);
  9508. +
  9509. + /* Let's have a good look at this chunk... */
  9510. +
  9511. + if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
  9512. + /* YAFFS1 only...
  9513. + * A deleted chunk
  9514. + */
  9515. + deleted++;
  9516. + dev->nFreeChunks++;
  9517. + /*T((" %d %d deleted\n",blk,c)); */
  9518. + } else if (!tags.chunkUsed) {
  9519. + /* An unassigned chunk in the block
  9520. + * This means that either the block is empty or
  9521. + * this is the one being allocated from
  9522. + */
  9523. +
  9524. + if (c == 0) {
  9525. + /* We're looking at the first chunk in the block so the block is unused */
  9526. + state = YAFFS_BLOCK_STATE_EMPTY;
  9527. + dev->nErasedBlocks++;
  9528. + } else {
  9529. + /* this is the block being allocated from */
  9530. + T(YAFFS_TRACE_SCAN,
  9531. + (TSTR
  9532. + (" Allocating from %d %d" TENDSTR),
  9533. + blk, c));
  9534. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  9535. + dev->allocationBlock = blk;
  9536. + dev->allocationPage = c;
  9537. + dev->allocationBlockFinder = blk;
  9538. + /* Set it to here to encourage the allocator to go forth from here. */
  9539. +
  9540. + }
  9541. +
  9542. + dev->nFreeChunks += (dev->nChunksPerBlock - c);
  9543. + } else if (tags.chunkId > 0) {
  9544. + /* chunkId > 0 so it is a data chunk... */
  9545. + unsigned int endpos;
  9546. +
  9547. + yaffs_SetChunkBit(dev, blk, c);
  9548. + bi->pagesInUse++;
  9549. +
  9550. + in = yaffs_FindOrCreateObjectByNumber(dev,
  9551. + tags.
  9552. + objectId,
  9553. + YAFFS_OBJECT_TYPE_FILE);
  9554. + /* PutChunkIntoFile checks for a clash (two data chunks with
  9555. + * the same chunkId).
  9556. + */
  9557. +
  9558. + if (!in)
  9559. + alloc_failed = 1;
  9560. +
  9561. + if (in) {
  9562. + if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
  9563. + alloc_failed = 1;
  9564. + }
  9565. +
  9566. + endpos =
  9567. + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
  9568. + tags.byteCount;
  9569. + if (in &&
  9570. + in->variantType == YAFFS_OBJECT_TYPE_FILE
  9571. + && in->variant.fileVariant.scannedFileSize <
  9572. + endpos) {
  9573. + in->variant.fileVariant.
  9574. + scannedFileSize = endpos;
  9575. + if (!dev->useHeaderFileSize) {
  9576. + in->variant.fileVariant.
  9577. + fileSize =
  9578. + in->variant.fileVariant.
  9579. + scannedFileSize;
  9580. + }
  9581. +
  9582. + }
  9583. + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
  9584. + } else {
  9585. + /* chunkId == 0, so it is an ObjectHeader.
  9586. + * Thus, we read in the object header and make the object
  9587. + */
  9588. + yaffs_SetChunkBit(dev, blk, c);
  9589. + bi->pagesInUse++;
  9590. +
  9591. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
  9592. + chunkData,
  9593. + NULL);
  9594. +
  9595. + oh = (yaffs_ObjectHeader *) chunkData;
  9596. +
  9597. + in = yaffs_FindObjectByNumber(dev,
  9598. + tags.objectId);
  9599. + if (in && in->variantType != oh->type) {
  9600. + /* This should not happen, but somehow
  9601. + * Wev'e ended up with an objectId that has been reused but not yet
  9602. + * deleted, and worse still it has changed type. Delete the old object.
  9603. + */
  9604. +
  9605. + yaffs_DeleteObject(in);
  9606. +
  9607. + in = 0;
  9608. + }
  9609. +
  9610. + in = yaffs_FindOrCreateObjectByNumber(dev,
  9611. + tags.
  9612. + objectId,
  9613. + oh->type);
  9614. +
  9615. + if (!in)
  9616. + alloc_failed = 1;
  9617. +
  9618. + if (in && oh->shadowsObject > 0) {
  9619. +
  9620. + struct yaffs_ShadowFixerStruct *fixer;
  9621. + fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
  9622. + if (fixer) {
  9623. + fixer->next = shadowFixerList;
  9624. + shadowFixerList = fixer;
  9625. + fixer->objectId = tags.objectId;
  9626. + fixer->shadowedId = oh->shadowsObject;
  9627. + }
  9628. +
  9629. + }
  9630. +
  9631. + if (in && in->valid) {
  9632. + /* We have already filled this one. We have a duplicate and need to resolve it. */
  9633. +
  9634. + unsigned existingSerial = in->serial;
  9635. + unsigned newSerial = tags.serialNumber;
  9636. +
  9637. + if (((existingSerial + 1) & 3) == newSerial) {
  9638. + /* Use new one - destroy the exisiting one */
  9639. + yaffs_DeleteChunk(dev,
  9640. + in->hdrChunk,
  9641. + 1, __LINE__);
  9642. + in->valid = 0;
  9643. + } else {
  9644. + /* Use existing - destroy this one. */
  9645. + yaffs_DeleteChunk(dev, chunk, 1,
  9646. + __LINE__);
  9647. + }
  9648. + }
  9649. +
  9650. + if (in && !in->valid &&
  9651. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  9652. + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
  9653. + /* We only load some info, don't fiddle with directory structure */
  9654. + in->valid = 1;
  9655. + in->variantType = oh->type;
  9656. +
  9657. + in->yst_mode = oh->yst_mode;
  9658. +#ifdef CONFIG_YAFFS_WINCE
  9659. + in->win_atime[0] = oh->win_atime[0];
  9660. + in->win_ctime[0] = oh->win_ctime[0];
  9661. + in->win_mtime[0] = oh->win_mtime[0];
  9662. + in->win_atime[1] = oh->win_atime[1];
  9663. + in->win_ctime[1] = oh->win_ctime[1];
  9664. + in->win_mtime[1] = oh->win_mtime[1];
  9665. +#else
  9666. + in->yst_uid = oh->yst_uid;
  9667. + in->yst_gid = oh->yst_gid;
  9668. + in->yst_atime = oh->yst_atime;
  9669. + in->yst_mtime = oh->yst_mtime;
  9670. + in->yst_ctime = oh->yst_ctime;
  9671. + in->yst_rdev = oh->yst_rdev;
  9672. +#endif
  9673. + in->hdrChunk = chunk;
  9674. + in->serial = tags.serialNumber;
  9675. +
  9676. + } else if (in && !in->valid) {
  9677. + /* we need to load this info */
  9678. +
  9679. + in->valid = 1;
  9680. + in->variantType = oh->type;
  9681. +
  9682. + in->yst_mode = oh->yst_mode;
  9683. +#ifdef CONFIG_YAFFS_WINCE
  9684. + in->win_atime[0] = oh->win_atime[0];
  9685. + in->win_ctime[0] = oh->win_ctime[0];
  9686. + in->win_mtime[0] = oh->win_mtime[0];
  9687. + in->win_atime[1] = oh->win_atime[1];
  9688. + in->win_ctime[1] = oh->win_ctime[1];
  9689. + in->win_mtime[1] = oh->win_mtime[1];
  9690. +#else
  9691. + in->yst_uid = oh->yst_uid;
  9692. + in->yst_gid = oh->yst_gid;
  9693. + in->yst_atime = oh->yst_atime;
  9694. + in->yst_mtime = oh->yst_mtime;
  9695. + in->yst_ctime = oh->yst_ctime;
  9696. + in->yst_rdev = oh->yst_rdev;
  9697. +#endif
  9698. + in->hdrChunk = chunk;
  9699. + in->serial = tags.serialNumber;
  9700. +
  9701. + yaffs_SetObjectName(in, oh->name);
  9702. + in->dirty = 0;
  9703. +
  9704. + /* directory stuff...
  9705. + * hook up to parent
  9706. + */
  9707. +
  9708. + parent =
  9709. + yaffs_FindOrCreateObjectByNumber
  9710. + (dev, oh->parentObjectId,
  9711. + YAFFS_OBJECT_TYPE_DIRECTORY);
  9712. + if (!parent)
  9713. + alloc_failed = 1;
  9714. + if (parent && parent->variantType ==
  9715. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  9716. + /* Set up as a directory */
  9717. + parent->variantType =
  9718. + YAFFS_OBJECT_TYPE_DIRECTORY;
  9719. + YINIT_LIST_HEAD(&parent->variant.
  9720. + directoryVariant.
  9721. + children);
  9722. + } else if (!parent || parent->variantType !=
  9723. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  9724. + /* Hoosterman, another problem....
  9725. + * We're trying to use a non-directory as a directory
  9726. + */
  9727. +
  9728. + T(YAFFS_TRACE_ERROR,
  9729. + (TSTR
  9730. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  9731. + TENDSTR)));
  9732. + parent = dev->lostNFoundDir;
  9733. + }
  9734. +
  9735. + yaffs_AddObjectToDirectory(parent, in);
  9736. +
  9737. + if (0 && (parent == dev->deletedDir ||
  9738. + parent == dev->unlinkedDir)) {
  9739. + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  9740. + dev->nDeletedFiles++;
  9741. + }
  9742. + /* Note re hardlinks.
  9743. + * Since we might scan a hardlink before its equivalent object is scanned
  9744. + * we put them all in a list.
  9745. + * After scanning is complete, we should have all the objects, so we run through this
  9746. + * list and fix up all the chains.
  9747. + */
  9748. +
  9749. + switch (in->variantType) {
  9750. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  9751. + /* Todo got a problem */
  9752. + break;
  9753. + case YAFFS_OBJECT_TYPE_FILE:
  9754. + if (dev->useHeaderFileSize)
  9755. +
  9756. + in->variant.fileVariant.
  9757. + fileSize =
  9758. + oh->fileSize;
  9759. +
  9760. + break;
  9761. + case YAFFS_OBJECT_TYPE_HARDLINK:
  9762. + in->variant.hardLinkVariant.
  9763. + equivalentObjectId =
  9764. + oh->equivalentObjectId;
  9765. + in->hardLinks.next =
  9766. + (struct ylist_head *)
  9767. + hardList;
  9768. + hardList = in;
  9769. + break;
  9770. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  9771. + /* Do nothing */
  9772. + break;
  9773. + case YAFFS_OBJECT_TYPE_SPECIAL:
  9774. + /* Do nothing */
  9775. + break;
  9776. + case YAFFS_OBJECT_TYPE_SYMLINK:
  9777. + in->variant.symLinkVariant.alias =
  9778. + yaffs_CloneString(oh->alias);
  9779. + if (!in->variant.symLinkVariant.alias)
  9780. + alloc_failed = 1;
  9781. + break;
  9782. + }
  9783. +
  9784. +/*
  9785. + if (parent == dev->deletedDir) {
  9786. + yaffs_DestroyObject(in);
  9787. + bi->hasShrinkHeader = 1;
  9788. + }
  9789. +*/
  9790. + }
  9791. + }
  9792. + }
  9793. +
  9794. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  9795. + /* If we got this far while scanning, then the block is fully allocated.*/
  9796. + state = YAFFS_BLOCK_STATE_FULL;
  9797. + }
  9798. +
  9799. + bi->blockState = state;
  9800. +
  9801. + /* Now let's see if it was dirty */
  9802. + if (bi->pagesInUse == 0 &&
  9803. + !bi->hasShrinkHeader &&
  9804. + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  9805. + yaffs_BlockBecameDirty(dev, blk);
  9806. + }
  9807. +
  9808. + }
  9809. +
  9810. +
  9811. + /* Ok, we've done all the scanning.
  9812. + * Fix up the hard link chains.
  9813. + * We should now have scanned all the objects, now it's time to add these
  9814. + * hardlinks.
  9815. + */
  9816. +
  9817. + yaffs_HardlinkFixup(dev, hardList);
  9818. +
  9819. + /* Fix up any shadowed objects */
  9820. + {
  9821. + struct yaffs_ShadowFixerStruct *fixer;
  9822. + yaffs_Object *obj;
  9823. +
  9824. + while (shadowFixerList) {
  9825. + fixer = shadowFixerList;
  9826. + shadowFixerList = fixer->next;
  9827. + /* Complete the rename transaction by deleting the shadowed object
  9828. + * then setting the object header to unshadowed.
  9829. + */
  9830. + obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
  9831. + if (obj)
  9832. + yaffs_DeleteObject(obj);
  9833. +
  9834. + obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
  9835. +
  9836. + if (obj)
  9837. + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
  9838. +
  9839. + YFREE(fixer);
  9840. + }
  9841. + }
  9842. +
  9843. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  9844. +
  9845. + if (alloc_failed)
  9846. + return YAFFS_FAIL;
  9847. +
  9848. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
  9849. +
  9850. +
  9851. + return YAFFS_OK;
  9852. +}
  9853. +
  9854. +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
  9855. +{
  9856. + __u8 *chunkData;
  9857. + yaffs_ObjectHeader *oh;
  9858. + yaffs_Device *dev;
  9859. + yaffs_ExtendedTags tags;
  9860. + int result;
  9861. + int alloc_failed = 0;
  9862. +
  9863. + if (!in)
  9864. + return;
  9865. +
  9866. + dev = in->myDev;
  9867. +
  9868. +#if 0
  9869. + T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
  9870. + in->objectId,
  9871. + in->lazyLoaded ? "not yet" : "already"));
  9872. +#endif
  9873. +
  9874. + if (in->lazyLoaded && in->hdrChunk > 0) {
  9875. + in->lazyLoaded = 0;
  9876. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9877. +
  9878. + result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
  9879. + oh = (yaffs_ObjectHeader *) chunkData;
  9880. +
  9881. + in->yst_mode = oh->yst_mode;
  9882. +#ifdef CONFIG_YAFFS_WINCE
  9883. + in->win_atime[0] = oh->win_atime[0];
  9884. + in->win_ctime[0] = oh->win_ctime[0];
  9885. + in->win_mtime[0] = oh->win_mtime[0];
  9886. + in->win_atime[1] = oh->win_atime[1];
  9887. + in->win_ctime[1] = oh->win_ctime[1];
  9888. + in->win_mtime[1] = oh->win_mtime[1];
  9889. +#else
  9890. + in->yst_uid = oh->yst_uid;
  9891. + in->yst_gid = oh->yst_gid;
  9892. + in->yst_atime = oh->yst_atime;
  9893. + in->yst_mtime = oh->yst_mtime;
  9894. + in->yst_ctime = oh->yst_ctime;
  9895. + in->yst_rdev = oh->yst_rdev;
  9896. +
  9897. +#endif
  9898. + yaffs_SetObjectName(in, oh->name);
  9899. +
  9900. + if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
  9901. + in->variant.symLinkVariant.alias =
  9902. + yaffs_CloneString(oh->alias);
  9903. + if (!in->variant.symLinkVariant.alias)
  9904. + alloc_failed = 1; /* Not returned to caller */
  9905. + }
  9906. +
  9907. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  9908. + }
  9909. +}
  9910. +
  9911. +static int yaffs_ScanBackwards(yaffs_Device *dev)
  9912. +{
  9913. + yaffs_ExtendedTags tags;
  9914. + int blk;
  9915. + int blockIterator;
  9916. + int startIterator;
  9917. + int endIterator;
  9918. + int nBlocksToScan = 0;
  9919. +
  9920. + int chunk;
  9921. + int result;
  9922. + int c;
  9923. + int deleted;
  9924. + yaffs_BlockState state;
  9925. + yaffs_Object *hardList = NULL;
  9926. + yaffs_BlockInfo *bi;
  9927. + __u32 sequenceNumber;
  9928. + yaffs_ObjectHeader *oh;
  9929. + yaffs_Object *in;
  9930. + yaffs_Object *parent;
  9931. + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
  9932. + int itsUnlinked;
  9933. + __u8 *chunkData;
  9934. +
  9935. + int fileSize;
  9936. + int isShrink;
  9937. + int foundChunksInBlock;
  9938. + int equivalentObjectId;
  9939. + int alloc_failed = 0;
  9940. +
  9941. +
  9942. + yaffs_BlockIndex *blockIndex = NULL;
  9943. + int altBlockIndex = 0;
  9944. +
  9945. + if (!dev->isYaffs2) {
  9946. + T(YAFFS_TRACE_SCAN,
  9947. + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
  9948. + return YAFFS_FAIL;
  9949. + }
  9950. +
  9951. + T(YAFFS_TRACE_SCAN,
  9952. + (TSTR
  9953. + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
  9954. + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
  9955. +
  9956. +
  9957. + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
  9958. +
  9959. + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
  9960. +
  9961. + if (!blockIndex) {
  9962. + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
  9963. + altBlockIndex = 1;
  9964. + }
  9965. +
  9966. + if (!blockIndex) {
  9967. + T(YAFFS_TRACE_SCAN,
  9968. + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
  9969. + return YAFFS_FAIL;
  9970. + }
  9971. +
  9972. + dev->blocksInCheckpoint = 0;
  9973. +
  9974. + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
  9975. +
  9976. + /* Scan all the blocks to determine their state */
  9977. + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
  9978. + bi = yaffs_GetBlockInfo(dev, blk);
  9979. + yaffs_ClearChunkBits(dev, blk);
  9980. + bi->pagesInUse = 0;
  9981. + bi->softDeletions = 0;
  9982. +
  9983. + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
  9984. +
  9985. + bi->blockState = state;
  9986. + bi->sequenceNumber = sequenceNumber;
  9987. +
  9988. + if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  9989. + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
  9990. + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
  9991. + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
  9992. +
  9993. + T(YAFFS_TRACE_SCAN_DEBUG,
  9994. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  9995. + state, sequenceNumber));
  9996. +
  9997. +
  9998. + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  9999. + dev->blocksInCheckpoint++;
  10000. +
  10001. + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
  10002. + T(YAFFS_TRACE_BAD_BLOCKS,
  10003. + (TSTR("block %d is bad" TENDSTR), blk));
  10004. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  10005. + T(YAFFS_TRACE_SCAN_DEBUG,
  10006. + (TSTR("Block empty " TENDSTR)));
  10007. + dev->nErasedBlocks++;
  10008. + dev->nFreeChunks += dev->nChunksPerBlock;
  10009. + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  10010. +
  10011. + /* Determine the highest sequence number */
  10012. + if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  10013. + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  10014. +
  10015. + blockIndex[nBlocksToScan].seq = sequenceNumber;
  10016. + blockIndex[nBlocksToScan].block = blk;
  10017. +
  10018. + nBlocksToScan++;
  10019. +
  10020. + if (sequenceNumber >= dev->sequenceNumber)
  10021. + dev->sequenceNumber = sequenceNumber;
  10022. + } else {
  10023. + /* TODO: Nasty sequence number! */
  10024. + T(YAFFS_TRACE_SCAN,
  10025. + (TSTR
  10026. + ("Block scanning block %d has bad sequence number %d"
  10027. + TENDSTR), blk, sequenceNumber));
  10028. +
  10029. + }
  10030. + }
  10031. + }
  10032. +
  10033. + T(YAFFS_TRACE_SCAN,
  10034. + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
  10035. +
  10036. +
  10037. +
  10038. + YYIELD();
  10039. +
  10040. + /* Sort the blocks */
  10041. +#ifndef CONFIG_YAFFS_USE_OWN_SORT
  10042. + {
  10043. + /* Use qsort now. */
  10044. + yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
  10045. + }
  10046. +#else
  10047. + {
  10048. + /* Dungy old bubble sort... */
  10049. +
  10050. + yaffs_BlockIndex temp;
  10051. + int i;
  10052. + int j;
  10053. +
  10054. + for (i = 0; i < nBlocksToScan; i++)
  10055. + for (j = i + 1; j < nBlocksToScan; j++)
  10056. + if (blockIndex[i].seq > blockIndex[j].seq) {
  10057. + temp = blockIndex[j];
  10058. + blockIndex[j] = blockIndex[i];
  10059. + blockIndex[i] = temp;
  10060. + }
  10061. + }
  10062. +#endif
  10063. +
  10064. + YYIELD();
  10065. +
  10066. + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
  10067. +
  10068. + /* Now scan the blocks looking at the data. */
  10069. + startIterator = 0;
  10070. + endIterator = nBlocksToScan - 1;
  10071. + T(YAFFS_TRACE_SCAN_DEBUG,
  10072. + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
  10073. +
  10074. + /* For each block.... backwards */
  10075. + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
  10076. + blockIterator--) {
  10077. + /* Cooperative multitasking! This loop can run for so
  10078. + long that watchdog timers expire. */
  10079. + YYIELD();
  10080. +
  10081. + /* get the block to scan in the correct order */
  10082. + blk = blockIndex[blockIterator].block;
  10083. +
  10084. + bi = yaffs_GetBlockInfo(dev, blk);
  10085. +
  10086. +
  10087. + state = bi->blockState;
  10088. +
  10089. + deleted = 0;
  10090. +
  10091. + /* For each chunk in each block that needs scanning.... */
  10092. + foundChunksInBlock = 0;
  10093. + for (c = dev->nChunksPerBlock - 1;
  10094. + !alloc_failed && c >= 0 &&
  10095. + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  10096. + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
  10097. + /* Scan backwards...
  10098. + * Read the tags and decide what to do
  10099. + */
  10100. +
  10101. + chunk = blk * dev->nChunksPerBlock + c;
  10102. +
  10103. + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
  10104. + &tags);
  10105. +
  10106. + /* Let's have a good look at this chunk... */
  10107. +
  10108. + if (!tags.chunkUsed) {
  10109. + /* An unassigned chunk in the block.
  10110. + * If there are used chunks after this one, then
  10111. + * it is a chunk that was skipped due to failing the erased
  10112. + * check. Just skip it so that it can be deleted.
  10113. + * But, more typically, We get here when this is an unallocated
  10114. + * chunk and his means that either the block is empty or
  10115. + * this is the one being allocated from
  10116. + */
  10117. +
  10118. + if (foundChunksInBlock) {
  10119. + /* This is a chunk that was skipped due to failing the erased check */
  10120. + } else if (c == 0) {
  10121. + /* We're looking at the first chunk in the block so the block is unused */
  10122. + state = YAFFS_BLOCK_STATE_EMPTY;
  10123. + dev->nErasedBlocks++;
  10124. + } else {
  10125. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  10126. + state == YAFFS_BLOCK_STATE_ALLOCATING) {
  10127. + if (dev->sequenceNumber == bi->sequenceNumber) {
  10128. + /* this is the block being allocated from */
  10129. +
  10130. + T(YAFFS_TRACE_SCAN,
  10131. + (TSTR
  10132. + (" Allocating from %d %d"
  10133. + TENDSTR), blk, c));
  10134. +
  10135. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  10136. + dev->allocationBlock = blk;
  10137. + dev->allocationPage = c;
  10138. + dev->allocationBlockFinder = blk;
  10139. + } else {
  10140. + /* This is a partially written block that is not
  10141. + * the current allocation block. This block must have
  10142. + * had a write failure, so set up for retirement.
  10143. + */
  10144. +
  10145. + /* bi->needsRetiring = 1; ??? TODO */
  10146. + bi->gcPrioritise = 1;
  10147. +
  10148. + T(YAFFS_TRACE_ALWAYS,
  10149. + (TSTR("Partially written block %d detected" TENDSTR),
  10150. + blk));
  10151. + }
  10152. + }
  10153. + }
  10154. +
  10155. + dev->nFreeChunks++;
  10156. +
  10157. + } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
  10158. + T(YAFFS_TRACE_SCAN,
  10159. + (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
  10160. + blk, c));
  10161. +
  10162. + dev->nFreeChunks++;
  10163. +
  10164. + } else if (tags.chunkId > 0) {
  10165. + /* chunkId > 0 so it is a data chunk... */
  10166. + unsigned int endpos;
  10167. + __u32 chunkBase =
  10168. + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
  10169. +
  10170. + foundChunksInBlock = 1;
  10171. +
  10172. +
  10173. + yaffs_SetChunkBit(dev, blk, c);
  10174. + bi->pagesInUse++;
  10175. +
  10176. + in = yaffs_FindOrCreateObjectByNumber(dev,
  10177. + tags.
  10178. + objectId,
  10179. + YAFFS_OBJECT_TYPE_FILE);
  10180. + if (!in) {
  10181. + /* Out of memory */
  10182. + alloc_failed = 1;
  10183. + }
  10184. +
  10185. + if (in &&
  10186. + in->variantType == YAFFS_OBJECT_TYPE_FILE
  10187. + && chunkBase <
  10188. + in->variant.fileVariant.shrinkSize) {
  10189. + /* This has not been invalidated by a resize */
  10190. + if (!yaffs_PutChunkIntoFile(in, tags.chunkId,
  10191. + chunk, -1)) {
  10192. + alloc_failed = 1;
  10193. + }
  10194. +
  10195. + /* File size is calculated by looking at the data chunks if we have not
  10196. + * seen an object header yet. Stop this practice once we find an object header.
  10197. + */
  10198. + endpos =
  10199. + (tags.chunkId -
  10200. + 1) * dev->nDataBytesPerChunk +
  10201. + tags.byteCount;
  10202. +
  10203. + if (!in->valid && /* have not got an object header yet */
  10204. + in->variant.fileVariant.
  10205. + scannedFileSize < endpos) {
  10206. + in->variant.fileVariant.
  10207. + scannedFileSize = endpos;
  10208. + in->variant.fileVariant.
  10209. + fileSize =
  10210. + in->variant.fileVariant.
  10211. + scannedFileSize;
  10212. + }
  10213. +
  10214. + } else if (in) {
  10215. + /* This chunk has been invalidated by a resize, so delete */
  10216. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  10217. +
  10218. + }
  10219. + } else {
  10220. + /* chunkId == 0, so it is an ObjectHeader.
  10221. + * Thus, we read in the object header and make the object
  10222. + */
  10223. + foundChunksInBlock = 1;
  10224. +
  10225. + yaffs_SetChunkBit(dev, blk, c);
  10226. + bi->pagesInUse++;
  10227. +
  10228. + oh = NULL;
  10229. + in = NULL;
  10230. +
  10231. + if (tags.extraHeaderInfoAvailable) {
  10232. + in = yaffs_FindOrCreateObjectByNumber
  10233. + (dev, tags.objectId,
  10234. + tags.extraObjectType);
  10235. + if (!in)
  10236. + alloc_failed = 1;
  10237. + }
  10238. +
  10239. + if (!in ||
  10240. +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  10241. + !in->valid ||
  10242. +#endif
  10243. + tags.extraShadows ||
  10244. + (!in->valid &&
  10245. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  10246. + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
  10247. +
  10248. + /* If we don't have valid info then we need to read the chunk
  10249. + * TODO In future we can probably defer reading the chunk and
  10250. + * living with invalid data until needed.
  10251. + */
  10252. +
  10253. + result = yaffs_ReadChunkWithTagsFromNAND(dev,
  10254. + chunk,
  10255. + chunkData,
  10256. + NULL);
  10257. +
  10258. + oh = (yaffs_ObjectHeader *) chunkData;
  10259. +
  10260. + if (dev->inbandTags) {
  10261. + /* Fix up the header if they got corrupted by inband tags */
  10262. + oh->shadowsObject = oh->inbandShadowsObject;
  10263. + oh->isShrink = oh->inbandIsShrink;
  10264. + }
  10265. +
  10266. + if (!in) {
  10267. + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
  10268. + if (!in)
  10269. + alloc_failed = 1;
  10270. + }
  10271. +
  10272. + }
  10273. +
  10274. + if (!in) {
  10275. + /* TODO Hoosterman we have a problem! */
  10276. + T(YAFFS_TRACE_ERROR,
  10277. + (TSTR
  10278. + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
  10279. + TENDSTR), tags.objectId, chunk));
  10280. + continue;
  10281. + }
  10282. +
  10283. + if (in->valid) {
  10284. + /* We have already filled this one.
  10285. + * We have a duplicate that will be discarded, but
  10286. + * we first have to suck out resize info if it is a file.
  10287. + */
  10288. +
  10289. + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
  10290. + ((oh &&
  10291. + oh->type == YAFFS_OBJECT_TYPE_FILE) ||
  10292. + (tags.extraHeaderInfoAvailable &&
  10293. + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
  10294. + __u32 thisSize =
  10295. + (oh) ? oh->fileSize : tags.
  10296. + extraFileLength;
  10297. + __u32 parentObjectId =
  10298. + (oh) ? oh->
  10299. + parentObjectId : tags.
  10300. + extraParentObjectId;
  10301. +
  10302. +
  10303. + isShrink =
  10304. + (oh) ? oh->isShrink : tags.
  10305. + extraIsShrinkHeader;
  10306. +
  10307. + /* If it is deleted (unlinked at start also means deleted)
  10308. + * we treat the file size as being zeroed at this point.
  10309. + */
  10310. + if (parentObjectId ==
  10311. + YAFFS_OBJECTID_DELETED
  10312. + || parentObjectId ==
  10313. + YAFFS_OBJECTID_UNLINKED) {
  10314. + thisSize = 0;
  10315. + isShrink = 1;
  10316. + }
  10317. +
  10318. + if (isShrink &&
  10319. + in->variant.fileVariant.
  10320. + shrinkSize > thisSize) {
  10321. + in->variant.fileVariant.
  10322. + shrinkSize =
  10323. + thisSize;
  10324. + }
  10325. +
  10326. + if (isShrink)
  10327. + bi->hasShrinkHeader = 1;
  10328. +
  10329. + }
  10330. + /* Use existing - destroy this one. */
  10331. + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
  10332. +
  10333. + }
  10334. +
  10335. + if (!in->valid && in->variantType !=
  10336. + (oh ? oh->type : tags.extraObjectType))
  10337. + T(YAFFS_TRACE_ERROR, (
  10338. + TSTR("yaffs tragedy: Bad object type, "
  10339. + TCONT("%d != %d, for object %d at chunk ")
  10340. + TCONT("%d during scan")
  10341. + TENDSTR), oh ?
  10342. + oh->type : tags.extraObjectType,
  10343. + in->variantType, tags.objectId,
  10344. + chunk));
  10345. +
  10346. + if (!in->valid &&
  10347. + (tags.objectId == YAFFS_OBJECTID_ROOT ||
  10348. + tags.objectId ==
  10349. + YAFFS_OBJECTID_LOSTNFOUND)) {
  10350. + /* We only load some info, don't fiddle with directory structure */
  10351. + in->valid = 1;
  10352. +
  10353. + if (oh) {
  10354. + in->variantType = oh->type;
  10355. +
  10356. + in->yst_mode = oh->yst_mode;
  10357. +#ifdef CONFIG_YAFFS_WINCE
  10358. + in->win_atime[0] = oh->win_atime[0];
  10359. + in->win_ctime[0] = oh->win_ctime[0];
  10360. + in->win_mtime[0] = oh->win_mtime[0];
  10361. + in->win_atime[1] = oh->win_atime[1];
  10362. + in->win_ctime[1] = oh->win_ctime[1];
  10363. + in->win_mtime[1] = oh->win_mtime[1];
  10364. +#else
  10365. + in->yst_uid = oh->yst_uid;
  10366. + in->yst_gid = oh->yst_gid;
  10367. + in->yst_atime = oh->yst_atime;
  10368. + in->yst_mtime = oh->yst_mtime;
  10369. + in->yst_ctime = oh->yst_ctime;
  10370. + in->yst_rdev = oh->yst_rdev;
  10371. +
  10372. +#endif
  10373. + } else {
  10374. + in->variantType = tags.extraObjectType;
  10375. + in->lazyLoaded = 1;
  10376. + }
  10377. +
  10378. + in->hdrChunk = chunk;
  10379. +
  10380. + } else if (!in->valid) {
  10381. + /* we need to load this info */
  10382. +
  10383. + in->valid = 1;
  10384. + in->hdrChunk = chunk;
  10385. +
  10386. + if (oh) {
  10387. + in->variantType = oh->type;
  10388. +
  10389. + in->yst_mode = oh->yst_mode;
  10390. +#ifdef CONFIG_YAFFS_WINCE
  10391. + in->win_atime[0] = oh->win_atime[0];
  10392. + in->win_ctime[0] = oh->win_ctime[0];
  10393. + in->win_mtime[0] = oh->win_mtime[0];
  10394. + in->win_atime[1] = oh->win_atime[1];
  10395. + in->win_ctime[1] = oh->win_ctime[1];
  10396. + in->win_mtime[1] = oh->win_mtime[1];
  10397. +#else
  10398. + in->yst_uid = oh->yst_uid;
  10399. + in->yst_gid = oh->yst_gid;
  10400. + in->yst_atime = oh->yst_atime;
  10401. + in->yst_mtime = oh->yst_mtime;
  10402. + in->yst_ctime = oh->yst_ctime;
  10403. + in->yst_rdev = oh->yst_rdev;
  10404. +#endif
  10405. +
  10406. + if (oh->shadowsObject > 0)
  10407. + yaffs_HandleShadowedObject(dev,
  10408. + oh->
  10409. + shadowsObject,
  10410. + 1);
  10411. +
  10412. +
  10413. + yaffs_SetObjectName(in, oh->name);
  10414. + parent =
  10415. + yaffs_FindOrCreateObjectByNumber
  10416. + (dev, oh->parentObjectId,
  10417. + YAFFS_OBJECT_TYPE_DIRECTORY);
  10418. +
  10419. + fileSize = oh->fileSize;
  10420. + isShrink = oh->isShrink;
  10421. + equivalentObjectId = oh->equivalentObjectId;
  10422. +
  10423. + } else {
  10424. + in->variantType = tags.extraObjectType;
  10425. + parent =
  10426. + yaffs_FindOrCreateObjectByNumber
  10427. + (dev, tags.extraParentObjectId,
  10428. + YAFFS_OBJECT_TYPE_DIRECTORY);
  10429. + fileSize = tags.extraFileLength;
  10430. + isShrink = tags.extraIsShrinkHeader;
  10431. + equivalentObjectId = tags.extraEquivalentObjectId;
  10432. + in->lazyLoaded = 1;
  10433. +
  10434. + }
  10435. + in->dirty = 0;
  10436. +
  10437. + if (!parent)
  10438. + alloc_failed = 1;
  10439. +
  10440. + /* directory stuff...
  10441. + * hook up to parent
  10442. + */
  10443. +
  10444. + if (parent && parent->variantType ==
  10445. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  10446. + /* Set up as a directory */
  10447. + parent->variantType =
  10448. + YAFFS_OBJECT_TYPE_DIRECTORY;
  10449. + YINIT_LIST_HEAD(&parent->variant.
  10450. + directoryVariant.
  10451. + children);
  10452. + } else if (!parent || parent->variantType !=
  10453. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  10454. + /* Hoosterman, another problem....
  10455. + * We're trying to use a non-directory as a directory
  10456. + */
  10457. +
  10458. + T(YAFFS_TRACE_ERROR,
  10459. + (TSTR
  10460. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  10461. + TENDSTR)));
  10462. + parent = dev->lostNFoundDir;
  10463. + }
  10464. +
  10465. + yaffs_AddObjectToDirectory(parent, in);
  10466. +
  10467. + itsUnlinked = (parent == dev->deletedDir) ||
  10468. + (parent == dev->unlinkedDir);
  10469. +
  10470. + if (isShrink) {
  10471. + /* Mark the block as having a shrinkHeader */
  10472. + bi->hasShrinkHeader = 1;
  10473. + }
  10474. +
  10475. + /* Note re hardlinks.
  10476. + * Since we might scan a hardlink before its equivalent object is scanned
  10477. + * we put them all in a list.
  10478. + * After scanning is complete, we should have all the objects, so we run
  10479. + * through this list and fix up all the chains.
  10480. + */
  10481. +
  10482. + switch (in->variantType) {
  10483. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  10484. + /* Todo got a problem */
  10485. + break;
  10486. + case YAFFS_OBJECT_TYPE_FILE:
  10487. +
  10488. + if (in->variant.fileVariant.
  10489. + scannedFileSize < fileSize) {
  10490. + /* This covers the case where the file size is greater
  10491. + * than where the data is
  10492. + * This will happen if the file is resized to be larger
  10493. + * than its current data extents.
  10494. + */
  10495. + in->variant.fileVariant.fileSize = fileSize;
  10496. + in->variant.fileVariant.scannedFileSize =
  10497. + in->variant.fileVariant.fileSize;
  10498. + }
  10499. +
  10500. + if (isShrink &&
  10501. + in->variant.fileVariant.shrinkSize > fileSize) {
  10502. + in->variant.fileVariant.shrinkSize = fileSize;
  10503. + }
  10504. +
  10505. + break;
  10506. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10507. + if (!itsUnlinked) {
  10508. + in->variant.hardLinkVariant.equivalentObjectId =
  10509. + equivalentObjectId;
  10510. + in->hardLinks.next =
  10511. + (struct ylist_head *) hardList;
  10512. + hardList = in;
  10513. + }
  10514. + break;
  10515. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10516. + /* Do nothing */
  10517. + break;
  10518. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10519. + /* Do nothing */
  10520. + break;
  10521. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10522. + if (oh) {
  10523. + in->variant.symLinkVariant.alias =
  10524. + yaffs_CloneString(oh->alias);
  10525. + if (!in->variant.symLinkVariant.alias)
  10526. + alloc_failed = 1;
  10527. + }
  10528. + break;
  10529. + }
  10530. +
  10531. + }
  10532. +
  10533. + }
  10534. +
  10535. + } /* End of scanning for each chunk */
  10536. +
  10537. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  10538. + /* If we got this far while scanning, then the block is fully allocated. */
  10539. + state = YAFFS_BLOCK_STATE_FULL;
  10540. + }
  10541. +
  10542. + bi->blockState = state;
  10543. +
  10544. + /* Now let's see if it was dirty */
  10545. + if (bi->pagesInUse == 0 &&
  10546. + !bi->hasShrinkHeader &&
  10547. + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
  10548. + yaffs_BlockBecameDirty(dev, blk);
  10549. + }
  10550. +
  10551. + }
  10552. +
  10553. + if (altBlockIndex)
  10554. + YFREE_ALT(blockIndex);
  10555. + else
  10556. + YFREE(blockIndex);
  10557. +
  10558. + /* Ok, we've done all the scanning.
  10559. + * Fix up the hard link chains.
  10560. + * We should now have scanned all the objects, now it's time to add these
  10561. + * hardlinks.
  10562. + */
  10563. + yaffs_HardlinkFixup(dev, hardList);
  10564. +
  10565. +
  10566. + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
  10567. +
  10568. + if (alloc_failed)
  10569. + return YAFFS_FAIL;
  10570. +
  10571. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
  10572. +
  10573. + return YAFFS_OK;
  10574. +}
  10575. +
  10576. +/*------------------------------ Directory Functions ----------------------------- */
  10577. +
  10578. +static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
  10579. +{
  10580. + struct ylist_head *lh;
  10581. + yaffs_Object *listObj;
  10582. +
  10583. + int count = 0;
  10584. +
  10585. + if (!obj) {
  10586. + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
  10587. + YBUG();
  10588. + return;
  10589. + }
  10590. +
  10591. + if (yaffs_SkipVerification(obj->myDev))
  10592. + return;
  10593. +
  10594. + if (!obj->parent) {
  10595. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
  10596. + YBUG();
  10597. + return;
  10598. + }
  10599. +
  10600. + if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10601. + T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
  10602. + YBUG();
  10603. + }
  10604. +
  10605. + /* Iterate through the objects in each hash entry */
  10606. +
  10607. + ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
  10608. + if (lh) {
  10609. + listObj = ylist_entry(lh, yaffs_Object, siblings);
  10610. + yaffs_VerifyObject(listObj);
  10611. + if (obj == listObj)
  10612. + count++;
  10613. + }
  10614. + }
  10615. +
  10616. + if (count != 1) {
  10617. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
  10618. + YBUG();
  10619. + }
  10620. +}
  10621. +
  10622. +static void yaffs_VerifyDirectory(yaffs_Object *directory)
  10623. +{
  10624. + struct ylist_head *lh;
  10625. + yaffs_Object *listObj;
  10626. +
  10627. + if (!directory) {
  10628. + YBUG();
  10629. + return;
  10630. + }
  10631. +
  10632. + if (yaffs_SkipFullVerification(directory->myDev))
  10633. + return;
  10634. +
  10635. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10636. + T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
  10637. + YBUG();
  10638. + }
  10639. +
  10640. + /* Iterate through the objects in each hash entry */
  10641. +
  10642. + ylist_for_each(lh, &directory->variant.directoryVariant.children) {
  10643. + if (lh) {
  10644. + listObj = ylist_entry(lh, yaffs_Object, siblings);
  10645. + if (listObj->parent != directory) {
  10646. + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
  10647. + YBUG();
  10648. + }
  10649. + yaffs_VerifyObjectInDirectory(listObj);
  10650. + }
  10651. + }
  10652. +}
  10653. +
  10654. +
  10655. +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
  10656. +{
  10657. + yaffs_Device *dev = obj->myDev;
  10658. + yaffs_Object *parent;
  10659. +
  10660. + yaffs_VerifyObjectInDirectory(obj);
  10661. + parent = obj->parent;
  10662. +
  10663. + yaffs_VerifyDirectory(parent);
  10664. +
  10665. + if (dev && dev->removeObjectCallback)
  10666. + dev->removeObjectCallback(obj);
  10667. +
  10668. +
  10669. + ylist_del_init(&obj->siblings);
  10670. + obj->parent = NULL;
  10671. +
  10672. + yaffs_VerifyDirectory(parent);
  10673. +}
  10674. +
  10675. +
  10676. +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
  10677. + yaffs_Object *obj)
  10678. +{
  10679. + if (!directory) {
  10680. + T(YAFFS_TRACE_ALWAYS,
  10681. + (TSTR
  10682. + ("tragedy: Trying to add an object to a null pointer directory"
  10683. + TENDSTR)));
  10684. + YBUG();
  10685. + return;
  10686. + }
  10687. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10688. + T(YAFFS_TRACE_ALWAYS,
  10689. + (TSTR
  10690. + ("tragedy: Trying to add an object to a non-directory"
  10691. + TENDSTR)));
  10692. + YBUG();
  10693. + }
  10694. +
  10695. + if (obj->siblings.prev == NULL) {
  10696. + /* Not initialised */
  10697. + YBUG();
  10698. + }
  10699. +
  10700. +
  10701. + yaffs_VerifyDirectory(directory);
  10702. +
  10703. + yaffs_RemoveObjectFromDirectory(obj);
  10704. +
  10705. +
  10706. + /* Now add it */
  10707. + ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
  10708. + obj->parent = directory;
  10709. +
  10710. + if (directory == obj->myDev->unlinkedDir
  10711. + || directory == obj->myDev->deletedDir) {
  10712. + obj->unlinked = 1;
  10713. + obj->myDev->nUnlinkedFiles++;
  10714. + obj->renameAllowed = 0;
  10715. + }
  10716. +
  10717. + yaffs_VerifyDirectory(directory);
  10718. + yaffs_VerifyObjectInDirectory(obj);
  10719. +}
  10720. +
  10721. +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
  10722. + const YCHAR *name)
  10723. +{
  10724. + int sum;
  10725. +
  10726. + struct ylist_head *i;
  10727. + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  10728. +
  10729. + yaffs_Object *l;
  10730. +
  10731. + if (!name)
  10732. + return NULL;
  10733. +
  10734. + if (!directory) {
  10735. + T(YAFFS_TRACE_ALWAYS,
  10736. + (TSTR
  10737. + ("tragedy: yaffs_FindObjectByName: null pointer directory"
  10738. + TENDSTR)));
  10739. + YBUG();
  10740. + return NULL;
  10741. + }
  10742. + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10743. + T(YAFFS_TRACE_ALWAYS,
  10744. + (TSTR
  10745. + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  10746. + YBUG();
  10747. + }
  10748. +
  10749. + sum = yaffs_CalcNameSum(name);
  10750. +
  10751. + ylist_for_each(i, &directory->variant.directoryVariant.children) {
  10752. + if (i) {
  10753. + l = ylist_entry(i, yaffs_Object, siblings);
  10754. +
  10755. + if (l->parent != directory)
  10756. + YBUG();
  10757. +
  10758. + yaffs_CheckObjectDetailsLoaded(l);
  10759. +
  10760. + /* Special case for lost-n-found */
  10761. + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  10762. + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
  10763. + return l;
  10764. + } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
  10765. + /* LostnFound chunk called Objxxx
  10766. + * Do a real check
  10767. + */
  10768. + yaffs_GetObjectName(l, buffer,
  10769. + YAFFS_MAX_NAME_LENGTH);
  10770. + if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
  10771. + return l;
  10772. + }
  10773. + }
  10774. + }
  10775. +
  10776. + return NULL;
  10777. +}
  10778. +
  10779. +
  10780. +#if 0
  10781. +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
  10782. + int (*fn) (yaffs_Object *))
  10783. +{
  10784. + struct ylist_head *i;
  10785. + yaffs_Object *l;
  10786. +
  10787. + if (!theDir) {
  10788. + T(YAFFS_TRACE_ALWAYS,
  10789. + (TSTR
  10790. + ("tragedy: yaffs_FindObjectByName: null pointer directory"
  10791. + TENDSTR)));
  10792. + YBUG();
  10793. + return YAFFS_FAIL;
  10794. + }
  10795. + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10796. + T(YAFFS_TRACE_ALWAYS,
  10797. + (TSTR
  10798. + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
  10799. + YBUG();
  10800. + return YAFFS_FAIL;
  10801. + }
  10802. +
  10803. + ylist_for_each(i, &theDir->variant.directoryVariant.children) {
  10804. + if (i) {
  10805. + l = ylist_entry(i, yaffs_Object, siblings);
  10806. + if (l && !fn(l))
  10807. + return YAFFS_FAIL;
  10808. + }
  10809. + }
  10810. +
  10811. + return YAFFS_OK;
  10812. +
  10813. +}
  10814. +#endif
  10815. +
  10816. +/* GetEquivalentObject dereferences any hard links to get to the
  10817. + * actual object.
  10818. + */
  10819. +
  10820. +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
  10821. +{
  10822. + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
  10823. + /* We want the object id of the equivalent object, not this one */
  10824. + obj = obj->variant.hardLinkVariant.equivalentObject;
  10825. + yaffs_CheckObjectDetailsLoaded(obj);
  10826. + }
  10827. + return obj;
  10828. +}
  10829. +
  10830. +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
  10831. +{
  10832. + memset(name, 0, buffSize * sizeof(YCHAR));
  10833. +
  10834. + yaffs_CheckObjectDetailsLoaded(obj);
  10835. +
  10836. + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
  10837. + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
  10838. + } else if (obj->hdrChunk <= 0) {
  10839. + YCHAR locName[20];
  10840. + YCHAR numString[20];
  10841. + YCHAR *x = &numString[19];
  10842. + unsigned v = obj->objectId;
  10843. + numString[19] = 0;
  10844. + while (v > 0) {
  10845. + x--;
  10846. + *x = '0' + (v % 10);
  10847. + v /= 10;
  10848. + }
  10849. + /* make up a name */
  10850. + yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
  10851. + yaffs_strcat(locName, x);
  10852. + yaffs_strncpy(name, locName, buffSize - 1);
  10853. +
  10854. + }
  10855. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  10856. + else if (obj->shortName[0])
  10857. + yaffs_strcpy(name, obj->shortName);
  10858. +#endif
  10859. + else {
  10860. + int result;
  10861. + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
  10862. +
  10863. + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
  10864. +
  10865. + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
  10866. +
  10867. + if (obj->hdrChunk > 0) {
  10868. + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
  10869. + obj->hdrChunk, buffer,
  10870. + NULL);
  10871. + }
  10872. + yaffs_strncpy(name, oh->name, buffSize - 1);
  10873. +
  10874. + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
  10875. + }
  10876. +
  10877. + return yaffs_strlen(name);
  10878. +}
  10879. +
  10880. +int yaffs_GetObjectFileLength(yaffs_Object *obj)
  10881. +{
  10882. + /* Dereference any hard linking */
  10883. + obj = yaffs_GetEquivalentObject(obj);
  10884. +
  10885. + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
  10886. + return obj->variant.fileVariant.fileSize;
  10887. + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
  10888. + return yaffs_strlen(obj->variant.symLinkVariant.alias);
  10889. + else {
  10890. + /* Only a directory should drop through to here */
  10891. + return obj->myDev->nDataBytesPerChunk;
  10892. + }
  10893. +}
  10894. +
  10895. +int yaffs_GetObjectLinkCount(yaffs_Object *obj)
  10896. +{
  10897. + int count = 0;
  10898. + struct ylist_head *i;
  10899. +
  10900. + if (!obj->unlinked)
  10901. + count++; /* the object itself */
  10902. +
  10903. + ylist_for_each(i, &obj->hardLinks)
  10904. + count++; /* add the hard links; */
  10905. +
  10906. + return count;
  10907. +}
  10908. +
  10909. +int yaffs_GetObjectInode(yaffs_Object *obj)
  10910. +{
  10911. + obj = yaffs_GetEquivalentObject(obj);
  10912. +
  10913. + return obj->objectId;
  10914. +}
  10915. +
  10916. +unsigned yaffs_GetObjectType(yaffs_Object *obj)
  10917. +{
  10918. + obj = yaffs_GetEquivalentObject(obj);
  10919. +
  10920. + switch (obj->variantType) {
  10921. + case YAFFS_OBJECT_TYPE_FILE:
  10922. + return DT_REG;
  10923. + break;
  10924. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10925. + return DT_DIR;
  10926. + break;
  10927. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10928. + return DT_LNK;
  10929. + break;
  10930. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10931. + return DT_REG;
  10932. + break;
  10933. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10934. + if (S_ISFIFO(obj->yst_mode))
  10935. + return DT_FIFO;
  10936. + if (S_ISCHR(obj->yst_mode))
  10937. + return DT_CHR;
  10938. + if (S_ISBLK(obj->yst_mode))
  10939. + return DT_BLK;
  10940. + if (S_ISSOCK(obj->yst_mode))
  10941. + return DT_SOCK;
  10942. + default:
  10943. + return DT_REG;
  10944. + break;
  10945. + }
  10946. +}
  10947. +
  10948. +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
  10949. +{
  10950. + obj = yaffs_GetEquivalentObject(obj);
  10951. + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
  10952. + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
  10953. + else
  10954. + return yaffs_CloneString(_Y(""));
  10955. +}
  10956. +
  10957. +#ifndef CONFIG_YAFFS_WINCE
  10958. +
  10959. +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
  10960. +{
  10961. + unsigned int valid = attr->ia_valid;
  10962. +
  10963. + if (valid & ATTR_MODE)
  10964. + obj->yst_mode = attr->ia_mode;
  10965. + if (valid & ATTR_UID)
  10966. + obj->yst_uid = attr->ia_uid;
  10967. + if (valid & ATTR_GID)
  10968. + obj->yst_gid = attr->ia_gid;
  10969. +
  10970. + if (valid & ATTR_ATIME)
  10971. + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  10972. + if (valid & ATTR_CTIME)
  10973. + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  10974. + if (valid & ATTR_MTIME)
  10975. + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  10976. +
  10977. + if (valid & ATTR_SIZE)
  10978. + yaffs_ResizeFile(obj, attr->ia_size);
  10979. +
  10980. + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
  10981. +
  10982. + return YAFFS_OK;
  10983. +
  10984. +}
  10985. +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
  10986. +{
  10987. + unsigned int valid = 0;
  10988. +
  10989. + attr->ia_mode = obj->yst_mode;
  10990. + valid |= ATTR_MODE;
  10991. + attr->ia_uid = obj->yst_uid;
  10992. + valid |= ATTR_UID;
  10993. + attr->ia_gid = obj->yst_gid;
  10994. + valid |= ATTR_GID;
  10995. +
  10996. + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  10997. + valid |= ATTR_ATIME;
  10998. + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  10999. + valid |= ATTR_CTIME;
  11000. + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  11001. + valid |= ATTR_MTIME;
  11002. +
  11003. + attr->ia_size = yaffs_GetFileSize(obj);
  11004. + valid |= ATTR_SIZE;
  11005. +
  11006. + attr->ia_valid = valid;
  11007. +
  11008. + return YAFFS_OK;
  11009. +}
  11010. +
  11011. +#endif
  11012. +
  11013. +#if 0
  11014. +int yaffs_DumpObject(yaffs_Object *obj)
  11015. +{
  11016. + YCHAR name[257];
  11017. +
  11018. + yaffs_GetObjectName(obj, name, 256);
  11019. +
  11020. + T(YAFFS_TRACE_ALWAYS,
  11021. + (TSTR
  11022. + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
  11023. + " chunk %d type %d size %d\n"
  11024. + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
  11025. + obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
  11026. + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
  11027. +
  11028. + return YAFFS_OK;
  11029. +}
  11030. +#endif
  11031. +
  11032. +/*---------------------------- Initialisation code -------------------------------------- */
  11033. +
  11034. +static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
  11035. +{
  11036. +
  11037. + /* Common functions, gotta have */
  11038. + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
  11039. + return 0;
  11040. +
  11041. +#ifdef CONFIG_YAFFS_YAFFS2
  11042. +
  11043. + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  11044. + if (dev->writeChunkWithTagsToNAND &&
  11045. + dev->readChunkWithTagsFromNAND &&
  11046. + !dev->writeChunkToNAND &&
  11047. + !dev->readChunkFromNAND &&
  11048. + dev->markNANDBlockBad && dev->queryNANDBlock)
  11049. + return 1;
  11050. +#endif
  11051. +
  11052. + /* Can use the "spare" style interface for yaffs1 */
  11053. + if (!dev->isYaffs2 &&
  11054. + !dev->writeChunkWithTagsToNAND &&
  11055. + !dev->readChunkWithTagsFromNAND &&
  11056. + dev->writeChunkToNAND &&
  11057. + dev->readChunkFromNAND &&
  11058. + !dev->markNANDBlockBad && !dev->queryNANDBlock)
  11059. + return 1;
  11060. +
  11061. + return 0; /* bad */
  11062. +}
  11063. +
  11064. +
  11065. +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
  11066. +{
  11067. + /* Initialise the unlinked, deleted, root and lost and found directories */
  11068. +
  11069. + dev->lostNFoundDir = dev->rootDir = NULL;
  11070. + dev->unlinkedDir = dev->deletedDir = NULL;
  11071. +
  11072. + dev->unlinkedDir =
  11073. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  11074. +
  11075. + dev->deletedDir =
  11076. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  11077. +
  11078. + dev->rootDir =
  11079. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
  11080. + YAFFS_ROOT_MODE | S_IFDIR);
  11081. + dev->lostNFoundDir =
  11082. + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
  11083. + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  11084. +
  11085. + if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
  11086. + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
  11087. + return YAFFS_OK;
  11088. + }
  11089. +
  11090. + return YAFFS_FAIL;
  11091. +}
  11092. +
  11093. +int yaffs_GutsInitialise(yaffs_Device *dev)
  11094. +{
  11095. + int init_failed = 0;
  11096. + unsigned x;
  11097. + int bits;
  11098. +
  11099. + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
  11100. +
  11101. + /* Check stuff that must be set */
  11102. +
  11103. + if (!dev) {
  11104. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
  11105. + return YAFFS_FAIL;
  11106. + }
  11107. +
  11108. + dev->internalStartBlock = dev->startBlock;
  11109. + dev->internalEndBlock = dev->endBlock;
  11110. + dev->blockOffset = 0;
  11111. + dev->chunkOffset = 0;
  11112. + dev->nFreeChunks = 0;
  11113. +
  11114. + dev->gcBlock = -1;
  11115. +
  11116. + if (dev->startBlock == 0) {
  11117. + dev->internalStartBlock = dev->startBlock + 1;
  11118. + dev->internalEndBlock = dev->endBlock + 1;
  11119. + dev->blockOffset = 1;
  11120. + dev->chunkOffset = dev->nChunksPerBlock;
  11121. + }
  11122. +
  11123. + /* Check geometry parameters. */
  11124. +
  11125. + if ((!dev->inbandTags && dev->isYaffs2 && dev->totalBytesPerChunk < 1024) ||
  11126. + (!dev->isYaffs2 && dev->totalBytesPerChunk < 512) ||
  11127. + (dev->inbandTags && !dev->isYaffs2) ||
  11128. + dev->nChunksPerBlock < 2 ||
  11129. + dev->nReservedBlocks < 2 ||
  11130. + dev->internalStartBlock <= 0 ||
  11131. + dev->internalEndBlock <= 0 ||
  11132. + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2)) { /* otherwise it is too small */
  11133. + T(YAFFS_TRACE_ALWAYS,
  11134. + (TSTR
  11135. + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
  11136. + TENDSTR), dev->totalBytesPerChunk, dev->isYaffs2 ? "2" : "", dev->inbandTags));
  11137. + return YAFFS_FAIL;
  11138. + }
  11139. +
  11140. + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
  11141. + T(YAFFS_TRACE_ALWAYS,
  11142. + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
  11143. + return YAFFS_FAIL;
  11144. + }
  11145. +
  11146. + /* Sort out space for inband tags, if required */
  11147. + if (dev->inbandTags)
  11148. + dev->nDataBytesPerChunk = dev->totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
  11149. + else
  11150. + dev->nDataBytesPerChunk = dev->totalBytesPerChunk;
  11151. +
  11152. + /* Got the right mix of functions? */
  11153. + if (!yaffs_CheckDevFunctions(dev)) {
  11154. + /* Function missing */
  11155. + T(YAFFS_TRACE_ALWAYS,
  11156. + (TSTR
  11157. + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
  11158. +
  11159. + return YAFFS_FAIL;
  11160. + }
  11161. +
  11162. + /* This is really a compilation check. */
  11163. + if (!yaffs_CheckStructures()) {
  11164. + T(YAFFS_TRACE_ALWAYS,
  11165. + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
  11166. + return YAFFS_FAIL;
  11167. + }
  11168. +
  11169. + if (dev->isMounted) {
  11170. + T(YAFFS_TRACE_ALWAYS,
  11171. + (TSTR("yaffs: device already mounted\n" TENDSTR)));
  11172. + return YAFFS_FAIL;
  11173. + }
  11174. +
  11175. + /* Finished with most checks. One or two more checks happen later on too. */
  11176. +
  11177. + dev->isMounted = 1;
  11178. +
  11179. + /* OK now calculate a few things for the device */
  11180. +
  11181. + /*
  11182. + * Calculate all the chunk size manipulation numbers:
  11183. + */
  11184. + x = dev->nDataBytesPerChunk;
  11185. + /* We always use dev->chunkShift and dev->chunkDiv */
  11186. + dev->chunkShift = Shifts(x);
  11187. + x >>= dev->chunkShift;
  11188. + dev->chunkDiv = x;
  11189. + /* We only use chunk mask if chunkDiv is 1 */
  11190. + dev->chunkMask = (1<<dev->chunkShift) - 1;
  11191. +
  11192. + /*
  11193. + * Calculate chunkGroupBits.
  11194. + * We need to find the next power of 2 > than internalEndBlock
  11195. + */
  11196. +
  11197. + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
  11198. +
  11199. + bits = ShiftsGE(x);
  11200. +
  11201. + /* Set up tnode width if wide tnodes are enabled. */
  11202. + if (!dev->wideTnodesDisabled) {
  11203. + /* bits must be even so that we end up with 32-bit words */
  11204. + if (bits & 1)
  11205. + bits++;
  11206. + if (bits < 16)
  11207. + dev->tnodeWidth = 16;
  11208. + else
  11209. + dev->tnodeWidth = bits;
  11210. + } else
  11211. + dev->tnodeWidth = 16;
  11212. +
  11213. + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
  11214. +
  11215. + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  11216. + * so if the bitwidth of the
  11217. + * chunk range we're using is greater than 16 we need
  11218. + * to figure out chunk shift and chunkGroupSize
  11219. + */
  11220. +
  11221. + if (bits <= dev->tnodeWidth)
  11222. + dev->chunkGroupBits = 0;
  11223. + else
  11224. + dev->chunkGroupBits = bits - dev->tnodeWidth;
  11225. +
  11226. +
  11227. + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
  11228. +
  11229. + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
  11230. + /* We have a problem because the soft delete won't work if
  11231. + * the chunk group size > chunks per block.
  11232. + * This can be remedied by using larger "virtual blocks".
  11233. + */
  11234. + T(YAFFS_TRACE_ALWAYS,
  11235. + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
  11236. +
  11237. + return YAFFS_FAIL;
  11238. + }
  11239. +
  11240. + /* OK, we've finished verifying the device, lets continue with initialisation */
  11241. +
  11242. + /* More device initialisation */
  11243. + dev->garbageCollections = 0;
  11244. + dev->passiveGarbageCollections = 0;
  11245. + dev->currentDirtyChecker = 0;
  11246. + dev->bufferedBlock = -1;
  11247. + dev->doingBufferedBlockRewrite = 0;
  11248. + dev->nDeletedFiles = 0;
  11249. + dev->nBackgroundDeletions = 0;
  11250. + dev->nUnlinkedFiles = 0;
  11251. + dev->eccFixed = 0;
  11252. + dev->eccUnfixed = 0;
  11253. + dev->tagsEccFixed = 0;
  11254. + dev->tagsEccUnfixed = 0;
  11255. + dev->nErasureFailures = 0;
  11256. + dev->nErasedBlocks = 0;
  11257. + dev->isDoingGC = 0;
  11258. + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
  11259. +
  11260. + /* Initialise temporary buffers and caches. */
  11261. + if (!yaffs_InitialiseTempBuffers(dev))
  11262. + init_failed = 1;
  11263. +
  11264. + dev->srCache = NULL;
  11265. + dev->gcCleanupList = NULL;
  11266. +
  11267. +
  11268. + if (!init_failed &&
  11269. + dev->nShortOpCaches > 0) {
  11270. + int i;
  11271. + void *buf;
  11272. + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
  11273. +
  11274. + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
  11275. + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
  11276. +
  11277. + dev->srCache = YMALLOC(srCacheBytes);
  11278. +
  11279. + buf = (__u8 *) dev->srCache;
  11280. +
  11281. + if (dev->srCache)
  11282. + memset(dev->srCache, 0, srCacheBytes);
  11283. +
  11284. + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
  11285. + dev->srCache[i].object = NULL;
  11286. + dev->srCache[i].lastUse = 0;
  11287. + dev->srCache[i].dirty = 0;
  11288. + dev->srCache[i].data = buf = YMALLOC_DMA(dev->totalBytesPerChunk);
  11289. + }
  11290. + if (!buf)
  11291. + init_failed = 1;
  11292. +
  11293. + dev->srLastUse = 0;
  11294. + }
  11295. +
  11296. + dev->cacheHits = 0;
  11297. +
  11298. + if (!init_failed) {
  11299. + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
  11300. + if (!dev->gcCleanupList)
  11301. + init_failed = 1;
  11302. + }
  11303. +
  11304. + if (dev->isYaffs2)
  11305. + dev->useHeaderFileSize = 1;
  11306. +
  11307. + if (!init_failed && !yaffs_InitialiseBlocks(dev))
  11308. + init_failed = 1;
  11309. +
  11310. + yaffs_InitialiseTnodes(dev);
  11311. + yaffs_InitialiseObjects(dev);
  11312. +
  11313. + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
  11314. + init_failed = 1;
  11315. +
  11316. +
  11317. + if (!init_failed) {
  11318. + /* Now scan the flash. */
  11319. + if (dev->isYaffs2) {
  11320. + if (yaffs_CheckpointRestore(dev)) {
  11321. + yaffs_CheckObjectDetailsLoaded(dev->rootDir);
  11322. + T(YAFFS_TRACE_ALWAYS,
  11323. + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
  11324. + } else {
  11325. +
  11326. + /* Clean up the mess caused by an aborted checkpoint load
  11327. + * and scan backwards.
  11328. + */
  11329. + yaffs_DeinitialiseBlocks(dev);
  11330. + yaffs_DeinitialiseTnodes(dev);
  11331. + yaffs_DeinitialiseObjects(dev);
  11332. +
  11333. +
  11334. + dev->nErasedBlocks = 0;
  11335. + dev->nFreeChunks = 0;
  11336. + dev->allocationBlock = -1;
  11337. + dev->allocationPage = -1;
  11338. + dev->nDeletedFiles = 0;
  11339. + dev->nUnlinkedFiles = 0;
  11340. + dev->nBackgroundDeletions = 0;
  11341. + dev->oldestDirtySequence = 0;
  11342. +
  11343. + if (!init_failed && !yaffs_InitialiseBlocks(dev))
  11344. + init_failed = 1;
  11345. +
  11346. + yaffs_InitialiseTnodes(dev);
  11347. + yaffs_InitialiseObjects(dev);
  11348. +
  11349. + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
  11350. + init_failed = 1;
  11351. +
  11352. + if (!init_failed && !yaffs_ScanBackwards(dev))
  11353. + init_failed = 1;
  11354. + }
  11355. + } else if (!yaffs_Scan(dev))
  11356. + init_failed = 1;
  11357. +
  11358. + yaffs_StripDeletedObjects(dev);
  11359. + }
  11360. +
  11361. + if (init_failed) {
  11362. + /* Clean up the mess */
  11363. + T(YAFFS_TRACE_TRACING,
  11364. + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
  11365. +
  11366. + yaffs_Deinitialise(dev);
  11367. + return YAFFS_FAIL;
  11368. + }
  11369. +
  11370. + /* Zero out stats */
  11371. + dev->nPageReads = 0;
  11372. + dev->nPageWrites = 0;
  11373. + dev->nBlockErasures = 0;
  11374. + dev->nGCCopies = 0;
  11375. + dev->nRetriedWrites = 0;
  11376. +
  11377. + dev->nRetiredBlocks = 0;
  11378. +
  11379. + yaffs_VerifyFreeChunks(dev);
  11380. + yaffs_VerifyBlocks(dev);
  11381. +
  11382. +
  11383. + T(YAFFS_TRACE_TRACING,
  11384. + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
  11385. + return YAFFS_OK;
  11386. +
  11387. +}
  11388. +
  11389. +void yaffs_Deinitialise(yaffs_Device *dev)
  11390. +{
  11391. + if (dev->isMounted) {
  11392. + int i;
  11393. +
  11394. + yaffs_DeinitialiseBlocks(dev);
  11395. + yaffs_DeinitialiseTnodes(dev);
  11396. + yaffs_DeinitialiseObjects(dev);
  11397. + if (dev->nShortOpCaches > 0 &&
  11398. + dev->srCache) {
  11399. +
  11400. + for (i = 0; i < dev->nShortOpCaches; i++) {
  11401. + if (dev->srCache[i].data)
  11402. + YFREE(dev->srCache[i].data);
  11403. + dev->srCache[i].data = NULL;
  11404. + }
  11405. +
  11406. + YFREE(dev->srCache);
  11407. + dev->srCache = NULL;
  11408. + }
  11409. +
  11410. + YFREE(dev->gcCleanupList);
  11411. +
  11412. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  11413. + YFREE(dev->tempBuffer[i].buffer);
  11414. +
  11415. + dev->isMounted = 0;
  11416. +
  11417. + if (dev->deinitialiseNAND)
  11418. + dev->deinitialiseNAND(dev);
  11419. + }
  11420. +}
  11421. +
  11422. +static int yaffs_CountFreeChunks(yaffs_Device *dev)
  11423. +{
  11424. + int nFree;
  11425. + int b;
  11426. +
  11427. + yaffs_BlockInfo *blk;
  11428. +
  11429. + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
  11430. + b++) {
  11431. + blk = yaffs_GetBlockInfo(dev, b);
  11432. +
  11433. + switch (blk->blockState) {
  11434. + case YAFFS_BLOCK_STATE_EMPTY:
  11435. + case YAFFS_BLOCK_STATE_ALLOCATING:
  11436. + case YAFFS_BLOCK_STATE_COLLECTING:
  11437. + case YAFFS_BLOCK_STATE_FULL:
  11438. + nFree +=
  11439. + (dev->nChunksPerBlock - blk->pagesInUse +
  11440. + blk->softDeletions);
  11441. + break;
  11442. + default:
  11443. + break;
  11444. + }
  11445. + }
  11446. +
  11447. + return nFree;
  11448. +}
  11449. +
  11450. +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
  11451. +{
  11452. + /* This is what we report to the outside world */
  11453. +
  11454. + int nFree;
  11455. + int nDirtyCacheChunks;
  11456. + int blocksForCheckpoint;
  11457. + int i;
  11458. +
  11459. +#if 1
  11460. + nFree = dev->nFreeChunks;
  11461. +#else
  11462. + nFree = yaffs_CountFreeChunks(dev);
  11463. +#endif
  11464. +
  11465. + nFree += dev->nDeletedFiles;
  11466. +
  11467. + /* Now count the number of dirty chunks in the cache and subtract those */
  11468. +
  11469. + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
  11470. + if (dev->srCache[i].dirty)
  11471. + nDirtyCacheChunks++;
  11472. + }
  11473. +
  11474. + nFree -= nDirtyCacheChunks;
  11475. +
  11476. + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
  11477. +
  11478. + /* Now we figure out how much to reserve for the checkpoint and report that... */
  11479. + blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
  11480. + if (blocksForCheckpoint < 0)
  11481. + blocksForCheckpoint = 0;
  11482. +
  11483. + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
  11484. +
  11485. + if (nFree < 0)
  11486. + nFree = 0;
  11487. +
  11488. + return nFree;
  11489. +
  11490. +}
  11491. +
  11492. +static int yaffs_freeVerificationFailures;
  11493. +
  11494. +static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
  11495. +{
  11496. + int counted;
  11497. + int difference;
  11498. +
  11499. + if (yaffs_SkipVerification(dev))
  11500. + return;
  11501. +
  11502. + counted = yaffs_CountFreeChunks(dev);
  11503. +
  11504. + difference = dev->nFreeChunks - counted;
  11505. +
  11506. + if (difference) {
  11507. + T(YAFFS_TRACE_ALWAYS,
  11508. + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
  11509. + dev->nFreeChunks, counted, difference));
  11510. + yaffs_freeVerificationFailures++;
  11511. + }
  11512. +}
  11513. +
  11514. +/*---------------------------------------- YAFFS test code ----------------------*/
  11515. +
  11516. +#define yaffs_CheckStruct(structure, syze, name) \
  11517. + do { \
  11518. + if (sizeof(structure) != syze) { \
  11519. + T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
  11520. + name, syze, sizeof(structure))); \
  11521. + return YAFFS_FAIL; \
  11522. + } \
  11523. + } while (0)
  11524. +
  11525. +static int yaffs_CheckStructures(void)
  11526. +{
  11527. +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
  11528. +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
  11529. +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
  11530. +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
  11531. + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode");
  11532. +#endif
  11533. +#ifndef CONFIG_YAFFS_WINCE
  11534. + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");
  11535. +#endif
  11536. + return YAFFS_OK;
  11537. +}
  11538. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_guts.h linux-2.6.32/fs/yaffs2/yaffs_guts.h
  11539. --- linux-2.6.32.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  11540. +++ linux-2.6.32/fs/yaffs2/yaffs_guts.h 2010-01-30 20:35:01.441990129 +0100
  11541. @@ -0,0 +1,904 @@
  11542. +/*
  11543. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  11544. + *
  11545. + * Copyright (C) 2002-2007 Aleph One Ltd.
  11546. + * for Toby Churchill Ltd and Brightstar Engineering
  11547. + *
  11548. + * Created by Charles Manning <charles@aleph1.co.uk>
  11549. + *
  11550. + * This program is free software; you can redistribute it and/or modify
  11551. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  11552. + * published by the Free Software Foundation.
  11553. + *
  11554. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  11555. + */
  11556. +
  11557. +#ifndef __YAFFS_GUTS_H__
  11558. +#define __YAFFS_GUTS_H__
  11559. +
  11560. +#include "devextras.h"
  11561. +#include "yportenv.h"
  11562. +
  11563. +#define YAFFS_OK 1
  11564. +#define YAFFS_FAIL 0
  11565. +
  11566. +/* Give us a Y=0x59,
  11567. + * Give us an A=0x41,
  11568. + * Give us an FF=0xFF
  11569. + * Give us an S=0x53
  11570. + * And what have we got...
  11571. + */
  11572. +#define YAFFS_MAGIC 0x5941FF53
  11573. +
  11574. +#define YAFFS_NTNODES_LEVEL0 16
  11575. +#define YAFFS_TNODES_LEVEL0_BITS 4
  11576. +#define YAFFS_TNODES_LEVEL0_MASK 0xf
  11577. +
  11578. +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  11579. +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  11580. +#define YAFFS_TNODES_INTERNAL_MASK 0x7
  11581. +#define YAFFS_TNODES_MAX_LEVEL 6
  11582. +
  11583. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  11584. +#define YAFFS_BYTES_PER_SPARE 16
  11585. +#define YAFFS_BYTES_PER_CHUNK 512
  11586. +#define YAFFS_CHUNK_SIZE_SHIFT 9
  11587. +#define YAFFS_CHUNKS_PER_BLOCK 32
  11588. +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  11589. +#endif
  11590. +
  11591. +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  11592. +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  11593. +
  11594. +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
  11595. +
  11596. +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
  11597. +
  11598. +#define YAFFS_ALLOCATION_NOBJECTS 100
  11599. +#define YAFFS_ALLOCATION_NTNODES 100
  11600. +#define YAFFS_ALLOCATION_NLINKS 100
  11601. +
  11602. +#define YAFFS_NOBJECT_BUCKETS 256
  11603. +
  11604. +
  11605. +#define YAFFS_OBJECT_SPACE 0x40000
  11606. +
  11607. +#define YAFFS_CHECKPOINT_VERSION 3
  11608. +
  11609. +#ifdef CONFIG_YAFFS_UNICODE
  11610. +#define YAFFS_MAX_NAME_LENGTH 127
  11611. +#define YAFFS_MAX_ALIAS_LENGTH 79
  11612. +#else
  11613. +#define YAFFS_MAX_NAME_LENGTH 255
  11614. +#define YAFFS_MAX_ALIAS_LENGTH 159
  11615. +#endif
  11616. +
  11617. +#define YAFFS_SHORT_NAME_LENGTH 15
  11618. +
  11619. +/* Some special object ids for pseudo objects */
  11620. +#define YAFFS_OBJECTID_ROOT 1
  11621. +#define YAFFS_OBJECTID_LOSTNFOUND 2
  11622. +#define YAFFS_OBJECTID_UNLINKED 3
  11623. +#define YAFFS_OBJECTID_DELETED 4
  11624. +
  11625. +/* Sseudo object ids for checkpointing */
  11626. +#define YAFFS_OBJECTID_SB_HEADER 0x10
  11627. +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  11628. +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  11629. +
  11630. +/* */
  11631. +
  11632. +#define YAFFS_MAX_SHORT_OP_CACHES 20
  11633. +
  11634. +#define YAFFS_N_TEMP_BUFFERS 6
  11635. +
  11636. +/* We limit the number attempts at sucessfully saving a chunk of data.
  11637. + * Small-page devices have 32 pages per block; large-page devices have 64.
  11638. + * Default to something in the order of 5 to 10 blocks worth of chunks.
  11639. + */
  11640. +#define YAFFS_WR_ATTEMPTS (5*64)
  11641. +
  11642. +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
  11643. + * The range is limited slightly to help distinguish bad numbers from good.
  11644. + * This also allows us to perhaps in the future use special numbers for
  11645. + * special purposes.
  11646. + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
  11647. + * and is a larger number than the lifetime of a 2GB device.
  11648. + */
  11649. +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  11650. +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
  11651. +
  11652. +/* Special sequence number for bad block that failed to be marked bad */
  11653. +#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
  11654. +
  11655. +/* ChunkCache is used for short read/write operations.*/
  11656. +typedef struct {
  11657. + struct yaffs_ObjectStruct *object;
  11658. + int chunkId;
  11659. + int lastUse;
  11660. + int dirty;
  11661. + int nBytes; /* Only valid if the cache is dirty */
  11662. + int locked; /* Can't push out or flush while locked. */
  11663. +#ifdef CONFIG_YAFFS_YAFFS2
  11664. + __u8 *data;
  11665. +#else
  11666. + __u8 data[YAFFS_BYTES_PER_CHUNK];
  11667. +#endif
  11668. +} yaffs_ChunkCache;
  11669. +
  11670. +
  11671. +
  11672. +/* Tags structures in RAM
  11673. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
  11674. + * the structure size will get blown out.
  11675. + */
  11676. +
  11677. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  11678. +typedef struct {
  11679. + unsigned chunkId:20;
  11680. + unsigned serialNumber:2;
  11681. + unsigned byteCountLSB:10;
  11682. + unsigned objectId:18;
  11683. + unsigned ecc:12;
  11684. + unsigned byteCountMSB:2;
  11685. +} yaffs_Tags;
  11686. +
  11687. +typedef union {
  11688. + yaffs_Tags asTags;
  11689. + __u8 asBytes[8];
  11690. +} yaffs_TagsUnion;
  11691. +
  11692. +#endif
  11693. +
  11694. +/* Stuff used for extended tags in YAFFS2 */
  11695. +
  11696. +typedef enum {
  11697. + YAFFS_ECC_RESULT_UNKNOWN,
  11698. + YAFFS_ECC_RESULT_NO_ERROR,
  11699. + YAFFS_ECC_RESULT_FIXED,
  11700. + YAFFS_ECC_RESULT_UNFIXED
  11701. +} yaffs_ECCResult;
  11702. +
  11703. +typedef enum {
  11704. + YAFFS_OBJECT_TYPE_UNKNOWN,
  11705. + YAFFS_OBJECT_TYPE_FILE,
  11706. + YAFFS_OBJECT_TYPE_SYMLINK,
  11707. + YAFFS_OBJECT_TYPE_DIRECTORY,
  11708. + YAFFS_OBJECT_TYPE_HARDLINK,
  11709. + YAFFS_OBJECT_TYPE_SPECIAL
  11710. +} yaffs_ObjectType;
  11711. +
  11712. +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  11713. +
  11714. +typedef struct {
  11715. +
  11716. + unsigned validMarker0;
  11717. + unsigned chunkUsed; /* Status of the chunk: used or unused */
  11718. + unsigned objectId; /* If 0 then this is not part of an object (unused) */
  11719. + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
  11720. + unsigned byteCount; /* Only valid for data chunks */
  11721. +
  11722. + /* The following stuff only has meaning when we read */
  11723. + yaffs_ECCResult eccResult;
  11724. + unsigned blockBad;
  11725. +
  11726. + /* YAFFS 1 stuff */
  11727. + unsigned chunkDeleted; /* The chunk is marked deleted */
  11728. + unsigned serialNumber; /* Yaffs1 2-bit serial number */
  11729. +
  11730. + /* YAFFS2 stuff */
  11731. + unsigned sequenceNumber; /* The sequence number of this block */
  11732. +
  11733. + /* Extra info if this is an object header (YAFFS2 only) */
  11734. +
  11735. + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
  11736. + unsigned extraParentObjectId; /* The parent object */
  11737. + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
  11738. + unsigned extraShadows; /* Does this shadow another object? */
  11739. +
  11740. + yaffs_ObjectType extraObjectType; /* What object type? */
  11741. +
  11742. + unsigned extraFileLength; /* Length if it is a file */
  11743. + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
  11744. +
  11745. + unsigned validMarker1;
  11746. +
  11747. +} yaffs_ExtendedTags;
  11748. +
  11749. +/* Spare structure for YAFFS1 */
  11750. +typedef struct {
  11751. + __u8 tagByte0;
  11752. + __u8 tagByte1;
  11753. + __u8 tagByte2;
  11754. + __u8 tagByte3;
  11755. + __u8 pageStatus; /* set to 0 to delete the chunk */
  11756. + __u8 blockStatus;
  11757. + __u8 tagByte4;
  11758. + __u8 tagByte5;
  11759. + __u8 ecc1[3];
  11760. + __u8 tagByte6;
  11761. + __u8 tagByte7;
  11762. + __u8 ecc2[3];
  11763. +} yaffs_Spare;
  11764. +
  11765. +/*Special structure for passing through to mtd */
  11766. +struct yaffs_NANDSpare {
  11767. + yaffs_Spare spare;
  11768. + int eccres1;
  11769. + int eccres2;
  11770. +};
  11771. +
  11772. +/* Block data in RAM */
  11773. +
  11774. +typedef enum {
  11775. + YAFFS_BLOCK_STATE_UNKNOWN = 0,
  11776. +
  11777. + YAFFS_BLOCK_STATE_SCANNING,
  11778. + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
  11779. + /* The block might have something on it (ie it is allocating or full, perhaps empty)
  11780. + * but it needs to be scanned to determine its true state.
  11781. + * This state is only valid during yaffs_Scan.
  11782. + * NB We tolerate empty because the pre-scanner might be incapable of deciding
  11783. + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
  11784. + */
  11785. +
  11786. + YAFFS_BLOCK_STATE_EMPTY,
  11787. + /* This block is empty */
  11788. +
  11789. + YAFFS_BLOCK_STATE_ALLOCATING,
  11790. + /* This block is partially allocated.
  11791. + * At least one page holds valid data.
  11792. + * This is the one currently being used for page
  11793. + * allocation. Should never be more than one of these
  11794. + */
  11795. +
  11796. + YAFFS_BLOCK_STATE_FULL,
  11797. + /* All the pages in this block have been allocated.
  11798. + */
  11799. +
  11800. + YAFFS_BLOCK_STATE_DIRTY,
  11801. + /* All pages have been allocated and deleted.
  11802. + * Erase me, reuse me.
  11803. + */
  11804. +
  11805. + YAFFS_BLOCK_STATE_CHECKPOINT,
  11806. + /* This block is assigned to holding checkpoint data.
  11807. + */
  11808. +
  11809. + YAFFS_BLOCK_STATE_COLLECTING,
  11810. + /* This block is being garbage collected */
  11811. +
  11812. + YAFFS_BLOCK_STATE_DEAD
  11813. + /* This block has failed and is not in use */
  11814. +} yaffs_BlockState;
  11815. +
  11816. +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  11817. +
  11818. +
  11819. +typedef struct {
  11820. +
  11821. + int softDeletions:10; /* number of soft deleted pages */
  11822. + int pagesInUse:10; /* number of pages in use */
  11823. + unsigned blockState:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
  11824. + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
  11825. + /* and retire the block. */
  11826. + __u32 skipErasedCheck:1; /* If this is set we can skip the erased check on this block */
  11827. + __u32 gcPrioritise:1; /* An ECC check or blank check has failed on this block.
  11828. + It should be prioritised for GC */
  11829. + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
  11830. +
  11831. +#ifdef CONFIG_YAFFS_YAFFS2
  11832. + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
  11833. + __u32 sequenceNumber; /* block sequence number for yaffs2 */
  11834. +#endif
  11835. +
  11836. +} yaffs_BlockInfo;
  11837. +
  11838. +/* -------------------------- Object structure -------------------------------*/
  11839. +/* This is the object structure as stored on NAND */
  11840. +
  11841. +typedef struct {
  11842. + yaffs_ObjectType type;
  11843. +
  11844. + /* Apply to everything */
  11845. + int parentObjectId;
  11846. + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
  11847. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  11848. +
  11849. + /* The following apply to directories, files, symlinks - not hard links */
  11850. + __u32 yst_mode; /* protection */
  11851. +
  11852. +#ifdef CONFIG_YAFFS_WINCE
  11853. + __u32 notForWinCE[5];
  11854. +#else
  11855. + __u32 yst_uid;
  11856. + __u32 yst_gid;
  11857. + __u32 yst_atime;
  11858. + __u32 yst_mtime;
  11859. + __u32 yst_ctime;
  11860. +#endif
  11861. +
  11862. + /* File size applies to files only */
  11863. + int fileSize;
  11864. +
  11865. + /* Equivalent object id applies to hard links only. */
  11866. + int equivalentObjectId;
  11867. +
  11868. + /* Alias is for symlinks only. */
  11869. + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  11870. +
  11871. + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
  11872. +
  11873. +#ifdef CONFIG_YAFFS_WINCE
  11874. + __u32 win_ctime[2];
  11875. + __u32 win_atime[2];
  11876. + __u32 win_mtime[2];
  11877. +#else
  11878. + __u32 roomToGrow[6];
  11879. +
  11880. +#endif
  11881. + __u32 inbandShadowsObject;
  11882. + __u32 inbandIsShrink;
  11883. +
  11884. + __u32 reservedSpace[2];
  11885. + int shadowsObject; /* This object header shadows the specified object if > 0 */
  11886. +
  11887. + /* isShrink applies to object headers written when we shrink the file (ie resize) */
  11888. + __u32 isShrink;
  11889. +
  11890. +} yaffs_ObjectHeader;
  11891. +
  11892. +/*--------------------------- Tnode -------------------------- */
  11893. +
  11894. +union yaffs_Tnode_union {
  11895. +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  11896. + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
  11897. +#else
  11898. + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
  11899. +#endif
  11900. +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
  11901. +
  11902. +};
  11903. +
  11904. +typedef union yaffs_Tnode_union yaffs_Tnode;
  11905. +
  11906. +struct yaffs_TnodeList_struct {
  11907. + struct yaffs_TnodeList_struct *next;
  11908. + yaffs_Tnode *tnodes;
  11909. +};
  11910. +
  11911. +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
  11912. +
  11913. +/*------------------------ Object -----------------------------*/
  11914. +/* An object can be one of:
  11915. + * - a directory (no data, has children links
  11916. + * - a regular file (data.... not prunes :->).
  11917. + * - a symlink [symbolic link] (the alias).
  11918. + * - a hard link
  11919. + */
  11920. +
  11921. +typedef struct {
  11922. + __u32 fileSize;
  11923. + __u32 scannedFileSize;
  11924. + __u32 shrinkSize;
  11925. + int topLevel;
  11926. + yaffs_Tnode *top;
  11927. +} yaffs_FileStructure;
  11928. +
  11929. +typedef struct {
  11930. + struct ylist_head children; /* list of child links */
  11931. +} yaffs_DirectoryStructure;
  11932. +
  11933. +typedef struct {
  11934. + YCHAR *alias;
  11935. +} yaffs_SymLinkStructure;
  11936. +
  11937. +typedef struct {
  11938. + struct yaffs_ObjectStruct *equivalentObject;
  11939. + __u32 equivalentObjectId;
  11940. +} yaffs_HardLinkStructure;
  11941. +
  11942. +typedef union {
  11943. + yaffs_FileStructure fileVariant;
  11944. + yaffs_DirectoryStructure directoryVariant;
  11945. + yaffs_SymLinkStructure symLinkVariant;
  11946. + yaffs_HardLinkStructure hardLinkVariant;
  11947. +} yaffs_ObjectVariant;
  11948. +
  11949. +struct yaffs_ObjectStruct {
  11950. + __u8 deleted:1; /* This should only apply to unlinked files. */
  11951. + __u8 softDeleted:1; /* it has also been soft deleted */
  11952. + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
  11953. + __u8 fake:1; /* A fake object has no presence on NAND. */
  11954. + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
  11955. + __u8 unlinkAllowed:1;
  11956. + __u8 dirty:1; /* the object needs to be written to flash */
  11957. + __u8 valid:1; /* When the file system is being loaded up, this
  11958. + * object might be created before the data
  11959. + * is available (ie. file data records appear before the header).
  11960. + */
  11961. + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
  11962. +
  11963. + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
  11964. + * still in the inode cache. Free of object is defered.
  11965. + * until the inode is released.
  11966. + */
  11967. + __u8 beingCreated:1; /* This object is still being created so skip some checks. */
  11968. +
  11969. + __u8 serial; /* serial number of chunk in NAND. Cached here */
  11970. + __u16 sum; /* sum of the name to speed searching */
  11971. +
  11972. + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
  11973. +
  11974. + struct ylist_head hashLink; /* list of objects in this hash bucket */
  11975. +
  11976. + struct ylist_head hardLinks; /* all the equivalent hard linked objects */
  11977. +
  11978. + /* directory structure stuff */
  11979. + /* also used for linking up the free list */
  11980. + struct yaffs_ObjectStruct *parent;
  11981. + struct ylist_head siblings;
  11982. +
  11983. + /* Where's my object header in NAND? */
  11984. + int hdrChunk;
  11985. +
  11986. + int nDataChunks; /* Number of data chunks attached to the file. */
  11987. +
  11988. + __u32 objectId; /* the object id value */
  11989. +
  11990. + __u32 yst_mode;
  11991. +
  11992. +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  11993. + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
  11994. +#endif
  11995. +
  11996. +#ifndef __KERNEL__
  11997. + __u32 inUse;
  11998. +#endif
  11999. +
  12000. +#ifdef CONFIG_YAFFS_WINCE
  12001. + __u32 win_ctime[2];
  12002. + __u32 win_mtime[2];
  12003. + __u32 win_atime[2];
  12004. +#else
  12005. + __u32 yst_uid;
  12006. + __u32 yst_gid;
  12007. + __u32 yst_atime;
  12008. + __u32 yst_mtime;
  12009. + __u32 yst_ctime;
  12010. +#endif
  12011. +
  12012. + __u32 yst_rdev;
  12013. +
  12014. +#ifdef __KERNEL__
  12015. + struct inode *myInode;
  12016. +
  12017. +#endif
  12018. +
  12019. + yaffs_ObjectType variantType;
  12020. +
  12021. + yaffs_ObjectVariant variant;
  12022. +
  12023. +};
  12024. +
  12025. +typedef struct yaffs_ObjectStruct yaffs_Object;
  12026. +
  12027. +struct yaffs_ObjectList_struct {
  12028. + yaffs_Object *objects;
  12029. + struct yaffs_ObjectList_struct *next;
  12030. +};
  12031. +
  12032. +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
  12033. +
  12034. +typedef struct {
  12035. + struct ylist_head list;
  12036. + int count;
  12037. +} yaffs_ObjectBucket;
  12038. +
  12039. +
  12040. +/* yaffs_CheckpointObject holds the definition of an object as dumped
  12041. + * by checkpointing.
  12042. + */
  12043. +
  12044. +typedef struct {
  12045. + int structType;
  12046. + __u32 objectId;
  12047. + __u32 parentId;
  12048. + int hdrChunk;
  12049. + yaffs_ObjectType variantType:3;
  12050. + __u8 deleted:1;
  12051. + __u8 softDeleted:1;
  12052. + __u8 unlinked:1;
  12053. + __u8 fake:1;
  12054. + __u8 renameAllowed:1;
  12055. + __u8 unlinkAllowed:1;
  12056. + __u8 serial;
  12057. +
  12058. + int nDataChunks;
  12059. + __u32 fileSizeOrEquivalentObjectId;
  12060. +} yaffs_CheckpointObject;
  12061. +
  12062. +/*--------------------- Temporary buffers ----------------
  12063. + *
  12064. + * These are chunk-sized working buffers. Each device has a few
  12065. + */
  12066. +
  12067. +typedef struct {
  12068. + __u8 *buffer;
  12069. + int line; /* track from whence this buffer was allocated */
  12070. + int maxLine;
  12071. +} yaffs_TempBuffer;
  12072. +
  12073. +/*----------------- Device ---------------------------------*/
  12074. +
  12075. +struct yaffs_DeviceStruct {
  12076. + struct ylist_head devList;
  12077. + const char *name;
  12078. +
  12079. + /* Entry parameters set up way early. Yaffs sets up the rest.*/
  12080. + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
  12081. + int nChunksPerBlock; /* does not need to be a power of 2 */
  12082. + int spareBytesPerChunk; /* spare area size */
  12083. + int startBlock; /* Start block we're allowed to use */
  12084. + int endBlock; /* End block we're allowed to use */
  12085. + int nReservedBlocks; /* We want this tuneable so that we can reduce */
  12086. + /* reserved blocks on NOR and RAM. */
  12087. +
  12088. +
  12089. + /* Stuff used by the shared space checkpointing mechanism */
  12090. + /* If this value is zero, then this mechanism is disabled */
  12091. +
  12092. +/* int nCheckpointReservedBlocks; */ /* Blocks to reserve for checkpoint data */
  12093. +
  12094. +
  12095. + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
  12096. + * the number of short op caches (don't use too many)
  12097. + */
  12098. +
  12099. + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
  12100. +
  12101. + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
  12102. +
  12103. + void *genericDevice; /* Pointer to device context
  12104. + * On an mtd this holds the mtd pointer.
  12105. + */
  12106. + void *superBlock;
  12107. +
  12108. + /* NAND access functions (Must be set before calling YAFFS)*/
  12109. +
  12110. + int (*writeChunkToNAND) (struct yaffs_DeviceStruct *dev,
  12111. + int chunkInNAND, const __u8 *data,
  12112. + const yaffs_Spare *spare);
  12113. + int (*readChunkFromNAND) (struct yaffs_DeviceStruct *dev,
  12114. + int chunkInNAND, __u8 *data,
  12115. + yaffs_Spare *spare);
  12116. + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct *dev,
  12117. + int blockInNAND);
  12118. + int (*initialiseNAND) (struct yaffs_DeviceStruct *dev);
  12119. + int (*deinitialiseNAND) (struct yaffs_DeviceStruct *dev);
  12120. +
  12121. +#ifdef CONFIG_YAFFS_YAFFS2
  12122. + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct *dev,
  12123. + int chunkInNAND, const __u8 *data,
  12124. + const yaffs_ExtendedTags *tags);
  12125. + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct *dev,
  12126. + int chunkInNAND, __u8 *data,
  12127. + yaffs_ExtendedTags *tags);
  12128. + int (*markNANDBlockBad) (struct yaffs_DeviceStruct *dev, int blockNo);
  12129. + int (*queryNANDBlock) (struct yaffs_DeviceStruct *dev, int blockNo,
  12130. + yaffs_BlockState *state, __u32 *sequenceNumber);
  12131. +#endif
  12132. +
  12133. + int isYaffs2;
  12134. +
  12135. + /* The removeObjectCallback function must be supplied by OS flavours that
  12136. + * need it. The Linux kernel does not use this, but yaffs direct does use
  12137. + * it to implement the faster readdir
  12138. + */
  12139. + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
  12140. +
  12141. + /* Callback to mark the superblock dirsty */
  12142. + void (*markSuperBlockDirty)(void *superblock);
  12143. +
  12144. + int wideTnodesDisabled; /* Set to disable wide tnodes */
  12145. +
  12146. + YCHAR *pathDividers; /* String of legal path dividers */
  12147. +
  12148. +
  12149. + /* End of stuff that must be set before initialisation. */
  12150. +
  12151. + /* Checkpoint control. Can be set before or after initialisation */
  12152. + __u8 skipCheckpointRead;
  12153. + __u8 skipCheckpointWrite;
  12154. +
  12155. + /* Runtime parameters. Set up by YAFFS. */
  12156. +
  12157. + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
  12158. + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
  12159. +
  12160. + /* Stuff to support wide tnodes */
  12161. + __u32 tnodeWidth;
  12162. + __u32 tnodeMask;
  12163. +
  12164. + /* Stuff for figuring out file offset to chunk conversions */
  12165. + __u32 chunkShift; /* Shift value */
  12166. + __u32 chunkDiv; /* Divisor after shifting: 1 for power-of-2 sizes */
  12167. + __u32 chunkMask; /* Mask to use for power-of-2 case */
  12168. +
  12169. + /* Stuff to handle inband tags */
  12170. + int inbandTags;
  12171. + __u32 totalBytesPerChunk;
  12172. +
  12173. +#ifdef __KERNEL__
  12174. +
  12175. + struct semaphore sem; /* Semaphore for waiting on erasure.*/
  12176. + struct semaphore grossLock; /* Gross locking semaphore */
  12177. + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
  12178. + * at compile time so we have to allocate it.
  12179. + */
  12180. + void (*putSuperFunc) (struct super_block *sb);
  12181. +#endif
  12182. +
  12183. + int isMounted;
  12184. +
  12185. + int isCheckpointed;
  12186. +
  12187. +
  12188. + /* Stuff to support block offsetting to support start block zero */
  12189. + int internalStartBlock;
  12190. + int internalEndBlock;
  12191. + int blockOffset;
  12192. + int chunkOffset;
  12193. +
  12194. +
  12195. + /* Runtime checkpointing stuff */
  12196. + int checkpointPageSequence; /* running sequence number of checkpoint pages */
  12197. + int checkpointByteCount;
  12198. + int checkpointByteOffset;
  12199. + __u8 *checkpointBuffer;
  12200. + int checkpointOpenForWrite;
  12201. + int blocksInCheckpoint;
  12202. + int checkpointCurrentChunk;
  12203. + int checkpointCurrentBlock;
  12204. + int checkpointNextBlock;
  12205. + int *checkpointBlockList;
  12206. + int checkpointMaxBlocks;
  12207. + __u32 checkpointSum;
  12208. + __u32 checkpointXor;
  12209. +
  12210. + int nCheckpointBlocksRequired; /* Number of blocks needed to store current checkpoint set */
  12211. +
  12212. + /* Block Info */
  12213. + yaffs_BlockInfo *blockInfo;
  12214. + __u8 *chunkBits; /* bitmap of chunks in use */
  12215. + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
  12216. + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
  12217. + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
  12218. + * Must be consistent with nChunksPerBlock.
  12219. + */
  12220. +
  12221. + int nErasedBlocks;
  12222. + int allocationBlock; /* Current block being allocated off */
  12223. + __u32 allocationPage;
  12224. + int allocationBlockFinder; /* Used to search for next allocation block */
  12225. +
  12226. + /* Runtime state */
  12227. + int nTnodesCreated;
  12228. + yaffs_Tnode *freeTnodes;
  12229. + int nFreeTnodes;
  12230. + yaffs_TnodeList *allocatedTnodeList;
  12231. +
  12232. + int isDoingGC;
  12233. + int gcBlock;
  12234. + int gcChunk;
  12235. +
  12236. + int nObjectsCreated;
  12237. + yaffs_Object *freeObjects;
  12238. + int nFreeObjects;
  12239. +
  12240. + int nHardLinks;
  12241. +
  12242. + yaffs_ObjectList *allocatedObjectList;
  12243. +
  12244. + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
  12245. +
  12246. + int nFreeChunks;
  12247. +
  12248. + int currentDirtyChecker; /* Used to find current dirtiest block */
  12249. +
  12250. + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
  12251. + int nonAggressiveSkip; /* GC state/mode */
  12252. +
  12253. + /* Statistcs */
  12254. + int nPageWrites;
  12255. + int nPageReads;
  12256. + int nBlockErasures;
  12257. + int nErasureFailures;
  12258. + int nGCCopies;
  12259. + int garbageCollections;
  12260. + int passiveGarbageCollections;
  12261. + int nRetriedWrites;
  12262. + int nRetiredBlocks;
  12263. + int eccFixed;
  12264. + int eccUnfixed;
  12265. + int tagsEccFixed;
  12266. + int tagsEccUnfixed;
  12267. + int nDeletions;
  12268. + int nUnmarkedDeletions;
  12269. +
  12270. + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
  12271. +
  12272. + /* Special directories */
  12273. + yaffs_Object *rootDir;
  12274. + yaffs_Object *lostNFoundDir;
  12275. +
  12276. + /* Buffer areas for storing data to recover from write failures TODO
  12277. + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
  12278. + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
  12279. + */
  12280. +
  12281. + int bufferedBlock; /* Which block is buffered here? */
  12282. + int doingBufferedBlockRewrite;
  12283. +
  12284. + yaffs_ChunkCache *srCache;
  12285. + int srLastUse;
  12286. +
  12287. + int cacheHits;
  12288. +
  12289. + /* Stuff for background deletion and unlinked files.*/
  12290. + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
  12291. + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
  12292. + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
  12293. + int nDeletedFiles; /* Count of files awaiting deletion;*/
  12294. + int nUnlinkedFiles; /* Count of unlinked files. */
  12295. + int nBackgroundDeletions; /* Count of background deletions. */
  12296. +
  12297. +
  12298. + /* Temporary buffer management */
  12299. + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
  12300. + int maxTemp;
  12301. + int tempInUse;
  12302. + int unmanagedTempAllocations;
  12303. + int unmanagedTempDeallocations;
  12304. +
  12305. + /* yaffs2 runtime stuff */
  12306. + unsigned sequenceNumber; /* Sequence number of currently allocating block */
  12307. + unsigned oldestDirtySequence;
  12308. +
  12309. +};
  12310. +
  12311. +typedef struct yaffs_DeviceStruct yaffs_Device;
  12312. +
  12313. +/* The static layout of block usage etc is stored in the super block header */
  12314. +typedef struct {
  12315. + int StructType;
  12316. + int version;
  12317. + int checkpointStartBlock;
  12318. + int checkpointEndBlock;
  12319. + int startBlock;
  12320. + int endBlock;
  12321. + int rfu[100];
  12322. +} yaffs_SuperBlockHeader;
  12323. +
  12324. +/* The CheckpointDevice structure holds the device information that changes at runtime and
  12325. + * must be preserved over unmount/mount cycles.
  12326. + */
  12327. +typedef struct {
  12328. + int structType;
  12329. + int nErasedBlocks;
  12330. + int allocationBlock; /* Current block being allocated off */
  12331. + __u32 allocationPage;
  12332. + int nFreeChunks;
  12333. +
  12334. + int nDeletedFiles; /* Count of files awaiting deletion;*/
  12335. + int nUnlinkedFiles; /* Count of unlinked files. */
  12336. + int nBackgroundDeletions; /* Count of background deletions. */
  12337. +
  12338. + /* yaffs2 runtime stuff */
  12339. + unsigned sequenceNumber; /* Sequence number of currently allocating block */
  12340. + unsigned oldestDirtySequence;
  12341. +
  12342. +} yaffs_CheckpointDevice;
  12343. +
  12344. +
  12345. +typedef struct {
  12346. + int structType;
  12347. + __u32 magic;
  12348. + __u32 version;
  12349. + __u32 head;
  12350. +} yaffs_CheckpointValidity;
  12351. +
  12352. +
  12353. +/*----------------------- YAFFS Functions -----------------------*/
  12354. +
  12355. +int yaffs_GutsInitialise(yaffs_Device *dev);
  12356. +void yaffs_Deinitialise(yaffs_Device *dev);
  12357. +
  12358. +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
  12359. +
  12360. +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
  12361. + yaffs_Object *newDir, const YCHAR *newName);
  12362. +
  12363. +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name);
  12364. +int yaffs_DeleteObject(yaffs_Object *obj);
  12365. +
  12366. +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize);
  12367. +int yaffs_GetObjectFileLength(yaffs_Object *obj);
  12368. +int yaffs_GetObjectInode(yaffs_Object *obj);
  12369. +unsigned yaffs_GetObjectType(yaffs_Object *obj);
  12370. +int yaffs_GetObjectLinkCount(yaffs_Object *obj);
  12371. +
  12372. +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
  12373. +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
  12374. +
  12375. +/* File operations */
  12376. +int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, loff_t offset,
  12377. + int nBytes);
  12378. +int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, loff_t offset,
  12379. + int nBytes, int writeThrough);
  12380. +int yaffs_ResizeFile(yaffs_Object *obj, loff_t newSize);
  12381. +
  12382. +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
  12383. + __u32 mode, __u32 uid, __u32 gid);
  12384. +int yaffs_FlushFile(yaffs_Object *obj, int updateTime);
  12385. +
  12386. +/* Flushing and checkpointing */
  12387. +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
  12388. +
  12389. +int yaffs_CheckpointSave(yaffs_Device *dev);
  12390. +int yaffs_CheckpointRestore(yaffs_Device *dev);
  12391. +
  12392. +/* Directory operations */
  12393. +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
  12394. + __u32 mode, __u32 uid, __u32 gid);
  12395. +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir, const YCHAR *name);
  12396. +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
  12397. + int (*fn) (yaffs_Object *));
  12398. +
  12399. +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number);
  12400. +
  12401. +/* Link operations */
  12402. +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
  12403. + yaffs_Object *equivalentObject);
  12404. +
  12405. +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
  12406. +
  12407. +/* Symlink operations */
  12408. +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
  12409. + __u32 mode, __u32 uid, __u32 gid,
  12410. + const YCHAR *alias);
  12411. +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj);
  12412. +
  12413. +/* Special inodes (fifos, sockets and devices) */
  12414. +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
  12415. + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
  12416. +
  12417. +/* Special directories */
  12418. +yaffs_Object *yaffs_Root(yaffs_Device *dev);
  12419. +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
  12420. +
  12421. +#ifdef CONFIG_YAFFS_WINCE
  12422. +/* CONFIG_YAFFS_WINCE special stuff */
  12423. +void yfsd_WinFileTimeNow(__u32 target[2]);
  12424. +#endif
  12425. +
  12426. +#ifdef __KERNEL__
  12427. +
  12428. +void yaffs_HandleDeferedFree(yaffs_Object *obj);
  12429. +#endif
  12430. +
  12431. +/* Debug dump */
  12432. +int yaffs_DumpObject(yaffs_Object *obj);
  12433. +
  12434. +void yaffs_GutsTest(yaffs_Device *dev);
  12435. +
  12436. +/* A few useful functions */
  12437. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
  12438. +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn);
  12439. +int yaffs_CheckFF(__u8 *buffer, int nBytes);
  12440. +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
  12441. +
  12442. +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo);
  12443. +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
  12444. +
  12445. +#endif
  12446. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffsinterface.h linux-2.6.32/fs/yaffs2/yaffsinterface.h
  12447. --- linux-2.6.32.orig/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
  12448. +++ linux-2.6.32/fs/yaffs2/yaffsinterface.h 2010-01-30 20:35:01.492020092 +0100
  12449. @@ -0,0 +1,21 @@
  12450. +/*
  12451. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  12452. + *
  12453. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12454. + * for Toby Churchill Ltd and Brightstar Engineering
  12455. + *
  12456. + * Created by Charles Manning <charles@aleph1.co.uk>
  12457. + *
  12458. + * This program is free software; you can redistribute it and/or modify
  12459. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  12460. + * published by the Free Software Foundation.
  12461. + *
  12462. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  12463. + */
  12464. +
  12465. +#ifndef __YAFFSINTERFACE_H__
  12466. +#define __YAFFSINTERFACE_H__
  12467. +
  12468. +int yaffs_Initialise(unsigned nBlocks);
  12469. +
  12470. +#endif
  12471. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif1.c linux-2.6.32/fs/yaffs2/yaffs_mtdif1.c
  12472. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
  12473. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif1.c 2010-01-30 20:35:01.534033438 +0100
  12474. @@ -0,0 +1,365 @@
  12475. +/*
  12476. + * YAFFS: Yet another FFS. A NAND-flash specific file system.
  12477. + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
  12478. + *
  12479. + * Copyright (C) 2002 Aleph One Ltd.
  12480. + * for Toby Churchill Ltd and Brightstar Engineering
  12481. + *
  12482. + * This program is free software; you can redistribute it and/or modify
  12483. + * it under the terms of the GNU General Public License version 2 as
  12484. + * published by the Free Software Foundation.
  12485. + */
  12486. +
  12487. +/*
  12488. + * This module provides the interface between yaffs_nand.c and the
  12489. + * MTD API. This version is used when the MTD interface supports the
  12490. + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
  12491. + * and we have small-page NAND device.
  12492. + *
  12493. + * These functions are invoked via function pointers in yaffs_nand.c.
  12494. + * This replaces functionality provided by functions in yaffs_mtdif.c
  12495. + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
  12496. + * called in yaffs_mtdif.c when the function pointers are NULL.
  12497. + * We assume the MTD layer is performing ECC (useNANDECC is true).
  12498. + */
  12499. +
  12500. +#include "yportenv.h"
  12501. +#include "yaffs_guts.h"
  12502. +#include "yaffs_packedtags1.h"
  12503. +#include "yaffs_tagscompat.h" /* for yaffs_CalcTagsECC */
  12504. +
  12505. +#include "linux/kernel.h"
  12506. +#include "linux/version.h"
  12507. +#include "linux/types.h"
  12508. +#include "linux/mtd/mtd.h"
  12509. +
  12510. +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
  12511. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12512. +
  12513. +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.10 2009-03-09 07:41:10 charles Exp $";
  12514. +
  12515. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12516. +# define YTAG1_SIZE 8
  12517. +#else
  12518. +# define YTAG1_SIZE 9
  12519. +#endif
  12520. +
  12521. +#if 0
  12522. +/* Use the following nand_ecclayout with MTD when using
  12523. + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
  12524. + * If you have existing Yaffs images and the byte order differs from this,
  12525. + * adjust 'oobfree' to match your existing Yaffs data.
  12526. + *
  12527. + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
  12528. + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
  12529. + * the 9th byte.
  12530. + *
  12531. + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
  12532. + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
  12533. + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
  12534. + * byte and B is the small-page bad-block indicator byte.
  12535. + */
  12536. +static struct nand_ecclayout nand_oob_16 = {
  12537. + .eccbytes = 6,
  12538. + .eccpos = { 8, 9, 10, 13, 14, 15 },
  12539. + .oobavail = 9,
  12540. + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
  12541. +};
  12542. +#endif
  12543. +
  12544. +/* Write a chunk (page) of data to NAND.
  12545. + *
  12546. + * Caller always provides ExtendedTags data which are converted to a more
  12547. + * compact (packed) form for storage in NAND. A mini-ECC runs over the
  12548. + * contents of the tags meta-data; used to valid the tags when read.
  12549. + *
  12550. + * - Pack ExtendedTags to PackedTags1 form
  12551. + * - Compute mini-ECC for PackedTags1
  12552. + * - Write data and packed tags to NAND.
  12553. + *
  12554. + * Note: Due to the use of the PackedTags1 meta-data which does not include
  12555. + * a full sequence number (as found in the larger PackedTags2 form) it is
  12556. + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
  12557. + * discarded and dirty. This is not ideal: newer NAND parts are supposed
  12558. + * to be written just once. When Yaffs performs this operation, this
  12559. + * function is called with a NULL data pointer -- calling MTD write_oob
  12560. + * without data is valid usage (2.6.17).
  12561. + *
  12562. + * Any underlying MTD error results in YAFFS_FAIL.
  12563. + * Returns YAFFS_OK or YAFFS_FAIL.
  12564. + */
  12565. +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  12566. + int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
  12567. +{
  12568. + struct mtd_info *mtd = dev->genericDevice;
  12569. + int chunkBytes = dev->nDataBytesPerChunk;
  12570. + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
  12571. + struct mtd_oob_ops ops;
  12572. + yaffs_PackedTags1 pt1;
  12573. + int retval;
  12574. +
  12575. + /* we assume that PackedTags1 and yaffs_Tags are compatible */
  12576. + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
  12577. + compile_time_assertion(sizeof(yaffs_Tags) == 8);
  12578. +
  12579. + dev->nPageWrites++;
  12580. +
  12581. + yaffs_PackTags1(&pt1, etags);
  12582. + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
  12583. +
  12584. + /* When deleting a chunk, the upper layer provides only skeletal
  12585. + * etags, one with chunkDeleted set. However, we need to update the
  12586. + * tags, not erase them completely. So we use the NAND write property
  12587. + * that only zeroed-bits stick and set tag bytes to all-ones and
  12588. + * zero just the (not) deleted bit.
  12589. + */
  12590. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12591. + if (etags->chunkDeleted) {
  12592. + memset(&pt1, 0xff, 8);
  12593. + /* clear delete status bit to indicate deleted */
  12594. + pt1.deleted = 0;
  12595. + }
  12596. +#else
  12597. + ((__u8 *)&pt1)[8] = 0xff;
  12598. + if (etags->chunkDeleted) {
  12599. + memset(&pt1, 0xff, 8);
  12600. + /* zero pageStatus byte to indicate deleted */
  12601. + ((__u8 *)&pt1)[8] = 0;
  12602. + }
  12603. +#endif
  12604. +
  12605. + memset(&ops, 0, sizeof(ops));
  12606. + ops.mode = MTD_OOB_AUTO;
  12607. + ops.len = (data) ? chunkBytes : 0;
  12608. + ops.ooblen = YTAG1_SIZE;
  12609. + ops.datbuf = (__u8 *)data;
  12610. + ops.oobbuf = (__u8 *)&pt1;
  12611. +
  12612. + retval = mtd->write_oob(mtd, addr, &ops);
  12613. + if (retval) {
  12614. + yaffs_trace(YAFFS_TRACE_MTD,
  12615. + "write_oob failed, chunk %d, mtd error %d\n",
  12616. + chunkInNAND, retval);
  12617. + }
  12618. + return retval ? YAFFS_FAIL : YAFFS_OK;
  12619. +}
  12620. +
  12621. +/* Return with empty ExtendedTags but add eccResult.
  12622. + */
  12623. +static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
  12624. +{
  12625. + if (etags) {
  12626. + memset(etags, 0, sizeof(*etags));
  12627. + etags->eccResult = eccResult;
  12628. + }
  12629. + return retval;
  12630. +}
  12631. +
  12632. +/* Read a chunk (page) from NAND.
  12633. + *
  12634. + * Caller expects ExtendedTags data to be usable even on error; that is,
  12635. + * all members except eccResult and blockBad are zeroed.
  12636. + *
  12637. + * - Check ECC results for data (if applicable)
  12638. + * - Check for blank/erased block (return empty ExtendedTags if blank)
  12639. + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
  12640. + * - Convert PackedTags1 to ExtendedTags
  12641. + * - Update eccResult and blockBad members to refect state.
  12642. + *
  12643. + * Returns YAFFS_OK or YAFFS_FAIL.
  12644. + */
  12645. +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
  12646. + int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
  12647. +{
  12648. + struct mtd_info *mtd = dev->genericDevice;
  12649. + int chunkBytes = dev->nDataBytesPerChunk;
  12650. + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
  12651. + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
  12652. + struct mtd_oob_ops ops;
  12653. + yaffs_PackedTags1 pt1;
  12654. + int retval;
  12655. + int deleted;
  12656. +
  12657. + dev->nPageReads++;
  12658. +
  12659. + memset(&ops, 0, sizeof(ops));
  12660. + ops.mode = MTD_OOB_AUTO;
  12661. + ops.len = (data) ? chunkBytes : 0;
  12662. + ops.ooblen = YTAG1_SIZE;
  12663. + ops.datbuf = data;
  12664. + ops.oobbuf = (__u8 *)&pt1;
  12665. +
  12666. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
  12667. + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
  12668. + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
  12669. + */
  12670. + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
  12671. +#endif
  12672. + /* Read page and oob using MTD.
  12673. + * Check status and determine ECC result.
  12674. + */
  12675. + retval = mtd->read_oob(mtd, addr, &ops);
  12676. + if (retval) {
  12677. + yaffs_trace(YAFFS_TRACE_MTD,
  12678. + "read_oob failed, chunk %d, mtd error %d\n",
  12679. + chunkInNAND, retval);
  12680. + }
  12681. +
  12682. + switch (retval) {
  12683. + case 0:
  12684. + /* no error */
  12685. + break;
  12686. +
  12687. + case -EUCLEAN:
  12688. + /* MTD's ECC fixed the data */
  12689. + eccres = YAFFS_ECC_RESULT_FIXED;
  12690. + dev->eccFixed++;
  12691. + break;
  12692. +
  12693. + case -EBADMSG:
  12694. + /* MTD's ECC could not fix the data */
  12695. + dev->eccUnfixed++;
  12696. + /* fall into... */
  12697. + default:
  12698. + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
  12699. + etags->blockBad = (mtd->block_isbad)(mtd, addr);
  12700. + return YAFFS_FAIL;
  12701. + }
  12702. +
  12703. + /* Check for a blank/erased chunk.
  12704. + */
  12705. + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
  12706. + /* when blank, upper layers want eccResult to be <= NO_ERROR */
  12707. + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
  12708. + }
  12709. +
  12710. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  12711. + /* Read deleted status (bit) then return it to it's non-deleted
  12712. + * state before performing tags mini-ECC check. pt1.deleted is
  12713. + * inverted.
  12714. + */
  12715. + deleted = !pt1.deleted;
  12716. + pt1.deleted = 1;
  12717. +#else
  12718. + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
  12719. +#endif
  12720. +
  12721. + /* Check the packed tags mini-ECC and correct if necessary/possible.
  12722. + */
  12723. + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
  12724. + switch (retval) {
  12725. + case 0:
  12726. + /* no tags error, use MTD result */
  12727. + break;
  12728. + case 1:
  12729. + /* recovered tags-ECC error */
  12730. + dev->tagsEccFixed++;
  12731. + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
  12732. + eccres = YAFFS_ECC_RESULT_FIXED;
  12733. + break;
  12734. + default:
  12735. + /* unrecovered tags-ECC error */
  12736. + dev->tagsEccUnfixed++;
  12737. + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
  12738. + }
  12739. +
  12740. + /* Unpack the tags to extended form and set ECC result.
  12741. + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
  12742. + */
  12743. + pt1.shouldBeFF = 0xFFFFFFFF;
  12744. + yaffs_UnpackTags1(etags, &pt1);
  12745. + etags->eccResult = eccres;
  12746. +
  12747. + /* Set deleted state */
  12748. + etags->chunkDeleted = deleted;
  12749. + return YAFFS_OK;
  12750. +}
  12751. +
  12752. +/* Mark a block bad.
  12753. + *
  12754. + * This is a persistant state.
  12755. + * Use of this function should be rare.
  12756. + *
  12757. + * Returns YAFFS_OK or YAFFS_FAIL.
  12758. + */
  12759. +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
  12760. +{
  12761. + struct mtd_info *mtd = dev->genericDevice;
  12762. + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
  12763. + int retval;
  12764. +
  12765. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad\n", blockNo);
  12766. +
  12767. + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
  12768. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  12769. +}
  12770. +
  12771. +/* Check any MTD prerequists.
  12772. + *
  12773. + * Returns YAFFS_OK or YAFFS_FAIL.
  12774. + */
  12775. +static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
  12776. +{
  12777. + /* 2.6.18 has mtd->ecclayout->oobavail */
  12778. + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
  12779. + int oobavail = mtd->ecclayout->oobavail;
  12780. +
  12781. + if (oobavail < YTAG1_SIZE) {
  12782. + yaffs_trace(YAFFS_TRACE_ERROR,
  12783. + "mtd device has only %d bytes for tags, need %d\n",
  12784. + oobavail, YTAG1_SIZE);
  12785. + return YAFFS_FAIL;
  12786. + }
  12787. + return YAFFS_OK;
  12788. +}
  12789. +
  12790. +/* Query for the current state of a specific block.
  12791. + *
  12792. + * Examine the tags of the first chunk of the block and return the state:
  12793. + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
  12794. + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
  12795. + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
  12796. + *
  12797. + * Always returns YAFFS_OK.
  12798. + */
  12799. +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  12800. + yaffs_BlockState *pState, __u32 *pSequenceNumber)
  12801. +{
  12802. + struct mtd_info *mtd = dev->genericDevice;
  12803. + int chunkNo = blockNo * dev->nChunksPerBlock;
  12804. + loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
  12805. + yaffs_ExtendedTags etags;
  12806. + int state = YAFFS_BLOCK_STATE_DEAD;
  12807. + int seqnum = 0;
  12808. + int retval;
  12809. +
  12810. + /* We don't yet have a good place to test for MTD config prerequists.
  12811. + * Do it here as we are called during the initial scan.
  12812. + */
  12813. + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK)
  12814. + return YAFFS_FAIL;
  12815. +
  12816. + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
  12817. + etags.blockBad = (mtd->block_isbad)(mtd, addr);
  12818. + if (etags.blockBad) {
  12819. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  12820. + "block %d is marked bad\n", blockNo);
  12821. + state = YAFFS_BLOCK_STATE_DEAD;
  12822. + } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
  12823. + /* bad tags, need to look more closely */
  12824. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  12825. + } else if (etags.chunkUsed) {
  12826. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  12827. + seqnum = etags.sequenceNumber;
  12828. + } else {
  12829. + state = YAFFS_BLOCK_STATE_EMPTY;
  12830. + }
  12831. +
  12832. + *pState = state;
  12833. + *pSequenceNumber = seqnum;
  12834. +
  12835. + /* query always succeeds */
  12836. + return YAFFS_OK;
  12837. +}
  12838. +
  12839. +#endif /*MTD_VERSION*/
  12840. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif1.h linux-2.6.32/fs/yaffs2/yaffs_mtdif1.h
  12841. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
  12842. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif1.h 2010-01-30 20:35:01.572068025 +0100
  12843. @@ -0,0 +1,28 @@
  12844. +/*
  12845. + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
  12846. + *
  12847. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12848. + * for Toby Churchill Ltd and Brightstar Engineering
  12849. + *
  12850. + * This program is free software; you can redistribute it and/or modify
  12851. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  12852. + * published by the Free Software Foundation.
  12853. + *
  12854. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  12855. + */
  12856. +
  12857. +#ifndef __YAFFS_MTDIF1_H__
  12858. +#define __YAFFS_MTDIF1_H__
  12859. +
  12860. +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  12861. + const __u8 *data, const yaffs_ExtendedTags *tags);
  12862. +
  12863. +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  12864. + __u8 *data, yaffs_ExtendedTags *tags);
  12865. +
  12866. +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  12867. +
  12868. +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  12869. + yaffs_BlockState *state, __u32 *sequenceNumber);
  12870. +
  12871. +#endif
  12872. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif2.c linux-2.6.32/fs/yaffs2/yaffs_mtdif2.c
  12873. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
  12874. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif2.c 2010-01-30 20:35:01.611828597 +0100
  12875. @@ -0,0 +1,246 @@
  12876. +/*
  12877. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  12878. + *
  12879. + * Copyright (C) 2002-2007 Aleph One Ltd.
  12880. + * for Toby Churchill Ltd and Brightstar Engineering
  12881. + *
  12882. + * Created by Charles Manning <charles@aleph1.co.uk>
  12883. + *
  12884. + * This program is free software; you can redistribute it and/or modify
  12885. + * it under the terms of the GNU General Public License version 2 as
  12886. + * published by the Free Software Foundation.
  12887. + */
  12888. +
  12889. +/* mtd interface for YAFFS2 */
  12890. +
  12891. +const char *yaffs_mtdif2_c_version =
  12892. + "$Id: yaffs_mtdif2.c,v 1.23 2009-03-06 17:20:53 wookey Exp $";
  12893. +
  12894. +#include "yportenv.h"
  12895. +
  12896. +
  12897. +#include "yaffs_mtdif2.h"
  12898. +
  12899. +#include "linux/mtd/mtd.h"
  12900. +#include "linux/types.h"
  12901. +#include "linux/time.h"
  12902. +
  12903. +#include "yaffs_packedtags2.h"
  12904. +
  12905. +/* NB For use with inband tags....
  12906. + * We assume that the data buffer is of size totalBytersPerChunk so that we can also
  12907. + * use it to load the tags.
  12908. + */
  12909. +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  12910. + const __u8 *data,
  12911. + const yaffs_ExtendedTags *tags)
  12912. +{
  12913. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  12914. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12915. + struct mtd_oob_ops ops;
  12916. +#else
  12917. + size_t dummy;
  12918. +#endif
  12919. + int retval = 0;
  12920. +
  12921. + loff_t addr;
  12922. +
  12923. + yaffs_PackedTags2 pt;
  12924. +
  12925. + T(YAFFS_TRACE_MTD,
  12926. + (TSTR
  12927. + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
  12928. + TENDSTR), chunkInNAND, data, tags));
  12929. +
  12930. +
  12931. + addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
  12932. +
  12933. + /* For yaffs2 writing there must be both data and tags.
  12934. + * If we're using inband tags, then the tags are stuffed into
  12935. + * the end of the data buffer.
  12936. + */
  12937. + if (!data || !tags)
  12938. + BUG();
  12939. + else if (dev->inbandTags) {
  12940. + yaffs_PackedTags2TagsPart *pt2tp;
  12941. + pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
  12942. + yaffs_PackTags2TagsPart(pt2tp, tags);
  12943. + } else
  12944. + yaffs_PackTags2(&pt, tags);
  12945. +
  12946. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  12947. + ops.mode = MTD_OOB_AUTO;
  12948. + ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
  12949. + ops.len = dev->totalBytesPerChunk;
  12950. + ops.ooboffs = 0;
  12951. + ops.datbuf = (__u8 *)data;
  12952. + ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
  12953. + retval = mtd->write_oob(mtd, addr, &ops);
  12954. +
  12955. +#else
  12956. + if (!dev->inbandTags) {
  12957. + retval =
  12958. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  12959. + &dummy, data, (__u8 *) &pt, NULL);
  12960. + } else {
  12961. + retval =
  12962. + mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
  12963. + data);
  12964. + }
  12965. +#endif
  12966. +
  12967. + if (retval == 0)
  12968. + return YAFFS_OK;
  12969. + else
  12970. + return YAFFS_FAIL;
  12971. +}
  12972. +
  12973. +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  12974. + __u8 *data, yaffs_ExtendedTags *tags)
  12975. +{
  12976. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  12977. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  12978. + struct mtd_oob_ops ops;
  12979. +#endif
  12980. + size_t dummy;
  12981. + int retval = 0;
  12982. + int localData = 0;
  12983. +
  12984. + loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
  12985. +
  12986. + yaffs_PackedTags2 pt;
  12987. +
  12988. + T(YAFFS_TRACE_MTD,
  12989. + (TSTR
  12990. + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
  12991. + TENDSTR), chunkInNAND, data, tags));
  12992. +
  12993. + if (dev->inbandTags) {
  12994. +
  12995. + if (!data) {
  12996. + localData = 1;
  12997. + data = yaffs_GetTempBuffer(dev, __LINE__);
  12998. + }
  12999. +
  13000. +
  13001. + }
  13002. +
  13003. +
  13004. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13005. + if (dev->inbandTags || (data && !tags))
  13006. + retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
  13007. + &dummy, data);
  13008. + else if (tags) {
  13009. + ops.mode = MTD_OOB_AUTO;
  13010. + ops.ooblen = sizeof(pt);
  13011. + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
  13012. + ops.ooboffs = 0;
  13013. + ops.datbuf = data;
  13014. + ops.oobbuf = dev->spareBuffer;
  13015. + retval = mtd->read_oob(mtd, addr, &ops);
  13016. + }
  13017. +#else
  13018. + if (!dev->inbandTags && data && tags) {
  13019. +
  13020. + retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13021. + &dummy, data, dev->spareBuffer,
  13022. + NULL);
  13023. + } else {
  13024. + if (data)
  13025. + retval =
  13026. + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13027. + data);
  13028. + if (!dev->inbandTags && tags)
  13029. + retval =
  13030. + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
  13031. + dev->spareBuffer);
  13032. + }
  13033. +#endif
  13034. +
  13035. +
  13036. + if (dev->inbandTags) {
  13037. + if (tags) {
  13038. + yaffs_PackedTags2TagsPart *pt2tp;
  13039. + pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
  13040. + yaffs_UnpackTags2TagsPart(tags, pt2tp);
  13041. + }
  13042. + } else {
  13043. + if (tags) {
  13044. + memcpy(&pt, dev->spareBuffer, sizeof(pt));
  13045. + yaffs_UnpackTags2(tags, &pt);
  13046. + }
  13047. + }
  13048. +
  13049. + if (localData)
  13050. + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
  13051. +
  13052. + if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
  13053. + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
  13054. + if (retval == 0)
  13055. + return YAFFS_OK;
  13056. + else
  13057. + return YAFFS_FAIL;
  13058. +}
  13059. +
  13060. +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
  13061. +{
  13062. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13063. + int retval;
  13064. + T(YAFFS_TRACE_MTD,
  13065. + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
  13066. +
  13067. + retval =
  13068. + mtd->block_markbad(mtd,
  13069. + blockNo * dev->nChunksPerBlock *
  13070. + dev->totalBytesPerChunk);
  13071. +
  13072. + if (retval == 0)
  13073. + return YAFFS_OK;
  13074. + else
  13075. + return YAFFS_FAIL;
  13076. +
  13077. +}
  13078. +
  13079. +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13080. + yaffs_BlockState *state, __u32 *sequenceNumber)
  13081. +{
  13082. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13083. + int retval;
  13084. +
  13085. + T(YAFFS_TRACE_MTD,
  13086. + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
  13087. + retval =
  13088. + mtd->block_isbad(mtd,
  13089. + blockNo * dev->nChunksPerBlock *
  13090. + dev->totalBytesPerChunk);
  13091. +
  13092. + if (retval) {
  13093. + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
  13094. +
  13095. + *state = YAFFS_BLOCK_STATE_DEAD;
  13096. + *sequenceNumber = 0;
  13097. + } else {
  13098. + yaffs_ExtendedTags t;
  13099. + nandmtd2_ReadChunkWithTagsFromNAND(dev,
  13100. + blockNo *
  13101. + dev->nChunksPerBlock, NULL,
  13102. + &t);
  13103. +
  13104. + if (t.chunkUsed) {
  13105. + *sequenceNumber = t.sequenceNumber;
  13106. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  13107. + } else {
  13108. + *sequenceNumber = 0;
  13109. + *state = YAFFS_BLOCK_STATE_EMPTY;
  13110. + }
  13111. + }
  13112. + T(YAFFS_TRACE_MTD,
  13113. + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
  13114. + *state));
  13115. +
  13116. + if (retval == 0)
  13117. + return YAFFS_OK;
  13118. + else
  13119. + return YAFFS_FAIL;
  13120. +}
  13121. +
  13122. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif2.h linux-2.6.32/fs/yaffs2/yaffs_mtdif2.h
  13123. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
  13124. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif2.h 2010-01-30 20:35:01.663084824 +0100
  13125. @@ -0,0 +1,29 @@
  13126. +/*
  13127. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13128. + *
  13129. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13130. + * for Toby Churchill Ltd and Brightstar Engineering
  13131. + *
  13132. + * Created by Charles Manning <charles@aleph1.co.uk>
  13133. + *
  13134. + * This program is free software; you can redistribute it and/or modify
  13135. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13136. + * published by the Free Software Foundation.
  13137. + *
  13138. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13139. + */
  13140. +
  13141. +#ifndef __YAFFS_MTDIF2_H__
  13142. +#define __YAFFS_MTDIF2_H__
  13143. +
  13144. +#include "yaffs_guts.h"
  13145. +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
  13146. + const __u8 *data,
  13147. + const yaffs_ExtendedTags *tags);
  13148. +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13149. + __u8 *data, yaffs_ExtendedTags *tags);
  13150. +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  13151. +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13152. + yaffs_BlockState *state, __u32 *sequenceNumber);
  13153. +
  13154. +#endif
  13155. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif.c linux-2.6.32/fs/yaffs2/yaffs_mtdif.c
  13156. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  13157. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif.c 2010-01-30 20:35:01.701828807 +0100
  13158. @@ -0,0 +1,241 @@
  13159. +/*
  13160. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13161. + *
  13162. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13163. + * for Toby Churchill Ltd and Brightstar Engineering
  13164. + *
  13165. + * Created by Charles Manning <charles@aleph1.co.uk>
  13166. + *
  13167. + * This program is free software; you can redistribute it and/or modify
  13168. + * it under the terms of the GNU General Public License version 2 as
  13169. + * published by the Free Software Foundation.
  13170. + */
  13171. +
  13172. +const char *yaffs_mtdif_c_version =
  13173. + "$Id: yaffs_mtdif.c,v 1.22 2009-03-06 17:20:51 wookey Exp $";
  13174. +
  13175. +#include "yportenv.h"
  13176. +
  13177. +
  13178. +#include "yaffs_mtdif.h"
  13179. +
  13180. +#include "linux/mtd/mtd.h"
  13181. +#include "linux/types.h"
  13182. +#include "linux/time.h"
  13183. +#include "linux/mtd/nand.h"
  13184. +
  13185. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
  13186. +static struct nand_oobinfo yaffs_oobinfo = {
  13187. + .useecc = 1,
  13188. + .eccbytes = 6,
  13189. + .eccpos = {8, 9, 10, 13, 14, 15}
  13190. +};
  13191. +
  13192. +static struct nand_oobinfo yaffs_noeccinfo = {
  13193. + .useecc = 0,
  13194. +};
  13195. +#endif
  13196. +
  13197. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13198. +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
  13199. +{
  13200. + oob[0] = spare->tagByte0;
  13201. + oob[1] = spare->tagByte1;
  13202. + oob[2] = spare->tagByte2;
  13203. + oob[3] = spare->tagByte3;
  13204. + oob[4] = spare->tagByte4;
  13205. + oob[5] = spare->tagByte5 & 0x3f;
  13206. + oob[5] |= spare->blockStatus == 'Y' ? 0 : 0x80;
  13207. + oob[5] |= spare->pageStatus == 0 ? 0 : 0x40;
  13208. + oob[6] = spare->tagByte6;
  13209. + oob[7] = spare->tagByte7;
  13210. +}
  13211. +
  13212. +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
  13213. +{
  13214. + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
  13215. + spare->tagByte0 = oob[0];
  13216. + spare->tagByte1 = oob[1];
  13217. + spare->tagByte2 = oob[2];
  13218. + spare->tagByte3 = oob[3];
  13219. + spare->tagByte4 = oob[4];
  13220. + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
  13221. + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
  13222. + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
  13223. + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
  13224. + spare->tagByte6 = oob[6];
  13225. + spare->tagByte7 = oob[7];
  13226. + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
  13227. +
  13228. + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
  13229. +}
  13230. +#endif
  13231. +
  13232. +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
  13233. + const __u8 *data, const yaffs_Spare *spare)
  13234. +{
  13235. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13236. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13237. + struct mtd_oob_ops ops;
  13238. +#endif
  13239. + size_t dummy;
  13240. + int retval = 0;
  13241. +
  13242. + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
  13243. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13244. + __u8 spareAsBytes[8]; /* OOB */
  13245. +
  13246. + if (data && !spare)
  13247. + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
  13248. + &dummy, data);
  13249. + else if (spare) {
  13250. + if (dev->useNANDECC) {
  13251. + translate_spare2oob(spare, spareAsBytes);
  13252. + ops.mode = MTD_OOB_AUTO;
  13253. + ops.ooblen = 8; /* temp hack */
  13254. + } else {
  13255. + ops.mode = MTD_OOB_RAW;
  13256. + ops.ooblen = YAFFS_BYTES_PER_SPARE;
  13257. + }
  13258. + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
  13259. + ops.datbuf = (u8 *)data;
  13260. + ops.ooboffs = 0;
  13261. + ops.oobbuf = spareAsBytes;
  13262. + retval = mtd->write_oob(mtd, addr, &ops);
  13263. + }
  13264. +#else
  13265. + __u8 *spareAsBytes = (__u8 *) spare;
  13266. +
  13267. + if (data && spare) {
  13268. + if (dev->useNANDECC)
  13269. + retval =
  13270. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13271. + &dummy, data, spareAsBytes,
  13272. + &yaffs_oobinfo);
  13273. + else
  13274. + retval =
  13275. + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13276. + &dummy, data, spareAsBytes,
  13277. + &yaffs_noeccinfo);
  13278. + } else {
  13279. + if (data)
  13280. + retval =
  13281. + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13282. + data);
  13283. + if (spare)
  13284. + retval =
  13285. + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
  13286. + &dummy, spareAsBytes);
  13287. + }
  13288. +#endif
  13289. +
  13290. + if (retval == 0)
  13291. + return YAFFS_OK;
  13292. + else
  13293. + return YAFFS_FAIL;
  13294. +}
  13295. +
  13296. +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
  13297. + yaffs_Spare *spare)
  13298. +{
  13299. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13300. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13301. + struct mtd_oob_ops ops;
  13302. +#endif
  13303. + size_t dummy;
  13304. + int retval = 0;
  13305. +
  13306. + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
  13307. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  13308. + __u8 spareAsBytes[8]; /* OOB */
  13309. +
  13310. + if (data && !spare)
  13311. + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
  13312. + &dummy, data);
  13313. + else if (spare) {
  13314. + if (dev->useNANDECC) {
  13315. + ops.mode = MTD_OOB_AUTO;
  13316. + ops.ooblen = 8; /* temp hack */
  13317. + } else {
  13318. + ops.mode = MTD_OOB_RAW;
  13319. + ops.ooblen = YAFFS_BYTES_PER_SPARE;
  13320. + }
  13321. + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
  13322. + ops.datbuf = data;
  13323. + ops.ooboffs = 0;
  13324. + ops.oobbuf = spareAsBytes;
  13325. + retval = mtd->read_oob(mtd, addr, &ops);
  13326. + if (dev->useNANDECC)
  13327. + translate_oob2spare(spare, spareAsBytes);
  13328. + }
  13329. +#else
  13330. + __u8 *spareAsBytes = (__u8 *) spare;
  13331. +
  13332. + if (data && spare) {
  13333. + if (dev->useNANDECC) {
  13334. + /* Careful, this call adds 2 ints */
  13335. + /* to the end of the spare data. Calling function */
  13336. + /* should allocate enough memory for spare, */
  13337. + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
  13338. + retval =
  13339. + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13340. + &dummy, data, spareAsBytes,
  13341. + &yaffs_oobinfo);
  13342. + } else {
  13343. + retval =
  13344. + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
  13345. + &dummy, data, spareAsBytes,
  13346. + &yaffs_noeccinfo);
  13347. + }
  13348. + } else {
  13349. + if (data)
  13350. + retval =
  13351. + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
  13352. + data);
  13353. + if (spare)
  13354. + retval =
  13355. + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
  13356. + &dummy, spareAsBytes);
  13357. + }
  13358. +#endif
  13359. +
  13360. + if (retval == 0)
  13361. + return YAFFS_OK;
  13362. + else
  13363. + return YAFFS_FAIL;
  13364. +}
  13365. +
  13366. +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
  13367. +{
  13368. + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
  13369. + __u32 addr =
  13370. + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
  13371. + * dev->nChunksPerBlock;
  13372. + struct erase_info ei;
  13373. + int retval = 0;
  13374. +
  13375. + ei.mtd = mtd;
  13376. + ei.addr = addr;
  13377. + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
  13378. + ei.time = 1000;
  13379. + ei.retries = 2;
  13380. + ei.callback = NULL;
  13381. + ei.priv = (u_long) dev;
  13382. +
  13383. + /* Todo finish off the ei if required */
  13384. +
  13385. + sema_init(&dev->sem, 0);
  13386. +
  13387. + retval = mtd->erase(mtd, &ei);
  13388. +
  13389. + if (retval == 0)
  13390. + return YAFFS_OK;
  13391. + else
  13392. + return YAFFS_FAIL;
  13393. +}
  13394. +
  13395. +int nandmtd_InitialiseNAND(yaffs_Device *dev)
  13396. +{
  13397. + return YAFFS_OK;
  13398. +}
  13399. +
  13400. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif.h linux-2.6.32/fs/yaffs2/yaffs_mtdif.h
  13401. --- linux-2.6.32.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  13402. +++ linux-2.6.32/fs/yaffs2/yaffs_mtdif.h 2010-01-30 20:35:01.733074983 +0100
  13403. @@ -0,0 +1,32 @@
  13404. +/*
  13405. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13406. + *
  13407. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13408. + * for Toby Churchill Ltd and Brightstar Engineering
  13409. + *
  13410. + * Created by Charles Manning <charles@aleph1.co.uk>
  13411. + *
  13412. + * This program is free software; you can redistribute it and/or modify
  13413. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13414. + * published by the Free Software Foundation.
  13415. + *
  13416. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13417. + */
  13418. +
  13419. +#ifndef __YAFFS_MTDIF_H__
  13420. +#define __YAFFS_MTDIF_H__
  13421. +
  13422. +#include "yaffs_guts.h"
  13423. +
  13424. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
  13425. +extern struct nand_oobinfo yaffs_oobinfo;
  13426. +extern struct nand_oobinfo yaffs_noeccinfo;
  13427. +#endif
  13428. +
  13429. +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
  13430. + const __u8 *data, const yaffs_Spare *spare);
  13431. +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
  13432. + yaffs_Spare *spare);
  13433. +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber);
  13434. +int nandmtd_InitialiseNAND(yaffs_Device *dev);
  13435. +#endif
  13436. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_nand.c linux-2.6.32/fs/yaffs2/yaffs_nand.c
  13437. --- linux-2.6.32.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  13438. +++ linux-2.6.32/fs/yaffs2/yaffs_nand.c 2010-01-30 20:35:01.771938289 +0100
  13439. @@ -0,0 +1,135 @@
  13440. +/*
  13441. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13442. + *
  13443. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13444. + * for Toby Churchill Ltd and Brightstar Engineering
  13445. + *
  13446. + * Created by Charles Manning <charles@aleph1.co.uk>
  13447. + *
  13448. + * This program is free software; you can redistribute it and/or modify
  13449. + * it under the terms of the GNU General Public License version 2 as
  13450. + * published by the Free Software Foundation.
  13451. + */
  13452. +
  13453. +const char *yaffs_nand_c_version =
  13454. + "$Id: yaffs_nand.c,v 1.10 2009-03-06 17:20:54 wookey Exp $";
  13455. +
  13456. +#include "yaffs_nand.h"
  13457. +#include "yaffs_tagscompat.h"
  13458. +#include "yaffs_tagsvalidity.h"
  13459. +
  13460. +#include "yaffs_getblockinfo.h"
  13461. +
  13462. +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13463. + __u8 *buffer,
  13464. + yaffs_ExtendedTags *tags)
  13465. +{
  13466. + int result;
  13467. + yaffs_ExtendedTags localTags;
  13468. +
  13469. + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
  13470. +
  13471. + /* If there are no tags provided, use local tags to get prioritised gc working */
  13472. + if (!tags)
  13473. + tags = &localTags;
  13474. +
  13475. + if (dev->readChunkWithTagsFromNAND)
  13476. + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
  13477. + tags);
  13478. + else
  13479. + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
  13480. + realignedChunkInNAND,
  13481. + buffer,
  13482. + tags);
  13483. + if (tags &&
  13484. + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR) {
  13485. +
  13486. + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
  13487. + yaffs_HandleChunkError(dev, bi);
  13488. + }
  13489. +
  13490. + return result;
  13491. +}
  13492. +
  13493. +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  13494. + int chunkInNAND,
  13495. + const __u8 *buffer,
  13496. + yaffs_ExtendedTags *tags)
  13497. +{
  13498. + chunkInNAND -= dev->chunkOffset;
  13499. +
  13500. +
  13501. + if (tags) {
  13502. + tags->sequenceNumber = dev->sequenceNumber;
  13503. + tags->chunkUsed = 1;
  13504. + if (!yaffs_ValidateTags(tags)) {
  13505. + T(YAFFS_TRACE_ERROR,
  13506. + (TSTR("Writing uninitialised tags" TENDSTR)));
  13507. + YBUG();
  13508. + }
  13509. + T(YAFFS_TRACE_WRITE,
  13510. + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
  13511. + tags->objectId, tags->chunkId));
  13512. + } else {
  13513. + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
  13514. + YBUG();
  13515. + }
  13516. +
  13517. + if (dev->writeChunkWithTagsToNAND)
  13518. + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
  13519. + tags);
  13520. + else
  13521. + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
  13522. + chunkInNAND,
  13523. + buffer,
  13524. + tags);
  13525. +}
  13526. +
  13527. +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
  13528. +{
  13529. + blockNo -= dev->blockOffset;
  13530. +
  13531. +;
  13532. + if (dev->markNANDBlockBad)
  13533. + return dev->markNANDBlockBad(dev, blockNo);
  13534. + else
  13535. + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
  13536. +}
  13537. +
  13538. +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
  13539. + int blockNo,
  13540. + yaffs_BlockState *state,
  13541. + __u32 *sequenceNumber)
  13542. +{
  13543. + blockNo -= dev->blockOffset;
  13544. +
  13545. + if (dev->queryNANDBlock)
  13546. + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
  13547. + else
  13548. + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
  13549. + state,
  13550. + sequenceNumber);
  13551. +}
  13552. +
  13553. +
  13554. +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13555. + int blockInNAND)
  13556. +{
  13557. + int result;
  13558. +
  13559. + blockInNAND -= dev->blockOffset;
  13560. +
  13561. +
  13562. + dev->nBlockErasures++;
  13563. + result = dev->eraseBlockInNAND(dev, blockInNAND);
  13564. +
  13565. + return result;
  13566. +}
  13567. +
  13568. +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
  13569. +{
  13570. + return dev->initialiseNAND(dev);
  13571. +}
  13572. +
  13573. +
  13574. +
  13575. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.32/fs/yaffs2/yaffs_nandemul2k.h
  13576. --- linux-2.6.32.orig/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
  13577. +++ linux-2.6.32/fs/yaffs2/yaffs_nandemul2k.h 2010-01-30 20:35:01.811826092 +0100
  13578. @@ -0,0 +1,39 @@
  13579. +/*
  13580. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13581. + *
  13582. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13583. + * for Toby Churchill Ltd and Brightstar Engineering
  13584. + *
  13585. + * Created by Charles Manning <charles@aleph1.co.uk>
  13586. + *
  13587. + * This program is free software; you can redistribute it and/or modify
  13588. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13589. + * published by the Free Software Foundation.
  13590. + *
  13591. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13592. + */
  13593. +
  13594. +/* Interface to emulated NAND functions (2k page size) */
  13595. +
  13596. +#ifndef __YAFFS_NANDEMUL2K_H__
  13597. +#define __YAFFS_NANDEMUL2K_H__
  13598. +
  13599. +#include "yaffs_guts.h"
  13600. +
  13601. +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
  13602. + int chunkInNAND, const __u8 *data,
  13603. + const yaffs_ExtendedTags *tags);
  13604. +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
  13605. + int chunkInNAND, __u8 *data,
  13606. + yaffs_ExtendedTags *tags);
  13607. +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
  13608. +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
  13609. + yaffs_BlockState *state, __u32 *sequenceNumber);
  13610. +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13611. + int blockInNAND);
  13612. +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
  13613. +int nandemul2k_GetBytesPerChunk(void);
  13614. +int nandemul2k_GetChunksPerBlock(void);
  13615. +int nandemul2k_GetNumberOfBlocks(void);
  13616. +
  13617. +#endif
  13618. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_nand.h linux-2.6.32/fs/yaffs2/yaffs_nand.h
  13619. --- linux-2.6.32.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  13620. +++ linux-2.6.32/fs/yaffs2/yaffs_nand.h 2010-01-30 20:35:01.851862979 +0100
  13621. @@ -0,0 +1,44 @@
  13622. +/*
  13623. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13624. + *
  13625. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13626. + * for Toby Churchill Ltd and Brightstar Engineering
  13627. + *
  13628. + * Created by Charles Manning <charles@aleph1.co.uk>
  13629. + *
  13630. + * This program is free software; you can redistribute it and/or modify
  13631. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13632. + * published by the Free Software Foundation.
  13633. + *
  13634. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13635. + */
  13636. +
  13637. +#ifndef __YAFFS_NAND_H__
  13638. +#define __YAFFS_NAND_H__
  13639. +#include "yaffs_guts.h"
  13640. +
  13641. +
  13642. +
  13643. +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
  13644. + __u8 *buffer,
  13645. + yaffs_ExtendedTags *tags);
  13646. +
  13647. +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
  13648. + int chunkInNAND,
  13649. + const __u8 *buffer,
  13650. + yaffs_ExtendedTags *tags);
  13651. +
  13652. +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
  13653. +
  13654. +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
  13655. + int blockNo,
  13656. + yaffs_BlockState *state,
  13657. + unsigned *sequenceNumber);
  13658. +
  13659. +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
  13660. + int blockInNAND);
  13661. +
  13662. +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
  13663. +
  13664. +#endif
  13665. +
  13666. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags1.c linux-2.6.32/fs/yaffs2/yaffs_packedtags1.c
  13667. --- linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  13668. +++ linux-2.6.32/fs/yaffs2/yaffs_packedtags1.c 2010-01-30 20:35:01.902201298 +0100
  13669. @@ -0,0 +1,50 @@
  13670. +/*
  13671. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13672. + *
  13673. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13674. + * for Toby Churchill Ltd and Brightstar Engineering
  13675. + *
  13676. + * Created by Charles Manning <charles@aleph1.co.uk>
  13677. + *
  13678. + * This program is free software; you can redistribute it and/or modify
  13679. + * it under the terms of the GNU General Public License version 2 as
  13680. + * published by the Free Software Foundation.
  13681. + */
  13682. +
  13683. +#include "yaffs_packedtags1.h"
  13684. +#include "yportenv.h"
  13685. +
  13686. +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t)
  13687. +{
  13688. + pt->chunkId = t->chunkId;
  13689. + pt->serialNumber = t->serialNumber;
  13690. + pt->byteCount = t->byteCount;
  13691. + pt->objectId = t->objectId;
  13692. + pt->ecc = 0;
  13693. + pt->deleted = (t->chunkDeleted) ? 0 : 1;
  13694. + pt->unusedStuff = 0;
  13695. + pt->shouldBeFF = 0xFFFFFFFF;
  13696. +
  13697. +}
  13698. +
  13699. +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt)
  13700. +{
  13701. + static const __u8 allFF[] =
  13702. + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  13703. +0xff };
  13704. +
  13705. + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
  13706. + t->blockBad = 0;
  13707. + if (pt->shouldBeFF != 0xFFFFFFFF)
  13708. + t->blockBad = 1;
  13709. + t->chunkUsed = 1;
  13710. + t->objectId = pt->objectId;
  13711. + t->chunkId = pt->chunkId;
  13712. + t->byteCount = pt->byteCount;
  13713. + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13714. + t->chunkDeleted = (pt->deleted) ? 0 : 1;
  13715. + t->serialNumber = pt->serialNumber;
  13716. + } else {
  13717. + memset(t, 0, sizeof(yaffs_ExtendedTags));
  13718. + }
  13719. +}
  13720. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags1.h linux-2.6.32/fs/yaffs2/yaffs_packedtags1.h
  13721. --- linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  13722. +++ linux-2.6.32/fs/yaffs2/yaffs_packedtags1.h 2010-01-30 20:35:01.951826961 +0100
  13723. @@ -0,0 +1,37 @@
  13724. +/*
  13725. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13726. + *
  13727. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13728. + * for Toby Churchill Ltd and Brightstar Engineering
  13729. + *
  13730. + * Created by Charles Manning <charles@aleph1.co.uk>
  13731. + *
  13732. + * This program is free software; you can redistribute it and/or modify
  13733. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13734. + * published by the Free Software Foundation.
  13735. + *
  13736. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13737. + */
  13738. +
  13739. +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
  13740. +
  13741. +#ifndef __YAFFS_PACKEDTAGS1_H__
  13742. +#define __YAFFS_PACKEDTAGS1_H__
  13743. +
  13744. +#include "yaffs_guts.h"
  13745. +
  13746. +typedef struct {
  13747. + unsigned chunkId:20;
  13748. + unsigned serialNumber:2;
  13749. + unsigned byteCount:10;
  13750. + unsigned objectId:18;
  13751. + unsigned ecc:12;
  13752. + unsigned deleted:1;
  13753. + unsigned unusedStuff:1;
  13754. + unsigned shouldBeFF;
  13755. +
  13756. +} yaffs_PackedTags1;
  13757. +
  13758. +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t);
  13759. +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt);
  13760. +#endif
  13761. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags2.c linux-2.6.32/fs/yaffs2/yaffs_packedtags2.c
  13762. --- linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  13763. +++ linux-2.6.32/fs/yaffs2/yaffs_packedtags2.c 2010-01-30 20:35:01.991823846 +0100
  13764. @@ -0,0 +1,206 @@
  13765. +/*
  13766. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  13767. + *
  13768. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13769. + * for Toby Churchill Ltd and Brightstar Engineering
  13770. + *
  13771. + * Created by Charles Manning <charles@aleph1.co.uk>
  13772. + *
  13773. + * This program is free software; you can redistribute it and/or modify
  13774. + * it under the terms of the GNU General Public License version 2 as
  13775. + * published by the Free Software Foundation.
  13776. + */
  13777. +
  13778. +#include "yaffs_packedtags2.h"
  13779. +#include "yportenv.h"
  13780. +#include "yaffs_tagsvalidity.h"
  13781. +
  13782. +/* This code packs a set of extended tags into a binary structure for
  13783. + * NAND storage
  13784. + */
  13785. +
  13786. +/* Some of the information is "extra" struff which can be packed in to
  13787. + * speed scanning
  13788. + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
  13789. + */
  13790. +
  13791. +/* Extra flags applied to chunkId */
  13792. +
  13793. +#define EXTRA_HEADER_INFO_FLAG 0x80000000
  13794. +#define EXTRA_SHRINK_FLAG 0x40000000
  13795. +#define EXTRA_SHADOWS_FLAG 0x20000000
  13796. +#define EXTRA_SPARE_FLAGS 0x10000000
  13797. +
  13798. +#define ALL_EXTRA_FLAGS 0xF0000000
  13799. +
  13800. +/* Also, the top 4 bits of the object Id are set to the object type. */
  13801. +#define EXTRA_OBJECT_TYPE_SHIFT (28)
  13802. +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
  13803. +
  13804. +
  13805. +static void yaffs_DumpPackedTags2TagsPart(const yaffs_PackedTags2TagsPart *ptt)
  13806. +{
  13807. + T(YAFFS_TRACE_MTD,
  13808. + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
  13809. + ptt->objectId, ptt->chunkId, ptt->byteCount,
  13810. + ptt->sequenceNumber));
  13811. +}
  13812. +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
  13813. +{
  13814. + yaffs_DumpPackedTags2TagsPart(&pt->t);
  13815. +}
  13816. +
  13817. +static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
  13818. +{
  13819. + T(YAFFS_TRACE_MTD,
  13820. + (TSTR
  13821. + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
  13822. + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
  13823. + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
  13824. + t->sequenceNumber));
  13825. +
  13826. +}
  13827. +
  13828. +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
  13829. + const yaffs_ExtendedTags *t)
  13830. +{
  13831. + ptt->chunkId = t->chunkId;
  13832. + ptt->sequenceNumber = t->sequenceNumber;
  13833. + ptt->byteCount = t->byteCount;
  13834. + ptt->objectId = t->objectId;
  13835. +
  13836. + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
  13837. + /* Store the extra header info instead */
  13838. + /* We save the parent object in the chunkId */
  13839. + ptt->chunkId = EXTRA_HEADER_INFO_FLAG
  13840. + | t->extraParentObjectId;
  13841. + if (t->extraIsShrinkHeader)
  13842. + ptt->chunkId |= EXTRA_SHRINK_FLAG;
  13843. + if (t->extraShadows)
  13844. + ptt->chunkId |= EXTRA_SHADOWS_FLAG;
  13845. +
  13846. + ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
  13847. + ptt->objectId |=
  13848. + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
  13849. +
  13850. + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
  13851. + ptt->byteCount = t->extraEquivalentObjectId;
  13852. + else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
  13853. + ptt->byteCount = t->extraFileLength;
  13854. + else
  13855. + ptt->byteCount = 0;
  13856. + }
  13857. +
  13858. + yaffs_DumpPackedTags2TagsPart(ptt);
  13859. + yaffs_DumpTags2(t);
  13860. +}
  13861. +
  13862. +
  13863. +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
  13864. +{
  13865. + yaffs_PackTags2TagsPart(&pt->t, t);
  13866. +
  13867. +#ifndef YAFFS_IGNORE_TAGS_ECC
  13868. + {
  13869. + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
  13870. + sizeof(yaffs_PackedTags2TagsPart),
  13871. + &pt->ecc);
  13872. + }
  13873. +#endif
  13874. +}
  13875. +
  13876. +
  13877. +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
  13878. + yaffs_PackedTags2TagsPart *ptt)
  13879. +{
  13880. +
  13881. + memset(t, 0, sizeof(yaffs_ExtendedTags));
  13882. +
  13883. + yaffs_InitialiseTags(t);
  13884. +
  13885. + if (ptt->sequenceNumber != 0xFFFFFFFF) {
  13886. + t->blockBad = 0;
  13887. + t->chunkUsed = 1;
  13888. + t->objectId = ptt->objectId;
  13889. + t->chunkId = ptt->chunkId;
  13890. + t->byteCount = ptt->byteCount;
  13891. + t->chunkDeleted = 0;
  13892. + t->serialNumber = 0;
  13893. + t->sequenceNumber = ptt->sequenceNumber;
  13894. +
  13895. + /* Do extra header info stuff */
  13896. +
  13897. + if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
  13898. + t->chunkId = 0;
  13899. + t->byteCount = 0;
  13900. +
  13901. + t->extraHeaderInfoAvailable = 1;
  13902. + t->extraParentObjectId =
  13903. + ptt->chunkId & (~(ALL_EXTRA_FLAGS));
  13904. + t->extraIsShrinkHeader =
  13905. + (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
  13906. + t->extraShadows =
  13907. + (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
  13908. + t->extraObjectType =
  13909. + ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
  13910. + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
  13911. +
  13912. + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
  13913. + t->extraEquivalentObjectId = ptt->byteCount;
  13914. + else
  13915. + t->extraFileLength = ptt->byteCount;
  13916. + }
  13917. + }
  13918. +
  13919. + yaffs_DumpPackedTags2TagsPart(ptt);
  13920. + yaffs_DumpTags2(t);
  13921. +
  13922. +}
  13923. +
  13924. +
  13925. +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
  13926. +{
  13927. +
  13928. + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13929. +
  13930. + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
  13931. + /* Page is in use */
  13932. +#ifndef YAFFS_IGNORE_TAGS_ECC
  13933. + {
  13934. + yaffs_ECCOther ecc;
  13935. + int result;
  13936. + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
  13937. + sizeof
  13938. + (yaffs_PackedTags2TagsPart),
  13939. + &ecc);
  13940. + result =
  13941. + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
  13942. + sizeof
  13943. + (yaffs_PackedTags2TagsPart),
  13944. + &pt->ecc, &ecc);
  13945. + switch (result) {
  13946. + case 0:
  13947. + eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  13948. + break;
  13949. + case 1:
  13950. + eccResult = YAFFS_ECC_RESULT_FIXED;
  13951. + break;
  13952. + case -1:
  13953. + eccResult = YAFFS_ECC_RESULT_UNFIXED;
  13954. + break;
  13955. + default:
  13956. + eccResult = YAFFS_ECC_RESULT_UNKNOWN;
  13957. + }
  13958. + }
  13959. +#endif
  13960. + }
  13961. +
  13962. + yaffs_UnpackTags2TagsPart(t, &pt->t);
  13963. +
  13964. + t->eccResult = eccResult;
  13965. +
  13966. + yaffs_DumpPackedTags2(pt);
  13967. + yaffs_DumpTags2(t);
  13968. +
  13969. +}
  13970. +
  13971. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags2.h linux-2.6.32/fs/yaffs2/yaffs_packedtags2.h
  13972. --- linux-2.6.32.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  13973. +++ linux-2.6.32/fs/yaffs2/yaffs_packedtags2.h 2010-01-30 20:35:02.031973375 +0100
  13974. @@ -0,0 +1,43 @@
  13975. +/*
  13976. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  13977. + *
  13978. + * Copyright (C) 2002-2007 Aleph One Ltd.
  13979. + * for Toby Churchill Ltd and Brightstar Engineering
  13980. + *
  13981. + * Created by Charles Manning <charles@aleph1.co.uk>
  13982. + *
  13983. + * This program is free software; you can redistribute it and/or modify
  13984. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  13985. + * published by the Free Software Foundation.
  13986. + *
  13987. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  13988. + */
  13989. +
  13990. +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
  13991. +
  13992. +#ifndef __YAFFS_PACKEDTAGS2_H__
  13993. +#define __YAFFS_PACKEDTAGS2_H__
  13994. +
  13995. +#include "yaffs_guts.h"
  13996. +#include "yaffs_ecc.h"
  13997. +
  13998. +typedef struct {
  13999. + unsigned sequenceNumber;
  14000. + unsigned objectId;
  14001. + unsigned chunkId;
  14002. + unsigned byteCount;
  14003. +} yaffs_PackedTags2TagsPart;
  14004. +
  14005. +typedef struct {
  14006. + yaffs_PackedTags2TagsPart t;
  14007. + yaffs_ECCOther ecc;
  14008. +} yaffs_PackedTags2;
  14009. +
  14010. +/* Full packed tags with ECC, used for oob tags */
  14011. +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t);
  14012. +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt);
  14013. +
  14014. +/* Only the tags part (no ECC for use with inband tags */
  14015. +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *pt, const yaffs_ExtendedTags *t);
  14016. +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t, yaffs_PackedTags2TagsPart *pt);
  14017. +#endif
  14018. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_qsort.c linux-2.6.32/fs/yaffs2/yaffs_qsort.c
  14019. --- linux-2.6.32.orig/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
  14020. +++ linux-2.6.32/fs/yaffs2/yaffs_qsort.c 2010-01-30 20:35:02.071829765 +0100
  14021. @@ -0,0 +1,163 @@
  14022. +/*
  14023. + * Copyright (c) 1992, 1993
  14024. + * The Regents of the University of California. All rights reserved.
  14025. + *
  14026. + * Redistribution and use in source and binary forms, with or without
  14027. + * modification, are permitted provided that the following conditions
  14028. + * are met:
  14029. + * 1. Redistributions of source code must retain the above copyright
  14030. + * notice, this list of conditions and the following disclaimer.
  14031. + * 2. Redistributions in binary form must reproduce the above copyright
  14032. + * notice, this list of conditions and the following disclaimer in the
  14033. + * documentation and/or other materials provided with the distribution.
  14034. + * 3. Neither the name of the University nor the names of its contributors
  14035. + * may be used to endorse or promote products derived from this software
  14036. + * without specific prior written permission.
  14037. + *
  14038. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  14039. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14040. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14041. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  14042. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  14043. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  14044. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  14045. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14046. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  14047. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  14048. + * SUCH DAMAGE.
  14049. + */
  14050. +
  14051. +#include "yportenv.h"
  14052. +/* #include <linux/string.h> */
  14053. +
  14054. +/*
  14055. + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
  14056. + */
  14057. +#define swapcode(TYPE, parmi, parmj, n) do { \
  14058. + long i = (n) / sizeof (TYPE); \
  14059. + register TYPE *pi = (TYPE *) (parmi); \
  14060. + register TYPE *pj = (TYPE *) (parmj); \
  14061. + do { \
  14062. + register TYPE t = *pi; \
  14063. + *pi++ = *pj; \
  14064. + *pj++ = t; \
  14065. + } while (--i > 0); \
  14066. +} while (0)
  14067. +
  14068. +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
  14069. + es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
  14070. +
  14071. +static __inline void
  14072. +swapfunc(char *a, char *b, int n, int swaptype)
  14073. +{
  14074. + if (swaptype <= 1)
  14075. + swapcode(long, a, b, n);
  14076. + else
  14077. + swapcode(char, a, b, n);
  14078. +}
  14079. +
  14080. +#define yswap(a, b) do { \
  14081. + if (swaptype == 0) { \
  14082. + long t = *(long *)(a); \
  14083. + *(long *)(a) = *(long *)(b); \
  14084. + *(long *)(b) = t; \
  14085. + } else \
  14086. + swapfunc(a, b, es, swaptype); \
  14087. +} while (0)
  14088. +
  14089. +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
  14090. +
  14091. +static __inline char *
  14092. +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
  14093. +{
  14094. + return cmp(a, b) < 0 ?
  14095. + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
  14096. + : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
  14097. +}
  14098. +
  14099. +#ifndef min
  14100. +#define min(a, b) (((a) < (b)) ? (a) : (b))
  14101. +#endif
  14102. +
  14103. +void
  14104. +yaffs_qsort(void *aa, size_t n, size_t es,
  14105. + int (*cmp)(const void *, const void *))
  14106. +{
  14107. + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  14108. + int d, r, swaptype, swap_cnt;
  14109. + register char *a = aa;
  14110. +
  14111. +loop: SWAPINIT(a, es);
  14112. + swap_cnt = 0;
  14113. + if (n < 7) {
  14114. + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
  14115. + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  14116. + pl -= es)
  14117. + yswap(pl, pl - es);
  14118. + return;
  14119. + }
  14120. + pm = (char *)a + (n / 2) * es;
  14121. + if (n > 7) {
  14122. + pl = (char *)a;
  14123. + pn = (char *)a + (n - 1) * es;
  14124. + if (n > 40) {
  14125. + d = (n / 8) * es;
  14126. + pl = med3(pl, pl + d, pl + 2 * d, cmp);
  14127. + pm = med3(pm - d, pm, pm + d, cmp);
  14128. + pn = med3(pn - 2 * d, pn - d, pn, cmp);
  14129. + }
  14130. + pm = med3(pl, pm, pn, cmp);
  14131. + }
  14132. + yswap(a, pm);
  14133. + pa = pb = (char *)a + es;
  14134. +
  14135. + pc = pd = (char *)a + (n - 1) * es;
  14136. + for (;;) {
  14137. + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
  14138. + if (r == 0) {
  14139. + swap_cnt = 1;
  14140. + yswap(pa, pb);
  14141. + pa += es;
  14142. + }
  14143. + pb += es;
  14144. + }
  14145. + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
  14146. + if (r == 0) {
  14147. + swap_cnt = 1;
  14148. + yswap(pc, pd);
  14149. + pd -= es;
  14150. + }
  14151. + pc -= es;
  14152. + }
  14153. + if (pb > pc)
  14154. + break;
  14155. + yswap(pb, pc);
  14156. + swap_cnt = 1;
  14157. + pb += es;
  14158. + pc -= es;
  14159. + }
  14160. + if (swap_cnt == 0) { /* Switch to insertion sort */
  14161. + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
  14162. + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
  14163. + pl -= es)
  14164. + yswap(pl, pl - es);
  14165. + return;
  14166. + }
  14167. +
  14168. + pn = (char *)a + n * es;
  14169. + r = min(pa - (char *)a, pb - pa);
  14170. + vecswap(a, pb - r, r);
  14171. + r = min((long)(pd - pc), (long)(pn - pd - es));
  14172. + vecswap(pb, pn - r, r);
  14173. + r = pb - pa;
  14174. + if (r > es)
  14175. + yaffs_qsort(a, r / es, es, cmp);
  14176. + r = pd - pc;
  14177. + if (r > es) {
  14178. + /* Iterate rather than recurse to save stack space */
  14179. + a = pn - r;
  14180. + n = r / es;
  14181. + goto loop;
  14182. + }
  14183. +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
  14184. +}
  14185. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_qsort.h linux-2.6.32/fs/yaffs2/yaffs_qsort.h
  14186. --- linux-2.6.32.orig/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
  14187. +++ linux-2.6.32/fs/yaffs2/yaffs_qsort.h 2010-01-30 20:35:02.131826281 +0100
  14188. @@ -0,0 +1,23 @@
  14189. +/*
  14190. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14191. + *
  14192. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14193. + * for Toby Churchill Ltd and Brightstar Engineering
  14194. + *
  14195. + * Created by Charles Manning <charles@aleph1.co.uk>
  14196. + *
  14197. + * This program is free software; you can redistribute it and/or modify
  14198. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14199. + * published by the Free Software Foundation.
  14200. + *
  14201. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14202. + */
  14203. +
  14204. +
  14205. +#ifndef __YAFFS_QSORT_H__
  14206. +#define __YAFFS_QSORT_H__
  14207. +
  14208. +extern void yaffs_qsort(void *const base, size_t total_elems, size_t size,
  14209. + int (*cmp)(const void *, const void *));
  14210. +
  14211. +#endif
  14212. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_tagscompat.c linux-2.6.32/fs/yaffs2/yaffs_tagscompat.c
  14213. --- linux-2.6.32.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  14214. +++ linux-2.6.32/fs/yaffs2/yaffs_tagscompat.c 2010-01-30 20:35:02.173084965 +0100
  14215. @@ -0,0 +1,541 @@
  14216. +/*
  14217. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14218. + *
  14219. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14220. + * for Toby Churchill Ltd and Brightstar Engineering
  14221. + *
  14222. + * Created by Charles Manning <charles@aleph1.co.uk>
  14223. + *
  14224. + * This program is free software; you can redistribute it and/or modify
  14225. + * it under the terms of the GNU General Public License version 2 as
  14226. + * published by the Free Software Foundation.
  14227. + */
  14228. +
  14229. +#include "yaffs_guts.h"
  14230. +#include "yaffs_tagscompat.h"
  14231. +#include "yaffs_ecc.h"
  14232. +#include "yaffs_getblockinfo.h"
  14233. +
  14234. +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
  14235. +#ifdef NOTYET
  14236. +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
  14237. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  14238. + const __u8 *data,
  14239. + const yaffs_Spare *spare);
  14240. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  14241. + const yaffs_Spare *spare);
  14242. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
  14243. +#endif
  14244. +
  14245. +static const char yaffs_countBitsTable[256] = {
  14246. + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  14247. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14248. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14249. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  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. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14253. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14254. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  14255. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14256. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  14257. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  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. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  14261. + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  14262. +};
  14263. +
  14264. +int yaffs_CountBits(__u8 x)
  14265. +{
  14266. + int retVal;
  14267. + retVal = yaffs_countBitsTable[x];
  14268. + return retVal;
  14269. +}
  14270. +
  14271. +/********** Tags ECC calculations *********/
  14272. +
  14273. +void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
  14274. +{
  14275. + yaffs_ECCCalculate(data, spare->ecc1);
  14276. + yaffs_ECCCalculate(&data[256], spare->ecc2);
  14277. +}
  14278. +
  14279. +void yaffs_CalcTagsECC(yaffs_Tags *tags)
  14280. +{
  14281. + /* Calculate an ecc */
  14282. +
  14283. + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
  14284. + unsigned i, j;
  14285. + unsigned ecc = 0;
  14286. + unsigned bit = 0;
  14287. +
  14288. + tags->ecc = 0;
  14289. +
  14290. + for (i = 0; i < 8; i++) {
  14291. + for (j = 1; j & 0xff; j <<= 1) {
  14292. + bit++;
  14293. + if (b[i] & j)
  14294. + ecc ^= bit;
  14295. + }
  14296. + }
  14297. +
  14298. + tags->ecc = ecc;
  14299. +
  14300. +}
  14301. +
  14302. +int yaffs_CheckECCOnTags(yaffs_Tags *tags)
  14303. +{
  14304. + unsigned ecc = tags->ecc;
  14305. +
  14306. + yaffs_CalcTagsECC(tags);
  14307. +
  14308. + ecc ^= tags->ecc;
  14309. +
  14310. + if (ecc && ecc <= 64) {
  14311. + /* TODO: Handle the failure better. Retire? */
  14312. + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
  14313. +
  14314. + ecc--;
  14315. +
  14316. + b[ecc / 8] ^= (1 << (ecc & 7));
  14317. +
  14318. + /* Now recvalc the ecc */
  14319. + yaffs_CalcTagsECC(tags);
  14320. +
  14321. + return 1; /* recovered error */
  14322. + } else if (ecc) {
  14323. + /* Wierd ecc failure value */
  14324. + /* TODO Need to do somethiong here */
  14325. + return -1; /* unrecovered error */
  14326. + }
  14327. +
  14328. + return 0;
  14329. +}
  14330. +
  14331. +/********** Tags **********/
  14332. +
  14333. +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
  14334. + yaffs_Tags *tagsPtr)
  14335. +{
  14336. + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
  14337. +
  14338. + yaffs_CalcTagsECC(tagsPtr);
  14339. +
  14340. + sparePtr->tagByte0 = tu->asBytes[0];
  14341. + sparePtr->tagByte1 = tu->asBytes[1];
  14342. + sparePtr->tagByte2 = tu->asBytes[2];
  14343. + sparePtr->tagByte3 = tu->asBytes[3];
  14344. + sparePtr->tagByte4 = tu->asBytes[4];
  14345. + sparePtr->tagByte5 = tu->asBytes[5];
  14346. + sparePtr->tagByte6 = tu->asBytes[6];
  14347. + sparePtr->tagByte7 = tu->asBytes[7];
  14348. +}
  14349. +
  14350. +static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
  14351. + yaffs_Tags *tagsPtr)
  14352. +{
  14353. + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
  14354. + int result;
  14355. +
  14356. + tu->asBytes[0] = sparePtr->tagByte0;
  14357. + tu->asBytes[1] = sparePtr->tagByte1;
  14358. + tu->asBytes[2] = sparePtr->tagByte2;
  14359. + tu->asBytes[3] = sparePtr->tagByte3;
  14360. + tu->asBytes[4] = sparePtr->tagByte4;
  14361. + tu->asBytes[5] = sparePtr->tagByte5;
  14362. + tu->asBytes[6] = sparePtr->tagByte6;
  14363. + tu->asBytes[7] = sparePtr->tagByte7;
  14364. +
  14365. + result = yaffs_CheckECCOnTags(tagsPtr);
  14366. + if (result > 0)
  14367. + dev->tagsEccFixed++;
  14368. + else if (result < 0)
  14369. + dev->tagsEccUnfixed++;
  14370. +}
  14371. +
  14372. +static void yaffs_SpareInitialise(yaffs_Spare *spare)
  14373. +{
  14374. + memset(spare, 0xFF, sizeof(yaffs_Spare));
  14375. +}
  14376. +
  14377. +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
  14378. + int chunkInNAND, const __u8 *data,
  14379. + yaffs_Spare *spare)
  14380. +{
  14381. + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
  14382. + T(YAFFS_TRACE_ERROR,
  14383. + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
  14384. + chunkInNAND));
  14385. + return YAFFS_FAIL;
  14386. + }
  14387. +
  14388. + dev->nPageWrites++;
  14389. + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
  14390. +}
  14391. +
  14392. +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
  14393. + int chunkInNAND,
  14394. + __u8 *data,
  14395. + yaffs_Spare *spare,
  14396. + yaffs_ECCResult *eccResult,
  14397. + int doErrorCorrection)
  14398. +{
  14399. + int retVal;
  14400. + yaffs_Spare localSpare;
  14401. +
  14402. + dev->nPageReads++;
  14403. +
  14404. + if (!spare && data) {
  14405. + /* If we don't have a real spare, then we use a local one. */
  14406. + /* Need this for the calculation of the ecc */
  14407. + spare = &localSpare;
  14408. + }
  14409. +
  14410. + if (!dev->useNANDECC) {
  14411. + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
  14412. + if (data && doErrorCorrection) {
  14413. + /* Do ECC correction */
  14414. + /* Todo handle any errors */
  14415. + int eccResult1, eccResult2;
  14416. + __u8 calcEcc[3];
  14417. +
  14418. + yaffs_ECCCalculate(data, calcEcc);
  14419. + eccResult1 =
  14420. + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
  14421. + yaffs_ECCCalculate(&data[256], calcEcc);
  14422. + eccResult2 =
  14423. + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
  14424. +
  14425. + if (eccResult1 > 0) {
  14426. + T(YAFFS_TRACE_ERROR,
  14427. + (TSTR
  14428. + ("**>>yaffs ecc error fix performed on chunk %d:0"
  14429. + TENDSTR), chunkInNAND));
  14430. + dev->eccFixed++;
  14431. + } else if (eccResult1 < 0) {
  14432. + T(YAFFS_TRACE_ERROR,
  14433. + (TSTR
  14434. + ("**>>yaffs ecc error unfixed on chunk %d:0"
  14435. + TENDSTR), chunkInNAND));
  14436. + dev->eccUnfixed++;
  14437. + }
  14438. +
  14439. + if (eccResult2 > 0) {
  14440. + T(YAFFS_TRACE_ERROR,
  14441. + (TSTR
  14442. + ("**>>yaffs ecc error fix performed on chunk %d:1"
  14443. + TENDSTR), chunkInNAND));
  14444. + dev->eccFixed++;
  14445. + } else if (eccResult2 < 0) {
  14446. + T(YAFFS_TRACE_ERROR,
  14447. + (TSTR
  14448. + ("**>>yaffs ecc error unfixed on chunk %d:1"
  14449. + TENDSTR), chunkInNAND));
  14450. + dev->eccUnfixed++;
  14451. + }
  14452. +
  14453. + if (eccResult1 || eccResult2) {
  14454. + /* We had a data problem on this page */
  14455. + yaffs_HandleReadDataError(dev, chunkInNAND);
  14456. + }
  14457. +
  14458. + if (eccResult1 < 0 || eccResult2 < 0)
  14459. + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
  14460. + else if (eccResult1 > 0 || eccResult2 > 0)
  14461. + *eccResult = YAFFS_ECC_RESULT_FIXED;
  14462. + else
  14463. + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  14464. + }
  14465. + } else {
  14466. + /* Must allocate enough memory for spare+2*sizeof(int) */
  14467. + /* for ecc results from device. */
  14468. + struct yaffs_NANDSpare nspare;
  14469. +
  14470. + memset(&nspare, 0, sizeof(nspare));
  14471. +
  14472. + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
  14473. + (yaffs_Spare *) &nspare);
  14474. + memcpy(spare, &nspare, sizeof(yaffs_Spare));
  14475. + if (data && doErrorCorrection) {
  14476. + if (nspare.eccres1 > 0) {
  14477. + T(YAFFS_TRACE_ERROR,
  14478. + (TSTR
  14479. + ("**>>mtd ecc error fix performed on chunk %d:0"
  14480. + TENDSTR), chunkInNAND));
  14481. + } else if (nspare.eccres1 < 0) {
  14482. + T(YAFFS_TRACE_ERROR,
  14483. + (TSTR
  14484. + ("**>>mtd ecc error unfixed on chunk %d:0"
  14485. + TENDSTR), chunkInNAND));
  14486. + }
  14487. +
  14488. + if (nspare.eccres2 > 0) {
  14489. + T(YAFFS_TRACE_ERROR,
  14490. + (TSTR
  14491. + ("**>>mtd ecc error fix performed on chunk %d:1"
  14492. + TENDSTR), chunkInNAND));
  14493. + } else if (nspare.eccres2 < 0) {
  14494. + T(YAFFS_TRACE_ERROR,
  14495. + (TSTR
  14496. + ("**>>mtd ecc error unfixed on chunk %d:1"
  14497. + TENDSTR), chunkInNAND));
  14498. + }
  14499. +
  14500. + if (nspare.eccres1 || nspare.eccres2) {
  14501. + /* We had a data problem on this page */
  14502. + yaffs_HandleReadDataError(dev, chunkInNAND);
  14503. + }
  14504. +
  14505. + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
  14506. + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
  14507. + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
  14508. + *eccResult = YAFFS_ECC_RESULT_FIXED;
  14509. + else
  14510. + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
  14511. +
  14512. + }
  14513. + }
  14514. + return retVal;
  14515. +}
  14516. +
  14517. +#ifdef NOTYET
  14518. +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
  14519. + int chunkInNAND)
  14520. +{
  14521. + static int init;
  14522. + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
  14523. + static __u8 data[YAFFS_BYTES_PER_CHUNK];
  14524. + /* Might as well always allocate the larger size for */
  14525. + /* dev->useNANDECC == true; */
  14526. + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
  14527. +
  14528. + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
  14529. +
  14530. + if (!init) {
  14531. + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
  14532. + init = 1;
  14533. + }
  14534. +
  14535. + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
  14536. + return YAFFS_FAIL;
  14537. + if (memcmp(cmpbuf, spare, 16))
  14538. + return YAFFS_FAIL;
  14539. +
  14540. + return YAFFS_OK;
  14541. +
  14542. +}
  14543. +#endif
  14544. +
  14545. +/*
  14546. + * Functions for robustisizing
  14547. + */
  14548. +
  14549. +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
  14550. +{
  14551. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  14552. +
  14553. + /* Mark the block for retirement */
  14554. + yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
  14555. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  14556. + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
  14557. +
  14558. + /* TODO:
  14559. + * Just do a garbage collection on the affected block
  14560. + * then retire the block
  14561. + * NB recursion
  14562. + */
  14563. +}
  14564. +
  14565. +#ifdef NOTYET
  14566. +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
  14567. +{
  14568. +}
  14569. +
  14570. +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
  14571. + const __u8 *data,
  14572. + const yaffs_Spare *spare)
  14573. +{
  14574. +}
  14575. +
  14576. +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
  14577. + const yaffs_Spare *spare)
  14578. +{
  14579. +}
  14580. +
  14581. +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
  14582. +{
  14583. + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
  14584. +
  14585. + /* Mark the block for retirement */
  14586. + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
  14587. + /* Delete the chunk */
  14588. + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
  14589. +}
  14590. +
  14591. +static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
  14592. + const yaffs_Spare *s0, const yaffs_Spare *s1)
  14593. +{
  14594. +
  14595. + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
  14596. + s0->tagByte0 != s1->tagByte0 ||
  14597. + s0->tagByte1 != s1->tagByte1 ||
  14598. + s0->tagByte2 != s1->tagByte2 ||
  14599. + s0->tagByte3 != s1->tagByte3 ||
  14600. + s0->tagByte4 != s1->tagByte4 ||
  14601. + s0->tagByte5 != s1->tagByte5 ||
  14602. + s0->tagByte6 != s1->tagByte6 ||
  14603. + s0->tagByte7 != s1->tagByte7 ||
  14604. + s0->ecc1[0] != s1->ecc1[0] ||
  14605. + s0->ecc1[1] != s1->ecc1[1] ||
  14606. + s0->ecc1[2] != s1->ecc1[2] ||
  14607. + s0->ecc2[0] != s1->ecc2[0] ||
  14608. + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
  14609. + return 0;
  14610. + }
  14611. +
  14612. + return 1;
  14613. +}
  14614. +#endif /* NOTYET */
  14615. +
  14616. +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
  14617. + int chunkInNAND,
  14618. + const __u8 *data,
  14619. + const yaffs_ExtendedTags *eTags)
  14620. +{
  14621. + yaffs_Spare spare;
  14622. + yaffs_Tags tags;
  14623. +
  14624. + yaffs_SpareInitialise(&spare);
  14625. +
  14626. + if (eTags->chunkDeleted)
  14627. + spare.pageStatus = 0;
  14628. + else {
  14629. + tags.objectId = eTags->objectId;
  14630. + tags.chunkId = eTags->chunkId;
  14631. +
  14632. + tags.byteCountLSB = eTags->byteCount & 0x3ff;
  14633. +
  14634. + if (dev->nDataBytesPerChunk >= 1024)
  14635. + tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
  14636. + else
  14637. + tags.byteCountMSB = 3;
  14638. +
  14639. +
  14640. + tags.serialNumber = eTags->serialNumber;
  14641. +
  14642. + if (!dev->useNANDECC && data)
  14643. + yaffs_CalcECC(data, &spare);
  14644. +
  14645. + yaffs_LoadTagsIntoSpare(&spare, &tags);
  14646. +
  14647. + }
  14648. +
  14649. + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
  14650. +}
  14651. +
  14652. +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
  14653. + int chunkInNAND,
  14654. + __u8 *data,
  14655. + yaffs_ExtendedTags *eTags)
  14656. +{
  14657. +
  14658. + yaffs_Spare spare;
  14659. + yaffs_Tags tags;
  14660. + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
  14661. +
  14662. + static yaffs_Spare spareFF;
  14663. + static int init;
  14664. +
  14665. + if (!init) {
  14666. + memset(&spareFF, 0xFF, sizeof(spareFF));
  14667. + init = 1;
  14668. + }
  14669. +
  14670. + if (yaffs_ReadChunkFromNAND
  14671. + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
  14672. + /* eTags may be NULL */
  14673. + if (eTags) {
  14674. +
  14675. + int deleted =
  14676. + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
  14677. +
  14678. + eTags->chunkDeleted = deleted;
  14679. + eTags->eccResult = eccResult;
  14680. + eTags->blockBad = 0; /* We're reading it */
  14681. + /* therefore it is not a bad block */
  14682. + eTags->chunkUsed =
  14683. + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
  14684. + 0) ? 1 : 0;
  14685. +
  14686. + if (eTags->chunkUsed) {
  14687. + yaffs_GetTagsFromSpare(dev, &spare, &tags);
  14688. +
  14689. + eTags->objectId = tags.objectId;
  14690. + eTags->chunkId = tags.chunkId;
  14691. + eTags->byteCount = tags.byteCountLSB;
  14692. +
  14693. + if (dev->nDataBytesPerChunk >= 1024)
  14694. + eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
  14695. +
  14696. + eTags->serialNumber = tags.serialNumber;
  14697. + }
  14698. + }
  14699. +
  14700. + return YAFFS_OK;
  14701. + } else {
  14702. + return YAFFS_FAIL;
  14703. + }
  14704. +}
  14705. +
  14706. +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
  14707. + int blockInNAND)
  14708. +{
  14709. +
  14710. + yaffs_Spare spare;
  14711. +
  14712. + memset(&spare, 0xff, sizeof(yaffs_Spare));
  14713. +
  14714. + spare.blockStatus = 'Y';
  14715. +
  14716. + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
  14717. + &spare);
  14718. + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
  14719. + NULL, &spare);
  14720. +
  14721. + return YAFFS_OK;
  14722. +
  14723. +}
  14724. +
  14725. +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
  14726. + int blockNo,
  14727. + yaffs_BlockState *state,
  14728. + __u32 *sequenceNumber)
  14729. +{
  14730. +
  14731. + yaffs_Spare spare0, spare1;
  14732. + static yaffs_Spare spareFF;
  14733. + static int init;
  14734. + yaffs_ECCResult dummy;
  14735. +
  14736. + if (!init) {
  14737. + memset(&spareFF, 0xFF, sizeof(spareFF));
  14738. + init = 1;
  14739. + }
  14740. +
  14741. + *sequenceNumber = 0;
  14742. +
  14743. + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
  14744. + &spare0, &dummy, 1);
  14745. + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
  14746. + &spare1, &dummy, 1);
  14747. +
  14748. + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
  14749. + *state = YAFFS_BLOCK_STATE_DEAD;
  14750. + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
  14751. + *state = YAFFS_BLOCK_STATE_EMPTY;
  14752. + else
  14753. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  14754. +
  14755. + return YAFFS_OK;
  14756. +}
  14757. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_tagscompat.h linux-2.6.32/fs/yaffs2/yaffs_tagscompat.h
  14758. --- linux-2.6.32.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  14759. +++ linux-2.6.32/fs/yaffs2/yaffs_tagscompat.h 2010-01-30 20:35:02.213084840 +0100
  14760. @@ -0,0 +1,39 @@
  14761. +/*
  14762. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14763. + *
  14764. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14765. + * for Toby Churchill Ltd and Brightstar Engineering
  14766. + *
  14767. + * Created by Charles Manning <charles@aleph1.co.uk>
  14768. + *
  14769. + * This program is free software; you can redistribute it and/or modify
  14770. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14771. + * published by the Free Software Foundation.
  14772. + *
  14773. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14774. + */
  14775. +
  14776. +#ifndef __YAFFS_TAGSCOMPAT_H__
  14777. +#define __YAFFS_TAGSCOMPAT_H__
  14778. +
  14779. +#include "yaffs_guts.h"
  14780. +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
  14781. + int chunkInNAND,
  14782. + const __u8 *data,
  14783. + const yaffs_ExtendedTags *tags);
  14784. +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
  14785. + int chunkInNAND,
  14786. + __u8 *data,
  14787. + yaffs_ExtendedTags *tags);
  14788. +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
  14789. + int blockNo);
  14790. +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
  14791. + int blockNo,
  14792. + yaffs_BlockState *state,
  14793. + __u32 *sequenceNumber);
  14794. +
  14795. +void yaffs_CalcTagsECC(yaffs_Tags *tags);
  14796. +int yaffs_CheckECCOnTags(yaffs_Tags *tags);
  14797. +int yaffs_CountBits(__u8 byte);
  14798. +
  14799. +#endif
  14800. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.32/fs/yaffs2/yaffs_tagsvalidity.c
  14801. --- linux-2.6.32.orig/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
  14802. +++ linux-2.6.32/fs/yaffs2/yaffs_tagsvalidity.c 2010-01-30 20:35:02.251827798 +0100
  14803. @@ -0,0 +1,28 @@
  14804. +/*
  14805. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14806. + *
  14807. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14808. + * for Toby Churchill Ltd and Brightstar Engineering
  14809. + *
  14810. + * Created by Charles Manning <charles@aleph1.co.uk>
  14811. + *
  14812. + * This program is free software; you can redistribute it and/or modify
  14813. + * it under the terms of the GNU General Public License version 2 as
  14814. + * published by the Free Software Foundation.
  14815. + */
  14816. +
  14817. +#include "yaffs_tagsvalidity.h"
  14818. +
  14819. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
  14820. +{
  14821. + memset(tags, 0, sizeof(yaffs_ExtendedTags));
  14822. + tags->validMarker0 = 0xAAAAAAAA;
  14823. + tags->validMarker1 = 0x55555555;
  14824. +}
  14825. +
  14826. +int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
  14827. +{
  14828. + return (tags->validMarker0 == 0xAAAAAAAA &&
  14829. + tags->validMarker1 == 0x55555555);
  14830. +
  14831. +}
  14832. diff -Nur linux-2.6.32.orig/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.32/fs/yaffs2/yaffs_tagsvalidity.h
  14833. --- linux-2.6.32.orig/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
  14834. +++ linux-2.6.32/fs/yaffs2/yaffs_tagsvalidity.h 2010-01-30 20:35:02.292761803 +0100
  14835. @@ -0,0 +1,24 @@
  14836. +/*
  14837. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14838. + *
  14839. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14840. + * for Toby Churchill Ltd and Brightstar Engineering
  14841. + *
  14842. + * Created by Charles Manning <charles@aleph1.co.uk>
  14843. + *
  14844. + * This program is free software; you can redistribute it and/or modify
  14845. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14846. + * published by the Free Software Foundation.
  14847. + *
  14848. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14849. + */
  14850. +
  14851. +
  14852. +#ifndef __YAFFS_TAGS_VALIDITY_H__
  14853. +#define __YAFFS_TAGS_VALIDITY_H__
  14854. +
  14855. +#include "yaffs_guts.h"
  14856. +
  14857. +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
  14858. +int yaffs_ValidateTags(yaffs_ExtendedTags *tags);
  14859. +#endif
  14860. diff -Nur linux-2.6.32.orig/fs/yaffs2/yportenv.h linux-2.6.32/fs/yaffs2/yportenv.h
  14861. --- linux-2.6.32.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  14862. +++ linux-2.6.32/fs/yaffs2/yportenv.h 2010-01-30 20:35:02.332076176 +0100
  14863. @@ -0,0 +1,203 @@
  14864. +/*
  14865. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14866. + *
  14867. + * Copyright (C) 2002-2007 Aleph One Ltd.
  14868. + * for Toby Churchill Ltd and Brightstar Engineering
  14869. + *
  14870. + * Created by Charles Manning <charles@aleph1.co.uk>
  14871. + *
  14872. + * This program is free software; you can redistribute it and/or modify
  14873. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14874. + * published by the Free Software Foundation.
  14875. + *
  14876. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14877. + */
  14878. +
  14879. +
  14880. +#ifndef __YPORTENV_H__
  14881. +#define __YPORTENV_H__
  14882. +
  14883. +/*
  14884. + * Define the MTD version in terms of Linux Kernel versions
  14885. + * This allows yaffs to be used independantly of the kernel
  14886. + * as well as with it.
  14887. + */
  14888. +
  14889. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  14890. +
  14891. +#if defined CONFIG_YAFFS_WINCE
  14892. +
  14893. +#include "ywinceenv.h"
  14894. +
  14895. +#elif defined __KERNEL__
  14896. +
  14897. +#include "moduleconfig.h"
  14898. +
  14899. +/* Linux kernel */
  14900. +
  14901. +#include <linux/version.h>
  14902. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  14903. +
  14904. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  14905. +#include <linux/config.h>
  14906. +#endif
  14907. +#include <linux/kernel.h>
  14908. +#include <linux/mm.h>
  14909. +#include <linux/sched.h>
  14910. +#include <linux/string.h>
  14911. +#include <linux/slab.h>
  14912. +#include <linux/vmalloc.h>
  14913. +
  14914. +#define YCHAR char
  14915. +#define YUCHAR unsigned char
  14916. +#define _Y(x) x
  14917. +#define yaffs_strcat(a, b) strcat(a, b)
  14918. +#define yaffs_strcpy(a, b) strcpy(a, b)
  14919. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  14920. +#define yaffs_strncmp(a, b, c) strncmp(a, b, c)
  14921. +#define yaffs_strlen(s) strlen(s)
  14922. +#define yaffs_sprintf sprintf
  14923. +#define yaffs_toupper(a) toupper(a)
  14924. +
  14925. +#define Y_INLINE inline
  14926. +
  14927. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  14928. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  14929. +
  14930. +/* #define YPRINTF(x) printk x */
  14931. +#define YMALLOC(x) kmalloc(x, GFP_NOFS)
  14932. +#define YFREE(x) kfree(x)
  14933. +#define YMALLOC_ALT(x) vmalloc(x)
  14934. +#define YFREE_ALT(x) vfree(x)
  14935. +#define YMALLOC_DMA(x) YMALLOC(x)
  14936. +
  14937. +/* KR - added for use in scan so processes aren't blocked indefinitely. */
  14938. +#define YYIELD() schedule()
  14939. +
  14940. +#define YAFFS_ROOT_MODE 0666
  14941. +#define YAFFS_LOSTNFOUND_MODE 0666
  14942. +
  14943. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  14944. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  14945. +#define Y_TIME_CONVERT(x) (x).tv_sec
  14946. +#else
  14947. +#define Y_CURRENT_TIME CURRENT_TIME
  14948. +#define Y_TIME_CONVERT(x) (x)
  14949. +#endif
  14950. +
  14951. +#define yaffs_SumCompare(x, y) ((x) == (y))
  14952. +#define yaffs_strcmp(a, b) strcmp(a, b)
  14953. +
  14954. +#define TENDSTR "\n"
  14955. +#define TSTR(x) KERN_WARNING x
  14956. +#define TCONT(x) x
  14957. +#define TOUT(p) printk p
  14958. +
  14959. +#define yaffs_trace(mask, fmt, args...) \
  14960. + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
  14961. + printk(KERN_WARNING "yaffs: " fmt, ## args); \
  14962. + } while (0)
  14963. +
  14964. +#define compile_time_assertion(assertion) \
  14965. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  14966. +
  14967. +#elif defined CONFIG_YAFFS_DIRECT
  14968. +
  14969. +#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22)
  14970. +
  14971. +/* Direct interface */
  14972. +#include "ydirectenv.h"
  14973. +
  14974. +#elif defined CONFIG_YAFFS_UTIL
  14975. +
  14976. +/* Stuff for YAFFS utilities */
  14977. +
  14978. +#include "stdlib.h"
  14979. +#include "stdio.h"
  14980. +#include "string.h"
  14981. +
  14982. +#include "devextras.h"
  14983. +
  14984. +#define YMALLOC(x) malloc(x)
  14985. +#define YFREE(x) free(x)
  14986. +#define YMALLOC_ALT(x) malloc(x)
  14987. +#define YFREE_ALT(x) free(x)
  14988. +
  14989. +#define YCHAR char
  14990. +#define YUCHAR unsigned char
  14991. +#define _Y(x) x
  14992. +#define yaffs_strcat(a, b) strcat(a, b)
  14993. +#define yaffs_strcpy(a, b) strcpy(a, b)
  14994. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  14995. +#define yaffs_strlen(s) strlen(s)
  14996. +#define yaffs_sprintf sprintf
  14997. +#define yaffs_toupper(a) toupper(a)
  14998. +
  14999. +#define Y_INLINE inline
  15000. +
  15001. +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
  15002. +/* #define YALERT(s) YINFO(s) */
  15003. +
  15004. +#define TENDSTR "\n"
  15005. +#define TSTR(x) x
  15006. +#define TOUT(p) printf p
  15007. +
  15008. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  15009. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  15010. +/* #define YPRINTF(x) printf x */
  15011. +
  15012. +#define YAFFS_ROOT_MODE 0666
  15013. +#define YAFFS_LOSTNFOUND_MODE 0666
  15014. +
  15015. +#define yaffs_SumCompare(x, y) ((x) == (y))
  15016. +#define yaffs_strcmp(a, b) strcmp(a, b)
  15017. +
  15018. +#else
  15019. +/* Should have specified a configuration type */
  15020. +#error Unknown configuration
  15021. +
  15022. +#endif
  15023. +
  15024. +/* see yaffs_fs.c */
  15025. +extern unsigned int yaffs_traceMask;
  15026. +extern unsigned int yaffs_wr_attempts;
  15027. +
  15028. +/*
  15029. + * Tracing flags.
  15030. + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
  15031. + */
  15032. +
  15033. +#define YAFFS_TRACE_OS 0x00000002
  15034. +#define YAFFS_TRACE_ALLOCATE 0x00000004
  15035. +#define YAFFS_TRACE_SCAN 0x00000008
  15036. +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
  15037. +#define YAFFS_TRACE_ERASE 0x00000020
  15038. +#define YAFFS_TRACE_GC 0x00000040
  15039. +#define YAFFS_TRACE_WRITE 0x00000080
  15040. +#define YAFFS_TRACE_TRACING 0x00000100
  15041. +#define YAFFS_TRACE_DELETION 0x00000200
  15042. +#define YAFFS_TRACE_BUFFERS 0x00000400
  15043. +#define YAFFS_TRACE_NANDACCESS 0x00000800
  15044. +#define YAFFS_TRACE_GC_DETAIL 0x00001000
  15045. +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
  15046. +#define YAFFS_TRACE_MTD 0x00004000
  15047. +#define YAFFS_TRACE_CHECKPOINT 0x00008000
  15048. +
  15049. +#define YAFFS_TRACE_VERIFY 0x00010000
  15050. +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
  15051. +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
  15052. +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
  15053. +
  15054. +
  15055. +#define YAFFS_TRACE_ERROR 0x40000000
  15056. +#define YAFFS_TRACE_BUG 0x80000000
  15057. +#define YAFFS_TRACE_ALWAYS 0xF0000000
  15058. +
  15059. +
  15060. +#define T(mask, p) do { if ((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
  15061. +
  15062. +#ifndef YBUG
  15063. +#define YBUG() do {T(YAFFS_TRACE_BUG, (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR), __LINE__)); } while (0)
  15064. +#endif
  15065. +
  15066. +#endif