yaffs2.patch 447 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909
  1. diff -Nur linux-2.6.36.orig/fs/Kconfig linux-2.6.36/fs/Kconfig
  2. --- linux-2.6.36.orig/fs/Kconfig 2010-10-20 22:30:22.000000000 +0200
  3. +++ linux-2.6.36/fs/Kconfig 2010-11-18 18:15:51.000000000 +0100
  4. @@ -174,6 +174,7 @@
  5. source "fs/befs/Kconfig"
  6. source "fs/bfs/Kconfig"
  7. source "fs/efs/Kconfig"
  8. +source "fs/yaffs2/Kconfig"
  9. source "fs/jffs2/Kconfig"
  10. # UBIFS File system configuration
  11. source "fs/ubifs/Kconfig"
  12. diff -Nur linux-2.6.36.orig/fs/Makefile linux-2.6.36/fs/Makefile
  13. --- linux-2.6.36.orig/fs/Makefile 2010-10-20 22:30:22.000000000 +0200
  14. +++ linux-2.6.36/fs/Makefile 2010-11-18 18:15:51.000000000 +0100
  15. @@ -126,3 +126,4 @@
  16. obj-$(CONFIG_GFS2_FS) += gfs2/
  17. obj-$(CONFIG_EXOFS_FS) += exofs/
  18. obj-$(CONFIG_CEPH_FS) += ceph/
  19. +obj-$(CONFIG_YAFFS_FS) += yaffs2/
  20. diff -Nur linux-2.6.36.orig/fs/yaffs2/Kconfig linux-2.6.36/fs/yaffs2/Kconfig
  21. --- linux-2.6.36.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  22. +++ linux-2.6.36/fs/yaffs2/Kconfig 2010-11-18 18:15:51.000000000 +0100
  23. @@ -0,0 +1,161 @@
  24. +#
  25. +# YAFFS file system configurations
  26. +#
  27. +
  28. +config YAFFS_FS
  29. + tristate "YAFFS2 file system support"
  30. + default n
  31. + depends on MTD_BLOCK
  32. + select YAFFS_YAFFS1
  33. + select YAFFS_YAFFS2
  34. + help
  35. + YAFFS2, or Yet Another Flash Filing System, is a filing system
  36. + optimised for NAND Flash chips.
  37. +
  38. + To compile the YAFFS2 file system support as a module, choose M
  39. + here: the module will be called yaffs2.
  40. +
  41. + If unsure, say N.
  42. +
  43. + Further information on YAFFS2 is available at
  44. + <http://www.aleph1.co.uk/yaffs/>.
  45. +
  46. +config YAFFS_YAFFS1
  47. + bool "512 byte / page devices"
  48. + depends on YAFFS_FS
  49. + default y
  50. + help
  51. + Enable YAFFS1 support -- yaffs for 512 byte / page devices
  52. +
  53. + Not needed for 2K-page devices.
  54. +
  55. + If unsure, say Y.
  56. +
  57. +config YAFFS_9BYTE_TAGS
  58. + bool "Use older-style on-NAND data format with pageStatus byte"
  59. + depends on YAFFS_YAFFS1
  60. + default n
  61. + help
  62. +
  63. + Older-style on-NAND data format has a "pageStatus" byte to record
  64. + chunk/page state. This byte is zero when the page is discarded.
  65. + Choose this option if you have existing on-NAND data using this
  66. + format that you need to continue to support. New data written
  67. + also uses the older-style format. Note: Use of this option
  68. + generally requires that MTD's oob layout be adjusted to use the
  69. + older-style format. See notes on tags formats and MTD versions
  70. + in yaffs_mtdif1.c.
  71. +
  72. + If unsure, say N.
  73. +
  74. +config YAFFS_DOES_ECC
  75. + bool "Lets Yaffs do its own ECC"
  76. + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
  77. + default n
  78. + help
  79. + This enables Yaffs to use its own ECC functions instead of using
  80. + the ones from the generic MTD-NAND driver.
  81. +
  82. + If unsure, say N.
  83. +
  84. +config YAFFS_ECC_WRONG_ORDER
  85. + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
  86. + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
  87. + default n
  88. + help
  89. + This makes yaffs_ecc.c use the same ecc byte order as Steven
  90. + Hill's nand_ecc.c. If not set, then you get the same ecc byte
  91. + order as SmartMedia.
  92. +
  93. + If unsure, say N.
  94. +
  95. +config YAFFS_YAFFS2
  96. + bool "2048 byte (or larger) / page devices"
  97. + depends on YAFFS_FS
  98. + default y
  99. + help
  100. + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
  101. +
  102. + If unsure, say Y.
  103. +
  104. +config YAFFS_AUTO_YAFFS2
  105. + bool "Autoselect yaffs2 format"
  106. + depends on YAFFS_YAFFS2
  107. + default y
  108. + help
  109. + Without this, you need to explicitely use yaffs2 as the file
  110. + system type. With this, you can say "yaffs" and yaffs or yaffs2
  111. + will be used depending on the device page size (yaffs on
  112. + 512-byte page devices, yaffs2 on 2K page devices).
  113. +
  114. + If unsure, say Y.
  115. +
  116. +config YAFFS_DISABLE_TAGS_ECC
  117. + bool "Disable YAFFS from doing ECC on tags by default"
  118. + depends on YAFFS_FS && YAFFS_YAFFS2
  119. + default n
  120. + help
  121. + This defaults Yaffs to using its own ECC calculations on tags instead of
  122. + just relying on the MTD.
  123. + This behavior can also be overridden with tags_ecc_on and
  124. + tags_ecc_off mount options.
  125. +
  126. + If unsure, say N.
  127. +
  128. +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  129. + bool "Force chunk erase check"
  130. + depends on YAFFS_FS
  131. + default n
  132. + help
  133. + Normally YAFFS only checks chunks before writing until an erased
  134. + chunk is found. This helps to detect any partially written
  135. + chunks that might have happened due to power loss.
  136. +
  137. + Enabling this forces on the test that chunks are erased in flash
  138. + before writing to them. This takes more time but is potentially
  139. + a bit more secure.
  140. +
  141. + Suggest setting Y during development and ironing out driver
  142. + issues etc. Suggest setting to N if you want faster writing.
  143. +
  144. + If unsure, say Y.
  145. +
  146. +config YAFFS_EMPTY_LOST_AND_FOUND
  147. + bool "Empty lost and found on boot"
  148. + depends on YAFFS_FS
  149. + default n
  150. + help
  151. + If this is enabled then the contents of lost and found is
  152. + automatically dumped at mount.
  153. +
  154. + If unsure, say N.
  155. +
  156. +config YAFFS_DISABLE_BLOCK_REFRESHING
  157. + bool "Disable yaffs2 block refreshing"
  158. + depends on YAFFS_FS
  159. + default n
  160. + help
  161. + If this is set, then block refreshing is disabled.
  162. + Block refreshing infrequently refreshes the oldest block in
  163. + a yaffs2 file system. This mechanism helps to refresh flash to
  164. + mitigate against data loss. This is particularly useful for MLC.
  165. +
  166. + If unsure, say N.
  167. +
  168. +config YAFFS_DISABLE_BACKGROUND
  169. + bool "Disable yaffs2 background processing"
  170. + depends on YAFFS_FS
  171. + default n
  172. + help
  173. + If this is set, then background processing is disabled.
  174. + Background processing makes many foreground activities faster.
  175. +
  176. + If unsure, say N.
  177. +
  178. +config YAFFS_XATTR
  179. + bool "Enable yaffs2 xattr support"
  180. + depends on YAFFS_FS
  181. + default y
  182. + help
  183. + If this is set then yaffs2 will provide xattr support.
  184. + If unsure, say Y.
  185. diff -Nur linux-2.6.36.orig/fs/yaffs2/Makefile linux-2.6.36/fs/yaffs2/Makefile
  186. --- linux-2.6.36.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  187. +++ linux-2.6.36/fs/yaffs2/Makefile 2010-11-18 18:15:51.000000000 +0100
  188. @@ -0,0 +1,17 @@
  189. +#
  190. +# Makefile for the linux YAFFS filesystem routines.
  191. +#
  192. +
  193. +obj-$(CONFIG_YAFFS_FS) += yaffs.o
  194. +
  195. +yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o
  196. +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
  197. +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
  198. +yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
  199. +yaffs-y += yaffs_nameval.o yaffs_attribs.o
  200. +yaffs-y += yaffs_allocator.o
  201. +yaffs-y += yaffs_yaffs1.o
  202. +yaffs-y += yaffs_yaffs2.o
  203. +yaffs-y += yaffs_bitmap.o
  204. +yaffs-y += yaffs_verify.o
  205. +
  206. diff -Nur linux-2.6.36.orig/fs/yaffs2/moduleconfig.h linux-2.6.36/fs/yaffs2/moduleconfig.h
  207. --- linux-2.6.36.orig/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
  208. +++ linux-2.6.36/fs/yaffs2/moduleconfig.h 2010-11-18 18:49:02.000000000 +0100
  209. @@ -0,0 +1,81 @@
  210. +/*
  211. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  212. + *
  213. + * Copyright (C) 2002-2010 Aleph One Ltd.
  214. + * for Toby Churchill Ltd and Brightstar Engineering
  215. + *
  216. + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
  217. + *
  218. + * This program is free software; you can redistribute it and/or modify
  219. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  220. + * published by the Free Software Foundation.
  221. + *
  222. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  223. + */
  224. +
  225. +#ifndef __YAFFS_CONFIG_H__
  226. +#define __YAFFS_CONFIG_H__
  227. +
  228. +#ifdef YAFFS_OUT_OF_TREE
  229. +
  230. +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
  231. +#define CONFIG_YAFFS_FS
  232. +#define CONFIG_YAFFS_YAFFS1
  233. +#define CONFIG_YAFFS_YAFFS2
  234. +
  235. +/* These options are independent of each other. Select those that matter. */
  236. +
  237. +/* Default: Not selected */
  238. +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
  239. +/* #define CONFIG_YAFFS_DOES_ECC */
  240. +
  241. +/* Default: Selected */
  242. +/* Meaning: Yaffs does its own ECC on tags for packed tags rather than use mtd */
  243. +#define CONFIG_YAFFS_DOES_TAGS_ECC
  244. +
  245. +/* Default: Not selected */
  246. +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
  247. +/* CONFIG_YAFFS_DOES_ECC is set */
  248. +/* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
  249. +
  250. +/* Default: Not selected */
  251. +/* Meaning: Always test whether chunks are erased before writing to them.
  252. + Use during mtd debugging and init. */
  253. +/* #define CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED */
  254. +
  255. +/* Default: Not Selected */
  256. +/* Meaning: At mount automatically empty all files from lost and found. */
  257. +/* This is done to fix an old problem where rmdir was not checking for an */
  258. +/* empty directory. This can also be achieved with a mount option. */
  259. +#define CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
  260. +
  261. +/* Default: Unselected */
  262. +/* Meaning: Select to disable block refreshing. */
  263. +/* Block Refreshing periodically rewrites the oldest block. */
  264. +/* #define CONFIG_DISABLE_BLOCK_REFRESHING */
  265. +
  266. +/* Default: Unselected */
  267. +/* Meaning: Select to disable background processing */
  268. +/* #define CONFIG_DISABLE_BACKGROUND */
  269. +
  270. +/* Default: Selected */
  271. +/* Meaning: Enable XATTR support */
  272. +#define CONFIG_YAFFS_XATTR
  273. +
  274. +/*
  275. +Older-style on-NAND data format has a "page_status" byte to record
  276. +chunk/page state. This byte is zeroed when the page is discarded.
  277. +Choose this option if you have existing on-NAND data in this format
  278. +that you need to continue to support. New data written also uses the
  279. +older-style format.
  280. +Note: Use of this option generally requires that MTD's oob layout be
  281. +adjusted to use the older-style format. See notes on tags formats and
  282. +MTD versions in yaffs_mtdif1.c.
  283. +*/
  284. +/* Default: Not selected */
  285. +/* Meaning: Use older-style on-NAND data format with page_status byte */
  286. +/* #define CONFIG_YAFFS_9BYTE_TAGS */
  287. +
  288. +#endif /* YAFFS_OUT_OF_TREE */
  289. +
  290. +#endif /* __YAFFS_CONFIG_H__ */
  291. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.c linux-2.6.36/fs/yaffs2/yaffs_allocator.c
  292. --- linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.c 1970-01-01 01:00:00.000000000 +0100
  293. +++ linux-2.6.36/fs/yaffs2/yaffs_allocator.c 2010-11-18 18:15:51.000000000 +0100
  294. @@ -0,0 +1,397 @@
  295. +/*
  296. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  297. + *
  298. + * Copyright (C) 2002-2010 Aleph One Ltd.
  299. + * for Toby Churchill Ltd and Brightstar Engineering
  300. + *
  301. + * Created by Charles Manning <charles@aleph1.co.uk>
  302. + *
  303. + * This program is free software; you can redistribute it and/or modify
  304. + * it under the terms of the GNU General Public License version 2 as
  305. + * published by the Free Software Foundation.
  306. + */
  307. +
  308. +#include "yaffs_allocator.h"
  309. +#include "yaffs_guts.h"
  310. +#include "yaffs_trace.h"
  311. +#include "yportenv.h"
  312. +
  313. +#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
  314. +
  315. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  316. +{
  317. + dev = dev;
  318. +}
  319. +
  320. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  321. +{
  322. + dev = dev;
  323. +}
  324. +
  325. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  326. +{
  327. + return (struct yaffs_tnode *)YMALLOC(dev->tnode_size);
  328. +}
  329. +
  330. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  331. +{
  332. + dev = dev;
  333. + YFREE(tn);
  334. +}
  335. +
  336. +void yaffs_init_raw_objs(struct yaffs_dev *dev)
  337. +{
  338. + dev = dev;
  339. +}
  340. +
  341. +void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  342. +{
  343. + dev = dev;
  344. +}
  345. +
  346. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  347. +{
  348. + dev = dev;
  349. + return (struct yaffs_obj *)YMALLOC(sizeof(struct yaffs_obj));
  350. +}
  351. +
  352. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  353. +{
  354. +
  355. + dev = dev;
  356. + YFREE(obj);
  357. +}
  358. +
  359. +#else
  360. +
  361. +struct yaffs_tnode_list {
  362. + struct yaffs_tnode_list *next;
  363. + struct yaffs_tnode *tnodes;
  364. +};
  365. +
  366. +struct yaffs_obj_list {
  367. + struct yaffs_obj_list *next;
  368. + struct yaffs_obj *objects;
  369. +};
  370. +
  371. +struct yaffs_allocator {
  372. + int n_tnodes_created;
  373. + struct yaffs_tnode *free_tnodes;
  374. + int n_free_tnodes;
  375. + struct yaffs_tnode_list *alloc_tnode_list;
  376. +
  377. + int n_obj_created;
  378. + struct yaffs_obj *free_objs;
  379. + int n_free_objects;
  380. +
  381. + struct yaffs_obj_list *allocated_obj_list;
  382. +};
  383. +
  384. +static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
  385. +{
  386. +
  387. + struct yaffs_allocator *allocator =
  388. + (struct yaffs_allocator *)dev->allocator;
  389. +
  390. + struct yaffs_tnode_list *tmp;
  391. +
  392. + if (!allocator) {
  393. + YBUG();
  394. + return;
  395. + }
  396. +
  397. + while (allocator->alloc_tnode_list) {
  398. + tmp = allocator->alloc_tnode_list->next;
  399. +
  400. + YFREE(allocator->alloc_tnode_list->tnodes);
  401. + YFREE(allocator->alloc_tnode_list);
  402. + allocator->alloc_tnode_list = tmp;
  403. +
  404. + }
  405. +
  406. + allocator->free_tnodes = NULL;
  407. + allocator->n_free_tnodes = 0;
  408. + allocator->n_tnodes_created = 0;
  409. +}
  410. +
  411. +static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
  412. +{
  413. + struct yaffs_allocator *allocator = dev->allocator;
  414. +
  415. + if (allocator) {
  416. + allocator->alloc_tnode_list = NULL;
  417. + allocator->free_tnodes = NULL;
  418. + allocator->n_free_tnodes = 0;
  419. + allocator->n_tnodes_created = 0;
  420. + } else {
  421. + YBUG();
  422. + }
  423. +}
  424. +
  425. +static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
  426. +{
  427. + struct yaffs_allocator *allocator =
  428. + (struct yaffs_allocator *)dev->allocator;
  429. + int i;
  430. + struct yaffs_tnode *new_tnodes;
  431. + u8 *mem;
  432. + struct yaffs_tnode *curr;
  433. + struct yaffs_tnode *next;
  434. + struct yaffs_tnode_list *tnl;
  435. +
  436. + if (!allocator) {
  437. + YBUG();
  438. + return YAFFS_FAIL;
  439. + }
  440. +
  441. + if (n_tnodes < 1)
  442. + return YAFFS_OK;
  443. +
  444. + /* make these things */
  445. +
  446. + new_tnodes = YMALLOC(n_tnodes * dev->tnode_size);
  447. + mem = (u8 *) new_tnodes;
  448. +
  449. + if (!new_tnodes) {
  450. + T(YAFFS_TRACE_ERROR,
  451. + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
  452. + return YAFFS_FAIL;
  453. + }
  454. +
  455. + /* New hookup for wide tnodes */
  456. + for (i = 0; i < n_tnodes - 1; i++) {
  457. + curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
  458. + next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
  459. + curr->internal[0] = next;
  460. + }
  461. +
  462. + curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
  463. + curr->internal[0] = allocator->free_tnodes;
  464. + allocator->free_tnodes = (struct yaffs_tnode *)mem;
  465. +
  466. + allocator->n_free_tnodes += n_tnodes;
  467. + allocator->n_tnodes_created += n_tnodes;
  468. +
  469. + /* Now add this bunch of tnodes to a list for freeing up.
  470. + * NB If we can't add this to the management list it isn't fatal
  471. + * but it just means we can't free this bunch of tnodes later.
  472. + */
  473. +
  474. + tnl = YMALLOC(sizeof(struct yaffs_tnode_list));
  475. + if (!tnl) {
  476. + T(YAFFS_TRACE_ERROR,
  477. + (TSTR
  478. + ("yaffs: Could not add tnodes to management list" TENDSTR)));
  479. + return YAFFS_FAIL;
  480. + } else {
  481. + tnl->tnodes = new_tnodes;
  482. + tnl->next = allocator->alloc_tnode_list;
  483. + allocator->alloc_tnode_list = tnl;
  484. + }
  485. +
  486. + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
  487. +
  488. + return YAFFS_OK;
  489. +}
  490. +
  491. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  492. +{
  493. + struct yaffs_allocator *allocator =
  494. + (struct yaffs_allocator *)dev->allocator;
  495. + struct yaffs_tnode *tn = NULL;
  496. +
  497. + if (!allocator) {
  498. + YBUG();
  499. + return NULL;
  500. + }
  501. +
  502. + /* If there are none left make more */
  503. + if (!allocator->free_tnodes)
  504. + yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
  505. +
  506. + if (allocator->free_tnodes) {
  507. + tn = allocator->free_tnodes;
  508. + allocator->free_tnodes = allocator->free_tnodes->internal[0];
  509. + allocator->n_free_tnodes--;
  510. + }
  511. +
  512. + return tn;
  513. +}
  514. +
  515. +/* FreeTnode frees up a tnode and puts it back on the free list */
  516. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  517. +{
  518. + struct yaffs_allocator *allocator = dev->allocator;
  519. +
  520. + if (!allocator) {
  521. + YBUG();
  522. + return;
  523. + }
  524. +
  525. + if (tn) {
  526. + tn->internal[0] = allocator->free_tnodes;
  527. + allocator->free_tnodes = tn;
  528. + allocator->n_free_tnodes++;
  529. + }
  530. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  531. +}
  532. +
  533. +static void yaffs_init_raw_objs(struct yaffs_dev *dev)
  534. +{
  535. + struct yaffs_allocator *allocator = dev->allocator;
  536. +
  537. + if (allocator) {
  538. + allocator->allocated_obj_list = NULL;
  539. + allocator->free_objs = NULL;
  540. + allocator->n_free_objects = 0;
  541. + } else {
  542. + YBUG();
  543. + }
  544. +}
  545. +
  546. +static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  547. +{
  548. + struct yaffs_allocator *allocator = dev->allocator;
  549. + struct yaffs_obj_list *tmp;
  550. +
  551. + if (!allocator) {
  552. + YBUG();
  553. + return;
  554. + }
  555. +
  556. + while (allocator->allocated_obj_list) {
  557. + tmp = allocator->allocated_obj_list->next;
  558. + YFREE(allocator->allocated_obj_list->objects);
  559. + YFREE(allocator->allocated_obj_list);
  560. +
  561. + allocator->allocated_obj_list = tmp;
  562. + }
  563. +
  564. + allocator->free_objs = NULL;
  565. + allocator->n_free_objects = 0;
  566. + allocator->n_obj_created = 0;
  567. +}
  568. +
  569. +static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
  570. +{
  571. + struct yaffs_allocator *allocator = dev->allocator;
  572. +
  573. + int i;
  574. + struct yaffs_obj *new_objs;
  575. + struct yaffs_obj_list *list;
  576. +
  577. + if (!allocator) {
  578. + YBUG();
  579. + return YAFFS_FAIL;
  580. + }
  581. +
  582. + if (n_obj < 1)
  583. + return YAFFS_OK;
  584. +
  585. + /* make these things */
  586. + new_objs = YMALLOC(n_obj * sizeof(struct yaffs_obj));
  587. + list = YMALLOC(sizeof(struct yaffs_obj_list));
  588. +
  589. + if (!new_objs || !list) {
  590. + if (new_objs) {
  591. + YFREE(new_objs);
  592. + new_objs = NULL;
  593. + }
  594. + if (list) {
  595. + YFREE(list);
  596. + list = NULL;
  597. + }
  598. + T(YAFFS_TRACE_ALLOCATE,
  599. + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
  600. + return YAFFS_FAIL;
  601. + }
  602. +
  603. + /* Hook them into the free list */
  604. + for (i = 0; i < n_obj - 1; i++) {
  605. + new_objs[i].siblings.next =
  606. + (struct list_head *)(&new_objs[i + 1]);
  607. + }
  608. +
  609. + new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
  610. + allocator->free_objs = new_objs;
  611. + allocator->n_free_objects += n_obj;
  612. + allocator->n_obj_created += n_obj;
  613. +
  614. + /* Now add this bunch of Objects to a list for freeing up. */
  615. +
  616. + list->objects = new_objs;
  617. + list->next = allocator->allocated_obj_list;
  618. + allocator->allocated_obj_list = list;
  619. +
  620. + return YAFFS_OK;
  621. +}
  622. +
  623. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  624. +{
  625. + struct yaffs_obj *obj = NULL;
  626. + struct yaffs_allocator *allocator = dev->allocator;
  627. +
  628. + if (!allocator) {
  629. + YBUG();
  630. + return obj;
  631. + }
  632. +
  633. + /* If there are none left make more */
  634. + if (!allocator->free_objs)
  635. + yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
  636. +
  637. + if (allocator->free_objs) {
  638. + obj = allocator->free_objs;
  639. + allocator->free_objs =
  640. + (struct yaffs_obj *)(allocator->free_objs->siblings.next);
  641. + allocator->n_free_objects--;
  642. + }
  643. +
  644. + return obj;
  645. +}
  646. +
  647. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  648. +{
  649. +
  650. + struct yaffs_allocator *allocator = dev->allocator;
  651. +
  652. + if (!allocator)
  653. + YBUG();
  654. + else {
  655. + /* Link into the free list. */
  656. + obj->siblings.next = (struct list_head *)(allocator->free_objs);
  657. + allocator->free_objs = obj;
  658. + allocator->n_free_objects++;
  659. + }
  660. +}
  661. +
  662. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  663. +{
  664. + if (dev->allocator) {
  665. + yaffs_deinit_raw_tnodes(dev);
  666. + yaffs_deinit_raw_objs(dev);
  667. +
  668. + YFREE(dev->allocator);
  669. + dev->allocator = NULL;
  670. + } else {
  671. + YBUG();
  672. + }
  673. +}
  674. +
  675. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  676. +{
  677. + struct yaffs_allocator *allocator;
  678. +
  679. + if (!dev->allocator) {
  680. + allocator = YMALLOC(sizeof(struct yaffs_allocator));
  681. + if (allocator) {
  682. + dev->allocator = allocator;
  683. + yaffs_init_raw_tnodes(dev);
  684. + yaffs_init_raw_objs(dev);
  685. + }
  686. + } else {
  687. + YBUG();
  688. + }
  689. +}
  690. +
  691. +#endif
  692. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.h linux-2.6.36/fs/yaffs2/yaffs_allocator.h
  693. --- linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.h 1970-01-01 01:00:00.000000000 +0100
  694. +++ linux-2.6.36/fs/yaffs2/yaffs_allocator.h 2010-11-18 18:15:51.000000000 +0100
  695. @@ -0,0 +1,30 @@
  696. +/*
  697. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  698. + *
  699. + * Copyright (C) 2002-2010 Aleph One Ltd.
  700. + * for Toby Churchill Ltd and Brightstar Engineering
  701. + *
  702. + * Created by Charles Manning <charles@aleph1.co.uk>
  703. + *
  704. + * This program is free software; you can redistribute it and/or modify
  705. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  706. + * published by the Free Software Foundation.
  707. + *
  708. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  709. + */
  710. +
  711. +#ifndef __YAFFS_ALLOCATOR_H__
  712. +#define __YAFFS_ALLOCATOR_H__
  713. +
  714. +#include "yaffs_guts.h"
  715. +
  716. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev);
  717. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev);
  718. +
  719. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev);
  720. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn);
  721. +
  722. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev);
  723. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj);
  724. +
  725. +#endif
  726. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.c linux-2.6.36/fs/yaffs2/yaffs_attribs.c
  727. --- linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.c 1970-01-01 01:00:00.000000000 +0100
  728. +++ linux-2.6.36/fs/yaffs2/yaffs_attribs.c 2010-11-18 18:15:51.000000000 +0100
  729. @@ -0,0 +1,124 @@
  730. +/*
  731. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  732. + *
  733. + * Copyright (C) 2002-2010 Aleph One Ltd.
  734. + * for Toby Churchill Ltd and Brightstar Engineering
  735. + *
  736. + * Created by Charles Manning <charles@aleph1.co.uk>
  737. + *
  738. + * This program is free software; you can redistribute it and/or modify
  739. + * it under the terms of the GNU General Public License version 2 as
  740. + * published by the Free Software Foundation.
  741. + */
  742. +
  743. +#include "yaffs_guts.h"
  744. +#include "yaffs_attribs.h"
  745. +
  746. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
  747. +{
  748. + obj->yst_uid = oh->yst_uid;
  749. + obj->yst_gid = oh->yst_gid;
  750. + obj->yst_atime = oh->yst_atime;
  751. + obj->yst_mtime = oh->yst_mtime;
  752. + obj->yst_ctime = oh->yst_ctime;
  753. + obj->yst_rdev = oh->yst_rdev;
  754. +}
  755. +
  756. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
  757. +{
  758. + oh->yst_uid = obj->yst_uid;
  759. + oh->yst_gid = obj->yst_gid;
  760. + oh->yst_atime = obj->yst_atime;
  761. + oh->yst_mtime = obj->yst_mtime;
  762. + oh->yst_ctime = obj->yst_ctime;
  763. + oh->yst_rdev = obj->yst_rdev;
  764. +
  765. +}
  766. +
  767. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
  768. +{
  769. + obj->yst_mtime = Y_CURRENT_TIME;
  770. + if (do_a)
  771. + obj->yst_atime = obj->yst_atime;
  772. + if (do_c)
  773. + obj->yst_ctime = obj->yst_atime;
  774. +}
  775. +
  776. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
  777. +{
  778. + yaffs_load_current_time(obj, 1, 1);
  779. + obj->yst_rdev = rdev;
  780. + obj->yst_uid = uid;
  781. + obj->yst_gid = gid;
  782. +}
  783. +
  784. +loff_t yaffs_get_file_size(struct yaffs_obj *obj)
  785. +{
  786. + YCHAR *alias = NULL;
  787. + obj = yaffs_get_equivalent_obj(obj);
  788. +
  789. + switch (obj->variant_type) {
  790. + case YAFFS_OBJECT_TYPE_FILE:
  791. + return obj->variant.file_variant.file_size;
  792. + case YAFFS_OBJECT_TYPE_SYMLINK:
  793. + alias = obj->variant.symlink_variant.alias;
  794. + if (!alias)
  795. + return 0;
  796. + return yaffs_strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
  797. + default:
  798. + return 0;
  799. + }
  800. +}
  801. +
  802. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
  803. +{
  804. + unsigned int valid = attr->ia_valid;
  805. +
  806. + if (valid & ATTR_MODE)
  807. + obj->yst_mode = attr->ia_mode;
  808. + if (valid & ATTR_UID)
  809. + obj->yst_uid = attr->ia_uid;
  810. + if (valid & ATTR_GID)
  811. + obj->yst_gid = attr->ia_gid;
  812. +
  813. + if (valid & ATTR_ATIME)
  814. + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  815. + if (valid & ATTR_CTIME)
  816. + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  817. + if (valid & ATTR_MTIME)
  818. + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  819. +
  820. + if (valid & ATTR_SIZE)
  821. + yaffs_resize_file(obj, attr->ia_size);
  822. +
  823. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  824. +
  825. + return YAFFS_OK;
  826. +
  827. +}
  828. +
  829. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
  830. +{
  831. + unsigned int valid = 0;
  832. +
  833. + attr->ia_mode = obj->yst_mode;
  834. + valid |= ATTR_MODE;
  835. + attr->ia_uid = obj->yst_uid;
  836. + valid |= ATTR_UID;
  837. + attr->ia_gid = obj->yst_gid;
  838. + valid |= ATTR_GID;
  839. +
  840. + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  841. + valid |= ATTR_ATIME;
  842. + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  843. + valid |= ATTR_CTIME;
  844. + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  845. + valid |= ATTR_MTIME;
  846. +
  847. + attr->ia_size = yaffs_get_file_size(obj);
  848. + valid |= ATTR_SIZE;
  849. +
  850. + attr->ia_valid = valid;
  851. +
  852. + return YAFFS_OK;
  853. +}
  854. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.h linux-2.6.36/fs/yaffs2/yaffs_attribs.h
  855. --- linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.h 1970-01-01 01:00:00.000000000 +0100
  856. +++ linux-2.6.36/fs/yaffs2/yaffs_attribs.h 2010-11-18 18:15:51.000000000 +0100
  857. @@ -0,0 +1,28 @@
  858. +/*
  859. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  860. + *
  861. + * Copyright (C) 2002-2010 Aleph One Ltd.
  862. + * for Toby Churchill Ltd and Brightstar Engineering
  863. + *
  864. + * Created by Charles Manning <charles@aleph1.co.uk>
  865. + *
  866. + * This program is free software; you can redistribute it and/or modify
  867. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  868. + * published by the Free Software Foundation.
  869. + *
  870. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  871. + */
  872. +
  873. +#ifndef __YAFFS_ATTRIBS_H__
  874. +#define __YAFFS_ATTRIBS_H__
  875. +
  876. +#include "yaffs_guts.h"
  877. +
  878. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh);
  879. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj);
  880. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev);
  881. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
  882. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
  883. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
  884. +
  885. +#endif
  886. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.c linux-2.6.36/fs/yaffs2/yaffs_bitmap.c
  887. --- linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.c 1970-01-01 01:00:00.000000000 +0100
  888. +++ linux-2.6.36/fs/yaffs2/yaffs_bitmap.c 2010-11-18 18:15:51.000000000 +0100
  889. @@ -0,0 +1,104 @@
  890. +/*
  891. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  892. + *
  893. + * Copyright (C) 2002-2010 Aleph One Ltd.
  894. + * for Toby Churchill Ltd and Brightstar Engineering
  895. + *
  896. + * Created by Charles Manning <charles@aleph1.co.uk>
  897. + *
  898. + * This program is free software; you can redistribute it and/or modify
  899. + * it under the terms of the GNU General Public License version 2 as
  900. + * published by the Free Software Foundation.
  901. + */
  902. +
  903. +#include "yaffs_bitmap.h"
  904. +#include "yaffs_trace.h"
  905. +/*
  906. + * Chunk bitmap manipulations
  907. + */
  908. +
  909. +static Y_INLINE u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
  910. +{
  911. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  912. + T(YAFFS_TRACE_ERROR,
  913. + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
  914. + blk));
  915. + YBUG();
  916. + }
  917. + return dev->chunk_bits +
  918. + (dev->chunk_bit_stride * (blk - dev->internal_start_block));
  919. +}
  920. +
  921. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
  922. +{
  923. + if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
  924. + chunk < 0 || chunk >= dev->param.chunks_per_block) {
  925. + T(YAFFS_TRACE_ERROR,
  926. + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid" TENDSTR),
  927. + blk, chunk));
  928. + YBUG();
  929. + }
  930. +}
  931. +
  932. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
  933. +{
  934. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  935. +
  936. + memset(blk_bits, 0, dev->chunk_bit_stride);
  937. +}
  938. +
  939. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  940. +{
  941. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  942. +
  943. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  944. +
  945. + blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
  946. +}
  947. +
  948. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  949. +{
  950. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  951. +
  952. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  953. +
  954. + blk_bits[chunk / 8] |= (1 << (chunk & 7));
  955. +}
  956. +
  957. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  958. +{
  959. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  960. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  961. +
  962. + return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  963. +}
  964. +
  965. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
  966. +{
  967. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  968. + int i;
  969. + for (i = 0; i < dev->chunk_bit_stride; i++) {
  970. + if (*blk_bits)
  971. + return 1;
  972. + blk_bits++;
  973. + }
  974. + return 0;
  975. +}
  976. +
  977. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
  978. +{
  979. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  980. + int i;
  981. + int n = 0;
  982. + for (i = 0; i < dev->chunk_bit_stride; i++) {
  983. + u8 x = *blk_bits;
  984. + while (x) {
  985. + if (x & 1)
  986. + n++;
  987. + x >>= 1;
  988. + }
  989. +
  990. + blk_bits++;
  991. + }
  992. + return n;
  993. +}
  994. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.h linux-2.6.36/fs/yaffs2/yaffs_bitmap.h
  995. --- linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.h 1970-01-01 01:00:00.000000000 +0100
  996. +++ linux-2.6.36/fs/yaffs2/yaffs_bitmap.h 2010-11-18 18:15:51.000000000 +0100
  997. @@ -0,0 +1,33 @@
  998. +/*
  999. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1000. + *
  1001. + * Copyright (C) 2002-2010 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. +/*
  1014. + * Chunk bitmap manipulations
  1015. + */
  1016. +
  1017. +#ifndef __YAFFS_BITMAP_H__
  1018. +#define __YAFFS_BITMAP_H__
  1019. +
  1020. +#include "yaffs_guts.h"
  1021. +
  1022. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk);
  1023. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk);
  1024. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1025. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1026. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1027. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk);
  1028. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk);
  1029. +
  1030. +#endif
  1031. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.c linux-2.6.36/fs/yaffs2/yaffs_checkptrw.c
  1032. --- linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  1033. +++ linux-2.6.36/fs/yaffs2/yaffs_checkptrw.c 2010-11-18 18:15:51.000000000 +0100
  1034. @@ -0,0 +1,420 @@
  1035. +/*
  1036. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1037. + *
  1038. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1039. + * for Toby Churchill Ltd and Brightstar Engineering
  1040. + *
  1041. + * Created by Charles Manning <charles@aleph1.co.uk>
  1042. + *
  1043. + * This program is free software; you can redistribute it and/or modify
  1044. + * it under the terms of the GNU General Public License version 2 as
  1045. + * published by the Free Software Foundation.
  1046. + */
  1047. +
  1048. +#include "yaffs_checkptrw.h"
  1049. +#include "yaffs_getblockinfo.h"
  1050. +
  1051. +static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
  1052. +{
  1053. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1054. +
  1055. + T(YAFFS_TRACE_CHECKPOINT,
  1056. + (TSTR("checkpt blocks available = %d" TENDSTR), blocks_avail));
  1057. +
  1058. + return (blocks_avail <= 0) ? 0 : 1;
  1059. +}
  1060. +
  1061. +static int yaffs_checkpt_erase(struct yaffs_dev *dev)
  1062. +{
  1063. + int i;
  1064. +
  1065. + if (!dev->param.erase_fn)
  1066. + return 0;
  1067. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d" TENDSTR),
  1068. + dev->internal_start_block,
  1069. + dev->internal_end_block));
  1070. +
  1071. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  1072. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  1073. + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  1074. + T(YAFFS_TRACE_CHECKPOINT,
  1075. + (TSTR("erasing checkpt block %d" TENDSTR), i));
  1076. +
  1077. + dev->n_erasures++;
  1078. +
  1079. + if (dev->param.
  1080. + erase_fn(dev,
  1081. + i - dev->block_offset /* realign */ )) {
  1082. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1083. + dev->n_erased_blocks++;
  1084. + dev->n_free_chunks +=
  1085. + dev->param.chunks_per_block;
  1086. + } else {
  1087. + dev->param.bad_block_fn(dev, i);
  1088. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  1089. + }
  1090. + }
  1091. + }
  1092. +
  1093. + dev->blocks_in_checkpt = 0;
  1094. +
  1095. + return 1;
  1096. +}
  1097. +
  1098. +static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
  1099. +{
  1100. + int i;
  1101. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1102. + T(YAFFS_TRACE_CHECKPOINT,
  1103. + (TSTR
  1104. + ("allocating checkpt block: erased %d reserved %d avail %d next %d "
  1105. + TENDSTR), dev->n_erased_blocks, dev->param.n_reserved_blocks,
  1106. + blocks_avail, dev->checkpt_next_block));
  1107. +
  1108. + if (dev->checkpt_next_block >= 0 &&
  1109. + dev->checkpt_next_block <= dev->internal_end_block &&
  1110. + blocks_avail > 0) {
  1111. +
  1112. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1113. + i++) {
  1114. + struct yaffs_block_info *bi =
  1115. + yaffs_get_block_info(dev, i);
  1116. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  1117. + dev->checkpt_next_block = i + 1;
  1118. + dev->checkpt_cur_block = i;
  1119. + T(YAFFS_TRACE_CHECKPOINT,
  1120. + (TSTR("allocating checkpt block %d" TENDSTR),
  1121. + i));
  1122. + return;
  1123. + }
  1124. + }
  1125. + }
  1126. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks" TENDSTR)));
  1127. +
  1128. + dev->checkpt_next_block = -1;
  1129. + dev->checkpt_cur_block = -1;
  1130. +}
  1131. +
  1132. +static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
  1133. +{
  1134. + int i;
  1135. + struct yaffs_ext_tags tags;
  1136. +
  1137. + T(YAFFS_TRACE_CHECKPOINT,
  1138. + (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
  1139. + dev->blocks_in_checkpt, dev->checkpt_next_block));
  1140. +
  1141. + if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
  1142. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1143. + i++) {
  1144. + int chunk = i * dev->param.chunks_per_block;
  1145. + int realigned_chunk = chunk - dev->chunk_offset;
  1146. +
  1147. + dev->param.read_chunk_tags_fn(dev, realigned_chunk,
  1148. + NULL, &tags);
  1149. + T(YAFFS_TRACE_CHECKPOINT,
  1150. + (TSTR
  1151. + ("find next checkpt block: search: block %d oid %d seq %d eccr %d"
  1152. + TENDSTR), i, tags.obj_id, tags.seq_number,
  1153. + tags.ecc_result));
  1154. +
  1155. + if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  1156. + /* Right kind of block */
  1157. + dev->checkpt_next_block = tags.obj_id;
  1158. + dev->checkpt_cur_block = i;
  1159. + dev->checkpt_block_list[dev->
  1160. + blocks_in_checkpt] = i;
  1161. + dev->blocks_in_checkpt++;
  1162. + T(YAFFS_TRACE_CHECKPOINT,
  1163. + (TSTR("found checkpt block %d" TENDSTR), i));
  1164. + return;
  1165. + }
  1166. + }
  1167. +
  1168. + T(YAFFS_TRACE_CHECKPOINT,
  1169. + (TSTR("found no more checkpt blocks" TENDSTR)));
  1170. +
  1171. + dev->checkpt_next_block = -1;
  1172. + dev->checkpt_cur_block = -1;
  1173. +}
  1174. +
  1175. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
  1176. +{
  1177. +
  1178. + dev->checkpt_open_write = writing;
  1179. +
  1180. + /* Got the functions we need? */
  1181. + if (!dev->param.write_chunk_tags_fn ||
  1182. + !dev->param.read_chunk_tags_fn ||
  1183. + !dev->param.erase_fn || !dev->param.bad_block_fn)
  1184. + return 0;
  1185. +
  1186. + if (writing && !yaffs2_checkpt_space_ok(dev))
  1187. + return 0;
  1188. +
  1189. + if (!dev->checkpt_buffer)
  1190. + dev->checkpt_buffer =
  1191. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  1192. + if (!dev->checkpt_buffer)
  1193. + return 0;
  1194. +
  1195. + dev->checkpt_page_seq = 0;
  1196. + dev->checkpt_byte_count = 0;
  1197. + dev->checkpt_sum = 0;
  1198. + dev->checkpt_xor = 0;
  1199. + dev->checkpt_cur_block = -1;
  1200. + dev->checkpt_cur_chunk = -1;
  1201. + dev->checkpt_next_block = dev->internal_start_block;
  1202. +
  1203. + /* Erase all the blocks in the checkpoint area */
  1204. + if (writing) {
  1205. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1206. + dev->checkpt_byte_offs = 0;
  1207. + return yaffs_checkpt_erase(dev);
  1208. + } else {
  1209. + int i;
  1210. + /* Set to a value that will kick off a read */
  1211. + dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
  1212. + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
  1213. + * going to be way more than we need */
  1214. + dev->blocks_in_checkpt = 0;
  1215. + dev->checkpt_max_blocks =
  1216. + (dev->internal_end_block - dev->internal_start_block) / 16 +
  1217. + 2;
  1218. + dev->checkpt_block_list =
  1219. + YMALLOC(sizeof(int) * dev->checkpt_max_blocks);
  1220. + if (!dev->checkpt_block_list)
  1221. + return 0;
  1222. +
  1223. + for (i = 0; i < dev->checkpt_max_blocks; i++)
  1224. + dev->checkpt_block_list[i] = -1;
  1225. + }
  1226. +
  1227. + return 1;
  1228. +}
  1229. +
  1230. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  1231. +{
  1232. + u32 composite_sum;
  1233. + composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
  1234. + *sum = composite_sum;
  1235. + return 1;
  1236. +}
  1237. +
  1238. +static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  1239. +{
  1240. + int chunk;
  1241. + int realigned_chunk;
  1242. +
  1243. + struct yaffs_ext_tags tags;
  1244. +
  1245. + if (dev->checkpt_cur_block < 0) {
  1246. + yaffs2_checkpt_find_erased_block(dev);
  1247. + dev->checkpt_cur_chunk = 0;
  1248. + }
  1249. +
  1250. + if (dev->checkpt_cur_block < 0)
  1251. + return 0;
  1252. +
  1253. + tags.is_deleted = 0;
  1254. + tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  1255. + tags.chunk_id = dev->checkpt_page_seq + 1;
  1256. + tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  1257. + tags.n_bytes = dev->data_bytes_per_chunk;
  1258. + if (dev->checkpt_cur_chunk == 0) {
  1259. + /* First chunk we write for the block? Set block state to
  1260. + checkpoint */
  1261. + struct yaffs_block_info *bi =
  1262. + yaffs_get_block_info(dev, dev->checkpt_cur_block);
  1263. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1264. + dev->blocks_in_checkpt++;
  1265. + }
  1266. +
  1267. + chunk =
  1268. + dev->checkpt_cur_block * dev->param.chunks_per_block +
  1269. + dev->checkpt_cur_chunk;
  1270. +
  1271. + T(YAFFS_TRACE_CHECKPOINT,
  1272. + (TSTR
  1273. + ("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
  1274. + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, tags.obj_id,
  1275. + tags.chunk_id));
  1276. +
  1277. + realigned_chunk = chunk - dev->chunk_offset;
  1278. +
  1279. + dev->n_page_writes++;
  1280. +
  1281. + dev->param.write_chunk_tags_fn(dev, realigned_chunk,
  1282. + dev->checkpt_buffer, &tags);
  1283. + dev->checkpt_byte_offs = 0;
  1284. + dev->checkpt_page_seq++;
  1285. + dev->checkpt_cur_chunk++;
  1286. + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  1287. + dev->checkpt_cur_chunk = 0;
  1288. + dev->checkpt_cur_block = -1;
  1289. + }
  1290. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1291. +
  1292. + return 1;
  1293. +}
  1294. +
  1295. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  1296. +{
  1297. + int i = 0;
  1298. + int ok = 1;
  1299. +
  1300. + u8 *data_bytes = (u8 *) data;
  1301. +
  1302. + if (!dev->checkpt_buffer)
  1303. + return 0;
  1304. +
  1305. + if (!dev->checkpt_open_write)
  1306. + return -1;
  1307. +
  1308. + while (i < n_bytes && ok) {
  1309. + dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  1310. + dev->checkpt_sum += *data_bytes;
  1311. + dev->checkpt_xor ^= *data_bytes;
  1312. +
  1313. + dev->checkpt_byte_offs++;
  1314. + i++;
  1315. + data_bytes++;
  1316. + dev->checkpt_byte_count++;
  1317. +
  1318. + if (dev->checkpt_byte_offs < 0 ||
  1319. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  1320. + ok = yaffs2_checkpt_flush_buffer(dev);
  1321. + }
  1322. +
  1323. + return i;
  1324. +}
  1325. +
  1326. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  1327. +{
  1328. + int i = 0;
  1329. + int ok = 1;
  1330. + struct yaffs_ext_tags tags;
  1331. +
  1332. + int chunk;
  1333. + int realigned_chunk;
  1334. +
  1335. + u8 *data_bytes = (u8 *) data;
  1336. +
  1337. + if (!dev->checkpt_buffer)
  1338. + return 0;
  1339. +
  1340. + if (dev->checkpt_open_write)
  1341. + return -1;
  1342. +
  1343. + while (i < n_bytes && ok) {
  1344. +
  1345. + if (dev->checkpt_byte_offs < 0 ||
  1346. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  1347. +
  1348. + if (dev->checkpt_cur_block < 0) {
  1349. + yaffs2_checkpt_find_block(dev);
  1350. + dev->checkpt_cur_chunk = 0;
  1351. + }
  1352. +
  1353. + if (dev->checkpt_cur_block < 0)
  1354. + ok = 0;
  1355. + else {
  1356. + chunk = dev->checkpt_cur_block *
  1357. + dev->param.chunks_per_block +
  1358. + dev->checkpt_cur_chunk;
  1359. +
  1360. + realigned_chunk = chunk - dev->chunk_offset;
  1361. +
  1362. + dev->n_page_reads++;
  1363. +
  1364. + /* read in the next chunk */
  1365. + dev->param.read_chunk_tags_fn(dev,
  1366. + realigned_chunk,
  1367. + dev->
  1368. + checkpt_buffer,
  1369. + &tags);
  1370. +
  1371. + if (tags.chunk_id != (dev->checkpt_page_seq + 1)
  1372. + || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
  1373. + || tags.seq_number !=
  1374. + YAFFS_SEQUENCE_CHECKPOINT_DATA)
  1375. + ok = 0;
  1376. +
  1377. + dev->checkpt_byte_offs = 0;
  1378. + dev->checkpt_page_seq++;
  1379. + dev->checkpt_cur_chunk++;
  1380. +
  1381. + if (dev->checkpt_cur_chunk >=
  1382. + dev->param.chunks_per_block)
  1383. + dev->checkpt_cur_block = -1;
  1384. + }
  1385. + }
  1386. +
  1387. + if (ok) {
  1388. + *data_bytes =
  1389. + dev->checkpt_buffer[dev->checkpt_byte_offs];
  1390. + dev->checkpt_sum += *data_bytes;
  1391. + dev->checkpt_xor ^= *data_bytes;
  1392. + dev->checkpt_byte_offs++;
  1393. + i++;
  1394. + data_bytes++;
  1395. + dev->checkpt_byte_count++;
  1396. + }
  1397. + }
  1398. +
  1399. + return i;
  1400. +}
  1401. +
  1402. +int yaffs_checkpt_close(struct yaffs_dev *dev)
  1403. +{
  1404. +
  1405. + if (dev->checkpt_open_write) {
  1406. + if (dev->checkpt_byte_offs != 0)
  1407. + yaffs2_checkpt_flush_buffer(dev);
  1408. + } else if (dev->checkpt_block_list) {
  1409. + int i;
  1410. + for (i = 0;
  1411. + i < dev->blocks_in_checkpt
  1412. + && dev->checkpt_block_list[i] >= 0; i++) {
  1413. + int blk = dev->checkpt_block_list[i];
  1414. + struct yaffs_block_info *bi = NULL;
  1415. + if (dev->internal_start_block <= blk
  1416. + && blk <= dev->internal_end_block)
  1417. + bi = yaffs_get_block_info(dev, blk);
  1418. + if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  1419. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1420. + else {
  1421. + /* Todo this looks odd... */
  1422. + }
  1423. + }
  1424. + YFREE(dev->checkpt_block_list);
  1425. + dev->checkpt_block_list = NULL;
  1426. + }
  1427. +
  1428. + dev->n_free_chunks -=
  1429. + dev->blocks_in_checkpt * dev->param.chunks_per_block;
  1430. + dev->n_erased_blocks -= dev->blocks_in_checkpt;
  1431. +
  1432. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
  1433. + dev->checkpt_byte_count));
  1434. +
  1435. + if (dev->checkpt_buffer) {
  1436. + /* free the buffer */
  1437. + YFREE(dev->checkpt_buffer);
  1438. + dev->checkpt_buffer = NULL;
  1439. + return 1;
  1440. + } else {
  1441. + return 0;
  1442. + }
  1443. +}
  1444. +
  1445. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  1446. +{
  1447. + /* Erase the checkpoint data */
  1448. +
  1449. + T(YAFFS_TRACE_CHECKPOINT,
  1450. + (TSTR("checkpoint invalidate of %d blocks" TENDSTR),
  1451. + dev->blocks_in_checkpt));
  1452. +
  1453. + return yaffs_checkpt_erase(dev);
  1454. +}
  1455. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.h linux-2.6.36/fs/yaffs2/yaffs_checkptrw.h
  1456. --- linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  1457. +++ linux-2.6.36/fs/yaffs2/yaffs_checkptrw.h 2010-11-18 18:15:51.000000000 +0100
  1458. @@ -0,0 +1,33 @@
  1459. +/*
  1460. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1461. + *
  1462. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1463. + * for Toby Churchill Ltd and Brightstar Engineering
  1464. + *
  1465. + * Created by Charles Manning <charles@aleph1.co.uk>
  1466. + *
  1467. + * This program is free software; you can redistribute it and/or modify
  1468. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1469. + * published by the Free Software Foundation.
  1470. + *
  1471. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1472. + */
  1473. +
  1474. +#ifndef __YAFFS_CHECKPTRW_H__
  1475. +#define __YAFFS_CHECKPTRW_H__
  1476. +
  1477. +#include "yaffs_guts.h"
  1478. +
  1479. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing);
  1480. +
  1481. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes);
  1482. +
  1483. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes);
  1484. +
  1485. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum);
  1486. +
  1487. +int yaffs_checkpt_close(struct yaffs_dev *dev);
  1488. +
  1489. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev);
  1490. +
  1491. +#endif
  1492. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.c linux-2.6.36/fs/yaffs2/yaffs_ecc.c
  1493. --- linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1494. +++ linux-2.6.36/fs/yaffs2/yaffs_ecc.c 2010-11-18 18:15:51.000000000 +0100
  1495. @@ -0,0 +1,322 @@
  1496. +/*
  1497. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1498. + *
  1499. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1500. + * for Toby Churchill Ltd and Brightstar Engineering
  1501. + *
  1502. + * Created by Charles Manning <charles@aleph1.co.uk>
  1503. + *
  1504. + * This program is free software; you can redistribute it and/or modify
  1505. + * it under the terms of the GNU General Public License version 2 as
  1506. + * published by the Free Software Foundation.
  1507. + */
  1508. +
  1509. +/*
  1510. + * This code implements the ECC algorithm used in SmartMedia.
  1511. + *
  1512. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1513. + * The two unused bit are set to 1.
  1514. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1515. + * blocks are used on a 512-byte NAND page.
  1516. + *
  1517. + */
  1518. +
  1519. +/* Table generated by gen-ecc.c
  1520. + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  1521. + * for each byte of data. These are instead provided in a table in bits7..2.
  1522. + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  1523. + * this bytes influence on the line parity.
  1524. + */
  1525. +
  1526. +#include "yportenv.h"
  1527. +
  1528. +#include "yaffs_ecc.h"
  1529. +
  1530. +static const unsigned char column_parity_table[] = {
  1531. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1532. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1533. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1534. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1535. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1536. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1537. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1538. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1539. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1540. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1541. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1542. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1543. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1544. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1545. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1546. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1547. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1548. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1549. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1550. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1551. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1552. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1553. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1554. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1555. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1556. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1557. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1558. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1559. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1560. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1561. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1562. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1563. +};
  1564. +
  1565. +/* Count the bits in an unsigned char or a U32 */
  1566. +
  1567. +static int yaffs_count_bits(unsigned char x)
  1568. +{
  1569. + int r = 0;
  1570. + while (x) {
  1571. + if (x & 1)
  1572. + r++;
  1573. + x >>= 1;
  1574. + }
  1575. + return r;
  1576. +}
  1577. +
  1578. +static int yaffs_count_bits32(unsigned x)
  1579. +{
  1580. + int r = 0;
  1581. + while (x) {
  1582. + if (x & 1)
  1583. + r++;
  1584. + x >>= 1;
  1585. + }
  1586. + return r;
  1587. +}
  1588. +
  1589. +/* Calculate the ECC for a 256-byte block of data */
  1590. +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
  1591. +{
  1592. + unsigned int i;
  1593. +
  1594. + unsigned char col_parity = 0;
  1595. + unsigned char line_parity = 0;
  1596. + unsigned char line_parity_prime = 0;
  1597. + unsigned char t;
  1598. + unsigned char b;
  1599. +
  1600. + for (i = 0; i < 256; i++) {
  1601. + b = column_parity_table[*data++];
  1602. + col_parity ^= b;
  1603. +
  1604. + if (b & 0x01) { /* odd number of bits in the byte */
  1605. + line_parity ^= i;
  1606. + line_parity_prime ^= ~i;
  1607. + }
  1608. + }
  1609. +
  1610. + ecc[2] = (~col_parity) | 0x03;
  1611. +
  1612. + t = 0;
  1613. + if (line_parity & 0x80)
  1614. + t |= 0x80;
  1615. + if (line_parity_prime & 0x80)
  1616. + t |= 0x40;
  1617. + if (line_parity & 0x40)
  1618. + t |= 0x20;
  1619. + if (line_parity_prime & 0x40)
  1620. + t |= 0x10;
  1621. + if (line_parity & 0x20)
  1622. + t |= 0x08;
  1623. + if (line_parity_prime & 0x20)
  1624. + t |= 0x04;
  1625. + if (line_parity & 0x10)
  1626. + t |= 0x02;
  1627. + if (line_parity_prime & 0x10)
  1628. + t |= 0x01;
  1629. + ecc[1] = ~t;
  1630. +
  1631. + t = 0;
  1632. + if (line_parity & 0x08)
  1633. + t |= 0x80;
  1634. + if (line_parity_prime & 0x08)
  1635. + t |= 0x40;
  1636. + if (line_parity & 0x04)
  1637. + t |= 0x20;
  1638. + if (line_parity_prime & 0x04)
  1639. + t |= 0x10;
  1640. + if (line_parity & 0x02)
  1641. + t |= 0x08;
  1642. + if (line_parity_prime & 0x02)
  1643. + t |= 0x04;
  1644. + if (line_parity & 0x01)
  1645. + t |= 0x02;
  1646. + if (line_parity_prime & 0x01)
  1647. + t |= 0x01;
  1648. + ecc[0] = ~t;
  1649. +
  1650. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1651. + /* Swap the bytes into the wrong order */
  1652. + t = ecc[0];
  1653. + ecc[0] = ecc[1];
  1654. + ecc[1] = t;
  1655. +#endif
  1656. +}
  1657. +
  1658. +/* Correct the ECC on a 256 byte block of data */
  1659. +
  1660. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1661. + const unsigned char *test_ecc)
  1662. +{
  1663. + unsigned char d0, d1, d2; /* deltas */
  1664. +
  1665. + d0 = read_ecc[0] ^ test_ecc[0];
  1666. + d1 = read_ecc[1] ^ test_ecc[1];
  1667. + d2 = read_ecc[2] ^ test_ecc[2];
  1668. +
  1669. + if ((d0 | d1 | d2) == 0)
  1670. + return 0; /* no error */
  1671. +
  1672. + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  1673. + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  1674. + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  1675. + /* Single bit (recoverable) error in data */
  1676. +
  1677. + unsigned byte;
  1678. + unsigned bit;
  1679. +
  1680. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1681. + /* swap the bytes to correct for the wrong order */
  1682. + unsigned char t;
  1683. +
  1684. + t = d0;
  1685. + d0 = d1;
  1686. + d1 = t;
  1687. +#endif
  1688. +
  1689. + bit = byte = 0;
  1690. +
  1691. + if (d1 & 0x80)
  1692. + byte |= 0x80;
  1693. + if (d1 & 0x20)
  1694. + byte |= 0x40;
  1695. + if (d1 & 0x08)
  1696. + byte |= 0x20;
  1697. + if (d1 & 0x02)
  1698. + byte |= 0x10;
  1699. + if (d0 & 0x80)
  1700. + byte |= 0x08;
  1701. + if (d0 & 0x20)
  1702. + byte |= 0x04;
  1703. + if (d0 & 0x08)
  1704. + byte |= 0x02;
  1705. + if (d0 & 0x02)
  1706. + byte |= 0x01;
  1707. +
  1708. + if (d2 & 0x80)
  1709. + bit |= 0x04;
  1710. + if (d2 & 0x20)
  1711. + bit |= 0x02;
  1712. + if (d2 & 0x08)
  1713. + bit |= 0x01;
  1714. +
  1715. + data[byte] ^= (1 << bit);
  1716. +
  1717. + return 1; /* Corrected the error */
  1718. + }
  1719. +
  1720. + if ((yaffs_count_bits(d0) +
  1721. + yaffs_count_bits(d1) + yaffs_count_bits(d2)) == 1) {
  1722. + /* Reccoverable error in ecc */
  1723. +
  1724. + read_ecc[0] = test_ecc[0];
  1725. + read_ecc[1] = test_ecc[1];
  1726. + read_ecc[2] = test_ecc[2];
  1727. +
  1728. + return 1; /* Corrected the error */
  1729. + }
  1730. +
  1731. + /* Unrecoverable error */
  1732. +
  1733. + return -1;
  1734. +
  1735. +}
  1736. +
  1737. +/*
  1738. + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  1739. + */
  1740. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1741. + struct yaffs_ecc_other *ecc_other)
  1742. +{
  1743. + unsigned int i;
  1744. +
  1745. + unsigned char col_parity = 0;
  1746. + unsigned line_parity = 0;
  1747. + unsigned line_parity_prime = 0;
  1748. + unsigned char b;
  1749. +
  1750. + for (i = 0; i < n_bytes; i++) {
  1751. + b = column_parity_table[*data++];
  1752. + col_parity ^= b;
  1753. +
  1754. + if (b & 0x01) {
  1755. + /* odd number of bits in the byte */
  1756. + line_parity ^= i;
  1757. + line_parity_prime ^= ~i;
  1758. + }
  1759. +
  1760. + }
  1761. +
  1762. + ecc_other->col_parity = (col_parity >> 2) & 0x3f;
  1763. + ecc_other->line_parity = line_parity;
  1764. + ecc_other->line_parity_prime = line_parity_prime;
  1765. +}
  1766. +
  1767. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1768. + struct yaffs_ecc_other *read_ecc,
  1769. + const struct yaffs_ecc_other *test_ecc)
  1770. +{
  1771. + unsigned char delta_col; /* column parity delta */
  1772. + unsigned delta_line; /* line parity delta */
  1773. + unsigned delta_line_prime; /* line parity delta */
  1774. + unsigned bit;
  1775. +
  1776. + delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
  1777. + delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
  1778. + delta_line_prime =
  1779. + read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
  1780. +
  1781. + if ((delta_col | delta_line | delta_line_prime) == 0)
  1782. + return 0; /* no error */
  1783. +
  1784. + if (delta_line == ~delta_line_prime &&
  1785. + (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
  1786. + /* Single bit (recoverable) error in data */
  1787. +
  1788. + bit = 0;
  1789. +
  1790. + if (delta_col & 0x20)
  1791. + bit |= 0x04;
  1792. + if (delta_col & 0x08)
  1793. + bit |= 0x02;
  1794. + if (delta_col & 0x02)
  1795. + bit |= 0x01;
  1796. +
  1797. + if (delta_line >= n_bytes)
  1798. + return -1;
  1799. +
  1800. + data[delta_line] ^= (1 << bit);
  1801. +
  1802. + return 1; /* corrected */
  1803. + }
  1804. +
  1805. + if ((yaffs_count_bits32(delta_line) +
  1806. + yaffs_count_bits32(delta_line_prime) +
  1807. + yaffs_count_bits(delta_col)) == 1) {
  1808. + /* Reccoverable error in ecc */
  1809. +
  1810. + *read_ecc = *test_ecc;
  1811. + return 1; /* corrected */
  1812. + }
  1813. +
  1814. + /* Unrecoverable error */
  1815. +
  1816. + return -1;
  1817. +}
  1818. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.h linux-2.6.36/fs/yaffs2/yaffs_ecc.h
  1819. --- linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1820. +++ linux-2.6.36/fs/yaffs2/yaffs_ecc.h 2010-11-18 18:15:51.000000000 +0100
  1821. @@ -0,0 +1,44 @@
  1822. +/*
  1823. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1824. + *
  1825. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1826. + * for Toby Churchill Ltd and Brightstar Engineering
  1827. + *
  1828. + * Created by Charles Manning <charles@aleph1.co.uk>
  1829. + *
  1830. + * This program is free software; you can redistribute it and/or modify
  1831. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1832. + * published by the Free Software Foundation.
  1833. + *
  1834. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1835. + */
  1836. +
  1837. +/*
  1838. + * This code implements the ECC algorithm used in SmartMedia.
  1839. + *
  1840. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1841. + * The two unused bit are set to 1.
  1842. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1843. + * blocks are used on a 512-byte NAND page.
  1844. + *
  1845. + */
  1846. +
  1847. +#ifndef __YAFFS_ECC_H__
  1848. +#define __YAFFS_ECC_H__
  1849. +
  1850. +struct yaffs_ecc_other {
  1851. + unsigned char col_parity;
  1852. + unsigned line_parity;
  1853. + unsigned line_parity_prime;
  1854. +};
  1855. +
  1856. +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc);
  1857. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1858. + const unsigned char *test_ecc);
  1859. +
  1860. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1861. + struct yaffs_ecc_other *ecc);
  1862. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1863. + struct yaffs_ecc_other *read_ecc,
  1864. + const struct yaffs_ecc_other *test_ecc);
  1865. +#endif
  1866. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_getblockinfo.h linux-2.6.36/fs/yaffs2/yaffs_getblockinfo.h
  1867. --- linux-2.6.36.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  1868. +++ linux-2.6.36/fs/yaffs2/yaffs_getblockinfo.h 2010-11-18 18:15:51.000000000 +0100
  1869. @@ -0,0 +1,36 @@
  1870. +/*
  1871. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1872. + *
  1873. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1874. + * for Toby Churchill Ltd and Brightstar Engineering
  1875. + *
  1876. + * Created by Charles Manning <charles@aleph1.co.uk>
  1877. + *
  1878. + * This program is free software; you can redistribute it and/or modify
  1879. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1880. + * published by the Free Software Foundation.
  1881. + *
  1882. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1883. + */
  1884. +
  1885. +#ifndef __YAFFS_GETBLOCKINFO_H__
  1886. +#define __YAFFS_GETBLOCKINFO_H__
  1887. +
  1888. +#include "yaffs_guts.h"
  1889. +#include "yaffs_trace.h"
  1890. +
  1891. +/* Function to manipulate block info */
  1892. +static Y_INLINE struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev
  1893. + *dev, int blk)
  1894. +{
  1895. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  1896. + T(YAFFS_TRACE_ERROR,
  1897. + (TSTR
  1898. + ("**>> yaffs: get_block_info block %d is not valid" TENDSTR),
  1899. + blk));
  1900. + YBUG();
  1901. + }
  1902. + return &dev->block_info[blk - dev->internal_start_block];
  1903. +}
  1904. +
  1905. +#endif
  1906. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_guts.c linux-2.6.36/fs/yaffs2/yaffs_guts.c
  1907. --- linux-2.6.36.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  1908. +++ linux-2.6.36/fs/yaffs2/yaffs_guts.c 2010-11-18 18:15:51.000000000 +0100
  1909. @@ -0,0 +1,5227 @@
  1910. +/*
  1911. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1912. + *
  1913. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1914. + * for Toby Churchill Ltd and Brightstar Engineering
  1915. + *
  1916. + * Created by Charles Manning <charles@aleph1.co.uk>
  1917. + *
  1918. + * This program is free software; you can redistribute it and/or modify
  1919. + * it under the terms of the GNU General Public License version 2 as
  1920. + * published by the Free Software Foundation.
  1921. + */
  1922. +
  1923. +#include "yportenv.h"
  1924. +#include "yaffs_trace.h"
  1925. +
  1926. +#include "yaffs_guts.h"
  1927. +#include "yaffs_tagsvalidity.h"
  1928. +#include "yaffs_getblockinfo.h"
  1929. +
  1930. +#include "yaffs_tagscompat.h"
  1931. +
  1932. +#include "yaffs_nand.h"
  1933. +
  1934. +#include "yaffs_yaffs1.h"
  1935. +#include "yaffs_yaffs2.h"
  1936. +#include "yaffs_bitmap.h"
  1937. +#include "yaffs_verify.h"
  1938. +
  1939. +#include "yaffs_nand.h"
  1940. +#include "yaffs_packedtags2.h"
  1941. +
  1942. +#include "yaffs_nameval.h"
  1943. +#include "yaffs_allocator.h"
  1944. +
  1945. +#include "yaffs_attribs.h"
  1946. +
  1947. +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
  1948. +#define YAFFS_GC_GOOD_ENOUGH 2
  1949. +#define YAFFS_GC_PASSIVE_THRESHOLD 4
  1950. +
  1951. +#include "yaffs_ecc.h"
  1952. +
  1953. +/* Robustification (if it ever comes about...) */
  1954. +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block);
  1955. +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  1956. + int erased_ok);
  1957. +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  1958. + const u8 * data,
  1959. + const struct yaffs_ext_tags *tags);
  1960. +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  1961. + const struct yaffs_ext_tags *tags);
  1962. +
  1963. +/* Other local prototypes */
  1964. +static void yaffs_update_parent(struct yaffs_obj *obj);
  1965. +static int yaffs_unlink_obj(struct yaffs_obj *obj);
  1966. +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj);
  1967. +
  1968. +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  1969. + const u8 * buffer,
  1970. + struct yaffs_ext_tags *tags, int use_reserver);
  1971. +
  1972. +static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  1973. + enum yaffs_obj_type type);
  1974. +
  1975. +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  1976. + struct yaffs_xattr_mod *xmod);
  1977. +
  1978. +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj);
  1979. +static int yaffs_generic_obj_del(struct yaffs_obj *in);
  1980. +
  1981. +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk);
  1982. +
  1983. +static int yaffs_unlink_worker(struct yaffs_obj *obj);
  1984. +
  1985. +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  1986. + int chunk_obj);
  1987. +
  1988. +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  1989. + struct yaffs_block_info **block_ptr);
  1990. +
  1991. +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in);
  1992. +
  1993. +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in);
  1994. +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object,
  1995. + int chunk_id);
  1996. +
  1997. +static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  1998. + struct yaffs_ext_tags *tags);
  1999. +
  2000. +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  2001. + int nand_chunk,
  2002. + const u8 * data,
  2003. + struct yaffs_ext_tags *tags);
  2004. +
  2005. +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
  2006. + const YCHAR * oh_name, int buff_size);
  2007. +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
  2008. + const YCHAR * name);
  2009. +
  2010. +/* Function to calculate chunk and offset */
  2011. +
  2012. +static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  2013. + int *chunk_out, u32 * offset_out)
  2014. +{
  2015. + int chunk;
  2016. + u32 offset;
  2017. +
  2018. + chunk = (u32) (addr >> dev->chunk_shift);
  2019. +
  2020. + if (dev->chunk_div == 1) {
  2021. + /* easy power of 2 case */
  2022. + offset = (u32) (addr & dev->chunk_mask);
  2023. + } else {
  2024. + /* Non power-of-2 case */
  2025. +
  2026. + loff_t chunk_base;
  2027. +
  2028. + chunk /= dev->chunk_div;
  2029. +
  2030. + chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  2031. + offset = (u32) (addr - chunk_base);
  2032. + }
  2033. +
  2034. + *chunk_out = chunk;
  2035. + *offset_out = offset;
  2036. +}
  2037. +
  2038. +/* Function to return the number of shifts for a power of 2 greater than or
  2039. + * equal to the given number
  2040. + * Note we don't try to cater for all possible numbers and this does not have to
  2041. + * be hellishly efficient.
  2042. + */
  2043. +
  2044. +static u32 calc_shifts_ceiling(u32 x)
  2045. +{
  2046. + int extra_bits;
  2047. + int shifts;
  2048. +
  2049. + shifts = extra_bits = 0;
  2050. +
  2051. + while (x > 1) {
  2052. + if (x & 1)
  2053. + extra_bits++;
  2054. + x >>= 1;
  2055. + shifts++;
  2056. + }
  2057. +
  2058. + if (extra_bits)
  2059. + shifts++;
  2060. +
  2061. + return shifts;
  2062. +}
  2063. +
  2064. +/* Function to return the number of shifts to get a 1 in bit 0
  2065. + */
  2066. +
  2067. +static u32 calc_shifts(u32 x)
  2068. +{
  2069. + u32 shifts;
  2070. +
  2071. + shifts = 0;
  2072. +
  2073. + if (!x)
  2074. + return 0;
  2075. +
  2076. + while (!(x & 1)) {
  2077. + x >>= 1;
  2078. + shifts++;
  2079. + }
  2080. +
  2081. + return shifts;
  2082. +}
  2083. +
  2084. +/*
  2085. + * Temporary buffer manipulations.
  2086. + */
  2087. +
  2088. +static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  2089. +{
  2090. + int i;
  2091. + u8 *buf = (u8 *) 1;
  2092. +
  2093. + memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  2094. +
  2095. + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  2096. + dev->temp_buffer[i].line = 0; /* not in use */
  2097. + dev->temp_buffer[i].buffer = buf =
  2098. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  2099. + }
  2100. +
  2101. + return buf ? YAFFS_OK : YAFFS_FAIL;
  2102. +}
  2103. +
  2104. +u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
  2105. +{
  2106. + int i, j;
  2107. +
  2108. + dev->temp_in_use++;
  2109. + if (dev->temp_in_use > dev->max_temp)
  2110. + dev->max_temp = dev->temp_in_use;
  2111. +
  2112. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2113. + if (dev->temp_buffer[i].line == 0) {
  2114. + dev->temp_buffer[i].line = line_no;
  2115. + if ((i + 1) > dev->max_temp) {
  2116. + dev->max_temp = i + 1;
  2117. + for (j = 0; j <= i; j++)
  2118. + dev->temp_buffer[j].max_line =
  2119. + dev->temp_buffer[j].line;
  2120. + }
  2121. +
  2122. + return dev->temp_buffer[i].buffer;
  2123. + }
  2124. + }
  2125. +
  2126. + T(YAFFS_TRACE_BUFFERS,
  2127. + (TSTR("Out of temp buffers at line %d, other held by lines:"),
  2128. + line_no));
  2129. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  2130. + T(YAFFS_TRACE_BUFFERS,
  2131. + (TSTR(" %d "), dev->temp_buffer[i].line));
  2132. +
  2133. + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
  2134. +
  2135. + /*
  2136. + * If we got here then we have to allocate an unmanaged one
  2137. + * This is not good.
  2138. + */
  2139. +
  2140. + dev->unmanaged_buffer_allocs++;
  2141. + return YMALLOC(dev->data_bytes_per_chunk);
  2142. +
  2143. +}
  2144. +
  2145. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
  2146. +{
  2147. + int i;
  2148. +
  2149. + dev->temp_in_use--;
  2150. +
  2151. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2152. + if (dev->temp_buffer[i].buffer == buffer) {
  2153. + dev->temp_buffer[i].line = 0;
  2154. + return;
  2155. + }
  2156. + }
  2157. +
  2158. + if (buffer) {
  2159. + /* assume it is an unmanaged one. */
  2160. + T(YAFFS_TRACE_BUFFERS,
  2161. + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
  2162. + line_no));
  2163. + YFREE(buffer);
  2164. + dev->unmanaged_buffer_deallocs++;
  2165. + }
  2166. +
  2167. +}
  2168. +
  2169. +/*
  2170. + * Determine if we have a managed buffer.
  2171. + */
  2172. +int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
  2173. +{
  2174. + int i;
  2175. +
  2176. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2177. + if (dev->temp_buffer[i].buffer == buffer)
  2178. + return 1;
  2179. + }
  2180. +
  2181. + for (i = 0; i < dev->param.n_caches; i++) {
  2182. + if (dev->cache[i].data == buffer)
  2183. + return 1;
  2184. + }
  2185. +
  2186. + if (buffer == dev->checkpt_buffer)
  2187. + return 1;
  2188. +
  2189. + T(YAFFS_TRACE_ALWAYS,
  2190. + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
  2191. + return 0;
  2192. +}
  2193. +
  2194. +/*
  2195. + * Verification code
  2196. + */
  2197. +
  2198. +/*
  2199. + * Simple hash function. Needs to have a reasonable spread
  2200. + */
  2201. +
  2202. +static Y_INLINE int yaffs_hash_fn(int n)
  2203. +{
  2204. + n = abs(n);
  2205. + return n % YAFFS_NOBJECT_BUCKETS;
  2206. +}
  2207. +
  2208. +/*
  2209. + * Access functions to useful fake objects.
  2210. + * Note that root might have a presence in NAND if permissions are set.
  2211. + */
  2212. +
  2213. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  2214. +{
  2215. + return dev->root_dir;
  2216. +}
  2217. +
  2218. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  2219. +{
  2220. + return dev->lost_n_found;
  2221. +}
  2222. +
  2223. +/*
  2224. + * Erased NAND checking functions
  2225. + */
  2226. +
  2227. +int yaffs_check_ff(u8 * buffer, int n_bytes)
  2228. +{
  2229. + /* Horrible, slow implementation */
  2230. + while (n_bytes--) {
  2231. + if (*buffer != 0xFF)
  2232. + return 0;
  2233. + buffer++;
  2234. + }
  2235. + return 1;
  2236. +}
  2237. +
  2238. +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  2239. +{
  2240. + int retval = YAFFS_OK;
  2241. + u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
  2242. + struct yaffs_ext_tags tags;
  2243. + int result;
  2244. +
  2245. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  2246. +
  2247. + if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  2248. + retval = YAFFS_FAIL;
  2249. +
  2250. + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
  2251. + T(YAFFS_TRACE_NANDACCESS,
  2252. + (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
  2253. + retval = YAFFS_FAIL;
  2254. + }
  2255. +
  2256. + yaffs_release_temp_buffer(dev, data, __LINE__);
  2257. +
  2258. + return retval;
  2259. +
  2260. +}
  2261. +
  2262. +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  2263. + int nand_chunk,
  2264. + const u8 * data,
  2265. + struct yaffs_ext_tags *tags)
  2266. +{
  2267. + int retval = YAFFS_OK;
  2268. + struct yaffs_ext_tags temp_tags;
  2269. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  2270. + int result;
  2271. +
  2272. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  2273. + if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  2274. + temp_tags.obj_id != tags->obj_id ||
  2275. + temp_tags.chunk_id != tags->chunk_id ||
  2276. + temp_tags.n_bytes != tags->n_bytes)
  2277. + retval = YAFFS_FAIL;
  2278. +
  2279. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2280. +
  2281. + return retval;
  2282. +}
  2283. +
  2284. +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  2285. + const u8 * data,
  2286. + struct yaffs_ext_tags *tags, int use_reserver)
  2287. +{
  2288. + int attempts = 0;
  2289. + int write_ok = 0;
  2290. + int chunk;
  2291. +
  2292. + yaffs2_checkpt_invalidate(dev);
  2293. +
  2294. + do {
  2295. + struct yaffs_block_info *bi = 0;
  2296. + int erased_ok = 0;
  2297. +
  2298. + chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  2299. + if (chunk < 0) {
  2300. + /* no space */
  2301. + break;
  2302. + }
  2303. +
  2304. + /* First check this chunk is erased, if it needs
  2305. + * checking. The checking policy (unless forced
  2306. + * always on) is as follows:
  2307. + *
  2308. + * Check the first page we try to write in a block.
  2309. + * If the check passes then we don't need to check any
  2310. + * more. If the check fails, we check again...
  2311. + * If the block has been erased, we don't need to check.
  2312. + *
  2313. + * However, if the block has been prioritised for gc,
  2314. + * then we think there might be something odd about
  2315. + * this block and stop using it.
  2316. + *
  2317. + * Rationale: We should only ever see chunks that have
  2318. + * not been erased if there was a partially written
  2319. + * chunk due to power loss. This checking policy should
  2320. + * catch that case with very few checks and thus save a
  2321. + * lot of checks that are most likely not needed.
  2322. + *
  2323. + * Mods to the above
  2324. + * If an erase check fails or the write fails we skip the
  2325. + * rest of the block.
  2326. + */
  2327. +
  2328. + /* let's give it a try */
  2329. + attempts++;
  2330. +
  2331. + if (dev->param.always_check_erased)
  2332. + bi->skip_erased_check = 0;
  2333. +
  2334. + if (!bi->skip_erased_check) {
  2335. + erased_ok = yaffs_check_chunk_erased(dev, chunk);
  2336. + if (erased_ok != YAFFS_OK) {
  2337. + T(YAFFS_TRACE_ERROR,
  2338. + (TSTR("**>> yaffs chunk %d was not erased"
  2339. + TENDSTR), chunk));
  2340. +
  2341. + /* If not erased, delete this one,
  2342. + * skip rest of block and
  2343. + * try another chunk */
  2344. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  2345. + yaffs_skip_rest_of_block(dev);
  2346. + continue;
  2347. + }
  2348. + }
  2349. +
  2350. + write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  2351. +
  2352. + if (!bi->skip_erased_check)
  2353. + write_ok =
  2354. + yaffs_verify_chunk_written(dev, chunk, data, tags);
  2355. +
  2356. + if (write_ok != YAFFS_OK) {
  2357. + /* Clean up aborted write, skip to next block and
  2358. + * try another chunk */
  2359. + yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  2360. + continue;
  2361. + }
  2362. +
  2363. + bi->skip_erased_check = 1;
  2364. +
  2365. + /* Copy the data into the robustification buffer */
  2366. + yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  2367. +
  2368. + } while (write_ok != YAFFS_OK &&
  2369. + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  2370. +
  2371. + if (!write_ok)
  2372. + chunk = -1;
  2373. +
  2374. + if (attempts > 1) {
  2375. + T(YAFFS_TRACE_ERROR,
  2376. + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
  2377. + attempts));
  2378. +
  2379. + dev->n_retired_writes += (attempts - 1);
  2380. + }
  2381. +
  2382. + return chunk;
  2383. +}
  2384. +
  2385. +/*
  2386. + * Block retiring for handling a broken block.
  2387. + */
  2388. +
  2389. +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  2390. +{
  2391. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2392. +
  2393. + yaffs2_checkpt_invalidate(dev);
  2394. +
  2395. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  2396. +
  2397. + if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  2398. + if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  2399. + T(YAFFS_TRACE_ALWAYS,
  2400. + (TSTR
  2401. + ("yaffs: Failed to mark bad and erase block %d"
  2402. + TENDSTR), flash_block));
  2403. + } else {
  2404. + struct yaffs_ext_tags tags;
  2405. + int chunk_id =
  2406. + flash_block * dev->param.chunks_per_block;
  2407. +
  2408. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  2409. +
  2410. + memset(buffer, 0xff, dev->data_bytes_per_chunk);
  2411. + yaffs_init_tags(&tags);
  2412. + tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  2413. + if (dev->param.write_chunk_tags_fn(dev, chunk_id -
  2414. + dev->chunk_offset,
  2415. + buffer,
  2416. + &tags) != YAFFS_OK)
  2417. + T(YAFFS_TRACE_ALWAYS,
  2418. + (TSTR
  2419. + ("yaffs: Failed to "
  2420. + TCONT("write bad block marker to block %d")
  2421. + TENDSTR), flash_block));
  2422. +
  2423. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2424. + }
  2425. + }
  2426. +
  2427. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  2428. + bi->gc_prioritise = 0;
  2429. + bi->needs_retiring = 0;
  2430. +
  2431. + dev->n_retired_blocks++;
  2432. +}
  2433. +
  2434. +/*
  2435. + * Functions for robustisizing TODO
  2436. + *
  2437. + */
  2438. +
  2439. +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  2440. + const u8 * data,
  2441. + const struct yaffs_ext_tags *tags)
  2442. +{
  2443. + dev = dev;
  2444. + nand_chunk = nand_chunk;
  2445. + data = data;
  2446. + tags = tags;
  2447. +}
  2448. +
  2449. +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  2450. + const struct yaffs_ext_tags *tags)
  2451. +{
  2452. + dev = dev;
  2453. + nand_chunk = nand_chunk;
  2454. + tags = tags;
  2455. +}
  2456. +
  2457. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  2458. + struct yaffs_block_info *bi)
  2459. +{
  2460. + if (!bi->gc_prioritise) {
  2461. + bi->gc_prioritise = 1;
  2462. + dev->has_pending_prioritised_gc = 1;
  2463. + bi->chunk_error_strikes++;
  2464. +
  2465. + if (bi->chunk_error_strikes > 3) {
  2466. + bi->needs_retiring = 1; /* Too many stikes, so retire this */
  2467. + T(YAFFS_TRACE_ALWAYS,
  2468. + (TSTR("yaffs: Block struck out" TENDSTR)));
  2469. +
  2470. + }
  2471. + }
  2472. +}
  2473. +
  2474. +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  2475. + int erased_ok)
  2476. +{
  2477. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  2478. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2479. +
  2480. + yaffs_handle_chunk_error(dev, bi);
  2481. +
  2482. + if (erased_ok) {
  2483. + /* Was an actual write failure, so mark the block for retirement */
  2484. + bi->needs_retiring = 1;
  2485. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  2486. + (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
  2487. + }
  2488. +
  2489. + /* Delete the chunk */
  2490. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  2491. + yaffs_skip_rest_of_block(dev);
  2492. +}
  2493. +
  2494. +/*---------------- Name handling functions ------------*/
  2495. +
  2496. +static u16 yaffs_calc_name_sum(const YCHAR * name)
  2497. +{
  2498. + u16 sum = 0;
  2499. + u16 i = 1;
  2500. +
  2501. + const YUCHAR *bname = (const YUCHAR *)name;
  2502. + if (bname) {
  2503. + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
  2504. +
  2505. + /* 0x1f mask is case insensitive */
  2506. + sum += ((*bname) & 0x1f) * i;
  2507. + i++;
  2508. + bname++;
  2509. + }
  2510. + }
  2511. + return sum;
  2512. +}
  2513. +
  2514. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  2515. +{
  2516. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  2517. + memset(obj->short_name, 0, sizeof(obj->short_name));
  2518. + if (name &&
  2519. + yaffs_strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  2520. + YAFFS_SHORT_NAME_LENGTH)
  2521. + yaffs_strcpy(obj->short_name, name);
  2522. + else
  2523. + obj->short_name[0] = _Y('\0');
  2524. +#endif
  2525. + obj->sum = yaffs_calc_name_sum(name);
  2526. +}
  2527. +
  2528. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  2529. + const struct yaffs_obj_hdr *oh)
  2530. +{
  2531. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  2532. + YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  2533. + memset(tmp_name, 0, sizeof(tmp_name));
  2534. + yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  2535. + YAFFS_MAX_NAME_LENGTH + 1);
  2536. + yaffs_set_obj_name(obj, tmp_name);
  2537. +#else
  2538. + yaffs_set_obj_name(obj, oh->name);
  2539. +#endif
  2540. +}
  2541. +
  2542. +/*-------------------- TNODES -------------------
  2543. +
  2544. + * List of spare tnodes
  2545. + * The list is hooked together using the first pointer
  2546. + * in the tnode.
  2547. + */
  2548. +
  2549. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  2550. +{
  2551. + struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  2552. + if (tn) {
  2553. + memset(tn, 0, dev->tnode_size);
  2554. + dev->n_tnodes++;
  2555. + }
  2556. +
  2557. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2558. +
  2559. + return tn;
  2560. +}
  2561. +
  2562. +/* FreeTnode frees up a tnode and puts it back on the free list */
  2563. +static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  2564. +{
  2565. + yaffs_free_raw_tnode(dev, tn);
  2566. + dev->n_tnodes--;
  2567. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2568. +}
  2569. +
  2570. +static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  2571. +{
  2572. + yaffs_deinit_raw_tnodes_and_objs(dev);
  2573. + dev->n_obj = 0;
  2574. + dev->n_tnodes = 0;
  2575. +}
  2576. +
  2577. +void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2578. + unsigned pos, unsigned val)
  2579. +{
  2580. + u32 *map = (u32 *) tn;
  2581. + u32 bit_in_map;
  2582. + u32 bit_in_word;
  2583. + u32 word_in_map;
  2584. + u32 mask;
  2585. +
  2586. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2587. + val >>= dev->chunk_grp_bits;
  2588. +
  2589. + bit_in_map = pos * dev->tnode_width;
  2590. + word_in_map = bit_in_map / 32;
  2591. + bit_in_word = bit_in_map & (32 - 1);
  2592. +
  2593. + mask = dev->tnode_mask << bit_in_word;
  2594. +
  2595. + map[word_in_map] &= ~mask;
  2596. + map[word_in_map] |= (mask & (val << bit_in_word));
  2597. +
  2598. + if (dev->tnode_width > (32 - bit_in_word)) {
  2599. + bit_in_word = (32 - bit_in_word);
  2600. + word_in_map++;;
  2601. + mask =
  2602. + dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
  2603. + map[word_in_map] &= ~mask;
  2604. + map[word_in_map] |= (mask & (val >> bit_in_word));
  2605. + }
  2606. +}
  2607. +
  2608. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2609. + unsigned pos)
  2610. +{
  2611. + u32 *map = (u32 *) tn;
  2612. + u32 bit_in_map;
  2613. + u32 bit_in_word;
  2614. + u32 word_in_map;
  2615. + u32 val;
  2616. +
  2617. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2618. +
  2619. + bit_in_map = pos * dev->tnode_width;
  2620. + word_in_map = bit_in_map / 32;
  2621. + bit_in_word = bit_in_map & (32 - 1);
  2622. +
  2623. + val = map[word_in_map] >> bit_in_word;
  2624. +
  2625. + if (dev->tnode_width > (32 - bit_in_word)) {
  2626. + bit_in_word = (32 - bit_in_word);
  2627. + word_in_map++;;
  2628. + val |= (map[word_in_map] << bit_in_word);
  2629. + }
  2630. +
  2631. + val &= dev->tnode_mask;
  2632. + val <<= dev->chunk_grp_bits;
  2633. +
  2634. + return val;
  2635. +}
  2636. +
  2637. +/* ------------------- End of individual tnode manipulation -----------------*/
  2638. +
  2639. +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  2640. + * The look up tree is represented by the top tnode and the number of top_level
  2641. + * in the tree. 0 means only the level 0 tnode is in the tree.
  2642. + */
  2643. +
  2644. +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  2645. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  2646. + struct yaffs_file_var *file_struct,
  2647. + u32 chunk_id)
  2648. +{
  2649. + struct yaffs_tnode *tn = file_struct->top;
  2650. + u32 i;
  2651. + int required_depth;
  2652. + int level = file_struct->top_level;
  2653. +
  2654. + dev = dev;
  2655. +
  2656. + /* Check sane level and chunk Id */
  2657. + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  2658. + return NULL;
  2659. +
  2660. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2661. + return NULL;
  2662. +
  2663. + /* First check we're tall enough (ie enough top_level) */
  2664. +
  2665. + i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2666. + required_depth = 0;
  2667. + while (i) {
  2668. + i >>= YAFFS_TNODES_INTERNAL_BITS;
  2669. + required_depth++;
  2670. + }
  2671. +
  2672. + if (required_depth > file_struct->top_level)
  2673. + return NULL; /* Not tall enough, so we can't find it */
  2674. +
  2675. + /* Traverse down to level 0 */
  2676. + while (level > 0 && tn) {
  2677. + tn = tn->internal[(chunk_id >>
  2678. + (YAFFS_TNODES_LEVEL0_BITS +
  2679. + (level - 1) *
  2680. + YAFFS_TNODES_INTERNAL_BITS)) &
  2681. + YAFFS_TNODES_INTERNAL_MASK];
  2682. + level--;
  2683. + }
  2684. +
  2685. + return tn;
  2686. +}
  2687. +
  2688. +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  2689. + * This happens in two steps:
  2690. + * 1. If the tree isn't tall enough, then make it taller.
  2691. + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  2692. + *
  2693. + * Used when modifying the tree.
  2694. + *
  2695. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  2696. + * be plugged into the ttree.
  2697. + */
  2698. +
  2699. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  2700. + struct yaffs_file_var *file_struct,
  2701. + u32 chunk_id,
  2702. + struct yaffs_tnode *passed_tn)
  2703. +{
  2704. + int required_depth;
  2705. + int i;
  2706. + int l;
  2707. + struct yaffs_tnode *tn;
  2708. +
  2709. + u32 x;
  2710. +
  2711. + /* Check sane level and page Id */
  2712. + if (file_struct->top_level < 0
  2713. + || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  2714. + return NULL;
  2715. +
  2716. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2717. + return NULL;
  2718. +
  2719. + /* First check we're tall enough (ie enough top_level) */
  2720. +
  2721. + x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2722. + required_depth = 0;
  2723. + while (x) {
  2724. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  2725. + required_depth++;
  2726. + }
  2727. +
  2728. + if (required_depth > file_struct->top_level) {
  2729. + /* Not tall enough, gotta make the tree taller */
  2730. + for (i = file_struct->top_level; i < required_depth; i++) {
  2731. +
  2732. + tn = yaffs_get_tnode(dev);
  2733. +
  2734. + if (tn) {
  2735. + tn->internal[0] = file_struct->top;
  2736. + file_struct->top = tn;
  2737. + file_struct->top_level++;
  2738. + } else {
  2739. + T(YAFFS_TRACE_ERROR,
  2740. + (TSTR("yaffs: no more tnodes" TENDSTR)));
  2741. + return NULL;
  2742. + }
  2743. + }
  2744. + }
  2745. +
  2746. + /* Traverse down to level 0, adding anything we need */
  2747. +
  2748. + l = file_struct->top_level;
  2749. + tn = file_struct->top;
  2750. +
  2751. + if (l > 0) {
  2752. + while (l > 0 && tn) {
  2753. + x = (chunk_id >>
  2754. + (YAFFS_TNODES_LEVEL0_BITS +
  2755. + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  2756. + YAFFS_TNODES_INTERNAL_MASK;
  2757. +
  2758. + if ((l > 1) && !tn->internal[x]) {
  2759. + /* Add missing non-level-zero tnode */
  2760. + tn->internal[x] = yaffs_get_tnode(dev);
  2761. + if (!tn->internal[x])
  2762. + return NULL;
  2763. + } else if (l == 1) {
  2764. + /* Looking from level 1 at level 0 */
  2765. + if (passed_tn) {
  2766. + /* If we already have one, then release it. */
  2767. + if (tn->internal[x])
  2768. + yaffs_free_tnode(dev,
  2769. + tn->
  2770. + internal[x]);
  2771. + tn->internal[x] = passed_tn;
  2772. +
  2773. + } else if (!tn->internal[x]) {
  2774. + /* Don't have one, none passed in */
  2775. + tn->internal[x] = yaffs_get_tnode(dev);
  2776. + if (!tn->internal[x])
  2777. + return NULL;
  2778. + }
  2779. + }
  2780. +
  2781. + tn = tn->internal[x];
  2782. + l--;
  2783. + }
  2784. + } else {
  2785. + /* top is level 0 */
  2786. + if (passed_tn) {
  2787. + memcpy(tn, passed_tn,
  2788. + (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  2789. + yaffs_free_tnode(dev, passed_tn);
  2790. + }
  2791. + }
  2792. +
  2793. + return tn;
  2794. +}
  2795. +
  2796. +static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  2797. + struct yaffs_ext_tags *tags, int obj_id,
  2798. + int inode_chunk)
  2799. +{
  2800. + int j;
  2801. +
  2802. + for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  2803. + if (yaffs_check_chunk_bit
  2804. + (dev, the_chunk / dev->param.chunks_per_block,
  2805. + the_chunk % dev->param.chunks_per_block)) {
  2806. +
  2807. + if (dev->chunk_grp_size == 1)
  2808. + return the_chunk;
  2809. + else {
  2810. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  2811. + tags);
  2812. + if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
  2813. + /* found it; */
  2814. + return the_chunk;
  2815. + }
  2816. + }
  2817. + }
  2818. + the_chunk++;
  2819. + }
  2820. + return -1;
  2821. +}
  2822. +
  2823. +static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  2824. +{
  2825. + struct yaffs_block_info *the_block;
  2826. + unsigned block_no;
  2827. +
  2828. + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
  2829. +
  2830. + block_no = chunk / dev->param.chunks_per_block;
  2831. + the_block = yaffs_get_block_info(dev, block_no);
  2832. + if (the_block) {
  2833. + the_block->soft_del_pages++;
  2834. + dev->n_free_chunks++;
  2835. + yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  2836. + }
  2837. +}
  2838. +
  2839. +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  2840. + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  2841. + * of the tnode.
  2842. + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  2843. + */
  2844. +
  2845. +static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  2846. + u32 level, int chunk_offset)
  2847. +{
  2848. + int i;
  2849. + int the_chunk;
  2850. + int all_done = 1;
  2851. + struct yaffs_dev *dev = in->my_dev;
  2852. +
  2853. + if (tn) {
  2854. + if (level > 0) {
  2855. +
  2856. + for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
  2857. + i--) {
  2858. + if (tn->internal[i]) {
  2859. + all_done =
  2860. + yaffs_soft_del_worker(in,
  2861. + tn->internal
  2862. + [i],
  2863. + level - 1,
  2864. + (chunk_offset
  2865. + <<
  2866. + YAFFS_TNODES_INTERNAL_BITS)
  2867. + + i);
  2868. + if (all_done) {
  2869. + yaffs_free_tnode(dev,
  2870. + tn->internal
  2871. + [i]);
  2872. + tn->internal[i] = NULL;
  2873. + } else {
  2874. + /* Hoosterman... how could this happen? */
  2875. + }
  2876. + }
  2877. + }
  2878. + return (all_done) ? 1 : 0;
  2879. + } else if (level == 0) {
  2880. +
  2881. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  2882. + the_chunk = yaffs_get_group_base(dev, tn, i);
  2883. + if (the_chunk) {
  2884. + /* Note this does not find the real chunk, only the chunk group.
  2885. + * We make an assumption that a chunk group is not larger than
  2886. + * a block.
  2887. + */
  2888. + yaffs_soft_del_chunk(dev, the_chunk);
  2889. + yaffs_load_tnode_0(dev, tn, i, 0);
  2890. + }
  2891. +
  2892. + }
  2893. + return 1;
  2894. +
  2895. + }
  2896. +
  2897. + }
  2898. +
  2899. + return 1;
  2900. +
  2901. +}
  2902. +
  2903. +static void yaffs_soft_del_file(struct yaffs_obj *obj)
  2904. +{
  2905. + if (obj->deleted &&
  2906. + obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
  2907. + if (obj->n_data_chunks <= 0) {
  2908. + /* Empty file with no duplicate object headers, just delete it immediately */
  2909. + yaffs_free_tnode(obj->my_dev,
  2910. + obj->variant.file_variant.top);
  2911. + obj->variant.file_variant.top = NULL;
  2912. + T(YAFFS_TRACE_TRACING,
  2913. + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
  2914. + obj->obj_id));
  2915. + yaffs_generic_obj_del(obj);
  2916. + } else {
  2917. + yaffs_soft_del_worker(obj,
  2918. + obj->variant.file_variant.top,
  2919. + obj->variant.
  2920. + file_variant.top_level, 0);
  2921. + obj->soft_del = 1;
  2922. + }
  2923. + }
  2924. +}
  2925. +
  2926. +/* Pruning removes any part of the file structure tree that is beyond the
  2927. + * bounds of the file (ie that does not point to chunks).
  2928. + *
  2929. + * A file should only get pruned when its size is reduced.
  2930. + *
  2931. + * Before pruning, the chunks must be pulled from the tree and the
  2932. + * level 0 tnode entries must be zeroed out.
  2933. + * Could also use this for file deletion, but that's probably better handled
  2934. + * by a special case.
  2935. + *
  2936. + * This function is recursive. For levels > 0 the function is called again on
  2937. + * any sub-tree. For level == 0 we just check if the sub-tree has data.
  2938. + * If there is no data in a subtree then it is pruned.
  2939. + */
  2940. +
  2941. +static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  2942. + struct yaffs_tnode *tn, u32 level,
  2943. + int del0)
  2944. +{
  2945. + int i;
  2946. + int has_data;
  2947. +
  2948. + if (tn) {
  2949. + has_data = 0;
  2950. +
  2951. + if (level > 0) {
  2952. + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  2953. + if (tn->internal[i]) {
  2954. + tn->internal[i] =
  2955. + yaffs_prune_worker(dev,
  2956. + tn->internal[i],
  2957. + level - 1,
  2958. + (i ==
  2959. + 0) ? del0 : 1);
  2960. + }
  2961. +
  2962. + if (tn->internal[i])
  2963. + has_data++;
  2964. + }
  2965. + } else {
  2966. + int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  2967. + u32 *map = (u32 *) tn;
  2968. +
  2969. + for (i = 0; !has_data && i < tnode_size_u32; i++) {
  2970. + if (map[i])
  2971. + has_data++;
  2972. + }
  2973. + }
  2974. +
  2975. + if (has_data == 0 && del0) {
  2976. + /* Free and return NULL */
  2977. +
  2978. + yaffs_free_tnode(dev, tn);
  2979. + tn = NULL;
  2980. + }
  2981. +
  2982. + }
  2983. +
  2984. + return tn;
  2985. +
  2986. +}
  2987. +
  2988. +static int yaffs_prune_tree(struct yaffs_dev *dev,
  2989. + struct yaffs_file_var *file_struct)
  2990. +{
  2991. + int i;
  2992. + int has_data;
  2993. + int done = 0;
  2994. + struct yaffs_tnode *tn;
  2995. +
  2996. + if (file_struct->top_level > 0) {
  2997. + file_struct->top =
  2998. + yaffs_prune_worker(dev, file_struct->top,
  2999. + file_struct->top_level, 0);
  3000. +
  3001. + /* Now we have a tree with all the non-zero branches NULL but the height
  3002. + * is the same as it was.
  3003. + * Let's see if we can trim internal tnodes to shorten the tree.
  3004. + * We can do this if only the 0th element in the tnode is in use
  3005. + * (ie all the non-zero are NULL)
  3006. + */
  3007. +
  3008. + while (file_struct->top_level && !done) {
  3009. + tn = file_struct->top;
  3010. +
  3011. + has_data = 0;
  3012. + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  3013. + if (tn->internal[i])
  3014. + has_data++;
  3015. + }
  3016. +
  3017. + if (!has_data) {
  3018. + file_struct->top = tn->internal[0];
  3019. + file_struct->top_level--;
  3020. + yaffs_free_tnode(dev, tn);
  3021. + } else {
  3022. + done = 1;
  3023. + }
  3024. + }
  3025. + }
  3026. +
  3027. + return YAFFS_OK;
  3028. +}
  3029. +
  3030. +/*-------------------- End of File Structure functions.-------------------*/
  3031. +
  3032. +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  3033. +static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  3034. +{
  3035. + struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  3036. +
  3037. + if (obj) {
  3038. + dev->n_obj++;
  3039. +
  3040. + /* Now sweeten it up... */
  3041. +
  3042. + memset(obj, 0, sizeof(struct yaffs_obj));
  3043. + obj->being_created = 1;
  3044. +
  3045. + obj->my_dev = dev;
  3046. + obj->hdr_chunk = 0;
  3047. + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  3048. + INIT_LIST_HEAD(&(obj->hard_links));
  3049. + INIT_LIST_HEAD(&(obj->hash_link));
  3050. + INIT_LIST_HEAD(&obj->siblings);
  3051. +
  3052. + /* Now make the directory sane */
  3053. + if (dev->root_dir) {
  3054. + obj->parent = dev->root_dir;
  3055. + list_add(&(obj->siblings),
  3056. + &dev->root_dir->variant.dir_variant.children);
  3057. + }
  3058. +
  3059. + /* Add it to the lost and found directory.
  3060. + * NB Can't put root or lost-n-found in lost-n-found so
  3061. + * check if lost-n-found exists first
  3062. + */
  3063. + if (dev->lost_n_found)
  3064. + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  3065. +
  3066. + obj->being_created = 0;
  3067. + }
  3068. +
  3069. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3070. +
  3071. + return obj;
  3072. +}
  3073. +
  3074. +static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  3075. + int number, u32 mode)
  3076. +{
  3077. +
  3078. + struct yaffs_obj *obj =
  3079. + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  3080. + if (obj) {
  3081. + obj->fake = 1; /* it is fake so it might have no NAND presence... */
  3082. + obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
  3083. + obj->unlink_allowed = 0; /* ... or unlink it */
  3084. + obj->deleted = 0;
  3085. + obj->unlinked = 0;
  3086. + obj->yst_mode = mode;
  3087. + obj->my_dev = dev;
  3088. + obj->hdr_chunk = 0; /* Not a valid chunk. */
  3089. + }
  3090. +
  3091. + return obj;
  3092. +
  3093. +}
  3094. +
  3095. +static void yaffs_unhash_obj(struct yaffs_obj *obj)
  3096. +{
  3097. + int bucket;
  3098. + struct yaffs_dev *dev = obj->my_dev;
  3099. +
  3100. + /* If it is still linked into the bucket list, free from the list */
  3101. + if (!list_empty(&obj->hash_link)) {
  3102. + list_del_init(&obj->hash_link);
  3103. + bucket = yaffs_hash_fn(obj->obj_id);
  3104. + dev->obj_bucket[bucket].count--;
  3105. + }
  3106. +}
  3107. +
  3108. +/* FreeObject frees up a Object and puts it back on the free list */
  3109. +static void yaffs_free_obj(struct yaffs_obj *obj)
  3110. +{
  3111. + struct yaffs_dev *dev = obj->my_dev;
  3112. +
  3113. + T(YAFFS_TRACE_OS,
  3114. + (TSTR("FreeObject %p inode %p" TENDSTR), obj, obj->my_inode));
  3115. +
  3116. + if (!obj)
  3117. + YBUG();
  3118. + if (obj->parent)
  3119. + YBUG();
  3120. + if (!list_empty(&obj->siblings))
  3121. + YBUG();
  3122. +
  3123. + if (obj->my_inode) {
  3124. + /* We're still hooked up to a cached inode.
  3125. + * Don't delete now, but mark for later deletion
  3126. + */
  3127. + obj->defered_free = 1;
  3128. + return;
  3129. + }
  3130. +
  3131. + yaffs_unhash_obj(obj);
  3132. +
  3133. + yaffs_free_raw_obj(dev, obj);
  3134. + dev->n_obj--;
  3135. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3136. +}
  3137. +
  3138. +void yaffs_handle_defered_free(struct yaffs_obj *obj)
  3139. +{
  3140. + if (obj->defered_free)
  3141. + yaffs_free_obj(obj);
  3142. +}
  3143. +
  3144. +static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  3145. +{
  3146. + int i;
  3147. +
  3148. + dev->n_obj = 0;
  3149. + dev->n_tnodes = 0;
  3150. +
  3151. + yaffs_init_raw_tnodes_and_objs(dev);
  3152. +
  3153. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  3154. + INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  3155. + dev->obj_bucket[i].count = 0;
  3156. + }
  3157. +}
  3158. +
  3159. +static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  3160. +{
  3161. + int i;
  3162. + int l = 999;
  3163. + int lowest = 999999;
  3164. +
  3165. + /* Search for the shortest list or one that
  3166. + * isn't too long.
  3167. + */
  3168. +
  3169. + for (i = 0; i < 10 && lowest > 4; i++) {
  3170. + dev->bucket_finder++;
  3171. + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  3172. + if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  3173. + lowest = dev->obj_bucket[dev->bucket_finder].count;
  3174. + l = dev->bucket_finder;
  3175. + }
  3176. +
  3177. + }
  3178. +
  3179. + return l;
  3180. +}
  3181. +
  3182. +static int yaffs_new_obj_id(struct yaffs_dev *dev)
  3183. +{
  3184. + int bucket = yaffs_find_nice_bucket(dev);
  3185. +
  3186. + /* Now find an object value that has not already been taken
  3187. + * by scanning the list.
  3188. + */
  3189. +
  3190. + int found = 0;
  3191. + struct list_head *i;
  3192. +
  3193. + u32 n = (u32) bucket;
  3194. +
  3195. + /* yaffs_check_obj_hash_sane(); */
  3196. +
  3197. + while (!found) {
  3198. + found = 1;
  3199. + n += YAFFS_NOBJECT_BUCKETS;
  3200. + if (1 || dev->obj_bucket[bucket].count > 0) {
  3201. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3202. + /* If there is already one in the list */
  3203. + if (i && list_entry(i, struct yaffs_obj,
  3204. + hash_link)->obj_id == n) {
  3205. + found = 0;
  3206. + }
  3207. + }
  3208. + }
  3209. + }
  3210. +
  3211. + return n;
  3212. +}
  3213. +
  3214. +static void yaffs_hash_obj(struct yaffs_obj *in)
  3215. +{
  3216. + int bucket = yaffs_hash_fn(in->obj_id);
  3217. + struct yaffs_dev *dev = in->my_dev;
  3218. +
  3219. + list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  3220. + dev->obj_bucket[bucket].count++;
  3221. +}
  3222. +
  3223. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  3224. +{
  3225. + int bucket = yaffs_hash_fn(number);
  3226. + struct list_head *i;
  3227. + struct yaffs_obj *in;
  3228. +
  3229. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3230. + /* Look if it is in the list */
  3231. + if (i) {
  3232. + in = list_entry(i, struct yaffs_obj, hash_link);
  3233. + if (in->obj_id == number) {
  3234. +
  3235. + /* Don't tell the VFS about this one if it is defered free */
  3236. + if (in->defered_free)
  3237. + return NULL;
  3238. +
  3239. + return in;
  3240. + }
  3241. + }
  3242. + }
  3243. +
  3244. + return NULL;
  3245. +}
  3246. +
  3247. +struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  3248. + enum yaffs_obj_type type)
  3249. +{
  3250. + struct yaffs_obj *the_obj = NULL;
  3251. + struct yaffs_tnode *tn = NULL;
  3252. +
  3253. + if (number < 0)
  3254. + number = yaffs_new_obj_id(dev);
  3255. +
  3256. + if (type == YAFFS_OBJECT_TYPE_FILE) {
  3257. + tn = yaffs_get_tnode(dev);
  3258. + if (!tn)
  3259. + return NULL;
  3260. + }
  3261. +
  3262. + the_obj = yaffs_alloc_empty_obj(dev);
  3263. + if (!the_obj) {
  3264. + if (tn)
  3265. + yaffs_free_tnode(dev, tn);
  3266. + return NULL;
  3267. + }
  3268. +
  3269. + if (the_obj) {
  3270. + the_obj->fake = 0;
  3271. + the_obj->rename_allowed = 1;
  3272. + the_obj->unlink_allowed = 1;
  3273. + the_obj->obj_id = number;
  3274. + yaffs_hash_obj(the_obj);
  3275. + the_obj->variant_type = type;
  3276. + yaffs_load_current_time(the_obj, 1, 1);
  3277. +
  3278. + switch (type) {
  3279. + case YAFFS_OBJECT_TYPE_FILE:
  3280. + the_obj->variant.file_variant.file_size = 0;
  3281. + the_obj->variant.file_variant.scanned_size = 0;
  3282. + the_obj->variant.file_variant.shrink_size = ~0; /* max */
  3283. + the_obj->variant.file_variant.top_level = 0;
  3284. + the_obj->variant.file_variant.top = tn;
  3285. + break;
  3286. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  3287. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  3288. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  3289. + break;
  3290. + case YAFFS_OBJECT_TYPE_SYMLINK:
  3291. + case YAFFS_OBJECT_TYPE_HARDLINK:
  3292. + case YAFFS_OBJECT_TYPE_SPECIAL:
  3293. + /* No action required */
  3294. + break;
  3295. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  3296. + /* todo this should not happen */
  3297. + break;
  3298. + }
  3299. + }
  3300. +
  3301. + return the_obj;
  3302. +}
  3303. +
  3304. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  3305. + int number,
  3306. + enum yaffs_obj_type type)
  3307. +{
  3308. + struct yaffs_obj *the_obj = NULL;
  3309. +
  3310. + if (number > 0)
  3311. + the_obj = yaffs_find_by_number(dev, number);
  3312. +
  3313. + if (!the_obj)
  3314. + the_obj = yaffs_new_obj(dev, number, type);
  3315. +
  3316. + return the_obj;
  3317. +
  3318. +}
  3319. +
  3320. +YCHAR *yaffs_clone_str(const YCHAR * str)
  3321. +{
  3322. + YCHAR *new_str = NULL;
  3323. + int len;
  3324. +
  3325. + if (!str)
  3326. + str = _Y("");
  3327. +
  3328. + len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  3329. + new_str = YMALLOC((len + 1) * sizeof(YCHAR));
  3330. + if (new_str) {
  3331. + yaffs_strncpy(new_str, str, len);
  3332. + new_str[len] = 0;
  3333. + }
  3334. + return new_str;
  3335. +
  3336. +}
  3337. +
  3338. +/*
  3339. + * Mknod (create) a new object.
  3340. + * equiv_obj only has meaning for a hard link;
  3341. + * alias_str only has meaning for a symlink.
  3342. + * rdev only has meaning for devices (a subset of special objects)
  3343. + */
  3344. +
  3345. +static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  3346. + struct yaffs_obj *parent,
  3347. + const YCHAR * name,
  3348. + u32 mode,
  3349. + u32 uid,
  3350. + u32 gid,
  3351. + struct yaffs_obj *equiv_obj,
  3352. + const YCHAR * alias_str, u32 rdev)
  3353. +{
  3354. + struct yaffs_obj *in;
  3355. + YCHAR *str = NULL;
  3356. +
  3357. + struct yaffs_dev *dev = parent->my_dev;
  3358. +
  3359. + /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
  3360. + if (yaffs_find_by_name(parent, name))
  3361. + return NULL;
  3362. +
  3363. + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  3364. + str = yaffs_clone_str(alias_str);
  3365. + if (!str)
  3366. + return NULL;
  3367. + }
  3368. +
  3369. + in = yaffs_new_obj(dev, -1, type);
  3370. +
  3371. + if (!in) {
  3372. + if (str)
  3373. + YFREE(str);
  3374. + return NULL;
  3375. + }
  3376. +
  3377. + if (in) {
  3378. + in->hdr_chunk = 0;
  3379. + in->valid = 1;
  3380. + in->variant_type = type;
  3381. +
  3382. + in->yst_mode = mode;
  3383. +
  3384. + yaffs_attribs_init(in, gid, uid, rdev);
  3385. +
  3386. + in->n_data_chunks = 0;
  3387. +
  3388. + yaffs_set_obj_name(in, name);
  3389. + in->dirty = 1;
  3390. +
  3391. + yaffs_add_obj_to_dir(parent, in);
  3392. +
  3393. + in->my_dev = parent->my_dev;
  3394. +
  3395. + switch (type) {
  3396. + case YAFFS_OBJECT_TYPE_SYMLINK:
  3397. + in->variant.symlink_variant.alias = str;
  3398. + break;
  3399. + case YAFFS_OBJECT_TYPE_HARDLINK:
  3400. + in->variant.hardlink_variant.equiv_obj = equiv_obj;
  3401. + in->variant.hardlink_variant.equiv_id =
  3402. + equiv_obj->obj_id;
  3403. + list_add(&in->hard_links, &equiv_obj->hard_links);
  3404. + break;
  3405. + case YAFFS_OBJECT_TYPE_FILE:
  3406. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  3407. + case YAFFS_OBJECT_TYPE_SPECIAL:
  3408. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  3409. + /* do nothing */
  3410. + break;
  3411. + }
  3412. +
  3413. + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  3414. + /* Could not create the object header, fail the creation */
  3415. + yaffs_del_obj(in);
  3416. + in = NULL;
  3417. + }
  3418. +
  3419. + yaffs_update_parent(parent);
  3420. + }
  3421. +
  3422. + return in;
  3423. +}
  3424. +
  3425. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  3426. + const YCHAR * name, u32 mode, u32 uid,
  3427. + u32 gid)
  3428. +{
  3429. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  3430. + uid, gid, NULL, NULL, 0);
  3431. +}
  3432. +
  3433. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  3434. + u32 mode, u32 uid, u32 gid)
  3435. +{
  3436. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  3437. + mode, uid, gid, NULL, NULL, 0);
  3438. +}
  3439. +
  3440. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  3441. + const YCHAR * name, u32 mode, u32 uid,
  3442. + u32 gid, u32 rdev)
  3443. +{
  3444. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  3445. + uid, gid, NULL, NULL, rdev);
  3446. +}
  3447. +
  3448. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  3449. + const YCHAR * name, u32 mode, u32 uid,
  3450. + u32 gid, const YCHAR * alias)
  3451. +{
  3452. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  3453. + uid, gid, NULL, alias, 0);
  3454. +}
  3455. +
  3456. +/* yaffs_link_obj returns the object id of the equivalent object.*/
  3457. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  3458. + struct yaffs_obj *equiv_obj)
  3459. +{
  3460. + /* Get the real object in case we were fed a hard link as an equivalent object */
  3461. + equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  3462. +
  3463. + if (yaffs_create_obj
  3464. + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  3465. + equiv_obj, NULL, 0)) {
  3466. + return equiv_obj;
  3467. + } else {
  3468. + return NULL;
  3469. + }
  3470. +
  3471. +}
  3472. +
  3473. +static int yaffs_change_obj_name(struct yaffs_obj *obj,
  3474. + struct yaffs_obj *new_dir,
  3475. + const YCHAR * new_name, int force, int shadows)
  3476. +{
  3477. + int unlink_op;
  3478. + int del_op;
  3479. +
  3480. + struct yaffs_obj *existing_target;
  3481. +
  3482. + if (new_dir == NULL)
  3483. + new_dir = obj->parent; /* use the old directory */
  3484. +
  3485. + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3486. + T(YAFFS_TRACE_ALWAYS,
  3487. + (TSTR
  3488. + ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
  3489. + TENDSTR)));
  3490. + YBUG();
  3491. + }
  3492. +
  3493. + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  3494. + if (obj->my_dev->param.is_yaffs2)
  3495. + unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  3496. + else
  3497. + unlink_op = (new_dir == obj->my_dev->unlinked_dir
  3498. + && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
  3499. +
  3500. + del_op = (new_dir == obj->my_dev->del_dir);
  3501. +
  3502. + existing_target = yaffs_find_by_name(new_dir, new_name);
  3503. +
  3504. + /* If the object is a file going into the unlinked directory,
  3505. + * then it is OK to just stuff it in since duplicate names are allowed.
  3506. + * else only proceed if the new name does not exist and if we're putting
  3507. + * it into a directory.
  3508. + */
  3509. + if ((unlink_op ||
  3510. + del_op ||
  3511. + force ||
  3512. + (shadows > 0) ||
  3513. + !existing_target) &&
  3514. + new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  3515. + yaffs_set_obj_name(obj, new_name);
  3516. + obj->dirty = 1;
  3517. +
  3518. + yaffs_add_obj_to_dir(new_dir, obj);
  3519. +
  3520. + if (unlink_op)
  3521. + obj->unlinked = 1;
  3522. +
  3523. + /* If it is a deletion then we mark it as a shrink for gc purposes. */
  3524. + if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
  3525. + 0)
  3526. + return YAFFS_OK;
  3527. + }
  3528. +
  3529. + return YAFFS_FAIL;
  3530. +}
  3531. +
  3532. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  3533. + struct yaffs_obj *new_dir, const YCHAR * new_name)
  3534. +{
  3535. + struct yaffs_obj *obj = NULL;
  3536. + struct yaffs_obj *existing_target = NULL;
  3537. + int force = 0;
  3538. + int result;
  3539. + struct yaffs_dev *dev;
  3540. +
  3541. + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3542. + YBUG();
  3543. + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3544. + YBUG();
  3545. +
  3546. + dev = old_dir->my_dev;
  3547. +
  3548. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  3549. + /* Special case for case insemsitive systems.
  3550. + * While look-up is case insensitive, the name isn't.
  3551. + * Therefore we might want to change x.txt to X.txt
  3552. + */
  3553. + if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
  3554. + force = 1;
  3555. +#endif
  3556. +
  3557. + if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
  3558. + YAFFS_MAX_NAME_LENGTH)
  3559. + /* ENAMETOOLONG */
  3560. + return YAFFS_FAIL;
  3561. +
  3562. + obj = yaffs_find_by_name(old_dir, old_name);
  3563. +
  3564. + if (obj && obj->rename_allowed) {
  3565. +
  3566. + /* Now do the handling for an existing target, if there is one */
  3567. +
  3568. + existing_target = yaffs_find_by_name(new_dir, new_name);
  3569. + if (existing_target &&
  3570. + existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY
  3571. + && !list_empty(&existing_target->variant.dir_variant.
  3572. + children)) {
  3573. + /* There is a target that is a non-empty directory, so we fail */
  3574. + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
  3575. + } else if (existing_target && existing_target != obj) {
  3576. + /* Nuke the target first, using shadowing,
  3577. + * but only if it isn't the same object.
  3578. + *
  3579. + * Note we must disable gc otherwise it can mess up the shadowing.
  3580. + *
  3581. + */
  3582. + dev->gc_disable = 1;
  3583. + yaffs_change_obj_name(obj, new_dir, new_name, force,
  3584. + existing_target->obj_id);
  3585. + existing_target->is_shadowed = 1;
  3586. + yaffs_unlink_obj(existing_target);
  3587. + dev->gc_disable = 0;
  3588. + }
  3589. +
  3590. + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
  3591. +
  3592. + yaffs_update_parent(old_dir);
  3593. + if (new_dir != old_dir)
  3594. + yaffs_update_parent(new_dir);
  3595. +
  3596. + return result;
  3597. + }
  3598. + return YAFFS_FAIL;
  3599. +}
  3600. +
  3601. +/*------------------------- Block Management and Page Allocation ----------------*/
  3602. +
  3603. +static int yaffs_init_blocks(struct yaffs_dev *dev)
  3604. +{
  3605. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  3606. +
  3607. + dev->block_info = NULL;
  3608. + dev->chunk_bits = NULL;
  3609. +
  3610. + dev->alloc_block = -1; /* force it to get a new one */
  3611. +
  3612. + /* If the first allocation strategy fails, thry the alternate one */
  3613. + dev->block_info = YMALLOC(n_blocks * sizeof(struct yaffs_block_info));
  3614. + if (!dev->block_info) {
  3615. + dev->block_info =
  3616. + YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_info));
  3617. + dev->block_info_alt = 1;
  3618. + } else {
  3619. + dev->block_info_alt = 0;
  3620. + }
  3621. +
  3622. + if (dev->block_info) {
  3623. + /* Set up dynamic blockinfo stuff. */
  3624. + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
  3625. + dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
  3626. + if (!dev->chunk_bits) {
  3627. + dev->chunk_bits =
  3628. + YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
  3629. + dev->chunk_bits_alt = 1;
  3630. + } else {
  3631. + dev->chunk_bits_alt = 0;
  3632. + }
  3633. + }
  3634. +
  3635. + if (dev->block_info && dev->chunk_bits) {
  3636. + memset(dev->block_info, 0,
  3637. + n_blocks * sizeof(struct yaffs_block_info));
  3638. + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  3639. + return YAFFS_OK;
  3640. + }
  3641. +
  3642. + return YAFFS_FAIL;
  3643. +}
  3644. +
  3645. +static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  3646. +{
  3647. + if (dev->block_info_alt && dev->block_info)
  3648. + YFREE_ALT(dev->block_info);
  3649. + else if (dev->block_info)
  3650. + YFREE(dev->block_info);
  3651. +
  3652. + dev->block_info_alt = 0;
  3653. +
  3654. + dev->block_info = NULL;
  3655. +
  3656. + if (dev->chunk_bits_alt && dev->chunk_bits)
  3657. + YFREE_ALT(dev->chunk_bits);
  3658. + else if (dev->chunk_bits)
  3659. + YFREE(dev->chunk_bits);
  3660. + dev->chunk_bits_alt = 0;
  3661. + dev->chunk_bits = NULL;
  3662. +}
  3663. +
  3664. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  3665. +{
  3666. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  3667. +
  3668. + int erased_ok = 0;
  3669. +
  3670. + /* If the block is still healthy erase it and mark as clean.
  3671. + * If the block has had a data failure, then retire it.
  3672. + */
  3673. +
  3674. + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  3675. + (TSTR("yaffs_block_became_dirty block %d state %d %s" TENDSTR),
  3676. + block_no, bi->block_state,
  3677. + (bi->needs_retiring) ? "needs retiring" : ""));
  3678. +
  3679. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  3680. +
  3681. + bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  3682. +
  3683. + /* If this is the block being garbage collected then stop gc'ing this block */
  3684. + if (block_no == dev->gc_block)
  3685. + dev->gc_block = 0;
  3686. +
  3687. + /* If this block is currently the best candidate for gc then drop as a candidate */
  3688. + if (block_no == dev->gc_dirtiest) {
  3689. + dev->gc_dirtiest = 0;
  3690. + dev->gc_pages_in_use = 0;
  3691. + }
  3692. +
  3693. + if (!bi->needs_retiring) {
  3694. + yaffs2_checkpt_invalidate(dev);
  3695. + erased_ok = yaffs_erase_block(dev, block_no);
  3696. + if (!erased_ok) {
  3697. + dev->n_erase_failures++;
  3698. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  3699. + (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
  3700. + }
  3701. + }
  3702. +
  3703. + if (erased_ok &&
  3704. + ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
  3705. + || !yaffs_skip_verification(dev))) {
  3706. + int i;
  3707. + for (i = 0; i < dev->param.chunks_per_block; i++) {
  3708. + if (!yaffs_check_chunk_erased
  3709. + (dev, block_no * dev->param.chunks_per_block + i)) {
  3710. + T(YAFFS_TRACE_ERROR,
  3711. + (TSTR
  3712. + (">>Block %d erasure supposedly OK, but chunk %d not erased"
  3713. + TENDSTR), block_no, i));
  3714. + }
  3715. + }
  3716. + }
  3717. +
  3718. + if (erased_ok) {
  3719. + /* Clean it up... */
  3720. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  3721. + bi->seq_number = 0;
  3722. + dev->n_erased_blocks++;
  3723. + bi->pages_in_use = 0;
  3724. + bi->soft_del_pages = 0;
  3725. + bi->has_shrink_hdr = 0;
  3726. + bi->skip_erased_check = 1; /* This is clean, so no need to check */
  3727. + bi->gc_prioritise = 0;
  3728. + yaffs_clear_chunk_bits(dev, block_no);
  3729. +
  3730. + T(YAFFS_TRACE_ERASE,
  3731. + (TSTR("Erased block %d" TENDSTR), block_no));
  3732. + } else {
  3733. + dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
  3734. +
  3735. + yaffs_retire_block(dev, block_no);
  3736. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  3737. + (TSTR("**>> Block %d retired" TENDSTR), block_no));
  3738. + }
  3739. +}
  3740. +
  3741. +static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  3742. +{
  3743. + int i;
  3744. +
  3745. + struct yaffs_block_info *bi;
  3746. +
  3747. + if (dev->n_erased_blocks < 1) {
  3748. + /* Hoosterman we've got a problem.
  3749. + * Can't get space to gc
  3750. + */
  3751. + T(YAFFS_TRACE_ERROR,
  3752. + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
  3753. +
  3754. + return -1;
  3755. + }
  3756. +
  3757. + /* Find an empty block. */
  3758. +
  3759. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  3760. + dev->alloc_block_finder++;
  3761. + if (dev->alloc_block_finder < dev->internal_start_block
  3762. + || dev->alloc_block_finder > dev->internal_end_block) {
  3763. + dev->alloc_block_finder = dev->internal_start_block;
  3764. + }
  3765. +
  3766. + bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  3767. +
  3768. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  3769. + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  3770. + dev->seq_number++;
  3771. + bi->seq_number = dev->seq_number;
  3772. + dev->n_erased_blocks--;
  3773. + T(YAFFS_TRACE_ALLOCATE,
  3774. + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
  3775. + dev->alloc_block_finder, dev->seq_number,
  3776. + dev->n_erased_blocks));
  3777. + return dev->alloc_block_finder;
  3778. + }
  3779. + }
  3780. +
  3781. + T(YAFFS_TRACE_ALWAYS,
  3782. + (TSTR
  3783. + ("yaffs tragedy: no more erased blocks, but there should have been %d"
  3784. + TENDSTR), dev->n_erased_blocks));
  3785. +
  3786. + return -1;
  3787. +}
  3788. +
  3789. +/*
  3790. + * Check if there's space to allocate...
  3791. + * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
  3792. + */
  3793. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  3794. +{
  3795. + int reserved_chunks;
  3796. + int reserved_blocks = dev->param.n_reserved_blocks;
  3797. + int checkpt_blocks;
  3798. +
  3799. + checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  3800. +
  3801. + reserved_chunks =
  3802. + ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
  3803. +
  3804. + return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  3805. +}
  3806. +
  3807. +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  3808. + struct yaffs_block_info **block_ptr)
  3809. +{
  3810. + int ret_val;
  3811. + struct yaffs_block_info *bi;
  3812. +
  3813. + if (dev->alloc_block < 0) {
  3814. + /* Get next block to allocate off */
  3815. + dev->alloc_block = yaffs_find_alloc_block(dev);
  3816. + dev->alloc_page = 0;
  3817. + }
  3818. +
  3819. + if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  3820. + /* Not enough space to allocate unless we're allowed to use the reserve. */
  3821. + return -1;
  3822. + }
  3823. +
  3824. + if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  3825. + && dev->alloc_page == 0) {
  3826. + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
  3827. + }
  3828. +
  3829. + /* Next page please.... */
  3830. + if (dev->alloc_block >= 0) {
  3831. + bi = yaffs_get_block_info(dev, dev->alloc_block);
  3832. +
  3833. + ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  3834. + dev->alloc_page;
  3835. + bi->pages_in_use++;
  3836. + yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  3837. +
  3838. + dev->alloc_page++;
  3839. +
  3840. + dev->n_free_chunks--;
  3841. +
  3842. + /* If the block is full set the state to full */
  3843. + if (dev->alloc_page >= dev->param.chunks_per_block) {
  3844. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  3845. + dev->alloc_block = -1;
  3846. + }
  3847. +
  3848. + if (block_ptr)
  3849. + *block_ptr = bi;
  3850. +
  3851. + return ret_val;
  3852. + }
  3853. +
  3854. + T(YAFFS_TRACE_ERROR,
  3855. + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
  3856. +
  3857. + return -1;
  3858. +}
  3859. +
  3860. +static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  3861. +{
  3862. + int n;
  3863. +
  3864. + n = dev->n_erased_blocks * dev->param.chunks_per_block;
  3865. +
  3866. + if (dev->alloc_block > 0)
  3867. + n += (dev->param.chunks_per_block - dev->alloc_page);
  3868. +
  3869. + return n;
  3870. +
  3871. +}
  3872. +
  3873. +/*
  3874. + * yaffs_skip_rest_of_block() skips over the rest of the allocation block
  3875. + * if we don't want to write to it.
  3876. + */
  3877. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  3878. +{
  3879. + if (dev->alloc_block > 0) {
  3880. + struct yaffs_block_info *bi =
  3881. + yaffs_get_block_info(dev, dev->alloc_block);
  3882. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  3883. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  3884. + dev->alloc_block = -1;
  3885. + }
  3886. + }
  3887. +}
  3888. +
  3889. +static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  3890. +{
  3891. + int old_chunk;
  3892. + int new_chunk;
  3893. + int mark_flash;
  3894. + int ret_val = YAFFS_OK;
  3895. + int i;
  3896. + int is_checkpt_block;
  3897. + int matching_chunk;
  3898. + int max_copies;
  3899. +
  3900. + int chunks_before = yaffs_get_erased_chunks(dev);
  3901. + int chunks_after;
  3902. +
  3903. + struct yaffs_ext_tags tags;
  3904. +
  3905. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  3906. +
  3907. + struct yaffs_obj *object;
  3908. +
  3909. + is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  3910. +
  3911. + T(YAFFS_TRACE_TRACING,
  3912. + (TSTR
  3913. + ("Collecting block %d, in use %d, shrink %d, whole_block %d"
  3914. + TENDSTR), block, bi->pages_in_use, bi->has_shrink_hdr,
  3915. + whole_block));
  3916. +
  3917. + /*yaffs_verify_free_chunks(dev); */
  3918. +
  3919. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  3920. + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  3921. +
  3922. + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
  3923. +
  3924. + dev->gc_disable = 1;
  3925. +
  3926. + if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  3927. + T(YAFFS_TRACE_TRACING,
  3928. + (TSTR
  3929. + ("Collecting block %d that has no chunks in use" TENDSTR),
  3930. + block));
  3931. + yaffs_block_became_dirty(dev, block);
  3932. + } else {
  3933. +
  3934. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  3935. +
  3936. + yaffs_verify_blk(dev, bi, block);
  3937. +
  3938. + max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  3939. + old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  3940. +
  3941. + for ( /* init already done */ ;
  3942. + ret_val == YAFFS_OK &&
  3943. + dev->gc_chunk < dev->param.chunks_per_block &&
  3944. + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  3945. + max_copies > 0; dev->gc_chunk++, old_chunk++) {
  3946. + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  3947. +
  3948. + /* This page is in use and might need to be copied off */
  3949. +
  3950. + max_copies--;
  3951. +
  3952. + mark_flash = 1;
  3953. +
  3954. + yaffs_init_tags(&tags);
  3955. +
  3956. + yaffs_rd_chunk_tags_nand(dev, old_chunk,
  3957. + buffer, &tags);
  3958. +
  3959. + object = yaffs_find_by_number(dev, tags.obj_id);
  3960. +
  3961. + T(YAFFS_TRACE_GC_DETAIL,
  3962. + (TSTR
  3963. + ("Collecting chunk in block %d, %d %d %d "
  3964. + TENDSTR), dev->gc_chunk, tags.obj_id,
  3965. + tags.chunk_id, tags.n_bytes));
  3966. +
  3967. + if (object && !yaffs_skip_verification(dev)) {
  3968. + if (tags.chunk_id == 0)
  3969. + matching_chunk =
  3970. + object->hdr_chunk;
  3971. + else if (object->soft_del)
  3972. + matching_chunk = old_chunk; /* Defeat the test */
  3973. + else
  3974. + matching_chunk =
  3975. + yaffs_find_chunk_in_file
  3976. + (object, tags.chunk_id,
  3977. + NULL);
  3978. +
  3979. + if (old_chunk != matching_chunk)
  3980. + T(YAFFS_TRACE_ERROR,
  3981. + (TSTR
  3982. + ("gc: page in gc mismatch: %d %d %d %d"
  3983. + TENDSTR), old_chunk,
  3984. + matching_chunk, tags.obj_id,
  3985. + tags.chunk_id));
  3986. +
  3987. + }
  3988. +
  3989. + if (!object) {
  3990. + T(YAFFS_TRACE_ERROR,
  3991. + (TSTR
  3992. + ("page %d in gc has no object: %d %d %d "
  3993. + TENDSTR), old_chunk,
  3994. + tags.obj_id, tags.chunk_id,
  3995. + tags.n_bytes));
  3996. + }
  3997. +
  3998. + if (object &&
  3999. + object->deleted &&
  4000. + object->soft_del && tags.chunk_id != 0) {
  4001. + /* Data chunk in a soft deleted file, throw it away
  4002. + * It's a soft deleted data chunk,
  4003. + * No need to copy this, just forget about it and
  4004. + * fix up the object.
  4005. + */
  4006. +
  4007. + /* Free chunks already includes softdeleted chunks.
  4008. + * How ever this chunk is going to soon be really deleted
  4009. + * which will increment free chunks.
  4010. + * We have to decrement free chunks so this works out properly.
  4011. + */
  4012. + dev->n_free_chunks--;
  4013. + bi->soft_del_pages--;
  4014. +
  4015. + object->n_data_chunks--;
  4016. +
  4017. + if (object->n_data_chunks <= 0) {
  4018. + /* remeber to clean up the object */
  4019. + dev->gc_cleanup_list[dev->
  4020. + n_clean_ups]
  4021. + = tags.obj_id;
  4022. + dev->n_clean_ups++;
  4023. + }
  4024. + mark_flash = 0;
  4025. + } else if (0) {
  4026. + /* Todo object && object->deleted && object->n_data_chunks == 0 */
  4027. + /* Deleted object header with no data chunks.
  4028. + * Can be discarded and the file deleted.
  4029. + */
  4030. + object->hdr_chunk = 0;
  4031. + yaffs_free_tnode(object->my_dev,
  4032. + object->
  4033. + variant.file_variant.
  4034. + top);
  4035. + object->variant.file_variant.top = NULL;
  4036. + yaffs_generic_obj_del(object);
  4037. +
  4038. + } else if (object) {
  4039. + /* It's either a data chunk in a live file or
  4040. + * an ObjectHeader, so we're interested in it.
  4041. + * NB Need to keep the ObjectHeaders of deleted files
  4042. + * until the whole file has been deleted off
  4043. + */
  4044. + tags.serial_number++;
  4045. +
  4046. + dev->n_gc_copies++;
  4047. +
  4048. + if (tags.chunk_id == 0) {
  4049. + /* It is an object Id,
  4050. + * We need to nuke the shrinkheader flags first
  4051. + * Also need to clean up shadowing.
  4052. + * We no longer want the shrink_header flag since its work is done
  4053. + * and if it is left in place it will mess up scanning.
  4054. + */
  4055. +
  4056. + struct yaffs_obj_hdr *oh;
  4057. + oh = (struct yaffs_obj_hdr *)
  4058. + buffer;
  4059. +
  4060. + oh->is_shrink = 0;
  4061. + tags.extra_is_shrink = 0;
  4062. +
  4063. + oh->shadows_obj = 0;
  4064. + oh->inband_shadowed_obj_id = 0;
  4065. + tags.extra_shadows = 0;
  4066. +
  4067. + /* Update file size */
  4068. + if (object->variant_type ==
  4069. + YAFFS_OBJECT_TYPE_FILE) {
  4070. + oh->file_size =
  4071. + object->variant.
  4072. + file_variant.
  4073. + file_size;
  4074. + tags.extra_length =
  4075. + oh->file_size;
  4076. + }
  4077. +
  4078. + yaffs_verify_oh(object, oh,
  4079. + &tags, 1);
  4080. + new_chunk =
  4081. + yaffs_write_new_chunk(dev,
  4082. + (u8 *)
  4083. + oh,
  4084. + &tags,
  4085. + 1);
  4086. + } else {
  4087. + new_chunk =
  4088. + yaffs_write_new_chunk(dev,
  4089. + buffer,
  4090. + &tags,
  4091. + 1);
  4092. + }
  4093. +
  4094. + if (new_chunk < 0) {
  4095. + ret_val = YAFFS_FAIL;
  4096. + } else {
  4097. +
  4098. + /* Ok, now fix up the Tnodes etc. */
  4099. +
  4100. + if (tags.chunk_id == 0) {
  4101. + /* It's a header */
  4102. + object->hdr_chunk =
  4103. + new_chunk;
  4104. + object->serial =
  4105. + tags.serial_number;
  4106. + } else {
  4107. + /* It's a data chunk */
  4108. + int ok;
  4109. + ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
  4110. + }
  4111. + }
  4112. + }
  4113. +
  4114. + if (ret_val == YAFFS_OK)
  4115. + yaffs_chunk_del(dev, old_chunk,
  4116. + mark_flash, __LINE__);
  4117. +
  4118. + }
  4119. + }
  4120. +
  4121. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  4122. +
  4123. + }
  4124. +
  4125. + yaffs_verify_collected_blk(dev, bi, block);
  4126. +
  4127. + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4128. + /*
  4129. + * The gc did not complete. Set block state back to FULL
  4130. + * because checkpointing does not restore gc.
  4131. + */
  4132. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  4133. + } else {
  4134. + /* The gc completed. */
  4135. + /* Do any required cleanups */
  4136. + for (i = 0; i < dev->n_clean_ups; i++) {
  4137. + /* Time to delete the file too */
  4138. + object =
  4139. + yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
  4140. + if (object) {
  4141. + yaffs_free_tnode(dev,
  4142. + object->variant.
  4143. + file_variant.top);
  4144. + object->variant.file_variant.top = NULL;
  4145. + T(YAFFS_TRACE_GC,
  4146. + (TSTR
  4147. + ("yaffs: About to finally delete object %d"
  4148. + TENDSTR), object->obj_id));
  4149. + yaffs_generic_obj_del(object);
  4150. + object->my_dev->n_deleted_files--;
  4151. + }
  4152. +
  4153. + }
  4154. +
  4155. + chunks_after = yaffs_get_erased_chunks(dev);
  4156. + if (chunks_before >= chunks_after) {
  4157. + T(YAFFS_TRACE_GC,
  4158. + (TSTR
  4159. + ("gc did not increase free chunks before %d after %d"
  4160. + TENDSTR), chunks_before, chunks_after));
  4161. + }
  4162. + dev->gc_block = 0;
  4163. + dev->gc_chunk = 0;
  4164. + dev->n_clean_ups = 0;
  4165. + }
  4166. +
  4167. + dev->gc_disable = 0;
  4168. +
  4169. + return ret_val;
  4170. +}
  4171. +
  4172. +/*
  4173. + * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
  4174. + * for garbage collection.
  4175. + */
  4176. +
  4177. +static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
  4178. + int aggressive, int background)
  4179. +{
  4180. + int i;
  4181. + int iterations;
  4182. + unsigned selected = 0;
  4183. + int prioritised = 0;
  4184. + int prioritised_exist = 0;
  4185. + struct yaffs_block_info *bi;
  4186. + int threshold;
  4187. +
  4188. + /* First let's see if we need to grab a prioritised block */
  4189. + if (dev->has_pending_prioritised_gc && !aggressive) {
  4190. + dev->gc_dirtiest = 0;
  4191. + bi = dev->block_info;
  4192. + for (i = dev->internal_start_block;
  4193. + i <= dev->internal_end_block && !selected; i++) {
  4194. +
  4195. + if (bi->gc_prioritise) {
  4196. + prioritised_exist = 1;
  4197. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4198. + yaffs_block_ok_for_gc(dev, bi)) {
  4199. + selected = i;
  4200. + prioritised = 1;
  4201. + }
  4202. + }
  4203. + bi++;
  4204. + }
  4205. +
  4206. + /*
  4207. + * If there is a prioritised block and none was selected then
  4208. + * this happened because there is at least one old dirty block gumming
  4209. + * up the works. Let's gc the oldest dirty block.
  4210. + */
  4211. +
  4212. + if (prioritised_exist &&
  4213. + !selected && dev->oldest_dirty_block > 0)
  4214. + selected = dev->oldest_dirty_block;
  4215. +
  4216. + if (!prioritised_exist) /* None found, so we can clear this */
  4217. + dev->has_pending_prioritised_gc = 0;
  4218. + }
  4219. +
  4220. + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  4221. + * search harder.
  4222. + * else (we're doing a leasurely gc), then we only bother to do this if the
  4223. + * block has only a few pages in use.
  4224. + */
  4225. +
  4226. + if (!selected) {
  4227. + int pages_used;
  4228. + int n_blocks =
  4229. + dev->internal_end_block - dev->internal_start_block + 1;
  4230. + if (aggressive) {
  4231. + threshold = dev->param.chunks_per_block;
  4232. + iterations = n_blocks;
  4233. + } else {
  4234. + int max_threshold;
  4235. +
  4236. + if (background)
  4237. + max_threshold = dev->param.chunks_per_block / 2;
  4238. + else
  4239. + max_threshold = dev->param.chunks_per_block / 8;
  4240. +
  4241. + if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4242. + max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4243. +
  4244. + threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
  4245. + if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4246. + threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4247. + if (threshold > max_threshold)
  4248. + threshold = max_threshold;
  4249. +
  4250. + iterations = n_blocks / 16 + 1;
  4251. + if (iterations > 100)
  4252. + iterations = 100;
  4253. + }
  4254. +
  4255. + for (i = 0;
  4256. + i < iterations &&
  4257. + (dev->gc_dirtiest < 1 ||
  4258. + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
  4259. + dev->gc_block_finder++;
  4260. + if (dev->gc_block_finder < dev->internal_start_block ||
  4261. + dev->gc_block_finder > dev->internal_end_block)
  4262. + dev->gc_block_finder =
  4263. + dev->internal_start_block;
  4264. +
  4265. + bi = yaffs_get_block_info(dev, dev->gc_block_finder);
  4266. +
  4267. + pages_used = bi->pages_in_use - bi->soft_del_pages;
  4268. +
  4269. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4270. + pages_used < dev->param.chunks_per_block &&
  4271. + (dev->gc_dirtiest < 1
  4272. + || pages_used < dev->gc_pages_in_use)
  4273. + && yaffs_block_ok_for_gc(dev, bi)) {
  4274. + dev->gc_dirtiest = dev->gc_block_finder;
  4275. + dev->gc_pages_in_use = pages_used;
  4276. + }
  4277. + }
  4278. +
  4279. + if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
  4280. + selected = dev->gc_dirtiest;
  4281. + }
  4282. +
  4283. + /*
  4284. + * If nothing has been selected for a while, try selecting the oldest dirty
  4285. + * because that's gumming up the works.
  4286. + */
  4287. +
  4288. + if (!selected && dev->param.is_yaffs2 &&
  4289. + dev->gc_not_done >= (background ? 10 : 20)) {
  4290. + yaffs2_find_oldest_dirty_seq(dev);
  4291. + if (dev->oldest_dirty_block > 0) {
  4292. + selected = dev->oldest_dirty_block;
  4293. + dev->gc_dirtiest = selected;
  4294. + dev->oldest_dirty_gc_count++;
  4295. + bi = yaffs_get_block_info(dev, selected);
  4296. + dev->gc_pages_in_use =
  4297. + bi->pages_in_use - bi->soft_del_pages;
  4298. + } else {
  4299. + dev->gc_not_done = 0;
  4300. + }
  4301. + }
  4302. +
  4303. + if (selected) {
  4304. + T(YAFFS_TRACE_GC,
  4305. + (TSTR
  4306. + ("GC Selected block %d with %d free, prioritised:%d"
  4307. + TENDSTR), selected,
  4308. + dev->param.chunks_per_block - dev->gc_pages_in_use,
  4309. + prioritised));
  4310. +
  4311. + dev->n_gc_blocks++;
  4312. + if (background)
  4313. + dev->bg_gcs++;
  4314. +
  4315. + dev->gc_dirtiest = 0;
  4316. + dev->gc_pages_in_use = 0;
  4317. + dev->gc_not_done = 0;
  4318. + if (dev->refresh_skip > 0)
  4319. + dev->refresh_skip--;
  4320. + } else {
  4321. + dev->gc_not_done++;
  4322. + T(YAFFS_TRACE_GC,
  4323. + (TSTR
  4324. + ("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s"
  4325. + TENDSTR), dev->gc_block_finder, dev->gc_not_done, threshold,
  4326. + dev->gc_dirtiest, dev->gc_pages_in_use,
  4327. + dev->oldest_dirty_block, background ? " bg" : ""));
  4328. + }
  4329. +
  4330. + return selected;
  4331. +}
  4332. +
  4333. +/* New garbage collector
  4334. + * If we're very low on erased blocks then we do aggressive garbage collection
  4335. + * otherwise we do "leasurely" garbage collection.
  4336. + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  4337. + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  4338. + *
  4339. + * The idea is to help clear out space in a more spread-out manner.
  4340. + * Dunno if it really does anything useful.
  4341. + */
  4342. +static int yaffs_check_gc(struct yaffs_dev *dev, int background)
  4343. +{
  4344. + int aggressive = 0;
  4345. + int gc_ok = YAFFS_OK;
  4346. + int max_tries = 0;
  4347. + int min_erased;
  4348. + int erased_chunks;
  4349. + int checkpt_block_adjust;
  4350. +
  4351. + if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
  4352. + return YAFFS_OK;
  4353. +
  4354. + if (dev->gc_disable) {
  4355. + /* Bail out so we don't get recursive gc */
  4356. + return YAFFS_OK;
  4357. + }
  4358. +
  4359. + /* This loop should pass the first time.
  4360. + * We'll only see looping here if the collection does not increase space.
  4361. + */
  4362. +
  4363. + do {
  4364. + max_tries++;
  4365. +
  4366. + checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
  4367. +
  4368. + min_erased =
  4369. + dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
  4370. + erased_chunks =
  4371. + dev->n_erased_blocks * dev->param.chunks_per_block;
  4372. +
  4373. + /* If we need a block soon then do aggressive gc. */
  4374. + if (dev->n_erased_blocks < min_erased)
  4375. + aggressive = 1;
  4376. + else {
  4377. + if (!background
  4378. + && erased_chunks > (dev->n_free_chunks / 4))
  4379. + break;
  4380. +
  4381. + if (dev->gc_skip > 20)
  4382. + dev->gc_skip = 20;
  4383. + if (erased_chunks < dev->n_free_chunks / 2 ||
  4384. + dev->gc_skip < 1 || background)
  4385. + aggressive = 0;
  4386. + else {
  4387. + dev->gc_skip--;
  4388. + break;
  4389. + }
  4390. + }
  4391. +
  4392. + dev->gc_skip = 5;
  4393. +
  4394. + /* If we don't already have a block being gc'd then see if we should start another */
  4395. +
  4396. + if (dev->gc_block < 1 && !aggressive) {
  4397. + dev->gc_block = yaffs2_find_refresh_block(dev);
  4398. + dev->gc_chunk = 0;
  4399. + dev->n_clean_ups = 0;
  4400. + }
  4401. + if (dev->gc_block < 1) {
  4402. + dev->gc_block =
  4403. + yaffs_find_gc_block(dev, aggressive, background);
  4404. + dev->gc_chunk = 0;
  4405. + dev->n_clean_ups = 0;
  4406. + }
  4407. +
  4408. + if (dev->gc_block > 0) {
  4409. + dev->all_gcs++;
  4410. + if (!aggressive)
  4411. + dev->passive_gc_count++;
  4412. +
  4413. + T(YAFFS_TRACE_GC,
  4414. + (TSTR
  4415. + ("yaffs: GC n_erased_blocks %d aggressive %d"
  4416. + TENDSTR), dev->n_erased_blocks, aggressive));
  4417. +
  4418. + gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
  4419. + }
  4420. +
  4421. + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
  4422. + && dev->gc_block > 0) {
  4423. + T(YAFFS_TRACE_GC,
  4424. + (TSTR
  4425. + ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
  4426. + TENDSTR), dev->n_erased_blocks, max_tries,
  4427. + dev->gc_block));
  4428. + }
  4429. + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
  4430. + (dev->gc_block > 0) && (max_tries < 2));
  4431. +
  4432. + return aggressive ? gc_ok : YAFFS_OK;
  4433. +}
  4434. +
  4435. +/*
  4436. + * yaffs_bg_gc()
  4437. + * Garbage collects. Intended to be called from a background thread.
  4438. + * Returns non-zero if at least half the free chunks are erased.
  4439. + */
  4440. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
  4441. +{
  4442. + int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
  4443. +
  4444. + T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR), urgency));
  4445. +
  4446. + yaffs_check_gc(dev, 1);
  4447. + return erased_chunks > dev->n_free_chunks / 2;
  4448. +}
  4449. +
  4450. +/*------------------------- TAGS --------------------------------*/
  4451. +
  4452. +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  4453. + int chunk_obj)
  4454. +{
  4455. + return (tags->chunk_id == chunk_obj &&
  4456. + tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
  4457. +
  4458. +}
  4459. +
  4460. +/*-------------------- Data file manipulation -----------------*/
  4461. +
  4462. +static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  4463. + struct yaffs_ext_tags *tags)
  4464. +{
  4465. + /*Get the Tnode, then get the level 0 offset chunk offset */
  4466. + struct yaffs_tnode *tn;
  4467. + int the_chunk = -1;
  4468. + struct yaffs_ext_tags local_tags;
  4469. + int ret_val = -1;
  4470. +
  4471. + struct yaffs_dev *dev = in->my_dev;
  4472. +
  4473. + if (!tags) {
  4474. + /* Passed a NULL, so use our own tags space */
  4475. + tags = &local_tags;
  4476. + }
  4477. +
  4478. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  4479. +
  4480. + if (tn) {
  4481. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4482. +
  4483. + ret_val =
  4484. + yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  4485. + inode_chunk);
  4486. + }
  4487. + return ret_val;
  4488. +}
  4489. +
  4490. +static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  4491. + struct yaffs_ext_tags *tags)
  4492. +{
  4493. + /* Get the Tnode, then get the level 0 offset chunk offset */
  4494. + struct yaffs_tnode *tn;
  4495. + int the_chunk = -1;
  4496. + struct yaffs_ext_tags local_tags;
  4497. +
  4498. + struct yaffs_dev *dev = in->my_dev;
  4499. + int ret_val = -1;
  4500. +
  4501. + if (!tags) {
  4502. + /* Passed a NULL, so use our own tags space */
  4503. + tags = &local_tags;
  4504. + }
  4505. +
  4506. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  4507. +
  4508. + if (tn) {
  4509. +
  4510. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4511. +
  4512. + ret_val =
  4513. + yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  4514. + inode_chunk);
  4515. +
  4516. + /* Delete the entry in the filestructure (if found) */
  4517. + if (ret_val != -1)
  4518. + yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  4519. + }
  4520. +
  4521. + return ret_val;
  4522. +}
  4523. +
  4524. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  4525. + int nand_chunk, int in_scan)
  4526. +{
  4527. + /* NB in_scan is zero unless scanning.
  4528. + * For forward scanning, in_scan is > 0;
  4529. + * for backward scanning in_scan is < 0
  4530. + *
  4531. + * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
  4532. + */
  4533. +
  4534. + struct yaffs_tnode *tn;
  4535. + struct yaffs_dev *dev = in->my_dev;
  4536. + int existing_cunk;
  4537. + struct yaffs_ext_tags existing_tags;
  4538. + struct yaffs_ext_tags new_tags;
  4539. + unsigned existing_serial, new_serial;
  4540. +
  4541. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  4542. + /* Just ignore an attempt at putting a chunk into a non-file during scanning
  4543. + * If it is not during Scanning then something went wrong!
  4544. + */
  4545. + if (!in_scan) {
  4546. + T(YAFFS_TRACE_ERROR,
  4547. + (TSTR
  4548. + ("yaffs tragedy:attempt to put data chunk into a non-file"
  4549. + TENDSTR)));
  4550. + YBUG();
  4551. + }
  4552. +
  4553. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  4554. + return YAFFS_OK;
  4555. + }
  4556. +
  4557. + tn = yaffs_add_find_tnode_0(dev,
  4558. + &in->variant.file_variant,
  4559. + inode_chunk, NULL);
  4560. + if (!tn)
  4561. + return YAFFS_FAIL;
  4562. +
  4563. + if (!nand_chunk)
  4564. + /* Dummy insert, bail now */
  4565. + return YAFFS_OK;
  4566. +
  4567. + existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4568. +
  4569. + if (in_scan != 0) {
  4570. + /* If we're scanning then we need to test for duplicates
  4571. + * NB This does not need to be efficient since it should only ever
  4572. + * happen when the power fails during a write, then only one
  4573. + * chunk should ever be affected.
  4574. + *
  4575. + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  4576. + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  4577. + */
  4578. +
  4579. + if (existing_cunk > 0) {
  4580. + /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
  4581. + * thus we have to do a FindChunkInFile to get the real chunk id.
  4582. + *
  4583. + * We have a duplicate now we need to decide which one to use:
  4584. + *
  4585. + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  4586. + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  4587. + * YAFFS1: Get both sets of tags and compare serial numbers.
  4588. + */
  4589. +
  4590. + if (in_scan > 0) {
  4591. + /* Only do this for forward scanning */
  4592. + yaffs_rd_chunk_tags_nand(dev,
  4593. + nand_chunk,
  4594. + NULL, &new_tags);
  4595. +
  4596. + /* Do a proper find */
  4597. + existing_cunk =
  4598. + yaffs_find_chunk_in_file(in, inode_chunk,
  4599. + &existing_tags);
  4600. + }
  4601. +
  4602. + if (existing_cunk <= 0) {
  4603. + /*Hoosterman - how did this happen? */
  4604. +
  4605. + T(YAFFS_TRACE_ERROR,
  4606. + (TSTR
  4607. + ("yaffs tragedy: existing chunk < 0 in scan"
  4608. + TENDSTR)));
  4609. +
  4610. + }
  4611. +
  4612. + /* NB The deleted flags should be false, otherwise the chunks will
  4613. + * not be loaded during a scan
  4614. + */
  4615. +
  4616. + if (in_scan > 0) {
  4617. + new_serial = new_tags.serial_number;
  4618. + existing_serial = existing_tags.serial_number;
  4619. + }
  4620. +
  4621. + if ((in_scan > 0) &&
  4622. + (existing_cunk <= 0 ||
  4623. + ((existing_serial + 1) & 3) == new_serial)) {
  4624. + /* Forward scanning.
  4625. + * Use new
  4626. + * Delete the old one and drop through to update the tnode
  4627. + */
  4628. + yaffs_chunk_del(dev, existing_cunk, 1,
  4629. + __LINE__);
  4630. + } else {
  4631. + /* Backward scanning or we want to use the existing one
  4632. + * Use existing.
  4633. + * Delete the new one and return early so that the tnode isn't changed
  4634. + */
  4635. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  4636. + return YAFFS_OK;
  4637. + }
  4638. + }
  4639. +
  4640. + }
  4641. +
  4642. + if (existing_cunk == 0)
  4643. + in->n_data_chunks++;
  4644. +
  4645. + yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  4646. +
  4647. + return YAFFS_OK;
  4648. +}
  4649. +
  4650. +static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
  4651. +{
  4652. + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
  4653. +
  4654. + if (nand_chunk >= 0)
  4655. + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
  4656. + buffer, NULL);
  4657. + else {
  4658. + T(YAFFS_TRACE_NANDACCESS,
  4659. + (TSTR("Chunk %d not found zero instead" TENDSTR),
  4660. + nand_chunk));
  4661. + /* get sane (zero) data if you read a hole */
  4662. + memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
  4663. + return 0;
  4664. + }
  4665. +
  4666. +}
  4667. +
  4668. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  4669. + int lyn)
  4670. +{
  4671. + int block;
  4672. + int page;
  4673. + struct yaffs_ext_tags tags;
  4674. + struct yaffs_block_info *bi;
  4675. +
  4676. + if (chunk_id <= 0)
  4677. + return;
  4678. +
  4679. + dev->n_deletions++;
  4680. + block = chunk_id / dev->param.chunks_per_block;
  4681. + page = chunk_id % dev->param.chunks_per_block;
  4682. +
  4683. + if (!yaffs_check_chunk_bit(dev, block, page))
  4684. + T(YAFFS_TRACE_VERIFY,
  4685. + (TSTR("Deleting invalid chunk %d" TENDSTR), chunk_id));
  4686. +
  4687. + bi = yaffs_get_block_info(dev, block);
  4688. +
  4689. + yaffs2_update_oldest_dirty_seq(dev, block, bi);
  4690. +
  4691. + T(YAFFS_TRACE_DELETION,
  4692. + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
  4693. +
  4694. + if (!dev->param.is_yaffs2 && mark_flash &&
  4695. + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
  4696. +
  4697. + yaffs_init_tags(&tags);
  4698. +
  4699. + tags.is_deleted = 1;
  4700. +
  4701. + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
  4702. + yaffs_handle_chunk_update(dev, chunk_id, &tags);
  4703. + } else {
  4704. + dev->n_unmarked_deletions++;
  4705. + }
  4706. +
  4707. + /* Pull out of the management area.
  4708. + * If the whole block became dirty, this will kick off an erasure.
  4709. + */
  4710. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
  4711. + bi->block_state == YAFFS_BLOCK_STATE_FULL ||
  4712. + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  4713. + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4714. + dev->n_free_chunks++;
  4715. +
  4716. + yaffs_clear_chunk_bit(dev, block, page);
  4717. +
  4718. + bi->pages_in_use--;
  4719. +
  4720. + if (bi->pages_in_use == 0 &&
  4721. + !bi->has_shrink_hdr &&
  4722. + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
  4723. + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  4724. + yaffs_block_became_dirty(dev, block);
  4725. + }
  4726. +
  4727. + }
  4728. +
  4729. +}
  4730. +
  4731. +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  4732. + const u8 * buffer, int n_bytes, int use_reserve)
  4733. +{
  4734. + /* Find old chunk Need to do this to get serial number
  4735. + * Write new one and patch into tree.
  4736. + * Invalidate old tags.
  4737. + */
  4738. +
  4739. + int prev_chunk_id;
  4740. + struct yaffs_ext_tags prev_tags;
  4741. +
  4742. + int new_chunk_id;
  4743. + struct yaffs_ext_tags new_tags;
  4744. +
  4745. + struct yaffs_dev *dev = in->my_dev;
  4746. +
  4747. + yaffs_check_gc(dev, 0);
  4748. +
  4749. + /* Get the previous chunk at this location in the file if it exists.
  4750. + * If it does not exist then put a zero into the tree. This creates
  4751. + * the tnode now, rather than later when it is harder to clean up.
  4752. + */
  4753. + prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
  4754. + if (prev_chunk_id < 1 &&
  4755. + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
  4756. + return 0;
  4757. +
  4758. + /* Set up new tags */
  4759. + yaffs_init_tags(&new_tags);
  4760. +
  4761. + new_tags.chunk_id = inode_chunk;
  4762. + new_tags.obj_id = in->obj_id;
  4763. + new_tags.serial_number =
  4764. + (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
  4765. + new_tags.n_bytes = n_bytes;
  4766. +
  4767. + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
  4768. + T(YAFFS_TRACE_ERROR,
  4769. + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR),
  4770. + n_bytes));
  4771. + YBUG();
  4772. + }
  4773. +
  4774. + new_chunk_id =
  4775. + yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
  4776. +
  4777. + if (new_chunk_id > 0) {
  4778. + yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
  4779. +
  4780. + if (prev_chunk_id > 0)
  4781. + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  4782. +
  4783. + yaffs_verify_file_sane(in);
  4784. + }
  4785. + return new_chunk_id;
  4786. +
  4787. +}
  4788. +
  4789. +/* UpdateObjectHeader updates the header on NAND for an object.
  4790. + * If name is not NULL, then that new name is used.
  4791. + */
  4792. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
  4793. + int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
  4794. +{
  4795. +
  4796. + struct yaffs_block_info *bi;
  4797. +
  4798. + struct yaffs_dev *dev = in->my_dev;
  4799. +
  4800. + int prev_chunk_id;
  4801. + int ret_val = 0;
  4802. + int result = 0;
  4803. +
  4804. + int new_chunk_id;
  4805. + struct yaffs_ext_tags new_tags;
  4806. + struct yaffs_ext_tags old_tags;
  4807. + const YCHAR *alias = NULL;
  4808. +
  4809. + u8 *buffer = NULL;
  4810. + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
  4811. +
  4812. + struct yaffs_obj_hdr *oh = NULL;
  4813. +
  4814. + yaffs_strcpy(old_name, _Y("silly old name"));
  4815. +
  4816. + if (!in->fake || in == dev->root_dir || /* The root_dir should also be saved */
  4817. + force || xmod) {
  4818. +
  4819. + yaffs_check_gc(dev, 0);
  4820. + yaffs_check_obj_details_loaded(in);
  4821. +
  4822. + buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
  4823. + oh = (struct yaffs_obj_hdr *)buffer;
  4824. +
  4825. + prev_chunk_id = in->hdr_chunk;
  4826. +
  4827. + if (prev_chunk_id > 0) {
  4828. + result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
  4829. + buffer, &old_tags);
  4830. +
  4831. + yaffs_verify_oh(in, oh, &old_tags, 0);
  4832. +
  4833. + memcpy(old_name, oh->name, sizeof(oh->name));
  4834. + memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
  4835. + } else {
  4836. + memset(buffer, 0xFF, dev->data_bytes_per_chunk);
  4837. + }
  4838. +
  4839. + oh->type = in->variant_type;
  4840. + oh->yst_mode = in->yst_mode;
  4841. + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
  4842. +
  4843. + yaffs_load_attribs_oh(oh, in);
  4844. +
  4845. + if (in->parent)
  4846. + oh->parent_obj_id = in->parent->obj_id;
  4847. + else
  4848. + oh->parent_obj_id = 0;
  4849. +
  4850. + if (name && *name) {
  4851. + memset(oh->name, 0, sizeof(oh->name));
  4852. + yaffs_load_oh_from_name(dev, oh->name, name);
  4853. + } else if (prev_chunk_id > 0) {
  4854. + memcpy(oh->name, old_name, sizeof(oh->name));
  4855. + } else {
  4856. + memset(oh->name, 0, sizeof(oh->name));
  4857. + }
  4858. +
  4859. + oh->is_shrink = is_shrink;
  4860. +
  4861. + switch (in->variant_type) {
  4862. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  4863. + /* Should not happen */
  4864. + break;
  4865. + case YAFFS_OBJECT_TYPE_FILE:
  4866. + oh->file_size =
  4867. + (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
  4868. + || oh->parent_obj_id ==
  4869. + YAFFS_OBJECTID_UNLINKED) ? 0 : in->
  4870. + variant.file_variant.file_size;
  4871. + break;
  4872. + case YAFFS_OBJECT_TYPE_HARDLINK:
  4873. + oh->equiv_id = in->variant.hardlink_variant.equiv_id;
  4874. + break;
  4875. + case YAFFS_OBJECT_TYPE_SPECIAL:
  4876. + /* Do nothing */
  4877. + break;
  4878. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  4879. + /* Do nothing */
  4880. + break;
  4881. + case YAFFS_OBJECT_TYPE_SYMLINK:
  4882. + alias = in->variant.symlink_variant.alias;
  4883. + if (!alias)
  4884. + alias = _Y("no alias");
  4885. + yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
  4886. + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  4887. + break;
  4888. + }
  4889. +
  4890. + /* process any xattrib modifications */
  4891. + if (xmod)
  4892. + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
  4893. +
  4894. + /* Tags */
  4895. + yaffs_init_tags(&new_tags);
  4896. + in->serial++;
  4897. + new_tags.chunk_id = 0;
  4898. + new_tags.obj_id = in->obj_id;
  4899. + new_tags.serial_number = in->serial;
  4900. +
  4901. + /* Add extra info for file header */
  4902. +
  4903. + new_tags.extra_available = 1;
  4904. + new_tags.extra_parent_id = oh->parent_obj_id;
  4905. + new_tags.extra_length = oh->file_size;
  4906. + new_tags.extra_is_shrink = oh->is_shrink;
  4907. + new_tags.extra_equiv_id = oh->equiv_id;
  4908. + new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
  4909. + new_tags.extra_obj_type = in->variant_type;
  4910. +
  4911. + yaffs_verify_oh(in, oh, &new_tags, 1);
  4912. +
  4913. + /* Create new chunk in NAND */
  4914. + new_chunk_id =
  4915. + yaffs_write_new_chunk(dev, buffer, &new_tags,
  4916. + (prev_chunk_id > 0) ? 1 : 0);
  4917. +
  4918. + if (new_chunk_id >= 0) {
  4919. +
  4920. + in->hdr_chunk = new_chunk_id;
  4921. +
  4922. + if (prev_chunk_id > 0) {
  4923. + yaffs_chunk_del(dev, prev_chunk_id, 1,
  4924. + __LINE__);
  4925. + }
  4926. +
  4927. + if (!yaffs_obj_cache_dirty(in))
  4928. + in->dirty = 0;
  4929. +
  4930. + /* If this was a shrink, then mark the block that the chunk lives on */
  4931. + if (is_shrink) {
  4932. + bi = yaffs_get_block_info(in->my_dev,
  4933. + new_chunk_id /
  4934. + in->my_dev->param.
  4935. + chunks_per_block);
  4936. + bi->has_shrink_hdr = 1;
  4937. + }
  4938. +
  4939. + }
  4940. +
  4941. + ret_val = new_chunk_id;
  4942. +
  4943. + }
  4944. +
  4945. + if (buffer)
  4946. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  4947. +
  4948. + return ret_val;
  4949. +}
  4950. +
  4951. +/*------------------------ Short Operations Cache ----------------------------------------
  4952. + * In many situations where there is no high level buffering a lot of
  4953. + * reads might be short sequential reads, and a lot of writes may be short
  4954. + * sequential writes. eg. scanning/writing a jpeg file.
  4955. + * In these cases, a short read/write cache can provide a huge perfomance
  4956. + * benefit with dumb-as-a-rock code.
  4957. + * In Linux, the page cache provides read buffering and the short op cache
  4958. + * provides write buffering.
  4959. + *
  4960. + * There are a limited number (~10) of cache chunks per device so that we don't
  4961. + * need a very intelligent search.
  4962. + */
  4963. +
  4964. +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  4965. +{
  4966. + struct yaffs_dev *dev = obj->my_dev;
  4967. + int i;
  4968. + struct yaffs_cache *cache;
  4969. + int n_caches = obj->my_dev->param.n_caches;
  4970. +
  4971. + for (i = 0; i < n_caches; i++) {
  4972. + cache = &dev->cache[i];
  4973. + if (cache->object == obj && cache->dirty)
  4974. + return 1;
  4975. + }
  4976. +
  4977. + return 0;
  4978. +}
  4979. +
  4980. +static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  4981. +{
  4982. + struct yaffs_dev *dev = obj->my_dev;
  4983. + int lowest = -99; /* Stop compiler whining. */
  4984. + int i;
  4985. + struct yaffs_cache *cache;
  4986. + int chunk_written = 0;
  4987. + int n_caches = obj->my_dev->param.n_caches;
  4988. +
  4989. + if (n_caches > 0) {
  4990. + do {
  4991. + cache = NULL;
  4992. +
  4993. + /* Find the dirty cache for this object with the lowest chunk id. */
  4994. + for (i = 0; i < n_caches; i++) {
  4995. + if (dev->cache[i].object == obj &&
  4996. + dev->cache[i].dirty) {
  4997. + if (!cache
  4998. + || dev->cache[i].chunk_id <
  4999. + lowest) {
  5000. + cache = &dev->cache[i];
  5001. + lowest = cache->chunk_id;
  5002. + }
  5003. + }
  5004. + }
  5005. +
  5006. + if (cache && !cache->locked) {
  5007. + /* Write it out and free it up */
  5008. +
  5009. + chunk_written =
  5010. + yaffs_wr_data_obj(cache->object,
  5011. + cache->chunk_id,
  5012. + cache->data,
  5013. + cache->n_bytes, 1);
  5014. + cache->dirty = 0;
  5015. + cache->object = NULL;
  5016. + }
  5017. +
  5018. + } while (cache && chunk_written > 0);
  5019. +
  5020. + if (cache) {
  5021. + /* Hoosterman, disk full while writing cache out. */
  5022. + T(YAFFS_TRACE_ERROR,
  5023. + (TSTR
  5024. + ("yaffs tragedy: no space during cache write"
  5025. + TENDSTR)));
  5026. +
  5027. + }
  5028. + }
  5029. +
  5030. +}
  5031. +
  5032. +/*yaffs_flush_whole_cache(dev)
  5033. + *
  5034. + *
  5035. + */
  5036. +
  5037. +void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  5038. +{
  5039. + struct yaffs_obj *obj;
  5040. + int n_caches = dev->param.n_caches;
  5041. + int i;
  5042. +
  5043. + /* Find a dirty object in the cache and flush it...
  5044. + * until there are no further dirty objects.
  5045. + */
  5046. + do {
  5047. + obj = NULL;
  5048. + for (i = 0; i < n_caches && !obj; i++) {
  5049. + if (dev->cache[i].object && dev->cache[i].dirty)
  5050. + obj = dev->cache[i].object;
  5051. +
  5052. + }
  5053. + if (obj)
  5054. + yaffs_flush_file_cache(obj);
  5055. +
  5056. + } while (obj);
  5057. +
  5058. +}
  5059. +
  5060. +/* Grab us a cache chunk for use.
  5061. + * First look for an empty one.
  5062. + * Then look for the least recently used non-dirty one.
  5063. + * Then look for the least recently used dirty one...., flush and look again.
  5064. + */
  5065. +static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  5066. +{
  5067. + int i;
  5068. +
  5069. + if (dev->param.n_caches > 0) {
  5070. + for (i = 0; i < dev->param.n_caches; i++) {
  5071. + if (!dev->cache[i].object)
  5072. + return &dev->cache[i];
  5073. + }
  5074. + }
  5075. +
  5076. + return NULL;
  5077. +}
  5078. +
  5079. +static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  5080. +{
  5081. + struct yaffs_cache *cache;
  5082. + struct yaffs_obj *the_obj;
  5083. + int usage;
  5084. + int i;
  5085. + int pushout;
  5086. +
  5087. + if (dev->param.n_caches > 0) {
  5088. + /* Try find a non-dirty one... */
  5089. +
  5090. + cache = yaffs_grab_chunk_worker(dev);
  5091. +
  5092. + if (!cache) {
  5093. + /* They were all dirty, find the last recently used object and flush
  5094. + * its cache, then find again.
  5095. + * NB what's here is not very accurate, we actually flush the object
  5096. + * the last recently used page.
  5097. + */
  5098. +
  5099. + /* With locking we can't assume we can use entry zero */
  5100. +
  5101. + the_obj = NULL;
  5102. + usage = -1;
  5103. + cache = NULL;
  5104. + pushout = -1;
  5105. +
  5106. + for (i = 0; i < dev->param.n_caches; i++) {
  5107. + if (dev->cache[i].object &&
  5108. + !dev->cache[i].locked &&
  5109. + (dev->cache[i].last_use < usage
  5110. + || !cache)) {
  5111. + usage = dev->cache[i].last_use;
  5112. + the_obj = dev->cache[i].object;
  5113. + cache = &dev->cache[i];
  5114. + pushout = i;
  5115. + }
  5116. + }
  5117. +
  5118. + if (!cache || cache->dirty) {
  5119. + /* Flush and try again */
  5120. + yaffs_flush_file_cache(the_obj);
  5121. + cache = yaffs_grab_chunk_worker(dev);
  5122. + }
  5123. +
  5124. + }
  5125. + return cache;
  5126. + } else {
  5127. + return NULL;
  5128. + }
  5129. +}
  5130. +
  5131. +/* Find a cached chunk */
  5132. +static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  5133. + int chunk_id)
  5134. +{
  5135. + struct yaffs_dev *dev = obj->my_dev;
  5136. + int i;
  5137. + if (dev->param.n_caches > 0) {
  5138. + for (i = 0; i < dev->param.n_caches; i++) {
  5139. + if (dev->cache[i].object == obj &&
  5140. + dev->cache[i].chunk_id == chunk_id) {
  5141. + dev->cache_hits++;
  5142. +
  5143. + return &dev->cache[i];
  5144. + }
  5145. + }
  5146. + }
  5147. + return NULL;
  5148. +}
  5149. +
  5150. +/* Mark the chunk for the least recently used algorithym */
  5151. +static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  5152. + int is_write)
  5153. +{
  5154. +
  5155. + if (dev->param.n_caches > 0) {
  5156. + if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
  5157. + /* Reset the cache usages */
  5158. + int i;
  5159. + for (i = 1; i < dev->param.n_caches; i++)
  5160. + dev->cache[i].last_use = 0;
  5161. +
  5162. + dev->cache_last_use = 0;
  5163. + }
  5164. +
  5165. + dev->cache_last_use++;
  5166. +
  5167. + cache->last_use = dev->cache_last_use;
  5168. +
  5169. + if (is_write)
  5170. + cache->dirty = 1;
  5171. + }
  5172. +}
  5173. +
  5174. +/* Invalidate a single cache page.
  5175. + * Do this when a whole page gets written,
  5176. + * ie the short cache for this page is no longer valid.
  5177. + */
  5178. +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  5179. +{
  5180. + if (object->my_dev->param.n_caches > 0) {
  5181. + struct yaffs_cache *cache =
  5182. + yaffs_find_chunk_cache(object, chunk_id);
  5183. +
  5184. + if (cache)
  5185. + cache->object = NULL;
  5186. + }
  5187. +}
  5188. +
  5189. +/* Invalidate all the cache pages associated with this object
  5190. + * Do this whenever ther file is deleted or resized.
  5191. + */
  5192. +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  5193. +{
  5194. + int i;
  5195. + struct yaffs_dev *dev = in->my_dev;
  5196. +
  5197. + if (dev->param.n_caches > 0) {
  5198. + /* Invalidate it. */
  5199. + for (i = 0; i < dev->param.n_caches; i++) {
  5200. + if (dev->cache[i].object == in)
  5201. + dev->cache[i].object = NULL;
  5202. + }
  5203. + }
  5204. +}
  5205. +
  5206. +/*--------------------- File read/write ------------------------
  5207. + * Read and write have very similar structures.
  5208. + * In general the read/write has three parts to it
  5209. + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  5210. + * Some complete chunks
  5211. + * An incomplete chunk to end off with
  5212. + *
  5213. + * Curve-balls: the first chunk might also be the last chunk.
  5214. + */
  5215. +
  5216. +int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
  5217. +{
  5218. +
  5219. + int chunk;
  5220. + u32 start;
  5221. + int n_copy;
  5222. + int n = n_bytes;
  5223. + int n_done = 0;
  5224. + struct yaffs_cache *cache;
  5225. +
  5226. + struct yaffs_dev *dev;
  5227. +
  5228. + dev = in->my_dev;
  5229. +
  5230. + while (n > 0) {
  5231. + /* chunk = offset / dev->data_bytes_per_chunk + 1; */
  5232. + /* start = offset % dev->data_bytes_per_chunk; */
  5233. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5234. + chunk++;
  5235. +
  5236. + /* OK now check for the curveball where the start and end are in
  5237. + * the same chunk.
  5238. + */
  5239. + if ((start + n) < dev->data_bytes_per_chunk)
  5240. + n_copy = n;
  5241. + else
  5242. + n_copy = dev->data_bytes_per_chunk - start;
  5243. +
  5244. + cache = yaffs_find_chunk_cache(in, chunk);
  5245. +
  5246. + /* If the chunk is already in the cache or it is less than a whole chunk
  5247. + * or we're using inband tags then use the cache (if there is caching)
  5248. + * else bypass the cache.
  5249. + */
  5250. + if (cache || n_copy != dev->data_bytes_per_chunk
  5251. + || dev->param.inband_tags) {
  5252. + if (dev->param.n_caches > 0) {
  5253. +
  5254. + /* If we can't find the data in the cache, then load it up. */
  5255. +
  5256. + if (!cache) {
  5257. + cache =
  5258. + yaffs_grab_chunk_cache(in->my_dev);
  5259. + cache->object = in;
  5260. + cache->chunk_id = chunk;
  5261. + cache->dirty = 0;
  5262. + cache->locked = 0;
  5263. + yaffs_rd_data_obj(in, chunk,
  5264. + cache->data);
  5265. + cache->n_bytes = 0;
  5266. + }
  5267. +
  5268. + yaffs_use_cache(dev, cache, 0);
  5269. +
  5270. + cache->locked = 1;
  5271. +
  5272. + memcpy(buffer, &cache->data[start], n_copy);
  5273. +
  5274. + cache->locked = 0;
  5275. + } else {
  5276. + /* Read into the local buffer then copy.. */
  5277. +
  5278. + u8 *local_buffer =
  5279. + yaffs_get_temp_buffer(dev, __LINE__);
  5280. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5281. +
  5282. + memcpy(buffer, &local_buffer[start], n_copy);
  5283. +
  5284. + yaffs_release_temp_buffer(dev, local_buffer,
  5285. + __LINE__);
  5286. + }
  5287. +
  5288. + } else {
  5289. +
  5290. + /* A full chunk. Read directly into the supplied buffer. */
  5291. + yaffs_rd_data_obj(in, chunk, buffer);
  5292. +
  5293. + }
  5294. +
  5295. + n -= n_copy;
  5296. + offset += n_copy;
  5297. + buffer += n_copy;
  5298. + n_done += n_copy;
  5299. +
  5300. + }
  5301. +
  5302. + return n_done;
  5303. +}
  5304. +
  5305. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  5306. + int n_bytes, int write_trhrough)
  5307. +{
  5308. +
  5309. + int chunk;
  5310. + u32 start;
  5311. + int n_copy;
  5312. + int n = n_bytes;
  5313. + int n_done = 0;
  5314. + int n_writeback;
  5315. + int start_write = offset;
  5316. + int chunk_written = 0;
  5317. + u32 n_bytes_read;
  5318. + u32 chunk_start;
  5319. +
  5320. + struct yaffs_dev *dev;
  5321. +
  5322. + dev = in->my_dev;
  5323. +
  5324. + while (n > 0 && chunk_written >= 0) {
  5325. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5326. +
  5327. + if (chunk * dev->data_bytes_per_chunk + start != offset ||
  5328. + start >= dev->data_bytes_per_chunk) {
  5329. + T(YAFFS_TRACE_ERROR,
  5330. + (TSTR
  5331. + ("AddrToChunk of offset %d gives chunk %d start %d"
  5332. + TENDSTR), (int)offset, chunk, start));
  5333. + }
  5334. + chunk++; /* File pos to chunk in file offset */
  5335. +
  5336. + /* OK now check for the curveball where the start and end are in
  5337. + * the same chunk.
  5338. + */
  5339. +
  5340. + if ((start + n) < dev->data_bytes_per_chunk) {
  5341. + n_copy = n;
  5342. +
  5343. + /* Now folks, to calculate how many bytes to write back....
  5344. + * If we're overwriting and not writing to then end of file then
  5345. + * we need to write back as much as was there before.
  5346. + */
  5347. +
  5348. + chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
  5349. +
  5350. + if (chunk_start > in->variant.file_variant.file_size)
  5351. + n_bytes_read = 0; /* Past end of file */
  5352. + else
  5353. + n_bytes_read =
  5354. + in->variant.file_variant.file_size -
  5355. + chunk_start;
  5356. +
  5357. + if (n_bytes_read > dev->data_bytes_per_chunk)
  5358. + n_bytes_read = dev->data_bytes_per_chunk;
  5359. +
  5360. + n_writeback =
  5361. + (n_bytes_read >
  5362. + (start + n)) ? n_bytes_read : (start + n);
  5363. +
  5364. + if (n_writeback < 0
  5365. + || n_writeback > dev->data_bytes_per_chunk)
  5366. + YBUG();
  5367. +
  5368. + } else {
  5369. + n_copy = dev->data_bytes_per_chunk - start;
  5370. + n_writeback = dev->data_bytes_per_chunk;
  5371. + }
  5372. +
  5373. + if (n_copy != dev->data_bytes_per_chunk
  5374. + || dev->param.inband_tags) {
  5375. + /* An incomplete start or end chunk (or maybe both start and end chunk),
  5376. + * or we're using inband tags, so we want to use the cache buffers.
  5377. + */
  5378. + if (dev->param.n_caches > 0) {
  5379. + struct yaffs_cache *cache;
  5380. + /* If we can't find the data in the cache, then load the cache */
  5381. + cache = yaffs_find_chunk_cache(in, chunk);
  5382. +
  5383. + if (!cache
  5384. + && yaffs_check_alloc_available(dev, 1)) {
  5385. + cache = yaffs_grab_chunk_cache(dev);
  5386. + cache->object = in;
  5387. + cache->chunk_id = chunk;
  5388. + cache->dirty = 0;
  5389. + cache->locked = 0;
  5390. + yaffs_rd_data_obj(in, chunk,
  5391. + cache->data);
  5392. + } else if (cache &&
  5393. + !cache->dirty &&
  5394. + !yaffs_check_alloc_available(dev,
  5395. + 1)) {
  5396. + /* Drop the cache if it was a read cache item and
  5397. + * no space check has been made for it.
  5398. + */
  5399. + cache = NULL;
  5400. + }
  5401. +
  5402. + if (cache) {
  5403. + yaffs_use_cache(dev, cache, 1);
  5404. + cache->locked = 1;
  5405. +
  5406. + memcpy(&cache->data[start], buffer,
  5407. + n_copy);
  5408. +
  5409. + cache->locked = 0;
  5410. + cache->n_bytes = n_writeback;
  5411. +
  5412. + if (write_trhrough) {
  5413. + chunk_written =
  5414. + yaffs_wr_data_obj
  5415. + (cache->object,
  5416. + cache->chunk_id,
  5417. + cache->data,
  5418. + cache->n_bytes, 1);
  5419. + cache->dirty = 0;
  5420. + }
  5421. +
  5422. + } else {
  5423. + chunk_written = -1; /* fail the write */
  5424. + }
  5425. + } else {
  5426. + /* An incomplete start or end chunk (or maybe both start and end chunk)
  5427. + * Read into the local buffer then copy, then copy over and write back.
  5428. + */
  5429. +
  5430. + u8 *local_buffer =
  5431. + yaffs_get_temp_buffer(dev, __LINE__);
  5432. +
  5433. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5434. +
  5435. + memcpy(&local_buffer[start], buffer, n_copy);
  5436. +
  5437. + chunk_written =
  5438. + yaffs_wr_data_obj(in, chunk,
  5439. + local_buffer,
  5440. + n_writeback, 0);
  5441. +
  5442. + yaffs_release_temp_buffer(dev, local_buffer,
  5443. + __LINE__);
  5444. +
  5445. + }
  5446. +
  5447. + } else {
  5448. + /* A full chunk. Write directly from the supplied buffer. */
  5449. +
  5450. + chunk_written =
  5451. + yaffs_wr_data_obj(in, chunk, buffer,
  5452. + dev->data_bytes_per_chunk, 0);
  5453. +
  5454. + /* Since we've overwritten the cached data, we better invalidate it. */
  5455. + yaffs_invalidate_chunk_cache(in, chunk);
  5456. + }
  5457. +
  5458. + if (chunk_written >= 0) {
  5459. + n -= n_copy;
  5460. + offset += n_copy;
  5461. + buffer += n_copy;
  5462. + n_done += n_copy;
  5463. + }
  5464. +
  5465. + }
  5466. +
  5467. + /* Update file object */
  5468. +
  5469. + if ((start_write + n_done) > in->variant.file_variant.file_size)
  5470. + in->variant.file_variant.file_size = (start_write + n_done);
  5471. +
  5472. + in->dirty = 1;
  5473. +
  5474. + return n_done;
  5475. +}
  5476. +
  5477. +int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  5478. + int n_bytes, int write_trhrough)
  5479. +{
  5480. + yaffs2_handle_hole(in, offset);
  5481. + return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
  5482. +}
  5483. +
  5484. +/* ---------------------- File resizing stuff ------------------ */
  5485. +
  5486. +static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
  5487. +{
  5488. +
  5489. + struct yaffs_dev *dev = in->my_dev;
  5490. + int old_size = in->variant.file_variant.file_size;
  5491. +
  5492. + int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
  5493. +
  5494. + int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
  5495. + dev->data_bytes_per_chunk;
  5496. + int i;
  5497. + int chunk_id;
  5498. +
  5499. + /* Delete backwards so that we don't end up with holes if
  5500. + * power is lost part-way through the operation.
  5501. + */
  5502. + for (i = last_del; i >= start_del; i--) {
  5503. + /* NB this could be optimised somewhat,
  5504. + * eg. could retrieve the tags and write them without
  5505. + * using yaffs_chunk_del
  5506. + */
  5507. +
  5508. + chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
  5509. + if (chunk_id > 0) {
  5510. + if (chunk_id <
  5511. + (dev->internal_start_block *
  5512. + dev->param.chunks_per_block)
  5513. + || chunk_id >=
  5514. + ((dev->internal_end_block +
  5515. + 1) * dev->param.chunks_per_block)) {
  5516. + T(YAFFS_TRACE_ALWAYS,
  5517. + (TSTR
  5518. + ("Found daft chunk_id %d for %d" TENDSTR),
  5519. + chunk_id, i));
  5520. + } else {
  5521. + in->n_data_chunks--;
  5522. + yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
  5523. + }
  5524. + }
  5525. + }
  5526. +
  5527. +}
  5528. +
  5529. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
  5530. +{
  5531. + int new_full;
  5532. + u32 new_partial;
  5533. + struct yaffs_dev *dev = obj->my_dev;
  5534. +
  5535. + yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
  5536. +
  5537. + yaffs_prune_chunks(obj, new_size);
  5538. +
  5539. + if (new_partial != 0) {
  5540. + int last_chunk = 1 + new_full;
  5541. + u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
  5542. +
  5543. + /* Got to read and rewrite the last chunk with its new size and zero pad */
  5544. + yaffs_rd_data_obj(obj, last_chunk, local_buffer);
  5545. + memset(local_buffer + new_partial, 0,
  5546. + dev->data_bytes_per_chunk - new_partial);
  5547. +
  5548. + yaffs_wr_data_obj(obj, last_chunk, local_buffer,
  5549. + new_partial, 1);
  5550. +
  5551. + yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
  5552. + }
  5553. +
  5554. + obj->variant.file_variant.file_size = new_size;
  5555. +
  5556. + yaffs_prune_tree(dev, &obj->variant.file_variant);
  5557. +}
  5558. +
  5559. +int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
  5560. +{
  5561. + struct yaffs_dev *dev = in->my_dev;
  5562. + int old_size = in->variant.file_variant.file_size;
  5563. +
  5564. + yaffs_flush_file_cache(in);
  5565. + yaffs_invalidate_whole_cache(in);
  5566. +
  5567. + yaffs_check_gc(dev, 0);
  5568. +
  5569. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
  5570. + return YAFFS_FAIL;
  5571. +
  5572. + if (new_size == old_size)
  5573. + return YAFFS_OK;
  5574. +
  5575. + if (new_size > old_size) {
  5576. + yaffs2_handle_hole(in, new_size);
  5577. + in->variant.file_variant.file_size = new_size;
  5578. + } else {
  5579. + /* new_size < old_size */
  5580. + yaffs_resize_file_down(in, new_size);
  5581. + }
  5582. +
  5583. + /* Write a new object header to reflect the resize.
  5584. + * show we've shrunk the file, if need be
  5585. + * Do this only if the file is not in the deleted directories
  5586. + * and is not shadowed.
  5587. + */
  5588. + if (in->parent &&
  5589. + !in->is_shadowed &&
  5590. + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  5591. + in->parent->obj_id != YAFFS_OBJECTID_DELETED)
  5592. + yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
  5593. +
  5594. + return YAFFS_OK;
  5595. +}
  5596. +
  5597. +int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
  5598. +{
  5599. + int ret_val;
  5600. + if (in->dirty) {
  5601. + yaffs_flush_file_cache(in);
  5602. + if (data_sync) /* Only sync data */
  5603. + ret_val = YAFFS_OK;
  5604. + else {
  5605. + if (update_time)
  5606. + yaffs_load_current_time(in, 0, 0);
  5607. +
  5608. + ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
  5609. + 0) ? YAFFS_OK : YAFFS_FAIL;
  5610. + }
  5611. + } else {
  5612. + ret_val = YAFFS_OK;
  5613. + }
  5614. +
  5615. + return ret_val;
  5616. +
  5617. +}
  5618. +
  5619. +static int yaffs_generic_obj_del(struct yaffs_obj *in)
  5620. +{
  5621. +
  5622. + /* First off, invalidate the file's data in the cache, without flushing. */
  5623. + yaffs_invalidate_whole_cache(in);
  5624. +
  5625. + if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
  5626. + /* Move to the unlinked directory so we have a record that it was deleted. */
  5627. + yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  5628. + 0);
  5629. +
  5630. + }
  5631. +
  5632. + yaffs_remove_obj_from_dir(in);
  5633. + yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  5634. + in->hdr_chunk = 0;
  5635. +
  5636. + yaffs_free_obj(in);
  5637. + return YAFFS_OK;
  5638. +
  5639. +}
  5640. +
  5641. +/* yaffs_del_file deletes the whole file data
  5642. + * and the inode associated with the file.
  5643. + * It does not delete the links associated with the file.
  5644. + */
  5645. +static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
  5646. +{
  5647. +
  5648. + int ret_val;
  5649. + int del_now = 0;
  5650. + struct yaffs_dev *dev = in->my_dev;
  5651. +
  5652. + if (!in->my_inode)
  5653. + del_now = 1;
  5654. +
  5655. + if (del_now) {
  5656. + ret_val =
  5657. + yaffs_change_obj_name(in, in->my_dev->del_dir,
  5658. + _Y("deleted"), 0, 0);
  5659. + T(YAFFS_TRACE_TRACING,
  5660. + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
  5661. + in->obj_id));
  5662. + in->deleted = 1;
  5663. + in->my_dev->n_deleted_files++;
  5664. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5665. + yaffs_resize_file(in, 0);
  5666. + yaffs_soft_del_file(in);
  5667. + } else {
  5668. + ret_val =
  5669. + yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
  5670. + _Y("unlinked"), 0, 0);
  5671. + }
  5672. +
  5673. + return ret_val;
  5674. +}
  5675. +
  5676. +int yaffs_del_file(struct yaffs_obj *in)
  5677. +{
  5678. + int ret_val = YAFFS_OK;
  5679. + int deleted; /* Need to cache value on stack if in is freed */
  5680. + struct yaffs_dev *dev = in->my_dev;
  5681. +
  5682. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5683. + yaffs_resize_file(in, 0);
  5684. +
  5685. + if (in->n_data_chunks > 0) {
  5686. + /* Use soft deletion if there is data in the file.
  5687. + * That won't be the case if it has been resized to zero.
  5688. + */
  5689. + if (!in->unlinked)
  5690. + ret_val = yaffs_unlink_file_if_needed(in);
  5691. +
  5692. + deleted = in->deleted;
  5693. +
  5694. + if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
  5695. + in->deleted = 1;
  5696. + deleted = 1;
  5697. + in->my_dev->n_deleted_files++;
  5698. + yaffs_soft_del_file(in);
  5699. + }
  5700. + return deleted ? YAFFS_OK : YAFFS_FAIL;
  5701. + } else {
  5702. + /* The file has no data chunks so we toss it immediately */
  5703. + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
  5704. + in->variant.file_variant.top = NULL;
  5705. + yaffs_generic_obj_del(in);
  5706. +
  5707. + return YAFFS_OK;
  5708. + }
  5709. +}
  5710. +
  5711. +static int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
  5712. +{
  5713. + return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
  5714. + !(list_empty(&obj->variant.dir_variant.children));
  5715. +}
  5716. +
  5717. +static int yaffs_del_dir(struct yaffs_obj *obj)
  5718. +{
  5719. + /* First check that the directory is empty. */
  5720. + if (yaffs_is_non_empty_dir(obj))
  5721. + return YAFFS_FAIL;
  5722. +
  5723. + return yaffs_generic_obj_del(obj);
  5724. +}
  5725. +
  5726. +static int yaffs_del_symlink(struct yaffs_obj *in)
  5727. +{
  5728. + if (in->variant.symlink_variant.alias)
  5729. + YFREE(in->variant.symlink_variant.alias);
  5730. + in->variant.symlink_variant.alias = NULL;
  5731. +
  5732. + return yaffs_generic_obj_del(in);
  5733. +}
  5734. +
  5735. +static int yaffs_del_link(struct yaffs_obj *in)
  5736. +{
  5737. + /* remove this hardlink from the list assocaited with the equivalent
  5738. + * object
  5739. + */
  5740. + list_del_init(&in->hard_links);
  5741. + return yaffs_generic_obj_del(in);
  5742. +}
  5743. +
  5744. +int yaffs_del_obj(struct yaffs_obj *obj)
  5745. +{
  5746. + int ret_val = -1;
  5747. + switch (obj->variant_type) {
  5748. + case YAFFS_OBJECT_TYPE_FILE:
  5749. + ret_val = yaffs_del_file(obj);
  5750. + break;
  5751. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5752. + if (!list_empty(&obj->variant.dir_variant.dirty)) {
  5753. + T(YAFFS_TRACE_BACKGROUND,
  5754. + (TSTR
  5755. + ("Remove object %d from dirty directories" TENDSTR),
  5756. + obj->obj_id));
  5757. + list_del_init(&obj->variant.dir_variant.dirty);
  5758. + }
  5759. + return yaffs_del_dir(obj);
  5760. + break;
  5761. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5762. + ret_val = yaffs_del_symlink(obj);
  5763. + break;
  5764. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5765. + ret_val = yaffs_del_link(obj);
  5766. + break;
  5767. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5768. + ret_val = yaffs_generic_obj_del(obj);
  5769. + break;
  5770. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5771. + ret_val = 0;
  5772. + break; /* should not happen. */
  5773. + }
  5774. +
  5775. + return ret_val;
  5776. +}
  5777. +
  5778. +static int yaffs_unlink_worker(struct yaffs_obj *obj)
  5779. +{
  5780. +
  5781. + int del_now = 0;
  5782. +
  5783. + if (!obj->my_inode)
  5784. + del_now = 1;
  5785. +
  5786. + if (obj)
  5787. + yaffs_update_parent(obj->parent);
  5788. +
  5789. + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  5790. + return yaffs_del_link(obj);
  5791. + } else if (!list_empty(&obj->hard_links)) {
  5792. + /* Curve ball: We're unlinking an object that has a hardlink.
  5793. + *
  5794. + * This problem arises because we are not strictly following
  5795. + * The Linux link/inode model.
  5796. + *
  5797. + * We can't really delete the object.
  5798. + * Instead, we do the following:
  5799. + * - Select a hardlink.
  5800. + * - Unhook it from the hard links
  5801. + * - Move it from its parent directory (so that the rename can work)
  5802. + * - Rename the object to the hardlink's name.
  5803. + * - Delete the hardlink
  5804. + */
  5805. +
  5806. + struct yaffs_obj *hl;
  5807. + struct yaffs_obj *parent;
  5808. + int ret_val;
  5809. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  5810. +
  5811. + hl = list_entry(obj->hard_links.next, struct yaffs_obj,
  5812. + hard_links);
  5813. +
  5814. + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  5815. + parent = hl->parent;
  5816. +
  5817. + list_del_init(&hl->hard_links);
  5818. +
  5819. + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
  5820. +
  5821. + ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
  5822. +
  5823. + if (ret_val == YAFFS_OK)
  5824. + ret_val = yaffs_generic_obj_del(hl);
  5825. +
  5826. + return ret_val;
  5827. +
  5828. + } else if (del_now) {
  5829. + switch (obj->variant_type) {
  5830. + case YAFFS_OBJECT_TYPE_FILE:
  5831. + return yaffs_del_file(obj);
  5832. + break;
  5833. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5834. + list_del_init(&obj->variant.dir_variant.dirty);
  5835. + return yaffs_del_dir(obj);
  5836. + break;
  5837. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5838. + return yaffs_del_symlink(obj);
  5839. + break;
  5840. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5841. + return yaffs_generic_obj_del(obj);
  5842. + break;
  5843. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5844. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5845. + default:
  5846. + return YAFFS_FAIL;
  5847. + }
  5848. + } else if (yaffs_is_non_empty_dir(obj)) {
  5849. + return YAFFS_FAIL;
  5850. + } else {
  5851. + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
  5852. + _Y("unlinked"), 0, 0);
  5853. + }
  5854. +}
  5855. +
  5856. +static int yaffs_unlink_obj(struct yaffs_obj *obj)
  5857. +{
  5858. +
  5859. + if (obj && obj->unlink_allowed)
  5860. + return yaffs_unlink_worker(obj);
  5861. +
  5862. + return YAFFS_FAIL;
  5863. +
  5864. +}
  5865. +
  5866. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
  5867. +{
  5868. + struct yaffs_obj *obj;
  5869. +
  5870. + obj = yaffs_find_by_name(dir, name);
  5871. + return yaffs_unlink_obj(obj);
  5872. +}
  5873. +
  5874. +/*----------------------- Initialisation Scanning ---------------------- */
  5875. +
  5876. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  5877. + int backward_scanning)
  5878. +{
  5879. + struct yaffs_obj *obj;
  5880. +
  5881. + if (!backward_scanning) {
  5882. + /* Handle YAFFS1 forward scanning case
  5883. + * For YAFFS1 we always do the deletion
  5884. + */
  5885. +
  5886. + } else {
  5887. + /* Handle YAFFS2 case (backward scanning)
  5888. + * If the shadowed object exists then ignore.
  5889. + */
  5890. + obj = yaffs_find_by_number(dev, obj_id);
  5891. + if (obj)
  5892. + return;
  5893. + }
  5894. +
  5895. + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  5896. + * We put it in unlinked dir to be cleaned up after the scanning
  5897. + */
  5898. + obj =
  5899. + yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
  5900. + if (!obj)
  5901. + return;
  5902. + obj->is_shadowed = 1;
  5903. + yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
  5904. + obj->variant.file_variant.shrink_size = 0;
  5905. + obj->valid = 1; /* So that we don't read any other info for this file */
  5906. +
  5907. +}
  5908. +
  5909. +void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
  5910. +{
  5911. + struct yaffs_obj *hl;
  5912. + struct yaffs_obj *in;
  5913. +
  5914. + while (hard_list) {
  5915. + hl = hard_list;
  5916. + hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
  5917. +
  5918. + in = yaffs_find_by_number(dev,
  5919. + hl->variant.
  5920. + hardlink_variant.equiv_id);
  5921. +
  5922. + if (in) {
  5923. + /* Add the hardlink pointers */
  5924. + hl->variant.hardlink_variant.equiv_obj = in;
  5925. + list_add(&hl->hard_links, &in->hard_links);
  5926. + } else {
  5927. + /* Todo Need to report/handle this better.
  5928. + * Got a problem... hardlink to a non-existant object
  5929. + */
  5930. + hl->variant.hardlink_variant.equiv_obj = NULL;
  5931. + INIT_LIST_HEAD(&hl->hard_links);
  5932. +
  5933. + }
  5934. + }
  5935. +}
  5936. +
  5937. +static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
  5938. +{
  5939. + /*
  5940. + * Sort out state of unlinked and deleted objects after scanning.
  5941. + */
  5942. + struct list_head *i;
  5943. + struct list_head *n;
  5944. + struct yaffs_obj *l;
  5945. +
  5946. + if (dev->read_only)
  5947. + return;
  5948. +
  5949. + /* Soft delete all the unlinked files */
  5950. + list_for_each_safe(i, n,
  5951. + &dev->unlinked_dir->variant.dir_variant.children) {
  5952. + if (i) {
  5953. + l = list_entry(i, struct yaffs_obj, siblings);
  5954. + yaffs_del_obj(l);
  5955. + }
  5956. + }
  5957. +
  5958. + list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
  5959. + if (i) {
  5960. + l = list_entry(i, struct yaffs_obj, siblings);
  5961. + yaffs_del_obj(l);
  5962. + }
  5963. + }
  5964. +
  5965. +}
  5966. +
  5967. +/*
  5968. + * This code iterates through all the objects making sure that they are rooted.
  5969. + * Any unrooted objects are re-rooted in lost+found.
  5970. + * An object needs to be in one of:
  5971. + * - Directly under deleted, unlinked
  5972. + * - Directly or indirectly under root.
  5973. + *
  5974. + * Note:
  5975. + * This code assumes that we don't ever change the current relationships between
  5976. + * directories:
  5977. + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
  5978. + * lost-n-found->parent == root_dir
  5979. + *
  5980. + * This fixes the problem where directories might have inadvertently been deleted
  5981. + * leaving the object "hanging" without being rooted in the directory tree.
  5982. + */
  5983. +
  5984. +static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
  5985. +{
  5986. + return (obj == dev->del_dir ||
  5987. + obj == dev->unlinked_dir || obj == dev->root_dir);
  5988. +}
  5989. +
  5990. +static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
  5991. +{
  5992. + struct yaffs_obj *obj;
  5993. + struct yaffs_obj *parent;
  5994. + int i;
  5995. + struct list_head *lh;
  5996. + struct list_head *n;
  5997. + int depth_limit;
  5998. + int hanging;
  5999. +
  6000. + if (dev->read_only)
  6001. + return;
  6002. +
  6003. + /* Iterate through the objects in each hash entry,
  6004. + * looking at each object.
  6005. + * Make sure it is rooted.
  6006. + */
  6007. +
  6008. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  6009. + list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
  6010. + if (lh) {
  6011. + obj =
  6012. + list_entry(lh, struct yaffs_obj, hash_link);
  6013. + parent = obj->parent;
  6014. +
  6015. + if (yaffs_has_null_parent(dev, obj)) {
  6016. + /* These directories are not hanging */
  6017. + hanging = 0;
  6018. + } else if (!parent
  6019. + || parent->variant_type !=
  6020. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  6021. + hanging = 1;
  6022. + } else if (yaffs_has_null_parent(dev, parent)) {
  6023. + hanging = 0;
  6024. + } else {
  6025. + /*
  6026. + * Need to follow the parent chain to see if it is hanging.
  6027. + */
  6028. + hanging = 0;
  6029. + depth_limit = 100;
  6030. +
  6031. + while (parent != dev->root_dir &&
  6032. + parent->parent &&
  6033. + parent->parent->variant_type ==
  6034. + YAFFS_OBJECT_TYPE_DIRECTORY
  6035. + && depth_limit > 0) {
  6036. + parent = parent->parent;
  6037. + depth_limit--;
  6038. + }
  6039. + if (parent != dev->root_dir)
  6040. + hanging = 1;
  6041. + }
  6042. + if (hanging) {
  6043. + T(YAFFS_TRACE_SCAN,
  6044. + (TSTR
  6045. + ("Hanging object %d moved to lost and found"
  6046. + TENDSTR), obj->obj_id));
  6047. + yaffs_add_obj_to_dir(dev->lost_n_found,
  6048. + obj);
  6049. + }
  6050. + }
  6051. + }
  6052. + }
  6053. +}
  6054. +
  6055. +/*
  6056. + * Delete directory contents for cleaning up lost and found.
  6057. + */
  6058. +static void yaffs_del_dir_contents(struct yaffs_obj *dir)
  6059. +{
  6060. + struct yaffs_obj *obj;
  6061. + struct list_head *lh;
  6062. + struct list_head *n;
  6063. +
  6064. + if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  6065. + YBUG();
  6066. +
  6067. + list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
  6068. + if (lh) {
  6069. + obj = list_entry(lh, struct yaffs_obj, siblings);
  6070. + if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  6071. + yaffs_del_dir_contents(obj);
  6072. +
  6073. + T(YAFFS_TRACE_SCAN,
  6074. + (TSTR("Deleting lost_found object %d" TENDSTR),
  6075. + obj->obj_id));
  6076. +
  6077. + /* Need to use UnlinkObject since Delete would not handle
  6078. + * hardlinked objects correctly.
  6079. + */
  6080. + yaffs_unlink_obj(obj);
  6081. + }
  6082. + }
  6083. +
  6084. +}
  6085. +
  6086. +static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
  6087. +{
  6088. + yaffs_del_dir_contents(dev->lost_n_found);
  6089. +}
  6090. +
  6091. +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
  6092. +{
  6093. + u8 *chunk_data;
  6094. + struct yaffs_obj_hdr *oh;
  6095. + struct yaffs_dev *dev;
  6096. + struct yaffs_ext_tags tags;
  6097. + int result;
  6098. + int alloc_failed = 0;
  6099. +
  6100. + if (!in)
  6101. + return;
  6102. +
  6103. + dev = in->my_dev;
  6104. +
  6105. + if (in->lazy_loaded && in->hdr_chunk > 0) {
  6106. + in->lazy_loaded = 0;
  6107. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  6108. +
  6109. + result =
  6110. + yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
  6111. + &tags);
  6112. + oh = (struct yaffs_obj_hdr *)chunk_data;
  6113. +
  6114. + in->yst_mode = oh->yst_mode;
  6115. + yaffs_load_attribs(in, oh);
  6116. + yaffs_set_obj_name_from_oh(in, oh);
  6117. +
  6118. + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6119. + in->variant.symlink_variant.alias =
  6120. + yaffs_clone_str(oh->alias);
  6121. + if (!in->variant.symlink_variant.alias)
  6122. + alloc_failed = 1; /* Not returned to caller */
  6123. + }
  6124. +
  6125. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  6126. + }
  6127. +}
  6128. +
  6129. +/*------------------------------ Directory Functions ----------------------------- */
  6130. +
  6131. +/*
  6132. + *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
  6133. + * link (ie. name) is created or deleted in the directory.
  6134. + *
  6135. + * ie.
  6136. + * create dir/a : update dir's mtime/ctime
  6137. + * rm dir/a: update dir's mtime/ctime
  6138. + * modify dir/a: don't update dir's mtimme/ctime
  6139. + *
  6140. + * This can be handled immediately or defered. Defering helps reduce the number
  6141. + * of updates when many files in a directory are changed within a brief period.
  6142. + *
  6143. + * If the directory updating is defered then yaffs_update_dirty_dirs must be
  6144. + * called periodically.
  6145. + */
  6146. +
  6147. +static void yaffs_update_parent(struct yaffs_obj *obj)
  6148. +{
  6149. + struct yaffs_dev *dev;
  6150. + if (!obj)
  6151. + return;
  6152. + dev = obj->my_dev;
  6153. + obj->dirty = 1;
  6154. + yaffs_load_current_time(obj, 0, 1);
  6155. + if (dev->param.defered_dir_update) {
  6156. + struct list_head *link = &obj->variant.dir_variant.dirty;
  6157. +
  6158. + if (list_empty(link)) {
  6159. + list_add(link, &dev->dirty_dirs);
  6160. + T(YAFFS_TRACE_BACKGROUND,
  6161. + (TSTR("Added object %d to dirty directories" TENDSTR),
  6162. + obj->obj_id));
  6163. + }
  6164. +
  6165. + } else {
  6166. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  6167. + }
  6168. +}
  6169. +
  6170. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  6171. +{
  6172. + struct list_head *link;
  6173. + struct yaffs_obj *obj;
  6174. + struct yaffs_dir_var *d_s;
  6175. + union yaffs_obj_var *o_v;
  6176. +
  6177. + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
  6178. +
  6179. + while (!list_empty(&dev->dirty_dirs)) {
  6180. + link = dev->dirty_dirs.next;
  6181. + list_del_init(link);
  6182. +
  6183. + d_s = list_entry(link, struct yaffs_dir_var, dirty);
  6184. + o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  6185. + obj = list_entry(o_v, struct yaffs_obj, variant);
  6186. +
  6187. + T(YAFFS_TRACE_BACKGROUND,
  6188. + (TSTR("Update directory %d" TENDSTR), obj->obj_id));
  6189. +
  6190. + if (obj->dirty)
  6191. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  6192. + }
  6193. +}
  6194. +
  6195. +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  6196. +{
  6197. + struct yaffs_dev *dev = obj->my_dev;
  6198. + struct yaffs_obj *parent;
  6199. +
  6200. + yaffs_verify_obj_in_dir(obj);
  6201. + parent = obj->parent;
  6202. +
  6203. + yaffs_verify_dir(parent);
  6204. +
  6205. + if (dev && dev->param.remove_obj_fn)
  6206. + dev->param.remove_obj_fn(obj);
  6207. +
  6208. + list_del_init(&obj->siblings);
  6209. + obj->parent = NULL;
  6210. +
  6211. + yaffs_verify_dir(parent);
  6212. +}
  6213. +
  6214. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  6215. +{
  6216. + if (!directory) {
  6217. + T(YAFFS_TRACE_ALWAYS,
  6218. + (TSTR
  6219. + ("tragedy: Trying to add an object to a null pointer directory"
  6220. + TENDSTR)));
  6221. + YBUG();
  6222. + return;
  6223. + }
  6224. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6225. + T(YAFFS_TRACE_ALWAYS,
  6226. + (TSTR
  6227. + ("tragedy: Trying to add an object to a non-directory"
  6228. + TENDSTR)));
  6229. + YBUG();
  6230. + }
  6231. +
  6232. + if (obj->siblings.prev == NULL) {
  6233. + /* Not initialised */
  6234. + YBUG();
  6235. + }
  6236. +
  6237. + yaffs_verify_dir(directory);
  6238. +
  6239. + yaffs_remove_obj_from_dir(obj);
  6240. +
  6241. + /* Now add it */
  6242. + list_add(&obj->siblings, &directory->variant.dir_variant.children);
  6243. + obj->parent = directory;
  6244. +
  6245. + if (directory == obj->my_dev->unlinked_dir
  6246. + || directory == obj->my_dev->del_dir) {
  6247. + obj->unlinked = 1;
  6248. + obj->my_dev->n_unlinked_files++;
  6249. + obj->rename_allowed = 0;
  6250. + }
  6251. +
  6252. + yaffs_verify_dir(directory);
  6253. + yaffs_verify_obj_in_dir(obj);
  6254. +}
  6255. +
  6256. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
  6257. + const YCHAR * name)
  6258. +{
  6259. + int sum;
  6260. +
  6261. + struct list_head *i;
  6262. + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  6263. +
  6264. + struct yaffs_obj *l;
  6265. +
  6266. + if (!name)
  6267. + return NULL;
  6268. +
  6269. + if (!directory) {
  6270. + T(YAFFS_TRACE_ALWAYS,
  6271. + (TSTR
  6272. + ("tragedy: yaffs_find_by_name: null pointer directory"
  6273. + TENDSTR)));
  6274. + YBUG();
  6275. + return NULL;
  6276. + }
  6277. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6278. + T(YAFFS_TRACE_ALWAYS,
  6279. + (TSTR("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
  6280. + YBUG();
  6281. + }
  6282. +
  6283. + sum = yaffs_calc_name_sum(name);
  6284. +
  6285. + list_for_each(i, &directory->variant.dir_variant.children) {
  6286. + if (i) {
  6287. + l = list_entry(i, struct yaffs_obj, siblings);
  6288. +
  6289. + if (l->parent != directory)
  6290. + YBUG();
  6291. +
  6292. + yaffs_check_obj_details_loaded(l);
  6293. +
  6294. + /* Special case for lost-n-found */
  6295. + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6296. + if (!yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME))
  6297. + return l;
  6298. + } else if (yaffs_sum_cmp(l->sum, sum)
  6299. + || l->hdr_chunk <= 0) {
  6300. + /* LostnFound chunk called Objxxx
  6301. + * Do a real check
  6302. + */
  6303. + yaffs_get_obj_name(l, buffer,
  6304. + YAFFS_MAX_NAME_LENGTH + 1);
  6305. + if (yaffs_strncmp
  6306. + (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
  6307. + return l;
  6308. + }
  6309. + }
  6310. + }
  6311. +
  6312. + return NULL;
  6313. +}
  6314. +
  6315. +/* GetEquivalentObject dereferences any hard links to get to the
  6316. + * actual object.
  6317. + */
  6318. +
  6319. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
  6320. +{
  6321. + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  6322. + /* We want the object id of the equivalent object, not this one */
  6323. + obj = obj->variant.hardlink_variant.equiv_obj;
  6324. + yaffs_check_obj_details_loaded(obj);
  6325. + }
  6326. + return obj;
  6327. +}
  6328. +
  6329. +/*
  6330. + * A note or two on object names.
  6331. + * * If the object name is missing, we then make one up in the form objnnn
  6332. + *
  6333. + * * ASCII names are stored in the object header's name field from byte zero
  6334. + * * Unicode names are historically stored starting from byte zero.
  6335. + *
  6336. + * Then there are automatic Unicode names...
  6337. + * The purpose of these is to save names in a way that can be read as
  6338. + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
  6339. + * system to share files.
  6340. + *
  6341. + * These automatic unicode are stored slightly differently...
  6342. + * - If the name can fit in the ASCII character space then they are saved as
  6343. + * ascii names as per above.
  6344. + * - If the name needs Unicode then the name is saved in Unicode
  6345. + * starting at oh->name[1].
  6346. +
  6347. + */
  6348. +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
  6349. + int buffer_size)
  6350. +{
  6351. + /* Create an object name if we could not find one. */
  6352. + if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
  6353. + YCHAR local_name[20];
  6354. + YCHAR num_string[20];
  6355. + YCHAR *x = &num_string[19];
  6356. + unsigned v = obj->obj_id;
  6357. + num_string[19] = 0;
  6358. + while (v > 0) {
  6359. + x--;
  6360. + *x = '0' + (v % 10);
  6361. + v /= 10;
  6362. + }
  6363. + /* make up a name */
  6364. + yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
  6365. + yaffs_strcat(local_name, x);
  6366. + yaffs_strncpy(name, local_name, buffer_size - 1);
  6367. + }
  6368. +}
  6369. +
  6370. +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
  6371. + const YCHAR * oh_name, int buff_size)
  6372. +{
  6373. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  6374. + if (dev->param.auto_unicode) {
  6375. + if (*oh_name) {
  6376. + /* It is an ASCII name, so do an ASCII to unicode conversion */
  6377. + const char *ascii_oh_name = (const char *)oh_name;
  6378. + int n = buff_size - 1;
  6379. + while (n > 0 && *ascii_oh_name) {
  6380. + *name = *ascii_oh_name;
  6381. + name++;
  6382. + ascii_oh_name++;
  6383. + n--;
  6384. + }
  6385. + } else {
  6386. + yaffs_strncpy(name, oh_name + 1, buff_size - 1);
  6387. + }
  6388. + } else {
  6389. +#else
  6390. + {
  6391. +#endif
  6392. + yaffs_strncpy(name, oh_name, buff_size - 1);
  6393. + }
  6394. +}
  6395. +
  6396. +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
  6397. + const YCHAR * name)
  6398. +{
  6399. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  6400. +
  6401. + int is_ascii;
  6402. + YCHAR *w;
  6403. +
  6404. + if (dev->param.auto_unicode) {
  6405. +
  6406. + is_ascii = 1;
  6407. + w = name;
  6408. +
  6409. + /* Figure out if the name will fit in ascii character set */
  6410. + while (is_ascii && *w) {
  6411. + if ((*w) & 0xff00)
  6412. + is_ascii = 0;
  6413. + w++;
  6414. + }
  6415. +
  6416. + if (is_ascii) {
  6417. + /* It is an ASCII name, so do a unicode to ascii conversion */
  6418. + char *ascii_oh_name = (char *)oh_name;
  6419. + int n = YAFFS_MAX_NAME_LENGTH - 1;
  6420. + while (n > 0 && *name) {
  6421. + *ascii_oh_name = *name;
  6422. + name++;
  6423. + ascii_oh_name++;
  6424. + n--;
  6425. + }
  6426. + } else {
  6427. + /* It is a unicode name, so save starting at the second YCHAR */
  6428. + *oh_name = 0;
  6429. + yaffs_strncpy(oh_name + 1, name,
  6430. + YAFFS_MAX_NAME_LENGTH - 2);
  6431. + }
  6432. + } else {
  6433. +#else
  6434. + {
  6435. +#endif
  6436. + yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
  6437. + }
  6438. +
  6439. +}
  6440. +
  6441. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
  6442. +{
  6443. + memset(name, 0, buffer_size * sizeof(YCHAR));
  6444. +
  6445. + yaffs_check_obj_details_loaded(obj);
  6446. +
  6447. + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6448. + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
  6449. + }
  6450. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  6451. + else if (obj->short_name[0]) {
  6452. + yaffs_strcpy(name, obj->short_name);
  6453. + }
  6454. +#endif
  6455. + else if (obj->hdr_chunk > 0) {
  6456. + int result;
  6457. + u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
  6458. +
  6459. + struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
  6460. +
  6461. + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
  6462. +
  6463. + if (obj->hdr_chunk > 0) {
  6464. + result = yaffs_rd_chunk_tags_nand(obj->my_dev,
  6465. + obj->hdr_chunk,
  6466. + buffer, NULL);
  6467. + }
  6468. + yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
  6469. + buffer_size);
  6470. +
  6471. + yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
  6472. + }
  6473. +
  6474. + yaffs_fix_null_name(obj, name, buffer_size);
  6475. +
  6476. + return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH);
  6477. +}
  6478. +
  6479. +int yaffs_get_obj_length(struct yaffs_obj *obj)
  6480. +{
  6481. + /* Dereference any hard linking */
  6482. + obj = yaffs_get_equivalent_obj(obj);
  6483. +
  6484. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  6485. + return obj->variant.file_variant.file_size;
  6486. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6487. + if (!obj->variant.symlink_variant.alias)
  6488. + return 0;
  6489. + return yaffs_strnlen(obj->variant.symlink_variant.alias,
  6490. + YAFFS_MAX_ALIAS_LENGTH);
  6491. + } else {
  6492. + /* Only a directory should drop through to here */
  6493. + return obj->my_dev->data_bytes_per_chunk;
  6494. + }
  6495. +}
  6496. +
  6497. +int yaffs_get_obj_link_count(struct yaffs_obj *obj)
  6498. +{
  6499. + int count = 0;
  6500. + struct list_head *i;
  6501. +
  6502. + if (!obj->unlinked)
  6503. + count++; /* the object itself */
  6504. +
  6505. + list_for_each(i, &obj->hard_links)
  6506. + count++; /* add the hard links; */
  6507. +
  6508. + return count;
  6509. +}
  6510. +
  6511. +int yaffs_get_obj_inode(struct yaffs_obj *obj)
  6512. +{
  6513. + obj = yaffs_get_equivalent_obj(obj);
  6514. +
  6515. + return obj->obj_id;
  6516. +}
  6517. +
  6518. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
  6519. +{
  6520. + obj = yaffs_get_equivalent_obj(obj);
  6521. +
  6522. + switch (obj->variant_type) {
  6523. + case YAFFS_OBJECT_TYPE_FILE:
  6524. + return DT_REG;
  6525. + break;
  6526. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6527. + return DT_DIR;
  6528. + break;
  6529. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6530. + return DT_LNK;
  6531. + break;
  6532. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6533. + return DT_REG;
  6534. + break;
  6535. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6536. + if (S_ISFIFO(obj->yst_mode))
  6537. + return DT_FIFO;
  6538. + if (S_ISCHR(obj->yst_mode))
  6539. + return DT_CHR;
  6540. + if (S_ISBLK(obj->yst_mode))
  6541. + return DT_BLK;
  6542. + if (S_ISSOCK(obj->yst_mode))
  6543. + return DT_SOCK;
  6544. + default:
  6545. + return DT_REG;
  6546. + break;
  6547. + }
  6548. +}
  6549. +
  6550. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
  6551. +{
  6552. + obj = yaffs_get_equivalent_obj(obj);
  6553. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  6554. + return yaffs_clone_str(obj->variant.symlink_variant.alias);
  6555. + else
  6556. + return yaffs_clone_str(_Y(""));
  6557. +}
  6558. +
  6559. +static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
  6560. + const YCHAR * name, const void *value, int size,
  6561. + int flags)
  6562. +{
  6563. + struct yaffs_xattr_mod xmod;
  6564. +
  6565. + int result;
  6566. +
  6567. + xmod.set = set;
  6568. + xmod.name = name;
  6569. + xmod.data = value;
  6570. + xmod.size = size;
  6571. + xmod.flags = flags;
  6572. + xmod.result = -ENOSPC;
  6573. +
  6574. + result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
  6575. +
  6576. + if (result > 0)
  6577. + return xmod.result;
  6578. + else
  6579. + return -ENOSPC;
  6580. +}
  6581. +
  6582. +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  6583. + struct yaffs_xattr_mod *xmod)
  6584. +{
  6585. + int retval = 0;
  6586. + int x_offs = sizeof(struct yaffs_obj_hdr);
  6587. + struct yaffs_dev *dev = obj->my_dev;
  6588. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  6589. +
  6590. + char *x_buffer = buffer + x_offs;
  6591. +
  6592. + if (xmod->set)
  6593. + retval =
  6594. + nval_set(x_buffer, x_size, xmod->name, xmod->data,
  6595. + xmod->size, xmod->flags);
  6596. + else
  6597. + retval = nval_del(x_buffer, x_size, xmod->name);
  6598. +
  6599. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  6600. + obj->xattr_known = 1;
  6601. +
  6602. + xmod->result = retval;
  6603. +
  6604. + return retval;
  6605. +}
  6606. +
  6607. +static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
  6608. + void *value, int size)
  6609. +{
  6610. + char *buffer = NULL;
  6611. + int result;
  6612. + struct yaffs_ext_tags tags;
  6613. + struct yaffs_dev *dev = obj->my_dev;
  6614. + int x_offs = sizeof(struct yaffs_obj_hdr);
  6615. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  6616. +
  6617. + char *x_buffer;
  6618. +
  6619. + int retval = 0;
  6620. +
  6621. + if (obj->hdr_chunk < 1)
  6622. + return -ENODATA;
  6623. +
  6624. + /* If we know that the object has no xattribs then don't do all the
  6625. + * reading and parsing.
  6626. + */
  6627. + if (obj->xattr_known && !obj->has_xattr) {
  6628. + if (name)
  6629. + return -ENODATA;
  6630. + else
  6631. + return 0;
  6632. + }
  6633. +
  6634. + buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
  6635. + if (!buffer)
  6636. + return -ENOMEM;
  6637. +
  6638. + result =
  6639. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
  6640. +
  6641. + if (result != YAFFS_OK)
  6642. + retval = -ENOENT;
  6643. + else {
  6644. + x_buffer = buffer + x_offs;
  6645. +
  6646. + if (!obj->xattr_known) {
  6647. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  6648. + obj->xattr_known = 1;
  6649. + }
  6650. +
  6651. + if (name)
  6652. + retval = nval_get(x_buffer, x_size, name, value, size);
  6653. + else
  6654. + retval = nval_list(x_buffer, x_size, value, size);
  6655. + }
  6656. + yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
  6657. + return retval;
  6658. +}
  6659. +
  6660. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  6661. + const void *value, int size, int flags)
  6662. +{
  6663. + return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
  6664. +}
  6665. +
  6666. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
  6667. +{
  6668. + return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
  6669. +}
  6670. +
  6671. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  6672. + int size)
  6673. +{
  6674. + return yaffs_do_xattrib_fetch(obj, name, value, size);
  6675. +}
  6676. +
  6677. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
  6678. +{
  6679. + return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
  6680. +}
  6681. +
  6682. +/*---------------------------- Initialisation code -------------------------------------- */
  6683. +
  6684. +static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
  6685. +{
  6686. +
  6687. + /* Common functions, gotta have */
  6688. + if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
  6689. + return 0;
  6690. +
  6691. +#ifdef CONFIG_YAFFS_YAFFS2
  6692. +
  6693. + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  6694. + if (dev->param.write_chunk_tags_fn &&
  6695. + dev->param.read_chunk_tags_fn &&
  6696. + !dev->param.write_chunk_fn &&
  6697. + !dev->param.read_chunk_fn &&
  6698. + dev->param.bad_block_fn && dev->param.query_block_fn)
  6699. + return 1;
  6700. +#endif
  6701. +
  6702. + /* Can use the "spare" style interface for yaffs1 */
  6703. + if (!dev->param.is_yaffs2 &&
  6704. + !dev->param.write_chunk_tags_fn &&
  6705. + !dev->param.read_chunk_tags_fn &&
  6706. + dev->param.write_chunk_fn &&
  6707. + dev->param.read_chunk_fn &&
  6708. + !dev->param.bad_block_fn && !dev->param.query_block_fn)
  6709. + return 1;
  6710. +
  6711. + return 0; /* bad */
  6712. +}
  6713. +
  6714. +static int yaffs_create_initial_dir(struct yaffs_dev *dev)
  6715. +{
  6716. + /* Initialise the unlinked, deleted, root and lost and found directories */
  6717. +
  6718. + dev->lost_n_found = dev->root_dir = NULL;
  6719. + dev->unlinked_dir = dev->del_dir = NULL;
  6720. +
  6721. + dev->unlinked_dir =
  6722. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  6723. +
  6724. + dev->del_dir =
  6725. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  6726. +
  6727. + dev->root_dir =
  6728. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
  6729. + YAFFS_ROOT_MODE | S_IFDIR);
  6730. + dev->lost_n_found =
  6731. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
  6732. + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  6733. +
  6734. + if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
  6735. + && dev->del_dir) {
  6736. + yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
  6737. + return YAFFS_OK;
  6738. + }
  6739. +
  6740. + return YAFFS_FAIL;
  6741. +}
  6742. +
  6743. +int yaffs_guts_initialise(struct yaffs_dev *dev)
  6744. +{
  6745. + int init_failed = 0;
  6746. + unsigned x;
  6747. + int bits;
  6748. +
  6749. + T(YAFFS_TRACE_TRACING,
  6750. + (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
  6751. +
  6752. + /* Check stuff that must be set */
  6753. +
  6754. + if (!dev) {
  6755. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
  6756. + return YAFFS_FAIL;
  6757. + }
  6758. +
  6759. + dev->internal_start_block = dev->param.start_block;
  6760. + dev->internal_end_block = dev->param.end_block;
  6761. + dev->block_offset = 0;
  6762. + dev->chunk_offset = 0;
  6763. + dev->n_free_chunks = 0;
  6764. +
  6765. + dev->gc_block = 0;
  6766. +
  6767. + if (dev->param.start_block == 0) {
  6768. + dev->internal_start_block = dev->param.start_block + 1;
  6769. + dev->internal_end_block = dev->param.end_block + 1;
  6770. + dev->block_offset = 1;
  6771. + dev->chunk_offset = dev->param.chunks_per_block;
  6772. + }
  6773. +
  6774. + /* Check geometry parameters. */
  6775. +
  6776. + if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) || (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) || (dev->param.inband_tags && !dev->param.is_yaffs2) || dev->param.chunks_per_block < 2 || dev->param.n_reserved_blocks < 2 || dev->internal_start_block <= 0 || dev->internal_end_block <= 0 || dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
  6777. + T(YAFFS_TRACE_ALWAYS,
  6778. + (TSTR
  6779. + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
  6780. + TENDSTR), dev->param.total_bytes_per_chunk,
  6781. + dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
  6782. + return YAFFS_FAIL;
  6783. + }
  6784. +
  6785. + if (yaffs_init_nand(dev) != YAFFS_OK) {
  6786. + T(YAFFS_TRACE_ALWAYS,
  6787. + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
  6788. + return YAFFS_FAIL;
  6789. + }
  6790. +
  6791. + /* Sort out space for inband tags, if required */
  6792. + if (dev->param.inband_tags)
  6793. + dev->data_bytes_per_chunk =
  6794. + dev->param.total_bytes_per_chunk -
  6795. + sizeof(struct yaffs_packed_tags2_tags_only);
  6796. + else
  6797. + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
  6798. +
  6799. + /* Got the right mix of functions? */
  6800. + if (!yaffs_check_dev_fns(dev)) {
  6801. + /* Function missing */
  6802. + T(YAFFS_TRACE_ALWAYS,
  6803. + (TSTR
  6804. + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
  6805. +
  6806. + return YAFFS_FAIL;
  6807. + }
  6808. +
  6809. + if (dev->is_mounted) {
  6810. + T(YAFFS_TRACE_ALWAYS,
  6811. + (TSTR("yaffs: device already mounted\n" TENDSTR)));
  6812. + return YAFFS_FAIL;
  6813. + }
  6814. +
  6815. + /* Finished with most checks. One or two more checks happen later on too. */
  6816. +
  6817. + dev->is_mounted = 1;
  6818. +
  6819. + /* OK now calculate a few things for the device */
  6820. +
  6821. + /*
  6822. + * Calculate all the chunk size manipulation numbers:
  6823. + */
  6824. + x = dev->data_bytes_per_chunk;
  6825. + /* We always use dev->chunk_shift and dev->chunk_div */
  6826. + dev->chunk_shift = calc_shifts(x);
  6827. + x >>= dev->chunk_shift;
  6828. + dev->chunk_div = x;
  6829. + /* We only use chunk mask if chunk_div is 1 */
  6830. + dev->chunk_mask = (1 << dev->chunk_shift) - 1;
  6831. +
  6832. + /*
  6833. + * Calculate chunk_grp_bits.
  6834. + * We need to find the next power of 2 > than internal_end_block
  6835. + */
  6836. +
  6837. + x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
  6838. +
  6839. + bits = calc_shifts_ceiling(x);
  6840. +
  6841. + /* Set up tnode width if wide tnodes are enabled. */
  6842. + if (!dev->param.wide_tnodes_disabled) {
  6843. + /* bits must be even so that we end up with 32-bit words */
  6844. + if (bits & 1)
  6845. + bits++;
  6846. + if (bits < 16)
  6847. + dev->tnode_width = 16;
  6848. + else
  6849. + dev->tnode_width = bits;
  6850. + } else {
  6851. + dev->tnode_width = 16;
  6852. + }
  6853. +
  6854. + dev->tnode_mask = (1 << dev->tnode_width) - 1;
  6855. +
  6856. + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  6857. + * so if the bitwidth of the
  6858. + * chunk range we're using is greater than 16 we need
  6859. + * to figure out chunk shift and chunk_grp_size
  6860. + */
  6861. +
  6862. + if (bits <= dev->tnode_width)
  6863. + dev->chunk_grp_bits = 0;
  6864. + else
  6865. + dev->chunk_grp_bits = bits - dev->tnode_width;
  6866. +
  6867. + dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
  6868. + if (dev->tnode_size < sizeof(struct yaffs_tnode))
  6869. + dev->tnode_size = sizeof(struct yaffs_tnode);
  6870. +
  6871. + dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
  6872. +
  6873. + if (dev->param.chunks_per_block < dev->chunk_grp_size) {
  6874. + /* We have a problem because the soft delete won't work if
  6875. + * the chunk group size > chunks per block.
  6876. + * This can be remedied by using larger "virtual blocks".
  6877. + */
  6878. + T(YAFFS_TRACE_ALWAYS,
  6879. + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
  6880. +
  6881. + return YAFFS_FAIL;
  6882. + }
  6883. +
  6884. + /* OK, we've finished verifying the device, lets continue with initialisation */
  6885. +
  6886. + /* More device initialisation */
  6887. + dev->all_gcs = 0;
  6888. + dev->passive_gc_count = 0;
  6889. + dev->oldest_dirty_gc_count = 0;
  6890. + dev->bg_gcs = 0;
  6891. + dev->gc_block_finder = 0;
  6892. + dev->buffered_block = -1;
  6893. + dev->doing_buffered_block_rewrite = 0;
  6894. + dev->n_deleted_files = 0;
  6895. + dev->n_bg_deletions = 0;
  6896. + dev->n_unlinked_files = 0;
  6897. + dev->n_ecc_fixed = 0;
  6898. + dev->n_ecc_unfixed = 0;
  6899. + dev->n_tags_ecc_fixed = 0;
  6900. + dev->n_tags_ecc_unfixed = 0;
  6901. + dev->n_erase_failures = 0;
  6902. + dev->n_erased_blocks = 0;
  6903. + dev->gc_disable = 0;
  6904. + dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
  6905. + INIT_LIST_HEAD(&dev->dirty_dirs);
  6906. + dev->oldest_dirty_seq = 0;
  6907. + dev->oldest_dirty_block = 0;
  6908. +
  6909. + /* Initialise temporary buffers and caches. */
  6910. + if (!yaffs_init_tmp_buffers(dev))
  6911. + init_failed = 1;
  6912. +
  6913. + dev->cache = NULL;
  6914. + dev->gc_cleanup_list = NULL;
  6915. +
  6916. + if (!init_failed && dev->param.n_caches > 0) {
  6917. + int i;
  6918. + void *buf;
  6919. + int cache_bytes =
  6920. + dev->param.n_caches * sizeof(struct yaffs_cache);
  6921. +
  6922. + if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
  6923. + dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
  6924. +
  6925. + dev->cache = YMALLOC(cache_bytes);
  6926. +
  6927. + buf = (u8 *) dev->cache;
  6928. +
  6929. + if (dev->cache)
  6930. + memset(dev->cache, 0, cache_bytes);
  6931. +
  6932. + for (i = 0; i < dev->param.n_caches && buf; i++) {
  6933. + dev->cache[i].object = NULL;
  6934. + dev->cache[i].last_use = 0;
  6935. + dev->cache[i].dirty = 0;
  6936. + dev->cache[i].data = buf =
  6937. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  6938. + }
  6939. + if (!buf)
  6940. + init_failed = 1;
  6941. +
  6942. + dev->cache_last_use = 0;
  6943. + }
  6944. +
  6945. + dev->cache_hits = 0;
  6946. +
  6947. + if (!init_failed) {
  6948. + dev->gc_cleanup_list =
  6949. + YMALLOC(dev->param.chunks_per_block * sizeof(u32));
  6950. + if (!dev->gc_cleanup_list)
  6951. + init_failed = 1;
  6952. + }
  6953. +
  6954. + if (dev->param.is_yaffs2)
  6955. + dev->param.use_header_file_size = 1;
  6956. +
  6957. + if (!init_failed && !yaffs_init_blocks(dev))
  6958. + init_failed = 1;
  6959. +
  6960. + yaffs_init_tnodes_and_objs(dev);
  6961. +
  6962. + if (!init_failed && !yaffs_create_initial_dir(dev))
  6963. + init_failed = 1;
  6964. +
  6965. + if (!init_failed) {
  6966. + /* Now scan the flash. */
  6967. + if (dev->param.is_yaffs2) {
  6968. + if (yaffs2_checkpt_restore(dev)) {
  6969. + yaffs_check_obj_details_loaded(dev->root_dir);
  6970. + T(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  6971. + (TSTR
  6972. + ("yaffs: restored from checkpoint"
  6973. + TENDSTR)));
  6974. + } else {
  6975. +
  6976. + /* Clean up the mess caused by an aborted checkpoint load
  6977. + * and scan backwards.
  6978. + */
  6979. + yaffs_deinit_blocks(dev);
  6980. +
  6981. + yaffs_deinit_tnodes_and_objs(dev);
  6982. +
  6983. + dev->n_erased_blocks = 0;
  6984. + dev->n_free_chunks = 0;
  6985. + dev->alloc_block = -1;
  6986. + dev->alloc_page = -1;
  6987. + dev->n_deleted_files = 0;
  6988. + dev->n_unlinked_files = 0;
  6989. + dev->n_bg_deletions = 0;
  6990. +
  6991. + if (!init_failed && !yaffs_init_blocks(dev))
  6992. + init_failed = 1;
  6993. +
  6994. + yaffs_init_tnodes_and_objs(dev);
  6995. +
  6996. + if (!init_failed
  6997. + && !yaffs_create_initial_dir(dev))
  6998. + init_failed = 1;
  6999. +
  7000. + if (!init_failed && !yaffs2_scan_backwards(dev))
  7001. + init_failed = 1;
  7002. + }
  7003. + } else if (!yaffs1_scan(dev)) {
  7004. + init_failed = 1;
  7005. + }
  7006. +
  7007. + yaffs_strip_deleted_objs(dev);
  7008. + yaffs_fix_hanging_objs(dev);
  7009. + if (dev->param.empty_lost_n_found)
  7010. + yaffs_empty_l_n_f(dev);
  7011. + }
  7012. +
  7013. + if (init_failed) {
  7014. + /* Clean up the mess */
  7015. + T(YAFFS_TRACE_TRACING,
  7016. + (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
  7017. +
  7018. + yaffs_deinitialise(dev);
  7019. + return YAFFS_FAIL;
  7020. + }
  7021. +
  7022. + /* Zero out stats */
  7023. + dev->n_page_reads = 0;
  7024. + dev->n_page_writes = 0;
  7025. + dev->n_erasures = 0;
  7026. + dev->n_gc_copies = 0;
  7027. + dev->n_retired_writes = 0;
  7028. +
  7029. + dev->n_retired_blocks = 0;
  7030. +
  7031. + yaffs_verify_free_chunks(dev);
  7032. + yaffs_verify_blocks(dev);
  7033. +
  7034. + /* Clean up any aborted checkpoint data */
  7035. + if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
  7036. + yaffs2_checkpt_invalidate(dev);
  7037. +
  7038. + T(YAFFS_TRACE_TRACING,
  7039. + (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
  7040. + return YAFFS_OK;
  7041. +
  7042. +}
  7043. +
  7044. +void yaffs_deinitialise(struct yaffs_dev *dev)
  7045. +{
  7046. + if (dev->is_mounted) {
  7047. + int i;
  7048. +
  7049. + yaffs_deinit_blocks(dev);
  7050. + yaffs_deinit_tnodes_and_objs(dev);
  7051. + if (dev->param.n_caches > 0 && dev->cache) {
  7052. +
  7053. + for (i = 0; i < dev->param.n_caches; i++) {
  7054. + if (dev->cache[i].data)
  7055. + YFREE(dev->cache[i].data);
  7056. + dev->cache[i].data = NULL;
  7057. + }
  7058. +
  7059. + YFREE(dev->cache);
  7060. + dev->cache = NULL;
  7061. + }
  7062. +
  7063. + YFREE(dev->gc_cleanup_list);
  7064. +
  7065. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  7066. + YFREE(dev->temp_buffer[i].buffer);
  7067. +
  7068. + dev->is_mounted = 0;
  7069. +
  7070. + if (dev->param.deinitialise_flash_fn)
  7071. + dev->param.deinitialise_flash_fn(dev);
  7072. + }
  7073. +}
  7074. +
  7075. +int yaffs_count_free_chunks(struct yaffs_dev *dev)
  7076. +{
  7077. + int n_free = 0;
  7078. + int b;
  7079. +
  7080. + struct yaffs_block_info *blk;
  7081. +
  7082. + blk = dev->block_info;
  7083. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  7084. + switch (blk->block_state) {
  7085. + case YAFFS_BLOCK_STATE_EMPTY:
  7086. + case YAFFS_BLOCK_STATE_ALLOCATING:
  7087. + case YAFFS_BLOCK_STATE_COLLECTING:
  7088. + case YAFFS_BLOCK_STATE_FULL:
  7089. + n_free +=
  7090. + (dev->param.chunks_per_block - blk->pages_in_use +
  7091. + blk->soft_del_pages);
  7092. + break;
  7093. + default:
  7094. + break;
  7095. + }
  7096. + blk++;
  7097. + }
  7098. +
  7099. + return n_free;
  7100. +}
  7101. +
  7102. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
  7103. +{
  7104. + /* This is what we report to the outside world */
  7105. +
  7106. + int n_free;
  7107. + int n_dirty_caches;
  7108. + int blocks_for_checkpt;
  7109. + int i;
  7110. +
  7111. + n_free = dev->n_free_chunks;
  7112. + n_free += dev->n_deleted_files;
  7113. +
  7114. + /* Now count the number of dirty chunks in the cache and subtract those */
  7115. +
  7116. + for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
  7117. + if (dev->cache[i].dirty)
  7118. + n_dirty_caches++;
  7119. + }
  7120. +
  7121. + n_free -= n_dirty_caches;
  7122. +
  7123. + n_free -=
  7124. + ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
  7125. +
  7126. + /* Now we figure out how much to reserve for the checkpoint and report that... */
  7127. + blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
  7128. +
  7129. + n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
  7130. +
  7131. + if (n_free < 0)
  7132. + n_free = 0;
  7133. +
  7134. + return n_free;
  7135. +
  7136. +}
  7137. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_guts.h linux-2.6.36/fs/yaffs2/yaffs_guts.h
  7138. --- linux-2.6.36.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  7139. +++ linux-2.6.36/fs/yaffs2/yaffs_guts.h 2010-11-18 18:15:51.000000000 +0100
  7140. @@ -0,0 +1,914 @@
  7141. +/*
  7142. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  7143. + *
  7144. + * Copyright (C) 2002-2010 Aleph One Ltd.
  7145. + * for Toby Churchill Ltd and Brightstar Engineering
  7146. + *
  7147. + * Created by Charles Manning <charles@aleph1.co.uk>
  7148. + *
  7149. + * This program is free software; you can redistribute it and/or modify
  7150. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  7151. + * published by the Free Software Foundation.
  7152. + *
  7153. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  7154. + */
  7155. +
  7156. +#ifndef __YAFFS_GUTS_H__
  7157. +#define __YAFFS_GUTS_H__
  7158. +
  7159. +#include "yportenv.h"
  7160. +
  7161. +#define YAFFS_OK 1
  7162. +#define YAFFS_FAIL 0
  7163. +
  7164. +/* Give us a Y=0x59,
  7165. + * Give us an A=0x41,
  7166. + * Give us an FF=0xFF
  7167. + * Give us an S=0x53
  7168. + * And what have we got...
  7169. + */
  7170. +#define YAFFS_MAGIC 0x5941FF53
  7171. +
  7172. +#define YAFFS_NTNODES_LEVEL0 16
  7173. +#define YAFFS_TNODES_LEVEL0_BITS 4
  7174. +#define YAFFS_TNODES_LEVEL0_MASK 0xf
  7175. +
  7176. +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  7177. +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  7178. +#define YAFFS_TNODES_INTERNAL_MASK 0x7
  7179. +#define YAFFS_TNODES_MAX_LEVEL 6
  7180. +
  7181. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  7182. +#define YAFFS_BYTES_PER_SPARE 16
  7183. +#define YAFFS_BYTES_PER_CHUNK 512
  7184. +#define YAFFS_CHUNK_SIZE_SHIFT 9
  7185. +#define YAFFS_CHUNKS_PER_BLOCK 32
  7186. +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  7187. +#endif
  7188. +
  7189. +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  7190. +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  7191. +
  7192. +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
  7193. +
  7194. +#define YAFFS_ALLOCATION_NOBJECTS 100
  7195. +#define YAFFS_ALLOCATION_NTNODES 100
  7196. +#define YAFFS_ALLOCATION_NLINKS 100
  7197. +
  7198. +#define YAFFS_NOBJECT_BUCKETS 256
  7199. +
  7200. +#define YAFFS_OBJECT_SPACE 0x40000
  7201. +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1)
  7202. +
  7203. +#define YAFFS_CHECKPOINT_VERSION 4
  7204. +
  7205. +#ifdef CONFIG_YAFFS_UNICODE
  7206. +#define YAFFS_MAX_NAME_LENGTH 127
  7207. +#define YAFFS_MAX_ALIAS_LENGTH 79
  7208. +#else
  7209. +#define YAFFS_MAX_NAME_LENGTH 255
  7210. +#define YAFFS_MAX_ALIAS_LENGTH 159
  7211. +#endif
  7212. +
  7213. +#define YAFFS_SHORT_NAME_LENGTH 15
  7214. +
  7215. +/* Some special object ids for pseudo objects */
  7216. +#define YAFFS_OBJECTID_ROOT 1
  7217. +#define YAFFS_OBJECTID_LOSTNFOUND 2
  7218. +#define YAFFS_OBJECTID_UNLINKED 3
  7219. +#define YAFFS_OBJECTID_DELETED 4
  7220. +
  7221. +/* Pseudo object ids for checkpointing */
  7222. +#define YAFFS_OBJECTID_SB_HEADER 0x10
  7223. +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  7224. +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  7225. +
  7226. +#define YAFFS_MAX_SHORT_OP_CACHES 20
  7227. +
  7228. +#define YAFFS_N_TEMP_BUFFERS 6
  7229. +
  7230. +/* We limit the number attempts at sucessfully saving a chunk of data.
  7231. + * Small-page devices have 32 pages per block; large-page devices have 64.
  7232. + * Default to something in the order of 5 to 10 blocks worth of chunks.
  7233. + */
  7234. +#define YAFFS_WR_ATTEMPTS (5*64)
  7235. +
  7236. +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
  7237. + * The range is limited slightly to help distinguish bad numbers from good.
  7238. + * This also allows us to perhaps in the future use special numbers for
  7239. + * special purposes.
  7240. + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
  7241. + * and is a larger number than the lifetime of a 2GB device.
  7242. + */
  7243. +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  7244. +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
  7245. +
  7246. +/* Special sequence number for bad block that failed to be marked bad */
  7247. +#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
  7248. +
  7249. +/* ChunkCache is used for short read/write operations.*/
  7250. +struct yaffs_cache {
  7251. + struct yaffs_obj *object;
  7252. + int chunk_id;
  7253. + int last_use;
  7254. + int dirty;
  7255. + int n_bytes; /* Only valid if the cache is dirty */
  7256. + int locked; /* Can't push out or flush while locked. */
  7257. + u8 *data;
  7258. +};
  7259. +
  7260. +/* Tags structures in RAM
  7261. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
  7262. + * the structure size will get blown out.
  7263. + */
  7264. +
  7265. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  7266. +struct yaffs_tags {
  7267. + unsigned chunk_id:20;
  7268. + unsigned serial_number:2;
  7269. + unsigned n_bytes_lsb:10;
  7270. + unsigned obj_id:18;
  7271. + unsigned ecc:12;
  7272. + unsigned n_bytes_msb:2;
  7273. +};
  7274. +
  7275. +union yaffs_tags_union {
  7276. + struct yaffs_tags as_tags;
  7277. + u8 as_bytes[8];
  7278. +};
  7279. +
  7280. +#endif
  7281. +
  7282. +/* Stuff used for extended tags in YAFFS2 */
  7283. +
  7284. +enum yaffs_ecc_result {
  7285. + YAFFS_ECC_RESULT_UNKNOWN,
  7286. + YAFFS_ECC_RESULT_NO_ERROR,
  7287. + YAFFS_ECC_RESULT_FIXED,
  7288. + YAFFS_ECC_RESULT_UNFIXED
  7289. +};
  7290. +
  7291. +enum yaffs_obj_type {
  7292. + YAFFS_OBJECT_TYPE_UNKNOWN,
  7293. + YAFFS_OBJECT_TYPE_FILE,
  7294. + YAFFS_OBJECT_TYPE_SYMLINK,
  7295. + YAFFS_OBJECT_TYPE_DIRECTORY,
  7296. + YAFFS_OBJECT_TYPE_HARDLINK,
  7297. + YAFFS_OBJECT_TYPE_SPECIAL
  7298. +};
  7299. +
  7300. +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  7301. +
  7302. +struct yaffs_ext_tags {
  7303. +
  7304. + unsigned validity0;
  7305. + unsigned chunk_used; /* Status of the chunk: used or unused */
  7306. + unsigned obj_id; /* If 0 then this is not part of an object (unused) */
  7307. + unsigned chunk_id; /* If 0 then this is a header, else a data chunk */
  7308. + unsigned n_bytes; /* Only valid for data chunks */
  7309. +
  7310. + /* The following stuff only has meaning when we read */
  7311. + enum yaffs_ecc_result ecc_result;
  7312. + unsigned block_bad;
  7313. +
  7314. + /* YAFFS 1 stuff */
  7315. + unsigned is_deleted; /* The chunk is marked deleted */
  7316. + unsigned serial_number; /* Yaffs1 2-bit serial number */
  7317. +
  7318. + /* YAFFS2 stuff */
  7319. + unsigned seq_number; /* The sequence number of this block */
  7320. +
  7321. + /* Extra info if this is an object header (YAFFS2 only) */
  7322. +
  7323. + unsigned extra_available; /* There is extra info available if this is not zero */
  7324. + unsigned extra_parent_id; /* The parent object */
  7325. + unsigned extra_is_shrink; /* Is it a shrink header? */
  7326. + unsigned extra_shadows; /* Does this shadow another object? */
  7327. +
  7328. + enum yaffs_obj_type extra_obj_type; /* What object type? */
  7329. +
  7330. + unsigned extra_length; /* Length if it is a file */
  7331. + unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */
  7332. +
  7333. + unsigned validity1;
  7334. +
  7335. +};
  7336. +
  7337. +/* Spare structure for YAFFS1 */
  7338. +struct yaffs_spare {
  7339. + u8 tb0;
  7340. + u8 tb1;
  7341. + u8 tb2;
  7342. + u8 tb3;
  7343. + u8 page_status; /* set to 0 to delete the chunk */
  7344. + u8 block_status;
  7345. + u8 tb4;
  7346. + u8 tb5;
  7347. + u8 ecc1[3];
  7348. + u8 tb6;
  7349. + u8 tb7;
  7350. + u8 ecc2[3];
  7351. +};
  7352. +
  7353. +/*Special structure for passing through to mtd */
  7354. +struct yaffs_nand_spare {
  7355. + struct yaffs_spare spare;
  7356. + int eccres1;
  7357. + int eccres2;
  7358. +};
  7359. +
  7360. +/* Block data in RAM */
  7361. +
  7362. +enum yaffs_block_state {
  7363. + YAFFS_BLOCK_STATE_UNKNOWN = 0,
  7364. +
  7365. + YAFFS_BLOCK_STATE_SCANNING,
  7366. + /* Being scanned */
  7367. +
  7368. + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
  7369. + /* The block might have something on it (ie it is allocating or full, perhaps empty)
  7370. + * but it needs to be scanned to determine its true state.
  7371. + * This state is only valid during scanning.
  7372. + * NB We tolerate empty because the pre-scanner might be incapable of deciding
  7373. + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
  7374. + */
  7375. +
  7376. + YAFFS_BLOCK_STATE_EMPTY,
  7377. + /* This block is empty */
  7378. +
  7379. + YAFFS_BLOCK_STATE_ALLOCATING,
  7380. + /* This block is partially allocated.
  7381. + * At least one page holds valid data.
  7382. + * This is the one currently being used for page
  7383. + * allocation. Should never be more than one of these.
  7384. + * If a block is only partially allocated at mount it is treated as full.
  7385. + */
  7386. +
  7387. + YAFFS_BLOCK_STATE_FULL,
  7388. + /* All the pages in this block have been allocated.
  7389. + * If a block was only partially allocated when mounted we treat
  7390. + * it as fully allocated.
  7391. + */
  7392. +
  7393. + YAFFS_BLOCK_STATE_DIRTY,
  7394. + /* The block was full and now all chunks have been deleted.
  7395. + * Erase me, reuse me.
  7396. + */
  7397. +
  7398. + YAFFS_BLOCK_STATE_CHECKPOINT,
  7399. + /* This block is assigned to holding checkpoint data. */
  7400. +
  7401. + YAFFS_BLOCK_STATE_COLLECTING,
  7402. + /* This block is being garbage collected */
  7403. +
  7404. + YAFFS_BLOCK_STATE_DEAD
  7405. + /* This block has failed and is not in use */
  7406. +};
  7407. +
  7408. +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  7409. +
  7410. +struct yaffs_block_info {
  7411. +
  7412. + int soft_del_pages:10; /* number of soft deleted pages */
  7413. + int pages_in_use:10; /* number of pages in use */
  7414. + unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
  7415. + u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */
  7416. + /* and retire the block. */
  7417. + u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */
  7418. + u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block.
  7419. + It should be prioritised for GC */
  7420. + u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
  7421. +
  7422. +#ifdef CONFIG_YAFFS_YAFFS2
  7423. + u32 has_shrink_hdr:1; /* This block has at least one shrink object header */
  7424. + u32 seq_number; /* block sequence number for yaffs2 */
  7425. +#endif
  7426. +
  7427. +};
  7428. +
  7429. +/* -------------------------- Object structure -------------------------------*/
  7430. +/* This is the object structure as stored on NAND */
  7431. +
  7432. +struct yaffs_obj_hdr {
  7433. + enum yaffs_obj_type type;
  7434. +
  7435. + /* Apply to everything */
  7436. + int parent_obj_id;
  7437. + u16 sum_no_longer_used; /* checksum of name. No longer used */
  7438. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  7439. +
  7440. + /* The following apply to directories, files, symlinks - not hard links */
  7441. + u32 yst_mode; /* protection */
  7442. +
  7443. + u32 yst_uid;
  7444. + u32 yst_gid;
  7445. + u32 yst_atime;
  7446. + u32 yst_mtime;
  7447. + u32 yst_ctime;
  7448. +
  7449. + /* File size applies to files only */
  7450. + int file_size;
  7451. +
  7452. + /* Equivalent object id applies to hard links only. */
  7453. + int equiv_id;
  7454. +
  7455. + /* Alias is for symlinks only. */
  7456. + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  7457. +
  7458. + u32 yst_rdev; /* device stuff for block and char devices (major/min) */
  7459. +
  7460. + u32 win_ctime[2];
  7461. + u32 win_atime[2];
  7462. + u32 win_mtime[2];
  7463. +
  7464. + u32 inband_shadowed_obj_id;
  7465. + u32 inband_is_shrink;
  7466. +
  7467. + u32 reserved[2];
  7468. + int shadows_obj; /* This object header shadows the specified object if > 0 */
  7469. +
  7470. + /* is_shrink applies to object headers written when we shrink the file (ie resize) */
  7471. + u32 is_shrink;
  7472. +
  7473. +};
  7474. +
  7475. +/*--------------------------- Tnode -------------------------- */
  7476. +
  7477. +struct yaffs_tnode {
  7478. + struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
  7479. +};
  7480. +
  7481. +/*------------------------ Object -----------------------------*/
  7482. +/* An object can be one of:
  7483. + * - a directory (no data, has children links
  7484. + * - a regular file (data.... not prunes :->).
  7485. + * - a symlink [symbolic link] (the alias).
  7486. + * - a hard link
  7487. + */
  7488. +
  7489. +struct yaffs_file_var {
  7490. + u32 file_size;
  7491. + u32 scanned_size;
  7492. + u32 shrink_size;
  7493. + int top_level;
  7494. + struct yaffs_tnode *top;
  7495. +};
  7496. +
  7497. +struct yaffs_dir_var {
  7498. + struct list_head children; /* list of child links */
  7499. + struct list_head dirty; /* Entry for list of dirty directories */
  7500. +};
  7501. +
  7502. +struct yaffs_symlink_var {
  7503. + YCHAR *alias;
  7504. +};
  7505. +
  7506. +struct yaffs_hardlink_var {
  7507. + struct yaffs_obj *equiv_obj;
  7508. + u32 equiv_id;
  7509. +};
  7510. +
  7511. +union yaffs_obj_var {
  7512. + struct yaffs_file_var file_variant;
  7513. + struct yaffs_dir_var dir_variant;
  7514. + struct yaffs_symlink_var symlink_variant;
  7515. + struct yaffs_hardlink_var hardlink_variant;
  7516. +};
  7517. +
  7518. +struct yaffs_obj {
  7519. + u8 deleted:1; /* This should only apply to unlinked files. */
  7520. + u8 soft_del:1; /* it has also been soft deleted */
  7521. + u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory. */
  7522. + u8 fake:1; /* A fake object has no presence on NAND. */
  7523. + u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */
  7524. + u8 unlink_allowed:1;
  7525. + u8 dirty:1; /* the object needs to be written to flash */
  7526. + u8 valid:1; /* When the file system is being loaded up, this
  7527. + * object might be created before the data
  7528. + * is available (ie. file data records appear before the header).
  7529. + */
  7530. + u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */
  7531. +
  7532. + u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is
  7533. + * still in the inode cache. Free of object is defered.
  7534. + * until the inode is released.
  7535. + */
  7536. + u8 being_created:1; /* This object is still being created so skip some checks. */
  7537. + u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */
  7538. +
  7539. + u8 xattr_known:1; /* We know if this has object has xattribs or not. */
  7540. + u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */
  7541. +
  7542. + u8 serial; /* serial number of chunk in NAND. Cached here */
  7543. + u16 sum; /* sum of the name to speed searching */
  7544. +
  7545. + struct yaffs_dev *my_dev; /* The device I'm on */
  7546. +
  7547. + struct list_head hash_link; /* list of objects in this hash bucket */
  7548. +
  7549. + struct list_head hard_links; /* all the equivalent hard linked objects */
  7550. +
  7551. + /* directory structure stuff */
  7552. + /* also used for linking up the free list */
  7553. + struct yaffs_obj *parent;
  7554. + struct list_head siblings;
  7555. +
  7556. + /* Where's my object header in NAND? */
  7557. + int hdr_chunk;
  7558. +
  7559. + int n_data_chunks; /* Number of data chunks attached to the file. */
  7560. +
  7561. + u32 obj_id; /* the object id value */
  7562. +
  7563. + u32 yst_mode;
  7564. +
  7565. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  7566. + YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
  7567. +#endif
  7568. +
  7569. +#ifdef CONFIG_YAFFS_WINCE
  7570. + u32 win_ctime[2];
  7571. + u32 win_mtime[2];
  7572. + u32 win_atime[2];
  7573. +#else
  7574. + u32 yst_uid;
  7575. + u32 yst_gid;
  7576. + u32 yst_atime;
  7577. + u32 yst_mtime;
  7578. + u32 yst_ctime;
  7579. +#endif
  7580. +
  7581. + u32 yst_rdev;
  7582. +
  7583. + void *my_inode;
  7584. +
  7585. + enum yaffs_obj_type variant_type;
  7586. +
  7587. + union yaffs_obj_var variant;
  7588. +
  7589. +};
  7590. +
  7591. +struct yaffs_obj_bucket {
  7592. + struct list_head list;
  7593. + int count;
  7594. +};
  7595. +
  7596. +/* yaffs_checkpt_obj holds the definition of an object as dumped
  7597. + * by checkpointing.
  7598. + */
  7599. +
  7600. +struct yaffs_checkpt_obj {
  7601. + int struct_type;
  7602. + u32 obj_id;
  7603. + u32 parent_id;
  7604. + int hdr_chunk;
  7605. + enum yaffs_obj_type variant_type:3;
  7606. + u8 deleted:1;
  7607. + u8 soft_del:1;
  7608. + u8 unlinked:1;
  7609. + u8 fake:1;
  7610. + u8 rename_allowed:1;
  7611. + u8 unlink_allowed:1;
  7612. + u8 serial;
  7613. + int n_data_chunks;
  7614. + u32 size_or_equiv_obj;
  7615. +};
  7616. +
  7617. +/*--------------------- Temporary buffers ----------------
  7618. + *
  7619. + * These are chunk-sized working buffers. Each device has a few
  7620. + */
  7621. +
  7622. +struct yaffs_buffer {
  7623. + u8 *buffer;
  7624. + int line; /* track from whence this buffer was allocated */
  7625. + int max_line;
  7626. +};
  7627. +
  7628. +/*----------------- Device ---------------------------------*/
  7629. +
  7630. +struct yaffs_param {
  7631. + const YCHAR *name;
  7632. +
  7633. + /*
  7634. + * Entry parameters set up way early. Yaffs sets up the rest.
  7635. + * The structure should be zeroed out before use so that unused
  7636. + * and defualt values are zero.
  7637. + */
  7638. +
  7639. + int inband_tags; /* Use unband tags */
  7640. + u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */
  7641. + int chunks_per_block; /* does not need to be a power of 2 */
  7642. + int spare_bytes_per_chunk; /* spare area size */
  7643. + int start_block; /* Start block we're allowed to use */
  7644. + int end_block; /* End block we're allowed to use */
  7645. + int n_reserved_blocks; /* We want this tuneable so that we can reduce */
  7646. + /* reserved blocks on NOR and RAM. */
  7647. +
  7648. + int n_caches; /* If <= 0, then short op caching is disabled, else
  7649. + * the number of short op caches (don't use too many).
  7650. + * 10 to 20 is a good bet.
  7651. + */
  7652. + int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */
  7653. + int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */
  7654. +
  7655. + int is_yaffs2; /* Use yaffs2 mode on this device */
  7656. +
  7657. + int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
  7658. +
  7659. + int refresh_period; /* How often we should check to do a block refresh */
  7660. +
  7661. + /* Checkpoint control. Can be set before or after initialisation */
  7662. + u8 skip_checkpt_rd;
  7663. + u8 skip_checkpt_wr;
  7664. +
  7665. + int enable_xattr; /* Enable xattribs */
  7666. +
  7667. + /* NAND access functions (Must be set before calling YAFFS) */
  7668. +
  7669. + int (*write_chunk_fn) (struct yaffs_dev * dev,
  7670. + int nand_chunk, const u8 * data,
  7671. + const struct yaffs_spare * spare);
  7672. + int (*read_chunk_fn) (struct yaffs_dev * dev,
  7673. + int nand_chunk, u8 * data,
  7674. + struct yaffs_spare * spare);
  7675. + int (*erase_fn) (struct yaffs_dev * dev, int flash_block);
  7676. + int (*initialise_flash_fn) (struct yaffs_dev * dev);
  7677. + int (*deinitialise_flash_fn) (struct yaffs_dev * dev);
  7678. +
  7679. +#ifdef CONFIG_YAFFS_YAFFS2
  7680. + int (*write_chunk_tags_fn) (struct yaffs_dev * dev,
  7681. + int nand_chunk, const u8 * data,
  7682. + const struct yaffs_ext_tags * tags);
  7683. + int (*read_chunk_tags_fn) (struct yaffs_dev * dev,
  7684. + int nand_chunk, u8 * data,
  7685. + struct yaffs_ext_tags * tags);
  7686. + int (*bad_block_fn) (struct yaffs_dev * dev, int block_no);
  7687. + int (*query_block_fn) (struct yaffs_dev * dev, int block_no,
  7688. + enum yaffs_block_state * state,
  7689. + u32 * seq_number);
  7690. +#endif
  7691. +
  7692. + /* The remove_obj_fn function must be supplied by OS flavours that
  7693. + * need it.
  7694. + * yaffs direct uses it to implement the faster readdir.
  7695. + * Linux uses it to protect the directory during unlocking.
  7696. + */
  7697. + void (*remove_obj_fn) (struct yaffs_obj * obj);
  7698. +
  7699. + /* Callback to mark the superblock dirty */
  7700. + void (*sb_dirty_fn) (struct yaffs_dev * dev);
  7701. +
  7702. + /* Callback to control garbage collection. */
  7703. + unsigned (*gc_control) (struct yaffs_dev * dev);
  7704. +
  7705. + /* Debug control flags. Don't use unless you know what you're doing */
  7706. + int use_header_file_size; /* Flag to determine if we should use file sizes from the header */
  7707. + int disable_lazy_load; /* Disable lazy loading on this device */
  7708. + int wide_tnodes_disabled; /* Set to disable wide tnodes */
  7709. + int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */
  7710. +
  7711. + int defered_dir_update; /* Set to defer directory updates */
  7712. +
  7713. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  7714. + int auto_unicode;
  7715. +#endif
  7716. + int always_check_erased; /* Force chunk erased check always on */
  7717. +};
  7718. +
  7719. +struct yaffs_dev {
  7720. + struct yaffs_param param;
  7721. +
  7722. + /* Context storage. Holds extra OS specific data for this device */
  7723. +
  7724. + void *os_context;
  7725. + void *driver_context;
  7726. +
  7727. + struct list_head dev_list;
  7728. +
  7729. + /* Runtime parameters. Set up by YAFFS. */
  7730. + int data_bytes_per_chunk;
  7731. +
  7732. + /* Non-wide tnode stuff */
  7733. + u16 chunk_grp_bits; /* Number of bits that need to be resolved if
  7734. + * the tnodes are not wide enough.
  7735. + */
  7736. + u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
  7737. +
  7738. + /* Stuff to support wide tnodes */
  7739. + u32 tnode_width;
  7740. + u32 tnode_mask;
  7741. + u32 tnode_size;
  7742. +
  7743. + /* Stuff for figuring out file offset to chunk conversions */
  7744. + u32 chunk_shift; /* Shift value */
  7745. + u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */
  7746. + u32 chunk_mask; /* Mask to use for power-of-2 case */
  7747. +
  7748. + int is_mounted;
  7749. + int read_only;
  7750. + int is_checkpointed;
  7751. +
  7752. + /* Stuff to support block offsetting to support start block zero */
  7753. + int internal_start_block;
  7754. + int internal_end_block;
  7755. + int block_offset;
  7756. + int chunk_offset;
  7757. +
  7758. + /* Runtime checkpointing stuff */
  7759. + int checkpt_page_seq; /* running sequence number of checkpoint pages */
  7760. + int checkpt_byte_count;
  7761. + int checkpt_byte_offs;
  7762. + u8 *checkpt_buffer;
  7763. + int checkpt_open_write;
  7764. + int blocks_in_checkpt;
  7765. + int checkpt_cur_chunk;
  7766. + int checkpt_cur_block;
  7767. + int checkpt_next_block;
  7768. + int *checkpt_block_list;
  7769. + int checkpt_max_blocks;
  7770. + u32 checkpt_sum;
  7771. + u32 checkpt_xor;
  7772. +
  7773. + int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */
  7774. +
  7775. + /* Block Info */
  7776. + struct yaffs_block_info *block_info;
  7777. + u8 *chunk_bits; /* bitmap of chunks in use */
  7778. + unsigned block_info_alt:1; /* was allocated using alternative strategy */
  7779. + unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */
  7780. + int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
  7781. + * Must be consistent with chunks_per_block.
  7782. + */
  7783. +
  7784. + int n_erased_blocks;
  7785. + int alloc_block; /* Current block being allocated off */
  7786. + u32 alloc_page;
  7787. + int alloc_block_finder; /* Used to search for next allocation block */
  7788. +
  7789. + /* Object and Tnode memory management */
  7790. + void *allocator;
  7791. + int n_obj;
  7792. + int n_tnodes;
  7793. +
  7794. + int n_hardlinks;
  7795. +
  7796. + struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
  7797. + u32 bucket_finder;
  7798. +
  7799. + int n_free_chunks;
  7800. +
  7801. + /* Garbage collection control */
  7802. + u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
  7803. + u32 n_clean_ups;
  7804. +
  7805. + unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */
  7806. + unsigned gc_disable;
  7807. + unsigned gc_block_finder;
  7808. + unsigned gc_dirtiest;
  7809. + unsigned gc_pages_in_use;
  7810. + unsigned gc_not_done;
  7811. + unsigned gc_block;
  7812. + unsigned gc_chunk;
  7813. + unsigned gc_skip;
  7814. +
  7815. + /* Special directories */
  7816. + struct yaffs_obj *root_dir;
  7817. + struct yaffs_obj *lost_n_found;
  7818. +
  7819. + /* Buffer areas for storing data to recover from write failures TODO
  7820. + * u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
  7821. + * struct yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK];
  7822. + */
  7823. +
  7824. + int buffered_block; /* Which block is buffered here? */
  7825. + int doing_buffered_block_rewrite;
  7826. +
  7827. + struct yaffs_cache *cache;
  7828. + int cache_last_use;
  7829. +
  7830. + /* Stuff for background deletion and unlinked files. */
  7831. + struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted files live. */
  7832. + struct yaffs_obj *del_dir; /* Directory where deleted objects are sent to disappear. */
  7833. + struct yaffs_obj *unlinked_deletion; /* Current file being background deleted. */
  7834. + int n_deleted_files; /* Count of files awaiting deletion; */
  7835. + int n_unlinked_files; /* Count of unlinked files. */
  7836. + int n_bg_deletions; /* Count of background deletions. */
  7837. +
  7838. + /* Temporary buffer management */
  7839. + struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
  7840. + int max_temp;
  7841. + int temp_in_use;
  7842. + int unmanaged_buffer_allocs;
  7843. + int unmanaged_buffer_deallocs;
  7844. +
  7845. + /* yaffs2 runtime stuff */
  7846. + unsigned seq_number; /* Sequence number of currently allocating block */
  7847. + unsigned oldest_dirty_seq;
  7848. + unsigned oldest_dirty_block;
  7849. +
  7850. + /* Block refreshing */
  7851. + int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */
  7852. +
  7853. + /* Dirty directory handling */
  7854. + struct list_head dirty_dirs; /* List of dirty directories */
  7855. +
  7856. + /* Statistcs */
  7857. + u32 n_page_writes;
  7858. + u32 n_page_reads;
  7859. + u32 n_erasures;
  7860. + u32 n_erase_failures;
  7861. + u32 n_gc_copies;
  7862. + u32 all_gcs;
  7863. + u32 passive_gc_count;
  7864. + u32 oldest_dirty_gc_count;
  7865. + u32 n_gc_blocks;
  7866. + u32 bg_gcs;
  7867. + u32 n_retired_writes;
  7868. + u32 n_retired_blocks;
  7869. + u32 n_ecc_fixed;
  7870. + u32 n_ecc_unfixed;
  7871. + u32 n_tags_ecc_fixed;
  7872. + u32 n_tags_ecc_unfixed;
  7873. + u32 n_deletions;
  7874. + u32 n_unmarked_deletions;
  7875. + u32 refresh_count;
  7876. + u32 cache_hits;
  7877. +
  7878. +};
  7879. +
  7880. +/* The CheckpointDevice structure holds the device information that changes at runtime and
  7881. + * must be preserved over unmount/mount cycles.
  7882. + */
  7883. +struct yaffs_checkpt_dev {
  7884. + int struct_type;
  7885. + int n_erased_blocks;
  7886. + int alloc_block; /* Current block being allocated off */
  7887. + u32 alloc_page;
  7888. + int n_free_chunks;
  7889. +
  7890. + int n_deleted_files; /* Count of files awaiting deletion; */
  7891. + int n_unlinked_files; /* Count of unlinked files. */
  7892. + int n_bg_deletions; /* Count of background deletions. */
  7893. +
  7894. + /* yaffs2 runtime stuff */
  7895. + unsigned seq_number; /* Sequence number of currently allocating block */
  7896. +
  7897. +};
  7898. +
  7899. +struct yaffs_checkpt_validity {
  7900. + int struct_type;
  7901. + u32 magic;
  7902. + u32 version;
  7903. + u32 head;
  7904. +};
  7905. +
  7906. +struct yaffs_shadow_fixer {
  7907. + int obj_id;
  7908. + int shadowed_id;
  7909. + struct yaffs_shadow_fixer *next;
  7910. +};
  7911. +
  7912. +/* Structure for doing xattr modifications */
  7913. +struct yaffs_xattr_mod {
  7914. + int set; /* If 0 then this is a deletion */
  7915. + const YCHAR *name;
  7916. + const void *data;
  7917. + int size;
  7918. + int flags;
  7919. + int result;
  7920. +};
  7921. +
  7922. +/*----------------------- YAFFS Functions -----------------------*/
  7923. +
  7924. +int yaffs_guts_initialise(struct yaffs_dev *dev);
  7925. +void yaffs_deinitialise(struct yaffs_dev *dev);
  7926. +
  7927. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
  7928. +
  7929. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  7930. + struct yaffs_obj *new_dir, const YCHAR * new_name);
  7931. +
  7932. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
  7933. +int yaffs_del_obj(struct yaffs_obj *obj);
  7934. +
  7935. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
  7936. +int yaffs_get_obj_length(struct yaffs_obj *obj);
  7937. +int yaffs_get_obj_inode(struct yaffs_obj *obj);
  7938. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
  7939. +int yaffs_get_obj_link_count(struct yaffs_obj *obj);
  7940. +
  7941. +/* File operations */
  7942. +int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
  7943. + int n_bytes);
  7944. +int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
  7945. + int n_bytes, int write_trhrough);
  7946. +int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
  7947. +
  7948. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  7949. + const YCHAR * name, u32 mode, u32 uid,
  7950. + u32 gid);
  7951. +
  7952. +int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync);
  7953. +
  7954. +/* Flushing and checkpointing */
  7955. +void yaffs_flush_whole_cache(struct yaffs_dev *dev);
  7956. +
  7957. +int yaffs_checkpoint_save(struct yaffs_dev *dev);
  7958. +int yaffs_checkpoint_restore(struct yaffs_dev *dev);
  7959. +
  7960. +/* Directory operations */
  7961. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  7962. + u32 mode, u32 uid, u32 gid);
  7963. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
  7964. + const YCHAR * name);
  7965. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
  7966. +
  7967. +/* Link operations */
  7968. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  7969. + struct yaffs_obj *equiv_obj);
  7970. +
  7971. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
  7972. +
  7973. +/* Symlink operations */
  7974. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  7975. + const YCHAR * name, u32 mode, u32 uid,
  7976. + u32 gid, const YCHAR * alias);
  7977. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
  7978. +
  7979. +/* Special inodes (fifos, sockets and devices) */
  7980. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  7981. + const YCHAR * name, u32 mode, u32 uid,
  7982. + u32 gid, u32 rdev);
  7983. +
  7984. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  7985. + const void *value, int size, int flags);
  7986. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  7987. + int size);
  7988. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
  7989. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name);
  7990. +
  7991. +/* Special directories */
  7992. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
  7993. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
  7994. +
  7995. +void yaffs_handle_defered_free(struct yaffs_obj *obj);
  7996. +
  7997. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
  7998. +
  7999. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
  8000. +
  8001. +/* Debug dump */
  8002. +int yaffs_dump_obj(struct yaffs_obj *obj);
  8003. +
  8004. +void yaffs_guts_test(struct yaffs_dev *dev);
  8005. +
  8006. +/* A few useful functions to be used within the core files*/
  8007. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  8008. + int lyn);
  8009. +int yaffs_check_ff(u8 * buffer, int n_bytes);
  8010. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  8011. + struct yaffs_block_info *bi);
  8012. +
  8013. +u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no);
  8014. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no);
  8015. +
  8016. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  8017. + int number,
  8018. + enum yaffs_obj_type type);
  8019. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  8020. + int nand_chunk, int in_scan);
  8021. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name);
  8022. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  8023. + const struct yaffs_obj_hdr *oh);
  8024. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
  8025. +YCHAR *yaffs_clone_str(const YCHAR * str);
  8026. +void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list);
  8027. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
  8028. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name,
  8029. + int force, int is_shrink, int shadows,
  8030. + struct yaffs_xattr_mod *xop);
  8031. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  8032. + int backward_scanning);
  8033. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
  8034. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
  8035. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  8036. + struct yaffs_file_var *file_struct,
  8037. + u32 chunk_id,
  8038. + struct yaffs_tnode *passed_tn);
  8039. +
  8040. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  8041. + int n_bytes, int write_trhrough);
  8042. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
  8043. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
  8044. +
  8045. +int yaffs_count_free_chunks(struct yaffs_dev *dev);
  8046. +
  8047. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  8048. + struct yaffs_file_var *file_struct,
  8049. + u32 chunk_id);
  8050. +
  8051. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  8052. + unsigned pos);
  8053. +
  8054. +#endif
  8055. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_linux.h linux-2.6.36/fs/yaffs2/yaffs_linux.h
  8056. --- linux-2.6.36.orig/fs/yaffs2/yaffs_linux.h 1970-01-01 01:00:00.000000000 +0100
  8057. +++ linux-2.6.36/fs/yaffs2/yaffs_linux.h 2010-11-18 18:15:51.000000000 +0100
  8058. @@ -0,0 +1,41 @@
  8059. +/*
  8060. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8061. + *
  8062. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8063. + * for Toby Churchill Ltd and Brightstar Engineering
  8064. + *
  8065. + * Created by Charles Manning <charles@aleph1.co.uk>
  8066. + *
  8067. + * This program is free software; you can redistribute it and/or modify
  8068. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8069. + * published by the Free Software Foundation.
  8070. + *
  8071. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8072. + */
  8073. +
  8074. +#ifndef __YAFFS_LINUX_H__
  8075. +#define __YAFFS_LINUX_H__
  8076. +
  8077. +#include "yportenv.h"
  8078. +
  8079. +struct yaffs_linux_context {
  8080. + struct list_head context_list; /* List of these we have mounted */
  8081. + struct yaffs_dev *dev;
  8082. + struct super_block *super;
  8083. + struct task_struct *bg_thread; /* Background thread for this device */
  8084. + int bg_running;
  8085. + struct mutex gross_lock; /* Gross locking mutex*/
  8086. + u8 *spare_buffer; /* For mtdif2 use. Don't know the size of the buffer
  8087. + * at compile time so we have to allocate it.
  8088. + */
  8089. + struct list_head search_contexts;
  8090. + void (*put_super_fn) (struct super_block * sb);
  8091. +
  8092. + struct task_struct *readdir_process;
  8093. + unsigned mount_id;
  8094. +};
  8095. +
  8096. +#define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
  8097. +#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
  8098. +
  8099. +#endif
  8100. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.c linux-2.6.36/fs/yaffs2/yaffs_mtdif.c
  8101. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  8102. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif.c 2010-11-18 18:15:51.000000000 +0100
  8103. @@ -0,0 +1,54 @@
  8104. +/*
  8105. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8106. + *
  8107. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8108. + * for Toby Churchill Ltd and Brightstar Engineering
  8109. + *
  8110. + * Created by Charles Manning <charles@aleph1.co.uk>
  8111. + *
  8112. + * This program is free software; you can redistribute it and/or modify
  8113. + * it under the terms of the GNU General Public License version 2 as
  8114. + * published by the Free Software Foundation.
  8115. + */
  8116. +
  8117. +#include "yportenv.h"
  8118. +
  8119. +#include "yaffs_mtdif.h"
  8120. +
  8121. +#include "linux/mtd/mtd.h"
  8122. +#include "linux/types.h"
  8123. +#include "linux/time.h"
  8124. +#include "linux/mtd/nand.h"
  8125. +
  8126. +#include "yaffs_linux.h"
  8127. +
  8128. +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
  8129. +{
  8130. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8131. + u32 addr =
  8132. + ((loff_t) block_no) * dev->param.total_bytes_per_chunk
  8133. + * dev->param.chunks_per_block;
  8134. + struct erase_info ei;
  8135. +
  8136. + int retval = 0;
  8137. +
  8138. + ei.mtd = mtd;
  8139. + ei.addr = addr;
  8140. + ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
  8141. + ei.time = 1000;
  8142. + ei.retries = 2;
  8143. + ei.callback = NULL;
  8144. + ei.priv = (u_long) dev;
  8145. +
  8146. + retval = mtd->erase(mtd, &ei);
  8147. +
  8148. + if (retval == 0)
  8149. + return YAFFS_OK;
  8150. + else
  8151. + return YAFFS_FAIL;
  8152. +}
  8153. +
  8154. +int nandmtd_initialise(struct yaffs_dev *dev)
  8155. +{
  8156. + return YAFFS_OK;
  8157. +}
  8158. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.h linux-2.6.36/fs/yaffs2/yaffs_mtdif.h
  8159. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  8160. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif.h 2010-11-18 18:15:51.000000000 +0100
  8161. @@ -0,0 +1,23 @@
  8162. +/*
  8163. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8164. + *
  8165. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8166. + * for Toby Churchill Ltd and Brightstar Engineering
  8167. + *
  8168. + * Created by Charles Manning <charles@aleph1.co.uk>
  8169. + *
  8170. + * This program is free software; you can redistribute it and/or modify
  8171. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8172. + * published by the Free Software Foundation.
  8173. + *
  8174. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8175. + */
  8176. +
  8177. +#ifndef __YAFFS_MTDIF_H__
  8178. +#define __YAFFS_MTDIF_H__
  8179. +
  8180. +#include "yaffs_guts.h"
  8181. +
  8182. +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no);
  8183. +int nandmtd_initialise(struct yaffs_dev *dev);
  8184. +#endif
  8185. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.c linux-2.6.36/fs/yaffs2/yaffs_mtdif1.c
  8186. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
  8187. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif1.c 2010-11-18 18:15:51.000000000 +0100
  8188. @@ -0,0 +1,365 @@
  8189. +/*
  8190. + * YAFFS: Yet another FFS. A NAND-flash specific file system.
  8191. + *
  8192. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8193. + * for Toby Churchill Ltd and Brightstar Engineering
  8194. + *
  8195. + * Created by Charles Manning <charles@aleph1.co.uk>
  8196. + *
  8197. + * This program is free software; you can redistribute it and/or modify
  8198. + * it under the terms of the GNU General Public License version 2 as
  8199. + * published by the Free Software Foundation.
  8200. + */
  8201. +
  8202. +/*
  8203. + * This module provides the interface between yaffs_nand.c and the
  8204. + * MTD API. This version is used when the MTD interface supports the
  8205. + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
  8206. + * and we have small-page NAND device.
  8207. + *
  8208. + * These functions are invoked via function pointers in yaffs_nand.c.
  8209. + * This replaces functionality provided by functions in yaffs_mtdif.c
  8210. + * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are
  8211. + * called in yaffs_mtdif.c when the function pointers are NULL.
  8212. + * We assume the MTD layer is performing ECC (use_nand_ecc is true).
  8213. + */
  8214. +
  8215. +#include "yportenv.h"
  8216. +#include "yaffs_trace.h"
  8217. +#include "yaffs_guts.h"
  8218. +#include "yaffs_packedtags1.h"
  8219. +#include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
  8220. +#include "yaffs_linux.h"
  8221. +
  8222. +#include "linux/kernel.h"
  8223. +#include "linux/version.h"
  8224. +#include "linux/types.h"
  8225. +#include "linux/mtd/mtd.h"
  8226. +
  8227. +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
  8228. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8229. +
  8230. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8231. +# define YTAG1_SIZE 8
  8232. +#else
  8233. +# define YTAG1_SIZE 9
  8234. +#endif
  8235. +
  8236. +#if 0
  8237. +/* Use the following nand_ecclayout with MTD when using
  8238. + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
  8239. + * If you have existing Yaffs images and the byte order differs from this,
  8240. + * adjust 'oobfree' to match your existing Yaffs data.
  8241. + *
  8242. + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
  8243. + * page_status byte (at NAND spare offset 4) scattered/gathered from/to
  8244. + * the 9th byte.
  8245. + *
  8246. + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
  8247. + * We have/need packed_tags1 plus page_status: T0,T1,T2,T3,T4,T5,T6,T7,P
  8248. + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the page_status
  8249. + * byte and B is the small-page bad-block indicator byte.
  8250. + */
  8251. +static struct nand_ecclayout nand_oob_16 = {
  8252. + .eccbytes = 6,
  8253. + .eccpos = {8, 9, 10, 13, 14, 15},
  8254. + .oobavail = 9,
  8255. + .oobfree = {{0, 4}, {6, 2}, {11, 2}, {4, 1}}
  8256. +};
  8257. +#endif
  8258. +
  8259. +/* Write a chunk (page) of data to NAND.
  8260. + *
  8261. + * Caller always provides ExtendedTags data which are converted to a more
  8262. + * compact (packed) form for storage in NAND. A mini-ECC runs over the
  8263. + * contents of the tags meta-data; used to valid the tags when read.
  8264. + *
  8265. + * - Pack ExtendedTags to packed_tags1 form
  8266. + * - Compute mini-ECC for packed_tags1
  8267. + * - Write data and packed tags to NAND.
  8268. + *
  8269. + * Note: Due to the use of the packed_tags1 meta-data which does not include
  8270. + * a full sequence number (as found in the larger packed_tags2 form) it is
  8271. + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
  8272. + * discarded and dirty. This is not ideal: newer NAND parts are supposed
  8273. + * to be written just once. When Yaffs performs this operation, this
  8274. + * function is called with a NULL data pointer -- calling MTD write_oob
  8275. + * without data is valid usage (2.6.17).
  8276. + *
  8277. + * Any underlying MTD error results in YAFFS_FAIL.
  8278. + * Returns YAFFS_OK or YAFFS_FAIL.
  8279. + */
  8280. +int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
  8281. + int nand_chunk, const u8 * data,
  8282. + const struct yaffs_ext_tags *etags)
  8283. +{
  8284. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8285. + int chunk_bytes = dev->data_bytes_per_chunk;
  8286. + loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
  8287. + struct mtd_oob_ops ops;
  8288. + struct yaffs_packed_tags1 pt1;
  8289. + int retval;
  8290. +
  8291. + /* we assume that packed_tags1 and struct yaffs_tags are compatible */
  8292. + compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
  8293. + compile_time_assertion(sizeof(struct yaffs_tags) == 8);
  8294. +
  8295. + yaffs_pack_tags1(&pt1, etags);
  8296. + yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
  8297. +
  8298. + /* When deleting a chunk, the upper layer provides only skeletal
  8299. + * etags, one with is_deleted set. However, we need to update the
  8300. + * tags, not erase them completely. So we use the NAND write property
  8301. + * that only zeroed-bits stick and set tag bytes to all-ones and
  8302. + * zero just the (not) deleted bit.
  8303. + */
  8304. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8305. + if (etags->is_deleted) {
  8306. + memset(&pt1, 0xff, 8);
  8307. + /* clear delete status bit to indicate deleted */
  8308. + pt1.deleted = 0;
  8309. + }
  8310. +#else
  8311. + ((u8 *) & pt1)[8] = 0xff;
  8312. + if (etags->is_deleted) {
  8313. + memset(&pt1, 0xff, 8);
  8314. + /* zero page_status byte to indicate deleted */
  8315. + ((u8 *) & pt1)[8] = 0;
  8316. + }
  8317. +#endif
  8318. +
  8319. + memset(&ops, 0, sizeof(ops));
  8320. + ops.mode = MTD_OOB_AUTO;
  8321. + ops.len = (data) ? chunk_bytes : 0;
  8322. + ops.ooblen = YTAG1_SIZE;
  8323. + ops.datbuf = (u8 *) data;
  8324. + ops.oobbuf = (u8 *) & pt1;
  8325. +
  8326. + retval = mtd->write_oob(mtd, addr, &ops);
  8327. + if (retval) {
  8328. + T(YAFFS_TRACE_MTD,
  8329. + (TSTR("write_oob failed, chunk %d, mtd error %d" TENDSTR),
  8330. + nand_chunk, retval));
  8331. + }
  8332. + return retval ? YAFFS_FAIL : YAFFS_OK;
  8333. +}
  8334. +
  8335. +/* Return with empty ExtendedTags but add ecc_result.
  8336. + */
  8337. +static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
  8338. +{
  8339. + if (etags) {
  8340. + memset(etags, 0, sizeof(*etags));
  8341. + etags->ecc_result = ecc_result;
  8342. + }
  8343. + return retval;
  8344. +}
  8345. +
  8346. +/* Read a chunk (page) from NAND.
  8347. + *
  8348. + * Caller expects ExtendedTags data to be usable even on error; that is,
  8349. + * all members except ecc_result and block_bad are zeroed.
  8350. + *
  8351. + * - Check ECC results for data (if applicable)
  8352. + * - Check for blank/erased block (return empty ExtendedTags if blank)
  8353. + * - Check the packed_tags1 mini-ECC (correct if necessary/possible)
  8354. + * - Convert packed_tags1 to ExtendedTags
  8355. + * - Update ecc_result and block_bad members to refect state.
  8356. + *
  8357. + * Returns YAFFS_OK or YAFFS_FAIL.
  8358. + */
  8359. +int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
  8360. + int nand_chunk, u8 * data,
  8361. + struct yaffs_ext_tags *etags)
  8362. +{
  8363. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8364. + int chunk_bytes = dev->data_bytes_per_chunk;
  8365. + loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
  8366. + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
  8367. + struct mtd_oob_ops ops;
  8368. + struct yaffs_packed_tags1 pt1;
  8369. + int retval;
  8370. + int deleted;
  8371. +
  8372. + memset(&ops, 0, sizeof(ops));
  8373. + ops.mode = MTD_OOB_AUTO;
  8374. + ops.len = (data) ? chunk_bytes : 0;
  8375. + ops.ooblen = YTAG1_SIZE;
  8376. + ops.datbuf = data;
  8377. + ops.oobbuf = (u8 *) & pt1;
  8378. +
  8379. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
  8380. + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
  8381. + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
  8382. + */
  8383. + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
  8384. +#endif
  8385. + /* Read page and oob using MTD.
  8386. + * Check status and determine ECC result.
  8387. + */
  8388. + retval = mtd->read_oob(mtd, addr, &ops);
  8389. + if (retval) {
  8390. + T(YAFFS_TRACE_MTD,
  8391. + (TSTR("read_oob failed, chunk %d, mtd error %d" TENDSTR),
  8392. + nand_chunk, retval));
  8393. + }
  8394. +
  8395. + switch (retval) {
  8396. + case 0:
  8397. + /* no error */
  8398. + break;
  8399. +
  8400. + case -EUCLEAN:
  8401. + /* MTD's ECC fixed the data */
  8402. + eccres = YAFFS_ECC_RESULT_FIXED;
  8403. + dev->n_ecc_fixed++;
  8404. + break;
  8405. +
  8406. + case -EBADMSG:
  8407. + /* MTD's ECC could not fix the data */
  8408. + dev->n_ecc_unfixed++;
  8409. + /* fall into... */
  8410. + default:
  8411. + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
  8412. + etags->block_bad = (mtd->block_isbad) (mtd, addr);
  8413. + return YAFFS_FAIL;
  8414. + }
  8415. +
  8416. + /* Check for a blank/erased chunk.
  8417. + */
  8418. + if (yaffs_check_ff((u8 *) & pt1, 8)) {
  8419. + /* when blank, upper layers want ecc_result to be <= NO_ERROR */
  8420. + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
  8421. + }
  8422. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8423. + /* Read deleted status (bit) then return it to it's non-deleted
  8424. + * state before performing tags mini-ECC check. pt1.deleted is
  8425. + * inverted.
  8426. + */
  8427. + deleted = !pt1.deleted;
  8428. + pt1.deleted = 1;
  8429. +#else
  8430. + deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7);
  8431. +#endif
  8432. +
  8433. + /* Check the packed tags mini-ECC and correct if necessary/possible.
  8434. + */
  8435. + retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1);
  8436. + switch (retval) {
  8437. + case 0:
  8438. + /* no tags error, use MTD result */
  8439. + break;
  8440. + case 1:
  8441. + /* recovered tags-ECC error */
  8442. + dev->n_tags_ecc_fixed++;
  8443. + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
  8444. + eccres = YAFFS_ECC_RESULT_FIXED;
  8445. + break;
  8446. + default:
  8447. + /* unrecovered tags-ECC error */
  8448. + dev->n_tags_ecc_unfixed++;
  8449. + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
  8450. + }
  8451. +
  8452. + /* Unpack the tags to extended form and set ECC result.
  8453. + * [set should_be_ff just to keep yaffs_unpack_tags1 happy]
  8454. + */
  8455. + pt1.should_be_ff = 0xFFFFFFFF;
  8456. + yaffs_unpack_tags1(etags, &pt1);
  8457. + etags->ecc_result = eccres;
  8458. +
  8459. + /* Set deleted state */
  8460. + etags->is_deleted = deleted;
  8461. + return YAFFS_OK;
  8462. +}
  8463. +
  8464. +/* Mark a block bad.
  8465. + *
  8466. + * This is a persistant state.
  8467. + * Use of this function should be rare.
  8468. + *
  8469. + * Returns YAFFS_OK or YAFFS_FAIL.
  8470. + */
  8471. +int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no)
  8472. +{
  8473. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8474. + int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
  8475. + int retval;
  8476. +
  8477. + T(YAFFS_TRACE_BAD_BLOCKS,
  8478. + (TSTR("marking block %d bad" TENDSTR), block_no));
  8479. +
  8480. + retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no);
  8481. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  8482. +}
  8483. +
  8484. +/* Check any MTD prerequists.
  8485. + *
  8486. + * Returns YAFFS_OK or YAFFS_FAIL.
  8487. + */
  8488. +static int nandmtd1_test_prerequists(struct mtd_info *mtd)
  8489. +{
  8490. + /* 2.6.18 has mtd->ecclayout->oobavail */
  8491. + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
  8492. + int oobavail = mtd->ecclayout->oobavail;
  8493. +
  8494. + if (oobavail < YTAG1_SIZE) {
  8495. + T(YAFFS_TRACE_ERROR,
  8496. + (TSTR
  8497. + ("mtd device has only %d bytes for tags, need %d" TENDSTR),
  8498. + oobavail, YTAG1_SIZE));
  8499. + return YAFFS_FAIL;
  8500. + }
  8501. + return YAFFS_OK;
  8502. +}
  8503. +
  8504. +/* Query for the current state of a specific block.
  8505. + *
  8506. + * Examine the tags of the first chunk of the block and return the state:
  8507. + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
  8508. + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
  8509. + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
  8510. + *
  8511. + * Always returns YAFFS_OK.
  8512. + */
  8513. +int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
  8514. + enum yaffs_block_state *state_ptr, u32 * seq_ptr)
  8515. +{
  8516. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8517. + int chunk_num = block_no * dev->param.chunks_per_block;
  8518. + loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
  8519. + struct yaffs_ext_tags etags;
  8520. + int state = YAFFS_BLOCK_STATE_DEAD;
  8521. + int seqnum = 0;
  8522. + int retval;
  8523. +
  8524. + /* We don't yet have a good place to test for MTD config prerequists.
  8525. + * Do it here as we are called during the initial scan.
  8526. + */
  8527. + if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
  8528. + return YAFFS_FAIL;
  8529. +
  8530. + retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
  8531. + etags.block_bad = (mtd->block_isbad) (mtd, addr);
  8532. + if (etags.block_bad) {
  8533. + T(YAFFS_TRACE_BAD_BLOCKS,
  8534. + (TSTR("block %d is marked bad" TENDSTR), block_no));
  8535. + state = YAFFS_BLOCK_STATE_DEAD;
  8536. + } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
  8537. + /* bad tags, need to look more closely */
  8538. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8539. + } else if (etags.chunk_used) {
  8540. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8541. + seqnum = etags.seq_number;
  8542. + } else {
  8543. + state = YAFFS_BLOCK_STATE_EMPTY;
  8544. + }
  8545. +
  8546. + *state_ptr = state;
  8547. + *seq_ptr = seqnum;
  8548. +
  8549. + /* query always succeeds */
  8550. + return YAFFS_OK;
  8551. +}
  8552. +
  8553. +#endif /*MTD_VERSION */
  8554. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.h linux-2.6.36/fs/yaffs2/yaffs_mtdif1.h
  8555. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
  8556. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif1.h 2010-11-18 18:15:51.000000000 +0100
  8557. @@ -0,0 +1,29 @@
  8558. +/*
  8559. + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
  8560. + *
  8561. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8562. + * for Toby Churchill Ltd and Brightstar Engineering
  8563. + *
  8564. + * This program is free software; you can redistribute it and/or modify
  8565. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8566. + * published by the Free Software Foundation.
  8567. + *
  8568. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8569. + */
  8570. +
  8571. +#ifndef __YAFFS_MTDIF1_H__
  8572. +#define __YAFFS_MTDIF1_H__
  8573. +
  8574. +int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8575. + const u8 * data,
  8576. + const struct yaffs_ext_tags *tags);
  8577. +
  8578. +int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8579. + u8 * data, struct yaffs_ext_tags *tags);
  8580. +
  8581. +int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no);
  8582. +
  8583. +int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
  8584. + enum yaffs_block_state *state, u32 * seq_number);
  8585. +
  8586. +#endif
  8587. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.c linux-2.6.36/fs/yaffs2/yaffs_mtdif2.c
  8588. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
  8589. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif2.c 2010-11-18 18:15:51.000000000 +0100
  8590. @@ -0,0 +1,261 @@
  8591. +/*
  8592. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8593. + *
  8594. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8595. + * for Toby Churchill Ltd and Brightstar Engineering
  8596. + *
  8597. + * Created by Charles Manning <charles@aleph1.co.uk>
  8598. + *
  8599. + * This program is free software; you can redistribute it and/or modify
  8600. + * it under the terms of the GNU General Public License version 2 as
  8601. + * published by the Free Software Foundation.
  8602. + */
  8603. +
  8604. +/* mtd interface for YAFFS2 */
  8605. +
  8606. +#include "yportenv.h"
  8607. +#include "yaffs_trace.h"
  8608. +
  8609. +#include "yaffs_mtdif2.h"
  8610. +
  8611. +#include "linux/mtd/mtd.h"
  8612. +#include "linux/types.h"
  8613. +#include "linux/time.h"
  8614. +
  8615. +#include "yaffs_packedtags2.h"
  8616. +
  8617. +#include "yaffs_linux.h"
  8618. +
  8619. +/* NB For use with inband tags....
  8620. + * We assume that the data buffer is of size total_bytes_per_chunk so that we can also
  8621. + * use it to load the tags.
  8622. + */
  8623. +int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8624. + const u8 * data,
  8625. + const struct yaffs_ext_tags *tags)
  8626. +{
  8627. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8628. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8629. + struct mtd_oob_ops ops;
  8630. +#else
  8631. + size_t dummy;
  8632. +#endif
  8633. + int retval = 0;
  8634. +
  8635. + loff_t addr;
  8636. +
  8637. + struct yaffs_packed_tags2 pt;
  8638. +
  8639. + int packed_tags_size =
  8640. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  8641. + void *packed_tags_ptr =
  8642. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  8643. +
  8644. + T(YAFFS_TRACE_MTD,
  8645. + (TSTR
  8646. + ("nandmtd2_write_chunk_tags chunk %d data %p tags %p"
  8647. + TENDSTR), nand_chunk, data, tags));
  8648. +
  8649. + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8650. +
  8651. + /* For yaffs2 writing there must be both data and tags.
  8652. + * If we're using inband tags, then the tags are stuffed into
  8653. + * the end of the data buffer.
  8654. + */
  8655. + if (!data || !tags)
  8656. + BUG();
  8657. + else if (dev->param.inband_tags) {
  8658. + struct yaffs_packed_tags2_tags_only *pt2tp;
  8659. + pt2tp =
  8660. + (struct yaffs_packed_tags2_tags_only *)(data +
  8661. + dev->
  8662. + data_bytes_per_chunk);
  8663. + yaffs_pack_tags2_tags_only(pt2tp, tags);
  8664. + } else {
  8665. + yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
  8666. + }
  8667. +
  8668. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  8669. + ops.mode = MTD_OOB_AUTO;
  8670. + ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
  8671. + ops.len = dev->param.total_bytes_per_chunk;
  8672. + ops.ooboffs = 0;
  8673. + ops.datbuf = (u8 *) data;
  8674. + ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
  8675. + retval = mtd->write_oob(mtd, addr, &ops);
  8676. +
  8677. +#else
  8678. + if (!dev->param.inband_tags) {
  8679. + retval =
  8680. + mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
  8681. + &dummy, data, (u8 *) packed_tags_ptr, NULL);
  8682. + } else {
  8683. + retval =
  8684. + mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
  8685. + &dummy, data);
  8686. + }
  8687. +#endif
  8688. +
  8689. + if (retval == 0)
  8690. + return YAFFS_OK;
  8691. + else
  8692. + return YAFFS_FAIL;
  8693. +}
  8694. +
  8695. +int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8696. + u8 * data, struct yaffs_ext_tags *tags)
  8697. +{
  8698. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8699. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8700. + struct mtd_oob_ops ops;
  8701. +#endif
  8702. + size_t dummy;
  8703. + int retval = 0;
  8704. + int local_data = 0;
  8705. +
  8706. + loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8707. +
  8708. + struct yaffs_packed_tags2 pt;
  8709. +
  8710. + int packed_tags_size =
  8711. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  8712. + void *packed_tags_ptr =
  8713. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  8714. +
  8715. + T(YAFFS_TRACE_MTD,
  8716. + (TSTR
  8717. + ("nandmtd2_read_chunk_tags chunk %d data %p tags %p"
  8718. + TENDSTR), nand_chunk, data, tags));
  8719. +
  8720. + if (dev->param.inband_tags) {
  8721. +
  8722. + if (!data) {
  8723. + local_data = 1;
  8724. + data = yaffs_get_temp_buffer(dev, __LINE__);
  8725. + }
  8726. +
  8727. + }
  8728. +
  8729. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  8730. + if (dev->param.inband_tags || (data && !tags))
  8731. + retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
  8732. + &dummy, data);
  8733. + else if (tags) {
  8734. + ops.mode = MTD_OOB_AUTO;
  8735. + ops.ooblen = packed_tags_size;
  8736. + ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
  8737. + ops.ooboffs = 0;
  8738. + ops.datbuf = data;
  8739. + ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
  8740. + retval = mtd->read_oob(mtd, addr, &ops);
  8741. + }
  8742. +#else
  8743. + if (!dev->param.inband_tags && data && tags) {
  8744. +
  8745. + retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
  8746. + &dummy, data, dev->spare_buffer, NULL);
  8747. + } else {
  8748. + if (data)
  8749. + retval =
  8750. + mtd->read(mtd, addr, dev->data_bytes_per_chunk,
  8751. + &dummy, data);
  8752. + if (!dev->param.inband_tags && tags)
  8753. + retval =
  8754. + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
  8755. + dev->spare_buffer);
  8756. + }
  8757. +#endif
  8758. +
  8759. + if (dev->param.inband_tags) {
  8760. + if (tags) {
  8761. + struct yaffs_packed_tags2_tags_only *pt2tp;
  8762. + pt2tp =
  8763. + (struct yaffs_packed_tags2_tags_only *)&data[dev->
  8764. + data_bytes_per_chunk];
  8765. + yaffs_unpack_tags2_tags_only(tags, pt2tp);
  8766. + }
  8767. + } else {
  8768. + if (tags) {
  8769. + memcpy(packed_tags_ptr,
  8770. + yaffs_dev_to_lc(dev)->spare_buffer,
  8771. + packed_tags_size);
  8772. + yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  8773. + }
  8774. + }
  8775. +
  8776. + if (local_data)
  8777. + yaffs_release_temp_buffer(dev, data, __LINE__);
  8778. +
  8779. + if (tags && retval == -EBADMSG
  8780. + && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  8781. + tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  8782. + dev->n_ecc_unfixed++;
  8783. + }
  8784. + if (tags && retval == -EUCLEAN
  8785. + && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  8786. + tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  8787. + dev->n_ecc_fixed++;
  8788. + }
  8789. + if (retval == 0)
  8790. + return YAFFS_OK;
  8791. + else
  8792. + return YAFFS_FAIL;
  8793. +}
  8794. +
  8795. +int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
  8796. +{
  8797. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8798. + int retval;
  8799. + T(YAFFS_TRACE_MTD,
  8800. + (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no));
  8801. +
  8802. + retval =
  8803. + mtd->block_markbad(mtd,
  8804. + block_no * dev->param.chunks_per_block *
  8805. + dev->param.total_bytes_per_chunk);
  8806. +
  8807. + if (retval == 0)
  8808. + return YAFFS_OK;
  8809. + else
  8810. + return YAFFS_FAIL;
  8811. +
  8812. +}
  8813. +
  8814. +int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  8815. + enum yaffs_block_state *state, u32 * seq_number)
  8816. +{
  8817. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8818. + int retval;
  8819. +
  8820. + T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no));
  8821. + retval =
  8822. + mtd->block_isbad(mtd,
  8823. + block_no * dev->param.chunks_per_block *
  8824. + dev->param.total_bytes_per_chunk);
  8825. +
  8826. + if (retval) {
  8827. + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
  8828. +
  8829. + *state = YAFFS_BLOCK_STATE_DEAD;
  8830. + *seq_number = 0;
  8831. + } else {
  8832. + struct yaffs_ext_tags t;
  8833. + nandmtd2_read_chunk_tags(dev, block_no *
  8834. + dev->param.chunks_per_block, NULL, &t);
  8835. +
  8836. + if (t.chunk_used) {
  8837. + *seq_number = t.seq_number;
  8838. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8839. + } else {
  8840. + *seq_number = 0;
  8841. + *state = YAFFS_BLOCK_STATE_EMPTY;
  8842. + }
  8843. + }
  8844. + T(YAFFS_TRACE_MTD,
  8845. + (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state));
  8846. +
  8847. + if (retval == 0)
  8848. + return YAFFS_OK;
  8849. + else
  8850. + return YAFFS_FAIL;
  8851. +}
  8852. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.h linux-2.6.36/fs/yaffs2/yaffs_mtdif2.h
  8853. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
  8854. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif2.h 2010-11-18 18:15:51.000000000 +0100
  8855. @@ -0,0 +1,29 @@
  8856. +/*
  8857. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8858. + *
  8859. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8860. + * for Toby Churchill Ltd and Brightstar Engineering
  8861. + *
  8862. + * Created by Charles Manning <charles@aleph1.co.uk>
  8863. + *
  8864. + * This program is free software; you can redistribute it and/or modify
  8865. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8866. + * published by the Free Software Foundation.
  8867. + *
  8868. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8869. + */
  8870. +
  8871. +#ifndef __YAFFS_MTDIF2_H__
  8872. +#define __YAFFS_MTDIF2_H__
  8873. +
  8874. +#include "yaffs_guts.h"
  8875. +int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8876. + const u8 * data,
  8877. + const struct yaffs_ext_tags *tags);
  8878. +int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8879. + u8 * data, struct yaffs_ext_tags *tags);
  8880. +int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no);
  8881. +int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  8882. + enum yaffs_block_state *state, u32 * seq_number);
  8883. +
  8884. +#endif
  8885. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.c linux-2.6.36/fs/yaffs2/yaffs_nameval.c
  8886. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.c 1970-01-01 01:00:00.000000000 +0100
  8887. +++ linux-2.6.36/fs/yaffs2/yaffs_nameval.c 2010-11-18 18:15:51.000000000 +0100
  8888. @@ -0,0 +1,201 @@
  8889. +/*
  8890. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8891. + *
  8892. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8893. + * for Toby Churchill Ltd and Brightstar Engineering
  8894. + *
  8895. + * Created by Charles Manning <charles@aleph1.co.uk>
  8896. + *
  8897. + * This program is free software; you can redistribute it and/or modify
  8898. + * it under the terms of the GNU General Public License version 2 as
  8899. + * published by the Free Software Foundation.
  8900. + */
  8901. +
  8902. +/*
  8903. + * This simple implementation of a name-value store assumes a small number of values and fits
  8904. + * into a small finite buffer.
  8905. + *
  8906. + * Each attribute is stored as a record:
  8907. + * sizeof(int) bytes record size.
  8908. + * strnlen+1 bytes name null terminated.
  8909. + * nbytes value.
  8910. + * ----------
  8911. + * total size stored in record size
  8912. + *
  8913. + * This code has not been tested with unicode yet.
  8914. + */
  8915. +
  8916. +#include "yaffs_nameval.h"
  8917. +
  8918. +#include "yportenv.h"
  8919. +
  8920. +static int nval_find(const char *xb, int xb_size, const YCHAR * name,
  8921. + int *exist_size)
  8922. +{
  8923. + int pos = 0;
  8924. + int size;
  8925. +
  8926. + memcpy(&size, xb, sizeof(int));
  8927. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8928. + if (yaffs_strncmp
  8929. + ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) {
  8930. + if (exist_size)
  8931. + *exist_size = size;
  8932. + return pos;
  8933. + }
  8934. + pos += size;
  8935. + if (pos < xb_size - sizeof(int))
  8936. + memcpy(&size, xb + pos, sizeof(int));
  8937. + else
  8938. + size = 0;
  8939. + }
  8940. + if (exist_size)
  8941. + *exist_size = 0;
  8942. + return -1;
  8943. +}
  8944. +
  8945. +static int nval_used(const char *xb, int xb_size)
  8946. +{
  8947. + int pos = 0;
  8948. + int size;
  8949. +
  8950. + memcpy(&size, xb + pos, sizeof(int));
  8951. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8952. + pos += size;
  8953. + if (pos < xb_size - sizeof(int))
  8954. + memcpy(&size, xb + pos, sizeof(int));
  8955. + else
  8956. + size = 0;
  8957. + }
  8958. + return pos;
  8959. +}
  8960. +
  8961. +int nval_del(char *xb, int xb_size, const YCHAR * name)
  8962. +{
  8963. + int pos = nval_find(xb, xb_size, name, NULL);
  8964. + int size;
  8965. +
  8966. + if (pos >= 0 && pos < xb_size) {
  8967. + /* Find size, shift rest over this record, then zero out the rest of buffer */
  8968. + memcpy(&size, xb + pos, sizeof(int));
  8969. + memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
  8970. + memset(xb + (xb_size - size), 0, size);
  8971. + return 0;
  8972. + } else {
  8973. + return -ENODATA;
  8974. + }
  8975. +}
  8976. +
  8977. +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
  8978. + int bsize, int flags)
  8979. +{
  8980. + int pos;
  8981. + int namelen = yaffs_strnlen(name, xb_size);
  8982. + int reclen;
  8983. + int size_exist = 0;
  8984. + int space;
  8985. + int start;
  8986. +
  8987. + pos = nval_find(xb, xb_size, name, &size_exist);
  8988. +
  8989. + if (flags & XATTR_CREATE && pos >= 0)
  8990. + return -EEXIST;
  8991. + if (flags & XATTR_REPLACE && pos < 0)
  8992. + return -ENODATA;
  8993. +
  8994. + start = nval_used(xb, xb_size);
  8995. + space = xb_size - start + size_exist;
  8996. +
  8997. + reclen = (sizeof(int) + namelen + 1 + bsize);
  8998. +
  8999. + if (reclen > space)
  9000. + return -ENOSPC;
  9001. +
  9002. + if (pos >= 0) {
  9003. + nval_del(xb, xb_size, name);
  9004. + start = nval_used(xb, xb_size);
  9005. + }
  9006. +
  9007. + pos = start;
  9008. +
  9009. + memcpy(xb + pos, &reclen, sizeof(int));
  9010. + pos += sizeof(int);
  9011. + yaffs_strncpy((YCHAR *) (xb + pos), name, reclen);
  9012. + pos += (namelen + 1);
  9013. + memcpy(xb + pos, buf, bsize);
  9014. + return 0;
  9015. +}
  9016. +
  9017. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  9018. + int bsize)
  9019. +{
  9020. + int pos = nval_find(xb, xb_size, name, NULL);
  9021. + int size;
  9022. +
  9023. + if (pos >= 0 && pos < xb_size) {
  9024. +
  9025. + memcpy(&size, xb + pos, sizeof(int));
  9026. + pos += sizeof(int); /* advance past record length */
  9027. + size -= sizeof(int);
  9028. +
  9029. + /* Advance over name string */
  9030. + while (xb[pos] && size > 0 && pos < xb_size) {
  9031. + pos++;
  9032. + size--;
  9033. + }
  9034. + /*Advance over NUL */
  9035. + pos++;
  9036. + size--;
  9037. +
  9038. + if (size <= bsize) {
  9039. + memcpy(buf, xb + pos, size);
  9040. + return size;
  9041. + }
  9042. +
  9043. + }
  9044. + if (pos >= 0)
  9045. + return -ERANGE;
  9046. + else
  9047. + return -ENODATA;
  9048. +}
  9049. +
  9050. +int nval_list(const char *xb, int xb_size, char *buf, int bsize)
  9051. +{
  9052. + int pos = 0;
  9053. + int size;
  9054. + int name_len;
  9055. + int ncopied = 0;
  9056. + int filled = 0;
  9057. +
  9058. + memcpy(&size, xb + pos, sizeof(int));
  9059. + while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size
  9060. + && !filled) {
  9061. + pos += sizeof(int);
  9062. + size -= sizeof(int);
  9063. + name_len = yaffs_strnlen((YCHAR *) (xb + pos), size);
  9064. + if (ncopied + name_len + 1 < bsize) {
  9065. + memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
  9066. + buf += name_len;
  9067. + *buf = '\0';
  9068. + buf++;
  9069. + if (sizeof(YCHAR) > 1) {
  9070. + *buf = '\0';
  9071. + buf++;
  9072. + }
  9073. + ncopied += (name_len + 1);
  9074. + } else {
  9075. + filled = 1;
  9076. + }
  9077. + pos += size;
  9078. + if (pos < xb_size - sizeof(int))
  9079. + memcpy(&size, xb + pos, sizeof(int));
  9080. + else
  9081. + size = 0;
  9082. + }
  9083. + return ncopied;
  9084. +}
  9085. +
  9086. +int nval_hasvalues(const char *xb, int xb_size)
  9087. +{
  9088. + return nval_used(xb, xb_size) > 0;
  9089. +}
  9090. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.h linux-2.6.36/fs/yaffs2/yaffs_nameval.h
  9091. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.h 1970-01-01 01:00:00.000000000 +0100
  9092. +++ linux-2.6.36/fs/yaffs2/yaffs_nameval.h 2010-11-18 18:15:51.000000000 +0100
  9093. @@ -0,0 +1,28 @@
  9094. +/*
  9095. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9096. + *
  9097. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9098. + * for Toby Churchill Ltd and Brightstar Engineering
  9099. + *
  9100. + * Created by Charles Manning <charles@aleph1.co.uk>
  9101. + *
  9102. + * This program is free software; you can redistribute it and/or modify
  9103. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9104. + * published by the Free Software Foundation.
  9105. + *
  9106. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9107. + */
  9108. +
  9109. +#ifndef __NAMEVAL_H__
  9110. +#define __NAMEVAL_H__
  9111. +
  9112. +#include "yportenv.h"
  9113. +
  9114. +int nval_del(char *xb, int xb_size, const YCHAR * name);
  9115. +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
  9116. + int bsize, int flags);
  9117. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  9118. + int bsize);
  9119. +int nval_list(const char *xb, int xb_size, char *buf, int bsize);
  9120. +int nval_hasvalues(const char *xb, int xb_size);
  9121. +#endif
  9122. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nand.c linux-2.6.36/fs/yaffs2/yaffs_nand.c
  9123. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  9124. +++ linux-2.6.36/fs/yaffs2/yaffs_nand.c 2010-11-18 18:15:51.000000000 +0100
  9125. @@ -0,0 +1,128 @@
  9126. +/*
  9127. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9128. + *
  9129. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9130. + * for Toby Churchill Ltd and Brightstar Engineering
  9131. + *
  9132. + * Created by Charles Manning <charles@aleph1.co.uk>
  9133. + *
  9134. + * This program is free software; you can redistribute it and/or modify
  9135. + * it under the terms of the GNU General Public License version 2 as
  9136. + * published by the Free Software Foundation.
  9137. + */
  9138. +
  9139. +#include "yaffs_nand.h"
  9140. +#include "yaffs_tagscompat.h"
  9141. +#include "yaffs_tagsvalidity.h"
  9142. +
  9143. +#include "yaffs_getblockinfo.h"
  9144. +
  9145. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  9146. + u8 * buffer, struct yaffs_ext_tags *tags)
  9147. +{
  9148. + int result;
  9149. + struct yaffs_ext_tags local_tags;
  9150. +
  9151. + int realigned_chunk = nand_chunk - dev->chunk_offset;
  9152. +
  9153. + dev->n_page_reads++;
  9154. +
  9155. + /* If there are no tags provided, use local tags to get prioritised gc working */
  9156. + if (!tags)
  9157. + tags = &local_tags;
  9158. +
  9159. + if (dev->param.read_chunk_tags_fn)
  9160. + result =
  9161. + dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer,
  9162. + tags);
  9163. + else
  9164. + result = yaffs_tags_compat_rd(dev,
  9165. + realigned_chunk, buffer, tags);
  9166. + if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
  9167. +
  9168. + struct yaffs_block_info *bi;
  9169. + bi = yaffs_get_block_info(dev,
  9170. + nand_chunk /
  9171. + dev->param.chunks_per_block);
  9172. + yaffs_handle_chunk_error(dev, bi);
  9173. + }
  9174. +
  9175. + return result;
  9176. +}
  9177. +
  9178. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  9179. + int nand_chunk,
  9180. + const u8 * buffer, struct yaffs_ext_tags *tags)
  9181. +{
  9182. +
  9183. + dev->n_page_writes++;
  9184. +
  9185. + nand_chunk -= dev->chunk_offset;
  9186. +
  9187. + if (tags) {
  9188. + tags->seq_number = dev->seq_number;
  9189. + tags->chunk_used = 1;
  9190. + if (!yaffs_validate_tags(tags)) {
  9191. + T(YAFFS_TRACE_ERROR,
  9192. + (TSTR("Writing uninitialised tags" TENDSTR)));
  9193. + YBUG();
  9194. + }
  9195. + T(YAFFS_TRACE_WRITE,
  9196. + (TSTR("Writing chunk %d tags %d %d" TENDSTR), nand_chunk,
  9197. + tags->obj_id, tags->chunk_id));
  9198. + } else {
  9199. + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
  9200. + YBUG();
  9201. + }
  9202. +
  9203. + if (dev->param.write_chunk_tags_fn)
  9204. + return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
  9205. + tags);
  9206. + else
  9207. + return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
  9208. +}
  9209. +
  9210. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
  9211. +{
  9212. + block_no -= dev->block_offset;
  9213. +
  9214. + if (dev->param.bad_block_fn)
  9215. + return dev->param.bad_block_fn(dev, block_no);
  9216. + else
  9217. + return yaffs_tags_compat_mark_bad(dev, block_no);
  9218. +}
  9219. +
  9220. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  9221. + int block_no,
  9222. + enum yaffs_block_state *state,
  9223. + u32 * seq_number)
  9224. +{
  9225. + block_no -= dev->block_offset;
  9226. +
  9227. + if (dev->param.query_block_fn)
  9228. + return dev->param.query_block_fn(dev, block_no, state,
  9229. + seq_number);
  9230. + else
  9231. + return yaffs_tags_compat_query_block(dev, block_no,
  9232. + state, seq_number);
  9233. +}
  9234. +
  9235. +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
  9236. +{
  9237. + int result;
  9238. +
  9239. + flash_block -= dev->block_offset;
  9240. +
  9241. + dev->n_erasures++;
  9242. +
  9243. + result = dev->param.erase_fn(dev, flash_block);
  9244. +
  9245. + return result;
  9246. +}
  9247. +
  9248. +int yaffs_init_nand(struct yaffs_dev *dev)
  9249. +{
  9250. + if (dev->param.initialise_flash_fn)
  9251. + return dev->param.initialise_flash_fn(dev);
  9252. + return YAFFS_OK;
  9253. +}
  9254. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nand.h linux-2.6.36/fs/yaffs2/yaffs_nand.h
  9255. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  9256. +++ linux-2.6.36/fs/yaffs2/yaffs_nand.h 2010-11-18 18:15:51.000000000 +0100
  9257. @@ -0,0 +1,38 @@
  9258. +/*
  9259. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9260. + *
  9261. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9262. + * for Toby Churchill Ltd and Brightstar Engineering
  9263. + *
  9264. + * Created by Charles Manning <charles@aleph1.co.uk>
  9265. + *
  9266. + * This program is free software; you can redistribute it and/or modify
  9267. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9268. + * published by the Free Software Foundation.
  9269. + *
  9270. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9271. + */
  9272. +
  9273. +#ifndef __YAFFS_NAND_H__
  9274. +#define __YAFFS_NAND_H__
  9275. +#include "yaffs_guts.h"
  9276. +
  9277. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  9278. + u8 * buffer, struct yaffs_ext_tags *tags);
  9279. +
  9280. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  9281. + int nand_chunk,
  9282. + const u8 * buffer, struct yaffs_ext_tags *tags);
  9283. +
  9284. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no);
  9285. +
  9286. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  9287. + int block_no,
  9288. + enum yaffs_block_state *state,
  9289. + unsigned *seq_number);
  9290. +
  9291. +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block);
  9292. +
  9293. +int yaffs_init_nand(struct yaffs_dev *dev);
  9294. +
  9295. +#endif
  9296. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.c linux-2.6.36/fs/yaffs2/yaffs_packedtags1.c
  9297. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  9298. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags1.c 2010-11-18 18:15:51.000000000 +0100
  9299. @@ -0,0 +1,53 @@
  9300. +/*
  9301. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9302. + *
  9303. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9304. + * for Toby Churchill Ltd and Brightstar Engineering
  9305. + *
  9306. + * Created by Charles Manning <charles@aleph1.co.uk>
  9307. + *
  9308. + * This program is free software; you can redistribute it and/or modify
  9309. + * it under the terms of the GNU General Public License version 2 as
  9310. + * published by the Free Software Foundation.
  9311. + */
  9312. +
  9313. +#include "yaffs_packedtags1.h"
  9314. +#include "yportenv.h"
  9315. +
  9316. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  9317. + const struct yaffs_ext_tags *t)
  9318. +{
  9319. + pt->chunk_id = t->chunk_id;
  9320. + pt->serial_number = t->serial_number;
  9321. + pt->n_bytes = t->n_bytes;
  9322. + pt->obj_id = t->obj_id;
  9323. + pt->ecc = 0;
  9324. + pt->deleted = (t->is_deleted) ? 0 : 1;
  9325. + pt->unused_stuff = 0;
  9326. + pt->should_be_ff = 0xFFFFFFFF;
  9327. +
  9328. +}
  9329. +
  9330. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  9331. + const struct yaffs_packed_tags1 *pt)
  9332. +{
  9333. + static const u8 all_ff[] =
  9334. + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9335. + 0xff
  9336. + };
  9337. +
  9338. + if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) {
  9339. + t->block_bad = 0;
  9340. + if (pt->should_be_ff != 0xFFFFFFFF)
  9341. + t->block_bad = 1;
  9342. + t->chunk_used = 1;
  9343. + t->obj_id = pt->obj_id;
  9344. + t->chunk_id = pt->chunk_id;
  9345. + t->n_bytes = pt->n_bytes;
  9346. + t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9347. + t->is_deleted = (pt->deleted) ? 0 : 1;
  9348. + t->serial_number = pt->serial_number;
  9349. + } else {
  9350. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  9351. + }
  9352. +}
  9353. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.h linux-2.6.36/fs/yaffs2/yaffs_packedtags1.h
  9354. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  9355. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags1.h 2010-11-18 18:15:51.000000000 +0100
  9356. @@ -0,0 +1,39 @@
  9357. +/*
  9358. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9359. + *
  9360. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9361. + * for Toby Churchill Ltd and Brightstar Engineering
  9362. + *
  9363. + * Created by Charles Manning <charles@aleph1.co.uk>
  9364. + *
  9365. + * This program is free software; you can redistribute it and/or modify
  9366. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9367. + * published by the Free Software Foundation.
  9368. + *
  9369. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9370. + */
  9371. +
  9372. +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
  9373. +
  9374. +#ifndef __YAFFS_PACKEDTAGS1_H__
  9375. +#define __YAFFS_PACKEDTAGS1_H__
  9376. +
  9377. +#include "yaffs_guts.h"
  9378. +
  9379. +struct yaffs_packed_tags1 {
  9380. + unsigned chunk_id:20;
  9381. + unsigned serial_number:2;
  9382. + unsigned n_bytes:10;
  9383. + unsigned obj_id:18;
  9384. + unsigned ecc:12;
  9385. + unsigned deleted:1;
  9386. + unsigned unused_stuff:1;
  9387. + unsigned should_be_ff;
  9388. +
  9389. +};
  9390. +
  9391. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  9392. + const struct yaffs_ext_tags *t);
  9393. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  9394. + const struct yaffs_packed_tags1 *pt);
  9395. +#endif
  9396. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.c linux-2.6.36/fs/yaffs2/yaffs_packedtags2.c
  9397. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  9398. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags2.c 2010-11-18 18:15:51.000000000 +0100
  9399. @@ -0,0 +1,197 @@
  9400. +/*
  9401. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9402. + *
  9403. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9404. + * for Toby Churchill Ltd and Brightstar Engineering
  9405. + *
  9406. + * Created by Charles Manning <charles@aleph1.co.uk>
  9407. + *
  9408. + * This program is free software; you can redistribute it and/or modify
  9409. + * it under the terms of the GNU General Public License version 2 as
  9410. + * published by the Free Software Foundation.
  9411. + */
  9412. +
  9413. +#include "yaffs_packedtags2.h"
  9414. +#include "yportenv.h"
  9415. +#include "yaffs_trace.h"
  9416. +#include "yaffs_tagsvalidity.h"
  9417. +
  9418. +/* This code packs a set of extended tags into a binary structure for
  9419. + * NAND storage
  9420. + */
  9421. +
  9422. +/* Some of the information is "extra" struff which can be packed in to
  9423. + * speed scanning
  9424. + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
  9425. + */
  9426. +
  9427. +/* Extra flags applied to chunk_id */
  9428. +
  9429. +#define EXTRA_HEADER_INFO_FLAG 0x80000000
  9430. +#define EXTRA_SHRINK_FLAG 0x40000000
  9431. +#define EXTRA_SHADOWS_FLAG 0x20000000
  9432. +#define EXTRA_SPARE_FLAGS 0x10000000
  9433. +
  9434. +#define ALL_EXTRA_FLAGS 0xF0000000
  9435. +
  9436. +/* Also, the top 4 bits of the object Id are set to the object type. */
  9437. +#define EXTRA_OBJECT_TYPE_SHIFT (28)
  9438. +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
  9439. +
  9440. +static void yaffs_dump_packed_tags2_tags_only(const struct
  9441. + yaffs_packed_tags2_tags_only *ptt)
  9442. +{
  9443. + T(YAFFS_TRACE_MTD,
  9444. + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
  9445. + ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number));
  9446. +}
  9447. +
  9448. +static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
  9449. +{
  9450. + yaffs_dump_packed_tags2_tags_only(&pt->t);
  9451. +}
  9452. +
  9453. +static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
  9454. +{
  9455. + T(YAFFS_TRACE_MTD,
  9456. + (TSTR
  9457. + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
  9458. + TENDSTR), t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
  9459. + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
  9460. + t->seq_number));
  9461. +
  9462. +}
  9463. +
  9464. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
  9465. + const struct yaffs_ext_tags *t)
  9466. +{
  9467. + ptt->chunk_id = t->chunk_id;
  9468. + ptt->seq_number = t->seq_number;
  9469. + ptt->n_bytes = t->n_bytes;
  9470. + ptt->obj_id = t->obj_id;
  9471. +
  9472. + if (t->chunk_id == 0 && t->extra_available) {
  9473. + /* Store the extra header info instead */
  9474. + /* We save the parent object in the chunk_id */
  9475. + ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
  9476. + if (t->extra_is_shrink)
  9477. + ptt->chunk_id |= EXTRA_SHRINK_FLAG;
  9478. + if (t->extra_shadows)
  9479. + ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
  9480. +
  9481. + ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9482. + ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
  9483. +
  9484. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9485. + ptt->n_bytes = t->extra_equiv_id;
  9486. + else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  9487. + ptt->n_bytes = t->extra_length;
  9488. + else
  9489. + ptt->n_bytes = 0;
  9490. + }
  9491. +
  9492. + yaffs_dump_packed_tags2_tags_only(ptt);
  9493. + yaffs_dump_tags2(t);
  9494. +}
  9495. +
  9496. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9497. + const struct yaffs_ext_tags *t, int tags_ecc)
  9498. +{
  9499. + yaffs_pack_tags2_tags_only(&pt->t, t);
  9500. +
  9501. + if (tags_ecc)
  9502. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9503. + sizeof(struct
  9504. + yaffs_packed_tags2_tags_only),
  9505. + &pt->ecc);
  9506. +}
  9507. +
  9508. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9509. + struct yaffs_packed_tags2_tags_only *ptt)
  9510. +{
  9511. +
  9512. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  9513. +
  9514. + yaffs_init_tags(t);
  9515. +
  9516. + if (ptt->seq_number != 0xFFFFFFFF) {
  9517. + t->block_bad = 0;
  9518. + t->chunk_used = 1;
  9519. + t->obj_id = ptt->obj_id;
  9520. + t->chunk_id = ptt->chunk_id;
  9521. + t->n_bytes = ptt->n_bytes;
  9522. + t->is_deleted = 0;
  9523. + t->serial_number = 0;
  9524. + t->seq_number = ptt->seq_number;
  9525. +
  9526. + /* Do extra header info stuff */
  9527. +
  9528. + if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
  9529. + t->chunk_id = 0;
  9530. + t->n_bytes = 0;
  9531. +
  9532. + t->extra_available = 1;
  9533. + t->extra_parent_id =
  9534. + ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
  9535. + t->extra_is_shrink =
  9536. + (ptt->chunk_id & EXTRA_SHRINK_FLAG) ? 1 : 0;
  9537. + t->extra_shadows =
  9538. + (ptt->chunk_id & EXTRA_SHADOWS_FLAG) ? 1 : 0;
  9539. + t->extra_obj_type =
  9540. + ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
  9541. + t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9542. +
  9543. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9544. + t->extra_equiv_id = ptt->n_bytes;
  9545. + else
  9546. + t->extra_length = ptt->n_bytes;
  9547. + }
  9548. + }
  9549. +
  9550. + yaffs_dump_packed_tags2_tags_only(ptt);
  9551. + yaffs_dump_tags2(t);
  9552. +
  9553. +}
  9554. +
  9555. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9556. + int tags_ecc)
  9557. +{
  9558. +
  9559. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9560. +
  9561. + if (pt->t.seq_number != 0xFFFFFFFF && tags_ecc) {
  9562. + /* Chunk is in use and we need to do ECC */
  9563. +
  9564. + struct yaffs_ecc_other ecc;
  9565. + int result;
  9566. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9567. + sizeof(struct
  9568. + yaffs_packed_tags2_tags_only),
  9569. + &ecc);
  9570. + result =
  9571. + yaffs_ecc_correct_other((unsigned char *)&pt->t,
  9572. + sizeof(struct
  9573. + yaffs_packed_tags2_tags_only),
  9574. + &pt->ecc, &ecc);
  9575. + switch (result) {
  9576. + case 0:
  9577. + ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9578. + break;
  9579. + case 1:
  9580. + ecc_result = YAFFS_ECC_RESULT_FIXED;
  9581. + break;
  9582. + case -1:
  9583. + ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9584. + break;
  9585. + default:
  9586. + ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  9587. + }
  9588. + }
  9589. +
  9590. + yaffs_unpack_tags2_tags_only(t, &pt->t);
  9591. +
  9592. + t->ecc_result = ecc_result;
  9593. +
  9594. + yaffs_dump_packed_tags2(pt);
  9595. + yaffs_dump_tags2(t);
  9596. +}
  9597. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.h linux-2.6.36/fs/yaffs2/yaffs_packedtags2.h
  9598. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  9599. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags2.h 2010-11-18 18:15:51.000000000 +0100
  9600. @@ -0,0 +1,47 @@
  9601. +/*
  9602. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9603. + *
  9604. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9605. + * for Toby Churchill Ltd and Brightstar Engineering
  9606. + *
  9607. + * Created by Charles Manning <charles@aleph1.co.uk>
  9608. + *
  9609. + * This program is free software; you can redistribute it and/or modify
  9610. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9611. + * published by the Free Software Foundation.
  9612. + *
  9613. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9614. + */
  9615. +
  9616. +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
  9617. +
  9618. +#ifndef __YAFFS_PACKEDTAGS2_H__
  9619. +#define __YAFFS_PACKEDTAGS2_H__
  9620. +
  9621. +#include "yaffs_guts.h"
  9622. +#include "yaffs_ecc.h"
  9623. +
  9624. +struct yaffs_packed_tags2_tags_only {
  9625. + unsigned seq_number;
  9626. + unsigned obj_id;
  9627. + unsigned chunk_id;
  9628. + unsigned n_bytes;
  9629. +};
  9630. +
  9631. +struct yaffs_packed_tags2 {
  9632. + struct yaffs_packed_tags2_tags_only t;
  9633. + struct yaffs_ecc_other ecc;
  9634. +};
  9635. +
  9636. +/* Full packed tags with ECC, used for oob tags */
  9637. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9638. + const struct yaffs_ext_tags *t, int tags_ecc);
  9639. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9640. + int tags_ecc);
  9641. +
  9642. +/* Only the tags part (no ECC for use with inband tags */
  9643. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt,
  9644. + const struct yaffs_ext_tags *t);
  9645. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9646. + struct yaffs_packed_tags2_tags_only *pt);
  9647. +#endif
  9648. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.c linux-2.6.36/fs/yaffs2/yaffs_tagscompat.c
  9649. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  9650. +++ linux-2.6.36/fs/yaffs2/yaffs_tagscompat.c 2010-11-18 18:15:51.000000000 +0100
  9651. @@ -0,0 +1,454 @@
  9652. +/*
  9653. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9654. + *
  9655. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9656. + * for Toby Churchill Ltd and Brightstar Engineering
  9657. + *
  9658. + * Created by Charles Manning <charles@aleph1.co.uk>
  9659. + *
  9660. + * This program is free software; you can redistribute it and/or modify
  9661. + * it under the terms of the GNU General Public License version 2 as
  9662. + * published by the Free Software Foundation.
  9663. + */
  9664. +
  9665. +#include "yaffs_guts.h"
  9666. +#include "yaffs_tagscompat.h"
  9667. +#include "yaffs_ecc.h"
  9668. +#include "yaffs_getblockinfo.h"
  9669. +#include "yaffs_trace.h"
  9670. +
  9671. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
  9672. +
  9673. +static const char yaffs_count_bits_table[256] = {
  9674. + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  9675. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9676. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9677. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9678. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9679. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9680. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9681. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9682. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9683. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9684. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9685. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9686. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9687. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9688. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9689. + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  9690. +};
  9691. +
  9692. +int yaffs_count_bits(u8 x)
  9693. +{
  9694. + int ret_val;
  9695. + ret_val = yaffs_count_bits_table[x];
  9696. + return ret_val;
  9697. +}
  9698. +
  9699. +/********** Tags ECC calculations *********/
  9700. +
  9701. +void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare)
  9702. +{
  9703. + yaffs_ecc_cacl(data, spare->ecc1);
  9704. + yaffs_ecc_cacl(&data[256], spare->ecc2);
  9705. +}
  9706. +
  9707. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
  9708. +{
  9709. + /* Calculate an ecc */
  9710. +
  9711. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9712. + unsigned i, j;
  9713. + unsigned ecc = 0;
  9714. + unsigned bit = 0;
  9715. +
  9716. + tags->ecc = 0;
  9717. +
  9718. + for (i = 0; i < 8; i++) {
  9719. + for (j = 1; j & 0xff; j <<= 1) {
  9720. + bit++;
  9721. + if (b[i] & j)
  9722. + ecc ^= bit;
  9723. + }
  9724. + }
  9725. +
  9726. + tags->ecc = ecc;
  9727. +
  9728. +}
  9729. +
  9730. +int yaffs_check_tags_ecc(struct yaffs_tags *tags)
  9731. +{
  9732. + unsigned ecc = tags->ecc;
  9733. +
  9734. + yaffs_calc_tags_ecc(tags);
  9735. +
  9736. + ecc ^= tags->ecc;
  9737. +
  9738. + if (ecc && ecc <= 64) {
  9739. + /* TODO: Handle the failure better. Retire? */
  9740. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9741. +
  9742. + ecc--;
  9743. +
  9744. + b[ecc / 8] ^= (1 << (ecc & 7));
  9745. +
  9746. + /* Now recvalc the ecc */
  9747. + yaffs_calc_tags_ecc(tags);
  9748. +
  9749. + return 1; /* recovered error */
  9750. + } else if (ecc) {
  9751. + /* Wierd ecc failure value */
  9752. + /* TODO Need to do somethiong here */
  9753. + return -1; /* unrecovered error */
  9754. + }
  9755. +
  9756. + return 0;
  9757. +}
  9758. +
  9759. +/********** Tags **********/
  9760. +
  9761. +static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
  9762. + struct yaffs_tags *tags_ptr)
  9763. +{
  9764. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9765. +
  9766. + yaffs_calc_tags_ecc(tags_ptr);
  9767. +
  9768. + spare_ptr->tb0 = tu->as_bytes[0];
  9769. + spare_ptr->tb1 = tu->as_bytes[1];
  9770. + spare_ptr->tb2 = tu->as_bytes[2];
  9771. + spare_ptr->tb3 = tu->as_bytes[3];
  9772. + spare_ptr->tb4 = tu->as_bytes[4];
  9773. + spare_ptr->tb5 = tu->as_bytes[5];
  9774. + spare_ptr->tb6 = tu->as_bytes[6];
  9775. + spare_ptr->tb7 = tu->as_bytes[7];
  9776. +}
  9777. +
  9778. +static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
  9779. + struct yaffs_spare *spare_ptr,
  9780. + struct yaffs_tags *tags_ptr)
  9781. +{
  9782. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9783. + int result;
  9784. +
  9785. + tu->as_bytes[0] = spare_ptr->tb0;
  9786. + tu->as_bytes[1] = spare_ptr->tb1;
  9787. + tu->as_bytes[2] = spare_ptr->tb2;
  9788. + tu->as_bytes[3] = spare_ptr->tb3;
  9789. + tu->as_bytes[4] = spare_ptr->tb4;
  9790. + tu->as_bytes[5] = spare_ptr->tb5;
  9791. + tu->as_bytes[6] = spare_ptr->tb6;
  9792. + tu->as_bytes[7] = spare_ptr->tb7;
  9793. +
  9794. + result = yaffs_check_tags_ecc(tags_ptr);
  9795. + if (result > 0)
  9796. + dev->n_tags_ecc_fixed++;
  9797. + else if (result < 0)
  9798. + dev->n_tags_ecc_unfixed++;
  9799. +}
  9800. +
  9801. +static void yaffs_spare_init(struct yaffs_spare *spare)
  9802. +{
  9803. + memset(spare, 0xFF, sizeof(struct yaffs_spare));
  9804. +}
  9805. +
  9806. +static int yaffs_wr_nand(struct yaffs_dev *dev,
  9807. + int nand_chunk, const u8 * data,
  9808. + struct yaffs_spare *spare)
  9809. +{
  9810. + if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
  9811. + T(YAFFS_TRACE_ERROR,
  9812. + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
  9813. + nand_chunk));
  9814. + return YAFFS_FAIL;
  9815. + }
  9816. +
  9817. + return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
  9818. +}
  9819. +
  9820. +static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
  9821. + int nand_chunk,
  9822. + u8 * data,
  9823. + struct yaffs_spare *spare,
  9824. + enum yaffs_ecc_result *ecc_result,
  9825. + int correct_errors)
  9826. +{
  9827. + int ret_val;
  9828. + struct yaffs_spare local_spare;
  9829. +
  9830. + if (!spare && data) {
  9831. + /* If we don't have a real spare, then we use a local one. */
  9832. + /* Need this for the calculation of the ecc */
  9833. + spare = &local_spare;
  9834. + }
  9835. +
  9836. + if (!dev->param.use_nand_ecc) {
  9837. + ret_val =
  9838. + dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
  9839. + if (data && correct_errors) {
  9840. + /* Do ECC correction */
  9841. + /* Todo handle any errors */
  9842. + int ecc_result1, ecc_result2;
  9843. + u8 calc_ecc[3];
  9844. +
  9845. + yaffs_ecc_cacl(data, calc_ecc);
  9846. + ecc_result1 =
  9847. + yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
  9848. + yaffs_ecc_cacl(&data[256], calc_ecc);
  9849. + ecc_result2 =
  9850. + yaffs_ecc_correct(&data[256], spare->ecc2,
  9851. + calc_ecc);
  9852. +
  9853. + if (ecc_result1 > 0) {
  9854. + T(YAFFS_TRACE_ERROR,
  9855. + (TSTR
  9856. + ("**>>yaffs ecc error fix performed on chunk %d:0"
  9857. + TENDSTR), nand_chunk));
  9858. + dev->n_ecc_fixed++;
  9859. + } else if (ecc_result1 < 0) {
  9860. + T(YAFFS_TRACE_ERROR,
  9861. + (TSTR
  9862. + ("**>>yaffs ecc error unfixed on chunk %d:0"
  9863. + TENDSTR), nand_chunk));
  9864. + dev->n_ecc_unfixed++;
  9865. + }
  9866. +
  9867. + if (ecc_result2 > 0) {
  9868. + T(YAFFS_TRACE_ERROR,
  9869. + (TSTR
  9870. + ("**>>yaffs ecc error fix performed on chunk %d:1"
  9871. + TENDSTR), nand_chunk));
  9872. + dev->n_ecc_fixed++;
  9873. + } else if (ecc_result2 < 0) {
  9874. + T(YAFFS_TRACE_ERROR,
  9875. + (TSTR
  9876. + ("**>>yaffs ecc error unfixed on chunk %d:1"
  9877. + TENDSTR), nand_chunk));
  9878. + dev->n_ecc_unfixed++;
  9879. + }
  9880. +
  9881. + if (ecc_result1 || ecc_result2) {
  9882. + /* We had a data problem on this page */
  9883. + yaffs_handle_rd_data_error(dev, nand_chunk);
  9884. + }
  9885. +
  9886. + if (ecc_result1 < 0 || ecc_result2 < 0)
  9887. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9888. + else if (ecc_result1 > 0 || ecc_result2 > 0)
  9889. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  9890. + else
  9891. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9892. + }
  9893. + } else {
  9894. + /* Must allocate enough memory for spare+2*sizeof(int) */
  9895. + /* for ecc results from device. */
  9896. + struct yaffs_nand_spare nspare;
  9897. +
  9898. + memset(&nspare, 0, sizeof(nspare));
  9899. +
  9900. + ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
  9901. + (struct yaffs_spare *)
  9902. + &nspare);
  9903. + memcpy(spare, &nspare, sizeof(struct yaffs_spare));
  9904. + if (data && correct_errors) {
  9905. + if (nspare.eccres1 > 0) {
  9906. + T(YAFFS_TRACE_ERROR,
  9907. + (TSTR
  9908. + ("**>>mtd ecc error fix performed on chunk %d:0"
  9909. + TENDSTR), nand_chunk));
  9910. + } else if (nspare.eccres1 < 0) {
  9911. + T(YAFFS_TRACE_ERROR,
  9912. + (TSTR
  9913. + ("**>>mtd ecc error unfixed on chunk %d:0"
  9914. + TENDSTR), nand_chunk));
  9915. + }
  9916. +
  9917. + if (nspare.eccres2 > 0) {
  9918. + T(YAFFS_TRACE_ERROR,
  9919. + (TSTR
  9920. + ("**>>mtd ecc error fix performed on chunk %d:1"
  9921. + TENDSTR), nand_chunk));
  9922. + } else if (nspare.eccres2 < 0) {
  9923. + T(YAFFS_TRACE_ERROR,
  9924. + (TSTR
  9925. + ("**>>mtd ecc error unfixed on chunk %d:1"
  9926. + TENDSTR), nand_chunk));
  9927. + }
  9928. +
  9929. + if (nspare.eccres1 || nspare.eccres2) {
  9930. + /* We had a data problem on this page */
  9931. + yaffs_handle_rd_data_error(dev, nand_chunk);
  9932. + }
  9933. +
  9934. + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
  9935. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9936. + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
  9937. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  9938. + else
  9939. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9940. +
  9941. + }
  9942. + }
  9943. + return ret_val;
  9944. +}
  9945. +
  9946. +/*
  9947. + * Functions for robustisizing
  9948. + */
  9949. +
  9950. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
  9951. +{
  9952. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  9953. +
  9954. + /* Mark the block for retirement */
  9955. + yaffs_get_block_info(dev,
  9956. + flash_block + dev->block_offset)->needs_retiring =
  9957. + 1;
  9958. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  9959. + (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
  9960. +
  9961. + /* TODO:
  9962. + * Just do a garbage collection on the affected block
  9963. + * then retire the block
  9964. + * NB recursion
  9965. + */
  9966. +}
  9967. +
  9968. +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  9969. + int nand_chunk,
  9970. + const u8 * data, const struct yaffs_ext_tags *ext_tags)
  9971. +{
  9972. + struct yaffs_spare spare;
  9973. + struct yaffs_tags tags;
  9974. +
  9975. + yaffs_spare_init(&spare);
  9976. +
  9977. + if (ext_tags->is_deleted)
  9978. + spare.page_status = 0;
  9979. + else {
  9980. + tags.obj_id = ext_tags->obj_id;
  9981. + tags.chunk_id = ext_tags->chunk_id;
  9982. +
  9983. + tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
  9984. +
  9985. + if (dev->data_bytes_per_chunk >= 1024)
  9986. + tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
  9987. + else
  9988. + tags.n_bytes_msb = 3;
  9989. +
  9990. + tags.serial_number = ext_tags->serial_number;
  9991. +
  9992. + if (!dev->param.use_nand_ecc && data)
  9993. + yaffs_calc_ecc(data, &spare);
  9994. +
  9995. + yaffs_load_tags_to_spare(&spare, &tags);
  9996. +
  9997. + }
  9998. +
  9999. + return yaffs_wr_nand(dev, nand_chunk, data, &spare);
  10000. +}
  10001. +
  10002. +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  10003. + int nand_chunk,
  10004. + u8 * data, struct yaffs_ext_tags *ext_tags)
  10005. +{
  10006. +
  10007. + struct yaffs_spare spare;
  10008. + struct yaffs_tags tags;
  10009. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  10010. +
  10011. + static struct yaffs_spare spare_ff;
  10012. + static int init;
  10013. +
  10014. + if (!init) {
  10015. + memset(&spare_ff, 0xFF, sizeof(spare_ff));
  10016. + init = 1;
  10017. + }
  10018. +
  10019. + if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) {
  10020. + /* ext_tags may be NULL */
  10021. + if (ext_tags) {
  10022. +
  10023. + int deleted =
  10024. + (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
  10025. +
  10026. + ext_tags->is_deleted = deleted;
  10027. + ext_tags->ecc_result = ecc_result;
  10028. + ext_tags->block_bad = 0; /* We're reading it */
  10029. + /* therefore it is not a bad block */
  10030. + ext_tags->chunk_used =
  10031. + (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
  10032. + 0) ? 1 : 0;
  10033. +
  10034. + if (ext_tags->chunk_used) {
  10035. + yaffs_get_tags_from_spare(dev, &spare, &tags);
  10036. +
  10037. + ext_tags->obj_id = tags.obj_id;
  10038. + ext_tags->chunk_id = tags.chunk_id;
  10039. + ext_tags->n_bytes = tags.n_bytes_lsb;
  10040. +
  10041. + if (dev->data_bytes_per_chunk >= 1024)
  10042. + ext_tags->n_bytes |=
  10043. + (((unsigned)tags.
  10044. + n_bytes_msb) << 10);
  10045. +
  10046. + ext_tags->serial_number = tags.serial_number;
  10047. + }
  10048. + }
  10049. +
  10050. + return YAFFS_OK;
  10051. + } else {
  10052. + return YAFFS_FAIL;
  10053. + }
  10054. +}
  10055. +
  10056. +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
  10057. +{
  10058. +
  10059. + struct yaffs_spare spare;
  10060. +
  10061. + memset(&spare, 0xff, sizeof(struct yaffs_spare));
  10062. +
  10063. + spare.block_status = 'Y';
  10064. +
  10065. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
  10066. + &spare);
  10067. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
  10068. + NULL, &spare);
  10069. +
  10070. + return YAFFS_OK;
  10071. +
  10072. +}
  10073. +
  10074. +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  10075. + int block_no,
  10076. + enum yaffs_block_state *state,
  10077. + u32 * seq_number)
  10078. +{
  10079. +
  10080. + struct yaffs_spare spare0, spare1;
  10081. + static struct yaffs_spare spare_ff;
  10082. + static int init;
  10083. + enum yaffs_ecc_result dummy;
  10084. +
  10085. + if (!init) {
  10086. + memset(&spare_ff, 0xFF, sizeof(spare_ff));
  10087. + init = 1;
  10088. + }
  10089. +
  10090. + *seq_number = 0;
  10091. +
  10092. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
  10093. + &spare0, &dummy, 1);
  10094. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
  10095. + NULL, &spare1, &dummy, 1);
  10096. +
  10097. + if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7)
  10098. + *state = YAFFS_BLOCK_STATE_DEAD;
  10099. + else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
  10100. + *state = YAFFS_BLOCK_STATE_EMPTY;
  10101. + else
  10102. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  10103. +
  10104. + return YAFFS_OK;
  10105. +}
  10106. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.h linux-2.6.36/fs/yaffs2/yaffs_tagscompat.h
  10107. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  10108. +++ linux-2.6.36/fs/yaffs2/yaffs_tagscompat.h 2010-11-18 18:15:51.000000000 +0100
  10109. @@ -0,0 +1,36 @@
  10110. +/*
  10111. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10112. + *
  10113. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10114. + * for Toby Churchill Ltd and Brightstar Engineering
  10115. + *
  10116. + * Created by Charles Manning <charles@aleph1.co.uk>
  10117. + *
  10118. + * This program is free software; you can redistribute it and/or modify
  10119. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10120. + * published by the Free Software Foundation.
  10121. + *
  10122. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10123. + */
  10124. +
  10125. +#ifndef __YAFFS_TAGSCOMPAT_H__
  10126. +#define __YAFFS_TAGSCOMPAT_H__
  10127. +
  10128. +#include "yaffs_guts.h"
  10129. +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  10130. + int nand_chunk,
  10131. + const u8 * data, const struct yaffs_ext_tags *tags);
  10132. +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  10133. + int nand_chunk,
  10134. + u8 * data, struct yaffs_ext_tags *tags);
  10135. +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no);
  10136. +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  10137. + int block_no,
  10138. + enum yaffs_block_state *state,
  10139. + u32 * seq_number);
  10140. +
  10141. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags);
  10142. +int yaffs_check_tags_ecc(struct yaffs_tags *tags);
  10143. +int yaffs_count_bits(u8 byte);
  10144. +
  10145. +#endif
  10146. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.c
  10147. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
  10148. +++ linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.c 2010-11-18 18:15:51.000000000 +0100
  10149. @@ -0,0 +1,27 @@
  10150. +/*
  10151. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10152. + *
  10153. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10154. + * for Toby Churchill Ltd and Brightstar Engineering
  10155. + *
  10156. + * Created by Charles Manning <charles@aleph1.co.uk>
  10157. + *
  10158. + * This program is free software; you can redistribute it and/or modify
  10159. + * it under the terms of the GNU General Public License version 2 as
  10160. + * published by the Free Software Foundation.
  10161. + */
  10162. +
  10163. +#include "yaffs_tagsvalidity.h"
  10164. +
  10165. +void yaffs_init_tags(struct yaffs_ext_tags *tags)
  10166. +{
  10167. + memset(tags, 0, sizeof(struct yaffs_ext_tags));
  10168. + tags->validity0 = 0xAAAAAAAA;
  10169. + tags->validity1 = 0x55555555;
  10170. +}
  10171. +
  10172. +int yaffs_validate_tags(struct yaffs_ext_tags *tags)
  10173. +{
  10174. + return (tags->validity0 == 0xAAAAAAAA && tags->validity1 == 0x55555555);
  10175. +
  10176. +}
  10177. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.h
  10178. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
  10179. +++ linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.h 2010-11-18 18:15:51.000000000 +0100
  10180. @@ -0,0 +1,23 @@
  10181. +/*
  10182. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10183. + *
  10184. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10185. + * for Toby Churchill Ltd and Brightstar Engineering
  10186. + *
  10187. + * Created by Charles Manning <charles@aleph1.co.uk>
  10188. + *
  10189. + * This program is free software; you can redistribute it and/or modify
  10190. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10191. + * published by the Free Software Foundation.
  10192. + *
  10193. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10194. + */
  10195. +
  10196. +#ifndef __YAFFS_TAGS_VALIDITY_H__
  10197. +#define __YAFFS_TAGS_VALIDITY_H__
  10198. +
  10199. +#include "yaffs_guts.h"
  10200. +
  10201. +void yaffs_init_tags(struct yaffs_ext_tags *tags);
  10202. +int yaffs_validate_tags(struct yaffs_ext_tags *tags);
  10203. +#endif
  10204. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_trace.h linux-2.6.36/fs/yaffs2/yaffs_trace.h
  10205. --- linux-2.6.36.orig/fs/yaffs2/yaffs_trace.h 1970-01-01 01:00:00.000000000 +0100
  10206. +++ linux-2.6.36/fs/yaffs2/yaffs_trace.h 2010-11-18 18:15:51.000000000 +0100
  10207. @@ -0,0 +1,59 @@
  10208. +/*
  10209. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10210. + *
  10211. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10212. + * for Toby Churchill Ltd and Brightstar Engineering
  10213. + *
  10214. + * Created by Charles Manning <charles@aleph1.co.uk>
  10215. + *
  10216. + * This program is free software; you can redistribute it and/or modify
  10217. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10218. + * published by the Free Software Foundation.
  10219. + *
  10220. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10221. + */
  10222. +
  10223. +#ifndef __YTRACE_H__
  10224. +#define __YTRACE_H__
  10225. +
  10226. +extern unsigned int yaffs_trace_mask;
  10227. +extern unsigned int yaffs_wr_attempts;
  10228. +
  10229. +/*
  10230. + * Tracing flags.
  10231. + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
  10232. + */
  10233. +
  10234. +#define YAFFS_TRACE_OS 0x00000002
  10235. +#define YAFFS_TRACE_ALLOCATE 0x00000004
  10236. +#define YAFFS_TRACE_SCAN 0x00000008
  10237. +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
  10238. +#define YAFFS_TRACE_ERASE 0x00000020
  10239. +#define YAFFS_TRACE_GC 0x00000040
  10240. +#define YAFFS_TRACE_WRITE 0x00000080
  10241. +#define YAFFS_TRACE_TRACING 0x00000100
  10242. +#define YAFFS_TRACE_DELETION 0x00000200
  10243. +#define YAFFS_TRACE_BUFFERS 0x00000400
  10244. +#define YAFFS_TRACE_NANDACCESS 0x00000800
  10245. +#define YAFFS_TRACE_GC_DETAIL 0x00001000
  10246. +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
  10247. +#define YAFFS_TRACE_MTD 0x00004000
  10248. +#define YAFFS_TRACE_CHECKPOINT 0x00008000
  10249. +
  10250. +#define YAFFS_TRACE_VERIFY 0x00010000
  10251. +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
  10252. +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
  10253. +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
  10254. +
  10255. +#define YAFFS_TRACE_SYNC 0x00100000
  10256. +#define YAFFS_TRACE_BACKGROUND 0x00200000
  10257. +#define YAFFS_TRACE_LOCK 0x00400000
  10258. +#define YAFFS_TRACE_MOUNT 0x00800000
  10259. +
  10260. +#define YAFFS_TRACE_ERROR 0x40000000
  10261. +#define YAFFS_TRACE_BUG 0x80000000
  10262. +#define YAFFS_TRACE_ALWAYS 0xF0000000
  10263. +
  10264. +#define T(mask, p) do { if ((mask) & (yaffs_trace_mask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
  10265. +
  10266. +#endif
  10267. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_verify.c linux-2.6.36/fs/yaffs2/yaffs_verify.c
  10268. --- linux-2.6.36.orig/fs/yaffs2/yaffs_verify.c 1970-01-01 01:00:00.000000000 +0100
  10269. +++ linux-2.6.36/fs/yaffs2/yaffs_verify.c 2010-11-18 18:15:51.000000000 +0100
  10270. @@ -0,0 +1,546 @@
  10271. +/*
  10272. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10273. + *
  10274. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10275. + * for Toby Churchill Ltd and Brightstar Engineering
  10276. + *
  10277. + * Created by Charles Manning <charles@aleph1.co.uk>
  10278. + *
  10279. + * This program is free software; you can redistribute it and/or modify
  10280. + * it under the terms of the GNU General Public License version 2 as
  10281. + * published by the Free Software Foundation.
  10282. + */
  10283. +
  10284. +#include "yaffs_verify.h"
  10285. +#include "yaffs_trace.h"
  10286. +#include "yaffs_bitmap.h"
  10287. +#include "yaffs_getblockinfo.h"
  10288. +#include "yaffs_nand.h"
  10289. +
  10290. +int yaffs_skip_verification(struct yaffs_dev *dev)
  10291. +{
  10292. + dev = dev;
  10293. + return !(yaffs_trace_mask &
  10294. + (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  10295. +}
  10296. +
  10297. +static int yaffs_skip_full_verification(struct yaffs_dev *dev)
  10298. +{
  10299. + dev = dev;
  10300. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
  10301. +}
  10302. +
  10303. +static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
  10304. +{
  10305. + dev = dev;
  10306. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
  10307. +}
  10308. +
  10309. +static const char *block_state_name[] = {
  10310. + "Unknown",
  10311. + "Needs scanning",
  10312. + "Scanning",
  10313. + "Empty",
  10314. + "Allocating",
  10315. + "Full",
  10316. + "Dirty",
  10317. + "Checkpoint",
  10318. + "Collecting",
  10319. + "Dead"
  10320. +};
  10321. +
  10322. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
  10323. +{
  10324. + int actually_used;
  10325. + int in_use;
  10326. +
  10327. + if (yaffs_skip_verification(dev))
  10328. + return;
  10329. +
  10330. + /* Report illegal runtime states */
  10331. + if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
  10332. + T(YAFFS_TRACE_VERIFY,
  10333. + (TSTR("Block %d has undefined state %d" TENDSTR), n,
  10334. + bi->block_state));
  10335. +
  10336. + switch (bi->block_state) {
  10337. + case YAFFS_BLOCK_STATE_UNKNOWN:
  10338. + case YAFFS_BLOCK_STATE_SCANNING:
  10339. + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
  10340. + T(YAFFS_TRACE_VERIFY,
  10341. + (TSTR("Block %d has bad run-state %s" TENDSTR), n,
  10342. + block_state_name[bi->block_state]));
  10343. + }
  10344. +
  10345. + /* Check pages in use and soft deletions are legal */
  10346. +
  10347. + actually_used = bi->pages_in_use - bi->soft_del_pages;
  10348. +
  10349. + if (bi->pages_in_use < 0
  10350. + || bi->pages_in_use > dev->param.chunks_per_block
  10351. + || bi->soft_del_pages < 0
  10352. + || bi->soft_del_pages > dev->param.chunks_per_block
  10353. + || actually_used < 0 || actually_used > dev->param.chunks_per_block)
  10354. + T(YAFFS_TRACE_VERIFY,
  10355. + (TSTR
  10356. + ("Block %d has illegal values pages_in_used %d soft_del_pages %d"
  10357. + TENDSTR), n, bi->pages_in_use, bi->soft_del_pages));
  10358. +
  10359. + /* Check chunk bitmap legal */
  10360. + in_use = yaffs_count_chunk_bits(dev, n);
  10361. + if (in_use != bi->pages_in_use)
  10362. + T(YAFFS_TRACE_VERIFY,
  10363. + (TSTR
  10364. + ("Block %d has inconsistent values pages_in_use %d counted chunk bits %d"
  10365. + TENDSTR), n, bi->pages_in_use, in_use));
  10366. +
  10367. +}
  10368. +
  10369. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10370. + struct yaffs_block_info *bi, int n)
  10371. +{
  10372. + yaffs_verify_blk(dev, bi, n);
  10373. +
  10374. + /* After collection the block should be in the erased state */
  10375. +
  10376. + if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
  10377. + bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
  10378. + T(YAFFS_TRACE_ERROR,
  10379. + (TSTR
  10380. + ("Block %d is in state %d after gc, should be erased"
  10381. + TENDSTR), n, bi->block_state));
  10382. + }
  10383. +}
  10384. +
  10385. +void yaffs_verify_blocks(struct yaffs_dev *dev)
  10386. +{
  10387. + int i;
  10388. + int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
  10389. + int illegal_states = 0;
  10390. +
  10391. + if (yaffs_skip_verification(dev))
  10392. + return;
  10393. +
  10394. + memset(state_count, 0, sizeof(state_count));
  10395. +
  10396. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  10397. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  10398. + yaffs_verify_blk(dev, bi, i);
  10399. +
  10400. + if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
  10401. + state_count[bi->block_state]++;
  10402. + else
  10403. + illegal_states++;
  10404. + }
  10405. +
  10406. + T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR)));
  10407. + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary" TENDSTR)));
  10408. +
  10409. + T(YAFFS_TRACE_VERIFY,
  10410. + (TSTR("%d blocks have illegal states" TENDSTR), illegal_states));
  10411. + if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  10412. + T(YAFFS_TRACE_VERIFY,
  10413. + (TSTR("Too many allocating blocks" TENDSTR)));
  10414. +
  10415. + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  10416. + T(YAFFS_TRACE_VERIFY,
  10417. + (TSTR("%s %d blocks" TENDSTR),
  10418. + block_state_name[i], state_count[i]));
  10419. +
  10420. + if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
  10421. + T(YAFFS_TRACE_VERIFY,
  10422. + (TSTR("Checkpoint block count wrong dev %d count %d" TENDSTR),
  10423. + dev->blocks_in_checkpt,
  10424. + state_count[YAFFS_BLOCK_STATE_CHECKPOINT]));
  10425. +
  10426. + if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
  10427. + T(YAFFS_TRACE_VERIFY,
  10428. + (TSTR("Erased block count wrong dev %d count %d" TENDSTR),
  10429. + dev->n_erased_blocks, state_count[YAFFS_BLOCK_STATE_EMPTY]));
  10430. +
  10431. + if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  10432. + T(YAFFS_TRACE_VERIFY,
  10433. + (TSTR("Too many collecting blocks %d (max is 1)" TENDSTR),
  10434. + state_count[YAFFS_BLOCK_STATE_COLLECTING]));
  10435. +
  10436. + T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR)));
  10437. +
  10438. +}
  10439. +
  10440. +/*
  10441. + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
  10442. + * case those tests will not be performed.
  10443. + */
  10444. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10445. + struct yaffs_ext_tags *tags, int parent_check)
  10446. +{
  10447. + if (obj && yaffs_skip_verification(obj->my_dev))
  10448. + return;
  10449. +
  10450. + if (!(tags && obj && oh)) {
  10451. + T(YAFFS_TRACE_VERIFY,
  10452. + (TSTR("Verifying object header tags %p obj %p oh %p" TENDSTR),
  10453. + tags, obj, oh));
  10454. + return;
  10455. + }
  10456. +
  10457. + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  10458. + oh->type > YAFFS_OBJECT_TYPE_MAX)
  10459. + T(YAFFS_TRACE_VERIFY,
  10460. + (TSTR("Obj %d header type is illegal value 0x%x" TENDSTR),
  10461. + tags->obj_id, oh->type));
  10462. +
  10463. + if (tags->obj_id != obj->obj_id)
  10464. + T(YAFFS_TRACE_VERIFY,
  10465. + (TSTR("Obj %d header mismatch obj_id %d" TENDSTR),
  10466. + tags->obj_id, obj->obj_id));
  10467. +
  10468. + /*
  10469. + * Check that the object's parent ids match if parent_check requested.
  10470. + *
  10471. + * Tests do not apply to the root object.
  10472. + */
  10473. +
  10474. + if (parent_check && tags->obj_id > 1 && !obj->parent)
  10475. + T(YAFFS_TRACE_VERIFY,
  10476. + (TSTR
  10477. + ("Obj %d header mismatch parent_id %d obj->parent is NULL"
  10478. + TENDSTR), tags->obj_id, oh->parent_obj_id));
  10479. +
  10480. + if (parent_check && obj->parent &&
  10481. + oh->parent_obj_id != obj->parent->obj_id &&
  10482. + (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
  10483. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
  10484. + T(YAFFS_TRACE_VERIFY,
  10485. + (TSTR
  10486. + ("Obj %d header mismatch parent_id %d parent_obj_id %d"
  10487. + TENDSTR), tags->obj_id, oh->parent_obj_id,
  10488. + obj->parent->obj_id));
  10489. +
  10490. + if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
  10491. + T(YAFFS_TRACE_VERIFY,
  10492. + (TSTR("Obj %d header name is NULL" TENDSTR), obj->obj_id));
  10493. +
  10494. + if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Trashed name */
  10495. + T(YAFFS_TRACE_VERIFY,
  10496. + (TSTR("Obj %d header name is 0xFF" TENDSTR), obj->obj_id));
  10497. +}
  10498. +
  10499. +void yaffs_verify_file(struct yaffs_obj *obj)
  10500. +{
  10501. + int required_depth;
  10502. + int actual_depth;
  10503. + u32 last_chunk;
  10504. + u32 x;
  10505. + u32 i;
  10506. + struct yaffs_dev *dev;
  10507. + struct yaffs_ext_tags tags;
  10508. + struct yaffs_tnode *tn;
  10509. + u32 obj_id;
  10510. +
  10511. + if (!obj)
  10512. + return;
  10513. +
  10514. + if (yaffs_skip_verification(obj->my_dev))
  10515. + return;
  10516. +
  10517. + dev = obj->my_dev;
  10518. + obj_id = obj->obj_id;
  10519. +
  10520. + /* Check file size is consistent with tnode depth */
  10521. + last_chunk =
  10522. + obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1;
  10523. + x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
  10524. + required_depth = 0;
  10525. + while (x > 0) {
  10526. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  10527. + required_depth++;
  10528. + }
  10529. +
  10530. + actual_depth = obj->variant.file_variant.top_level;
  10531. +
  10532. + /* Check that the chunks in the tnode tree are all correct.
  10533. + * We do this by scanning through the tnode tree and
  10534. + * checking the tags for every chunk match.
  10535. + */
  10536. +
  10537. + if (yaffs_skip_nand_verification(dev))
  10538. + return;
  10539. +
  10540. + for (i = 1; i <= last_chunk; i++) {
  10541. + tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
  10542. +
  10543. + if (tn) {
  10544. + u32 the_chunk = yaffs_get_group_base(dev, tn, i);
  10545. + if (the_chunk > 0) {
  10546. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),obj_id,i,the_chunk)); */
  10547. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  10548. + &tags);
  10549. + if (tags.obj_id != obj_id || tags.chunk_id != i) {
  10550. + T(~0,
  10551. + (TSTR
  10552. + ("Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)"
  10553. + TENDSTR), obj_id, i, the_chunk,
  10554. + tags.obj_id, tags.chunk_id));
  10555. + }
  10556. + }
  10557. + }
  10558. + }
  10559. +}
  10560. +
  10561. +void yaffs_verify_link(struct yaffs_obj *obj)
  10562. +{
  10563. + if (obj && yaffs_skip_verification(obj->my_dev))
  10564. + return;
  10565. +
  10566. + /* Verify sane equivalent object */
  10567. +}
  10568. +
  10569. +void yaffs_verify_symlink(struct yaffs_obj *obj)
  10570. +{
  10571. + if (obj && yaffs_skip_verification(obj->my_dev))
  10572. + return;
  10573. +
  10574. + /* Verify symlink string */
  10575. +}
  10576. +
  10577. +void yaffs_verify_special(struct yaffs_obj *obj)
  10578. +{
  10579. + if (obj && yaffs_skip_verification(obj->my_dev))
  10580. + return;
  10581. +}
  10582. +
  10583. +void yaffs_verify_obj(struct yaffs_obj *obj)
  10584. +{
  10585. + struct yaffs_dev *dev;
  10586. +
  10587. + u32 chunk_min;
  10588. + u32 chunk_max;
  10589. +
  10590. + u32 chunk_id_ok;
  10591. + u32 chunk_in_range;
  10592. + u32 chunk_wrongly_deleted;
  10593. + u32 chunk_valid;
  10594. +
  10595. + if (!obj)
  10596. + return;
  10597. +
  10598. + if (obj->being_created)
  10599. + return;
  10600. +
  10601. + dev = obj->my_dev;
  10602. +
  10603. + if (yaffs_skip_verification(dev))
  10604. + return;
  10605. +
  10606. + /* Check sane object header chunk */
  10607. +
  10608. + chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
  10609. + chunk_max =
  10610. + (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
  10611. +
  10612. + chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
  10613. + ((unsigned)(obj->hdr_chunk)) <= chunk_max);
  10614. + chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
  10615. + chunk_valid = chunk_in_range &&
  10616. + yaffs_check_chunk_bit(dev,
  10617. + obj->hdr_chunk / dev->param.chunks_per_block,
  10618. + obj->hdr_chunk % dev->param.chunks_per_block);
  10619. + chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
  10620. +
  10621. + if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted)) {
  10622. + T(YAFFS_TRACE_VERIFY,
  10623. + (TSTR("Obj %d has chunk_id %d %s %s" TENDSTR),
  10624. + obj->obj_id, obj->hdr_chunk,
  10625. + chunk_id_ok ? "" : ",out of range",
  10626. + chunk_wrongly_deleted ? ",marked as deleted" : ""));
  10627. + }
  10628. +
  10629. + if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
  10630. + struct yaffs_ext_tags tags;
  10631. + struct yaffs_obj_hdr *oh;
  10632. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  10633. +
  10634. + oh = (struct yaffs_obj_hdr *)buffer;
  10635. +
  10636. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
  10637. +
  10638. + yaffs_verify_oh(obj, oh, &tags, 1);
  10639. +
  10640. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  10641. + }
  10642. +
  10643. + /* Verify it has a parent */
  10644. + if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
  10645. + T(YAFFS_TRACE_VERIFY,
  10646. + (TSTR
  10647. + ("Obj %d has parent pointer %p which does not look like an object"
  10648. + TENDSTR), obj->obj_id, obj->parent));
  10649. + }
  10650. +
  10651. + /* Verify parent is a directory */
  10652. + if (obj->parent
  10653. + && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10654. + T(YAFFS_TRACE_VERIFY,
  10655. + (TSTR("Obj %d's parent is not a directory (type %d)" TENDSTR),
  10656. + obj->obj_id, obj->parent->variant_type));
  10657. + }
  10658. +
  10659. + switch (obj->variant_type) {
  10660. + case YAFFS_OBJECT_TYPE_FILE:
  10661. + yaffs_verify_file(obj);
  10662. + break;
  10663. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10664. + yaffs_verify_symlink(obj);
  10665. + break;
  10666. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10667. + yaffs_verify_dir(obj);
  10668. + break;
  10669. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10670. + yaffs_verify_link(obj);
  10671. + break;
  10672. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10673. + yaffs_verify_special(obj);
  10674. + break;
  10675. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  10676. + default:
  10677. + T(YAFFS_TRACE_VERIFY,
  10678. + (TSTR("Obj %d has illegaltype %d" TENDSTR),
  10679. + obj->obj_id, obj->variant_type));
  10680. + break;
  10681. + }
  10682. +}
  10683. +
  10684. +void yaffs_verify_objects(struct yaffs_dev *dev)
  10685. +{
  10686. + struct yaffs_obj *obj;
  10687. + int i;
  10688. + struct list_head *lh;
  10689. +
  10690. + if (yaffs_skip_verification(dev))
  10691. + return;
  10692. +
  10693. + /* Iterate through the objects in each hash entry */
  10694. +
  10695. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  10696. + list_for_each(lh, &dev->obj_bucket[i].list) {
  10697. + if (lh) {
  10698. + obj =
  10699. + list_entry(lh, struct yaffs_obj, hash_link);
  10700. + yaffs_verify_obj(obj);
  10701. + }
  10702. + }
  10703. + }
  10704. +}
  10705. +
  10706. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
  10707. +{
  10708. + struct list_head *lh;
  10709. + struct yaffs_obj *list_obj;
  10710. +
  10711. + int count = 0;
  10712. +
  10713. + if (!obj) {
  10714. + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
  10715. + YBUG();
  10716. + return;
  10717. + }
  10718. +
  10719. + if (yaffs_skip_verification(obj->my_dev))
  10720. + return;
  10721. +
  10722. + if (!obj->parent) {
  10723. + T(YAFFS_TRACE_ALWAYS,
  10724. + (TSTR("Object does not have parent" TENDSTR)));
  10725. + YBUG();
  10726. + return;
  10727. + }
  10728. +
  10729. + if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10730. + T(YAFFS_TRACE_ALWAYS,
  10731. + (TSTR("Parent is not directory" TENDSTR)));
  10732. + YBUG();
  10733. + }
  10734. +
  10735. + /* Iterate through the objects in each hash entry */
  10736. +
  10737. + list_for_each(lh, &obj->parent->variant.dir_variant.children) {
  10738. + if (lh) {
  10739. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10740. + yaffs_verify_obj(list_obj);
  10741. + if (obj == list_obj)
  10742. + count++;
  10743. + }
  10744. + }
  10745. +
  10746. + if (count != 1) {
  10747. + T(YAFFS_TRACE_ALWAYS,
  10748. + (TSTR("Object in directory %d times" TENDSTR), count));
  10749. + YBUG();
  10750. + }
  10751. +}
  10752. +
  10753. +void yaffs_verify_dir(struct yaffs_obj *directory)
  10754. +{
  10755. + struct list_head *lh;
  10756. + struct yaffs_obj *list_obj;
  10757. +
  10758. + if (!directory) {
  10759. + YBUG();
  10760. + return;
  10761. + }
  10762. +
  10763. + if (yaffs_skip_full_verification(directory->my_dev))
  10764. + return;
  10765. +
  10766. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10767. + T(YAFFS_TRACE_ALWAYS,
  10768. + (TSTR("Directory has wrong type: %d" TENDSTR),
  10769. + directory->variant_type));
  10770. + YBUG();
  10771. + }
  10772. +
  10773. + /* Iterate through the objects in each hash entry */
  10774. +
  10775. + list_for_each(lh, &directory->variant.dir_variant.children) {
  10776. + if (lh) {
  10777. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10778. + if (list_obj->parent != directory) {
  10779. + T(YAFFS_TRACE_ALWAYS,
  10780. + (TSTR
  10781. + ("Object in directory list has wrong parent %p"
  10782. + TENDSTR), list_obj->parent));
  10783. + YBUG();
  10784. + }
  10785. + yaffs_verify_obj_in_dir(list_obj);
  10786. + }
  10787. + }
  10788. +}
  10789. +
  10790. +static int yaffs_free_verification_failures;
  10791. +
  10792. +void yaffs_verify_free_chunks(struct yaffs_dev *dev)
  10793. +{
  10794. + int counted;
  10795. + int difference;
  10796. +
  10797. + if (yaffs_skip_verification(dev))
  10798. + return;
  10799. +
  10800. + counted = yaffs_count_free_chunks(dev);
  10801. +
  10802. + difference = dev->n_free_chunks - counted;
  10803. +
  10804. + if (difference) {
  10805. + T(YAFFS_TRACE_ALWAYS,
  10806. + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
  10807. + dev->n_free_chunks, counted, difference));
  10808. + yaffs_free_verification_failures++;
  10809. + }
  10810. +}
  10811. +
  10812. +int yaffs_verify_file_sane(struct yaffs_obj *in)
  10813. +{
  10814. + in = in;
  10815. + return YAFFS_OK;
  10816. +}
  10817. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_verify.h linux-2.6.36/fs/yaffs2/yaffs_verify.h
  10818. --- linux-2.6.36.orig/fs/yaffs2/yaffs_verify.h 1970-01-01 01:00:00.000000000 +0100
  10819. +++ linux-2.6.36/fs/yaffs2/yaffs_verify.h 2010-11-18 18:15:51.000000000 +0100
  10820. @@ -0,0 +1,43 @@
  10821. +/*
  10822. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10823. + *
  10824. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10825. + * for Toby Churchill Ltd and Brightstar Engineering
  10826. + *
  10827. + * Created by Charles Manning <charles@aleph1.co.uk>
  10828. + *
  10829. + * This program is free software; you can redistribute it and/or modify
  10830. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10831. + * published by the Free Software Foundation.
  10832. + *
  10833. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10834. + */
  10835. +
  10836. +#ifndef __YAFFS_VERIFY_H__
  10837. +#define __YAFFS_VERIFY_H__
  10838. +
  10839. +#include "yaffs_guts.h"
  10840. +
  10841. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi,
  10842. + int n);
  10843. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10844. + struct yaffs_block_info *bi, int n);
  10845. +void yaffs_verify_blocks(struct yaffs_dev *dev);
  10846. +
  10847. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10848. + struct yaffs_ext_tags *tags, int parent_check);
  10849. +void yaffs_verify_file(struct yaffs_obj *obj);
  10850. +void yaffs_verify_link(struct yaffs_obj *obj);
  10851. +void yaffs_verify_symlink(struct yaffs_obj *obj);
  10852. +void yaffs_verify_special(struct yaffs_obj *obj);
  10853. +void yaffs_verify_obj(struct yaffs_obj *obj);
  10854. +void yaffs_verify_objects(struct yaffs_dev *dev);
  10855. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj);
  10856. +void yaffs_verify_dir(struct yaffs_obj *directory);
  10857. +void yaffs_verify_free_chunks(struct yaffs_dev *dev);
  10858. +
  10859. +int yaffs_verify_file_sane(struct yaffs_obj *obj);
  10860. +
  10861. +int yaffs_skip_verification(struct yaffs_dev *dev);
  10862. +
  10863. +#endif
  10864. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_vfs.c linux-2.6.36/fs/yaffs2/yaffs_vfs.c
  10865. --- linux-2.6.36.orig/fs/yaffs2/yaffs_vfs.c 1970-01-01 01:00:00.000000000 +0100
  10866. +++ linux-2.6.36/fs/yaffs2/yaffs_vfs.c 2010-11-18 18:15:51.000000000 +0100
  10867. @@ -0,0 +1,3565 @@
  10868. +/*
  10869. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10870. + *
  10871. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10872. + * for Toby Churchill Ltd and Brightstar Engineering
  10873. + *
  10874. + * Created by Charles Manning <charles@aleph1.co.uk>
  10875. + * Acknowledgements:
  10876. + * Luc van OostenRyck for numerous patches.
  10877. + * Nick Bane for numerous patches.
  10878. + * Nick Bane for 2.5/2.6 integration.
  10879. + * Andras Toth for mknod rdev issue.
  10880. + * Michael Fischer for finding the problem with inode inconsistency.
  10881. + * Some code bodily lifted from JFFS
  10882. + *
  10883. + * This program is free software; you can redistribute it and/or modify
  10884. + * it under the terms of the GNU General Public License version 2 as
  10885. + * published by the Free Software Foundation.
  10886. + */
  10887. +
  10888. +/*
  10889. + *
  10890. + * This is the file system front-end to YAFFS that hooks it up to
  10891. + * the VFS.
  10892. + *
  10893. + * Special notes:
  10894. + * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
  10895. + * this superblock
  10896. + * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
  10897. + * superblock
  10898. + * >> inode->u.generic_ip points to the associated struct yaffs_obj.
  10899. + */
  10900. +
  10901. +/*
  10902. + * There are two variants of the VFS glue code. This variant should compile
  10903. + * for any version of Linux.
  10904. + */
  10905. +#include <linux/version.h>
  10906. +
  10907. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
  10908. +#define YAFFS_COMPILE_BACKGROUND
  10909. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23))
  10910. +#define YAFFS_COMPILE_FREEZER
  10911. +#endif
  10912. +#endif
  10913. +
  10914. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
  10915. +#define YAFFS_COMPILE_EXPORTFS
  10916. +#endif
  10917. +
  10918. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
  10919. +#define YAFFS_USE_SETATTR_COPY
  10920. +#define YAFFS_USE_TRUNCATE_SETSIZE
  10921. +#endif
  10922. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
  10923. +#define YAFFS_HAS_EVICT_INODE
  10924. +#endif
  10925. +
  10926. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
  10927. +#define YAFFS_NEW_FOLLOW_LINK 1
  10928. +#else
  10929. +#define YAFFS_NEW_FOLLOW_LINK 0
  10930. +#endif
  10931. +
  10932. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  10933. +#include <linux/config.h>
  10934. +#endif
  10935. +
  10936. +#include <linux/kernel.h>
  10937. +#include <linux/module.h>
  10938. +#include <linux/slab.h>
  10939. +#include <linux/init.h>
  10940. +#include <linux/fs.h>
  10941. +#include <linux/proc_fs.h>
  10942. +#include <linux/smp_lock.h>
  10943. +#include <linux/pagemap.h>
  10944. +#include <linux/mtd/mtd.h>
  10945. +#include <linux/interrupt.h>
  10946. +#include <linux/string.h>
  10947. +#include <linux/ctype.h>
  10948. +
  10949. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  10950. +#include <linux/namei.h>
  10951. +#endif
  10952. +
  10953. +#ifdef YAFFS_COMPILE_EXPORTFS
  10954. +#include <linux/exportfs.h>
  10955. +#endif
  10956. +
  10957. +#ifdef YAFFS_COMPILE_BACKGROUND
  10958. +#include <linux/kthread.h>
  10959. +#include <linux/delay.h>
  10960. +#endif
  10961. +#ifdef YAFFS_COMPILE_FREEZER
  10962. +#include <linux/freezer.h>
  10963. +#endif
  10964. +
  10965. +#include <asm/div64.h>
  10966. +
  10967. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  10968. +
  10969. +#include <linux/statfs.h>
  10970. +
  10971. +#define UnlockPage(p) unlock_page(p)
  10972. +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  10973. +
  10974. +/* FIXME: use sb->s_id instead ? */
  10975. +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  10976. +
  10977. +#else
  10978. +
  10979. +#include <linux/locks.h>
  10980. +#define BDEVNAME_SIZE 0
  10981. +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
  10982. +
  10983. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
  10984. +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
  10985. +#define __user
  10986. +#endif
  10987. +
  10988. +#endif
  10989. +
  10990. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  10991. +#define YPROC_ROOT (&proc_root)
  10992. +#else
  10993. +#define YPROC_ROOT NULL
  10994. +#endif
  10995. +
  10996. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
  10997. +#define Y_INIT_TIMER(a) init_timer(a)
  10998. +#else
  10999. +#define Y_INIT_TIMER(a) init_timer_on_stack(a)
  11000. +#endif
  11001. +
  11002. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11003. +#define WRITE_SIZE_STR "writesize"
  11004. +#define WRITE_SIZE(mtd) ((mtd)->writesize)
  11005. +#else
  11006. +#define WRITE_SIZE_STR "oobblock"
  11007. +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
  11008. +#endif
  11009. +
  11010. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
  11011. +#define YAFFS_USE_WRITE_BEGIN_END 1
  11012. +#else
  11013. +#define YAFFS_USE_WRITE_BEGIN_END 0
  11014. +#endif
  11015. +
  11016. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
  11017. +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  11018. +{
  11019. + uint64_t result = partition_size;
  11020. + do_div(result, block_size);
  11021. + return (uint32_t) result;
  11022. +}
  11023. +#else
  11024. +#define YCALCBLOCKS(s, b) ((s)/(b))
  11025. +#endif
  11026. +
  11027. +#include <linux/uaccess.h>
  11028. +#include <linux/mtd/mtd.h>
  11029. +
  11030. +#include "yportenv.h"
  11031. +#include "yaffs_trace.h"
  11032. +#include "yaffs_guts.h"
  11033. +#include "yaffs_attribs.h"
  11034. +
  11035. +#include "yaffs_linux.h"
  11036. +
  11037. +#include "yaffs_mtdif.h"
  11038. +#include "yaffs_mtdif1.h"
  11039. +#include "yaffs_mtdif2.h"
  11040. +
  11041. +unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
  11042. +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  11043. +unsigned int yaffs_auto_checkpoint = 1;
  11044. +unsigned int yaffs_gc_control = 1;
  11045. +unsigned int yaffs_bg_enable = 1;
  11046. +
  11047. +/* Module Parameters */
  11048. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11049. +module_param(yaffs_trace_mask, uint, 0644);
  11050. +module_param(yaffs_wr_attempts, uint, 0644);
  11051. +module_param(yaffs_auto_checkpoint, uint, 0644);
  11052. +module_param(yaffs_gc_control, uint, 0644);
  11053. +module_param(yaffs_bg_enable, uint, 0644);
  11054. +#else
  11055. +MODULE_PARM(yaffs_trace_mask, "i");
  11056. +MODULE_PARM(yaffs_wr_attempts, "i");
  11057. +MODULE_PARM(yaffs_auto_checkpoint, "i");
  11058. +MODULE_PARM(yaffs_gc_control, "i");
  11059. +#endif
  11060. +
  11061. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  11062. +/* use iget and read_inode */
  11063. +#define Y_IGET(sb, inum) iget((sb), (inum))
  11064. +static void yaffs_read_inode(struct inode *inode);
  11065. +
  11066. +#else
  11067. +/* Call local equivalent */
  11068. +#define YAFFS_USE_OWN_IGET
  11069. +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
  11070. +
  11071. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
  11072. +#endif
  11073. +
  11074. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11075. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
  11076. +#else
  11077. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
  11078. +#endif
  11079. +
  11080. +#define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
  11081. +#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
  11082. +
  11083. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11084. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
  11085. +#else
  11086. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
  11087. +#endif
  11088. +
  11089. +#define update_dir_time(dir) do {\
  11090. + (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
  11091. + } while(0)
  11092. +
  11093. +static void yaffs_put_super(struct super_block *sb);
  11094. +
  11095. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  11096. + loff_t * pos);
  11097. +static ssize_t yaffs_hold_space(struct file *f);
  11098. +static void yaffs_release_space(struct file *f);
  11099. +
  11100. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11101. +static int yaffs_file_flush(struct file *file, fl_owner_t id);
  11102. +#else
  11103. +static int yaffs_file_flush(struct file *file);
  11104. +#endif
  11105. +
  11106. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  11107. +static int yaffs_sync_object(struct file *file, int datasync);
  11108. +#else
  11109. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  11110. + int datasync);
  11111. +#endif
  11112. +
  11113. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
  11114. +
  11115. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11116. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  11117. + struct nameidata *n);
  11118. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  11119. + struct nameidata *n);
  11120. +#else
  11121. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
  11122. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
  11123. +#endif
  11124. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  11125. + struct dentry *dentry);
  11126. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
  11127. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  11128. + const char *symname);
  11129. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  11130. +
  11131. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11132. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  11133. + dev_t dev);
  11134. +#else
  11135. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  11136. + int dev);
  11137. +#endif
  11138. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  11139. + struct inode *new_dir, struct dentry *new_dentry);
  11140. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
  11141. +
  11142. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11143. +static int yaffs_sync_fs(struct super_block *sb, int wait);
  11144. +static void yaffs_write_super(struct super_block *sb);
  11145. +#else
  11146. +static int yaffs_sync_fs(struct super_block *sb);
  11147. +static int yaffs_write_super(struct super_block *sb);
  11148. +#endif
  11149. +
  11150. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11151. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
  11152. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11153. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
  11154. +#else
  11155. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
  11156. +#endif
  11157. +
  11158. +#ifdef YAFFS_HAS_PUT_INODE
  11159. +static void yaffs_put_inode(struct inode *inode);
  11160. +#endif
  11161. +
  11162. +#ifdef YAFFS_HAS_EVICT_INODE
  11163. +static void yaffs_evict_inode(struct inode *);
  11164. +#else
  11165. +static void yaffs_delete_inode(struct inode *);
  11166. +static void yaffs_clear_inode(struct inode *);
  11167. +#endif
  11168. +
  11169. +static int yaffs_readpage(struct file *file, struct page *page);
  11170. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11171. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
  11172. +#else
  11173. +static int yaffs_writepage(struct page *page);
  11174. +#endif
  11175. +
  11176. +#ifdef CONFIG_YAFFS_XATTR
  11177. +int yaffs_setxattr(struct dentry *dentry, const char *name,
  11178. + const void *value, size_t size, int flags);
  11179. +ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
  11180. + size_t size);
  11181. +int yaffs_removexattr(struct dentry *dentry, const char *name);
  11182. +ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
  11183. +#endif
  11184. +
  11185. +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
  11186. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  11187. + loff_t pos, unsigned len, unsigned flags,
  11188. + struct page **pagep, void **fsdata);
  11189. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  11190. + loff_t pos, unsigned len, unsigned copied,
  11191. + struct page *pg, void *fsdadata);
  11192. +#else
  11193. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  11194. + unsigned offset, unsigned to);
  11195. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  11196. + unsigned to);
  11197. +
  11198. +#endif
  11199. +
  11200. +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
  11201. + int buflen);
  11202. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11203. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
  11204. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  11205. +#else
  11206. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  11207. +#endif
  11208. +
  11209. +static void yaffs_touch_super(struct yaffs_dev *dev);
  11210. +
  11211. +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
  11212. +
  11213. +static int yaffs_vfs_setattr(struct inode *, struct iattr *);
  11214. +
  11215. +static struct address_space_operations yaffs_file_address_operations = {
  11216. + .readpage = yaffs_readpage,
  11217. + .writepage = yaffs_writepage,
  11218. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  11219. + .write_begin = yaffs_write_begin,
  11220. + .write_end = yaffs_write_end,
  11221. +#else
  11222. + .prepare_write = yaffs_prepare_write,
  11223. + .commit_write = yaffs_commit_write,
  11224. +#endif
  11225. +};
  11226. +
  11227. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
  11228. +static const struct file_operations yaffs_file_operations = {
  11229. + .read = do_sync_read,
  11230. + .write = do_sync_write,
  11231. + .aio_read = generic_file_aio_read,
  11232. + .aio_write = generic_file_aio_write,
  11233. + .mmap = generic_file_mmap,
  11234. + .flush = yaffs_file_flush,
  11235. + .fsync = yaffs_sync_object,
  11236. + .splice_read = generic_file_splice_read,
  11237. + .splice_write = generic_file_splice_write,
  11238. + .llseek = generic_file_llseek,
  11239. +};
  11240. +
  11241. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11242. +
  11243. +static const struct file_operations yaffs_file_operations = {
  11244. + .read = do_sync_read,
  11245. + .write = do_sync_write,
  11246. + .aio_read = generic_file_aio_read,
  11247. + .aio_write = generic_file_aio_write,
  11248. + .mmap = generic_file_mmap,
  11249. + .flush = yaffs_file_flush,
  11250. + .fsync = yaffs_sync_object,
  11251. + .sendfile = generic_file_sendfile,
  11252. +};
  11253. +
  11254. +#else
  11255. +
  11256. +static const struct file_operations yaffs_file_operations = {
  11257. + .read = generic_file_read,
  11258. + .write = generic_file_write,
  11259. + .mmap = generic_file_mmap,
  11260. + .flush = yaffs_file_flush,
  11261. + .fsync = yaffs_sync_object,
  11262. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11263. + .sendfile = generic_file_sendfile,
  11264. +#endif
  11265. +};
  11266. +#endif
  11267. +
  11268. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
  11269. +static void zero_user_segment(struct page *page, unsigned start, unsigned end)
  11270. +{
  11271. + void *kaddr = kmap_atomic(page, KM_USER0);
  11272. + memset(kaddr + start, 0, end - start);
  11273. + kunmap_atomic(kaddr, KM_USER0);
  11274. + flush_dcache_page(page);
  11275. +}
  11276. +#endif
  11277. +
  11278. +static const struct inode_operations yaffs_file_inode_operations = {
  11279. + .setattr = yaffs_setattr,
  11280. +#ifdef CONFIG_YAFFS_XATTR
  11281. + .setxattr = yaffs_setxattr,
  11282. + .getxattr = yaffs_getxattr,
  11283. + .listxattr = yaffs_listxattr,
  11284. + .removexattr = yaffs_removexattr,
  11285. +#endif
  11286. +};
  11287. +
  11288. +static const struct inode_operations yaffs_symlink_inode_operations = {
  11289. + .readlink = yaffs_readlink,
  11290. + .follow_link = yaffs_follow_link,
  11291. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11292. + .put_link = yaffs_put_link,
  11293. +#endif
  11294. + .setattr = yaffs_setattr,
  11295. +#ifdef CONFIG_YAFFS_XATTR
  11296. + .setxattr = yaffs_setxattr,
  11297. + .getxattr = yaffs_getxattr,
  11298. + .listxattr = yaffs_listxattr,
  11299. + .removexattr = yaffs_removexattr,
  11300. +#endif
  11301. +};
  11302. +
  11303. +static const struct inode_operations yaffs_dir_inode_operations = {
  11304. + .create = yaffs_create,
  11305. + .lookup = yaffs_lookup,
  11306. + .link = yaffs_link,
  11307. + .unlink = yaffs_unlink,
  11308. + .symlink = yaffs_symlink,
  11309. + .mkdir = yaffs_mkdir,
  11310. + .rmdir = yaffs_unlink,
  11311. + .mknod = yaffs_mknod,
  11312. + .rename = yaffs_rename,
  11313. + .setattr = yaffs_setattr,
  11314. +#ifdef CONFIG_YAFFS_XATTR
  11315. + .setxattr = yaffs_setxattr,
  11316. + .getxattr = yaffs_getxattr,
  11317. + .listxattr = yaffs_listxattr,
  11318. + .removexattr = yaffs_removexattr,
  11319. +#endif
  11320. +};
  11321. +
  11322. +static const struct file_operations yaffs_dir_operations = {
  11323. + .read = generic_read_dir,
  11324. + .readdir = yaffs_readdir,
  11325. + .fsync = yaffs_sync_object,
  11326. + .llseek = yaffs_dir_llseek,
  11327. +};
  11328. +
  11329. +static const struct super_operations yaffs_super_ops = {
  11330. + .statfs = yaffs_statfs,
  11331. +
  11332. +#ifndef YAFFS_USE_OWN_IGET
  11333. + .read_inode = yaffs_read_inode,
  11334. +#endif
  11335. +#ifdef YAFFS_HAS_PUT_INODE
  11336. + .put_inode = yaffs_put_inode,
  11337. +#endif
  11338. + .put_super = yaffs_put_super,
  11339. +#ifdef YAFFS_HAS_EVICT_INODE
  11340. + .evict_inode = yaffs_evict_inode,
  11341. +#else
  11342. + .delete_inode = yaffs_delete_inode,
  11343. + .clear_inode = yaffs_clear_inode,
  11344. +#endif
  11345. + .sync_fs = yaffs_sync_fs,
  11346. + .write_super = yaffs_write_super,
  11347. +};
  11348. +
  11349. +static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
  11350. +{
  11351. +#ifdef YAFFS_USE_SETATTR_COPY
  11352. + setattr_copy(inode, attr);
  11353. + return 0;
  11354. +#else
  11355. + return inode_setattr(inode, attr);
  11356. +#endif
  11357. +
  11358. +}
  11359. +
  11360. +static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
  11361. +{
  11362. +#ifdef YAFFS_USE_TRUNCATE_SETSIZE
  11363. + truncate_setsize(inode, newsize);
  11364. + return 0;
  11365. +#else
  11366. + truncate_inode_pages(&inode->i_data, newsize);
  11367. + return 0;
  11368. +#endif
  11369. +
  11370. +}
  11371. +
  11372. +static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
  11373. +{
  11374. + return yaffs_gc_control;
  11375. +}
  11376. +
  11377. +static void yaffs_gross_lock(struct yaffs_dev *dev)
  11378. +{
  11379. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
  11380. + mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11381. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
  11382. +}
  11383. +
  11384. +static void yaffs_gross_unlock(struct yaffs_dev *dev)
  11385. +{
  11386. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
  11387. + mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11388. +}
  11389. +
  11390. +#ifdef YAFFS_COMPILE_EXPORTFS
  11391. +
  11392. +static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  11393. + uint32_t generation)
  11394. +{
  11395. + return Y_IGET(sb, ino);
  11396. +}
  11397. +
  11398. +static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
  11399. + struct fid *fid, int fh_len,
  11400. + int fh_type)
  11401. +{
  11402. + return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  11403. + yaffs2_nfs_get_inode);
  11404. +}
  11405. +
  11406. +static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
  11407. + struct fid *fid, int fh_len,
  11408. + int fh_type)
  11409. +{
  11410. + return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  11411. + yaffs2_nfs_get_inode);
  11412. +}
  11413. +
  11414. +struct dentry *yaffs2_get_parent(struct dentry *dentry)
  11415. +{
  11416. +
  11417. + struct super_block *sb = dentry->d_inode->i_sb;
  11418. + struct dentry *parent = ERR_PTR(-ENOENT);
  11419. + struct inode *inode;
  11420. + unsigned long parent_ino;
  11421. + struct yaffs_obj *d_obj;
  11422. + struct yaffs_obj *parent_obj;
  11423. +
  11424. + d_obj = yaffs_inode_to_obj(dentry->d_inode);
  11425. +
  11426. + if (d_obj) {
  11427. + parent_obj = d_obj->parent;
  11428. + if (parent_obj) {
  11429. + parent_ino = yaffs_get_obj_inode(parent_obj);
  11430. + inode = Y_IGET(sb, parent_ino);
  11431. +
  11432. + if (IS_ERR(inode)) {
  11433. + parent = ERR_CAST(inode);
  11434. + } else {
  11435. + parent = d_obtain_alias(inode);
  11436. + if (!IS_ERR(parent)) {
  11437. + parent = ERR_PTR(-ENOMEM);
  11438. + iput(inode);
  11439. + }
  11440. + }
  11441. + }
  11442. + }
  11443. +
  11444. + return parent;
  11445. +}
  11446. +
  11447. +/* Just declare a zero structure as a NULL value implies
  11448. + * using the default functions of exportfs.
  11449. + */
  11450. +
  11451. +static struct export_operations yaffs_export_ops = {
  11452. + .fh_to_dentry = yaffs2_fh_to_dentry,
  11453. + .fh_to_parent = yaffs2_fh_to_parent,
  11454. + .get_parent = yaffs2_get_parent,
  11455. +};
  11456. +
  11457. +#endif
  11458. +
  11459. +/*-----------------------------------------------------------------*/
  11460. +/* Directory search context allows us to unlock access to yaffs during
  11461. + * filldir without causing problems with the directory being modified.
  11462. + * This is similar to the tried and tested mechanism used in yaffs direct.
  11463. + *
  11464. + * A search context iterates along a doubly linked list of siblings in the
  11465. + * directory. If the iterating object is deleted then this would corrupt
  11466. + * the list iteration, likely causing a crash. The search context avoids
  11467. + * this by using the remove_obj_fn to move the search context to the
  11468. + * next object before the object is deleted.
  11469. + *
  11470. + * Many readdirs (and thus seach conexts) may be alive simulateously so
  11471. + * each struct yaffs_dev has a list of these.
  11472. + *
  11473. + * A seach context lives for the duration of a readdir.
  11474. + *
  11475. + * All these functions must be called while yaffs is locked.
  11476. + */
  11477. +
  11478. +struct yaffs_search_context {
  11479. + struct yaffs_dev *dev;
  11480. + struct yaffs_obj *dir_obj;
  11481. + struct yaffs_obj *next_return;
  11482. + struct list_head others;
  11483. +};
  11484. +
  11485. +/*
  11486. + * yaffs_new_search() creates a new search context, initialises it and
  11487. + * adds it to the device's search context list.
  11488. + *
  11489. + * Called at start of readdir.
  11490. + */
  11491. +static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
  11492. +{
  11493. + struct yaffs_dev *dev = dir->my_dev;
  11494. + struct yaffs_search_context *sc =
  11495. + YMALLOC(sizeof(struct yaffs_search_context));
  11496. + if (sc) {
  11497. + sc->dir_obj = dir;
  11498. + sc->dev = dev;
  11499. + if (list_empty(&sc->dir_obj->variant.dir_variant.children))
  11500. + sc->next_return = NULL;
  11501. + else
  11502. + sc->next_return =
  11503. + list_entry(dir->variant.dir_variant.children.next,
  11504. + struct yaffs_obj, siblings);
  11505. + INIT_LIST_HEAD(&sc->others);
  11506. + list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
  11507. + }
  11508. + return sc;
  11509. +}
  11510. +
  11511. +/*
  11512. + * yaffs_search_end() disposes of a search context and cleans up.
  11513. + */
  11514. +static void yaffs_search_end(struct yaffs_search_context *sc)
  11515. +{
  11516. + if (sc) {
  11517. + list_del(&sc->others);
  11518. + YFREE(sc);
  11519. + }
  11520. +}
  11521. +
  11522. +/*
  11523. + * yaffs_search_advance() moves a search context to the next object.
  11524. + * Called when the search iterates or when an object removal causes
  11525. + * the search context to be moved to the next object.
  11526. + */
  11527. +static void yaffs_search_advance(struct yaffs_search_context *sc)
  11528. +{
  11529. + if (!sc)
  11530. + return;
  11531. +
  11532. + if (sc->next_return == NULL ||
  11533. + list_empty(&sc->dir_obj->variant.dir_variant.children))
  11534. + sc->next_return = NULL;
  11535. + else {
  11536. + struct list_head *next = sc->next_return->siblings.next;
  11537. +
  11538. + if (next == &sc->dir_obj->variant.dir_variant.children)
  11539. + sc->next_return = NULL; /* end of list */
  11540. + else
  11541. + sc->next_return =
  11542. + list_entry(next, struct yaffs_obj, siblings);
  11543. + }
  11544. +}
  11545. +
  11546. +/*
  11547. + * yaffs_remove_obj_callback() is called when an object is unlinked.
  11548. + * We check open search contexts and advance any which are currently
  11549. + * on the object being iterated.
  11550. + */
  11551. +static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
  11552. +{
  11553. +
  11554. + struct list_head *i;
  11555. + struct yaffs_search_context *sc;
  11556. + struct list_head *search_contexts =
  11557. + &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
  11558. +
  11559. + /* Iterate through the directory search contexts.
  11560. + * If any are currently on the object being removed, then advance
  11561. + * the search context to the next object to prevent a hanging pointer.
  11562. + */
  11563. + list_for_each(i, search_contexts) {
  11564. + if (i) {
  11565. + sc = list_entry(i, struct yaffs_search_context, others);
  11566. + if (sc->next_return == obj)
  11567. + yaffs_search_advance(sc);
  11568. + }
  11569. + }
  11570. +
  11571. +}
  11572. +
  11573. +/*-----------------------------------------------------------------*/
  11574. +
  11575. +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
  11576. + int buflen)
  11577. +{
  11578. + unsigned char *alias;
  11579. + int ret;
  11580. +
  11581. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11582. +
  11583. + yaffs_gross_lock(dev);
  11584. +
  11585. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11586. +
  11587. + yaffs_gross_unlock(dev);
  11588. +
  11589. + if (!alias)
  11590. + return -ENOMEM;
  11591. +
  11592. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  11593. + kfree(alias);
  11594. + return ret;
  11595. +}
  11596. +
  11597. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11598. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11599. +{
  11600. + void *ret;
  11601. +#else
  11602. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11603. +{
  11604. + int ret
  11605. +#endif
  11606. + unsigned char *alias;
  11607. + int ret_int = 0;
  11608. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11609. +
  11610. + yaffs_gross_lock(dev);
  11611. +
  11612. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11613. + yaffs_gross_unlock(dev);
  11614. +
  11615. + if (!alias) {
  11616. + ret_int = -ENOMEM;
  11617. + goto out;
  11618. + }
  11619. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11620. + nd_set_link(nd, alias);
  11621. + ret = alias;
  11622. +out:
  11623. + if (ret_int)
  11624. + ret = ERR_PTR(ret_int);
  11625. + return ret;
  11626. +#else
  11627. + ret = vfs_follow_link(nd, alias);
  11628. + kfree(alias);
  11629. +out:
  11630. + if (ret_int)
  11631. + ret = ret_int;
  11632. + return ret;
  11633. +#endif
  11634. +}
  11635. +
  11636. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11637. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
  11638. +{
  11639. + kfree(alias);
  11640. +}
  11641. +#endif
  11642. +
  11643. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  11644. + struct yaffs_obj *obj);
  11645. +
  11646. +/*
  11647. + * Lookup is used to find objects in the fs
  11648. + */
  11649. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11650. +
  11651. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  11652. + struct nameidata *n)
  11653. +#else
  11654. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  11655. +#endif
  11656. +{
  11657. + struct yaffs_obj *obj;
  11658. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  11659. +
  11660. + struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
  11661. +
  11662. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  11663. + yaffs_gross_lock(dev);
  11664. +
  11665. + T(YAFFS_TRACE_OS,
  11666. + (TSTR("yaffs_lookup for %d:%s\n"),
  11667. + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
  11668. +
  11669. + obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
  11670. +
  11671. + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
  11672. +
  11673. + /* Can't hold gross lock when calling yaffs_get_inode() */
  11674. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  11675. + yaffs_gross_unlock(dev);
  11676. +
  11677. + if (obj) {
  11678. + T(YAFFS_TRACE_OS,
  11679. + (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
  11680. +
  11681. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  11682. +
  11683. + if (inode) {
  11684. + T(YAFFS_TRACE_OS, (TSTR("yaffs_loookup dentry \n")));
  11685. +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
  11686. + * d_add even if NULL inode */
  11687. +#if 0
  11688. + /*dget(dentry); // try to solve directory bug */
  11689. + d_add(dentry, inode);
  11690. +
  11691. + /* return dentry; */
  11692. + return NULL;
  11693. +#endif
  11694. + }
  11695. +
  11696. + } else {
  11697. + T(YAFFS_TRACE_OS, (TSTR("yaffs_lookup not found\n")));
  11698. +
  11699. + }
  11700. +
  11701. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  11702. + * NULL which creates dentry hash */
  11703. + d_add(dentry, inode);
  11704. +
  11705. + return NULL;
  11706. +}
  11707. +
  11708. +#ifdef YAFFS_HAS_PUT_INODE
  11709. +
  11710. +/* For now put inode is just for debugging
  11711. + * Put inode is called when the inode **structure** is put.
  11712. + */
  11713. +static void yaffs_put_inode(struct inode *inode)
  11714. +{
  11715. + T(YAFFS_TRACE_OS,
  11716. + (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
  11717. + atomic_read(&inode->i_count)));
  11718. +
  11719. +}
  11720. +#endif
  11721. +
  11722. +static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
  11723. +{
  11724. + /* Clear the association between the inode and
  11725. + * the struct yaffs_obj.
  11726. + */
  11727. + obj->my_inode = NULL;
  11728. + yaffs_inode_to_obj_lv(inode) = NULL;
  11729. +
  11730. + /* If the object freeing was deferred, then the real
  11731. + * free happens now.
  11732. + * This should fix the inode inconsistency problem.
  11733. + */
  11734. + yaffs_handle_defered_free(obj);
  11735. +}
  11736. +
  11737. +#ifdef YAFFS_HAS_EVICT_INODE
  11738. +/* yaffs_evict_inode combines into one operation what was previously done in
  11739. + * yaffs_clear_inode() and yaffs_delete_inode()
  11740. + *
  11741. + */
  11742. +static void yaffs_evict_inode(struct inode *inode)
  11743. +{
  11744. + struct yaffs_obj *obj;
  11745. + struct yaffs_dev *dev;
  11746. + int deleteme = 0;
  11747. +
  11748. + obj = yaffs_inode_to_obj(inode);
  11749. +
  11750. + T(YAFFS_TRACE_OS,
  11751. + (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11752. + atomic_read(&inode->i_count),
  11753. + obj ? "object exists" : "null object"));
  11754. +
  11755. + if (!inode->i_nlink && !is_bad_inode(inode))
  11756. + deleteme = 1;
  11757. + truncate_inode_pages(&inode->i_data, 0);
  11758. + end_writeback(inode);
  11759. +
  11760. + if (deleteme && obj) {
  11761. + dev = obj->my_dev;
  11762. + yaffs_gross_lock(dev);
  11763. + yaffs_del_obj(obj);
  11764. + yaffs_gross_unlock(dev);
  11765. + }
  11766. + if (obj) {
  11767. + dev = obj->my_dev;
  11768. + yaffs_gross_lock(dev);
  11769. + yaffs_unstitch_obj(inode, obj);
  11770. + yaffs_gross_unlock(dev);
  11771. + }
  11772. +
  11773. +}
  11774. +#else
  11775. +
  11776. +/* clear is called to tell the fs to release any per-inode data it holds.
  11777. + * The object might still exist on disk and is just being thrown out of the cache
  11778. + * or else the object has actually been deleted and we're being called via
  11779. + * the chain
  11780. + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
  11781. + */
  11782. +
  11783. +static void yaffs_clear_inode(struct inode *inode)
  11784. +{
  11785. + struct yaffs_obj *obj;
  11786. + struct yaffs_dev *dev;
  11787. +
  11788. + obj = yaffs_inode_to_obj(inode);
  11789. +
  11790. + T(YAFFS_TRACE_OS,
  11791. + (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11792. + atomic_read(&inode->i_count),
  11793. + obj ? "object exists" : "null object"));
  11794. +
  11795. + if (obj) {
  11796. + dev = obj->my_dev;
  11797. + yaffs_gross_lock(dev);
  11798. + yaffs_unstitch_obj(inode, obj);
  11799. + yaffs_gross_unlock(dev);
  11800. + }
  11801. +
  11802. +}
  11803. +
  11804. +/* delete is called when the link count is zero and the inode
  11805. + * is put (ie. nobody wants to know about it anymore, time to
  11806. + * delete the file).
  11807. + * NB Must call clear_inode()
  11808. + */
  11809. +static void yaffs_delete_inode(struct inode *inode)
  11810. +{
  11811. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11812. + struct yaffs_dev *dev;
  11813. +
  11814. + T(YAFFS_TRACE_OS,
  11815. + (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11816. + atomic_read(&inode->i_count),
  11817. + obj ? "object exists" : "null object"));
  11818. +
  11819. + if (obj) {
  11820. + dev = obj->my_dev;
  11821. + yaffs_gross_lock(dev);
  11822. + yaffs_del_obj(obj);
  11823. + yaffs_gross_unlock(dev);
  11824. + }
  11825. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  11826. + truncate_inode_pages(&inode->i_data, 0);
  11827. +#endif
  11828. + clear_inode(inode);
  11829. +}
  11830. +#endif
  11831. +
  11832. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11833. +static int yaffs_file_flush(struct file *file, fl_owner_t id)
  11834. +#else
  11835. +static int yaffs_file_flush(struct file *file)
  11836. +#endif
  11837. +{
  11838. + struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
  11839. +
  11840. + struct yaffs_dev *dev = obj->my_dev;
  11841. +
  11842. + T(YAFFS_TRACE_OS,
  11843. + (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
  11844. + obj->dirty ? "dirty" : "clean"));
  11845. +
  11846. + yaffs_gross_lock(dev);
  11847. +
  11848. + yaffs_flush_file(obj, 1, 0);
  11849. +
  11850. + yaffs_gross_unlock(dev);
  11851. +
  11852. + return 0;
  11853. +}
  11854. +
  11855. +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  11856. +{
  11857. + /* Lifted from jffs2 */
  11858. +
  11859. + struct yaffs_obj *obj;
  11860. + unsigned char *pg_buf;
  11861. + int ret;
  11862. +
  11863. + struct yaffs_dev *dev;
  11864. +
  11865. + T(YAFFS_TRACE_OS,
  11866. + (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
  11867. + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
  11868. + (unsigned)PAGE_CACHE_SIZE));
  11869. +
  11870. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11871. +
  11872. + dev = obj->my_dev;
  11873. +
  11874. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11875. + BUG_ON(!PageLocked(pg));
  11876. +#else
  11877. + if (!PageLocked(pg))
  11878. + PAGE_BUG(pg);
  11879. +#endif
  11880. +
  11881. + pg_buf = kmap(pg);
  11882. + /* FIXME: Can kmap fail? */
  11883. +
  11884. + yaffs_gross_lock(dev);
  11885. +
  11886. + ret = yaffs_file_rd(obj, pg_buf,
  11887. + pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
  11888. +
  11889. + yaffs_gross_unlock(dev);
  11890. +
  11891. + if (ret >= 0)
  11892. + ret = 0;
  11893. +
  11894. + if (ret) {
  11895. + ClearPageUptodate(pg);
  11896. + SetPageError(pg);
  11897. + } else {
  11898. + SetPageUptodate(pg);
  11899. + ClearPageError(pg);
  11900. + }
  11901. +
  11902. + flush_dcache_page(pg);
  11903. + kunmap(pg);
  11904. +
  11905. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
  11906. + return ret;
  11907. +}
  11908. +
  11909. +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  11910. +{
  11911. + int ret = yaffs_readpage_nolock(f, pg);
  11912. + UnlockPage(pg);
  11913. + return ret;
  11914. +}
  11915. +
  11916. +static int yaffs_readpage(struct file *f, struct page *pg)
  11917. +{
  11918. + int ret;
  11919. +
  11920. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
  11921. + ret = yaffs_readpage_unlock(f, pg);
  11922. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
  11923. + return ret;
  11924. +}
  11925. +
  11926. +/* writepage inspired by/stolen from smbfs */
  11927. +
  11928. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11929. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  11930. +#else
  11931. +static int yaffs_writepage(struct page *page)
  11932. +#endif
  11933. +{
  11934. + struct yaffs_dev *dev;
  11935. + struct address_space *mapping = page->mapping;
  11936. + struct inode *inode;
  11937. + unsigned long end_index;
  11938. + char *buffer;
  11939. + struct yaffs_obj *obj;
  11940. + int n_written = 0;
  11941. + unsigned n_bytes;
  11942. + loff_t i_size;
  11943. +
  11944. + if (!mapping)
  11945. + BUG();
  11946. + inode = mapping->host;
  11947. + if (!inode)
  11948. + BUG();
  11949. + i_size = i_size_read(inode);
  11950. +
  11951. + end_index = i_size >> PAGE_CACHE_SHIFT;
  11952. +
  11953. + if (page->index < end_index)
  11954. + n_bytes = PAGE_CACHE_SIZE;
  11955. + else {
  11956. + n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
  11957. +
  11958. + if (page->index > end_index || !n_bytes) {
  11959. + T(YAFFS_TRACE_OS,
  11960. + (TSTR
  11961. + ("yaffs_writepage at %08x, inode size = %08x!!!\n"),
  11962. + (unsigned)(page->index << PAGE_CACHE_SHIFT),
  11963. + (unsigned)inode->i_size));
  11964. + T(YAFFS_TRACE_OS,
  11965. + (TSTR(" -> don't care!!\n")));
  11966. +
  11967. + zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  11968. + set_page_writeback(page);
  11969. + unlock_page(page);
  11970. + end_page_writeback(page);
  11971. + return 0;
  11972. + }
  11973. + }
  11974. +
  11975. + if (n_bytes != PAGE_CACHE_SIZE)
  11976. + zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
  11977. +
  11978. + get_page(page);
  11979. +
  11980. + buffer = kmap(page);
  11981. +
  11982. + obj = yaffs_inode_to_obj(inode);
  11983. + dev = obj->my_dev;
  11984. + yaffs_gross_lock(dev);
  11985. +
  11986. + T(YAFFS_TRACE_OS,
  11987. + (TSTR("yaffs_writepage at %08x, size %08x\n"),
  11988. + (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
  11989. + T(YAFFS_TRACE_OS,
  11990. + (TSTR("writepag0: obj = %05x, ino = %05x\n"),
  11991. + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
  11992. +
  11993. + n_written = yaffs_wr_file(obj, buffer,
  11994. + page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
  11995. +
  11996. + yaffs_touch_super(dev);
  11997. +
  11998. + T(YAFFS_TRACE_OS,
  11999. + (TSTR("writepag1: obj = %05x, ino = %05x\n"),
  12000. + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
  12001. +
  12002. + yaffs_gross_unlock(dev);
  12003. +
  12004. + kunmap(page);
  12005. + set_page_writeback(page);
  12006. + unlock_page(page);
  12007. + end_page_writeback(page);
  12008. + put_page(page);
  12009. +
  12010. + return (n_written == n_bytes) ? 0 : -ENOSPC;
  12011. +}
  12012. +
  12013. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  12014. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  12015. + loff_t pos, unsigned len, unsigned flags,
  12016. + struct page **pagep, void **fsdata)
  12017. +{
  12018. + struct page *pg = NULL;
  12019. + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  12020. +
  12021. + int ret = 0;
  12022. + int space_held = 0;
  12023. +
  12024. + /* Get a page */
  12025. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
  12026. + pg = grab_cache_page_write_begin(mapping, index, flags);
  12027. +#else
  12028. + pg = __grab_cache_page(mapping, index);
  12029. +#endif
  12030. +
  12031. + *pagep = pg;
  12032. + if (!pg) {
  12033. + ret = -ENOMEM;
  12034. + goto out;
  12035. + }
  12036. + T(YAFFS_TRACE_OS,
  12037. + (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
  12038. + (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0));
  12039. +
  12040. + /* Get fs space */
  12041. + space_held = yaffs_hold_space(filp);
  12042. +
  12043. + if (!space_held) {
  12044. + ret = -ENOSPC;
  12045. + goto out;
  12046. + }
  12047. +
  12048. + /* Update page if required */
  12049. +
  12050. + if (!Page_Uptodate(pg))
  12051. + ret = yaffs_readpage_nolock(filp, pg);
  12052. +
  12053. + if (ret)
  12054. + goto out;
  12055. +
  12056. + /* Happy path return */
  12057. + T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
  12058. +
  12059. + return 0;
  12060. +
  12061. +out:
  12062. + T(YAFFS_TRACE_OS,
  12063. + (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
  12064. + if (space_held)
  12065. + yaffs_release_space(filp);
  12066. + if (pg) {
  12067. + unlock_page(pg);
  12068. + page_cache_release(pg);
  12069. + }
  12070. + return ret;
  12071. +}
  12072. +
  12073. +#else
  12074. +
  12075. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  12076. + unsigned offset, unsigned to)
  12077. +{
  12078. + T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
  12079. +
  12080. + if (!Page_Uptodate(pg))
  12081. + return yaffs_readpage_nolock(f, pg);
  12082. + return 0;
  12083. +}
  12084. +#endif
  12085. +
  12086. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  12087. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  12088. + loff_t pos, unsigned len, unsigned copied,
  12089. + struct page *pg, void *fsdadata)
  12090. +{
  12091. + int ret = 0;
  12092. + void *addr, *kva;
  12093. + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  12094. +
  12095. + kva = kmap(pg);
  12096. + addr = kva + offset_into_page;
  12097. +
  12098. + T(YAFFS_TRACE_OS,
  12099. + ("yaffs_write_end addr %p pos %x n_bytes %d\n",
  12100. + addr, (unsigned)pos, copied));
  12101. +
  12102. + ret = yaffs_file_write(filp, addr, copied, &pos);
  12103. +
  12104. + if (ret != copied) {
  12105. + T(YAFFS_TRACE_OS,
  12106. + (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
  12107. + ret, copied));
  12108. + SetPageError(pg);
  12109. + } else {
  12110. + /* Nothing */
  12111. + }
  12112. +
  12113. + kunmap(pg);
  12114. +
  12115. + yaffs_release_space(filp);
  12116. + unlock_page(pg);
  12117. + page_cache_release(pg);
  12118. + return ret;
  12119. +}
  12120. +#else
  12121. +
  12122. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  12123. + unsigned to)
  12124. +{
  12125. + void *addr, *kva;
  12126. +
  12127. + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
  12128. + int n_bytes = to - offset;
  12129. + int n_written;
  12130. +
  12131. + unsigned spos = pos;
  12132. + unsigned saddr;
  12133. +
  12134. + kva = kmap(pg);
  12135. + addr = kva + offset;
  12136. +
  12137. + saddr = (unsigned)addr;
  12138. +
  12139. + T(YAFFS_TRACE_OS,
  12140. + (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
  12141. + saddr, spos, n_bytes));
  12142. +
  12143. + n_written = yaffs_file_write(f, addr, n_bytes, &pos);
  12144. +
  12145. + if (n_written != n_bytes) {
  12146. + T(YAFFS_TRACE_OS,
  12147. + (TSTR
  12148. + ("yaffs_commit_write not same size n_written %d n_bytes %d\n"),
  12149. + n_written, n_bytes));
  12150. + SetPageError(pg);
  12151. + } else {
  12152. + /* Nothing */
  12153. + }
  12154. +
  12155. + kunmap(pg);
  12156. +
  12157. + T(YAFFS_TRACE_OS,
  12158. + (TSTR("yaffs_commit_write returning %d\n"),
  12159. + n_written == n_bytes ? 0 : n_written));
  12160. +
  12161. + return n_written == n_bytes ? 0 : n_written;
  12162. +}
  12163. +#endif
  12164. +
  12165. +static void yaffs_fill_inode_from_obj(struct inode *inode,
  12166. + struct yaffs_obj *obj)
  12167. +{
  12168. + if (inode && obj) {
  12169. +
  12170. + /* Check mode against the variant type and attempt to repair if broken. */
  12171. + u32 mode = obj->yst_mode;
  12172. + switch (obj->variant_type) {
  12173. + case YAFFS_OBJECT_TYPE_FILE:
  12174. + if (!S_ISREG(mode)) {
  12175. + obj->yst_mode &= ~S_IFMT;
  12176. + obj->yst_mode |= S_IFREG;
  12177. + }
  12178. +
  12179. + break;
  12180. + case YAFFS_OBJECT_TYPE_SYMLINK:
  12181. + if (!S_ISLNK(mode)) {
  12182. + obj->yst_mode &= ~S_IFMT;
  12183. + obj->yst_mode |= S_IFLNK;
  12184. + }
  12185. +
  12186. + break;
  12187. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  12188. + if (!S_ISDIR(mode)) {
  12189. + obj->yst_mode &= ~S_IFMT;
  12190. + obj->yst_mode |= S_IFDIR;
  12191. + }
  12192. +
  12193. + break;
  12194. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  12195. + case YAFFS_OBJECT_TYPE_HARDLINK:
  12196. + case YAFFS_OBJECT_TYPE_SPECIAL:
  12197. + default:
  12198. + /* TODO? */
  12199. + break;
  12200. + }
  12201. +
  12202. + inode->i_flags |= S_NOATIME;
  12203. +
  12204. + inode->i_ino = obj->obj_id;
  12205. + inode->i_mode = obj->yst_mode;
  12206. + inode->i_uid = obj->yst_uid;
  12207. + inode->i_gid = obj->yst_gid;
  12208. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  12209. + inode->i_blksize = inode->i_sb->s_blocksize;
  12210. +#endif
  12211. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12212. +
  12213. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  12214. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  12215. + inode->i_atime.tv_nsec = 0;
  12216. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  12217. + inode->i_mtime.tv_nsec = 0;
  12218. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  12219. + inode->i_ctime.tv_nsec = 0;
  12220. +#else
  12221. + inode->i_rdev = obj->yst_rdev;
  12222. + inode->i_atime = obj->yst_atime;
  12223. + inode->i_mtime = obj->yst_mtime;
  12224. + inode->i_ctime = obj->yst_ctime;
  12225. +#endif
  12226. + inode->i_size = yaffs_get_obj_length(obj);
  12227. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12228. +
  12229. + inode->i_nlink = yaffs_get_obj_link_count(obj);
  12230. +
  12231. + T(YAFFS_TRACE_OS,
  12232. + (TSTR
  12233. + ("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
  12234. + inode->i_mode, inode->i_uid, inode->i_gid,
  12235. + (int)inode->i_size, atomic_read(&inode->i_count)));
  12236. +
  12237. + switch (obj->yst_mode & S_IFMT) {
  12238. + default: /* fifo, device or socket */
  12239. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12240. + init_special_inode(inode, obj->yst_mode,
  12241. + old_decode_dev(obj->yst_rdev));
  12242. +#else
  12243. + init_special_inode(inode, obj->yst_mode,
  12244. + (dev_t) (obj->yst_rdev));
  12245. +#endif
  12246. + break;
  12247. + case S_IFREG: /* file */
  12248. + inode->i_op = &yaffs_file_inode_operations;
  12249. + inode->i_fop = &yaffs_file_operations;
  12250. + inode->i_mapping->a_ops =
  12251. + &yaffs_file_address_operations;
  12252. + break;
  12253. + case S_IFDIR: /* directory */
  12254. + inode->i_op = &yaffs_dir_inode_operations;
  12255. + inode->i_fop = &yaffs_dir_operations;
  12256. + break;
  12257. + case S_IFLNK: /* symlink */
  12258. + inode->i_op = &yaffs_symlink_inode_operations;
  12259. + break;
  12260. + }
  12261. +
  12262. + yaffs_inode_to_obj_lv(inode) = obj;
  12263. +
  12264. + obj->my_inode = inode;
  12265. +
  12266. + } else {
  12267. + T(YAFFS_TRACE_OS,
  12268. + (TSTR("yaffs_fill_inode invalid parameters\n")));
  12269. + }
  12270. +
  12271. +}
  12272. +
  12273. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  12274. + struct yaffs_obj *obj)
  12275. +{
  12276. + struct inode *inode;
  12277. +
  12278. + if (!sb) {
  12279. + T(YAFFS_TRACE_OS,
  12280. + (TSTR("yaffs_get_inode for NULL super_block!!\n")));
  12281. + return NULL;
  12282. +
  12283. + }
  12284. +
  12285. + if (!obj) {
  12286. + T(YAFFS_TRACE_OS,
  12287. + (TSTR("yaffs_get_inode for NULL object!!\n")));
  12288. + return NULL;
  12289. +
  12290. + }
  12291. +
  12292. + T(YAFFS_TRACE_OS,
  12293. + (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
  12294. +
  12295. + inode = Y_IGET(sb, obj->obj_id);
  12296. + if (IS_ERR(inode))
  12297. + return NULL;
  12298. +
  12299. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  12300. + /* iget also increments the inode's i_count */
  12301. + /* NB You can't be holding gross_lock or deadlock will happen! */
  12302. +
  12303. + return inode;
  12304. +}
  12305. +
  12306. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  12307. + loff_t * pos)
  12308. +{
  12309. + struct yaffs_obj *obj;
  12310. + int n_written, ipos;
  12311. + struct inode *inode;
  12312. + struct yaffs_dev *dev;
  12313. +
  12314. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12315. +
  12316. + dev = obj->my_dev;
  12317. +
  12318. + yaffs_gross_lock(dev);
  12319. +
  12320. + inode = f->f_dentry->d_inode;
  12321. +
  12322. + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  12323. + ipos = inode->i_size;
  12324. + else
  12325. + ipos = *pos;
  12326. +
  12327. + if (!obj)
  12328. + T(YAFFS_TRACE_OS,
  12329. + (TSTR("yaffs_file_write: hey obj is null!\n")));
  12330. + else
  12331. + T(YAFFS_TRACE_OS,
  12332. + (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
  12333. + "to object %d at %d(%x)\n"),
  12334. + (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos));
  12335. +
  12336. + n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
  12337. +
  12338. + yaffs_touch_super(dev);
  12339. +
  12340. + T(YAFFS_TRACE_OS,
  12341. + (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
  12342. + (unsigned)n, (unsigned)n));
  12343. +
  12344. + if (n_written > 0) {
  12345. + ipos += n_written;
  12346. + *pos = ipos;
  12347. + if (ipos > inode->i_size) {
  12348. + inode->i_size = ipos;
  12349. + inode->i_blocks = (ipos + 511) >> 9;
  12350. +
  12351. + T(YAFFS_TRACE_OS,
  12352. + (TSTR("yaffs_file_write size updated to %d bytes, "
  12353. + "%d blocks\n"), ipos, (int)(inode->i_blocks)));
  12354. + }
  12355. +
  12356. + }
  12357. + yaffs_gross_unlock(dev);
  12358. + return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
  12359. +}
  12360. +
  12361. +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
  12362. +/* For now we just assume few parallel writes and check against a small number. */
  12363. +/* Todo: need to do this with a counter to handle parallel reads better */
  12364. +
  12365. +static ssize_t yaffs_hold_space(struct file *f)
  12366. +{
  12367. + struct yaffs_obj *obj;
  12368. + struct yaffs_dev *dev;
  12369. +
  12370. + int n_free_chunks;
  12371. +
  12372. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12373. +
  12374. + dev = obj->my_dev;
  12375. +
  12376. + yaffs_gross_lock(dev);
  12377. +
  12378. + n_free_chunks = yaffs_get_n_free_chunks(dev);
  12379. +
  12380. + yaffs_gross_unlock(dev);
  12381. +
  12382. + return (n_free_chunks > 20) ? 1 : 0;
  12383. +}
  12384. +
  12385. +static void yaffs_release_space(struct file *f)
  12386. +{
  12387. + struct yaffs_obj *obj;
  12388. + struct yaffs_dev *dev;
  12389. +
  12390. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12391. +
  12392. + dev = obj->my_dev;
  12393. +
  12394. + yaffs_gross_lock(dev);
  12395. +
  12396. + yaffs_gross_unlock(dev);
  12397. +}
  12398. +
  12399. +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
  12400. +{
  12401. + long long retval;
  12402. +
  12403. + lock_kernel();
  12404. +
  12405. + switch (origin) {
  12406. + case 2:
  12407. + offset += i_size_read(file->f_path.dentry->d_inode);
  12408. + break;
  12409. + case 1:
  12410. + offset += file->f_pos;
  12411. + }
  12412. + retval = -EINVAL;
  12413. +
  12414. + if (offset >= 0) {
  12415. + if (offset != file->f_pos)
  12416. + file->f_pos = offset;
  12417. +
  12418. + retval = offset;
  12419. + }
  12420. + unlock_kernel();
  12421. + return retval;
  12422. +}
  12423. +
  12424. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  12425. +{
  12426. + struct yaffs_obj *obj;
  12427. + struct yaffs_dev *dev;
  12428. + struct yaffs_search_context *sc;
  12429. + struct inode *inode = f->f_dentry->d_inode;
  12430. + unsigned long offset, curoffs;
  12431. + struct yaffs_obj *l;
  12432. + int ret_val = 0;
  12433. +
  12434. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12435. +
  12436. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12437. + dev = obj->my_dev;
  12438. +
  12439. + yaffs_gross_lock(dev);
  12440. +
  12441. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12442. +
  12443. + offset = f->f_pos;
  12444. +
  12445. + sc = yaffs_new_search(obj);
  12446. + if (!sc) {
  12447. + ret_val = -ENOMEM;
  12448. + goto out;
  12449. + }
  12450. +
  12451. + T(YAFFS_TRACE_OS,
  12452. + (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
  12453. +
  12454. + if (offset == 0) {
  12455. + T(YAFFS_TRACE_OS,
  12456. + (TSTR("yaffs_readdir: entry . ino %d \n"),
  12457. + (int)inode->i_ino));
  12458. + yaffs_gross_unlock(dev);
  12459. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
  12460. + yaffs_gross_lock(dev);
  12461. + goto out;
  12462. + }
  12463. + yaffs_gross_lock(dev);
  12464. + offset++;
  12465. + f->f_pos++;
  12466. + }
  12467. + if (offset == 1) {
  12468. + T(YAFFS_TRACE_OS,
  12469. + (TSTR("yaffs_readdir: entry .. ino %d \n"),
  12470. + (int)f->f_dentry->d_parent->d_inode->i_ino));
  12471. + yaffs_gross_unlock(dev);
  12472. + if (filldir(dirent, "..", 2, offset,
  12473. + f->f_dentry->d_parent->d_inode->i_ino,
  12474. + DT_DIR) < 0) {
  12475. + yaffs_gross_lock(dev);
  12476. + goto out;
  12477. + }
  12478. + yaffs_gross_lock(dev);
  12479. + offset++;
  12480. + f->f_pos++;
  12481. + }
  12482. +
  12483. + curoffs = 1;
  12484. +
  12485. + /* If the directory has changed since the open or last call to
  12486. + readdir, rewind to after the 2 canned entries. */
  12487. + if (f->f_version != inode->i_version) {
  12488. + offset = 2;
  12489. + f->f_pos = offset;
  12490. + f->f_version = inode->i_version;
  12491. + }
  12492. +
  12493. + while (sc->next_return) {
  12494. + curoffs++;
  12495. + l = sc->next_return;
  12496. + if (curoffs >= offset) {
  12497. + int this_inode = yaffs_get_obj_inode(l);
  12498. + int this_type = yaffs_get_obj_type(l);
  12499. +
  12500. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12501. + T(YAFFS_TRACE_OS,
  12502. + (TSTR("yaffs_readdir: %s inode %d\n"),
  12503. + name, yaffs_get_obj_inode(l)));
  12504. +
  12505. + yaffs_gross_unlock(dev);
  12506. +
  12507. + if (filldir(dirent,
  12508. + name,
  12509. + strlen(name),
  12510. + offset, this_inode, this_type) < 0) {
  12511. + yaffs_gross_lock(dev);
  12512. + goto out;
  12513. + }
  12514. +
  12515. + yaffs_gross_lock(dev);
  12516. +
  12517. + offset++;
  12518. + f->f_pos++;
  12519. + }
  12520. + yaffs_search_advance(sc);
  12521. + }
  12522. +
  12523. +out:
  12524. + yaffs_search_end(sc);
  12525. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12526. + yaffs_gross_unlock(dev);
  12527. +
  12528. + return ret_val;
  12529. +}
  12530. +
  12531. +/*
  12532. + * File creation. Allocate an inode, and we're done..
  12533. + */
  12534. +
  12535. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  12536. +#define YCRED(x) x
  12537. +#else
  12538. +#define YCRED(x) (x->cred)
  12539. +#endif
  12540. +
  12541. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12542. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12543. + dev_t rdev)
  12544. +#else
  12545. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12546. + int rdev)
  12547. +#endif
  12548. +{
  12549. + struct inode *inode;
  12550. +
  12551. + struct yaffs_obj *obj = NULL;
  12552. + struct yaffs_dev *dev;
  12553. +
  12554. + struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
  12555. +
  12556. + int error = -ENOSPC;
  12557. + uid_t uid = YCRED(current)->fsuid;
  12558. + gid_t gid =
  12559. + (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  12560. +
  12561. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  12562. + mode |= S_ISGID;
  12563. +
  12564. + if (parent) {
  12565. + T(YAFFS_TRACE_OS,
  12566. + (TSTR("yaffs_mknod: parent object %d type %d\n"),
  12567. + parent->obj_id, parent->variant_type));
  12568. + } else {
  12569. + T(YAFFS_TRACE_OS,
  12570. + (TSTR("yaffs_mknod: could not get parent object\n")));
  12571. + return -EPERM;
  12572. + }
  12573. +
  12574. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
  12575. + "mode %x dev %x\n"),
  12576. + dentry->d_name.name, mode, rdev));
  12577. +
  12578. + dev = parent->my_dev;
  12579. +
  12580. + yaffs_gross_lock(dev);
  12581. +
  12582. + switch (mode & S_IFMT) {
  12583. + default:
  12584. + /* Special (socket, fifo, device...) */
  12585. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
  12586. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12587. + obj =
  12588. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12589. + gid, old_encode_dev(rdev));
  12590. +#else
  12591. + obj =
  12592. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12593. + gid, rdev);
  12594. +#endif
  12595. + break;
  12596. + case S_IFREG: /* file */
  12597. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
  12598. + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
  12599. + gid);
  12600. + break;
  12601. + case S_IFDIR: /* directory */
  12602. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making directory\n")));
  12603. + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
  12604. + uid, gid);
  12605. + break;
  12606. + case S_IFLNK: /* symlink */
  12607. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
  12608. + obj = NULL; /* Do we ever get here? */
  12609. + break;
  12610. + }
  12611. +
  12612. + /* Can not call yaffs_get_inode() with gross lock held */
  12613. + yaffs_gross_unlock(dev);
  12614. +
  12615. + if (obj) {
  12616. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  12617. + d_instantiate(dentry, inode);
  12618. + update_dir_time(dir);
  12619. + T(YAFFS_TRACE_OS,
  12620. + (TSTR("yaffs_mknod created object %d count = %d\n"),
  12621. + obj->obj_id, atomic_read(&inode->i_count)));
  12622. + error = 0;
  12623. + yaffs_fill_inode_from_obj(dir, parent);
  12624. + } else {
  12625. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod failed making object\n")));
  12626. + error = -ENOMEM;
  12627. + }
  12628. +
  12629. + return error;
  12630. +}
  12631. +
  12632. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  12633. +{
  12634. + int ret_val;
  12635. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
  12636. + ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  12637. + return ret_val;
  12638. +}
  12639. +
  12640. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12641. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  12642. + struct nameidata *n)
  12643. +#else
  12644. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  12645. +#endif
  12646. +{
  12647. + T(YAFFS_TRACE_OS, (TSTR("yaffs_create\n")));
  12648. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  12649. +}
  12650. +
  12651. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  12652. +{
  12653. + int ret_val;
  12654. +
  12655. + struct yaffs_dev *dev;
  12656. + struct yaffs_obj *obj;
  12657. +
  12658. + T(YAFFS_TRACE_OS,
  12659. + (TSTR("yaffs_unlink %d:%s\n"),
  12660. + (int)(dir->i_ino), dentry->d_name.name));
  12661. + obj = yaffs_inode_to_obj(dir);
  12662. + dev = obj->my_dev;
  12663. +
  12664. + yaffs_gross_lock(dev);
  12665. +
  12666. + ret_val = yaffs_unlinker(obj, dentry->d_name.name);
  12667. +
  12668. + if (ret_val == YAFFS_OK) {
  12669. + dentry->d_inode->i_nlink--;
  12670. + dir->i_version++;
  12671. + yaffs_gross_unlock(dev);
  12672. + mark_inode_dirty(dentry->d_inode);
  12673. + update_dir_time(dir);
  12674. + return 0;
  12675. + }
  12676. + yaffs_gross_unlock(dev);
  12677. + return -ENOTEMPTY;
  12678. +}
  12679. +
  12680. +/*
  12681. + * Create a link...
  12682. + */
  12683. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  12684. + struct dentry *dentry)
  12685. +{
  12686. + struct inode *inode = old_dentry->d_inode;
  12687. + struct yaffs_obj *obj = NULL;
  12688. + struct yaffs_obj *link = NULL;
  12689. + struct yaffs_dev *dev;
  12690. +
  12691. + T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
  12692. +
  12693. + obj = yaffs_inode_to_obj(inode);
  12694. + dev = obj->my_dev;
  12695. +
  12696. + yaffs_gross_lock(dev);
  12697. +
  12698. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  12699. + link =
  12700. + yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12701. + obj);
  12702. +
  12703. + if (link) {
  12704. + old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
  12705. + d_instantiate(dentry, old_dentry->d_inode);
  12706. + atomic_inc(&old_dentry->d_inode->i_count);
  12707. + T(YAFFS_TRACE_OS,
  12708. + (TSTR("yaffs_link link count %d i_count %d\n"),
  12709. + old_dentry->d_inode->i_nlink,
  12710. + atomic_read(&old_dentry->d_inode->i_count)));
  12711. + }
  12712. +
  12713. + yaffs_gross_unlock(dev);
  12714. +
  12715. + if (link) {
  12716. + update_dir_time(dir);
  12717. + return 0;
  12718. + }
  12719. +
  12720. + return -EPERM;
  12721. +}
  12722. +
  12723. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  12724. + const char *symname)
  12725. +{
  12726. + struct yaffs_obj *obj;
  12727. + struct yaffs_dev *dev;
  12728. + uid_t uid = YCRED(current)->fsuid;
  12729. + gid_t gid =
  12730. + (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  12731. +
  12732. + T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
  12733. +
  12734. + dev = yaffs_inode_to_obj(dir)->my_dev;
  12735. + yaffs_gross_lock(dev);
  12736. + obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12737. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  12738. + yaffs_gross_unlock(dev);
  12739. +
  12740. + if (obj) {
  12741. + struct inode *inode;
  12742. +
  12743. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12744. + d_instantiate(dentry, inode);
  12745. + update_dir_time(dir);
  12746. + T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
  12747. + return 0;
  12748. + } else {
  12749. + T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
  12750. + }
  12751. +
  12752. + return -ENOMEM;
  12753. +}
  12754. +
  12755. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  12756. +static int yaffs_sync_object(struct file *file, int datasync)
  12757. +#else
  12758. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  12759. + int datasync)
  12760. +#endif
  12761. +{
  12762. +
  12763. + struct yaffs_obj *obj;
  12764. + struct yaffs_dev *dev;
  12765. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  12766. + struct dentry *dentry = file->f_path.dentry;
  12767. +#endif
  12768. +
  12769. + obj = yaffs_dentry_to_obj(dentry);
  12770. +
  12771. + dev = obj->my_dev;
  12772. +
  12773. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, (TSTR("yaffs_sync_object\n")));
  12774. + yaffs_gross_lock(dev);
  12775. + yaffs_flush_file(obj, 1, datasync);
  12776. + yaffs_gross_unlock(dev);
  12777. + return 0;
  12778. +}
  12779. +
  12780. +/*
  12781. + * The VFS layer already does all the dentry stuff for rename.
  12782. + *
  12783. + * NB: POSIX says you can rename an object over an old object of the same name
  12784. + */
  12785. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  12786. + struct inode *new_dir, struct dentry *new_dentry)
  12787. +{
  12788. + struct yaffs_dev *dev;
  12789. + int ret_val = YAFFS_FAIL;
  12790. + struct yaffs_obj *target;
  12791. +
  12792. + T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
  12793. + dev = yaffs_inode_to_obj(old_dir)->my_dev;
  12794. +
  12795. + yaffs_gross_lock(dev);
  12796. +
  12797. + /* Check if the target is an existing directory that is not empty. */
  12798. + target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
  12799. + new_dentry->d_name.name);
  12800. +
  12801. + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
  12802. + !list_empty(&target->variant.dir_variant.children)) {
  12803. +
  12804. + T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
  12805. +
  12806. + ret_val = YAFFS_FAIL;
  12807. + } else {
  12808. + /* Now does unlinking internally using shadowing mechanism */
  12809. + T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
  12810. +
  12811. + ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
  12812. + old_dentry->d_name.name,
  12813. + yaffs_inode_to_obj(new_dir),
  12814. + new_dentry->d_name.name);
  12815. + }
  12816. + yaffs_gross_unlock(dev);
  12817. +
  12818. + if (ret_val == YAFFS_OK) {
  12819. + if (target) {
  12820. + new_dentry->d_inode->i_nlink--;
  12821. + mark_inode_dirty(new_dentry->d_inode);
  12822. + }
  12823. +
  12824. + update_dir_time(old_dir);
  12825. + if (old_dir != new_dir)
  12826. + update_dir_time(new_dir);
  12827. + return 0;
  12828. + } else {
  12829. + return -ENOTEMPTY;
  12830. + }
  12831. +}
  12832. +
  12833. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  12834. +{
  12835. + struct inode *inode = dentry->d_inode;
  12836. + int error = 0;
  12837. + struct yaffs_dev *dev;
  12838. +
  12839. + T(YAFFS_TRACE_OS,
  12840. + (TSTR("yaffs_setattr of object %d\n"),
  12841. + yaffs_inode_to_obj(inode)->obj_id));
  12842. +
  12843. + /* Fail if a requested resize >= 2GB */
  12844. + if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
  12845. + error = -EINVAL;
  12846. +
  12847. + if (error == 0)
  12848. + error = inode_change_ok(inode, attr);
  12849. + if (error == 0) {
  12850. + int result;
  12851. + if (!error) {
  12852. + error = yaffs_vfs_setattr(inode, attr);
  12853. + T(YAFFS_TRACE_OS, (TSTR("inode_setattr called\n")));
  12854. + if (attr->ia_valid & ATTR_SIZE) {
  12855. + yaffs_vfs_setsize(inode, attr->ia_size);
  12856. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12857. + }
  12858. + }
  12859. + dev = yaffs_inode_to_obj(inode)->my_dev;
  12860. + if (attr->ia_valid & ATTR_SIZE) {
  12861. + T(YAFFS_TRACE_OS, (TSTR("resize to %d(%x)\n"),
  12862. + (int)(attr->ia_size),
  12863. + (int)(attr->ia_size)));
  12864. + }
  12865. + yaffs_gross_lock(dev);
  12866. + result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
  12867. + if (result == YAFFS_OK) {
  12868. + error = 0;
  12869. + } else {
  12870. + error = -EPERM;
  12871. + }
  12872. + yaffs_gross_unlock(dev);
  12873. +
  12874. + }
  12875. +
  12876. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setattr done returning %d\n"), error));
  12877. +
  12878. + return error;
  12879. +}
  12880. +
  12881. +#ifdef CONFIG_YAFFS_XATTR
  12882. +int yaffs_setxattr(struct dentry *dentry, const char *name,
  12883. + const void *value, size_t size, int flags)
  12884. +{
  12885. + struct inode *inode = dentry->d_inode;
  12886. + int error = 0;
  12887. + struct yaffs_dev *dev;
  12888. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12889. +
  12890. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr of object %d\n"), obj->obj_id));
  12891. +
  12892. + if (error == 0) {
  12893. + int result;
  12894. + dev = obj->my_dev;
  12895. + yaffs_gross_lock(dev);
  12896. + result = yaffs_set_xattrib(obj, name, value, size, flags);
  12897. + if (result == YAFFS_OK)
  12898. + error = 0;
  12899. + else if (result < 0)
  12900. + error = result;
  12901. + yaffs_gross_unlock(dev);
  12902. +
  12903. + }
  12904. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr done returning %d\n"), error));
  12905. +
  12906. + return error;
  12907. +}
  12908. +
  12909. +ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
  12910. + size_t size)
  12911. +{
  12912. + struct inode *inode = dentry->d_inode;
  12913. + int error = 0;
  12914. + struct yaffs_dev *dev;
  12915. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12916. +
  12917. + T(YAFFS_TRACE_OS,
  12918. + (TSTR("yaffs_getxattr \"%s\" from object %d\n"), name, obj->obj_id));
  12919. +
  12920. + if (error == 0) {
  12921. + dev = obj->my_dev;
  12922. + yaffs_gross_lock(dev);
  12923. + error = yaffs_get_xattrib(obj, name, buff, size);
  12924. + yaffs_gross_unlock(dev);
  12925. +
  12926. + }
  12927. + T(YAFFS_TRACE_OS, (TSTR("yaffs_getxattr done returning %d\n"), error));
  12928. +
  12929. + return error;
  12930. +}
  12931. +
  12932. +int yaffs_removexattr(struct dentry *dentry, const char *name)
  12933. +{
  12934. + struct inode *inode = dentry->d_inode;
  12935. + int error = 0;
  12936. + struct yaffs_dev *dev;
  12937. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12938. +
  12939. + T(YAFFS_TRACE_OS,
  12940. + (TSTR("yaffs_removexattr of object %d\n"), obj->obj_id));
  12941. +
  12942. + if (error == 0) {
  12943. + int result;
  12944. + dev = obj->my_dev;
  12945. + yaffs_gross_lock(dev);
  12946. + result = yaffs_remove_xattrib(obj, name);
  12947. + if (result == YAFFS_OK)
  12948. + error = 0;
  12949. + else if (result < 0)
  12950. + error = result;
  12951. + yaffs_gross_unlock(dev);
  12952. +
  12953. + }
  12954. + T(YAFFS_TRACE_OS,
  12955. + (TSTR("yaffs_removexattr done returning %d\n"), error));
  12956. +
  12957. + return error;
  12958. +}
  12959. +
  12960. +ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
  12961. +{
  12962. + struct inode *inode = dentry->d_inode;
  12963. + int error = 0;
  12964. + struct yaffs_dev *dev;
  12965. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12966. +
  12967. + T(YAFFS_TRACE_OS,
  12968. + (TSTR("yaffs_listxattr of object %d\n"), obj->obj_id));
  12969. +
  12970. + if (error == 0) {
  12971. + dev = obj->my_dev;
  12972. + yaffs_gross_lock(dev);
  12973. + error = yaffs_list_xattrib(obj, buff, size);
  12974. + yaffs_gross_unlock(dev);
  12975. +
  12976. + }
  12977. + T(YAFFS_TRACE_OS, (TSTR("yaffs_listxattr done returning %d\n"), error));
  12978. +
  12979. + return error;
  12980. +}
  12981. +
  12982. +#endif
  12983. +
  12984. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  12985. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  12986. +{
  12987. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  12988. + struct super_block *sb = dentry->d_sb;
  12989. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12990. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  12991. +{
  12992. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  12993. +#else
  12994. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  12995. +{
  12996. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  12997. +#endif
  12998. +
  12999. + T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
  13000. +
  13001. + yaffs_gross_lock(dev);
  13002. +
  13003. + buf->f_type = YAFFS_MAGIC;
  13004. + buf->f_bsize = sb->s_blocksize;
  13005. + buf->f_namelen = 255;
  13006. +
  13007. + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
  13008. + /* Do this if chunk size is not a power of 2 */
  13009. +
  13010. + uint64_t bytes_in_dev;
  13011. + uint64_t bytes_free;
  13012. +
  13013. + bytes_in_dev =
  13014. + ((uint64_t)
  13015. + ((dev->param.end_block - dev->param.start_block +
  13016. + 1))) * ((uint64_t) (dev->param.chunks_per_block *
  13017. + dev->data_bytes_per_chunk));
  13018. +
  13019. + do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
  13020. + buf->f_blocks = bytes_in_dev;
  13021. +
  13022. + bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
  13023. + ((uint64_t) (dev->data_bytes_per_chunk));
  13024. +
  13025. + do_div(bytes_free, sb->s_blocksize);
  13026. +
  13027. + buf->f_bfree = bytes_free;
  13028. +
  13029. + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
  13030. +
  13031. + buf->f_blocks =
  13032. + (dev->param.end_block - dev->param.start_block + 1) *
  13033. + dev->param.chunks_per_block /
  13034. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13035. + buf->f_bfree =
  13036. + yaffs_get_n_free_chunks(dev) /
  13037. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13038. + } else {
  13039. + buf->f_blocks =
  13040. + (dev->param.end_block - dev->param.start_block + 1) *
  13041. + dev->param.chunks_per_block *
  13042. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13043. +
  13044. + buf->f_bfree =
  13045. + yaffs_get_n_free_chunks(dev) *
  13046. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13047. + }
  13048. +
  13049. + buf->f_files = 0;
  13050. + buf->f_ffree = 0;
  13051. + buf->f_bavail = buf->f_bfree;
  13052. +
  13053. + yaffs_gross_unlock(dev);
  13054. + return 0;
  13055. +}
  13056. +
  13057. +static void yaffs_flush_inodes(struct super_block *sb)
  13058. +{
  13059. + struct inode *iptr;
  13060. + struct yaffs_obj *obj;
  13061. +
  13062. + list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
  13063. + obj = yaffs_inode_to_obj(iptr);
  13064. + if (obj) {
  13065. + T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
  13066. + obj->obj_id));
  13067. + yaffs_flush_file(obj, 1, 0);
  13068. + }
  13069. + }
  13070. +}
  13071. +
  13072. +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
  13073. +{
  13074. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13075. + if (!dev)
  13076. + return;
  13077. +
  13078. + yaffs_flush_inodes(sb);
  13079. + yaffs_update_dirty_dirs(dev);
  13080. + yaffs_flush_whole_cache(dev);
  13081. + if (do_checkpoint)
  13082. + yaffs_checkpoint_save(dev);
  13083. +}
  13084. +
  13085. +static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
  13086. +{
  13087. + unsigned erased_chunks =
  13088. + dev->n_erased_blocks * dev->param.chunks_per_block;
  13089. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13090. + unsigned scattered = 0; /* Free chunks not in an erased block */
  13091. +
  13092. + if (erased_chunks < dev->n_free_chunks)
  13093. + scattered = (dev->n_free_chunks - erased_chunks);
  13094. +
  13095. + if (!context->bg_running)
  13096. + return 0;
  13097. + else if (scattered < (dev->param.chunks_per_block * 2))
  13098. + return 0;
  13099. + else if (erased_chunks > dev->n_free_chunks / 2)
  13100. + return 0;
  13101. + else if (erased_chunks > dev->n_free_chunks / 4)
  13102. + return 1;
  13103. + else
  13104. + return 2;
  13105. +}
  13106. +
  13107. +static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
  13108. +{
  13109. +
  13110. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13111. + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
  13112. + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
  13113. + int do_checkpoint;
  13114. +
  13115. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13116. + (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
  13117. + gc_urgent,
  13118. + sb->s_dirt ? "dirty" : "clean",
  13119. + request_checkpoint ? "checkpoint requested" : "no checkpoint",
  13120. + oneshot_checkpoint ? " one-shot" : ""));
  13121. +
  13122. + yaffs_gross_lock(dev);
  13123. + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
  13124. + oneshot_checkpoint) && !dev->is_checkpointed;
  13125. +
  13126. + if (sb->s_dirt || do_checkpoint) {
  13127. + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
  13128. + sb->s_dirt = 0;
  13129. + if (oneshot_checkpoint)
  13130. + yaffs_auto_checkpoint &= ~4;
  13131. + }
  13132. + yaffs_gross_unlock(dev);
  13133. +
  13134. + return 0;
  13135. +}
  13136. +
  13137. +/*
  13138. + * yaffs background thread functions .
  13139. + * yaffs_bg_thread_fn() the thread function
  13140. + * yaffs_bg_start() launches the background thread.
  13141. + * yaffs_bg_stop() cleans up the background thread.
  13142. + *
  13143. + * NB:
  13144. + * The thread should only run after the yaffs is initialised
  13145. + * The thread should be stopped before yaffs is unmounted.
  13146. + * The thread should not do any writing while the fs is in read only.
  13147. + */
  13148. +
  13149. +#ifdef YAFFS_COMPILE_BACKGROUND
  13150. +
  13151. +void yaffs_background_waker(unsigned long data)
  13152. +{
  13153. + wake_up_process((struct task_struct *)data);
  13154. +}
  13155. +
  13156. +static int yaffs_bg_thread_fn(void *data)
  13157. +{
  13158. + struct yaffs_dev *dev = (struct yaffs_dev *)data;
  13159. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13160. + unsigned long now = jiffies;
  13161. + unsigned long next_dir_update = now;
  13162. + unsigned long next_gc = now;
  13163. + unsigned long expires;
  13164. + unsigned int urgency;
  13165. +
  13166. + int gc_result;
  13167. + struct timer_list timer;
  13168. +
  13169. + T(YAFFS_TRACE_BACKGROUND,
  13170. + (TSTR("yaffs_background starting for dev %p\n"), (void *)dev));
  13171. +
  13172. +#ifdef YAFFS_COMPILE_FREEZER
  13173. + set_freezable();
  13174. +#endif
  13175. + while (context->bg_running) {
  13176. + T(YAFFS_TRACE_BACKGROUND, (TSTR("yaffs_background\n")));
  13177. +
  13178. + if (kthread_should_stop())
  13179. + break;
  13180. +
  13181. +#ifdef YAFFS_COMPILE_FREEZER
  13182. + if (try_to_freeze())
  13183. + continue;
  13184. +#endif
  13185. + yaffs_gross_lock(dev);
  13186. +
  13187. + now = jiffies;
  13188. +
  13189. + if (time_after(now, next_dir_update) && yaffs_bg_enable) {
  13190. + yaffs_update_dirty_dirs(dev);
  13191. + next_dir_update = now + HZ;
  13192. + }
  13193. +
  13194. + if (time_after(now, next_gc) && yaffs_bg_enable) {
  13195. + if (!dev->is_checkpointed) {
  13196. + urgency = yaffs_bg_gc_urgency(dev);
  13197. + gc_result = yaffs_bg_gc(dev, urgency);
  13198. + if (urgency > 1)
  13199. + next_gc = now + HZ / 20 + 1;
  13200. + else if (urgency > 0)
  13201. + next_gc = now + HZ / 10 + 1;
  13202. + else
  13203. + next_gc = now + HZ * 2;
  13204. + } else {
  13205. + /*
  13206. + * gc not running so set to next_dir_update
  13207. + * to cut down on wake ups
  13208. + */
  13209. + next_gc = next_dir_update;
  13210. + }
  13211. + }
  13212. + yaffs_gross_unlock(dev);
  13213. +#if 1
  13214. + expires = next_dir_update;
  13215. + if (time_before(next_gc, expires))
  13216. + expires = next_gc;
  13217. + if (time_before(expires, now))
  13218. + expires = now + HZ;
  13219. +
  13220. + Y_INIT_TIMER(&timer);
  13221. + timer.expires = expires + 1;
  13222. + timer.data = (unsigned long)current;
  13223. + timer.function = yaffs_background_waker;
  13224. +
  13225. + set_current_state(TASK_INTERRUPTIBLE);
  13226. + add_timer(&timer);
  13227. + schedule();
  13228. + del_timer_sync(&timer);
  13229. +#else
  13230. + msleep(10);
  13231. +#endif
  13232. + }
  13233. +
  13234. + return 0;
  13235. +}
  13236. +
  13237. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13238. +{
  13239. + int retval = 0;
  13240. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13241. +
  13242. + if (dev->read_only)
  13243. + return -1;
  13244. +
  13245. + context->bg_running = 1;
  13246. +
  13247. + context->bg_thread = kthread_run(yaffs_bg_thread_fn,
  13248. + (void *)dev, "yaffs-bg-%d",
  13249. + context->mount_id);
  13250. +
  13251. + if (IS_ERR(context->bg_thread)) {
  13252. + retval = PTR_ERR(context->bg_thread);
  13253. + context->bg_thread = NULL;
  13254. + context->bg_running = 0;
  13255. + }
  13256. + return retval;
  13257. +}
  13258. +
  13259. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13260. +{
  13261. + struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
  13262. +
  13263. + ctxt->bg_running = 0;
  13264. +
  13265. + if (ctxt->bg_thread) {
  13266. + kthread_stop(ctxt->bg_thread);
  13267. + ctxt->bg_thread = NULL;
  13268. + }
  13269. +}
  13270. +#else
  13271. +static int yaffs_bg_thread_fn(void *data)
  13272. +{
  13273. + return 0;
  13274. +}
  13275. +
  13276. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13277. +{
  13278. + return 0;
  13279. +}
  13280. +
  13281. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13282. +{
  13283. +}
  13284. +#endif
  13285. +
  13286. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13287. +static void yaffs_write_super(struct super_block *sb)
  13288. +#else
  13289. +static int yaffs_write_super(struct super_block *sb)
  13290. +#endif
  13291. +{
  13292. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
  13293. +
  13294. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13295. + (TSTR("yaffs_write_super%s\n"),
  13296. + request_checkpoint ? " checkpt" : ""));
  13297. +
  13298. + yaffs_do_sync_fs(sb, request_checkpoint);
  13299. +
  13300. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  13301. + return 0;
  13302. +#endif
  13303. +}
  13304. +
  13305. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13306. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  13307. +#else
  13308. +static int yaffs_sync_fs(struct super_block *sb)
  13309. +#endif
  13310. +{
  13311. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
  13312. +
  13313. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  13314. + (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
  13315. +
  13316. + yaffs_do_sync_fs(sb, request_checkpoint);
  13317. +
  13318. + return 0;
  13319. +}
  13320. +
  13321. +#ifdef YAFFS_USE_OWN_IGET
  13322. +
  13323. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  13324. +{
  13325. + struct inode *inode;
  13326. + struct yaffs_obj *obj;
  13327. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13328. +
  13329. + T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
  13330. +
  13331. + inode = iget_locked(sb, ino);
  13332. + if (!inode)
  13333. + return ERR_PTR(-ENOMEM);
  13334. + if (!(inode->i_state & I_NEW))
  13335. + return inode;
  13336. +
  13337. + /* NB This is called as a side effect of other functions, but
  13338. + * we had to release the lock to prevent deadlocks, so
  13339. + * need to lock again.
  13340. + */
  13341. +
  13342. + yaffs_gross_lock(dev);
  13343. +
  13344. + obj = yaffs_find_by_number(dev, inode->i_ino);
  13345. +
  13346. + yaffs_fill_inode_from_obj(inode, obj);
  13347. +
  13348. + yaffs_gross_unlock(dev);
  13349. +
  13350. + unlock_new_inode(inode);
  13351. + return inode;
  13352. +}
  13353. +
  13354. +#else
  13355. +
  13356. +static void yaffs_read_inode(struct inode *inode)
  13357. +{
  13358. + /* NB This is called as a side effect of other functions, but
  13359. + * we had to release the lock to prevent deadlocks, so
  13360. + * need to lock again.
  13361. + */
  13362. +
  13363. + struct yaffs_obj *obj;
  13364. + struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
  13365. +
  13366. + T(YAFFS_TRACE_OS,
  13367. + (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
  13368. +
  13369. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  13370. + yaffs_gross_lock(dev);
  13371. +
  13372. + obj = yaffs_find_by_number(dev, inode->i_ino);
  13373. +
  13374. + yaffs_fill_inode_from_obj(inode, obj);
  13375. +
  13376. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  13377. + yaffs_gross_unlock(dev);
  13378. +}
  13379. +
  13380. +#endif
  13381. +
  13382. +static LIST_HEAD(yaffs_context_list);
  13383. +struct mutex yaffs_context_lock;
  13384. +
  13385. +static void yaffs_put_super(struct super_block *sb)
  13386. +{
  13387. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13388. +
  13389. + T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
  13390. +
  13391. + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13392. + (TSTR("Shutting down yaffs background thread\n")));
  13393. + yaffs_bg_stop(dev);
  13394. + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13395. + (TSTR("yaffs background thread shut down\n")));
  13396. +
  13397. + yaffs_gross_lock(dev);
  13398. +
  13399. + yaffs_flush_super(sb, 1);
  13400. +
  13401. + if (yaffs_dev_to_lc(dev)->put_super_fn)
  13402. + yaffs_dev_to_lc(dev)->put_super_fn(sb);
  13403. +
  13404. + yaffs_deinitialise(dev);
  13405. +
  13406. + yaffs_gross_unlock(dev);
  13407. +
  13408. + mutex_lock(&yaffs_context_lock);
  13409. + list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
  13410. + mutex_unlock(&yaffs_context_lock);
  13411. +
  13412. + if (yaffs_dev_to_lc(dev)->spare_buffer) {
  13413. + YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
  13414. + yaffs_dev_to_lc(dev)->spare_buffer = NULL;
  13415. + }
  13416. +
  13417. + kfree(dev);
  13418. +}
  13419. +
  13420. +static void yaffs_mtd_put_super(struct super_block *sb)
  13421. +{
  13422. + struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
  13423. +
  13424. + if (mtd->sync)
  13425. + mtd->sync(mtd);
  13426. +
  13427. + put_mtd_device(mtd);
  13428. +}
  13429. +
  13430. +static void yaffs_touch_super(struct yaffs_dev *dev)
  13431. +{
  13432. + struct super_block *sb = yaffs_dev_to_lc(dev)->super;
  13433. +
  13434. + T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
  13435. + if (sb)
  13436. + sb->s_dirt = 1;
  13437. +}
  13438. +
  13439. +struct yaffs_options {
  13440. + int inband_tags;
  13441. + int skip_checkpoint_read;
  13442. + int skip_checkpoint_write;
  13443. + int no_cache;
  13444. + int tags_ecc_on;
  13445. + int tags_ecc_overridden;
  13446. + int lazy_loading_enabled;
  13447. + int lazy_loading_overridden;
  13448. + int empty_lost_and_found;
  13449. + int empty_lost_and_found_overridden;
  13450. +};
  13451. +
  13452. +#define MAX_OPT_LEN 30
  13453. +static int yaffs_parse_options(struct yaffs_options *options,
  13454. + const char *options_str)
  13455. +{
  13456. + char cur_opt[MAX_OPT_LEN + 1];
  13457. + int p;
  13458. + int error = 0;
  13459. +
  13460. + /* Parse through the options which is a comma seperated list */
  13461. +
  13462. + while (options_str && *options_str && !error) {
  13463. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  13464. + p = 0;
  13465. +
  13466. + while (*options_str == ',')
  13467. + options_str++;
  13468. +
  13469. + while (*options_str && *options_str != ',') {
  13470. + if (p < MAX_OPT_LEN) {
  13471. + cur_opt[p] = *options_str;
  13472. + p++;
  13473. + }
  13474. + options_str++;
  13475. + }
  13476. +
  13477. + if (!strcmp(cur_opt, "inband-tags")) {
  13478. + options->inband_tags = 1;
  13479. + } else if (!strcmp(cur_opt, "tags-ecc-off")) {
  13480. + options->tags_ecc_on = 0;
  13481. + options->tags_ecc_overridden = 1;
  13482. + } else if (!strcmp(cur_opt, "tags-ecc-on")) {
  13483. + options->tags_ecc_on = 1;
  13484. + options->tags_ecc_overridden = 1;
  13485. + } else if (!strcmp(cur_opt, "lazy-loading-off")) {
  13486. + options->lazy_loading_enabled = 0;
  13487. + options->lazy_loading_overridden = 1;
  13488. + } else if (!strcmp(cur_opt, "lazy-loading-on")) {
  13489. + options->lazy_loading_enabled = 1;
  13490. + options->lazy_loading_overridden = 1;
  13491. + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
  13492. + options->empty_lost_and_found = 0;
  13493. + options->empty_lost_and_found_overridden = 1;
  13494. + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
  13495. + options->empty_lost_and_found = 1;
  13496. + options->empty_lost_and_found_overridden = 1;
  13497. + } else if (!strcmp(cur_opt, "no-cache")) {
  13498. + options->no_cache = 1;
  13499. + } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
  13500. + options->skip_checkpoint_read = 1;
  13501. + } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
  13502. + options->skip_checkpoint_write = 1;
  13503. + } else if (!strcmp(cur_opt, "no-checkpoint")) {
  13504. + options->skip_checkpoint_read = 1;
  13505. + options->skip_checkpoint_write = 1;
  13506. + } else {
  13507. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  13508. + cur_opt);
  13509. + error = 1;
  13510. + }
  13511. + }
  13512. +
  13513. + return error;
  13514. +}
  13515. +
  13516. +static struct super_block *yaffs_internal_read_super(int yaffs_version,
  13517. + struct super_block *sb,
  13518. + void *data, int silent)
  13519. +{
  13520. + int n_blocks;
  13521. + struct inode *inode = NULL;
  13522. + struct dentry *root;
  13523. + struct yaffs_dev *dev = 0;
  13524. + char devname_buf[BDEVNAME_SIZE + 1];
  13525. + struct mtd_info *mtd;
  13526. + int err;
  13527. + char *data_str = (char *)data;
  13528. + struct yaffs_linux_context *context = NULL;
  13529. + struct yaffs_param *param;
  13530. +
  13531. + int read_only = 0;
  13532. +
  13533. + struct yaffs_options options;
  13534. +
  13535. + unsigned mount_id;
  13536. + int found;
  13537. + struct yaffs_linux_context *context_iterator;
  13538. + struct list_head *l;
  13539. +
  13540. + sb->s_magic = YAFFS_MAGIC;
  13541. + sb->s_op = &yaffs_super_ops;
  13542. + sb->s_flags |= MS_NOATIME;
  13543. +
  13544. + read_only = ((sb->s_flags & MS_RDONLY) != 0);
  13545. +
  13546. +#ifdef YAFFS_COMPILE_EXPORTFS
  13547. + sb->s_export_op = &yaffs_export_ops;
  13548. +#endif
  13549. +
  13550. + if (!sb)
  13551. + printk(KERN_INFO "yaffs: sb is NULL\n");
  13552. + else if (!sb->s_dev)
  13553. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  13554. + else if (!yaffs_devname(sb, devname_buf))
  13555. + printk(KERN_INFO "yaffs: devname is NULL\n");
  13556. + else
  13557. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
  13558. + sb->s_dev,
  13559. + yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
  13560. +
  13561. + if (!data_str)
  13562. + data_str = "";
  13563. +
  13564. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  13565. +
  13566. + memset(&options, 0, sizeof(options));
  13567. +
  13568. + if (yaffs_parse_options(&options, data_str)) {
  13569. + /* Option parsing failed */
  13570. + return NULL;
  13571. + }
  13572. +
  13573. + sb->s_blocksize = PAGE_CACHE_SIZE;
  13574. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  13575. +
  13576. + T(YAFFS_TRACE_OS,
  13577. + (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
  13578. + T(YAFFS_TRACE_OS,
  13579. + (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
  13580. +
  13581. + T(YAFFS_TRACE_ALWAYS,
  13582. + (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
  13583. + MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
  13584. +
  13585. + /* Check it's an mtd device..... */
  13586. + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
  13587. + return NULL; /* This isn't an mtd device */
  13588. +
  13589. + /* Get the device */
  13590. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  13591. + if (!mtd) {
  13592. + T(YAFFS_TRACE_ALWAYS,
  13593. + (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
  13594. + MINOR(sb->s_dev)));
  13595. + return NULL;
  13596. + }
  13597. + /* Check it's NAND */
  13598. + if (mtd->type != MTD_NANDFLASH) {
  13599. + T(YAFFS_TRACE_ALWAYS,
  13600. + (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
  13601. + mtd->type));
  13602. + return NULL;
  13603. + }
  13604. +
  13605. + T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
  13606. + T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
  13607. + T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
  13608. + T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
  13609. + T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
  13610. + T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
  13611. + T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
  13612. + T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
  13613. + T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
  13614. + T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
  13615. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  13616. + T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
  13617. +#else
  13618. + T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
  13619. +#endif
  13620. +
  13621. +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
  13622. +
  13623. + if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
  13624. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
  13625. + yaffs_version = 2;
  13626. + }
  13627. +
  13628. + /* Added NCB 26/5/2006 for completeness */
  13629. + if (yaffs_version == 2 && !options.inband_tags
  13630. + && WRITE_SIZE(mtd) == 512) {
  13631. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
  13632. + yaffs_version = 1;
  13633. + }
  13634. +#endif
  13635. +
  13636. + if (yaffs_version == 2) {
  13637. + /* Check for version 2 style functions */
  13638. + if (!mtd->erase ||
  13639. + !mtd->block_isbad ||
  13640. + !mtd->block_markbad || !mtd->read || !mtd->write ||
  13641. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13642. + !mtd->read_oob || !mtd->write_oob) {
  13643. +#else
  13644. + !mtd->write_ecc ||
  13645. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  13646. +#endif
  13647. + T(YAFFS_TRACE_ALWAYS,
  13648. + (TSTR("yaffs: MTD device does not support required "
  13649. + "functions\n")));
  13650. + return NULL;
  13651. + }
  13652. +
  13653. + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  13654. + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  13655. + !options.inband_tags) {
  13656. + T(YAFFS_TRACE_ALWAYS,
  13657. + (TSTR("yaffs: MTD device does not have the "
  13658. + "right page sizes\n")));
  13659. + return NULL;
  13660. + }
  13661. + } else {
  13662. + /* Check for V1 style functions */
  13663. + if (!mtd->erase || !mtd->read || !mtd->write ||
  13664. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13665. + !mtd->read_oob || !mtd->write_oob) {
  13666. +#else
  13667. + !mtd->write_ecc ||
  13668. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  13669. +#endif
  13670. + T(YAFFS_TRACE_ALWAYS,
  13671. + (TSTR("yaffs: MTD device does not support required "
  13672. + "functions\n")));
  13673. + return NULL;
  13674. + }
  13675. +
  13676. + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  13677. + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  13678. + T(YAFFS_TRACE_ALWAYS,
  13679. + (TSTR("yaffs: MTD device does not support have the "
  13680. + "right page sizes\n")));
  13681. + return NULL;
  13682. + }
  13683. + }
  13684. +
  13685. + /* OK, so if we got here, we have an MTD that's NAND and looks
  13686. + * like it has the right capabilities
  13687. + * Set the struct yaffs_dev up for mtd
  13688. + */
  13689. +
  13690. + if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
  13691. + read_only = 1;
  13692. + printk(KERN_INFO
  13693. + "yaffs: mtd is read only, setting superblock read only");
  13694. + sb->s_flags |= MS_RDONLY;
  13695. + }
  13696. +
  13697. + dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
  13698. + context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
  13699. +
  13700. + if (!dev || !context) {
  13701. + if (dev)
  13702. + kfree(dev);
  13703. + if (context)
  13704. + kfree(context);
  13705. + dev = NULL;
  13706. + context = NULL;
  13707. + }
  13708. +
  13709. + if (!dev) {
  13710. + /* Deep shit could not allocate device structure */
  13711. + T(YAFFS_TRACE_ALWAYS,
  13712. + (TSTR("yaffs_read_super: Failed trying to allocate "
  13713. + "struct yaffs_dev. \n")));
  13714. + return NULL;
  13715. + }
  13716. + memset(dev, 0, sizeof(struct yaffs_dev));
  13717. + param = &(dev->param);
  13718. +
  13719. + memset(context, 0, sizeof(struct yaffs_linux_context));
  13720. + dev->os_context = context;
  13721. + INIT_LIST_HEAD(&(context->context_list));
  13722. + context->dev = dev;
  13723. + context->super = sb;
  13724. +
  13725. + dev->read_only = read_only;
  13726. +
  13727. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13728. + sb->s_fs_info = dev;
  13729. +#else
  13730. + sb->u.generic_sbp = dev;
  13731. +#endif
  13732. +
  13733. + dev->driver_context = mtd;
  13734. + param->name = mtd->name;
  13735. +
  13736. + /* Set up the memory size parameters.... */
  13737. +
  13738. + n_blocks =
  13739. + YCALCBLOCKS(mtd->size,
  13740. + (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
  13741. +
  13742. + param->start_block = 0;
  13743. + param->end_block = n_blocks - 1;
  13744. + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
  13745. + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
  13746. + param->n_reserved_blocks = 5;
  13747. + param->n_caches = (options.no_cache) ? 0 : 10;
  13748. + param->inband_tags = options.inband_tags;
  13749. +
  13750. +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  13751. + param->disable_lazy_load = 1;
  13752. +#endif
  13753. +#ifdef CONFIG_YAFFS_XATTR
  13754. + param->enable_xattr = 1;
  13755. +#endif
  13756. + if (options.lazy_loading_overridden)
  13757. + param->disable_lazy_load = !options.lazy_loading_enabled;
  13758. +
  13759. +#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
  13760. + param->no_tags_ecc = 1;
  13761. +#endif
  13762. +
  13763. +#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
  13764. +#else
  13765. + param->defered_dir_update = 1;
  13766. +#endif
  13767. +
  13768. + if (options.tags_ecc_overridden)
  13769. + param->no_tags_ecc = !options.tags_ecc_on;
  13770. +
  13771. +#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
  13772. + param->empty_lost_n_found = 1;
  13773. +#endif
  13774. +
  13775. +#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
  13776. + param->refresh_period = 0;
  13777. +#else
  13778. + param->refresh_period = 500;
  13779. +#endif
  13780. +
  13781. +#ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
  13782. + param->always_check_erased = 1;
  13783. +#endif
  13784. +
  13785. + if (options.empty_lost_and_found_overridden)
  13786. + param->empty_lost_n_found = options.empty_lost_and_found;
  13787. +
  13788. + /* ... and the functions. */
  13789. + if (yaffs_version == 2) {
  13790. + param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
  13791. + param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
  13792. + param->bad_block_fn = nandmtd2_mark_block_bad;
  13793. + param->query_block_fn = nandmtd2_query_block;
  13794. + yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
  13795. + param->is_yaffs2 = 1;
  13796. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13797. + param->total_bytes_per_chunk = mtd->writesize;
  13798. + param->chunks_per_block = mtd->erasesize / mtd->writesize;
  13799. +#else
  13800. + param->total_bytes_per_chunk = mtd->oobblock;
  13801. + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
  13802. +#endif
  13803. + n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  13804. +
  13805. + param->start_block = 0;
  13806. + param->end_block = n_blocks - 1;
  13807. + } else {
  13808. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13809. + /* use the MTD interface in yaffs_mtdif1.c */
  13810. + param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
  13811. + param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
  13812. + param->bad_block_fn = nandmtd1_mark_block_bad;
  13813. + param->query_block_fn = nandmtd1_query_block;
  13814. +#else
  13815. + param->write_chunk_fn = nandmtd_write_chunk;
  13816. + param->read_chunk_fn = nandmtd_read_chunk;
  13817. +#endif
  13818. + param->is_yaffs2 = 0;
  13819. + }
  13820. + /* ... and common functions */
  13821. + param->erase_fn = nandmtd_erase_block;
  13822. + param->initialise_flash_fn = nandmtd_initialise;
  13823. +
  13824. + yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
  13825. +
  13826. + param->sb_dirty_fn = yaffs_touch_super;
  13827. + param->gc_control = yaffs_gc_control_callback;
  13828. +
  13829. + yaffs_dev_to_lc(dev)->super = sb;
  13830. +
  13831. +#ifndef CONFIG_YAFFS_DOES_ECC
  13832. + param->use_nand_ecc = 1;
  13833. +#endif
  13834. +
  13835. +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
  13836. + param->wide_tnodes_disabled = 1;
  13837. +#endif
  13838. +
  13839. + param->skip_checkpt_rd = options.skip_checkpoint_read;
  13840. + param->skip_checkpt_wr = options.skip_checkpoint_write;
  13841. +
  13842. + mutex_lock(&yaffs_context_lock);
  13843. + /* Get a mount id */
  13844. + found = 0;
  13845. + for (mount_id = 0; !found; mount_id++) {
  13846. + found = 1;
  13847. + list_for_each(l, &yaffs_context_list) {
  13848. + context_iterator =
  13849. + list_entry(l, struct yaffs_linux_context,
  13850. + context_list);
  13851. + if (context_iterator->mount_id == mount_id)
  13852. + found = 0;
  13853. + }
  13854. + }
  13855. + context->mount_id = mount_id;
  13856. +
  13857. + list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
  13858. + &yaffs_context_list);
  13859. + mutex_unlock(&yaffs_context_lock);
  13860. +
  13861. + /* Directory search handling... */
  13862. + INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
  13863. + param->remove_obj_fn = yaffs_remove_obj_callback;
  13864. +
  13865. + mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
  13866. +
  13867. + yaffs_gross_lock(dev);
  13868. +
  13869. + err = yaffs_guts_initialise(dev);
  13870. +
  13871. + T(YAFFS_TRACE_OS,
  13872. + (TSTR("yaffs_read_super: guts initialised %s\n"),
  13873. + (err == YAFFS_OK) ? "OK" : "FAILED"));
  13874. +
  13875. + if (err == YAFFS_OK)
  13876. + yaffs_bg_start(dev);
  13877. +
  13878. + if (!context->bg_thread)
  13879. + param->defered_dir_update = 0;
  13880. +
  13881. + /* Release lock before yaffs_get_inode() */
  13882. + yaffs_gross_unlock(dev);
  13883. +
  13884. + /* Create root inode */
  13885. + if (err == YAFFS_OK)
  13886. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
  13887. +
  13888. + if (!inode)
  13889. + return NULL;
  13890. +
  13891. + inode->i_op = &yaffs_dir_inode_operations;
  13892. + inode->i_fop = &yaffs_dir_operations;
  13893. +
  13894. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
  13895. +
  13896. + root = d_alloc_root(inode);
  13897. +
  13898. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
  13899. +
  13900. + if (!root) {
  13901. + iput(inode);
  13902. + return NULL;
  13903. + }
  13904. + sb->s_root = root;
  13905. + sb->s_dirt = !dev->is_checkpointed;
  13906. + T(YAFFS_TRACE_ALWAYS,
  13907. + (TSTR("yaffs_read_super: is_checkpointed %d\n"),
  13908. + dev->is_checkpointed));
  13909. +
  13910. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
  13911. + return sb;
  13912. +}
  13913. +
  13914. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13915. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  13916. + int silent)
  13917. +{
  13918. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  13919. +}
  13920. +
  13921. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13922. +static int yaffs_read_super(struct file_system_type *fs,
  13923. + int flags, const char *dev_name,
  13924. + void *data, struct vfsmount *mnt)
  13925. +{
  13926. +
  13927. + return get_sb_bdev(fs, flags, dev_name, data,
  13928. + yaffs_internal_read_super_mtd, mnt);
  13929. +}
  13930. +#else
  13931. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  13932. + int flags, const char *dev_name,
  13933. + void *data)
  13934. +{
  13935. +
  13936. + return get_sb_bdev(fs, flags, dev_name, data,
  13937. + yaffs_internal_read_super_mtd);
  13938. +}
  13939. +#endif
  13940. +
  13941. +static struct file_system_type yaffs_fs_type = {
  13942. + .owner = THIS_MODULE,
  13943. + .name = "yaffs",
  13944. + .get_sb = yaffs_read_super,
  13945. + .kill_sb = kill_block_super,
  13946. + .fs_flags = FS_REQUIRES_DEV,
  13947. +};
  13948. +#else
  13949. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  13950. + int silent)
  13951. +{
  13952. + return yaffs_internal_read_super(1, sb, data, silent);
  13953. +}
  13954. +
  13955. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  13956. + FS_REQUIRES_DEV);
  13957. +#endif
  13958. +
  13959. +#ifdef CONFIG_YAFFS_YAFFS2
  13960. +
  13961. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13962. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  13963. + int silent)
  13964. +{
  13965. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  13966. +}
  13967. +
  13968. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13969. +static int yaffs2_read_super(struct file_system_type *fs,
  13970. + int flags, const char *dev_name, void *data,
  13971. + struct vfsmount *mnt)
  13972. +{
  13973. + return get_sb_bdev(fs, flags, dev_name, data,
  13974. + yaffs2_internal_read_super_mtd, mnt);
  13975. +}
  13976. +#else
  13977. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  13978. + int flags, const char *dev_name,
  13979. + void *data)
  13980. +{
  13981. +
  13982. + return get_sb_bdev(fs, flags, dev_name, data,
  13983. + yaffs2_internal_read_super_mtd);
  13984. +}
  13985. +#endif
  13986. +
  13987. +static struct file_system_type yaffs2_fs_type = {
  13988. + .owner = THIS_MODULE,
  13989. + .name = "yaffs2",
  13990. + .get_sb = yaffs2_read_super,
  13991. + .kill_sb = kill_block_super,
  13992. + .fs_flags = FS_REQUIRES_DEV,
  13993. +};
  13994. +#else
  13995. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  13996. + void *data, int silent)
  13997. +{
  13998. + return yaffs_internal_read_super(2, sb, data, silent);
  13999. +}
  14000. +
  14001. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  14002. + FS_REQUIRES_DEV);
  14003. +#endif
  14004. +
  14005. +#endif /* CONFIG_YAFFS_YAFFS2 */
  14006. +
  14007. +static struct proc_dir_entry *my_proc_entry;
  14008. +static struct proc_dir_entry *debug_proc_entry;
  14009. +
  14010. +static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
  14011. +{
  14012. + buf +=
  14013. + sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
  14014. + buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
  14015. + buf +=
  14016. + sprintf(buf, "total_bytes_per_chunk %d\n",
  14017. + dev->param.total_bytes_per_chunk);
  14018. + buf +=
  14019. + sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
  14020. + buf +=
  14021. + sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
  14022. + buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
  14023. + buf +=
  14024. + sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
  14025. + buf +=
  14026. + sprintf(buf, "empty_lost_n_found... %d\n",
  14027. + dev->param.empty_lost_n_found);
  14028. + buf +=
  14029. + sprintf(buf, "disable_lazy_load.... %d\n",
  14030. + dev->param.disable_lazy_load);
  14031. + buf +=
  14032. + sprintf(buf, "refresh_period....... %d\n",
  14033. + dev->param.refresh_period);
  14034. + buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
  14035. + buf +=
  14036. + sprintf(buf, "n_reserved_blocks.... %d\n",
  14037. + dev->param.n_reserved_blocks);
  14038. + buf +=
  14039. + sprintf(buf, "always_check_erased.. %d\n",
  14040. + dev->param.always_check_erased);
  14041. +
  14042. + buf += sprintf(buf, "\n");
  14043. +
  14044. + return buf;
  14045. +}
  14046. +
  14047. +static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
  14048. +{
  14049. + buf +=
  14050. + sprintf(buf, "data_bytes_per_chunk. %d\n",
  14051. + dev->data_bytes_per_chunk);
  14052. + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
  14053. + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
  14054. + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
  14055. + buf +=
  14056. + sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
  14057. + buf += sprintf(buf, "\n");
  14058. + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
  14059. + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
  14060. + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
  14061. + buf += sprintf(buf, "\n");
  14062. + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
  14063. + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
  14064. + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
  14065. + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
  14066. + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
  14067. + buf +=
  14068. + sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
  14069. + buf +=
  14070. + sprintf(buf, "oldest_dirty_gc_count %u\n",
  14071. + dev->oldest_dirty_gc_count);
  14072. + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
  14073. + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
  14074. + buf +=
  14075. + sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
  14076. + buf +=
  14077. + sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
  14078. + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
  14079. + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
  14080. + buf +=
  14081. + sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
  14082. + buf +=
  14083. + sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
  14084. + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
  14085. + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
  14086. + buf +=
  14087. + sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
  14088. + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
  14089. + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
  14090. +
  14091. + return buf;
  14092. +}
  14093. +
  14094. +static int yaffs_proc_read(char *page,
  14095. + char **start,
  14096. + off_t offset, int count, int *eof, void *data)
  14097. +{
  14098. + struct list_head *item;
  14099. + char *buf = page;
  14100. + int step = offset;
  14101. + int n = 0;
  14102. +
  14103. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  14104. + * We use 'offset' (*ppos) to indicate where we are in dev_list.
  14105. + * This also assumes the user has posted a read buffer large
  14106. + * enough to hold the complete output; but that's life in /proc.
  14107. + */
  14108. +
  14109. + *(int *)start = 1;
  14110. +
  14111. + /* Print header first */
  14112. + if (step == 0)
  14113. + buf +=
  14114. + sprintf(buf,
  14115. + "Multi-version YAFFS built:" __DATE__ " " __TIME__
  14116. + "\n");
  14117. + else if (step == 1)
  14118. + buf += sprintf(buf, "\n");
  14119. + else {
  14120. + step -= 2;
  14121. +
  14122. + mutex_lock(&yaffs_context_lock);
  14123. +
  14124. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14125. + list_for_each(item, &yaffs_context_list) {
  14126. + struct yaffs_linux_context *dc =
  14127. + list_entry(item, struct yaffs_linux_context,
  14128. + context_list);
  14129. + struct yaffs_dev *dev = dc->dev;
  14130. +
  14131. + if (n < (step & ~1)) {
  14132. + n += 2;
  14133. + continue;
  14134. + }
  14135. + if ((step & 1) == 0) {
  14136. + buf +=
  14137. + sprintf(buf, "\nDevice %d \"%s\"\n", n,
  14138. + dev->param.name);
  14139. + buf = yaffs_dump_dev_part0(buf, dev);
  14140. + } else {
  14141. + buf = yaffs_dump_dev_part1(buf, dev);
  14142. + }
  14143. +
  14144. + break;
  14145. + }
  14146. + mutex_unlock(&yaffs_context_lock);
  14147. + }
  14148. +
  14149. + return buf - page < count ? buf - page : count;
  14150. +}
  14151. +
  14152. +static int yaffs_stats_proc_read(char *page,
  14153. + char **start,
  14154. + off_t offset, int count, int *eof, void *data)
  14155. +{
  14156. + struct list_head *item;
  14157. + char *buf = page;
  14158. + int n = 0;
  14159. +
  14160. + mutex_lock(&yaffs_context_lock);
  14161. +
  14162. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14163. + list_for_each(item, &yaffs_context_list) {
  14164. + struct yaffs_linux_context *dc =
  14165. + list_entry(item, struct yaffs_linux_context, context_list);
  14166. + struct yaffs_dev *dev = dc->dev;
  14167. +
  14168. + int erased_chunks;
  14169. +
  14170. + erased_chunks =
  14171. + dev->n_erased_blocks * dev->param.chunks_per_block;
  14172. +
  14173. + buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n",
  14174. + n, dev->n_free_chunks, erased_chunks,
  14175. + dev->bg_gcs, dev->oldest_dirty_gc_count,
  14176. + dev->n_obj, dev->n_tnodes);
  14177. + }
  14178. + mutex_unlock(&yaffs_context_lock);
  14179. +
  14180. + return buf - page < count ? buf - page : count;
  14181. +}
  14182. +
  14183. +/**
  14184. + * Set the verbosity of the warnings and error messages.
  14185. + *
  14186. + * Note that the names can only be a..z or _ with the current code.
  14187. + */
  14188. +
  14189. +static struct {
  14190. + char *mask_name;
  14191. + unsigned mask_bitfield;
  14192. +} mask_flags[] = {
  14193. + {"allocate", YAFFS_TRACE_ALLOCATE},
  14194. + {"always", YAFFS_TRACE_ALWAYS},
  14195. + {"background", YAFFS_TRACE_BACKGROUND},
  14196. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  14197. + {"buffers", YAFFS_TRACE_BUFFERS},
  14198. + {"bug", YAFFS_TRACE_BUG},
  14199. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  14200. + {"deletion", YAFFS_TRACE_DELETION},
  14201. + {"erase", YAFFS_TRACE_ERASE},
  14202. + {"error", YAFFS_TRACE_ERROR},
  14203. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  14204. + {"gc", YAFFS_TRACE_GC},
  14205. + {"lock", YAFFS_TRACE_LOCK},
  14206. + {"mtd", YAFFS_TRACE_MTD},
  14207. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  14208. + {"os", YAFFS_TRACE_OS},
  14209. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  14210. + {"scan", YAFFS_TRACE_SCAN},
  14211. + {"mount", YAFFS_TRACE_MOUNT},
  14212. + {"tracing", YAFFS_TRACE_TRACING},
  14213. + {"sync", YAFFS_TRACE_SYNC},
  14214. + {"write", YAFFS_TRACE_WRITE},
  14215. + {"verify", YAFFS_TRACE_VERIFY},
  14216. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  14217. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  14218. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  14219. + {"all", 0xffffffff},
  14220. + {"none", 0},
  14221. + {NULL, 0},
  14222. +};
  14223. +
  14224. +#define MAX_MASK_NAME_LENGTH 40
  14225. +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
  14226. + unsigned long count, void *data)
  14227. +{
  14228. + unsigned rg = 0, mask_bitfield;
  14229. + char *end;
  14230. + char *mask_name;
  14231. + const char *x;
  14232. + char substring[MAX_MASK_NAME_LENGTH + 1];
  14233. + int i;
  14234. + int done = 0;
  14235. + int add, len = 0;
  14236. + int pos = 0;
  14237. +
  14238. + rg = yaffs_trace_mask;
  14239. +
  14240. + while (!done && (pos < count)) {
  14241. + done = 1;
  14242. + while ((pos < count) && isspace(buf[pos]))
  14243. + pos++;
  14244. +
  14245. + switch (buf[pos]) {
  14246. + case '+':
  14247. + case '-':
  14248. + case '=':
  14249. + add = buf[pos];
  14250. + pos++;
  14251. + break;
  14252. +
  14253. + default:
  14254. + add = ' ';
  14255. + break;
  14256. + }
  14257. + mask_name = NULL;
  14258. +
  14259. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  14260. +
  14261. + if (end > buf + pos) {
  14262. + mask_name = "numeral";
  14263. + len = end - (buf + pos);
  14264. + pos += len;
  14265. + done = 0;
  14266. + } else {
  14267. + for (x = buf + pos, i = 0;
  14268. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  14269. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  14270. + substring[i] = *x;
  14271. + substring[i] = '\0';
  14272. +
  14273. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14274. + if (strcmp(substring, mask_flags[i].mask_name)
  14275. + == 0) {
  14276. + mask_name = mask_flags[i].mask_name;
  14277. + mask_bitfield =
  14278. + mask_flags[i].mask_bitfield;
  14279. + done = 0;
  14280. + break;
  14281. + }
  14282. + }
  14283. + }
  14284. +
  14285. + if (mask_name != NULL) {
  14286. + done = 0;
  14287. + switch (add) {
  14288. + case '-':
  14289. + rg &= ~mask_bitfield;
  14290. + break;
  14291. + case '+':
  14292. + rg |= mask_bitfield;
  14293. + break;
  14294. + case '=':
  14295. + rg = mask_bitfield;
  14296. + break;
  14297. + default:
  14298. + rg |= mask_bitfield;
  14299. + break;
  14300. + }
  14301. + }
  14302. + }
  14303. +
  14304. + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
  14305. +
  14306. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
  14307. +
  14308. + if (rg & YAFFS_TRACE_ALWAYS) {
  14309. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14310. + char flag;
  14311. + flag = ((rg & mask_flags[i].mask_bitfield) ==
  14312. + mask_flags[i].mask_bitfield) ? '+' : '-';
  14313. + printk(KERN_DEBUG "%c%s\n", flag,
  14314. + mask_flags[i].mask_name);
  14315. + }
  14316. + }
  14317. +
  14318. + return count;
  14319. +}
  14320. +
  14321. +static int yaffs_proc_write(struct file *file, const char *buf,
  14322. + unsigned long count, void *data)
  14323. +{
  14324. + return yaffs_proc_write_trace_options(file, buf, count, data);
  14325. +}
  14326. +
  14327. +/* Stuff to handle installation of file systems */
  14328. +struct file_system_to_install {
  14329. + struct file_system_type *fst;
  14330. + int installed;
  14331. +};
  14332. +
  14333. +static struct file_system_to_install fs_to_install[] = {
  14334. + {&yaffs_fs_type, 0},
  14335. + {&yaffs2_fs_type, 0},
  14336. + {NULL, 0}
  14337. +};
  14338. +
  14339. +static int __init init_yaffs_fs(void)
  14340. +{
  14341. + int error = 0;
  14342. + struct file_system_to_install *fsinst;
  14343. +
  14344. + T(YAFFS_TRACE_ALWAYS,
  14345. + (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
  14346. +
  14347. +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  14348. + T(YAFFS_TRACE_ALWAYS,
  14349. + (TSTR
  14350. + (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
  14351. +#endif
  14352. +
  14353. + mutex_init(&yaffs_context_lock);
  14354. +
  14355. + /* Install the proc_fs entries */
  14356. + my_proc_entry = create_proc_entry("yaffs",
  14357. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14358. +
  14359. + if (my_proc_entry) {
  14360. + my_proc_entry->write_proc = yaffs_proc_write;
  14361. + my_proc_entry->read_proc = yaffs_proc_read;
  14362. + my_proc_entry->data = NULL;
  14363. + } else {
  14364. + return -ENOMEM;
  14365. + }
  14366. +
  14367. + debug_proc_entry = create_proc_entry("yaffs_stats",
  14368. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14369. +
  14370. + if (debug_proc_entry) {
  14371. + debug_proc_entry->write_proc = NULL;
  14372. + debug_proc_entry->read_proc = yaffs_stats_proc_read;
  14373. + debug_proc_entry->data = NULL;
  14374. + } else {
  14375. + return -ENOMEM;
  14376. + }
  14377. +
  14378. + /* Now add the file system entries */
  14379. +
  14380. + fsinst = fs_to_install;
  14381. +
  14382. + while (fsinst->fst && !error) {
  14383. + error = register_filesystem(fsinst->fst);
  14384. + if (!error)
  14385. + fsinst->installed = 1;
  14386. + fsinst++;
  14387. + }
  14388. +
  14389. + /* Any errors? uninstall */
  14390. + if (error) {
  14391. + fsinst = fs_to_install;
  14392. +
  14393. + while (fsinst->fst) {
  14394. + if (fsinst->installed) {
  14395. + unregister_filesystem(fsinst->fst);
  14396. + fsinst->installed = 0;
  14397. + }
  14398. + fsinst++;
  14399. + }
  14400. + }
  14401. +
  14402. + return error;
  14403. +}
  14404. +
  14405. +static void __exit exit_yaffs_fs(void)
  14406. +{
  14407. +
  14408. + struct file_system_to_install *fsinst;
  14409. +
  14410. + T(YAFFS_TRACE_ALWAYS,
  14411. + (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
  14412. +
  14413. + remove_proc_entry("yaffs", YPROC_ROOT);
  14414. + remove_proc_entry("yaffs_stats", YPROC_ROOT);
  14415. +
  14416. + fsinst = fs_to_install;
  14417. +
  14418. + while (fsinst->fst) {
  14419. + if (fsinst->installed) {
  14420. + unregister_filesystem(fsinst->fst);
  14421. + fsinst->installed = 0;
  14422. + }
  14423. + fsinst++;
  14424. + }
  14425. +}
  14426. +
  14427. +module_init(init_yaffs_fs)
  14428. + module_exit(exit_yaffs_fs)
  14429. +
  14430. + MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  14431. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
  14432. +MODULE_LICENSE("GPL");
  14433. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.c linux-2.6.36/fs/yaffs2/yaffs_yaffs1.c
  14434. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.c 1970-01-01 01:00:00.000000000 +0100
  14435. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs1.c 2010-11-18 18:15:51.000000000 +0100
  14436. @@ -0,0 +1,437 @@
  14437. +/*
  14438. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14439. + *
  14440. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14441. + * for Toby Churchill Ltd and Brightstar Engineering
  14442. + *
  14443. + * Created by Charles Manning <charles@aleph1.co.uk>
  14444. + *
  14445. + * This program is free software; you can redistribute it and/or modify
  14446. + * it under the terms of the GNU General Public License version 2 as
  14447. + * published by the Free Software Foundation.
  14448. + */
  14449. +
  14450. +#include "yaffs_yaffs1.h"
  14451. +#include "yportenv.h"
  14452. +#include "yaffs_trace.h"
  14453. +#include "yaffs_bitmap.h"
  14454. +#include "yaffs_getblockinfo.h"
  14455. +#include "yaffs_nand.h"
  14456. +#include "yaffs_attribs.h"
  14457. +
  14458. +int yaffs1_scan(struct yaffs_dev *dev)
  14459. +{
  14460. + struct yaffs_ext_tags tags;
  14461. + int blk;
  14462. + int result;
  14463. +
  14464. + int chunk;
  14465. + int c;
  14466. + int deleted;
  14467. + enum yaffs_block_state state;
  14468. + struct yaffs_obj *hard_list = NULL;
  14469. + struct yaffs_block_info *bi;
  14470. + u32 seq_number;
  14471. + struct yaffs_obj_hdr *oh;
  14472. + struct yaffs_obj *in;
  14473. + struct yaffs_obj *parent;
  14474. +
  14475. + int alloc_failed = 0;
  14476. +
  14477. + struct yaffs_shadow_fixer *shadow_fixers = NULL;
  14478. +
  14479. + u8 *chunk_data;
  14480. +
  14481. + T(YAFFS_TRACE_SCAN,
  14482. + (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
  14483. + dev->internal_start_block, dev->internal_end_block));
  14484. +
  14485. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  14486. +
  14487. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  14488. +
  14489. + /* Scan all the blocks to determine their state */
  14490. + bi = dev->block_info;
  14491. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  14492. + blk++) {
  14493. + yaffs_clear_chunk_bits(dev, blk);
  14494. + bi->pages_in_use = 0;
  14495. + bi->soft_del_pages = 0;
  14496. +
  14497. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  14498. +
  14499. + bi->block_state = state;
  14500. + bi->seq_number = seq_number;
  14501. +
  14502. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  14503. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  14504. +
  14505. + T(YAFFS_TRACE_SCAN_DEBUG,
  14506. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  14507. + state, seq_number));
  14508. +
  14509. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  14510. + T(YAFFS_TRACE_BAD_BLOCKS,
  14511. + (TSTR("block %d is bad" TENDSTR), blk));
  14512. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  14513. + T(YAFFS_TRACE_SCAN_DEBUG,
  14514. + (TSTR("Block empty " TENDSTR)));
  14515. + dev->n_erased_blocks++;
  14516. + dev->n_free_chunks += dev->param.chunks_per_block;
  14517. + }
  14518. + bi++;
  14519. + }
  14520. +
  14521. + /* For each block.... */
  14522. + for (blk = dev->internal_start_block;
  14523. + !alloc_failed && blk <= dev->internal_end_block; blk++) {
  14524. +
  14525. + YYIELD();
  14526. +
  14527. + bi = yaffs_get_block_info(dev, blk);
  14528. + state = bi->block_state;
  14529. +
  14530. + deleted = 0;
  14531. +
  14532. + /* For each chunk in each block that needs scanning.... */
  14533. + for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
  14534. + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  14535. + /* Read the tags and decide what to do */
  14536. + chunk = blk * dev->param.chunks_per_block + c;
  14537. +
  14538. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  14539. + &tags);
  14540. +
  14541. + /* Let's have a good look at this chunk... */
  14542. +
  14543. + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
  14544. + || tags.is_deleted) {
  14545. + /* YAFFS1 only...
  14546. + * A deleted chunk
  14547. + */
  14548. + deleted++;
  14549. + dev->n_free_chunks++;
  14550. + /*T((" %d %d deleted\n",blk,c)); */
  14551. + } else if (!tags.chunk_used) {
  14552. + /* An unassigned chunk in the block
  14553. + * This means that either the block is empty or
  14554. + * this is the one being allocated from
  14555. + */
  14556. +
  14557. + if (c == 0) {
  14558. + /* We're looking at the first chunk in the block so the block is unused */
  14559. + state = YAFFS_BLOCK_STATE_EMPTY;
  14560. + dev->n_erased_blocks++;
  14561. + } else {
  14562. + /* this is the block being allocated from */
  14563. + T(YAFFS_TRACE_SCAN,
  14564. + (TSTR
  14565. + (" Allocating from %d %d" TENDSTR),
  14566. + blk, c));
  14567. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  14568. + dev->alloc_block = blk;
  14569. + dev->alloc_page = c;
  14570. + dev->alloc_block_finder = blk;
  14571. + /* Set block finder here to encourage the allocator to go forth from here. */
  14572. +
  14573. + }
  14574. +
  14575. + dev->n_free_chunks +=
  14576. + (dev->param.chunks_per_block - c);
  14577. + } else if (tags.chunk_id > 0) {
  14578. + /* chunk_id > 0 so it is a data chunk... */
  14579. + unsigned int endpos;
  14580. +
  14581. + yaffs_set_chunk_bit(dev, blk, c);
  14582. + bi->pages_in_use++;
  14583. +
  14584. + in = yaffs_find_or_create_by_number(dev,
  14585. + tags.obj_id,
  14586. + YAFFS_OBJECT_TYPE_FILE);
  14587. + /* PutChunkIntoFile checks for a clash (two data chunks with
  14588. + * the same chunk_id).
  14589. + */
  14590. +
  14591. + if (!in)
  14592. + alloc_failed = 1;
  14593. +
  14594. + if (in) {
  14595. + if (!yaffs_put_chunk_in_file
  14596. + (in, tags.chunk_id, chunk, 1))
  14597. + alloc_failed = 1;
  14598. + }
  14599. +
  14600. + endpos =
  14601. + (tags.chunk_id -
  14602. + 1) * dev->data_bytes_per_chunk +
  14603. + tags.n_bytes;
  14604. + if (in
  14605. + && in->variant_type ==
  14606. + YAFFS_OBJECT_TYPE_FILE
  14607. + && in->variant.file_variant.scanned_size <
  14608. + endpos) {
  14609. + in->variant.file_variant.scanned_size =
  14610. + endpos;
  14611. + if (!dev->param.use_header_file_size) {
  14612. + in->variant.
  14613. + file_variant.file_size =
  14614. + in->variant.
  14615. + file_variant.scanned_size;
  14616. + }
  14617. +
  14618. + }
  14619. + /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
  14620. + } else {
  14621. + /* chunk_id == 0, so it is an ObjectHeader.
  14622. + * Thus, we read in the object header and make the object
  14623. + */
  14624. + yaffs_set_chunk_bit(dev, blk, c);
  14625. + bi->pages_in_use++;
  14626. +
  14627. + result = yaffs_rd_chunk_tags_nand(dev, chunk,
  14628. + chunk_data,
  14629. + NULL);
  14630. +
  14631. + oh = (struct yaffs_obj_hdr *)chunk_data;
  14632. +
  14633. + in = yaffs_find_by_number(dev, tags.obj_id);
  14634. + if (in && in->variant_type != oh->type) {
  14635. + /* This should not happen, but somehow
  14636. + * Wev'e ended up with an obj_id that has been reused but not yet
  14637. + * deleted, and worse still it has changed type. Delete the old object.
  14638. + */
  14639. +
  14640. + yaffs_del_obj(in);
  14641. +
  14642. + in = 0;
  14643. + }
  14644. +
  14645. + in = yaffs_find_or_create_by_number(dev,
  14646. + tags.obj_id,
  14647. + oh->type);
  14648. +
  14649. + if (!in)
  14650. + alloc_failed = 1;
  14651. +
  14652. + if (in && oh->shadows_obj > 0) {
  14653. +
  14654. + struct yaffs_shadow_fixer *fixer;
  14655. + fixer =
  14656. + YMALLOC(sizeof
  14657. + (struct
  14658. + yaffs_shadow_fixer));
  14659. + if (fixer) {
  14660. + fixer->next = shadow_fixers;
  14661. + shadow_fixers = fixer;
  14662. + fixer->obj_id = tags.obj_id;
  14663. + fixer->shadowed_id =
  14664. + oh->shadows_obj;
  14665. + T(YAFFS_TRACE_SCAN,
  14666. + (TSTR
  14667. + (" Shadow fixer: %d shadows %d"
  14668. + TENDSTR), fixer->obj_id,
  14669. + fixer->shadowed_id));
  14670. +
  14671. + }
  14672. +
  14673. + }
  14674. +
  14675. + if (in && in->valid) {
  14676. + /* We have already filled this one. We have a duplicate and need to resolve it. */
  14677. +
  14678. + unsigned existing_serial = in->serial;
  14679. + unsigned new_serial =
  14680. + tags.serial_number;
  14681. +
  14682. + if (((existing_serial + 1) & 3) ==
  14683. + new_serial) {
  14684. + /* Use new one - destroy the exisiting one */
  14685. + yaffs_chunk_del(dev,
  14686. + in->hdr_chunk,
  14687. + 1, __LINE__);
  14688. + in->valid = 0;
  14689. + } else {
  14690. + /* Use existing - destroy this one. */
  14691. + yaffs_chunk_del(dev, chunk, 1,
  14692. + __LINE__);
  14693. + }
  14694. + }
  14695. +
  14696. + if (in && !in->valid &&
  14697. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  14698. + tags.obj_id ==
  14699. + YAFFS_OBJECTID_LOSTNFOUND)) {
  14700. + /* We only load some info, don't fiddle with directory structure */
  14701. + in->valid = 1;
  14702. + in->variant_type = oh->type;
  14703. +
  14704. + in->yst_mode = oh->yst_mode;
  14705. + yaffs_load_attribs(in, oh);
  14706. + in->hdr_chunk = chunk;
  14707. + in->serial = tags.serial_number;
  14708. +
  14709. + } else if (in && !in->valid) {
  14710. + /* we need to load this info */
  14711. +
  14712. + in->valid = 1;
  14713. + in->variant_type = oh->type;
  14714. +
  14715. + in->yst_mode = oh->yst_mode;
  14716. + yaffs_load_attribs(in, oh);
  14717. + in->hdr_chunk = chunk;
  14718. + in->serial = tags.serial_number;
  14719. +
  14720. + yaffs_set_obj_name_from_oh(in, oh);
  14721. + in->dirty = 0;
  14722. +
  14723. + /* directory stuff...
  14724. + * hook up to parent
  14725. + */
  14726. +
  14727. + parent =
  14728. + yaffs_find_or_create_by_number
  14729. + (dev, oh->parent_obj_id,
  14730. + YAFFS_OBJECT_TYPE_DIRECTORY);
  14731. + if (!parent)
  14732. + alloc_failed = 1;
  14733. + if (parent && parent->variant_type ==
  14734. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  14735. + /* Set up as a directory */
  14736. + parent->variant_type =
  14737. + YAFFS_OBJECT_TYPE_DIRECTORY;
  14738. + INIT_LIST_HEAD(&parent->
  14739. + variant.dir_variant.children);
  14740. + } else if (!parent
  14741. + || parent->variant_type !=
  14742. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  14743. + /* Hoosterman, another problem....
  14744. + * We're trying to use a non-directory as a directory
  14745. + */
  14746. +
  14747. + T(YAFFS_TRACE_ERROR,
  14748. + (TSTR
  14749. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  14750. + TENDSTR)));
  14751. + parent = dev->lost_n_found;
  14752. + }
  14753. +
  14754. + yaffs_add_obj_to_dir(parent, in);
  14755. +
  14756. + if (0 && (parent == dev->del_dir ||
  14757. + parent ==
  14758. + dev->unlinked_dir)) {
  14759. + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  14760. + dev->n_deleted_files++;
  14761. + }
  14762. + /* Note re hardlinks.
  14763. + * Since we might scan a hardlink before its equivalent object is scanned
  14764. + * we put them all in a list.
  14765. + * After scanning is complete, we should have all the objects, so we run through this
  14766. + * list and fix up all the chains.
  14767. + */
  14768. +
  14769. + switch (in->variant_type) {
  14770. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  14771. + /* Todo got a problem */
  14772. + break;
  14773. + case YAFFS_OBJECT_TYPE_FILE:
  14774. + if (dev->param.
  14775. + use_header_file_size)
  14776. +
  14777. + in->variant.
  14778. + file_variant.file_size
  14779. + = oh->file_size;
  14780. +
  14781. + break;
  14782. + case YAFFS_OBJECT_TYPE_HARDLINK:
  14783. + in->variant.
  14784. + hardlink_variant.equiv_id =
  14785. + oh->equiv_id;
  14786. + in->hard_links.next =
  14787. + (struct list_head *)
  14788. + hard_list;
  14789. + hard_list = in;
  14790. + break;
  14791. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  14792. + /* Do nothing */
  14793. + break;
  14794. + case YAFFS_OBJECT_TYPE_SPECIAL:
  14795. + /* Do nothing */
  14796. + break;
  14797. + case YAFFS_OBJECT_TYPE_SYMLINK:
  14798. + in->variant.symlink_variant.
  14799. + alias =
  14800. + yaffs_clone_str(oh->alias);
  14801. + if (!in->variant.
  14802. + symlink_variant.alias)
  14803. + alloc_failed = 1;
  14804. + break;
  14805. + }
  14806. +
  14807. + }
  14808. + }
  14809. + }
  14810. +
  14811. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  14812. + /* If we got this far while scanning, then the block is fully allocated. */
  14813. + state = YAFFS_BLOCK_STATE_FULL;
  14814. + }
  14815. +
  14816. + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  14817. + /* If the block was partially allocated then treat it as fully allocated. */
  14818. + state = YAFFS_BLOCK_STATE_FULL;
  14819. + dev->alloc_block = -1;
  14820. + }
  14821. +
  14822. + bi->block_state = state;
  14823. +
  14824. + /* Now let's see if it was dirty */
  14825. + if (bi->pages_in_use == 0 &&
  14826. + !bi->has_shrink_hdr &&
  14827. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  14828. + yaffs_block_became_dirty(dev, blk);
  14829. + }
  14830. +
  14831. + }
  14832. +
  14833. + /* Ok, we've done all the scanning.
  14834. + * Fix up the hard link chains.
  14835. + * We should now have scanned all the objects, now it's time to add these
  14836. + * hardlinks.
  14837. + */
  14838. +
  14839. + yaffs_link_fixup(dev, hard_list);
  14840. +
  14841. + /* Fix up any shadowed objects */
  14842. + {
  14843. + struct yaffs_shadow_fixer *fixer;
  14844. + struct yaffs_obj *obj;
  14845. +
  14846. + while (shadow_fixers) {
  14847. + fixer = shadow_fixers;
  14848. + shadow_fixers = fixer->next;
  14849. + /* Complete the rename transaction by deleting the shadowed object
  14850. + * then setting the object header to unshadowed.
  14851. + */
  14852. + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  14853. + if (obj)
  14854. + yaffs_del_obj(obj);
  14855. +
  14856. + obj = yaffs_find_by_number(dev, fixer->obj_id);
  14857. +
  14858. + if (obj)
  14859. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  14860. +
  14861. + YFREE(fixer);
  14862. + }
  14863. + }
  14864. +
  14865. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  14866. +
  14867. + if (alloc_failed)
  14868. + return YAFFS_FAIL;
  14869. +
  14870. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
  14871. +
  14872. + return YAFFS_OK;
  14873. +}
  14874. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.h linux-2.6.36/fs/yaffs2/yaffs_yaffs1.h
  14875. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.h 1970-01-01 01:00:00.000000000 +0100
  14876. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs1.h 2010-11-18 18:15:51.000000000 +0100
  14877. @@ -0,0 +1,22 @@
  14878. +/*
  14879. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14880. + *
  14881. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14882. + * for Toby Churchill Ltd and Brightstar Engineering
  14883. + *
  14884. + * Created by Charles Manning <charles@aleph1.co.uk>
  14885. + *
  14886. + * This program is free software; you can redistribute it and/or modify
  14887. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14888. + * published by the Free Software Foundation.
  14889. + *
  14890. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14891. + */
  14892. +
  14893. +#ifndef __YAFFS_YAFFS1_H__
  14894. +#define __YAFFS_YAFFS1_H__
  14895. +
  14896. +#include "yaffs_guts.h"
  14897. +int yaffs1_scan(struct yaffs_dev *dev);
  14898. +
  14899. +#endif
  14900. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.c linux-2.6.36/fs/yaffs2/yaffs_yaffs2.c
  14901. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.c 1970-01-01 01:00:00.000000000 +0100
  14902. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs2.c 2010-11-18 18:15:51.000000000 +0100
  14903. @@ -0,0 +1,1620 @@
  14904. +/*
  14905. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14906. + *
  14907. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14908. + * for Toby Churchill Ltd and Brightstar Engineering
  14909. + *
  14910. + * Created by Charles Manning <charles@aleph1.co.uk>
  14911. + *
  14912. + * This program is free software; you can redistribute it and/or modify
  14913. + * it under the terms of the GNU General Public License version 2 as
  14914. + * published by the Free Software Foundation.
  14915. + */
  14916. +
  14917. +#include "yaffs_guts.h"
  14918. +#include "yaffs_trace.h"
  14919. +#include "yaffs_yaffs2.h"
  14920. +#include "yaffs_checkptrw.h"
  14921. +#include "yaffs_bitmap.h"
  14922. +#include "yaffs_nand.h"
  14923. +#include "yaffs_getblockinfo.h"
  14924. +#include "yaffs_verify.h"
  14925. +#include "yaffs_attribs.h"
  14926. +
  14927. +/*
  14928. + * Checkpoints are really no benefit on very small partitions.
  14929. + *
  14930. + * To save space on small partitions don't bother with checkpoints unless
  14931. + * the partition is at least this big.
  14932. + */
  14933. +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
  14934. +
  14935. +#define YAFFS_SMALL_HOLE_THRESHOLD 4
  14936. +
  14937. +/*
  14938. + * Oldest Dirty Sequence Number handling.
  14939. + */
  14940. +
  14941. +/* yaffs_calc_oldest_dirty_seq()
  14942. + * yaffs2_find_oldest_dirty_seq()
  14943. + * Calculate the oldest dirty sequence number if we don't know it.
  14944. + */
  14945. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
  14946. +{
  14947. + int i;
  14948. + unsigned seq;
  14949. + unsigned block_no = 0;
  14950. + struct yaffs_block_info *b;
  14951. +
  14952. + if (!dev->param.is_yaffs2)
  14953. + return;
  14954. +
  14955. + /* Find the oldest dirty sequence number. */
  14956. + seq = dev->seq_number + 1;
  14957. + b = dev->block_info;
  14958. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  14959. + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
  14960. + (b->pages_in_use - b->soft_del_pages) <
  14961. + dev->param.chunks_per_block && b->seq_number < seq) {
  14962. + seq = b->seq_number;
  14963. + block_no = i;
  14964. + }
  14965. + b++;
  14966. + }
  14967. +
  14968. + if (block_no) {
  14969. + dev->oldest_dirty_seq = seq;
  14970. + dev->oldest_dirty_block = block_no;
  14971. + }
  14972. +
  14973. +}
  14974. +
  14975. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
  14976. +{
  14977. + if (!dev->param.is_yaffs2)
  14978. + return;
  14979. +
  14980. + if (!dev->oldest_dirty_seq)
  14981. + yaffs_calc_oldest_dirty_seq(dev);
  14982. +}
  14983. +
  14984. +/*
  14985. + * yaffs_clear_oldest_dirty_seq()
  14986. + * Called when a block is erased or marked bad. (ie. when its seq_number
  14987. + * becomes invalid). If the value matches the oldest then we clear
  14988. + * dev->oldest_dirty_seq to force its recomputation.
  14989. + */
  14990. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  14991. + struct yaffs_block_info *bi)
  14992. +{
  14993. +
  14994. + if (!dev->param.is_yaffs2)
  14995. + return;
  14996. +
  14997. + if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
  14998. + dev->oldest_dirty_seq = 0;
  14999. + dev->oldest_dirty_block = 0;
  15000. + }
  15001. +}
  15002. +
  15003. +/*
  15004. + * yaffs2_update_oldest_dirty_seq()
  15005. + * Update the oldest dirty sequence number whenever we dirty a block.
  15006. + * Only do this if the oldest_dirty_seq is actually being tracked.
  15007. + */
  15008. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  15009. + struct yaffs_block_info *bi)
  15010. +{
  15011. + if (!dev->param.is_yaffs2)
  15012. + return;
  15013. +
  15014. + if (dev->oldest_dirty_seq) {
  15015. + if (dev->oldest_dirty_seq > bi->seq_number) {
  15016. + dev->oldest_dirty_seq = bi->seq_number;
  15017. + dev->oldest_dirty_block = block_no;
  15018. + }
  15019. + }
  15020. +}
  15021. +
  15022. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
  15023. +{
  15024. +
  15025. + if (!dev->param.is_yaffs2)
  15026. + return 1; /* disqualification only applies to yaffs2. */
  15027. +
  15028. + if (!bi->has_shrink_hdr)
  15029. + return 1; /* can gc */
  15030. +
  15031. + yaffs2_find_oldest_dirty_seq(dev);
  15032. +
  15033. + /* Can't do gc of this block if there are any blocks older than this one that have
  15034. + * discarded pages.
  15035. + */
  15036. + return (bi->seq_number <= dev->oldest_dirty_seq);
  15037. +}
  15038. +
  15039. +/*
  15040. + * yaffs2_find_refresh_block()
  15041. + * periodically finds the oldest full block by sequence number for refreshing.
  15042. + * Only for yaffs2.
  15043. + */
  15044. +u32 yaffs2_find_refresh_block(struct yaffs_dev * dev)
  15045. +{
  15046. + u32 b;
  15047. +
  15048. + u32 oldest = 0;
  15049. + u32 oldest_seq = 0;
  15050. +
  15051. + struct yaffs_block_info *bi;
  15052. +
  15053. + if (!dev->param.is_yaffs2)
  15054. + return oldest;
  15055. +
  15056. + /*
  15057. + * If refresh period < 10 then refreshing is disabled.
  15058. + */
  15059. + if (dev->param.refresh_period < 10)
  15060. + return oldest;
  15061. +
  15062. + /*
  15063. + * Fix broken values.
  15064. + */
  15065. + if (dev->refresh_skip > dev->param.refresh_period)
  15066. + dev->refresh_skip = dev->param.refresh_period;
  15067. +
  15068. + if (dev->refresh_skip > 0)
  15069. + return oldest;
  15070. +
  15071. + /*
  15072. + * Refresh skip is now zero.
  15073. + * We'll do a refresh this time around....
  15074. + * Update the refresh skip and find the oldest block.
  15075. + */
  15076. + dev->refresh_skip = dev->param.refresh_period;
  15077. + dev->refresh_count++;
  15078. + bi = dev->block_info;
  15079. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  15080. +
  15081. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  15082. +
  15083. + if (oldest < 1 || bi->seq_number < oldest_seq) {
  15084. + oldest = b;
  15085. + oldest_seq = bi->seq_number;
  15086. + }
  15087. + }
  15088. + bi++;
  15089. + }
  15090. +
  15091. + if (oldest > 0) {
  15092. + T(YAFFS_TRACE_GC,
  15093. + (TSTR
  15094. + ("GC refresh count %d selected block %d with seq_number %d"
  15095. + TENDSTR), dev->refresh_count, oldest, oldest_seq));
  15096. + }
  15097. +
  15098. + return oldest;
  15099. +}
  15100. +
  15101. +int yaffs2_checkpt_required(struct yaffs_dev *dev)
  15102. +{
  15103. + int nblocks;
  15104. +
  15105. + if (!dev->param.is_yaffs2)
  15106. + return 0;
  15107. +
  15108. + nblocks = dev->internal_end_block - dev->internal_start_block + 1;
  15109. +
  15110. + return !dev->param.skip_checkpt_wr &&
  15111. + !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
  15112. +}
  15113. +
  15114. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
  15115. +{
  15116. + int retval;
  15117. +
  15118. + if (!dev->param.is_yaffs2)
  15119. + return 0;
  15120. +
  15121. + if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
  15122. + /* Not a valid value so recalculate */
  15123. + int n_bytes = 0;
  15124. + int n_blocks;
  15125. + int dev_blocks =
  15126. + (dev->param.end_block - dev->param.start_block + 1);
  15127. +
  15128. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15129. + n_bytes += sizeof(struct yaffs_checkpt_dev);
  15130. + n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
  15131. + n_bytes += dev_blocks * dev->chunk_bit_stride;
  15132. + n_bytes +=
  15133. + (sizeof(struct yaffs_checkpt_obj) +
  15134. + sizeof(u32)) * (dev->n_obj);
  15135. + n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes);
  15136. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15137. + n_bytes += sizeof(u32); /* checksum */
  15138. +
  15139. + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
  15140. +
  15141. + n_blocks =
  15142. + (n_bytes /
  15143. + (dev->data_bytes_per_chunk *
  15144. + dev->param.chunks_per_block)) + 3;
  15145. +
  15146. + dev->checkpoint_blocks_required = n_blocks;
  15147. + }
  15148. +
  15149. + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
  15150. + if (retval < 0)
  15151. + retval = 0;
  15152. + return retval;
  15153. +}
  15154. +
  15155. +/*--------------------- Checkpointing --------------------*/
  15156. +
  15157. +static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15158. +{
  15159. + struct yaffs_checkpt_validity cp;
  15160. +
  15161. + memset(&cp, 0, sizeof(cp));
  15162. +
  15163. + cp.struct_type = sizeof(cp);
  15164. + cp.magic = YAFFS_MAGIC;
  15165. + cp.version = YAFFS_CHECKPOINT_VERSION;
  15166. + cp.head = (head) ? 1 : 0;
  15167. +
  15168. + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
  15169. +}
  15170. +
  15171. +static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15172. +{
  15173. + struct yaffs_checkpt_validity cp;
  15174. + int ok;
  15175. +
  15176. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15177. +
  15178. + if (ok)
  15179. + ok = (cp.struct_type == sizeof(cp)) &&
  15180. + (cp.magic == YAFFS_MAGIC) &&
  15181. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  15182. + (cp.head == ((head) ? 1 : 0));
  15183. + return ok ? 1 : 0;
  15184. +}
  15185. +
  15186. +static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
  15187. + struct yaffs_dev *dev)
  15188. +{
  15189. + cp->n_erased_blocks = dev->n_erased_blocks;
  15190. + cp->alloc_block = dev->alloc_block;
  15191. + cp->alloc_page = dev->alloc_page;
  15192. + cp->n_free_chunks = dev->n_free_chunks;
  15193. +
  15194. + cp->n_deleted_files = dev->n_deleted_files;
  15195. + cp->n_unlinked_files = dev->n_unlinked_files;
  15196. + cp->n_bg_deletions = dev->n_bg_deletions;
  15197. + cp->seq_number = dev->seq_number;
  15198. +
  15199. +}
  15200. +
  15201. +static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
  15202. + struct yaffs_checkpt_dev *cp)
  15203. +{
  15204. + dev->n_erased_blocks = cp->n_erased_blocks;
  15205. + dev->alloc_block = cp->alloc_block;
  15206. + dev->alloc_page = cp->alloc_page;
  15207. + dev->n_free_chunks = cp->n_free_chunks;
  15208. +
  15209. + dev->n_deleted_files = cp->n_deleted_files;
  15210. + dev->n_unlinked_files = cp->n_unlinked_files;
  15211. + dev->n_bg_deletions = cp->n_bg_deletions;
  15212. + dev->seq_number = cp->seq_number;
  15213. +}
  15214. +
  15215. +static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
  15216. +{
  15217. + struct yaffs_checkpt_dev cp;
  15218. + u32 n_bytes;
  15219. + u32 n_blocks =
  15220. + (dev->internal_end_block - dev->internal_start_block + 1);
  15221. +
  15222. + int ok;
  15223. +
  15224. + /* Write device runtime values */
  15225. + yaffs2_dev_to_checkpt_dev(&cp, dev);
  15226. + cp.struct_type = sizeof(cp);
  15227. +
  15228. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15229. +
  15230. + /* Write block info */
  15231. + if (ok) {
  15232. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15233. + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
  15234. + n_bytes);
  15235. + }
  15236. +
  15237. + /* Write chunk bits */
  15238. + if (ok) {
  15239. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15240. + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
  15241. + n_bytes);
  15242. + }
  15243. + return ok ? 1 : 0;
  15244. +
  15245. +}
  15246. +
  15247. +static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
  15248. +{
  15249. + struct yaffs_checkpt_dev cp;
  15250. + u32 n_bytes;
  15251. + u32 n_blocks =
  15252. + (dev->internal_end_block - dev->internal_start_block + 1);
  15253. +
  15254. + int ok;
  15255. +
  15256. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15257. + if (!ok)
  15258. + return 0;
  15259. +
  15260. + if (cp.struct_type != sizeof(cp))
  15261. + return 0;
  15262. +
  15263. + yaffs_checkpt_dev_to_dev(dev, &cp);
  15264. +
  15265. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15266. +
  15267. + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
  15268. +
  15269. + if (!ok)
  15270. + return 0;
  15271. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15272. +
  15273. + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15274. +
  15275. + return ok ? 1 : 0;
  15276. +}
  15277. +
  15278. +static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
  15279. + struct yaffs_obj *obj)
  15280. +{
  15281. +
  15282. + cp->obj_id = obj->obj_id;
  15283. + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
  15284. + cp->hdr_chunk = obj->hdr_chunk;
  15285. + cp->variant_type = obj->variant_type;
  15286. + cp->deleted = obj->deleted;
  15287. + cp->soft_del = obj->soft_del;
  15288. + cp->unlinked = obj->unlinked;
  15289. + cp->fake = obj->fake;
  15290. + cp->rename_allowed = obj->rename_allowed;
  15291. + cp->unlink_allowed = obj->unlink_allowed;
  15292. + cp->serial = obj->serial;
  15293. + cp->n_data_chunks = obj->n_data_chunks;
  15294. +
  15295. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15296. + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
  15297. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15298. + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
  15299. +}
  15300. +
  15301. +static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
  15302. + struct yaffs_checkpt_obj *cp)
  15303. +{
  15304. +
  15305. + struct yaffs_obj *parent;
  15306. +
  15307. + if (obj->variant_type != cp->variant_type) {
  15308. + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
  15309. + TCONT
  15310. + ("chunk %d does not match existing object type %d")
  15311. + TENDSTR), cp->obj_id,
  15312. + cp->variant_type, cp->hdr_chunk,
  15313. + obj->variant_type));
  15314. + return 0;
  15315. + }
  15316. +
  15317. + obj->obj_id = cp->obj_id;
  15318. +
  15319. + if (cp->parent_id)
  15320. + parent = yaffs_find_or_create_by_number(obj->my_dev,
  15321. + cp->parent_id,
  15322. + YAFFS_OBJECT_TYPE_DIRECTORY);
  15323. + else
  15324. + parent = NULL;
  15325. +
  15326. + if (parent) {
  15327. + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  15328. + T(YAFFS_TRACE_ALWAYS,
  15329. + (TSTR
  15330. + ("Checkpoint read object %d parent %d type %d"
  15331. + TCONT(" chunk %d Parent type, %d, not directory")
  15332. + TENDSTR), cp->obj_id, cp->parent_id,
  15333. + cp->variant_type, cp->hdr_chunk,
  15334. + parent->variant_type));
  15335. + return 0;
  15336. + }
  15337. + yaffs_add_obj_to_dir(parent, obj);
  15338. + }
  15339. +
  15340. + obj->hdr_chunk = cp->hdr_chunk;
  15341. + obj->variant_type = cp->variant_type;
  15342. + obj->deleted = cp->deleted;
  15343. + obj->soft_del = cp->soft_del;
  15344. + obj->unlinked = cp->unlinked;
  15345. + obj->fake = cp->fake;
  15346. + obj->rename_allowed = cp->rename_allowed;
  15347. + obj->unlink_allowed = cp->unlink_allowed;
  15348. + obj->serial = cp->serial;
  15349. + obj->n_data_chunks = cp->n_data_chunks;
  15350. +
  15351. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15352. + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
  15353. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15354. + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
  15355. +
  15356. + if (obj->hdr_chunk > 0)
  15357. + obj->lazy_loaded = 1;
  15358. + return 1;
  15359. +}
  15360. +
  15361. +static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
  15362. + struct yaffs_tnode *tn, u32 level,
  15363. + int chunk_offset)
  15364. +{
  15365. + int i;
  15366. + struct yaffs_dev *dev = in->my_dev;
  15367. + int ok = 1;
  15368. +
  15369. + if (tn) {
  15370. + if (level > 0) {
  15371. +
  15372. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  15373. + if (tn->internal[i]) {
  15374. + ok = yaffs2_checkpt_tnode_worker(in,
  15375. + tn->
  15376. + internal
  15377. + [i],
  15378. + level -
  15379. + 1,
  15380. + (chunk_offset
  15381. + <<
  15382. + YAFFS_TNODES_INTERNAL_BITS)
  15383. + + i);
  15384. + }
  15385. + }
  15386. + } else if (level == 0) {
  15387. + u32 base_offset =
  15388. + chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
  15389. + ok = (yaffs2_checkpt_wr
  15390. + (dev, &base_offset,
  15391. + sizeof(base_offset)) == sizeof(base_offset));
  15392. + if (ok)
  15393. + ok = (yaffs2_checkpt_wr
  15394. + (dev, tn,
  15395. + dev->tnode_size) == dev->tnode_size);
  15396. + }
  15397. + }
  15398. +
  15399. + return ok;
  15400. +
  15401. +}
  15402. +
  15403. +static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
  15404. +{
  15405. + u32 end_marker = ~0;
  15406. + int ok = 1;
  15407. +
  15408. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  15409. + ok = yaffs2_checkpt_tnode_worker(obj,
  15410. + obj->variant.file_variant.top,
  15411. + obj->variant.file_variant.
  15412. + top_level, 0);
  15413. + if (ok)
  15414. + ok = (yaffs2_checkpt_wr
  15415. + (obj->my_dev, &end_marker,
  15416. + sizeof(end_marker)) == sizeof(end_marker));
  15417. + }
  15418. +
  15419. + return ok ? 1 : 0;
  15420. +}
  15421. +
  15422. +static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
  15423. +{
  15424. + u32 base_chunk;
  15425. + int ok = 1;
  15426. + struct yaffs_dev *dev = obj->my_dev;
  15427. + struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
  15428. + struct yaffs_tnode *tn;
  15429. + int nread = 0;
  15430. +
  15431. + ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
  15432. + sizeof(base_chunk));
  15433. +
  15434. + while (ok && (~base_chunk)) {
  15435. + nread++;
  15436. + /* Read level 0 tnode */
  15437. +
  15438. + tn = yaffs_get_tnode(dev);
  15439. + if (tn) {
  15440. + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
  15441. + dev->tnode_size);
  15442. + } else {
  15443. + ok = 0;
  15444. + }
  15445. +
  15446. + if (tn && ok)
  15447. + ok = yaffs_add_find_tnode_0(dev,
  15448. + file_stuct_ptr,
  15449. + base_chunk, tn) ? 1 : 0;
  15450. +
  15451. + if (ok)
  15452. + ok = (yaffs2_checkpt_rd
  15453. + (dev, &base_chunk,
  15454. + sizeof(base_chunk)) == sizeof(base_chunk));
  15455. +
  15456. + }
  15457. +
  15458. + T(YAFFS_TRACE_CHECKPOINT,
  15459. + (TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
  15460. + nread, base_chunk, ok));
  15461. +
  15462. + return ok ? 1 : 0;
  15463. +}
  15464. +
  15465. +static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
  15466. +{
  15467. + struct yaffs_obj *obj;
  15468. + struct yaffs_checkpt_obj cp;
  15469. + int i;
  15470. + int ok = 1;
  15471. + struct list_head *lh;
  15472. +
  15473. + /* Iterate through the objects in each hash entry,
  15474. + * dumping them to the checkpointing stream.
  15475. + */
  15476. +
  15477. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  15478. + list_for_each(lh, &dev->obj_bucket[i].list) {
  15479. + if (lh) {
  15480. + obj =
  15481. + list_entry(lh, struct yaffs_obj, hash_link);
  15482. + if (!obj->defered_free) {
  15483. + yaffs2_obj_checkpt_obj(&cp, obj);
  15484. + cp.struct_type = sizeof(cp);
  15485. +
  15486. + T(YAFFS_TRACE_CHECKPOINT,
  15487. + (TSTR
  15488. + ("Checkpoint write object %d parent %d type %d chunk %d obj addr %p"
  15489. + TENDSTR), cp.obj_id, cp.parent_id,
  15490. + cp.variant_type, cp.hdr_chunk, obj));
  15491. +
  15492. + ok = (yaffs2_checkpt_wr
  15493. + (dev, &cp,
  15494. + sizeof(cp)) == sizeof(cp));
  15495. +
  15496. + if (ok
  15497. + && obj->variant_type ==
  15498. + YAFFS_OBJECT_TYPE_FILE)
  15499. + ok = yaffs2_wr_checkpt_tnodes
  15500. + (obj);
  15501. + }
  15502. + }
  15503. + }
  15504. + }
  15505. +
  15506. + /* Dump end of list */
  15507. + memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
  15508. + cp.struct_type = sizeof(cp);
  15509. +
  15510. + if (ok)
  15511. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15512. +
  15513. + return ok ? 1 : 0;
  15514. +}
  15515. +
  15516. +static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
  15517. +{
  15518. + struct yaffs_obj *obj;
  15519. + struct yaffs_checkpt_obj cp;
  15520. + int ok = 1;
  15521. + int done = 0;
  15522. + struct yaffs_obj *hard_list = NULL;
  15523. +
  15524. + while (ok && !done) {
  15525. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15526. + if (cp.struct_type != sizeof(cp)) {
  15527. + T(YAFFS_TRACE_CHECKPOINT,
  15528. + (TSTR("struct size %d instead of %d ok %d" TENDSTR),
  15529. + cp.struct_type, (int)sizeof(cp), ok));
  15530. + ok = 0;
  15531. + }
  15532. +
  15533. + T(YAFFS_TRACE_CHECKPOINT,
  15534. + (TSTR
  15535. + ("Checkpoint read object %d parent %d type %d chunk %d "
  15536. + TENDSTR), cp.obj_id, cp.parent_id, cp.variant_type,
  15537. + cp.hdr_chunk));
  15538. +
  15539. + if (ok && cp.obj_id == ~0) {
  15540. + done = 1;
  15541. + } else if (ok) {
  15542. + obj =
  15543. + yaffs_find_or_create_by_number(dev, cp.obj_id,
  15544. + cp.variant_type);
  15545. + if (obj) {
  15546. + ok = taffs2_checkpt_obj_to_obj(obj, &cp);
  15547. + if (!ok)
  15548. + break;
  15549. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  15550. + ok = yaffs2_rd_checkpt_tnodes(obj);
  15551. + } else if (obj->variant_type ==
  15552. + YAFFS_OBJECT_TYPE_HARDLINK) {
  15553. + obj->hard_links.next =
  15554. + (struct list_head *)hard_list;
  15555. + hard_list = obj;
  15556. + }
  15557. + } else {
  15558. + ok = 0;
  15559. + }
  15560. + }
  15561. + }
  15562. +
  15563. + if (ok)
  15564. + yaffs_link_fixup(dev, hard_list);
  15565. +
  15566. + return ok ? 1 : 0;
  15567. +}
  15568. +
  15569. +static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
  15570. +{
  15571. + u32 checkpt_sum;
  15572. + int ok;
  15573. +
  15574. + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
  15575. +
  15576. + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
  15577. + sizeof(checkpt_sum));
  15578. +
  15579. + if (!ok)
  15580. + return 0;
  15581. +
  15582. + return 1;
  15583. +}
  15584. +
  15585. +static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
  15586. +{
  15587. + u32 checkpt_sum0;
  15588. + u32 checkpt_sum1;
  15589. + int ok;
  15590. +
  15591. + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
  15592. +
  15593. + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
  15594. + sizeof(checkpt_sum1));
  15595. +
  15596. + if (!ok)
  15597. + return 0;
  15598. +
  15599. + if (checkpt_sum0 != checkpt_sum1)
  15600. + return 0;
  15601. +
  15602. + return 1;
  15603. +}
  15604. +
  15605. +static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
  15606. +{
  15607. + int ok = 1;
  15608. +
  15609. + if (!yaffs2_checkpt_required(dev)) {
  15610. + T(YAFFS_TRACE_CHECKPOINT,
  15611. + (TSTR("skipping checkpoint write" TENDSTR)));
  15612. + ok = 0;
  15613. + }
  15614. +
  15615. + if (ok)
  15616. + ok = yaffs2_checkpt_open(dev, 1);
  15617. +
  15618. + if (ok) {
  15619. + T(YAFFS_TRACE_CHECKPOINT,
  15620. + (TSTR("write checkpoint validity" TENDSTR)));
  15621. + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
  15622. + }
  15623. + if (ok) {
  15624. + T(YAFFS_TRACE_CHECKPOINT,
  15625. + (TSTR("write checkpoint device" TENDSTR)));
  15626. + ok = yaffs2_wr_checkpt_dev(dev);
  15627. + }
  15628. + if (ok) {
  15629. + T(YAFFS_TRACE_CHECKPOINT,
  15630. + (TSTR("write checkpoint objects" TENDSTR)));
  15631. + ok = yaffs2_wr_checkpt_objs(dev);
  15632. + }
  15633. + if (ok) {
  15634. + T(YAFFS_TRACE_CHECKPOINT,
  15635. + (TSTR("write checkpoint validity" TENDSTR)));
  15636. + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
  15637. + }
  15638. +
  15639. + if (ok)
  15640. + ok = yaffs2_wr_checkpt_sum(dev);
  15641. +
  15642. + if (!yaffs_checkpt_close(dev))
  15643. + ok = 0;
  15644. +
  15645. + if (ok)
  15646. + dev->is_checkpointed = 1;
  15647. + else
  15648. + dev->is_checkpointed = 0;
  15649. +
  15650. + return dev->is_checkpointed;
  15651. +}
  15652. +
  15653. +static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
  15654. +{
  15655. + int ok = 1;
  15656. +
  15657. + if (!dev->param.is_yaffs2)
  15658. + ok = 0;
  15659. +
  15660. + if (ok && dev->param.skip_checkpt_rd) {
  15661. + T(YAFFS_TRACE_CHECKPOINT,
  15662. + (TSTR("skipping checkpoint read" TENDSTR)));
  15663. + ok = 0;
  15664. + }
  15665. +
  15666. + if (ok)
  15667. + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
  15668. +
  15669. + if (ok) {
  15670. + T(YAFFS_TRACE_CHECKPOINT,
  15671. + (TSTR("read checkpoint validity" TENDSTR)));
  15672. + ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
  15673. + }
  15674. + if (ok) {
  15675. + T(YAFFS_TRACE_CHECKPOINT,
  15676. + (TSTR("read checkpoint device" TENDSTR)));
  15677. + ok = yaffs2_rd_checkpt_dev(dev);
  15678. + }
  15679. + if (ok) {
  15680. + T(YAFFS_TRACE_CHECKPOINT,
  15681. + (TSTR("read checkpoint objects" TENDSTR)));
  15682. + ok = yaffs2_rd_checkpt_objs(dev);
  15683. + }
  15684. + if (ok) {
  15685. + T(YAFFS_TRACE_CHECKPOINT,
  15686. + (TSTR("read checkpoint validity" TENDSTR)));
  15687. + ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
  15688. + }
  15689. +
  15690. + if (ok) {
  15691. + ok = yaffs2_rd_checkpt_sum(dev);
  15692. + T(YAFFS_TRACE_CHECKPOINT,
  15693. + (TSTR("read checkpoint checksum %d" TENDSTR), ok));
  15694. + }
  15695. +
  15696. + if (!yaffs_checkpt_close(dev))
  15697. + ok = 0;
  15698. +
  15699. + if (ok)
  15700. + dev->is_checkpointed = 1;
  15701. + else
  15702. + dev->is_checkpointed = 0;
  15703. +
  15704. + return ok ? 1 : 0;
  15705. +
  15706. +}
  15707. +
  15708. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
  15709. +{
  15710. + if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
  15711. + dev->is_checkpointed = 0;
  15712. + yaffs2_checkpt_invalidate_stream(dev);
  15713. + }
  15714. + if (dev->param.sb_dirty_fn)
  15715. + dev->param.sb_dirty_fn(dev);
  15716. +}
  15717. +
  15718. +int yaffs_checkpoint_save(struct yaffs_dev *dev)
  15719. +{
  15720. +
  15721. + T(YAFFS_TRACE_CHECKPOINT,
  15722. + (TSTR("save entry: is_checkpointed %d" TENDSTR),
  15723. + dev->is_checkpointed));
  15724. +
  15725. + yaffs_verify_objects(dev);
  15726. + yaffs_verify_blocks(dev);
  15727. + yaffs_verify_free_chunks(dev);
  15728. +
  15729. + if (!dev->is_checkpointed) {
  15730. + yaffs2_checkpt_invalidate(dev);
  15731. + yaffs2_wr_checkpt_data(dev);
  15732. + }
  15733. +
  15734. + T(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  15735. + (TSTR("save exit: is_checkpointed %d" TENDSTR),
  15736. + dev->is_checkpointed));
  15737. +
  15738. + return dev->is_checkpointed;
  15739. +}
  15740. +
  15741. +int yaffs2_checkpt_restore(struct yaffs_dev *dev)
  15742. +{
  15743. + int retval;
  15744. + T(YAFFS_TRACE_CHECKPOINT,
  15745. + (TSTR("restore entry: is_checkpointed %d" TENDSTR),
  15746. + dev->is_checkpointed));
  15747. +
  15748. + retval = yaffs2_rd_checkpt_data(dev);
  15749. +
  15750. + if (dev->is_checkpointed) {
  15751. + yaffs_verify_objects(dev);
  15752. + yaffs_verify_blocks(dev);
  15753. + yaffs_verify_free_chunks(dev);
  15754. + }
  15755. +
  15756. + T(YAFFS_TRACE_CHECKPOINT,
  15757. + (TSTR("restore exit: is_checkpointed %d" TENDSTR),
  15758. + dev->is_checkpointed));
  15759. +
  15760. + return retval;
  15761. +}
  15762. +
  15763. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
  15764. +{
  15765. + /* if new_size > old_file_size.
  15766. + * We're going to be writing a hole.
  15767. + * If the hole is small then write zeros otherwise write a start of hole marker.
  15768. + */
  15769. +
  15770. + loff_t old_file_size;
  15771. + int increase;
  15772. + int small_hole;
  15773. + int result = YAFFS_OK;
  15774. + struct yaffs_dev *dev = NULL;
  15775. +
  15776. + u8 *local_buffer = NULL;
  15777. +
  15778. + int small_increase_ok = 0;
  15779. +
  15780. + if (!obj)
  15781. + return YAFFS_FAIL;
  15782. +
  15783. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15784. + return YAFFS_FAIL;
  15785. +
  15786. + dev = obj->my_dev;
  15787. +
  15788. + /* Bail out if not yaffs2 mode */
  15789. + if (!dev->param.is_yaffs2)
  15790. + return YAFFS_OK;
  15791. +
  15792. + old_file_size = obj->variant.file_variant.file_size;
  15793. +
  15794. + if (new_size <= old_file_size)
  15795. + return YAFFS_OK;
  15796. +
  15797. + increase = new_size - old_file_size;
  15798. +
  15799. + if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
  15800. + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
  15801. + small_hole = 1;
  15802. + else
  15803. + small_hole = 0;
  15804. +
  15805. + if (small_hole)
  15806. + local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
  15807. +
  15808. + if (local_buffer) {
  15809. + /* fill hole with zero bytes */
  15810. + int pos = old_file_size;
  15811. + int this_write;
  15812. + int written;
  15813. + memset(local_buffer, 0, dev->data_bytes_per_chunk);
  15814. + small_increase_ok = 1;
  15815. +
  15816. + while (increase > 0 && small_increase_ok) {
  15817. + this_write = increase;
  15818. + if (this_write > dev->data_bytes_per_chunk)
  15819. + this_write = dev->data_bytes_per_chunk;
  15820. + written =
  15821. + yaffs_do_file_wr(obj, local_buffer, pos, this_write,
  15822. + 0);
  15823. + if (written == this_write) {
  15824. + pos += this_write;
  15825. + increase -= this_write;
  15826. + } else {
  15827. + small_increase_ok = 0;
  15828. + }
  15829. + }
  15830. +
  15831. + yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
  15832. +
  15833. + /* If we were out of space then reverse any chunks we've added */
  15834. + if (!small_increase_ok)
  15835. + yaffs_resize_file_down(obj, old_file_size);
  15836. + }
  15837. +
  15838. + if (!small_increase_ok &&
  15839. + obj->parent &&
  15840. + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  15841. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
  15842. + /* Write a hole start header with the old file size */
  15843. + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
  15844. + }
  15845. +
  15846. + return result;
  15847. +
  15848. +}
  15849. +
  15850. +struct yaffs_block_index {
  15851. + int seq;
  15852. + int block;
  15853. +};
  15854. +
  15855. +static int yaffs2_ybicmp(const void *a, const void *b)
  15856. +{
  15857. + int aseq = ((struct yaffs_block_index *)a)->seq;
  15858. + int bseq = ((struct yaffs_block_index *)b)->seq;
  15859. + int ablock = ((struct yaffs_block_index *)a)->block;
  15860. + int bblock = ((struct yaffs_block_index *)b)->block;
  15861. + if (aseq == bseq)
  15862. + return ablock - bblock;
  15863. + else
  15864. + return aseq - bseq;
  15865. +}
  15866. +
  15867. +int yaffs2_scan_backwards(struct yaffs_dev *dev)
  15868. +{
  15869. + struct yaffs_ext_tags tags;
  15870. + int blk;
  15871. + int block_iter;
  15872. + int start_iter;
  15873. + int end_iter;
  15874. + int n_to_scan = 0;
  15875. +
  15876. + int chunk;
  15877. + int result;
  15878. + int c;
  15879. + int deleted;
  15880. + enum yaffs_block_state state;
  15881. + struct yaffs_obj *hard_list = NULL;
  15882. + struct yaffs_block_info *bi;
  15883. + u32 seq_number;
  15884. + struct yaffs_obj_hdr *oh;
  15885. + struct yaffs_obj *in;
  15886. + struct yaffs_obj *parent;
  15887. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  15888. + int is_unlinked;
  15889. + u8 *chunk_data;
  15890. +
  15891. + int file_size;
  15892. + int is_shrink;
  15893. + int found_chunks;
  15894. + int equiv_id;
  15895. + int alloc_failed = 0;
  15896. +
  15897. + struct yaffs_block_index *block_index = NULL;
  15898. + int alt_block_index = 0;
  15899. +
  15900. + T(YAFFS_TRACE_SCAN,
  15901. + (TSTR
  15902. + ("yaffs2_scan_backwards starts intstartblk %d intendblk %d..."
  15903. + TENDSTR), dev->internal_start_block, dev->internal_end_block));
  15904. +
  15905. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  15906. +
  15907. + block_index = YMALLOC(n_blocks * sizeof(struct yaffs_block_index));
  15908. +
  15909. + if (!block_index) {
  15910. + block_index =
  15911. + YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_index));
  15912. + alt_block_index = 1;
  15913. + }
  15914. +
  15915. + if (!block_index) {
  15916. + T(YAFFS_TRACE_SCAN,
  15917. + (TSTR
  15918. + ("yaffs2_scan_backwards() could not allocate block index!"
  15919. + TENDSTR)));
  15920. + return YAFFS_FAIL;
  15921. + }
  15922. +
  15923. + dev->blocks_in_checkpt = 0;
  15924. +
  15925. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  15926. +
  15927. + /* Scan all the blocks to determine their state */
  15928. + bi = dev->block_info;
  15929. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  15930. + blk++) {
  15931. + yaffs_clear_chunk_bits(dev, blk);
  15932. + bi->pages_in_use = 0;
  15933. + bi->soft_del_pages = 0;
  15934. +
  15935. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  15936. +
  15937. + bi->block_state = state;
  15938. + bi->seq_number = seq_number;
  15939. +
  15940. + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  15941. + bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
  15942. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  15943. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  15944. +
  15945. + T(YAFFS_TRACE_SCAN_DEBUG,
  15946. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  15947. + state, seq_number));
  15948. +
  15949. + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  15950. + dev->blocks_in_checkpt++;
  15951. +
  15952. + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
  15953. + T(YAFFS_TRACE_BAD_BLOCKS,
  15954. + (TSTR("block %d is bad" TENDSTR), blk));
  15955. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  15956. + T(YAFFS_TRACE_SCAN_DEBUG,
  15957. + (TSTR("Block empty " TENDSTR)));
  15958. + dev->n_erased_blocks++;
  15959. + dev->n_free_chunks += dev->param.chunks_per_block;
  15960. + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  15961. +
  15962. + /* Determine the highest sequence number */
  15963. + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  15964. + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  15965. +
  15966. + block_index[n_to_scan].seq = seq_number;
  15967. + block_index[n_to_scan].block = blk;
  15968. +
  15969. + n_to_scan++;
  15970. +
  15971. + if (seq_number >= dev->seq_number)
  15972. + dev->seq_number = seq_number;
  15973. + } else {
  15974. + /* TODO: Nasty sequence number! */
  15975. + T(YAFFS_TRACE_SCAN,
  15976. + (TSTR
  15977. + ("Block scanning block %d has bad sequence number %d"
  15978. + TENDSTR), blk, seq_number));
  15979. +
  15980. + }
  15981. + }
  15982. + bi++;
  15983. + }
  15984. +
  15985. + T(YAFFS_TRACE_SCAN,
  15986. + (TSTR("%d blocks to be sorted..." TENDSTR), n_to_scan));
  15987. +
  15988. + YYIELD();
  15989. +
  15990. + /* Sort the blocks by sequence number */
  15991. + yaffs_sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  15992. + yaffs2_ybicmp);
  15993. +
  15994. + YYIELD();
  15995. +
  15996. + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
  15997. +
  15998. + /* Now scan the blocks looking at the data. */
  15999. + start_iter = 0;
  16000. + end_iter = n_to_scan - 1;
  16001. + T(YAFFS_TRACE_SCAN_DEBUG,
  16002. + (TSTR("%d blocks to be scanned" TENDSTR), n_to_scan));
  16003. +
  16004. + /* For each block.... backwards */
  16005. + for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
  16006. + block_iter--) {
  16007. + /* Cooperative multitasking! This loop can run for so
  16008. + long that watchdog timers expire. */
  16009. + YYIELD();
  16010. +
  16011. + /* get the block to scan in the correct order */
  16012. + blk = block_index[block_iter].block;
  16013. +
  16014. + bi = yaffs_get_block_info(dev, blk);
  16015. +
  16016. + state = bi->block_state;
  16017. +
  16018. + deleted = 0;
  16019. +
  16020. + /* For each chunk in each block that needs scanning.... */
  16021. + found_chunks = 0;
  16022. + for (c = dev->param.chunks_per_block - 1;
  16023. + !alloc_failed && c >= 0 &&
  16024. + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  16025. + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
  16026. + /* Scan backwards...
  16027. + * Read the tags and decide what to do
  16028. + */
  16029. +
  16030. + chunk = blk * dev->param.chunks_per_block + c;
  16031. +
  16032. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  16033. + &tags);
  16034. +
  16035. + /* Let's have a good look at this chunk... */
  16036. +
  16037. + if (!tags.chunk_used) {
  16038. + /* An unassigned chunk in the block.
  16039. + * If there are used chunks after this one, then
  16040. + * it is a chunk that was skipped due to failing the erased
  16041. + * check. Just skip it so that it can be deleted.
  16042. + * But, more typically, We get here when this is an unallocated
  16043. + * chunk and his means that either the block is empty or
  16044. + * this is the one being allocated from
  16045. + */
  16046. +
  16047. + if (found_chunks) {
  16048. + /* This is a chunk that was skipped due to failing the erased check */
  16049. + } else if (c == 0) {
  16050. + /* We're looking at the first chunk in the block so the block is unused */
  16051. + state = YAFFS_BLOCK_STATE_EMPTY;
  16052. + dev->n_erased_blocks++;
  16053. + } else {
  16054. + if (state ==
  16055. + YAFFS_BLOCK_STATE_NEEDS_SCANNING
  16056. + || state ==
  16057. + YAFFS_BLOCK_STATE_ALLOCATING) {
  16058. + if (dev->seq_number ==
  16059. + bi->seq_number) {
  16060. + /* this is the block being allocated from */
  16061. +
  16062. + T(YAFFS_TRACE_SCAN,
  16063. + (TSTR
  16064. + (" Allocating from %d %d"
  16065. + TENDSTR), blk, c));
  16066. +
  16067. + state =
  16068. + YAFFS_BLOCK_STATE_ALLOCATING;
  16069. + dev->alloc_block = blk;
  16070. + dev->alloc_page = c;
  16071. + dev->
  16072. + alloc_block_finder =
  16073. + blk;
  16074. + } else {
  16075. + /* This is a partially written block that is not
  16076. + * the current allocation block.
  16077. + */
  16078. +
  16079. + T(YAFFS_TRACE_SCAN,
  16080. + (TSTR
  16081. + ("Partially written block %d detected"
  16082. + TENDSTR), blk));
  16083. + }
  16084. + }
  16085. + }
  16086. +
  16087. + dev->n_free_chunks++;
  16088. +
  16089. + } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
  16090. + T(YAFFS_TRACE_SCAN,
  16091. + (TSTR
  16092. + (" Unfixed ECC in chunk(%d:%d), chunk ignored"
  16093. + TENDSTR), blk, c));
  16094. +
  16095. + dev->n_free_chunks++;
  16096. +
  16097. + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
  16098. + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
  16099. + (tags.chunk_id > 0
  16100. + && tags.n_bytes > dev->data_bytes_per_chunk)
  16101. + || tags.seq_number != bi->seq_number) {
  16102. + T(YAFFS_TRACE_SCAN,
  16103. + (TSTR
  16104. + ("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"
  16105. + TENDSTR), blk, c, tags.obj_id,
  16106. + tags.chunk_id, tags.n_bytes));
  16107. +
  16108. + dev->n_free_chunks++;
  16109. +
  16110. + } else if (tags.chunk_id > 0) {
  16111. + /* chunk_id > 0 so it is a data chunk... */
  16112. + unsigned int endpos;
  16113. + u32 chunk_base =
  16114. + (tags.chunk_id -
  16115. + 1) * dev->data_bytes_per_chunk;
  16116. +
  16117. + found_chunks = 1;
  16118. +
  16119. + yaffs_set_chunk_bit(dev, blk, c);
  16120. + bi->pages_in_use++;
  16121. +
  16122. + in = yaffs_find_or_create_by_number(dev,
  16123. + tags.obj_id,
  16124. + YAFFS_OBJECT_TYPE_FILE);
  16125. + if (!in) {
  16126. + /* Out of memory */
  16127. + alloc_failed = 1;
  16128. + }
  16129. +
  16130. + if (in &&
  16131. + in->variant_type == YAFFS_OBJECT_TYPE_FILE
  16132. + && chunk_base <
  16133. + in->variant.file_variant.shrink_size) {
  16134. + /* This has not been invalidated by a resize */
  16135. + if (!yaffs_put_chunk_in_file
  16136. + (in, tags.chunk_id, chunk, -1)) {
  16137. + alloc_failed = 1;
  16138. + }
  16139. +
  16140. + /* File size is calculated by looking at the data chunks if we have not
  16141. + * seen an object header yet. Stop this practice once we find an object header.
  16142. + */
  16143. + endpos = chunk_base + tags.n_bytes;
  16144. +
  16145. + if (!in->valid && /* have not got an object header yet */
  16146. + in->variant.file_variant.
  16147. + scanned_size < endpos) {
  16148. + in->variant.file_variant.
  16149. + scanned_size = endpos;
  16150. + in->variant.file_variant.
  16151. + file_size = endpos;
  16152. + }
  16153. +
  16154. + } else if (in) {
  16155. + /* This chunk has been invalidated by a resize, or a past file deletion
  16156. + * so delete the chunk*/
  16157. + yaffs_chunk_del(dev, chunk, 1,
  16158. + __LINE__);
  16159. +
  16160. + }
  16161. + } else {
  16162. + /* chunk_id == 0, so it is an ObjectHeader.
  16163. + * Thus, we read in the object header and make the object
  16164. + */
  16165. + found_chunks = 1;
  16166. +
  16167. + yaffs_set_chunk_bit(dev, blk, c);
  16168. + bi->pages_in_use++;
  16169. +
  16170. + oh = NULL;
  16171. + in = NULL;
  16172. +
  16173. + if (tags.extra_available) {
  16174. + in = yaffs_find_or_create_by_number(dev,
  16175. + tags.
  16176. + obj_id,
  16177. + tags.
  16178. + extra_obj_type);
  16179. + if (!in)
  16180. + alloc_failed = 1;
  16181. + }
  16182. +
  16183. + if (!in ||
  16184. + (!in->valid && dev->param.disable_lazy_load)
  16185. + || tags.extra_shadows || (!in->valid
  16186. + && (tags.obj_id ==
  16187. + YAFFS_OBJECTID_ROOT
  16188. + || tags.
  16189. + obj_id ==
  16190. + YAFFS_OBJECTID_LOSTNFOUND)))
  16191. + {
  16192. +
  16193. + /* If we don't have valid info then we need to read the chunk
  16194. + * TODO In future we can probably defer reading the chunk and
  16195. + * living with invalid data until needed.
  16196. + */
  16197. +
  16198. + result = yaffs_rd_chunk_tags_nand(dev,
  16199. + chunk,
  16200. + chunk_data,
  16201. + NULL);
  16202. +
  16203. + oh = (struct yaffs_obj_hdr *)chunk_data;
  16204. +
  16205. + if (dev->param.inband_tags) {
  16206. + /* Fix up the header if they got corrupted by inband tags */
  16207. + oh->shadows_obj =
  16208. + oh->inband_shadowed_obj_id;
  16209. + oh->is_shrink =
  16210. + oh->inband_is_shrink;
  16211. + }
  16212. +
  16213. + if (!in) {
  16214. + in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
  16215. + if (!in)
  16216. + alloc_failed = 1;
  16217. + }
  16218. +
  16219. + }
  16220. +
  16221. + if (!in) {
  16222. + /* TODO Hoosterman we have a problem! */
  16223. + T(YAFFS_TRACE_ERROR,
  16224. + (TSTR
  16225. + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
  16226. + TENDSTR), tags.obj_id, chunk));
  16227. + continue;
  16228. + }
  16229. +
  16230. + if (in->valid) {
  16231. + /* We have already filled this one.
  16232. + * We have a duplicate that will be discarded, but
  16233. + * we first have to suck out resize info if it is a file.
  16234. + */
  16235. +
  16236. + if ((in->variant_type ==
  16237. + YAFFS_OBJECT_TYPE_FILE) && ((oh
  16238. + &&
  16239. + oh->
  16240. + type
  16241. + ==
  16242. + YAFFS_OBJECT_TYPE_FILE)
  16243. + ||
  16244. + (tags.
  16245. + extra_available
  16246. + &&
  16247. + tags.
  16248. + extra_obj_type
  16249. + ==
  16250. + YAFFS_OBJECT_TYPE_FILE)))
  16251. + {
  16252. + u32 this_size =
  16253. + (oh) ? oh->
  16254. + file_size :
  16255. + tags.extra_length;
  16256. + u32 parent_obj_id =
  16257. + (oh) ? oh->parent_obj_id :
  16258. + tags.extra_parent_id;
  16259. +
  16260. + is_shrink =
  16261. + (oh) ? oh->
  16262. + is_shrink :
  16263. + tags.extra_is_shrink;
  16264. +
  16265. + /* If it is deleted (unlinked at start also means deleted)
  16266. + * we treat the file size as being zeroed at this point.
  16267. + */
  16268. + if (parent_obj_id ==
  16269. + YAFFS_OBJECTID_DELETED
  16270. + || parent_obj_id ==
  16271. + YAFFS_OBJECTID_UNLINKED) {
  16272. + this_size = 0;
  16273. + is_shrink = 1;
  16274. + }
  16275. +
  16276. + if (is_shrink
  16277. + && in->variant.file_variant.
  16278. + shrink_size > this_size)
  16279. + in->variant.
  16280. + file_variant.
  16281. + shrink_size =
  16282. + this_size;
  16283. +
  16284. + if (is_shrink)
  16285. + bi->has_shrink_hdr = 1;
  16286. +
  16287. + }
  16288. + /* Use existing - destroy this one. */
  16289. + yaffs_chunk_del(dev, chunk, 1,
  16290. + __LINE__);
  16291. +
  16292. + }
  16293. +
  16294. + if (!in->valid && in->variant_type !=
  16295. + (oh ? oh->type : tags.extra_obj_type))
  16296. + T(YAFFS_TRACE_ERROR,
  16297. + (TSTR
  16298. + ("yaffs tragedy: Bad object type, "
  16299. + TCONT
  16300. + ("%d != %d, for object %d at chunk ")
  16301. + TCONT("%d during scan")
  16302. + TENDSTR), oh ?
  16303. + oh->type : tags.extra_obj_type,
  16304. + in->variant_type, tags.obj_id,
  16305. + chunk));
  16306. +
  16307. + if (!in->valid &&
  16308. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  16309. + tags.obj_id ==
  16310. + YAFFS_OBJECTID_LOSTNFOUND)) {
  16311. + /* We only load some info, don't fiddle with directory structure */
  16312. + in->valid = 1;
  16313. +
  16314. + if (oh) {
  16315. +
  16316. + in->yst_mode = oh->yst_mode;
  16317. + yaffs_load_attribs(in, oh);
  16318. + in->lazy_loaded = 0;
  16319. + } else {
  16320. + in->lazy_loaded = 1;
  16321. + }
  16322. + in->hdr_chunk = chunk;
  16323. +
  16324. + } else if (!in->valid) {
  16325. + /* we need to load this info */
  16326. +
  16327. + in->valid = 1;
  16328. + in->hdr_chunk = chunk;
  16329. +
  16330. + if (oh) {
  16331. + in->variant_type = oh->type;
  16332. +
  16333. + in->yst_mode = oh->yst_mode;
  16334. + yaffs_load_attribs(in, oh);
  16335. +
  16336. + if (oh->shadows_obj > 0)
  16337. + yaffs_handle_shadowed_obj
  16338. + (dev,
  16339. + oh->shadows_obj,
  16340. + 1);
  16341. +
  16342. + yaffs_set_obj_name_from_oh(in,
  16343. + oh);
  16344. + parent =
  16345. + yaffs_find_or_create_by_number
  16346. + (dev, oh->parent_obj_id,
  16347. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16348. +
  16349. + file_size = oh->file_size;
  16350. + is_shrink = oh->is_shrink;
  16351. + equiv_id = oh->equiv_id;
  16352. +
  16353. + } else {
  16354. + in->variant_type =
  16355. + tags.extra_obj_type;
  16356. + parent =
  16357. + yaffs_find_or_create_by_number
  16358. + (dev, tags.extra_parent_id,
  16359. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16360. + file_size = tags.extra_length;
  16361. + is_shrink =
  16362. + tags.extra_is_shrink;
  16363. + equiv_id = tags.extra_equiv_id;
  16364. + in->lazy_loaded = 1;
  16365. +
  16366. + }
  16367. + in->dirty = 0;
  16368. +
  16369. + if (!parent)
  16370. + alloc_failed = 1;
  16371. +
  16372. + /* directory stuff...
  16373. + * hook up to parent
  16374. + */
  16375. +
  16376. + if (parent && parent->variant_type ==
  16377. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  16378. + /* Set up as a directory */
  16379. + parent->variant_type =
  16380. + YAFFS_OBJECT_TYPE_DIRECTORY;
  16381. + INIT_LIST_HEAD(&parent->
  16382. + variant.dir_variant.children);
  16383. + } else if (!parent
  16384. + || parent->variant_type !=
  16385. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  16386. + /* Hoosterman, another problem....
  16387. + * We're trying to use a non-directory as a directory
  16388. + */
  16389. +
  16390. + T(YAFFS_TRACE_ERROR,
  16391. + (TSTR
  16392. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  16393. + TENDSTR)));
  16394. + parent = dev->lost_n_found;
  16395. + }
  16396. +
  16397. + yaffs_add_obj_to_dir(parent, in);
  16398. +
  16399. + is_unlinked = (parent == dev->del_dir)
  16400. + || (parent == dev->unlinked_dir);
  16401. +
  16402. + if (is_shrink) {
  16403. + /* Mark the block as having a shrink header */
  16404. + bi->has_shrink_hdr = 1;
  16405. + }
  16406. +
  16407. + /* Note re hardlinks.
  16408. + * Since we might scan a hardlink before its equivalent object is scanned
  16409. + * we put them all in a list.
  16410. + * After scanning is complete, we should have all the objects, so we run
  16411. + * through this list and fix up all the chains.
  16412. + */
  16413. +
  16414. + switch (in->variant_type) {
  16415. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  16416. + /* Todo got a problem */
  16417. + break;
  16418. + case YAFFS_OBJECT_TYPE_FILE:
  16419. +
  16420. + if (in->variant.
  16421. + file_variant.scanned_size <
  16422. + file_size) {
  16423. + /* This covers the case where the file size is greater
  16424. + * than where the data is
  16425. + * This will happen if the file is resized to be larger
  16426. + * than its current data extents.
  16427. + */
  16428. + in->variant.
  16429. + file_variant.
  16430. + file_size =
  16431. + file_size;
  16432. + in->variant.
  16433. + file_variant.
  16434. + scanned_size =
  16435. + file_size;
  16436. + }
  16437. +
  16438. + if (in->variant.file_variant.
  16439. + shrink_size > file_size)
  16440. + in->variant.
  16441. + file_variant.
  16442. + shrink_size =
  16443. + file_size;
  16444. +
  16445. + break;
  16446. + case YAFFS_OBJECT_TYPE_HARDLINK:
  16447. + if (!is_unlinked) {
  16448. + in->variant.
  16449. + hardlink_variant.
  16450. + equiv_id = equiv_id;
  16451. + in->hard_links.next =
  16452. + (struct list_head *)
  16453. + hard_list;
  16454. + hard_list = in;
  16455. + }
  16456. + break;
  16457. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  16458. + /* Do nothing */
  16459. + break;
  16460. + case YAFFS_OBJECT_TYPE_SPECIAL:
  16461. + /* Do nothing */
  16462. + break;
  16463. + case YAFFS_OBJECT_TYPE_SYMLINK:
  16464. + if (oh) {
  16465. + in->variant.
  16466. + symlink_variant.
  16467. + alias =
  16468. + yaffs_clone_str(oh->
  16469. + alias);
  16470. + if (!in->variant.
  16471. + symlink_variant.
  16472. + alias)
  16473. + alloc_failed =
  16474. + 1;
  16475. + }
  16476. + break;
  16477. + }
  16478. +
  16479. + }
  16480. +
  16481. + }
  16482. +
  16483. + } /* End of scanning for each chunk */
  16484. +
  16485. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  16486. + /* If we got this far while scanning, then the block is fully allocated. */
  16487. + state = YAFFS_BLOCK_STATE_FULL;
  16488. + }
  16489. +
  16490. + bi->block_state = state;
  16491. +
  16492. + /* Now let's see if it was dirty */
  16493. + if (bi->pages_in_use == 0 &&
  16494. + !bi->has_shrink_hdr &&
  16495. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  16496. + yaffs_block_became_dirty(dev, blk);
  16497. + }
  16498. +
  16499. + }
  16500. +
  16501. + yaffs_skip_rest_of_block(dev);
  16502. +
  16503. + if (alt_block_index)
  16504. + YFREE_ALT(block_index);
  16505. + else
  16506. + YFREE(block_index);
  16507. +
  16508. + /* Ok, we've done all the scanning.
  16509. + * Fix up the hard link chains.
  16510. + * We should now have scanned all the objects, now it's time to add these
  16511. + * hardlinks.
  16512. + */
  16513. + yaffs_link_fixup(dev, hard_list);
  16514. +
  16515. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  16516. +
  16517. + if (alloc_failed)
  16518. + return YAFFS_FAIL;
  16519. +
  16520. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR)));
  16521. +
  16522. + return YAFFS_OK;
  16523. +}
  16524. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.h linux-2.6.36/fs/yaffs2/yaffs_yaffs2.h
  16525. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.h 1970-01-01 01:00:00.000000000 +0100
  16526. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs2.h 2010-11-18 18:15:51.000000000 +0100
  16527. @@ -0,0 +1,39 @@
  16528. +/*
  16529. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16530. + *
  16531. + * Copyright (C) 2002-2010 Aleph One Ltd.
  16532. + * for Toby Churchill Ltd and Brightstar Engineering
  16533. + *
  16534. + * Created by Charles Manning <charles@aleph1.co.uk>
  16535. + *
  16536. + * This program is free software; you can redistribute it and/or modify
  16537. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16538. + * published by the Free Software Foundation.
  16539. + *
  16540. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16541. + */
  16542. +
  16543. +#ifndef __YAFFS_YAFFS2_H__
  16544. +#define __YAFFS_YAFFS2_H__
  16545. +
  16546. +#include "yaffs_guts.h"
  16547. +
  16548. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
  16549. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
  16550. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  16551. + struct yaffs_block_info *bi);
  16552. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  16553. + struct yaffs_block_info *bi);
  16554. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
  16555. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
  16556. +int yaffs2_checkpt_required(struct yaffs_dev *dev);
  16557. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
  16558. +
  16559. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
  16560. +int yaffs2_checkpt_save(struct yaffs_dev *dev);
  16561. +int yaffs2_checkpt_restore(struct yaffs_dev *dev);
  16562. +
  16563. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
  16564. +int yaffs2_scan_backwards(struct yaffs_dev *dev);
  16565. +
  16566. +#endif
  16567. diff -Nur linux-2.6.36.orig/fs/yaffs2/yportenv.h linux-2.6.36/fs/yaffs2/yportenv.h
  16568. --- linux-2.6.36.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  16569. +++ linux-2.6.36/fs/yaffs2/yportenv.h 2010-11-18 18:15:51.000000000 +0100
  16570. @@ -0,0 +1,339 @@
  16571. +/*
  16572. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16573. + *
  16574. + * Copyright (C) 2002-2010 Aleph One Ltd.
  16575. + * for Toby Churchill Ltd and Brightstar Engineering
  16576. + *
  16577. + * Created by Charles Manning <charles@aleph1.co.uk>
  16578. + *
  16579. + * This program is free software; you can redistribute it and/or modify
  16580. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16581. + * published by the Free Software Foundation.
  16582. + *
  16583. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16584. + */
  16585. +
  16586. +#ifndef __YPORTENV_H__
  16587. +#define __YPORTENV_H__
  16588. +
  16589. +/*
  16590. + * Define the MTD version in terms of Linux Kernel versions
  16591. + * This allows yaffs to be used independantly of the kernel
  16592. + * as well as with it.
  16593. + */
  16594. +
  16595. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  16596. +
  16597. +#if defined CONFIG_YAFFS_WINCE
  16598. +
  16599. +#include "ywinceenv.h"
  16600. +
  16601. +#elif defined __KERNEL__
  16602. +
  16603. +#include "moduleconfig.h"
  16604. +
  16605. +/* Linux kernel */
  16606. +
  16607. +#include <linux/version.h>
  16608. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  16609. +
  16610. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  16611. +#include <linux/config.h>
  16612. +#endif
  16613. +#include <linux/version.h>
  16614. +#include <linux/kernel.h>
  16615. +#include <linux/mm.h>
  16616. +#include <linux/sched.h>
  16617. +#include <linux/string.h>
  16618. +#include <linux/slab.h>
  16619. +#include <linux/vmalloc.h>
  16620. +#include <linux/xattr.h>
  16621. +#include <linux/list.h>
  16622. +#include <linux/types.h>
  16623. +#include <linux/fs.h>
  16624. +#include <linux/stat.h>
  16625. +#include <linux/sort.h>
  16626. +
  16627. +#define YCHAR char
  16628. +#define YUCHAR unsigned char
  16629. +#define _Y(x) x
  16630. +#define yaffs_strcat(a, b) strcat(a, b)
  16631. +#define yaffs_strcpy(a, b) strcpy(a, b)
  16632. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  16633. +#define yaffs_strncmp(a, b, c) strncmp(a, b, c)
  16634. +#define yaffs_strnlen(s,m) strnlen(s,m)
  16635. +#define yaffs_sprintf sprintf
  16636. +#define yaffs_toupper(a) toupper(a)
  16637. +#define yaffs_sort(base, n, sz, cmp_fn) sort(base, n, sz, cmp_fn, NULL)
  16638. +
  16639. +#define Y_INLINE __inline__
  16640. +
  16641. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16642. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16643. +
  16644. +/* #define YPRINTF(x) printk x */
  16645. +#define YMALLOC(x) kmalloc(x, GFP_NOFS)
  16646. +#define YFREE(x) kfree(x)
  16647. +#define YMALLOC_ALT(x) vmalloc(x)
  16648. +#define YFREE_ALT(x) vfree(x)
  16649. +#define YMALLOC_DMA(x) YMALLOC(x)
  16650. +
  16651. +#define YYIELD() schedule()
  16652. +#define Y_DUMP_STACK() dump_stack()
  16653. +
  16654. +#define YAFFS_ROOT_MODE 0755
  16655. +#define YAFFS_LOSTNFOUND_MODE 0700
  16656. +
  16657. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  16658. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  16659. +#define Y_TIME_CONVERT(x) (x).tv_sec
  16660. +#else
  16661. +#define Y_CURRENT_TIME CURRENT_TIME
  16662. +#define Y_TIME_CONVERT(x) (x)
  16663. +#endif
  16664. +
  16665. +#define yaffs_sum_cmp(x, y) ((x) == (y))
  16666. +#define yaffs_strcmp(a, b) strcmp(a, b)
  16667. +
  16668. +#define TENDSTR "\n"
  16669. +#define TSTR(x) KERN_DEBUG x
  16670. +#define TCONT(x) x
  16671. +#define TOUT(p) printk p
  16672. +
  16673. +#define compile_time_assertion(assertion) \
  16674. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  16675. +
  16676. +#elif defined CONFIG_YAFFS_DIRECT
  16677. +
  16678. +#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22)
  16679. +
  16680. +/* Direct interface */
  16681. +#include "ydirectenv.h"
  16682. +
  16683. +#elif defined CONFIG_YAFFS_UTIL
  16684. +
  16685. +/* Stuff for YAFFS utilities */
  16686. +
  16687. +#include "stdlib.h"
  16688. +#include "stdio.h"
  16689. +#include "string.h"
  16690. +
  16691. +#define YMALLOC(x) malloc(x)
  16692. +#define YFREE(x) free(x)
  16693. +#define YMALLOC_ALT(x) malloc(x)
  16694. +#define YFREE_ALT(x) free(x)
  16695. +
  16696. +#define YCHAR char
  16697. +#define YUCHAR unsigned char
  16698. +#define _Y(x) x
  16699. +#define yaffs_strcat(a, b) strcat(a, b)
  16700. +#define yaffs_strcpy(a, b) strcpy(a, b)
  16701. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  16702. +#define yaffs_strnlen(s,m) strnlen(s,m)
  16703. +#define yaffs_sprintf sprintf
  16704. +#define yaffs_toupper(a) toupper(a)
  16705. +
  16706. +#define Y_INLINE inline
  16707. +
  16708. +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
  16709. +/* #define YALERT(s) YINFO(s) */
  16710. +
  16711. +#define TENDSTR "\n"
  16712. +#define TSTR(x) x
  16713. +#define TOUT(p) printf p
  16714. +
  16715. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16716. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16717. +/* #define YPRINTF(x) printf x */
  16718. +
  16719. +#define YAFFS_ROOT_MODE 0755
  16720. +#define YAFFS_LOSTNFOUND_MODE 0700
  16721. +
  16722. +#define yaffs_sum_cmp(x, y) ((x) == (y))
  16723. +#define yaffs_strcmp(a, b) strcmp(a, b)
  16724. +
  16725. +#else
  16726. +/* Should have specified a configuration type */
  16727. +#error Unknown configuration
  16728. +
  16729. +#endif
  16730. +
  16731. +#if defined(CONFIG_YAFFS_DIRECT) || defined(CONFIG_YAFFS_WINCE)
  16732. +
  16733. +#ifdef CONFIG_YAFFSFS_PROVIDE_VALUES
  16734. +
  16735. +#ifndef O_RDONLY
  16736. +#define O_RDONLY 00
  16737. +#endif
  16738. +
  16739. +#ifndef O_WRONLY
  16740. +#define O_WRONLY 01
  16741. +#endif
  16742. +
  16743. +#ifndef O_RDWR
  16744. +#define O_RDWR 02
  16745. +#endif
  16746. +
  16747. +#ifndef O_CREAT
  16748. +#define O_CREAT 0100
  16749. +#endif
  16750. +
  16751. +#ifndef O_EXCL
  16752. +#define O_EXCL 0200
  16753. +#endif
  16754. +
  16755. +#ifndef O_TRUNC
  16756. +#define O_TRUNC 01000
  16757. +#endif
  16758. +
  16759. +#ifndef O_APPEND
  16760. +#define O_APPEND 02000
  16761. +#endif
  16762. +
  16763. +#ifndef SEEK_SET
  16764. +#define SEEK_SET 0
  16765. +#endif
  16766. +
  16767. +#ifndef SEEK_CUR
  16768. +#define SEEK_CUR 1
  16769. +#endif
  16770. +
  16771. +#ifndef SEEK_END
  16772. +#define SEEK_END 2
  16773. +#endif
  16774. +
  16775. +#ifndef EBUSY
  16776. +#define EBUSY 16
  16777. +#endif
  16778. +
  16779. +#ifndef ENODEV
  16780. +#define ENODEV 19
  16781. +#endif
  16782. +
  16783. +#ifndef EINVAL
  16784. +#define EINVAL 22
  16785. +#endif
  16786. +
  16787. +#ifndef ENFILE
  16788. +#define ENFILE 23
  16789. +#endif
  16790. +
  16791. +#ifndef EBADF
  16792. +#define EBADF 9
  16793. +#endif
  16794. +
  16795. +#ifndef EACCES
  16796. +#define EACCES 13
  16797. +#endif
  16798. +
  16799. +#ifndef EXDEV
  16800. +#define EXDEV 18
  16801. +#endif
  16802. +
  16803. +#ifndef ENOENT
  16804. +#define ENOENT 2
  16805. +#endif
  16806. +
  16807. +#ifndef ENOSPC
  16808. +#define ENOSPC 28
  16809. +#endif
  16810. +
  16811. +#ifndef ERANGE
  16812. +#define ERANGE 34
  16813. +#endif
  16814. +
  16815. +#ifndef ENODATA
  16816. +#define ENODATA 61
  16817. +#endif
  16818. +
  16819. +#ifndef ENOTEMPTY
  16820. +#define ENOTEMPTY 39
  16821. +#endif
  16822. +
  16823. +#ifndef ENAMETOOLONG
  16824. +#define ENAMETOOLONG 36
  16825. +#endif
  16826. +
  16827. +#ifndef ENOMEM
  16828. +#define ENOMEM 12
  16829. +#endif
  16830. +
  16831. +#ifndef EEXIST
  16832. +#define EEXIST 17
  16833. +#endif
  16834. +
  16835. +#ifndef ENOTDIR
  16836. +#define ENOTDIR 20
  16837. +#endif
  16838. +
  16839. +#ifndef EISDIR
  16840. +#define EISDIR 21
  16841. +#endif
  16842. +
  16843. +// Mode flags
  16844. +
  16845. +#ifndef S_IFMT
  16846. +#define S_IFMT 0170000
  16847. +#endif
  16848. +
  16849. +#ifndef S_IFLNK
  16850. +#define S_IFLNK 0120000
  16851. +#endif
  16852. +
  16853. +#ifndef S_IFDIR
  16854. +#define S_IFDIR 0040000
  16855. +#endif
  16856. +
  16857. +#ifndef S_IFREG
  16858. +#define S_IFREG 0100000
  16859. +#endif
  16860. +
  16861. +#ifndef S_IREAD
  16862. +#define S_IREAD 0000400
  16863. +#endif
  16864. +
  16865. +#ifndef S_IWRITE
  16866. +#define S_IWRITE 0000200
  16867. +#endif
  16868. +
  16869. +#ifndef S_IEXEC
  16870. +#define S_IEXEC 0000100
  16871. +#endif
  16872. +
  16873. +#ifndef XATTR_CREATE
  16874. +#define XATTR_CREATE 1
  16875. +#endif
  16876. +
  16877. +#ifndef XATTR_REPLACE
  16878. +#define XATTR_REPLACE 2
  16879. +#endif
  16880. +
  16881. +#ifndef R_OK
  16882. +#define R_OK 4
  16883. +#define W_OK 2
  16884. +#define X_OK 1
  16885. +#define F_OK 0
  16886. +#endif
  16887. +
  16888. +#else
  16889. +#include <errno.h>
  16890. +#include <sys/stat.h>
  16891. +#include <fcntl.h>
  16892. +#endif
  16893. +
  16894. +#endif
  16895. +
  16896. +#ifndef Y_DUMP_STACK
  16897. +#define Y_DUMP_STACK() do { } while (0)
  16898. +#endif
  16899. +
  16900. +#ifndef YBUG
  16901. +#define YBUG() do {\
  16902. + T(YAFFS_TRACE_BUG,\
  16903. + (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),\
  16904. + __LINE__));\
  16905. + Y_DUMP_STACK();\
  16906. +} while (0)
  16907. +#endif
  16908. +
  16909. +#endif