yaffs2.patch 447 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912
  1. diff -Nur linux-2.6.36.orig/fs/Kconfig linux-2.6.36/fs/Kconfig
  2. --- linux-2.6.36.orig/fs/Kconfig 2011-01-10 19:28:45.000000000 +0100
  3. +++ linux-2.6.36/fs/Kconfig 2011-01-10 19:29:29.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 2011-01-10 19:28:45.000000000 +0100
  14. +++ linux-2.6.36/fs/Makefile 2011-01-10 19:30:04.000000000 +0100
  15. @@ -124,6 +124,7 @@
  16. obj-$(CONFIG_OCFS2_FS) += ocfs2/
  17. obj-$(CONFIG_BTRFS_FS) += btrfs/
  18. obj-$(CONFIG_GFS2_FS) += gfs2/
  19. +obj-$(CONFIG_YAFFS_FS) += yaffs2/
  20. obj-$(CONFIG_EXOFS_FS) += exofs/
  21. obj-$(CONFIG_CEPH_FS) += ceph/
  22. obj-$(CONFIG_AUFS_FS) += aufs/
  23. diff -Nur linux-2.6.36.orig/fs/yaffs2/Kconfig linux-2.6.36/fs/yaffs2/Kconfig
  24. --- linux-2.6.36.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  25. +++ linux-2.6.36/fs/yaffs2/Kconfig 2011-01-10 19:29:29.000000000 +0100
  26. @@ -0,0 +1,161 @@
  27. +#
  28. +# YAFFS file system configurations
  29. +#
  30. +
  31. +config YAFFS_FS
  32. + tristate "YAFFS2 file system support"
  33. + default n
  34. + depends on MTD_BLOCK
  35. + select YAFFS_YAFFS1
  36. + select YAFFS_YAFFS2
  37. + help
  38. + YAFFS2, or Yet Another Flash Filing System, is a filing system
  39. + optimised for NAND Flash chips.
  40. +
  41. + To compile the YAFFS2 file system support as a module, choose M
  42. + here: the module will be called yaffs2.
  43. +
  44. + If unsure, say N.
  45. +
  46. + Further information on YAFFS2 is available at
  47. + <http://www.aleph1.co.uk/yaffs/>.
  48. +
  49. +config YAFFS_YAFFS1
  50. + bool "512 byte / page devices"
  51. + depends on YAFFS_FS
  52. + default y
  53. + help
  54. + Enable YAFFS1 support -- yaffs for 512 byte / page devices
  55. +
  56. + Not needed for 2K-page devices.
  57. +
  58. + If unsure, say Y.
  59. +
  60. +config YAFFS_9BYTE_TAGS
  61. + bool "Use older-style on-NAND data format with pageStatus byte"
  62. + depends on YAFFS_YAFFS1
  63. + default n
  64. + help
  65. +
  66. + Older-style on-NAND data format has a "pageStatus" byte to record
  67. + chunk/page state. This byte is zero when the page is discarded.
  68. + Choose this option if you have existing on-NAND data using this
  69. + format that you need to continue to support. New data written
  70. + also uses the older-style format. Note: Use of this option
  71. + generally requires that MTD's oob layout be adjusted to use the
  72. + older-style format. See notes on tags formats and MTD versions
  73. + in yaffs_mtdif1.c.
  74. +
  75. + If unsure, say N.
  76. +
  77. +config YAFFS_DOES_ECC
  78. + bool "Lets Yaffs do its own ECC"
  79. + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
  80. + default n
  81. + help
  82. + This enables Yaffs to use its own ECC functions instead of using
  83. + the ones from the generic MTD-NAND driver.
  84. +
  85. + If unsure, say N.
  86. +
  87. +config YAFFS_ECC_WRONG_ORDER
  88. + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
  89. + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
  90. + default n
  91. + help
  92. + This makes yaffs_ecc.c use the same ecc byte order as Steven
  93. + Hill's nand_ecc.c. If not set, then you get the same ecc byte
  94. + order as SmartMedia.
  95. +
  96. + If unsure, say N.
  97. +
  98. +config YAFFS_YAFFS2
  99. + bool "2048 byte (or larger) / page devices"
  100. + depends on YAFFS_FS
  101. + default y
  102. + help
  103. + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
  104. +
  105. + If unsure, say Y.
  106. +
  107. +config YAFFS_AUTO_YAFFS2
  108. + bool "Autoselect yaffs2 format"
  109. + depends on YAFFS_YAFFS2
  110. + default y
  111. + help
  112. + Without this, you need to explicitely use yaffs2 as the file
  113. + system type. With this, you can say "yaffs" and yaffs or yaffs2
  114. + will be used depending on the device page size (yaffs on
  115. + 512-byte page devices, yaffs2 on 2K page devices).
  116. +
  117. + If unsure, say Y.
  118. +
  119. +config YAFFS_DISABLE_TAGS_ECC
  120. + bool "Disable YAFFS from doing ECC on tags by default"
  121. + depends on YAFFS_FS && YAFFS_YAFFS2
  122. + default n
  123. + help
  124. + This defaults Yaffs to using its own ECC calculations on tags instead of
  125. + just relying on the MTD.
  126. + This behavior can also be overridden with tags_ecc_on and
  127. + tags_ecc_off mount options.
  128. +
  129. + If unsure, say N.
  130. +
  131. +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  132. + bool "Force chunk erase check"
  133. + depends on YAFFS_FS
  134. + default n
  135. + help
  136. + Normally YAFFS only checks chunks before writing until an erased
  137. + chunk is found. This helps to detect any partially written
  138. + chunks that might have happened due to power loss.
  139. +
  140. + Enabling this forces on the test that chunks are erased in flash
  141. + before writing to them. This takes more time but is potentially
  142. + a bit more secure.
  143. +
  144. + Suggest setting Y during development and ironing out driver
  145. + issues etc. Suggest setting to N if you want faster writing.
  146. +
  147. + If unsure, say Y.
  148. +
  149. +config YAFFS_EMPTY_LOST_AND_FOUND
  150. + bool "Empty lost and found on boot"
  151. + depends on YAFFS_FS
  152. + default n
  153. + help
  154. + If this is enabled then the contents of lost and found is
  155. + automatically dumped at mount.
  156. +
  157. + If unsure, say N.
  158. +
  159. +config YAFFS_DISABLE_BLOCK_REFRESHING
  160. + bool "Disable yaffs2 block refreshing"
  161. + depends on YAFFS_FS
  162. + default n
  163. + help
  164. + If this is set, then block refreshing is disabled.
  165. + Block refreshing infrequently refreshes the oldest block in
  166. + a yaffs2 file system. This mechanism helps to refresh flash to
  167. + mitigate against data loss. This is particularly useful for MLC.
  168. +
  169. + If unsure, say N.
  170. +
  171. +config YAFFS_DISABLE_BACKGROUND
  172. + bool "Disable yaffs2 background processing"
  173. + depends on YAFFS_FS
  174. + default n
  175. + help
  176. + If this is set, then background processing is disabled.
  177. + Background processing makes many foreground activities faster.
  178. +
  179. + If unsure, say N.
  180. +
  181. +config YAFFS_XATTR
  182. + bool "Enable yaffs2 xattr support"
  183. + depends on YAFFS_FS
  184. + default y
  185. + help
  186. + If this is set then yaffs2 will provide xattr support.
  187. + If unsure, say Y.
  188. diff -Nur linux-2.6.36.orig/fs/yaffs2/Makefile linux-2.6.36/fs/yaffs2/Makefile
  189. --- linux-2.6.36.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  190. +++ linux-2.6.36/fs/yaffs2/Makefile 2011-01-10 19:29:29.000000000 +0100
  191. @@ -0,0 +1,17 @@
  192. +#
  193. +# Makefile for the linux YAFFS filesystem routines.
  194. +#
  195. +
  196. +obj-$(CONFIG_YAFFS_FS) += yaffs.o
  197. +
  198. +yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o
  199. +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
  200. +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
  201. +yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
  202. +yaffs-y += yaffs_nameval.o yaffs_attribs.o
  203. +yaffs-y += yaffs_allocator.o
  204. +yaffs-y += yaffs_yaffs1.o
  205. +yaffs-y += yaffs_yaffs2.o
  206. +yaffs-y += yaffs_bitmap.o
  207. +yaffs-y += yaffs_verify.o
  208. +
  209. diff -Nur linux-2.6.36.orig/fs/yaffs2/moduleconfig.h linux-2.6.36/fs/yaffs2/moduleconfig.h
  210. --- linux-2.6.36.orig/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
  211. +++ linux-2.6.36/fs/yaffs2/moduleconfig.h 2011-01-10 19:29:29.000000000 +0100
  212. @@ -0,0 +1,81 @@
  213. +/*
  214. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  215. + *
  216. + * Copyright (C) 2002-2010 Aleph One Ltd.
  217. + * for Toby Churchill Ltd and Brightstar Engineering
  218. + *
  219. + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
  220. + *
  221. + * This program is free software; you can redistribute it and/or modify
  222. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  223. + * published by the Free Software Foundation.
  224. + *
  225. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  226. + */
  227. +
  228. +#ifndef __YAFFS_CONFIG_H__
  229. +#define __YAFFS_CONFIG_H__
  230. +
  231. +#ifdef YAFFS_OUT_OF_TREE
  232. +
  233. +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
  234. +#define CONFIG_YAFFS_FS
  235. +#define CONFIG_YAFFS_YAFFS1
  236. +#define CONFIG_YAFFS_YAFFS2
  237. +
  238. +/* These options are independent of each other. Select those that matter. */
  239. +
  240. +/* Default: Not selected */
  241. +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
  242. +/* #define CONFIG_YAFFS_DOES_ECC */
  243. +
  244. +/* Default: Selected */
  245. +/* Meaning: Yaffs does its own ECC on tags for packed tags rather than use mtd */
  246. +#define CONFIG_YAFFS_DOES_TAGS_ECC
  247. +
  248. +/* Default: Not selected */
  249. +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
  250. +/* CONFIG_YAFFS_DOES_ECC is set */
  251. +/* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
  252. +
  253. +/* Default: Not selected */
  254. +/* Meaning: Always test whether chunks are erased before writing to them.
  255. + Use during mtd debugging and init. */
  256. +/* #define CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED */
  257. +
  258. +/* Default: Not Selected */
  259. +/* Meaning: At mount automatically empty all files from lost and found. */
  260. +/* This is done to fix an old problem where rmdir was not checking for an */
  261. +/* empty directory. This can also be achieved with a mount option. */
  262. +#define CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
  263. +
  264. +/* Default: Unselected */
  265. +/* Meaning: Select to disable block refreshing. */
  266. +/* Block Refreshing periodically rewrites the oldest block. */
  267. +/* #define CONFIG_DISABLE_BLOCK_REFRESHING */
  268. +
  269. +/* Default: Unselected */
  270. +/* Meaning: Select to disable background processing */
  271. +/* #define CONFIG_DISABLE_BACKGROUND */
  272. +
  273. +/* Default: Selected */
  274. +/* Meaning: Enable XATTR support */
  275. +#define CONFIG_YAFFS_XATTR
  276. +
  277. +/*
  278. +Older-style on-NAND data format has a "page_status" byte to record
  279. +chunk/page state. This byte is zeroed when the page is discarded.
  280. +Choose this option if you have existing on-NAND data in this format
  281. +that you need to continue to support. New data written also uses the
  282. +older-style format.
  283. +Note: Use of this option generally requires that MTD's oob layout be
  284. +adjusted to use the older-style format. See notes on tags formats and
  285. +MTD versions in yaffs_mtdif1.c.
  286. +*/
  287. +/* Default: Not selected */
  288. +/* Meaning: Use older-style on-NAND data format with page_status byte */
  289. +/* #define CONFIG_YAFFS_9BYTE_TAGS */
  290. +
  291. +#endif /* YAFFS_OUT_OF_TREE */
  292. +
  293. +#endif /* __YAFFS_CONFIG_H__ */
  294. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.c linux-2.6.36/fs/yaffs2/yaffs_allocator.c
  295. --- linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.c 1970-01-01 01:00:00.000000000 +0100
  296. +++ linux-2.6.36/fs/yaffs2/yaffs_allocator.c 2011-01-10 19:29:29.000000000 +0100
  297. @@ -0,0 +1,397 @@
  298. +/*
  299. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  300. + *
  301. + * Copyright (C) 2002-2010 Aleph One Ltd.
  302. + * for Toby Churchill Ltd and Brightstar Engineering
  303. + *
  304. + * Created by Charles Manning <charles@aleph1.co.uk>
  305. + *
  306. + * This program is free software; you can redistribute it and/or modify
  307. + * it under the terms of the GNU General Public License version 2 as
  308. + * published by the Free Software Foundation.
  309. + */
  310. +
  311. +#include "yaffs_allocator.h"
  312. +#include "yaffs_guts.h"
  313. +#include "yaffs_trace.h"
  314. +#include "yportenv.h"
  315. +
  316. +#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
  317. +
  318. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  319. +{
  320. + dev = dev;
  321. +}
  322. +
  323. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  324. +{
  325. + dev = dev;
  326. +}
  327. +
  328. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  329. +{
  330. + return (struct yaffs_tnode *)YMALLOC(dev->tnode_size);
  331. +}
  332. +
  333. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  334. +{
  335. + dev = dev;
  336. + YFREE(tn);
  337. +}
  338. +
  339. +void yaffs_init_raw_objs(struct yaffs_dev *dev)
  340. +{
  341. + dev = dev;
  342. +}
  343. +
  344. +void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  345. +{
  346. + dev = dev;
  347. +}
  348. +
  349. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  350. +{
  351. + dev = dev;
  352. + return (struct yaffs_obj *)YMALLOC(sizeof(struct yaffs_obj));
  353. +}
  354. +
  355. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  356. +{
  357. +
  358. + dev = dev;
  359. + YFREE(obj);
  360. +}
  361. +
  362. +#else
  363. +
  364. +struct yaffs_tnode_list {
  365. + struct yaffs_tnode_list *next;
  366. + struct yaffs_tnode *tnodes;
  367. +};
  368. +
  369. +struct yaffs_obj_list {
  370. + struct yaffs_obj_list *next;
  371. + struct yaffs_obj *objects;
  372. +};
  373. +
  374. +struct yaffs_allocator {
  375. + int n_tnodes_created;
  376. + struct yaffs_tnode *free_tnodes;
  377. + int n_free_tnodes;
  378. + struct yaffs_tnode_list *alloc_tnode_list;
  379. +
  380. + int n_obj_created;
  381. + struct yaffs_obj *free_objs;
  382. + int n_free_objects;
  383. +
  384. + struct yaffs_obj_list *allocated_obj_list;
  385. +};
  386. +
  387. +static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
  388. +{
  389. +
  390. + struct yaffs_allocator *allocator =
  391. + (struct yaffs_allocator *)dev->allocator;
  392. +
  393. + struct yaffs_tnode_list *tmp;
  394. +
  395. + if (!allocator) {
  396. + YBUG();
  397. + return;
  398. + }
  399. +
  400. + while (allocator->alloc_tnode_list) {
  401. + tmp = allocator->alloc_tnode_list->next;
  402. +
  403. + YFREE(allocator->alloc_tnode_list->tnodes);
  404. + YFREE(allocator->alloc_tnode_list);
  405. + allocator->alloc_tnode_list = tmp;
  406. +
  407. + }
  408. +
  409. + allocator->free_tnodes = NULL;
  410. + allocator->n_free_tnodes = 0;
  411. + allocator->n_tnodes_created = 0;
  412. +}
  413. +
  414. +static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
  415. +{
  416. + struct yaffs_allocator *allocator = dev->allocator;
  417. +
  418. + if (allocator) {
  419. + allocator->alloc_tnode_list = NULL;
  420. + allocator->free_tnodes = NULL;
  421. + allocator->n_free_tnodes = 0;
  422. + allocator->n_tnodes_created = 0;
  423. + } else {
  424. + YBUG();
  425. + }
  426. +}
  427. +
  428. +static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
  429. +{
  430. + struct yaffs_allocator *allocator =
  431. + (struct yaffs_allocator *)dev->allocator;
  432. + int i;
  433. + struct yaffs_tnode *new_tnodes;
  434. + u8 *mem;
  435. + struct yaffs_tnode *curr;
  436. + struct yaffs_tnode *next;
  437. + struct yaffs_tnode_list *tnl;
  438. +
  439. + if (!allocator) {
  440. + YBUG();
  441. + return YAFFS_FAIL;
  442. + }
  443. +
  444. + if (n_tnodes < 1)
  445. + return YAFFS_OK;
  446. +
  447. + /* make these things */
  448. +
  449. + new_tnodes = YMALLOC(n_tnodes * dev->tnode_size);
  450. + mem = (u8 *) new_tnodes;
  451. +
  452. + if (!new_tnodes) {
  453. + T(YAFFS_TRACE_ERROR,
  454. + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
  455. + return YAFFS_FAIL;
  456. + }
  457. +
  458. + /* New hookup for wide tnodes */
  459. + for (i = 0; i < n_tnodes - 1; i++) {
  460. + curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
  461. + next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
  462. + curr->internal[0] = next;
  463. + }
  464. +
  465. + curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
  466. + curr->internal[0] = allocator->free_tnodes;
  467. + allocator->free_tnodes = (struct yaffs_tnode *)mem;
  468. +
  469. + allocator->n_free_tnodes += n_tnodes;
  470. + allocator->n_tnodes_created += n_tnodes;
  471. +
  472. + /* Now add this bunch of tnodes to a list for freeing up.
  473. + * NB If we can't add this to the management list it isn't fatal
  474. + * but it just means we can't free this bunch of tnodes later.
  475. + */
  476. +
  477. + tnl = YMALLOC(sizeof(struct yaffs_tnode_list));
  478. + if (!tnl) {
  479. + T(YAFFS_TRACE_ERROR,
  480. + (TSTR
  481. + ("yaffs: Could not add tnodes to management list" TENDSTR)));
  482. + return YAFFS_FAIL;
  483. + } else {
  484. + tnl->tnodes = new_tnodes;
  485. + tnl->next = allocator->alloc_tnode_list;
  486. + allocator->alloc_tnode_list = tnl;
  487. + }
  488. +
  489. + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
  490. +
  491. + return YAFFS_OK;
  492. +}
  493. +
  494. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  495. +{
  496. + struct yaffs_allocator *allocator =
  497. + (struct yaffs_allocator *)dev->allocator;
  498. + struct yaffs_tnode *tn = NULL;
  499. +
  500. + if (!allocator) {
  501. + YBUG();
  502. + return NULL;
  503. + }
  504. +
  505. + /* If there are none left make more */
  506. + if (!allocator->free_tnodes)
  507. + yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
  508. +
  509. + if (allocator->free_tnodes) {
  510. + tn = allocator->free_tnodes;
  511. + allocator->free_tnodes = allocator->free_tnodes->internal[0];
  512. + allocator->n_free_tnodes--;
  513. + }
  514. +
  515. + return tn;
  516. +}
  517. +
  518. +/* FreeTnode frees up a tnode and puts it back on the free list */
  519. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  520. +{
  521. + struct yaffs_allocator *allocator = dev->allocator;
  522. +
  523. + if (!allocator) {
  524. + YBUG();
  525. + return;
  526. + }
  527. +
  528. + if (tn) {
  529. + tn->internal[0] = allocator->free_tnodes;
  530. + allocator->free_tnodes = tn;
  531. + allocator->n_free_tnodes++;
  532. + }
  533. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  534. +}
  535. +
  536. +static void yaffs_init_raw_objs(struct yaffs_dev *dev)
  537. +{
  538. + struct yaffs_allocator *allocator = dev->allocator;
  539. +
  540. + if (allocator) {
  541. + allocator->allocated_obj_list = NULL;
  542. + allocator->free_objs = NULL;
  543. + allocator->n_free_objects = 0;
  544. + } else {
  545. + YBUG();
  546. + }
  547. +}
  548. +
  549. +static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  550. +{
  551. + struct yaffs_allocator *allocator = dev->allocator;
  552. + struct yaffs_obj_list *tmp;
  553. +
  554. + if (!allocator) {
  555. + YBUG();
  556. + return;
  557. + }
  558. +
  559. + while (allocator->allocated_obj_list) {
  560. + tmp = allocator->allocated_obj_list->next;
  561. + YFREE(allocator->allocated_obj_list->objects);
  562. + YFREE(allocator->allocated_obj_list);
  563. +
  564. + allocator->allocated_obj_list = tmp;
  565. + }
  566. +
  567. + allocator->free_objs = NULL;
  568. + allocator->n_free_objects = 0;
  569. + allocator->n_obj_created = 0;
  570. +}
  571. +
  572. +static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
  573. +{
  574. + struct yaffs_allocator *allocator = dev->allocator;
  575. +
  576. + int i;
  577. + struct yaffs_obj *new_objs;
  578. + struct yaffs_obj_list *list;
  579. +
  580. + if (!allocator) {
  581. + YBUG();
  582. + return YAFFS_FAIL;
  583. + }
  584. +
  585. + if (n_obj < 1)
  586. + return YAFFS_OK;
  587. +
  588. + /* make these things */
  589. + new_objs = YMALLOC(n_obj * sizeof(struct yaffs_obj));
  590. + list = YMALLOC(sizeof(struct yaffs_obj_list));
  591. +
  592. + if (!new_objs || !list) {
  593. + if (new_objs) {
  594. + YFREE(new_objs);
  595. + new_objs = NULL;
  596. + }
  597. + if (list) {
  598. + YFREE(list);
  599. + list = NULL;
  600. + }
  601. + T(YAFFS_TRACE_ALLOCATE,
  602. + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
  603. + return YAFFS_FAIL;
  604. + }
  605. +
  606. + /* Hook them into the free list */
  607. + for (i = 0; i < n_obj - 1; i++) {
  608. + new_objs[i].siblings.next =
  609. + (struct list_head *)(&new_objs[i + 1]);
  610. + }
  611. +
  612. + new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
  613. + allocator->free_objs = new_objs;
  614. + allocator->n_free_objects += n_obj;
  615. + allocator->n_obj_created += n_obj;
  616. +
  617. + /* Now add this bunch of Objects to a list for freeing up. */
  618. +
  619. + list->objects = new_objs;
  620. + list->next = allocator->allocated_obj_list;
  621. + allocator->allocated_obj_list = list;
  622. +
  623. + return YAFFS_OK;
  624. +}
  625. +
  626. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  627. +{
  628. + struct yaffs_obj *obj = NULL;
  629. + struct yaffs_allocator *allocator = dev->allocator;
  630. +
  631. + if (!allocator) {
  632. + YBUG();
  633. + return obj;
  634. + }
  635. +
  636. + /* If there are none left make more */
  637. + if (!allocator->free_objs)
  638. + yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
  639. +
  640. + if (allocator->free_objs) {
  641. + obj = allocator->free_objs;
  642. + allocator->free_objs =
  643. + (struct yaffs_obj *)(allocator->free_objs->siblings.next);
  644. + allocator->n_free_objects--;
  645. + }
  646. +
  647. + return obj;
  648. +}
  649. +
  650. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  651. +{
  652. +
  653. + struct yaffs_allocator *allocator = dev->allocator;
  654. +
  655. + if (!allocator)
  656. + YBUG();
  657. + else {
  658. + /* Link into the free list. */
  659. + obj->siblings.next = (struct list_head *)(allocator->free_objs);
  660. + allocator->free_objs = obj;
  661. + allocator->n_free_objects++;
  662. + }
  663. +}
  664. +
  665. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  666. +{
  667. + if (dev->allocator) {
  668. + yaffs_deinit_raw_tnodes(dev);
  669. + yaffs_deinit_raw_objs(dev);
  670. +
  671. + YFREE(dev->allocator);
  672. + dev->allocator = NULL;
  673. + } else {
  674. + YBUG();
  675. + }
  676. +}
  677. +
  678. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  679. +{
  680. + struct yaffs_allocator *allocator;
  681. +
  682. + if (!dev->allocator) {
  683. + allocator = YMALLOC(sizeof(struct yaffs_allocator));
  684. + if (allocator) {
  685. + dev->allocator = allocator;
  686. + yaffs_init_raw_tnodes(dev);
  687. + yaffs_init_raw_objs(dev);
  688. + }
  689. + } else {
  690. + YBUG();
  691. + }
  692. +}
  693. +
  694. +#endif
  695. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.h linux-2.6.36/fs/yaffs2/yaffs_allocator.h
  696. --- linux-2.6.36.orig/fs/yaffs2/yaffs_allocator.h 1970-01-01 01:00:00.000000000 +0100
  697. +++ linux-2.6.36/fs/yaffs2/yaffs_allocator.h 2011-01-10 19:29:29.000000000 +0100
  698. @@ -0,0 +1,30 @@
  699. +/*
  700. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  701. + *
  702. + * Copyright (C) 2002-2010 Aleph One Ltd.
  703. + * for Toby Churchill Ltd and Brightstar Engineering
  704. + *
  705. + * Created by Charles Manning <charles@aleph1.co.uk>
  706. + *
  707. + * This program is free software; you can redistribute it and/or modify
  708. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  709. + * published by the Free Software Foundation.
  710. + *
  711. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  712. + */
  713. +
  714. +#ifndef __YAFFS_ALLOCATOR_H__
  715. +#define __YAFFS_ALLOCATOR_H__
  716. +
  717. +#include "yaffs_guts.h"
  718. +
  719. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev);
  720. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev);
  721. +
  722. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev);
  723. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn);
  724. +
  725. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev);
  726. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj);
  727. +
  728. +#endif
  729. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.c linux-2.6.36/fs/yaffs2/yaffs_attribs.c
  730. --- linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.c 1970-01-01 01:00:00.000000000 +0100
  731. +++ linux-2.6.36/fs/yaffs2/yaffs_attribs.c 2011-01-10 19:29:29.000000000 +0100
  732. @@ -0,0 +1,124 @@
  733. +/*
  734. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  735. + *
  736. + * Copyright (C) 2002-2010 Aleph One Ltd.
  737. + * for Toby Churchill Ltd and Brightstar Engineering
  738. + *
  739. + * Created by Charles Manning <charles@aleph1.co.uk>
  740. + *
  741. + * This program is free software; you can redistribute it and/or modify
  742. + * it under the terms of the GNU General Public License version 2 as
  743. + * published by the Free Software Foundation.
  744. + */
  745. +
  746. +#include "yaffs_guts.h"
  747. +#include "yaffs_attribs.h"
  748. +
  749. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
  750. +{
  751. + obj->yst_uid = oh->yst_uid;
  752. + obj->yst_gid = oh->yst_gid;
  753. + obj->yst_atime = oh->yst_atime;
  754. + obj->yst_mtime = oh->yst_mtime;
  755. + obj->yst_ctime = oh->yst_ctime;
  756. + obj->yst_rdev = oh->yst_rdev;
  757. +}
  758. +
  759. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
  760. +{
  761. + oh->yst_uid = obj->yst_uid;
  762. + oh->yst_gid = obj->yst_gid;
  763. + oh->yst_atime = obj->yst_atime;
  764. + oh->yst_mtime = obj->yst_mtime;
  765. + oh->yst_ctime = obj->yst_ctime;
  766. + oh->yst_rdev = obj->yst_rdev;
  767. +
  768. +}
  769. +
  770. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
  771. +{
  772. + obj->yst_mtime = Y_CURRENT_TIME;
  773. + if (do_a)
  774. + obj->yst_atime = obj->yst_atime;
  775. + if (do_c)
  776. + obj->yst_ctime = obj->yst_atime;
  777. +}
  778. +
  779. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
  780. +{
  781. + yaffs_load_current_time(obj, 1, 1);
  782. + obj->yst_rdev = rdev;
  783. + obj->yst_uid = uid;
  784. + obj->yst_gid = gid;
  785. +}
  786. +
  787. +loff_t yaffs_get_file_size(struct yaffs_obj *obj)
  788. +{
  789. + YCHAR *alias = NULL;
  790. + obj = yaffs_get_equivalent_obj(obj);
  791. +
  792. + switch (obj->variant_type) {
  793. + case YAFFS_OBJECT_TYPE_FILE:
  794. + return obj->variant.file_variant.file_size;
  795. + case YAFFS_OBJECT_TYPE_SYMLINK:
  796. + alias = obj->variant.symlink_variant.alias;
  797. + if (!alias)
  798. + return 0;
  799. + return yaffs_strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
  800. + default:
  801. + return 0;
  802. + }
  803. +}
  804. +
  805. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
  806. +{
  807. + unsigned int valid = attr->ia_valid;
  808. +
  809. + if (valid & ATTR_MODE)
  810. + obj->yst_mode = attr->ia_mode;
  811. + if (valid & ATTR_UID)
  812. + obj->yst_uid = attr->ia_uid;
  813. + if (valid & ATTR_GID)
  814. + obj->yst_gid = attr->ia_gid;
  815. +
  816. + if (valid & ATTR_ATIME)
  817. + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  818. + if (valid & ATTR_CTIME)
  819. + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  820. + if (valid & ATTR_MTIME)
  821. + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  822. +
  823. + if (valid & ATTR_SIZE)
  824. + yaffs_resize_file(obj, attr->ia_size);
  825. +
  826. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  827. +
  828. + return YAFFS_OK;
  829. +
  830. +}
  831. +
  832. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
  833. +{
  834. + unsigned int valid = 0;
  835. +
  836. + attr->ia_mode = obj->yst_mode;
  837. + valid |= ATTR_MODE;
  838. + attr->ia_uid = obj->yst_uid;
  839. + valid |= ATTR_UID;
  840. + attr->ia_gid = obj->yst_gid;
  841. + valid |= ATTR_GID;
  842. +
  843. + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  844. + valid |= ATTR_ATIME;
  845. + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  846. + valid |= ATTR_CTIME;
  847. + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  848. + valid |= ATTR_MTIME;
  849. +
  850. + attr->ia_size = yaffs_get_file_size(obj);
  851. + valid |= ATTR_SIZE;
  852. +
  853. + attr->ia_valid = valid;
  854. +
  855. + return YAFFS_OK;
  856. +}
  857. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.h linux-2.6.36/fs/yaffs2/yaffs_attribs.h
  858. --- linux-2.6.36.orig/fs/yaffs2/yaffs_attribs.h 1970-01-01 01:00:00.000000000 +0100
  859. +++ linux-2.6.36/fs/yaffs2/yaffs_attribs.h 2011-01-10 19:29:29.000000000 +0100
  860. @@ -0,0 +1,28 @@
  861. +/*
  862. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  863. + *
  864. + * Copyright (C) 2002-2010 Aleph One Ltd.
  865. + * for Toby Churchill Ltd and Brightstar Engineering
  866. + *
  867. + * Created by Charles Manning <charles@aleph1.co.uk>
  868. + *
  869. + * This program is free software; you can redistribute it and/or modify
  870. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  871. + * published by the Free Software Foundation.
  872. + *
  873. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  874. + */
  875. +
  876. +#ifndef __YAFFS_ATTRIBS_H__
  877. +#define __YAFFS_ATTRIBS_H__
  878. +
  879. +#include "yaffs_guts.h"
  880. +
  881. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh);
  882. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj);
  883. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev);
  884. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
  885. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
  886. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
  887. +
  888. +#endif
  889. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.c linux-2.6.36/fs/yaffs2/yaffs_bitmap.c
  890. --- linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.c 1970-01-01 01:00:00.000000000 +0100
  891. +++ linux-2.6.36/fs/yaffs2/yaffs_bitmap.c 2011-01-10 19:29:29.000000000 +0100
  892. @@ -0,0 +1,104 @@
  893. +/*
  894. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  895. + *
  896. + * Copyright (C) 2002-2010 Aleph One Ltd.
  897. + * for Toby Churchill Ltd and Brightstar Engineering
  898. + *
  899. + * Created by Charles Manning <charles@aleph1.co.uk>
  900. + *
  901. + * This program is free software; you can redistribute it and/or modify
  902. + * it under the terms of the GNU General Public License version 2 as
  903. + * published by the Free Software Foundation.
  904. + */
  905. +
  906. +#include "yaffs_bitmap.h"
  907. +#include "yaffs_trace.h"
  908. +/*
  909. + * Chunk bitmap manipulations
  910. + */
  911. +
  912. +static Y_INLINE u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
  913. +{
  914. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  915. + T(YAFFS_TRACE_ERROR,
  916. + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
  917. + blk));
  918. + YBUG();
  919. + }
  920. + return dev->chunk_bits +
  921. + (dev->chunk_bit_stride * (blk - dev->internal_start_block));
  922. +}
  923. +
  924. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
  925. +{
  926. + if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
  927. + chunk < 0 || chunk >= dev->param.chunks_per_block) {
  928. + T(YAFFS_TRACE_ERROR,
  929. + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid" TENDSTR),
  930. + blk, chunk));
  931. + YBUG();
  932. + }
  933. +}
  934. +
  935. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
  936. +{
  937. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  938. +
  939. + memset(blk_bits, 0, dev->chunk_bit_stride);
  940. +}
  941. +
  942. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  943. +{
  944. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  945. +
  946. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  947. +
  948. + blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
  949. +}
  950. +
  951. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  952. +{
  953. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  954. +
  955. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  956. +
  957. + blk_bits[chunk / 8] |= (1 << (chunk & 7));
  958. +}
  959. +
  960. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  961. +{
  962. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  963. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  964. +
  965. + return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  966. +}
  967. +
  968. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
  969. +{
  970. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  971. + int i;
  972. + for (i = 0; i < dev->chunk_bit_stride; i++) {
  973. + if (*blk_bits)
  974. + return 1;
  975. + blk_bits++;
  976. + }
  977. + return 0;
  978. +}
  979. +
  980. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
  981. +{
  982. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  983. + int i;
  984. + int n = 0;
  985. + for (i = 0; i < dev->chunk_bit_stride; i++) {
  986. + u8 x = *blk_bits;
  987. + while (x) {
  988. + if (x & 1)
  989. + n++;
  990. + x >>= 1;
  991. + }
  992. +
  993. + blk_bits++;
  994. + }
  995. + return n;
  996. +}
  997. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.h linux-2.6.36/fs/yaffs2/yaffs_bitmap.h
  998. --- linux-2.6.36.orig/fs/yaffs2/yaffs_bitmap.h 1970-01-01 01:00:00.000000000 +0100
  999. +++ linux-2.6.36/fs/yaffs2/yaffs_bitmap.h 2011-01-10 19:29:29.000000000 +0100
  1000. @@ -0,0 +1,33 @@
  1001. +/*
  1002. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1003. + *
  1004. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1005. + * for Toby Churchill Ltd and Brightstar Engineering
  1006. + *
  1007. + * Created by Charles Manning <charles@aleph1.co.uk>
  1008. + *
  1009. + * This program is free software; you can redistribute it and/or modify
  1010. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1011. + * published by the Free Software Foundation.
  1012. + *
  1013. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1014. + */
  1015. +
  1016. +/*
  1017. + * Chunk bitmap manipulations
  1018. + */
  1019. +
  1020. +#ifndef __YAFFS_BITMAP_H__
  1021. +#define __YAFFS_BITMAP_H__
  1022. +
  1023. +#include "yaffs_guts.h"
  1024. +
  1025. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk);
  1026. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk);
  1027. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1028. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1029. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  1030. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk);
  1031. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk);
  1032. +
  1033. +#endif
  1034. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.c linux-2.6.36/fs/yaffs2/yaffs_checkptrw.c
  1035. --- linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  1036. +++ linux-2.6.36/fs/yaffs2/yaffs_checkptrw.c 2011-01-10 19:29:29.000000000 +0100
  1037. @@ -0,0 +1,420 @@
  1038. +/*
  1039. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1040. + *
  1041. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1042. + * for Toby Churchill Ltd and Brightstar Engineering
  1043. + *
  1044. + * Created by Charles Manning <charles@aleph1.co.uk>
  1045. + *
  1046. + * This program is free software; you can redistribute it and/or modify
  1047. + * it under the terms of the GNU General Public License version 2 as
  1048. + * published by the Free Software Foundation.
  1049. + */
  1050. +
  1051. +#include "yaffs_checkptrw.h"
  1052. +#include "yaffs_getblockinfo.h"
  1053. +
  1054. +static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
  1055. +{
  1056. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1057. +
  1058. + T(YAFFS_TRACE_CHECKPOINT,
  1059. + (TSTR("checkpt blocks available = %d" TENDSTR), blocks_avail));
  1060. +
  1061. + return (blocks_avail <= 0) ? 0 : 1;
  1062. +}
  1063. +
  1064. +static int yaffs_checkpt_erase(struct yaffs_dev *dev)
  1065. +{
  1066. + int i;
  1067. +
  1068. + if (!dev->param.erase_fn)
  1069. + return 0;
  1070. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d" TENDSTR),
  1071. + dev->internal_start_block,
  1072. + dev->internal_end_block));
  1073. +
  1074. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  1075. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  1076. + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  1077. + T(YAFFS_TRACE_CHECKPOINT,
  1078. + (TSTR("erasing checkpt block %d" TENDSTR), i));
  1079. +
  1080. + dev->n_erasures++;
  1081. +
  1082. + if (dev->param.
  1083. + erase_fn(dev,
  1084. + i - dev->block_offset /* realign */ )) {
  1085. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1086. + dev->n_erased_blocks++;
  1087. + dev->n_free_chunks +=
  1088. + dev->param.chunks_per_block;
  1089. + } else {
  1090. + dev->param.bad_block_fn(dev, i);
  1091. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  1092. + }
  1093. + }
  1094. + }
  1095. +
  1096. + dev->blocks_in_checkpt = 0;
  1097. +
  1098. + return 1;
  1099. +}
  1100. +
  1101. +static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
  1102. +{
  1103. + int i;
  1104. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1105. + T(YAFFS_TRACE_CHECKPOINT,
  1106. + (TSTR
  1107. + ("allocating checkpt block: erased %d reserved %d avail %d next %d "
  1108. + TENDSTR), dev->n_erased_blocks, dev->param.n_reserved_blocks,
  1109. + blocks_avail, dev->checkpt_next_block));
  1110. +
  1111. + if (dev->checkpt_next_block >= 0 &&
  1112. + dev->checkpt_next_block <= dev->internal_end_block &&
  1113. + blocks_avail > 0) {
  1114. +
  1115. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1116. + i++) {
  1117. + struct yaffs_block_info *bi =
  1118. + yaffs_get_block_info(dev, i);
  1119. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  1120. + dev->checkpt_next_block = i + 1;
  1121. + dev->checkpt_cur_block = i;
  1122. + T(YAFFS_TRACE_CHECKPOINT,
  1123. + (TSTR("allocating checkpt block %d" TENDSTR),
  1124. + i));
  1125. + return;
  1126. + }
  1127. + }
  1128. + }
  1129. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks" TENDSTR)));
  1130. +
  1131. + dev->checkpt_next_block = -1;
  1132. + dev->checkpt_cur_block = -1;
  1133. +}
  1134. +
  1135. +static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
  1136. +{
  1137. + int i;
  1138. + struct yaffs_ext_tags tags;
  1139. +
  1140. + T(YAFFS_TRACE_CHECKPOINT,
  1141. + (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
  1142. + dev->blocks_in_checkpt, dev->checkpt_next_block));
  1143. +
  1144. + if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
  1145. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1146. + i++) {
  1147. + int chunk = i * dev->param.chunks_per_block;
  1148. + int realigned_chunk = chunk - dev->chunk_offset;
  1149. +
  1150. + dev->param.read_chunk_tags_fn(dev, realigned_chunk,
  1151. + NULL, &tags);
  1152. + T(YAFFS_TRACE_CHECKPOINT,
  1153. + (TSTR
  1154. + ("find next checkpt block: search: block %d oid %d seq %d eccr %d"
  1155. + TENDSTR), i, tags.obj_id, tags.seq_number,
  1156. + tags.ecc_result));
  1157. +
  1158. + if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  1159. + /* Right kind of block */
  1160. + dev->checkpt_next_block = tags.obj_id;
  1161. + dev->checkpt_cur_block = i;
  1162. + dev->checkpt_block_list[dev->
  1163. + blocks_in_checkpt] = i;
  1164. + dev->blocks_in_checkpt++;
  1165. + T(YAFFS_TRACE_CHECKPOINT,
  1166. + (TSTR("found checkpt block %d" TENDSTR), i));
  1167. + return;
  1168. + }
  1169. + }
  1170. +
  1171. + T(YAFFS_TRACE_CHECKPOINT,
  1172. + (TSTR("found no more checkpt blocks" TENDSTR)));
  1173. +
  1174. + dev->checkpt_next_block = -1;
  1175. + dev->checkpt_cur_block = -1;
  1176. +}
  1177. +
  1178. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
  1179. +{
  1180. +
  1181. + dev->checkpt_open_write = writing;
  1182. +
  1183. + /* Got the functions we need? */
  1184. + if (!dev->param.write_chunk_tags_fn ||
  1185. + !dev->param.read_chunk_tags_fn ||
  1186. + !dev->param.erase_fn || !dev->param.bad_block_fn)
  1187. + return 0;
  1188. +
  1189. + if (writing && !yaffs2_checkpt_space_ok(dev))
  1190. + return 0;
  1191. +
  1192. + if (!dev->checkpt_buffer)
  1193. + dev->checkpt_buffer =
  1194. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  1195. + if (!dev->checkpt_buffer)
  1196. + return 0;
  1197. +
  1198. + dev->checkpt_page_seq = 0;
  1199. + dev->checkpt_byte_count = 0;
  1200. + dev->checkpt_sum = 0;
  1201. + dev->checkpt_xor = 0;
  1202. + dev->checkpt_cur_block = -1;
  1203. + dev->checkpt_cur_chunk = -1;
  1204. + dev->checkpt_next_block = dev->internal_start_block;
  1205. +
  1206. + /* Erase all the blocks in the checkpoint area */
  1207. + if (writing) {
  1208. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1209. + dev->checkpt_byte_offs = 0;
  1210. + return yaffs_checkpt_erase(dev);
  1211. + } else {
  1212. + int i;
  1213. + /* Set to a value that will kick off a read */
  1214. + dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
  1215. + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
  1216. + * going to be way more than we need */
  1217. + dev->blocks_in_checkpt = 0;
  1218. + dev->checkpt_max_blocks =
  1219. + (dev->internal_end_block - dev->internal_start_block) / 16 +
  1220. + 2;
  1221. + dev->checkpt_block_list =
  1222. + YMALLOC(sizeof(int) * dev->checkpt_max_blocks);
  1223. + if (!dev->checkpt_block_list)
  1224. + return 0;
  1225. +
  1226. + for (i = 0; i < dev->checkpt_max_blocks; i++)
  1227. + dev->checkpt_block_list[i] = -1;
  1228. + }
  1229. +
  1230. + return 1;
  1231. +}
  1232. +
  1233. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  1234. +{
  1235. + u32 composite_sum;
  1236. + composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
  1237. + *sum = composite_sum;
  1238. + return 1;
  1239. +}
  1240. +
  1241. +static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  1242. +{
  1243. + int chunk;
  1244. + int realigned_chunk;
  1245. +
  1246. + struct yaffs_ext_tags tags;
  1247. +
  1248. + if (dev->checkpt_cur_block < 0) {
  1249. + yaffs2_checkpt_find_erased_block(dev);
  1250. + dev->checkpt_cur_chunk = 0;
  1251. + }
  1252. +
  1253. + if (dev->checkpt_cur_block < 0)
  1254. + return 0;
  1255. +
  1256. + tags.is_deleted = 0;
  1257. + tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  1258. + tags.chunk_id = dev->checkpt_page_seq + 1;
  1259. + tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  1260. + tags.n_bytes = dev->data_bytes_per_chunk;
  1261. + if (dev->checkpt_cur_chunk == 0) {
  1262. + /* First chunk we write for the block? Set block state to
  1263. + checkpoint */
  1264. + struct yaffs_block_info *bi =
  1265. + yaffs_get_block_info(dev, dev->checkpt_cur_block);
  1266. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1267. + dev->blocks_in_checkpt++;
  1268. + }
  1269. +
  1270. + chunk =
  1271. + dev->checkpt_cur_block * dev->param.chunks_per_block +
  1272. + dev->checkpt_cur_chunk;
  1273. +
  1274. + T(YAFFS_TRACE_CHECKPOINT,
  1275. + (TSTR
  1276. + ("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
  1277. + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, tags.obj_id,
  1278. + tags.chunk_id));
  1279. +
  1280. + realigned_chunk = chunk - dev->chunk_offset;
  1281. +
  1282. + dev->n_page_writes++;
  1283. +
  1284. + dev->param.write_chunk_tags_fn(dev, realigned_chunk,
  1285. + dev->checkpt_buffer, &tags);
  1286. + dev->checkpt_byte_offs = 0;
  1287. + dev->checkpt_page_seq++;
  1288. + dev->checkpt_cur_chunk++;
  1289. + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  1290. + dev->checkpt_cur_chunk = 0;
  1291. + dev->checkpt_cur_block = -1;
  1292. + }
  1293. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1294. +
  1295. + return 1;
  1296. +}
  1297. +
  1298. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  1299. +{
  1300. + int i = 0;
  1301. + int ok = 1;
  1302. +
  1303. + u8 *data_bytes = (u8 *) data;
  1304. +
  1305. + if (!dev->checkpt_buffer)
  1306. + return 0;
  1307. +
  1308. + if (!dev->checkpt_open_write)
  1309. + return -1;
  1310. +
  1311. + while (i < n_bytes && ok) {
  1312. + dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  1313. + dev->checkpt_sum += *data_bytes;
  1314. + dev->checkpt_xor ^= *data_bytes;
  1315. +
  1316. + dev->checkpt_byte_offs++;
  1317. + i++;
  1318. + data_bytes++;
  1319. + dev->checkpt_byte_count++;
  1320. +
  1321. + if (dev->checkpt_byte_offs < 0 ||
  1322. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  1323. + ok = yaffs2_checkpt_flush_buffer(dev);
  1324. + }
  1325. +
  1326. + return i;
  1327. +}
  1328. +
  1329. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  1330. +{
  1331. + int i = 0;
  1332. + int ok = 1;
  1333. + struct yaffs_ext_tags tags;
  1334. +
  1335. + int chunk;
  1336. + int realigned_chunk;
  1337. +
  1338. + u8 *data_bytes = (u8 *) data;
  1339. +
  1340. + if (!dev->checkpt_buffer)
  1341. + return 0;
  1342. +
  1343. + if (dev->checkpt_open_write)
  1344. + return -1;
  1345. +
  1346. + while (i < n_bytes && ok) {
  1347. +
  1348. + if (dev->checkpt_byte_offs < 0 ||
  1349. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  1350. +
  1351. + if (dev->checkpt_cur_block < 0) {
  1352. + yaffs2_checkpt_find_block(dev);
  1353. + dev->checkpt_cur_chunk = 0;
  1354. + }
  1355. +
  1356. + if (dev->checkpt_cur_block < 0)
  1357. + ok = 0;
  1358. + else {
  1359. + chunk = dev->checkpt_cur_block *
  1360. + dev->param.chunks_per_block +
  1361. + dev->checkpt_cur_chunk;
  1362. +
  1363. + realigned_chunk = chunk - dev->chunk_offset;
  1364. +
  1365. + dev->n_page_reads++;
  1366. +
  1367. + /* read in the next chunk */
  1368. + dev->param.read_chunk_tags_fn(dev,
  1369. + realigned_chunk,
  1370. + dev->
  1371. + checkpt_buffer,
  1372. + &tags);
  1373. +
  1374. + if (tags.chunk_id != (dev->checkpt_page_seq + 1)
  1375. + || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
  1376. + || tags.seq_number !=
  1377. + YAFFS_SEQUENCE_CHECKPOINT_DATA)
  1378. + ok = 0;
  1379. +
  1380. + dev->checkpt_byte_offs = 0;
  1381. + dev->checkpt_page_seq++;
  1382. + dev->checkpt_cur_chunk++;
  1383. +
  1384. + if (dev->checkpt_cur_chunk >=
  1385. + dev->param.chunks_per_block)
  1386. + dev->checkpt_cur_block = -1;
  1387. + }
  1388. + }
  1389. +
  1390. + if (ok) {
  1391. + *data_bytes =
  1392. + dev->checkpt_buffer[dev->checkpt_byte_offs];
  1393. + dev->checkpt_sum += *data_bytes;
  1394. + dev->checkpt_xor ^= *data_bytes;
  1395. + dev->checkpt_byte_offs++;
  1396. + i++;
  1397. + data_bytes++;
  1398. + dev->checkpt_byte_count++;
  1399. + }
  1400. + }
  1401. +
  1402. + return i;
  1403. +}
  1404. +
  1405. +int yaffs_checkpt_close(struct yaffs_dev *dev)
  1406. +{
  1407. +
  1408. + if (dev->checkpt_open_write) {
  1409. + if (dev->checkpt_byte_offs != 0)
  1410. + yaffs2_checkpt_flush_buffer(dev);
  1411. + } else if (dev->checkpt_block_list) {
  1412. + int i;
  1413. + for (i = 0;
  1414. + i < dev->blocks_in_checkpt
  1415. + && dev->checkpt_block_list[i] >= 0; i++) {
  1416. + int blk = dev->checkpt_block_list[i];
  1417. + struct yaffs_block_info *bi = NULL;
  1418. + if (dev->internal_start_block <= blk
  1419. + && blk <= dev->internal_end_block)
  1420. + bi = yaffs_get_block_info(dev, blk);
  1421. + if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  1422. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1423. + else {
  1424. + /* Todo this looks odd... */
  1425. + }
  1426. + }
  1427. + YFREE(dev->checkpt_block_list);
  1428. + dev->checkpt_block_list = NULL;
  1429. + }
  1430. +
  1431. + dev->n_free_chunks -=
  1432. + dev->blocks_in_checkpt * dev->param.chunks_per_block;
  1433. + dev->n_erased_blocks -= dev->blocks_in_checkpt;
  1434. +
  1435. + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
  1436. + dev->checkpt_byte_count));
  1437. +
  1438. + if (dev->checkpt_buffer) {
  1439. + /* free the buffer */
  1440. + YFREE(dev->checkpt_buffer);
  1441. + dev->checkpt_buffer = NULL;
  1442. + return 1;
  1443. + } else {
  1444. + return 0;
  1445. + }
  1446. +}
  1447. +
  1448. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  1449. +{
  1450. + /* Erase the checkpoint data */
  1451. +
  1452. + T(YAFFS_TRACE_CHECKPOINT,
  1453. + (TSTR("checkpoint invalidate of %d blocks" TENDSTR),
  1454. + dev->blocks_in_checkpt));
  1455. +
  1456. + return yaffs_checkpt_erase(dev);
  1457. +}
  1458. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.h linux-2.6.36/fs/yaffs2/yaffs_checkptrw.h
  1459. --- linux-2.6.36.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  1460. +++ linux-2.6.36/fs/yaffs2/yaffs_checkptrw.h 2011-01-10 19:29:29.000000000 +0100
  1461. @@ -0,0 +1,33 @@
  1462. +/*
  1463. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1464. + *
  1465. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1466. + * for Toby Churchill Ltd and Brightstar Engineering
  1467. + *
  1468. + * Created by Charles Manning <charles@aleph1.co.uk>
  1469. + *
  1470. + * This program is free software; you can redistribute it and/or modify
  1471. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1472. + * published by the Free Software Foundation.
  1473. + *
  1474. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1475. + */
  1476. +
  1477. +#ifndef __YAFFS_CHECKPTRW_H__
  1478. +#define __YAFFS_CHECKPTRW_H__
  1479. +
  1480. +#include "yaffs_guts.h"
  1481. +
  1482. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing);
  1483. +
  1484. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes);
  1485. +
  1486. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes);
  1487. +
  1488. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum);
  1489. +
  1490. +int yaffs_checkpt_close(struct yaffs_dev *dev);
  1491. +
  1492. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev);
  1493. +
  1494. +#endif
  1495. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.c linux-2.6.36/fs/yaffs2/yaffs_ecc.c
  1496. --- linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1497. +++ linux-2.6.36/fs/yaffs2/yaffs_ecc.c 2011-01-10 19:29:29.000000000 +0100
  1498. @@ -0,0 +1,322 @@
  1499. +/*
  1500. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1501. + *
  1502. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1503. + * for Toby Churchill Ltd and Brightstar Engineering
  1504. + *
  1505. + * Created by Charles Manning <charles@aleph1.co.uk>
  1506. + *
  1507. + * This program is free software; you can redistribute it and/or modify
  1508. + * it under the terms of the GNU General Public License version 2 as
  1509. + * published by the Free Software Foundation.
  1510. + */
  1511. +
  1512. +/*
  1513. + * This code implements the ECC algorithm used in SmartMedia.
  1514. + *
  1515. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1516. + * The two unused bit are set to 1.
  1517. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1518. + * blocks are used on a 512-byte NAND page.
  1519. + *
  1520. + */
  1521. +
  1522. +/* Table generated by gen-ecc.c
  1523. + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  1524. + * for each byte of data. These are instead provided in a table in bits7..2.
  1525. + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  1526. + * this bytes influence on the line parity.
  1527. + */
  1528. +
  1529. +#include "yportenv.h"
  1530. +
  1531. +#include "yaffs_ecc.h"
  1532. +
  1533. +static const unsigned char column_parity_table[] = {
  1534. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1535. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1536. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1537. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1538. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1539. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1540. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1541. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1542. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1543. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1544. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1545. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1546. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1547. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1548. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1549. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1550. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1551. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1552. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1553. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1554. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1555. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1556. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1557. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1558. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1559. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1560. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1561. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1562. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1563. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1564. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1565. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1566. +};
  1567. +
  1568. +/* Count the bits in an unsigned char or a U32 */
  1569. +
  1570. +static int yaffs_count_bits(unsigned char x)
  1571. +{
  1572. + int r = 0;
  1573. + while (x) {
  1574. + if (x & 1)
  1575. + r++;
  1576. + x >>= 1;
  1577. + }
  1578. + return r;
  1579. +}
  1580. +
  1581. +static int yaffs_count_bits32(unsigned x)
  1582. +{
  1583. + int r = 0;
  1584. + while (x) {
  1585. + if (x & 1)
  1586. + r++;
  1587. + x >>= 1;
  1588. + }
  1589. + return r;
  1590. +}
  1591. +
  1592. +/* Calculate the ECC for a 256-byte block of data */
  1593. +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
  1594. +{
  1595. + unsigned int i;
  1596. +
  1597. + unsigned char col_parity = 0;
  1598. + unsigned char line_parity = 0;
  1599. + unsigned char line_parity_prime = 0;
  1600. + unsigned char t;
  1601. + unsigned char b;
  1602. +
  1603. + for (i = 0; i < 256; i++) {
  1604. + b = column_parity_table[*data++];
  1605. + col_parity ^= b;
  1606. +
  1607. + if (b & 0x01) { /* odd number of bits in the byte */
  1608. + line_parity ^= i;
  1609. + line_parity_prime ^= ~i;
  1610. + }
  1611. + }
  1612. +
  1613. + ecc[2] = (~col_parity) | 0x03;
  1614. +
  1615. + t = 0;
  1616. + if (line_parity & 0x80)
  1617. + t |= 0x80;
  1618. + if (line_parity_prime & 0x80)
  1619. + t |= 0x40;
  1620. + if (line_parity & 0x40)
  1621. + t |= 0x20;
  1622. + if (line_parity_prime & 0x40)
  1623. + t |= 0x10;
  1624. + if (line_parity & 0x20)
  1625. + t |= 0x08;
  1626. + if (line_parity_prime & 0x20)
  1627. + t |= 0x04;
  1628. + if (line_parity & 0x10)
  1629. + t |= 0x02;
  1630. + if (line_parity_prime & 0x10)
  1631. + t |= 0x01;
  1632. + ecc[1] = ~t;
  1633. +
  1634. + t = 0;
  1635. + if (line_parity & 0x08)
  1636. + t |= 0x80;
  1637. + if (line_parity_prime & 0x08)
  1638. + t |= 0x40;
  1639. + if (line_parity & 0x04)
  1640. + t |= 0x20;
  1641. + if (line_parity_prime & 0x04)
  1642. + t |= 0x10;
  1643. + if (line_parity & 0x02)
  1644. + t |= 0x08;
  1645. + if (line_parity_prime & 0x02)
  1646. + t |= 0x04;
  1647. + if (line_parity & 0x01)
  1648. + t |= 0x02;
  1649. + if (line_parity_prime & 0x01)
  1650. + t |= 0x01;
  1651. + ecc[0] = ~t;
  1652. +
  1653. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1654. + /* Swap the bytes into the wrong order */
  1655. + t = ecc[0];
  1656. + ecc[0] = ecc[1];
  1657. + ecc[1] = t;
  1658. +#endif
  1659. +}
  1660. +
  1661. +/* Correct the ECC on a 256 byte block of data */
  1662. +
  1663. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1664. + const unsigned char *test_ecc)
  1665. +{
  1666. + unsigned char d0, d1, d2; /* deltas */
  1667. +
  1668. + d0 = read_ecc[0] ^ test_ecc[0];
  1669. + d1 = read_ecc[1] ^ test_ecc[1];
  1670. + d2 = read_ecc[2] ^ test_ecc[2];
  1671. +
  1672. + if ((d0 | d1 | d2) == 0)
  1673. + return 0; /* no error */
  1674. +
  1675. + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  1676. + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  1677. + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  1678. + /* Single bit (recoverable) error in data */
  1679. +
  1680. + unsigned byte;
  1681. + unsigned bit;
  1682. +
  1683. +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  1684. + /* swap the bytes to correct for the wrong order */
  1685. + unsigned char t;
  1686. +
  1687. + t = d0;
  1688. + d0 = d1;
  1689. + d1 = t;
  1690. +#endif
  1691. +
  1692. + bit = byte = 0;
  1693. +
  1694. + if (d1 & 0x80)
  1695. + byte |= 0x80;
  1696. + if (d1 & 0x20)
  1697. + byte |= 0x40;
  1698. + if (d1 & 0x08)
  1699. + byte |= 0x20;
  1700. + if (d1 & 0x02)
  1701. + byte |= 0x10;
  1702. + if (d0 & 0x80)
  1703. + byte |= 0x08;
  1704. + if (d0 & 0x20)
  1705. + byte |= 0x04;
  1706. + if (d0 & 0x08)
  1707. + byte |= 0x02;
  1708. + if (d0 & 0x02)
  1709. + byte |= 0x01;
  1710. +
  1711. + if (d2 & 0x80)
  1712. + bit |= 0x04;
  1713. + if (d2 & 0x20)
  1714. + bit |= 0x02;
  1715. + if (d2 & 0x08)
  1716. + bit |= 0x01;
  1717. +
  1718. + data[byte] ^= (1 << bit);
  1719. +
  1720. + return 1; /* Corrected the error */
  1721. + }
  1722. +
  1723. + if ((yaffs_count_bits(d0) +
  1724. + yaffs_count_bits(d1) + yaffs_count_bits(d2)) == 1) {
  1725. + /* Reccoverable error in ecc */
  1726. +
  1727. + read_ecc[0] = test_ecc[0];
  1728. + read_ecc[1] = test_ecc[1];
  1729. + read_ecc[2] = test_ecc[2];
  1730. +
  1731. + return 1; /* Corrected the error */
  1732. + }
  1733. +
  1734. + /* Unrecoverable error */
  1735. +
  1736. + return -1;
  1737. +
  1738. +}
  1739. +
  1740. +/*
  1741. + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  1742. + */
  1743. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1744. + struct yaffs_ecc_other *ecc_other)
  1745. +{
  1746. + unsigned int i;
  1747. +
  1748. + unsigned char col_parity = 0;
  1749. + unsigned line_parity = 0;
  1750. + unsigned line_parity_prime = 0;
  1751. + unsigned char b;
  1752. +
  1753. + for (i = 0; i < n_bytes; i++) {
  1754. + b = column_parity_table[*data++];
  1755. + col_parity ^= b;
  1756. +
  1757. + if (b & 0x01) {
  1758. + /* odd number of bits in the byte */
  1759. + line_parity ^= i;
  1760. + line_parity_prime ^= ~i;
  1761. + }
  1762. +
  1763. + }
  1764. +
  1765. + ecc_other->col_parity = (col_parity >> 2) & 0x3f;
  1766. + ecc_other->line_parity = line_parity;
  1767. + ecc_other->line_parity_prime = line_parity_prime;
  1768. +}
  1769. +
  1770. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1771. + struct yaffs_ecc_other *read_ecc,
  1772. + const struct yaffs_ecc_other *test_ecc)
  1773. +{
  1774. + unsigned char delta_col; /* column parity delta */
  1775. + unsigned delta_line; /* line parity delta */
  1776. + unsigned delta_line_prime; /* line parity delta */
  1777. + unsigned bit;
  1778. +
  1779. + delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
  1780. + delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
  1781. + delta_line_prime =
  1782. + read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
  1783. +
  1784. + if ((delta_col | delta_line | delta_line_prime) == 0)
  1785. + return 0; /* no error */
  1786. +
  1787. + if (delta_line == ~delta_line_prime &&
  1788. + (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
  1789. + /* Single bit (recoverable) error in data */
  1790. +
  1791. + bit = 0;
  1792. +
  1793. + if (delta_col & 0x20)
  1794. + bit |= 0x04;
  1795. + if (delta_col & 0x08)
  1796. + bit |= 0x02;
  1797. + if (delta_col & 0x02)
  1798. + bit |= 0x01;
  1799. +
  1800. + if (delta_line >= n_bytes)
  1801. + return -1;
  1802. +
  1803. + data[delta_line] ^= (1 << bit);
  1804. +
  1805. + return 1; /* corrected */
  1806. + }
  1807. +
  1808. + if ((yaffs_count_bits32(delta_line) +
  1809. + yaffs_count_bits32(delta_line_prime) +
  1810. + yaffs_count_bits(delta_col)) == 1) {
  1811. + /* Reccoverable error in ecc */
  1812. +
  1813. + *read_ecc = *test_ecc;
  1814. + return 1; /* corrected */
  1815. + }
  1816. +
  1817. + /* Unrecoverable error */
  1818. +
  1819. + return -1;
  1820. +}
  1821. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.h linux-2.6.36/fs/yaffs2/yaffs_ecc.h
  1822. --- linux-2.6.36.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1823. +++ linux-2.6.36/fs/yaffs2/yaffs_ecc.h 2011-01-10 19:29:29.000000000 +0100
  1824. @@ -0,0 +1,44 @@
  1825. +/*
  1826. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1827. + *
  1828. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1829. + * for Toby Churchill Ltd and Brightstar Engineering
  1830. + *
  1831. + * Created by Charles Manning <charles@aleph1.co.uk>
  1832. + *
  1833. + * This program is free software; you can redistribute it and/or modify
  1834. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1835. + * published by the Free Software Foundation.
  1836. + *
  1837. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1838. + */
  1839. +
  1840. +/*
  1841. + * This code implements the ECC algorithm used in SmartMedia.
  1842. + *
  1843. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1844. + * The two unused bit are set to 1.
  1845. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
  1846. + * blocks are used on a 512-byte NAND page.
  1847. + *
  1848. + */
  1849. +
  1850. +#ifndef __YAFFS_ECC_H__
  1851. +#define __YAFFS_ECC_H__
  1852. +
  1853. +struct yaffs_ecc_other {
  1854. + unsigned char col_parity;
  1855. + unsigned line_parity;
  1856. + unsigned line_parity_prime;
  1857. +};
  1858. +
  1859. +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc);
  1860. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1861. + const unsigned char *test_ecc);
  1862. +
  1863. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1864. + struct yaffs_ecc_other *ecc);
  1865. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1866. + struct yaffs_ecc_other *read_ecc,
  1867. + const struct yaffs_ecc_other *test_ecc);
  1868. +#endif
  1869. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_getblockinfo.h linux-2.6.36/fs/yaffs2/yaffs_getblockinfo.h
  1870. --- linux-2.6.36.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  1871. +++ linux-2.6.36/fs/yaffs2/yaffs_getblockinfo.h 2011-01-10 19:29:29.000000000 +0100
  1872. @@ -0,0 +1,36 @@
  1873. +/*
  1874. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1875. + *
  1876. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1877. + * for Toby Churchill Ltd and Brightstar Engineering
  1878. + *
  1879. + * Created by Charles Manning <charles@aleph1.co.uk>
  1880. + *
  1881. + * This program is free software; you can redistribute it and/or modify
  1882. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1883. + * published by the Free Software Foundation.
  1884. + *
  1885. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1886. + */
  1887. +
  1888. +#ifndef __YAFFS_GETBLOCKINFO_H__
  1889. +#define __YAFFS_GETBLOCKINFO_H__
  1890. +
  1891. +#include "yaffs_guts.h"
  1892. +#include "yaffs_trace.h"
  1893. +
  1894. +/* Function to manipulate block info */
  1895. +static Y_INLINE struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev
  1896. + *dev, int blk)
  1897. +{
  1898. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  1899. + T(YAFFS_TRACE_ERROR,
  1900. + (TSTR
  1901. + ("**>> yaffs: get_block_info block %d is not valid" TENDSTR),
  1902. + blk));
  1903. + YBUG();
  1904. + }
  1905. + return &dev->block_info[blk - dev->internal_start_block];
  1906. +}
  1907. +
  1908. +#endif
  1909. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_guts.c linux-2.6.36/fs/yaffs2/yaffs_guts.c
  1910. --- linux-2.6.36.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  1911. +++ linux-2.6.36/fs/yaffs2/yaffs_guts.c 2011-01-10 19:29:29.000000000 +0100
  1912. @@ -0,0 +1,5227 @@
  1913. +/*
  1914. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1915. + *
  1916. + * Copyright (C) 2002-2010 Aleph One Ltd.
  1917. + * for Toby Churchill Ltd and Brightstar Engineering
  1918. + *
  1919. + * Created by Charles Manning <charles@aleph1.co.uk>
  1920. + *
  1921. + * This program is free software; you can redistribute it and/or modify
  1922. + * it under the terms of the GNU General Public License version 2 as
  1923. + * published by the Free Software Foundation.
  1924. + */
  1925. +
  1926. +#include "yportenv.h"
  1927. +#include "yaffs_trace.h"
  1928. +
  1929. +#include "yaffs_guts.h"
  1930. +#include "yaffs_tagsvalidity.h"
  1931. +#include "yaffs_getblockinfo.h"
  1932. +
  1933. +#include "yaffs_tagscompat.h"
  1934. +
  1935. +#include "yaffs_nand.h"
  1936. +
  1937. +#include "yaffs_yaffs1.h"
  1938. +#include "yaffs_yaffs2.h"
  1939. +#include "yaffs_bitmap.h"
  1940. +#include "yaffs_verify.h"
  1941. +
  1942. +#include "yaffs_nand.h"
  1943. +#include "yaffs_packedtags2.h"
  1944. +
  1945. +#include "yaffs_nameval.h"
  1946. +#include "yaffs_allocator.h"
  1947. +
  1948. +#include "yaffs_attribs.h"
  1949. +
  1950. +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
  1951. +#define YAFFS_GC_GOOD_ENOUGH 2
  1952. +#define YAFFS_GC_PASSIVE_THRESHOLD 4
  1953. +
  1954. +#include "yaffs_ecc.h"
  1955. +
  1956. +/* Robustification (if it ever comes about...) */
  1957. +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block);
  1958. +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  1959. + int erased_ok);
  1960. +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  1961. + const u8 * data,
  1962. + const struct yaffs_ext_tags *tags);
  1963. +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  1964. + const struct yaffs_ext_tags *tags);
  1965. +
  1966. +/* Other local prototypes */
  1967. +static void yaffs_update_parent(struct yaffs_obj *obj);
  1968. +static int yaffs_unlink_obj(struct yaffs_obj *obj);
  1969. +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj);
  1970. +
  1971. +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  1972. + const u8 * buffer,
  1973. + struct yaffs_ext_tags *tags, int use_reserver);
  1974. +
  1975. +static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  1976. + enum yaffs_obj_type type);
  1977. +
  1978. +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  1979. + struct yaffs_xattr_mod *xmod);
  1980. +
  1981. +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj);
  1982. +static int yaffs_generic_obj_del(struct yaffs_obj *in);
  1983. +
  1984. +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk);
  1985. +
  1986. +static int yaffs_unlink_worker(struct yaffs_obj *obj);
  1987. +
  1988. +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  1989. + int chunk_obj);
  1990. +
  1991. +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  1992. + struct yaffs_block_info **block_ptr);
  1993. +
  1994. +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in);
  1995. +
  1996. +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in);
  1997. +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object,
  1998. + int chunk_id);
  1999. +
  2000. +static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  2001. + struct yaffs_ext_tags *tags);
  2002. +
  2003. +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  2004. + int nand_chunk,
  2005. + const u8 * data,
  2006. + struct yaffs_ext_tags *tags);
  2007. +
  2008. +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
  2009. + const YCHAR * oh_name, int buff_size);
  2010. +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
  2011. + const YCHAR * name);
  2012. +
  2013. +/* Function to calculate chunk and offset */
  2014. +
  2015. +static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  2016. + int *chunk_out, u32 * offset_out)
  2017. +{
  2018. + int chunk;
  2019. + u32 offset;
  2020. +
  2021. + chunk = (u32) (addr >> dev->chunk_shift);
  2022. +
  2023. + if (dev->chunk_div == 1) {
  2024. + /* easy power of 2 case */
  2025. + offset = (u32) (addr & dev->chunk_mask);
  2026. + } else {
  2027. + /* Non power-of-2 case */
  2028. +
  2029. + loff_t chunk_base;
  2030. +
  2031. + chunk /= dev->chunk_div;
  2032. +
  2033. + chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  2034. + offset = (u32) (addr - chunk_base);
  2035. + }
  2036. +
  2037. + *chunk_out = chunk;
  2038. + *offset_out = offset;
  2039. +}
  2040. +
  2041. +/* Function to return the number of shifts for a power of 2 greater than or
  2042. + * equal to the given number
  2043. + * Note we don't try to cater for all possible numbers and this does not have to
  2044. + * be hellishly efficient.
  2045. + */
  2046. +
  2047. +static u32 calc_shifts_ceiling(u32 x)
  2048. +{
  2049. + int extra_bits;
  2050. + int shifts;
  2051. +
  2052. + shifts = extra_bits = 0;
  2053. +
  2054. + while (x > 1) {
  2055. + if (x & 1)
  2056. + extra_bits++;
  2057. + x >>= 1;
  2058. + shifts++;
  2059. + }
  2060. +
  2061. + if (extra_bits)
  2062. + shifts++;
  2063. +
  2064. + return shifts;
  2065. +}
  2066. +
  2067. +/* Function to return the number of shifts to get a 1 in bit 0
  2068. + */
  2069. +
  2070. +static u32 calc_shifts(u32 x)
  2071. +{
  2072. + u32 shifts;
  2073. +
  2074. + shifts = 0;
  2075. +
  2076. + if (!x)
  2077. + return 0;
  2078. +
  2079. + while (!(x & 1)) {
  2080. + x >>= 1;
  2081. + shifts++;
  2082. + }
  2083. +
  2084. + return shifts;
  2085. +}
  2086. +
  2087. +/*
  2088. + * Temporary buffer manipulations.
  2089. + */
  2090. +
  2091. +static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  2092. +{
  2093. + int i;
  2094. + u8 *buf = (u8 *) 1;
  2095. +
  2096. + memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  2097. +
  2098. + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  2099. + dev->temp_buffer[i].line = 0; /* not in use */
  2100. + dev->temp_buffer[i].buffer = buf =
  2101. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  2102. + }
  2103. +
  2104. + return buf ? YAFFS_OK : YAFFS_FAIL;
  2105. +}
  2106. +
  2107. +u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
  2108. +{
  2109. + int i, j;
  2110. +
  2111. + dev->temp_in_use++;
  2112. + if (dev->temp_in_use > dev->max_temp)
  2113. + dev->max_temp = dev->temp_in_use;
  2114. +
  2115. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2116. + if (dev->temp_buffer[i].line == 0) {
  2117. + dev->temp_buffer[i].line = line_no;
  2118. + if ((i + 1) > dev->max_temp) {
  2119. + dev->max_temp = i + 1;
  2120. + for (j = 0; j <= i; j++)
  2121. + dev->temp_buffer[j].max_line =
  2122. + dev->temp_buffer[j].line;
  2123. + }
  2124. +
  2125. + return dev->temp_buffer[i].buffer;
  2126. + }
  2127. + }
  2128. +
  2129. + T(YAFFS_TRACE_BUFFERS,
  2130. + (TSTR("Out of temp buffers at line %d, other held by lines:"),
  2131. + line_no));
  2132. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  2133. + T(YAFFS_TRACE_BUFFERS,
  2134. + (TSTR(" %d "), dev->temp_buffer[i].line));
  2135. +
  2136. + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
  2137. +
  2138. + /*
  2139. + * If we got here then we have to allocate an unmanaged one
  2140. + * This is not good.
  2141. + */
  2142. +
  2143. + dev->unmanaged_buffer_allocs++;
  2144. + return YMALLOC(dev->data_bytes_per_chunk);
  2145. +
  2146. +}
  2147. +
  2148. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
  2149. +{
  2150. + int i;
  2151. +
  2152. + dev->temp_in_use--;
  2153. +
  2154. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2155. + if (dev->temp_buffer[i].buffer == buffer) {
  2156. + dev->temp_buffer[i].line = 0;
  2157. + return;
  2158. + }
  2159. + }
  2160. +
  2161. + if (buffer) {
  2162. + /* assume it is an unmanaged one. */
  2163. + T(YAFFS_TRACE_BUFFERS,
  2164. + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
  2165. + line_no));
  2166. + YFREE(buffer);
  2167. + dev->unmanaged_buffer_deallocs++;
  2168. + }
  2169. +
  2170. +}
  2171. +
  2172. +/*
  2173. + * Determine if we have a managed buffer.
  2174. + */
  2175. +int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
  2176. +{
  2177. + int i;
  2178. +
  2179. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2180. + if (dev->temp_buffer[i].buffer == buffer)
  2181. + return 1;
  2182. + }
  2183. +
  2184. + for (i = 0; i < dev->param.n_caches; i++) {
  2185. + if (dev->cache[i].data == buffer)
  2186. + return 1;
  2187. + }
  2188. +
  2189. + if (buffer == dev->checkpt_buffer)
  2190. + return 1;
  2191. +
  2192. + T(YAFFS_TRACE_ALWAYS,
  2193. + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
  2194. + return 0;
  2195. +}
  2196. +
  2197. +/*
  2198. + * Verification code
  2199. + */
  2200. +
  2201. +/*
  2202. + * Simple hash function. Needs to have a reasonable spread
  2203. + */
  2204. +
  2205. +static Y_INLINE int yaffs_hash_fn(int n)
  2206. +{
  2207. + n = abs(n);
  2208. + return n % YAFFS_NOBJECT_BUCKETS;
  2209. +}
  2210. +
  2211. +/*
  2212. + * Access functions to useful fake objects.
  2213. + * Note that root might have a presence in NAND if permissions are set.
  2214. + */
  2215. +
  2216. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  2217. +{
  2218. + return dev->root_dir;
  2219. +}
  2220. +
  2221. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  2222. +{
  2223. + return dev->lost_n_found;
  2224. +}
  2225. +
  2226. +/*
  2227. + * Erased NAND checking functions
  2228. + */
  2229. +
  2230. +int yaffs_check_ff(u8 * buffer, int n_bytes)
  2231. +{
  2232. + /* Horrible, slow implementation */
  2233. + while (n_bytes--) {
  2234. + if (*buffer != 0xFF)
  2235. + return 0;
  2236. + buffer++;
  2237. + }
  2238. + return 1;
  2239. +}
  2240. +
  2241. +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  2242. +{
  2243. + int retval = YAFFS_OK;
  2244. + u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
  2245. + struct yaffs_ext_tags tags;
  2246. + int result;
  2247. +
  2248. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  2249. +
  2250. + if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  2251. + retval = YAFFS_FAIL;
  2252. +
  2253. + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
  2254. + T(YAFFS_TRACE_NANDACCESS,
  2255. + (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
  2256. + retval = YAFFS_FAIL;
  2257. + }
  2258. +
  2259. + yaffs_release_temp_buffer(dev, data, __LINE__);
  2260. +
  2261. + return retval;
  2262. +
  2263. +}
  2264. +
  2265. +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  2266. + int nand_chunk,
  2267. + const u8 * data,
  2268. + struct yaffs_ext_tags *tags)
  2269. +{
  2270. + int retval = YAFFS_OK;
  2271. + struct yaffs_ext_tags temp_tags;
  2272. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  2273. + int result;
  2274. +
  2275. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  2276. + if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  2277. + temp_tags.obj_id != tags->obj_id ||
  2278. + temp_tags.chunk_id != tags->chunk_id ||
  2279. + temp_tags.n_bytes != tags->n_bytes)
  2280. + retval = YAFFS_FAIL;
  2281. +
  2282. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2283. +
  2284. + return retval;
  2285. +}
  2286. +
  2287. +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  2288. + const u8 * data,
  2289. + struct yaffs_ext_tags *tags, int use_reserver)
  2290. +{
  2291. + int attempts = 0;
  2292. + int write_ok = 0;
  2293. + int chunk;
  2294. +
  2295. + yaffs2_checkpt_invalidate(dev);
  2296. +
  2297. + do {
  2298. + struct yaffs_block_info *bi = 0;
  2299. + int erased_ok = 0;
  2300. +
  2301. + chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  2302. + if (chunk < 0) {
  2303. + /* no space */
  2304. + break;
  2305. + }
  2306. +
  2307. + /* First check this chunk is erased, if it needs
  2308. + * checking. The checking policy (unless forced
  2309. + * always on) is as follows:
  2310. + *
  2311. + * Check the first page we try to write in a block.
  2312. + * If the check passes then we don't need to check any
  2313. + * more. If the check fails, we check again...
  2314. + * If the block has been erased, we don't need to check.
  2315. + *
  2316. + * However, if the block has been prioritised for gc,
  2317. + * then we think there might be something odd about
  2318. + * this block and stop using it.
  2319. + *
  2320. + * Rationale: We should only ever see chunks that have
  2321. + * not been erased if there was a partially written
  2322. + * chunk due to power loss. This checking policy should
  2323. + * catch that case with very few checks and thus save a
  2324. + * lot of checks that are most likely not needed.
  2325. + *
  2326. + * Mods to the above
  2327. + * If an erase check fails or the write fails we skip the
  2328. + * rest of the block.
  2329. + */
  2330. +
  2331. + /* let's give it a try */
  2332. + attempts++;
  2333. +
  2334. + if (dev->param.always_check_erased)
  2335. + bi->skip_erased_check = 0;
  2336. +
  2337. + if (!bi->skip_erased_check) {
  2338. + erased_ok = yaffs_check_chunk_erased(dev, chunk);
  2339. + if (erased_ok != YAFFS_OK) {
  2340. + T(YAFFS_TRACE_ERROR,
  2341. + (TSTR("**>> yaffs chunk %d was not erased"
  2342. + TENDSTR), chunk));
  2343. +
  2344. + /* If not erased, delete this one,
  2345. + * skip rest of block and
  2346. + * try another chunk */
  2347. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  2348. + yaffs_skip_rest_of_block(dev);
  2349. + continue;
  2350. + }
  2351. + }
  2352. +
  2353. + write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  2354. +
  2355. + if (!bi->skip_erased_check)
  2356. + write_ok =
  2357. + yaffs_verify_chunk_written(dev, chunk, data, tags);
  2358. +
  2359. + if (write_ok != YAFFS_OK) {
  2360. + /* Clean up aborted write, skip to next block and
  2361. + * try another chunk */
  2362. + yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  2363. + continue;
  2364. + }
  2365. +
  2366. + bi->skip_erased_check = 1;
  2367. +
  2368. + /* Copy the data into the robustification buffer */
  2369. + yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  2370. +
  2371. + } while (write_ok != YAFFS_OK &&
  2372. + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  2373. +
  2374. + if (!write_ok)
  2375. + chunk = -1;
  2376. +
  2377. + if (attempts > 1) {
  2378. + T(YAFFS_TRACE_ERROR,
  2379. + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
  2380. + attempts));
  2381. +
  2382. + dev->n_retired_writes += (attempts - 1);
  2383. + }
  2384. +
  2385. + return chunk;
  2386. +}
  2387. +
  2388. +/*
  2389. + * Block retiring for handling a broken block.
  2390. + */
  2391. +
  2392. +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  2393. +{
  2394. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2395. +
  2396. + yaffs2_checkpt_invalidate(dev);
  2397. +
  2398. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  2399. +
  2400. + if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  2401. + if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  2402. + T(YAFFS_TRACE_ALWAYS,
  2403. + (TSTR
  2404. + ("yaffs: Failed to mark bad and erase block %d"
  2405. + TENDSTR), flash_block));
  2406. + } else {
  2407. + struct yaffs_ext_tags tags;
  2408. + int chunk_id =
  2409. + flash_block * dev->param.chunks_per_block;
  2410. +
  2411. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  2412. +
  2413. + memset(buffer, 0xff, dev->data_bytes_per_chunk);
  2414. + yaffs_init_tags(&tags);
  2415. + tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  2416. + if (dev->param.write_chunk_tags_fn(dev, chunk_id -
  2417. + dev->chunk_offset,
  2418. + buffer,
  2419. + &tags) != YAFFS_OK)
  2420. + T(YAFFS_TRACE_ALWAYS,
  2421. + (TSTR
  2422. + ("yaffs: Failed to "
  2423. + TCONT("write bad block marker to block %d")
  2424. + TENDSTR), flash_block));
  2425. +
  2426. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2427. + }
  2428. + }
  2429. +
  2430. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  2431. + bi->gc_prioritise = 0;
  2432. + bi->needs_retiring = 0;
  2433. +
  2434. + dev->n_retired_blocks++;
  2435. +}
  2436. +
  2437. +/*
  2438. + * Functions for robustisizing TODO
  2439. + *
  2440. + */
  2441. +
  2442. +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  2443. + const u8 * data,
  2444. + const struct yaffs_ext_tags *tags)
  2445. +{
  2446. + dev = dev;
  2447. + nand_chunk = nand_chunk;
  2448. + data = data;
  2449. + tags = tags;
  2450. +}
  2451. +
  2452. +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  2453. + const struct yaffs_ext_tags *tags)
  2454. +{
  2455. + dev = dev;
  2456. + nand_chunk = nand_chunk;
  2457. + tags = tags;
  2458. +}
  2459. +
  2460. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  2461. + struct yaffs_block_info *bi)
  2462. +{
  2463. + if (!bi->gc_prioritise) {
  2464. + bi->gc_prioritise = 1;
  2465. + dev->has_pending_prioritised_gc = 1;
  2466. + bi->chunk_error_strikes++;
  2467. +
  2468. + if (bi->chunk_error_strikes > 3) {
  2469. + bi->needs_retiring = 1; /* Too many stikes, so retire this */
  2470. + T(YAFFS_TRACE_ALWAYS,
  2471. + (TSTR("yaffs: Block struck out" TENDSTR)));
  2472. +
  2473. + }
  2474. + }
  2475. +}
  2476. +
  2477. +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  2478. + int erased_ok)
  2479. +{
  2480. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  2481. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2482. +
  2483. + yaffs_handle_chunk_error(dev, bi);
  2484. +
  2485. + if (erased_ok) {
  2486. + /* Was an actual write failure, so mark the block for retirement */
  2487. + bi->needs_retiring = 1;
  2488. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  2489. + (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
  2490. + }
  2491. +
  2492. + /* Delete the chunk */
  2493. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  2494. + yaffs_skip_rest_of_block(dev);
  2495. +}
  2496. +
  2497. +/*---------------- Name handling functions ------------*/
  2498. +
  2499. +static u16 yaffs_calc_name_sum(const YCHAR * name)
  2500. +{
  2501. + u16 sum = 0;
  2502. + u16 i = 1;
  2503. +
  2504. + const YUCHAR *bname = (const YUCHAR *)name;
  2505. + if (bname) {
  2506. + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
  2507. +
  2508. + /* 0x1f mask is case insensitive */
  2509. + sum += ((*bname) & 0x1f) * i;
  2510. + i++;
  2511. + bname++;
  2512. + }
  2513. + }
  2514. + return sum;
  2515. +}
  2516. +
  2517. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  2518. +{
  2519. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  2520. + memset(obj->short_name, 0, sizeof(obj->short_name));
  2521. + if (name &&
  2522. + yaffs_strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  2523. + YAFFS_SHORT_NAME_LENGTH)
  2524. + yaffs_strcpy(obj->short_name, name);
  2525. + else
  2526. + obj->short_name[0] = _Y('\0');
  2527. +#endif
  2528. + obj->sum = yaffs_calc_name_sum(name);
  2529. +}
  2530. +
  2531. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  2532. + const struct yaffs_obj_hdr *oh)
  2533. +{
  2534. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  2535. + YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  2536. + memset(tmp_name, 0, sizeof(tmp_name));
  2537. + yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  2538. + YAFFS_MAX_NAME_LENGTH + 1);
  2539. + yaffs_set_obj_name(obj, tmp_name);
  2540. +#else
  2541. + yaffs_set_obj_name(obj, oh->name);
  2542. +#endif
  2543. +}
  2544. +
  2545. +/*-------------------- TNODES -------------------
  2546. +
  2547. + * List of spare tnodes
  2548. + * The list is hooked together using the first pointer
  2549. + * in the tnode.
  2550. + */
  2551. +
  2552. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  2553. +{
  2554. + struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  2555. + if (tn) {
  2556. + memset(tn, 0, dev->tnode_size);
  2557. + dev->n_tnodes++;
  2558. + }
  2559. +
  2560. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2561. +
  2562. + return tn;
  2563. +}
  2564. +
  2565. +/* FreeTnode frees up a tnode and puts it back on the free list */
  2566. +static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  2567. +{
  2568. + yaffs_free_raw_tnode(dev, tn);
  2569. + dev->n_tnodes--;
  2570. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2571. +}
  2572. +
  2573. +static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  2574. +{
  2575. + yaffs_deinit_raw_tnodes_and_objs(dev);
  2576. + dev->n_obj = 0;
  2577. + dev->n_tnodes = 0;
  2578. +}
  2579. +
  2580. +void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2581. + unsigned pos, unsigned val)
  2582. +{
  2583. + u32 *map = (u32 *) tn;
  2584. + u32 bit_in_map;
  2585. + u32 bit_in_word;
  2586. + u32 word_in_map;
  2587. + u32 mask;
  2588. +
  2589. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2590. + val >>= dev->chunk_grp_bits;
  2591. +
  2592. + bit_in_map = pos * dev->tnode_width;
  2593. + word_in_map = bit_in_map / 32;
  2594. + bit_in_word = bit_in_map & (32 - 1);
  2595. +
  2596. + mask = dev->tnode_mask << bit_in_word;
  2597. +
  2598. + map[word_in_map] &= ~mask;
  2599. + map[word_in_map] |= (mask & (val << bit_in_word));
  2600. +
  2601. + if (dev->tnode_width > (32 - bit_in_word)) {
  2602. + bit_in_word = (32 - bit_in_word);
  2603. + word_in_map++;;
  2604. + mask =
  2605. + dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
  2606. + map[word_in_map] &= ~mask;
  2607. + map[word_in_map] |= (mask & (val >> bit_in_word));
  2608. + }
  2609. +}
  2610. +
  2611. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2612. + unsigned pos)
  2613. +{
  2614. + u32 *map = (u32 *) tn;
  2615. + u32 bit_in_map;
  2616. + u32 bit_in_word;
  2617. + u32 word_in_map;
  2618. + u32 val;
  2619. +
  2620. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2621. +
  2622. + bit_in_map = pos * dev->tnode_width;
  2623. + word_in_map = bit_in_map / 32;
  2624. + bit_in_word = bit_in_map & (32 - 1);
  2625. +
  2626. + val = map[word_in_map] >> bit_in_word;
  2627. +
  2628. + if (dev->tnode_width > (32 - bit_in_word)) {
  2629. + bit_in_word = (32 - bit_in_word);
  2630. + word_in_map++;;
  2631. + val |= (map[word_in_map] << bit_in_word);
  2632. + }
  2633. +
  2634. + val &= dev->tnode_mask;
  2635. + val <<= dev->chunk_grp_bits;
  2636. +
  2637. + return val;
  2638. +}
  2639. +
  2640. +/* ------------------- End of individual tnode manipulation -----------------*/
  2641. +
  2642. +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  2643. + * The look up tree is represented by the top tnode and the number of top_level
  2644. + * in the tree. 0 means only the level 0 tnode is in the tree.
  2645. + */
  2646. +
  2647. +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  2648. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  2649. + struct yaffs_file_var *file_struct,
  2650. + u32 chunk_id)
  2651. +{
  2652. + struct yaffs_tnode *tn = file_struct->top;
  2653. + u32 i;
  2654. + int required_depth;
  2655. + int level = file_struct->top_level;
  2656. +
  2657. + dev = dev;
  2658. +
  2659. + /* Check sane level and chunk Id */
  2660. + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  2661. + return NULL;
  2662. +
  2663. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2664. + return NULL;
  2665. +
  2666. + /* First check we're tall enough (ie enough top_level) */
  2667. +
  2668. + i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2669. + required_depth = 0;
  2670. + while (i) {
  2671. + i >>= YAFFS_TNODES_INTERNAL_BITS;
  2672. + required_depth++;
  2673. + }
  2674. +
  2675. + if (required_depth > file_struct->top_level)
  2676. + return NULL; /* Not tall enough, so we can't find it */
  2677. +
  2678. + /* Traverse down to level 0 */
  2679. + while (level > 0 && tn) {
  2680. + tn = tn->internal[(chunk_id >>
  2681. + (YAFFS_TNODES_LEVEL0_BITS +
  2682. + (level - 1) *
  2683. + YAFFS_TNODES_INTERNAL_BITS)) &
  2684. + YAFFS_TNODES_INTERNAL_MASK];
  2685. + level--;
  2686. + }
  2687. +
  2688. + return tn;
  2689. +}
  2690. +
  2691. +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  2692. + * This happens in two steps:
  2693. + * 1. If the tree isn't tall enough, then make it taller.
  2694. + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  2695. + *
  2696. + * Used when modifying the tree.
  2697. + *
  2698. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  2699. + * be plugged into the ttree.
  2700. + */
  2701. +
  2702. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  2703. + struct yaffs_file_var *file_struct,
  2704. + u32 chunk_id,
  2705. + struct yaffs_tnode *passed_tn)
  2706. +{
  2707. + int required_depth;
  2708. + int i;
  2709. + int l;
  2710. + struct yaffs_tnode *tn;
  2711. +
  2712. + u32 x;
  2713. +
  2714. + /* Check sane level and page Id */
  2715. + if (file_struct->top_level < 0
  2716. + || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  2717. + return NULL;
  2718. +
  2719. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2720. + return NULL;
  2721. +
  2722. + /* First check we're tall enough (ie enough top_level) */
  2723. +
  2724. + x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2725. + required_depth = 0;
  2726. + while (x) {
  2727. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  2728. + required_depth++;
  2729. + }
  2730. +
  2731. + if (required_depth > file_struct->top_level) {
  2732. + /* Not tall enough, gotta make the tree taller */
  2733. + for (i = file_struct->top_level; i < required_depth; i++) {
  2734. +
  2735. + tn = yaffs_get_tnode(dev);
  2736. +
  2737. + if (tn) {
  2738. + tn->internal[0] = file_struct->top;
  2739. + file_struct->top = tn;
  2740. + file_struct->top_level++;
  2741. + } else {
  2742. + T(YAFFS_TRACE_ERROR,
  2743. + (TSTR("yaffs: no more tnodes" TENDSTR)));
  2744. + return NULL;
  2745. + }
  2746. + }
  2747. + }
  2748. +
  2749. + /* Traverse down to level 0, adding anything we need */
  2750. +
  2751. + l = file_struct->top_level;
  2752. + tn = file_struct->top;
  2753. +
  2754. + if (l > 0) {
  2755. + while (l > 0 && tn) {
  2756. + x = (chunk_id >>
  2757. + (YAFFS_TNODES_LEVEL0_BITS +
  2758. + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  2759. + YAFFS_TNODES_INTERNAL_MASK;
  2760. +
  2761. + if ((l > 1) && !tn->internal[x]) {
  2762. + /* Add missing non-level-zero tnode */
  2763. + tn->internal[x] = yaffs_get_tnode(dev);
  2764. + if (!tn->internal[x])
  2765. + return NULL;
  2766. + } else if (l == 1) {
  2767. + /* Looking from level 1 at level 0 */
  2768. + if (passed_tn) {
  2769. + /* If we already have one, then release it. */
  2770. + if (tn->internal[x])
  2771. + yaffs_free_tnode(dev,
  2772. + tn->
  2773. + internal[x]);
  2774. + tn->internal[x] = passed_tn;
  2775. +
  2776. + } else if (!tn->internal[x]) {
  2777. + /* Don't have one, none passed in */
  2778. + tn->internal[x] = yaffs_get_tnode(dev);
  2779. + if (!tn->internal[x])
  2780. + return NULL;
  2781. + }
  2782. + }
  2783. +
  2784. + tn = tn->internal[x];
  2785. + l--;
  2786. + }
  2787. + } else {
  2788. + /* top is level 0 */
  2789. + if (passed_tn) {
  2790. + memcpy(tn, passed_tn,
  2791. + (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  2792. + yaffs_free_tnode(dev, passed_tn);
  2793. + }
  2794. + }
  2795. +
  2796. + return tn;
  2797. +}
  2798. +
  2799. +static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  2800. + struct yaffs_ext_tags *tags, int obj_id,
  2801. + int inode_chunk)
  2802. +{
  2803. + int j;
  2804. +
  2805. + for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  2806. + if (yaffs_check_chunk_bit
  2807. + (dev, the_chunk / dev->param.chunks_per_block,
  2808. + the_chunk % dev->param.chunks_per_block)) {
  2809. +
  2810. + if (dev->chunk_grp_size == 1)
  2811. + return the_chunk;
  2812. + else {
  2813. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  2814. + tags);
  2815. + if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
  2816. + /* found it; */
  2817. + return the_chunk;
  2818. + }
  2819. + }
  2820. + }
  2821. + the_chunk++;
  2822. + }
  2823. + return -1;
  2824. +}
  2825. +
  2826. +static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  2827. +{
  2828. + struct yaffs_block_info *the_block;
  2829. + unsigned block_no;
  2830. +
  2831. + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
  2832. +
  2833. + block_no = chunk / dev->param.chunks_per_block;
  2834. + the_block = yaffs_get_block_info(dev, block_no);
  2835. + if (the_block) {
  2836. + the_block->soft_del_pages++;
  2837. + dev->n_free_chunks++;
  2838. + yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  2839. + }
  2840. +}
  2841. +
  2842. +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  2843. + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  2844. + * of the tnode.
  2845. + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  2846. + */
  2847. +
  2848. +static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  2849. + u32 level, int chunk_offset)
  2850. +{
  2851. + int i;
  2852. + int the_chunk;
  2853. + int all_done = 1;
  2854. + struct yaffs_dev *dev = in->my_dev;
  2855. +
  2856. + if (tn) {
  2857. + if (level > 0) {
  2858. +
  2859. + for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
  2860. + i--) {
  2861. + if (tn->internal[i]) {
  2862. + all_done =
  2863. + yaffs_soft_del_worker(in,
  2864. + tn->internal
  2865. + [i],
  2866. + level - 1,
  2867. + (chunk_offset
  2868. + <<
  2869. + YAFFS_TNODES_INTERNAL_BITS)
  2870. + + i);
  2871. + if (all_done) {
  2872. + yaffs_free_tnode(dev,
  2873. + tn->internal
  2874. + [i]);
  2875. + tn->internal[i] = NULL;
  2876. + } else {
  2877. + /* Hoosterman... how could this happen? */
  2878. + }
  2879. + }
  2880. + }
  2881. + return (all_done) ? 1 : 0;
  2882. + } else if (level == 0) {
  2883. +
  2884. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  2885. + the_chunk = yaffs_get_group_base(dev, tn, i);
  2886. + if (the_chunk) {
  2887. + /* Note this does not find the real chunk, only the chunk group.
  2888. + * We make an assumption that a chunk group is not larger than
  2889. + * a block.
  2890. + */
  2891. + yaffs_soft_del_chunk(dev, the_chunk);
  2892. + yaffs_load_tnode_0(dev, tn, i, 0);
  2893. + }
  2894. +
  2895. + }
  2896. + return 1;
  2897. +
  2898. + }
  2899. +
  2900. + }
  2901. +
  2902. + return 1;
  2903. +
  2904. +}
  2905. +
  2906. +static void yaffs_soft_del_file(struct yaffs_obj *obj)
  2907. +{
  2908. + if (obj->deleted &&
  2909. + obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
  2910. + if (obj->n_data_chunks <= 0) {
  2911. + /* Empty file with no duplicate object headers, just delete it immediately */
  2912. + yaffs_free_tnode(obj->my_dev,
  2913. + obj->variant.file_variant.top);
  2914. + obj->variant.file_variant.top = NULL;
  2915. + T(YAFFS_TRACE_TRACING,
  2916. + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
  2917. + obj->obj_id));
  2918. + yaffs_generic_obj_del(obj);
  2919. + } else {
  2920. + yaffs_soft_del_worker(obj,
  2921. + obj->variant.file_variant.top,
  2922. + obj->variant.
  2923. + file_variant.top_level, 0);
  2924. + obj->soft_del = 1;
  2925. + }
  2926. + }
  2927. +}
  2928. +
  2929. +/* Pruning removes any part of the file structure tree that is beyond the
  2930. + * bounds of the file (ie that does not point to chunks).
  2931. + *
  2932. + * A file should only get pruned when its size is reduced.
  2933. + *
  2934. + * Before pruning, the chunks must be pulled from the tree and the
  2935. + * level 0 tnode entries must be zeroed out.
  2936. + * Could also use this for file deletion, but that's probably better handled
  2937. + * by a special case.
  2938. + *
  2939. + * This function is recursive. For levels > 0 the function is called again on
  2940. + * any sub-tree. For level == 0 we just check if the sub-tree has data.
  2941. + * If there is no data in a subtree then it is pruned.
  2942. + */
  2943. +
  2944. +static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  2945. + struct yaffs_tnode *tn, u32 level,
  2946. + int del0)
  2947. +{
  2948. + int i;
  2949. + int has_data;
  2950. +
  2951. + if (tn) {
  2952. + has_data = 0;
  2953. +
  2954. + if (level > 0) {
  2955. + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  2956. + if (tn->internal[i]) {
  2957. + tn->internal[i] =
  2958. + yaffs_prune_worker(dev,
  2959. + tn->internal[i],
  2960. + level - 1,
  2961. + (i ==
  2962. + 0) ? del0 : 1);
  2963. + }
  2964. +
  2965. + if (tn->internal[i])
  2966. + has_data++;
  2967. + }
  2968. + } else {
  2969. + int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  2970. + u32 *map = (u32 *) tn;
  2971. +
  2972. + for (i = 0; !has_data && i < tnode_size_u32; i++) {
  2973. + if (map[i])
  2974. + has_data++;
  2975. + }
  2976. + }
  2977. +
  2978. + if (has_data == 0 && del0) {
  2979. + /* Free and return NULL */
  2980. +
  2981. + yaffs_free_tnode(dev, tn);
  2982. + tn = NULL;
  2983. + }
  2984. +
  2985. + }
  2986. +
  2987. + return tn;
  2988. +
  2989. +}
  2990. +
  2991. +static int yaffs_prune_tree(struct yaffs_dev *dev,
  2992. + struct yaffs_file_var *file_struct)
  2993. +{
  2994. + int i;
  2995. + int has_data;
  2996. + int done = 0;
  2997. + struct yaffs_tnode *tn;
  2998. +
  2999. + if (file_struct->top_level > 0) {
  3000. + file_struct->top =
  3001. + yaffs_prune_worker(dev, file_struct->top,
  3002. + file_struct->top_level, 0);
  3003. +
  3004. + /* Now we have a tree with all the non-zero branches NULL but the height
  3005. + * is the same as it was.
  3006. + * Let's see if we can trim internal tnodes to shorten the tree.
  3007. + * We can do this if only the 0th element in the tnode is in use
  3008. + * (ie all the non-zero are NULL)
  3009. + */
  3010. +
  3011. + while (file_struct->top_level && !done) {
  3012. + tn = file_struct->top;
  3013. +
  3014. + has_data = 0;
  3015. + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  3016. + if (tn->internal[i])
  3017. + has_data++;
  3018. + }
  3019. +
  3020. + if (!has_data) {
  3021. + file_struct->top = tn->internal[0];
  3022. + file_struct->top_level--;
  3023. + yaffs_free_tnode(dev, tn);
  3024. + } else {
  3025. + done = 1;
  3026. + }
  3027. + }
  3028. + }
  3029. +
  3030. + return YAFFS_OK;
  3031. +}
  3032. +
  3033. +/*-------------------- End of File Structure functions.-------------------*/
  3034. +
  3035. +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  3036. +static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  3037. +{
  3038. + struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  3039. +
  3040. + if (obj) {
  3041. + dev->n_obj++;
  3042. +
  3043. + /* Now sweeten it up... */
  3044. +
  3045. + memset(obj, 0, sizeof(struct yaffs_obj));
  3046. + obj->being_created = 1;
  3047. +
  3048. + obj->my_dev = dev;
  3049. + obj->hdr_chunk = 0;
  3050. + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  3051. + INIT_LIST_HEAD(&(obj->hard_links));
  3052. + INIT_LIST_HEAD(&(obj->hash_link));
  3053. + INIT_LIST_HEAD(&obj->siblings);
  3054. +
  3055. + /* Now make the directory sane */
  3056. + if (dev->root_dir) {
  3057. + obj->parent = dev->root_dir;
  3058. + list_add(&(obj->siblings),
  3059. + &dev->root_dir->variant.dir_variant.children);
  3060. + }
  3061. +
  3062. + /* Add it to the lost and found directory.
  3063. + * NB Can't put root or lost-n-found in lost-n-found so
  3064. + * check if lost-n-found exists first
  3065. + */
  3066. + if (dev->lost_n_found)
  3067. + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  3068. +
  3069. + obj->being_created = 0;
  3070. + }
  3071. +
  3072. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3073. +
  3074. + return obj;
  3075. +}
  3076. +
  3077. +static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  3078. + int number, u32 mode)
  3079. +{
  3080. +
  3081. + struct yaffs_obj *obj =
  3082. + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  3083. + if (obj) {
  3084. + obj->fake = 1; /* it is fake so it might have no NAND presence... */
  3085. + obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
  3086. + obj->unlink_allowed = 0; /* ... or unlink it */
  3087. + obj->deleted = 0;
  3088. + obj->unlinked = 0;
  3089. + obj->yst_mode = mode;
  3090. + obj->my_dev = dev;
  3091. + obj->hdr_chunk = 0; /* Not a valid chunk. */
  3092. + }
  3093. +
  3094. + return obj;
  3095. +
  3096. +}
  3097. +
  3098. +static void yaffs_unhash_obj(struct yaffs_obj *obj)
  3099. +{
  3100. + int bucket;
  3101. + struct yaffs_dev *dev = obj->my_dev;
  3102. +
  3103. + /* If it is still linked into the bucket list, free from the list */
  3104. + if (!list_empty(&obj->hash_link)) {
  3105. + list_del_init(&obj->hash_link);
  3106. + bucket = yaffs_hash_fn(obj->obj_id);
  3107. + dev->obj_bucket[bucket].count--;
  3108. + }
  3109. +}
  3110. +
  3111. +/* FreeObject frees up a Object and puts it back on the free list */
  3112. +static void yaffs_free_obj(struct yaffs_obj *obj)
  3113. +{
  3114. + struct yaffs_dev *dev = obj->my_dev;
  3115. +
  3116. + T(YAFFS_TRACE_OS,
  3117. + (TSTR("FreeObject %p inode %p" TENDSTR), obj, obj->my_inode));
  3118. +
  3119. + if (!obj)
  3120. + YBUG();
  3121. + if (obj->parent)
  3122. + YBUG();
  3123. + if (!list_empty(&obj->siblings))
  3124. + YBUG();
  3125. +
  3126. + if (obj->my_inode) {
  3127. + /* We're still hooked up to a cached inode.
  3128. + * Don't delete now, but mark for later deletion
  3129. + */
  3130. + obj->defered_free = 1;
  3131. + return;
  3132. + }
  3133. +
  3134. + yaffs_unhash_obj(obj);
  3135. +
  3136. + yaffs_free_raw_obj(dev, obj);
  3137. + dev->n_obj--;
  3138. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3139. +}
  3140. +
  3141. +void yaffs_handle_defered_free(struct yaffs_obj *obj)
  3142. +{
  3143. + if (obj->defered_free)
  3144. + yaffs_free_obj(obj);
  3145. +}
  3146. +
  3147. +static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  3148. +{
  3149. + int i;
  3150. +
  3151. + dev->n_obj = 0;
  3152. + dev->n_tnodes = 0;
  3153. +
  3154. + yaffs_init_raw_tnodes_and_objs(dev);
  3155. +
  3156. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  3157. + INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  3158. + dev->obj_bucket[i].count = 0;
  3159. + }
  3160. +}
  3161. +
  3162. +static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  3163. +{
  3164. + int i;
  3165. + int l = 999;
  3166. + int lowest = 999999;
  3167. +
  3168. + /* Search for the shortest list or one that
  3169. + * isn't too long.
  3170. + */
  3171. +
  3172. + for (i = 0; i < 10 && lowest > 4; i++) {
  3173. + dev->bucket_finder++;
  3174. + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  3175. + if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  3176. + lowest = dev->obj_bucket[dev->bucket_finder].count;
  3177. + l = dev->bucket_finder;
  3178. + }
  3179. +
  3180. + }
  3181. +
  3182. + return l;
  3183. +}
  3184. +
  3185. +static int yaffs_new_obj_id(struct yaffs_dev *dev)
  3186. +{
  3187. + int bucket = yaffs_find_nice_bucket(dev);
  3188. +
  3189. + /* Now find an object value that has not already been taken
  3190. + * by scanning the list.
  3191. + */
  3192. +
  3193. + int found = 0;
  3194. + struct list_head *i;
  3195. +
  3196. + u32 n = (u32) bucket;
  3197. +
  3198. + /* yaffs_check_obj_hash_sane(); */
  3199. +
  3200. + while (!found) {
  3201. + found = 1;
  3202. + n += YAFFS_NOBJECT_BUCKETS;
  3203. + if (1 || dev->obj_bucket[bucket].count > 0) {
  3204. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3205. + /* If there is already one in the list */
  3206. + if (i && list_entry(i, struct yaffs_obj,
  3207. + hash_link)->obj_id == n) {
  3208. + found = 0;
  3209. + }
  3210. + }
  3211. + }
  3212. + }
  3213. +
  3214. + return n;
  3215. +}
  3216. +
  3217. +static void yaffs_hash_obj(struct yaffs_obj *in)
  3218. +{
  3219. + int bucket = yaffs_hash_fn(in->obj_id);
  3220. + struct yaffs_dev *dev = in->my_dev;
  3221. +
  3222. + list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  3223. + dev->obj_bucket[bucket].count++;
  3224. +}
  3225. +
  3226. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  3227. +{
  3228. + int bucket = yaffs_hash_fn(number);
  3229. + struct list_head *i;
  3230. + struct yaffs_obj *in;
  3231. +
  3232. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3233. + /* Look if it is in the list */
  3234. + if (i) {
  3235. + in = list_entry(i, struct yaffs_obj, hash_link);
  3236. + if (in->obj_id == number) {
  3237. +
  3238. + /* Don't tell the VFS about this one if it is defered free */
  3239. + if (in->defered_free)
  3240. + return NULL;
  3241. +
  3242. + return in;
  3243. + }
  3244. + }
  3245. + }
  3246. +
  3247. + return NULL;
  3248. +}
  3249. +
  3250. +struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  3251. + enum yaffs_obj_type type)
  3252. +{
  3253. + struct yaffs_obj *the_obj = NULL;
  3254. + struct yaffs_tnode *tn = NULL;
  3255. +
  3256. + if (number < 0)
  3257. + number = yaffs_new_obj_id(dev);
  3258. +
  3259. + if (type == YAFFS_OBJECT_TYPE_FILE) {
  3260. + tn = yaffs_get_tnode(dev);
  3261. + if (!tn)
  3262. + return NULL;
  3263. + }
  3264. +
  3265. + the_obj = yaffs_alloc_empty_obj(dev);
  3266. + if (!the_obj) {
  3267. + if (tn)
  3268. + yaffs_free_tnode(dev, tn);
  3269. + return NULL;
  3270. + }
  3271. +
  3272. + if (the_obj) {
  3273. + the_obj->fake = 0;
  3274. + the_obj->rename_allowed = 1;
  3275. + the_obj->unlink_allowed = 1;
  3276. + the_obj->obj_id = number;
  3277. + yaffs_hash_obj(the_obj);
  3278. + the_obj->variant_type = type;
  3279. + yaffs_load_current_time(the_obj, 1, 1);
  3280. +
  3281. + switch (type) {
  3282. + case YAFFS_OBJECT_TYPE_FILE:
  3283. + the_obj->variant.file_variant.file_size = 0;
  3284. + the_obj->variant.file_variant.scanned_size = 0;
  3285. + the_obj->variant.file_variant.shrink_size = ~0; /* max */
  3286. + the_obj->variant.file_variant.top_level = 0;
  3287. + the_obj->variant.file_variant.top = tn;
  3288. + break;
  3289. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  3290. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  3291. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  3292. + break;
  3293. + case YAFFS_OBJECT_TYPE_SYMLINK:
  3294. + case YAFFS_OBJECT_TYPE_HARDLINK:
  3295. + case YAFFS_OBJECT_TYPE_SPECIAL:
  3296. + /* No action required */
  3297. + break;
  3298. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  3299. + /* todo this should not happen */
  3300. + break;
  3301. + }
  3302. + }
  3303. +
  3304. + return the_obj;
  3305. +}
  3306. +
  3307. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  3308. + int number,
  3309. + enum yaffs_obj_type type)
  3310. +{
  3311. + struct yaffs_obj *the_obj = NULL;
  3312. +
  3313. + if (number > 0)
  3314. + the_obj = yaffs_find_by_number(dev, number);
  3315. +
  3316. + if (!the_obj)
  3317. + the_obj = yaffs_new_obj(dev, number, type);
  3318. +
  3319. + return the_obj;
  3320. +
  3321. +}
  3322. +
  3323. +YCHAR *yaffs_clone_str(const YCHAR * str)
  3324. +{
  3325. + YCHAR *new_str = NULL;
  3326. + int len;
  3327. +
  3328. + if (!str)
  3329. + str = _Y("");
  3330. +
  3331. + len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  3332. + new_str = YMALLOC((len + 1) * sizeof(YCHAR));
  3333. + if (new_str) {
  3334. + yaffs_strncpy(new_str, str, len);
  3335. + new_str[len] = 0;
  3336. + }
  3337. + return new_str;
  3338. +
  3339. +}
  3340. +
  3341. +/*
  3342. + * Mknod (create) a new object.
  3343. + * equiv_obj only has meaning for a hard link;
  3344. + * alias_str only has meaning for a symlink.
  3345. + * rdev only has meaning for devices (a subset of special objects)
  3346. + */
  3347. +
  3348. +static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  3349. + struct yaffs_obj *parent,
  3350. + const YCHAR * name,
  3351. + u32 mode,
  3352. + u32 uid,
  3353. + u32 gid,
  3354. + struct yaffs_obj *equiv_obj,
  3355. + const YCHAR * alias_str, u32 rdev)
  3356. +{
  3357. + struct yaffs_obj *in;
  3358. + YCHAR *str = NULL;
  3359. +
  3360. + struct yaffs_dev *dev = parent->my_dev;
  3361. +
  3362. + /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
  3363. + if (yaffs_find_by_name(parent, name))
  3364. + return NULL;
  3365. +
  3366. + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  3367. + str = yaffs_clone_str(alias_str);
  3368. + if (!str)
  3369. + return NULL;
  3370. + }
  3371. +
  3372. + in = yaffs_new_obj(dev, -1, type);
  3373. +
  3374. + if (!in) {
  3375. + if (str)
  3376. + YFREE(str);
  3377. + return NULL;
  3378. + }
  3379. +
  3380. + if (in) {
  3381. + in->hdr_chunk = 0;
  3382. + in->valid = 1;
  3383. + in->variant_type = type;
  3384. +
  3385. + in->yst_mode = mode;
  3386. +
  3387. + yaffs_attribs_init(in, gid, uid, rdev);
  3388. +
  3389. + in->n_data_chunks = 0;
  3390. +
  3391. + yaffs_set_obj_name(in, name);
  3392. + in->dirty = 1;
  3393. +
  3394. + yaffs_add_obj_to_dir(parent, in);
  3395. +
  3396. + in->my_dev = parent->my_dev;
  3397. +
  3398. + switch (type) {
  3399. + case YAFFS_OBJECT_TYPE_SYMLINK:
  3400. + in->variant.symlink_variant.alias = str;
  3401. + break;
  3402. + case YAFFS_OBJECT_TYPE_HARDLINK:
  3403. + in->variant.hardlink_variant.equiv_obj = equiv_obj;
  3404. + in->variant.hardlink_variant.equiv_id =
  3405. + equiv_obj->obj_id;
  3406. + list_add(&in->hard_links, &equiv_obj->hard_links);
  3407. + break;
  3408. + case YAFFS_OBJECT_TYPE_FILE:
  3409. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  3410. + case YAFFS_OBJECT_TYPE_SPECIAL:
  3411. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  3412. + /* do nothing */
  3413. + break;
  3414. + }
  3415. +
  3416. + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  3417. + /* Could not create the object header, fail the creation */
  3418. + yaffs_del_obj(in);
  3419. + in = NULL;
  3420. + }
  3421. +
  3422. + yaffs_update_parent(parent);
  3423. + }
  3424. +
  3425. + return in;
  3426. +}
  3427. +
  3428. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  3429. + const YCHAR * name, u32 mode, u32 uid,
  3430. + u32 gid)
  3431. +{
  3432. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  3433. + uid, gid, NULL, NULL, 0);
  3434. +}
  3435. +
  3436. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  3437. + u32 mode, u32 uid, u32 gid)
  3438. +{
  3439. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  3440. + mode, uid, gid, NULL, NULL, 0);
  3441. +}
  3442. +
  3443. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  3444. + const YCHAR * name, u32 mode, u32 uid,
  3445. + u32 gid, u32 rdev)
  3446. +{
  3447. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  3448. + uid, gid, NULL, NULL, rdev);
  3449. +}
  3450. +
  3451. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  3452. + const YCHAR * name, u32 mode, u32 uid,
  3453. + u32 gid, const YCHAR * alias)
  3454. +{
  3455. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  3456. + uid, gid, NULL, alias, 0);
  3457. +}
  3458. +
  3459. +/* yaffs_link_obj returns the object id of the equivalent object.*/
  3460. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  3461. + struct yaffs_obj *equiv_obj)
  3462. +{
  3463. + /* Get the real object in case we were fed a hard link as an equivalent object */
  3464. + equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  3465. +
  3466. + if (yaffs_create_obj
  3467. + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  3468. + equiv_obj, NULL, 0)) {
  3469. + return equiv_obj;
  3470. + } else {
  3471. + return NULL;
  3472. + }
  3473. +
  3474. +}
  3475. +
  3476. +static int yaffs_change_obj_name(struct yaffs_obj *obj,
  3477. + struct yaffs_obj *new_dir,
  3478. + const YCHAR * new_name, int force, int shadows)
  3479. +{
  3480. + int unlink_op;
  3481. + int del_op;
  3482. +
  3483. + struct yaffs_obj *existing_target;
  3484. +
  3485. + if (new_dir == NULL)
  3486. + new_dir = obj->parent; /* use the old directory */
  3487. +
  3488. + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3489. + T(YAFFS_TRACE_ALWAYS,
  3490. + (TSTR
  3491. + ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
  3492. + TENDSTR)));
  3493. + YBUG();
  3494. + }
  3495. +
  3496. + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  3497. + if (obj->my_dev->param.is_yaffs2)
  3498. + unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  3499. + else
  3500. + unlink_op = (new_dir == obj->my_dev->unlinked_dir
  3501. + && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
  3502. +
  3503. + del_op = (new_dir == obj->my_dev->del_dir);
  3504. +
  3505. + existing_target = yaffs_find_by_name(new_dir, new_name);
  3506. +
  3507. + /* If the object is a file going into the unlinked directory,
  3508. + * then it is OK to just stuff it in since duplicate names are allowed.
  3509. + * else only proceed if the new name does not exist and if we're putting
  3510. + * it into a directory.
  3511. + */
  3512. + if ((unlink_op ||
  3513. + del_op ||
  3514. + force ||
  3515. + (shadows > 0) ||
  3516. + !existing_target) &&
  3517. + new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  3518. + yaffs_set_obj_name(obj, new_name);
  3519. + obj->dirty = 1;
  3520. +
  3521. + yaffs_add_obj_to_dir(new_dir, obj);
  3522. +
  3523. + if (unlink_op)
  3524. + obj->unlinked = 1;
  3525. +
  3526. + /* If it is a deletion then we mark it as a shrink for gc purposes. */
  3527. + if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
  3528. + 0)
  3529. + return YAFFS_OK;
  3530. + }
  3531. +
  3532. + return YAFFS_FAIL;
  3533. +}
  3534. +
  3535. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  3536. + struct yaffs_obj *new_dir, const YCHAR * new_name)
  3537. +{
  3538. + struct yaffs_obj *obj = NULL;
  3539. + struct yaffs_obj *existing_target = NULL;
  3540. + int force = 0;
  3541. + int result;
  3542. + struct yaffs_dev *dev;
  3543. +
  3544. + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3545. + YBUG();
  3546. + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3547. + YBUG();
  3548. +
  3549. + dev = old_dir->my_dev;
  3550. +
  3551. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  3552. + /* Special case for case insemsitive systems.
  3553. + * While look-up is case insensitive, the name isn't.
  3554. + * Therefore we might want to change x.txt to X.txt
  3555. + */
  3556. + if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
  3557. + force = 1;
  3558. +#endif
  3559. +
  3560. + if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
  3561. + YAFFS_MAX_NAME_LENGTH)
  3562. + /* ENAMETOOLONG */
  3563. + return YAFFS_FAIL;
  3564. +
  3565. + obj = yaffs_find_by_name(old_dir, old_name);
  3566. +
  3567. + if (obj && obj->rename_allowed) {
  3568. +
  3569. + /* Now do the handling for an existing target, if there is one */
  3570. +
  3571. + existing_target = yaffs_find_by_name(new_dir, new_name);
  3572. + if (existing_target &&
  3573. + existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY
  3574. + && !list_empty(&existing_target->variant.dir_variant.
  3575. + children)) {
  3576. + /* There is a target that is a non-empty directory, so we fail */
  3577. + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
  3578. + } else if (existing_target && existing_target != obj) {
  3579. + /* Nuke the target first, using shadowing,
  3580. + * but only if it isn't the same object.
  3581. + *
  3582. + * Note we must disable gc otherwise it can mess up the shadowing.
  3583. + *
  3584. + */
  3585. + dev->gc_disable = 1;
  3586. + yaffs_change_obj_name(obj, new_dir, new_name, force,
  3587. + existing_target->obj_id);
  3588. + existing_target->is_shadowed = 1;
  3589. + yaffs_unlink_obj(existing_target);
  3590. + dev->gc_disable = 0;
  3591. + }
  3592. +
  3593. + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
  3594. +
  3595. + yaffs_update_parent(old_dir);
  3596. + if (new_dir != old_dir)
  3597. + yaffs_update_parent(new_dir);
  3598. +
  3599. + return result;
  3600. + }
  3601. + return YAFFS_FAIL;
  3602. +}
  3603. +
  3604. +/*------------------------- Block Management and Page Allocation ----------------*/
  3605. +
  3606. +static int yaffs_init_blocks(struct yaffs_dev *dev)
  3607. +{
  3608. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  3609. +
  3610. + dev->block_info = NULL;
  3611. + dev->chunk_bits = NULL;
  3612. +
  3613. + dev->alloc_block = -1; /* force it to get a new one */
  3614. +
  3615. + /* If the first allocation strategy fails, thry the alternate one */
  3616. + dev->block_info = YMALLOC(n_blocks * sizeof(struct yaffs_block_info));
  3617. + if (!dev->block_info) {
  3618. + dev->block_info =
  3619. + YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_info));
  3620. + dev->block_info_alt = 1;
  3621. + } else {
  3622. + dev->block_info_alt = 0;
  3623. + }
  3624. +
  3625. + if (dev->block_info) {
  3626. + /* Set up dynamic blockinfo stuff. */
  3627. + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
  3628. + dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks);
  3629. + if (!dev->chunk_bits) {
  3630. + dev->chunk_bits =
  3631. + YMALLOC_ALT(dev->chunk_bit_stride * n_blocks);
  3632. + dev->chunk_bits_alt = 1;
  3633. + } else {
  3634. + dev->chunk_bits_alt = 0;
  3635. + }
  3636. + }
  3637. +
  3638. + if (dev->block_info && dev->chunk_bits) {
  3639. + memset(dev->block_info, 0,
  3640. + n_blocks * sizeof(struct yaffs_block_info));
  3641. + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  3642. + return YAFFS_OK;
  3643. + }
  3644. +
  3645. + return YAFFS_FAIL;
  3646. +}
  3647. +
  3648. +static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  3649. +{
  3650. + if (dev->block_info_alt && dev->block_info)
  3651. + YFREE_ALT(dev->block_info);
  3652. + else if (dev->block_info)
  3653. + YFREE(dev->block_info);
  3654. +
  3655. + dev->block_info_alt = 0;
  3656. +
  3657. + dev->block_info = NULL;
  3658. +
  3659. + if (dev->chunk_bits_alt && dev->chunk_bits)
  3660. + YFREE_ALT(dev->chunk_bits);
  3661. + else if (dev->chunk_bits)
  3662. + YFREE(dev->chunk_bits);
  3663. + dev->chunk_bits_alt = 0;
  3664. + dev->chunk_bits = NULL;
  3665. +}
  3666. +
  3667. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  3668. +{
  3669. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  3670. +
  3671. + int erased_ok = 0;
  3672. +
  3673. + /* If the block is still healthy erase it and mark as clean.
  3674. + * If the block has had a data failure, then retire it.
  3675. + */
  3676. +
  3677. + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  3678. + (TSTR("yaffs_block_became_dirty block %d state %d %s" TENDSTR),
  3679. + block_no, bi->block_state,
  3680. + (bi->needs_retiring) ? "needs retiring" : ""));
  3681. +
  3682. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  3683. +
  3684. + bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  3685. +
  3686. + /* If this is the block being garbage collected then stop gc'ing this block */
  3687. + if (block_no == dev->gc_block)
  3688. + dev->gc_block = 0;
  3689. +
  3690. + /* If this block is currently the best candidate for gc then drop as a candidate */
  3691. + if (block_no == dev->gc_dirtiest) {
  3692. + dev->gc_dirtiest = 0;
  3693. + dev->gc_pages_in_use = 0;
  3694. + }
  3695. +
  3696. + if (!bi->needs_retiring) {
  3697. + yaffs2_checkpt_invalidate(dev);
  3698. + erased_ok = yaffs_erase_block(dev, block_no);
  3699. + if (!erased_ok) {
  3700. + dev->n_erase_failures++;
  3701. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  3702. + (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
  3703. + }
  3704. + }
  3705. +
  3706. + if (erased_ok &&
  3707. + ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
  3708. + || !yaffs_skip_verification(dev))) {
  3709. + int i;
  3710. + for (i = 0; i < dev->param.chunks_per_block; i++) {
  3711. + if (!yaffs_check_chunk_erased
  3712. + (dev, block_no * dev->param.chunks_per_block + i)) {
  3713. + T(YAFFS_TRACE_ERROR,
  3714. + (TSTR
  3715. + (">>Block %d erasure supposedly OK, but chunk %d not erased"
  3716. + TENDSTR), block_no, i));
  3717. + }
  3718. + }
  3719. + }
  3720. +
  3721. + if (erased_ok) {
  3722. + /* Clean it up... */
  3723. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  3724. + bi->seq_number = 0;
  3725. + dev->n_erased_blocks++;
  3726. + bi->pages_in_use = 0;
  3727. + bi->soft_del_pages = 0;
  3728. + bi->has_shrink_hdr = 0;
  3729. + bi->skip_erased_check = 1; /* This is clean, so no need to check */
  3730. + bi->gc_prioritise = 0;
  3731. + yaffs_clear_chunk_bits(dev, block_no);
  3732. +
  3733. + T(YAFFS_TRACE_ERASE,
  3734. + (TSTR("Erased block %d" TENDSTR), block_no));
  3735. + } else {
  3736. + dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
  3737. +
  3738. + yaffs_retire_block(dev, block_no);
  3739. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  3740. + (TSTR("**>> Block %d retired" TENDSTR), block_no));
  3741. + }
  3742. +}
  3743. +
  3744. +static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  3745. +{
  3746. + int i;
  3747. +
  3748. + struct yaffs_block_info *bi;
  3749. +
  3750. + if (dev->n_erased_blocks < 1) {
  3751. + /* Hoosterman we've got a problem.
  3752. + * Can't get space to gc
  3753. + */
  3754. + T(YAFFS_TRACE_ERROR,
  3755. + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
  3756. +
  3757. + return -1;
  3758. + }
  3759. +
  3760. + /* Find an empty block. */
  3761. +
  3762. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  3763. + dev->alloc_block_finder++;
  3764. + if (dev->alloc_block_finder < dev->internal_start_block
  3765. + || dev->alloc_block_finder > dev->internal_end_block) {
  3766. + dev->alloc_block_finder = dev->internal_start_block;
  3767. + }
  3768. +
  3769. + bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  3770. +
  3771. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  3772. + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  3773. + dev->seq_number++;
  3774. + bi->seq_number = dev->seq_number;
  3775. + dev->n_erased_blocks--;
  3776. + T(YAFFS_TRACE_ALLOCATE,
  3777. + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
  3778. + dev->alloc_block_finder, dev->seq_number,
  3779. + dev->n_erased_blocks));
  3780. + return dev->alloc_block_finder;
  3781. + }
  3782. + }
  3783. +
  3784. + T(YAFFS_TRACE_ALWAYS,
  3785. + (TSTR
  3786. + ("yaffs tragedy: no more erased blocks, but there should have been %d"
  3787. + TENDSTR), dev->n_erased_blocks));
  3788. +
  3789. + return -1;
  3790. +}
  3791. +
  3792. +/*
  3793. + * Check if there's space to allocate...
  3794. + * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
  3795. + */
  3796. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  3797. +{
  3798. + int reserved_chunks;
  3799. + int reserved_blocks = dev->param.n_reserved_blocks;
  3800. + int checkpt_blocks;
  3801. +
  3802. + checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  3803. +
  3804. + reserved_chunks =
  3805. + ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
  3806. +
  3807. + return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  3808. +}
  3809. +
  3810. +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  3811. + struct yaffs_block_info **block_ptr)
  3812. +{
  3813. + int ret_val;
  3814. + struct yaffs_block_info *bi;
  3815. +
  3816. + if (dev->alloc_block < 0) {
  3817. + /* Get next block to allocate off */
  3818. + dev->alloc_block = yaffs_find_alloc_block(dev);
  3819. + dev->alloc_page = 0;
  3820. + }
  3821. +
  3822. + if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  3823. + /* Not enough space to allocate unless we're allowed to use the reserve. */
  3824. + return -1;
  3825. + }
  3826. +
  3827. + if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  3828. + && dev->alloc_page == 0) {
  3829. + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
  3830. + }
  3831. +
  3832. + /* Next page please.... */
  3833. + if (dev->alloc_block >= 0) {
  3834. + bi = yaffs_get_block_info(dev, dev->alloc_block);
  3835. +
  3836. + ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  3837. + dev->alloc_page;
  3838. + bi->pages_in_use++;
  3839. + yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  3840. +
  3841. + dev->alloc_page++;
  3842. +
  3843. + dev->n_free_chunks--;
  3844. +
  3845. + /* If the block is full set the state to full */
  3846. + if (dev->alloc_page >= dev->param.chunks_per_block) {
  3847. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  3848. + dev->alloc_block = -1;
  3849. + }
  3850. +
  3851. + if (block_ptr)
  3852. + *block_ptr = bi;
  3853. +
  3854. + return ret_val;
  3855. + }
  3856. +
  3857. + T(YAFFS_TRACE_ERROR,
  3858. + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
  3859. +
  3860. + return -1;
  3861. +}
  3862. +
  3863. +static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  3864. +{
  3865. + int n;
  3866. +
  3867. + n = dev->n_erased_blocks * dev->param.chunks_per_block;
  3868. +
  3869. + if (dev->alloc_block > 0)
  3870. + n += (dev->param.chunks_per_block - dev->alloc_page);
  3871. +
  3872. + return n;
  3873. +
  3874. +}
  3875. +
  3876. +/*
  3877. + * yaffs_skip_rest_of_block() skips over the rest of the allocation block
  3878. + * if we don't want to write to it.
  3879. + */
  3880. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  3881. +{
  3882. + if (dev->alloc_block > 0) {
  3883. + struct yaffs_block_info *bi =
  3884. + yaffs_get_block_info(dev, dev->alloc_block);
  3885. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  3886. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  3887. + dev->alloc_block = -1;
  3888. + }
  3889. + }
  3890. +}
  3891. +
  3892. +static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  3893. +{
  3894. + int old_chunk;
  3895. + int new_chunk;
  3896. + int mark_flash;
  3897. + int ret_val = YAFFS_OK;
  3898. + int i;
  3899. + int is_checkpt_block;
  3900. + int matching_chunk;
  3901. + int max_copies;
  3902. +
  3903. + int chunks_before = yaffs_get_erased_chunks(dev);
  3904. + int chunks_after;
  3905. +
  3906. + struct yaffs_ext_tags tags;
  3907. +
  3908. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  3909. +
  3910. + struct yaffs_obj *object;
  3911. +
  3912. + is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  3913. +
  3914. + T(YAFFS_TRACE_TRACING,
  3915. + (TSTR
  3916. + ("Collecting block %d, in use %d, shrink %d, whole_block %d"
  3917. + TENDSTR), block, bi->pages_in_use, bi->has_shrink_hdr,
  3918. + whole_block));
  3919. +
  3920. + /*yaffs_verify_free_chunks(dev); */
  3921. +
  3922. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  3923. + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  3924. +
  3925. + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
  3926. +
  3927. + dev->gc_disable = 1;
  3928. +
  3929. + if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  3930. + T(YAFFS_TRACE_TRACING,
  3931. + (TSTR
  3932. + ("Collecting block %d that has no chunks in use" TENDSTR),
  3933. + block));
  3934. + yaffs_block_became_dirty(dev, block);
  3935. + } else {
  3936. +
  3937. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  3938. +
  3939. + yaffs_verify_blk(dev, bi, block);
  3940. +
  3941. + max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  3942. + old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  3943. +
  3944. + for ( /* init already done */ ;
  3945. + ret_val == YAFFS_OK &&
  3946. + dev->gc_chunk < dev->param.chunks_per_block &&
  3947. + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  3948. + max_copies > 0; dev->gc_chunk++, old_chunk++) {
  3949. + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  3950. +
  3951. + /* This page is in use and might need to be copied off */
  3952. +
  3953. + max_copies--;
  3954. +
  3955. + mark_flash = 1;
  3956. +
  3957. + yaffs_init_tags(&tags);
  3958. +
  3959. + yaffs_rd_chunk_tags_nand(dev, old_chunk,
  3960. + buffer, &tags);
  3961. +
  3962. + object = yaffs_find_by_number(dev, tags.obj_id);
  3963. +
  3964. + T(YAFFS_TRACE_GC_DETAIL,
  3965. + (TSTR
  3966. + ("Collecting chunk in block %d, %d %d %d "
  3967. + TENDSTR), dev->gc_chunk, tags.obj_id,
  3968. + tags.chunk_id, tags.n_bytes));
  3969. +
  3970. + if (object && !yaffs_skip_verification(dev)) {
  3971. + if (tags.chunk_id == 0)
  3972. + matching_chunk =
  3973. + object->hdr_chunk;
  3974. + else if (object->soft_del)
  3975. + matching_chunk = old_chunk; /* Defeat the test */
  3976. + else
  3977. + matching_chunk =
  3978. + yaffs_find_chunk_in_file
  3979. + (object, tags.chunk_id,
  3980. + NULL);
  3981. +
  3982. + if (old_chunk != matching_chunk)
  3983. + T(YAFFS_TRACE_ERROR,
  3984. + (TSTR
  3985. + ("gc: page in gc mismatch: %d %d %d %d"
  3986. + TENDSTR), old_chunk,
  3987. + matching_chunk, tags.obj_id,
  3988. + tags.chunk_id));
  3989. +
  3990. + }
  3991. +
  3992. + if (!object) {
  3993. + T(YAFFS_TRACE_ERROR,
  3994. + (TSTR
  3995. + ("page %d in gc has no object: %d %d %d "
  3996. + TENDSTR), old_chunk,
  3997. + tags.obj_id, tags.chunk_id,
  3998. + tags.n_bytes));
  3999. + }
  4000. +
  4001. + if (object &&
  4002. + object->deleted &&
  4003. + object->soft_del && tags.chunk_id != 0) {
  4004. + /* Data chunk in a soft deleted file, throw it away
  4005. + * It's a soft deleted data chunk,
  4006. + * No need to copy this, just forget about it and
  4007. + * fix up the object.
  4008. + */
  4009. +
  4010. + /* Free chunks already includes softdeleted chunks.
  4011. + * How ever this chunk is going to soon be really deleted
  4012. + * which will increment free chunks.
  4013. + * We have to decrement free chunks so this works out properly.
  4014. + */
  4015. + dev->n_free_chunks--;
  4016. + bi->soft_del_pages--;
  4017. +
  4018. + object->n_data_chunks--;
  4019. +
  4020. + if (object->n_data_chunks <= 0) {
  4021. + /* remeber to clean up the object */
  4022. + dev->gc_cleanup_list[dev->
  4023. + n_clean_ups]
  4024. + = tags.obj_id;
  4025. + dev->n_clean_ups++;
  4026. + }
  4027. + mark_flash = 0;
  4028. + } else if (0) {
  4029. + /* Todo object && object->deleted && object->n_data_chunks == 0 */
  4030. + /* Deleted object header with no data chunks.
  4031. + * Can be discarded and the file deleted.
  4032. + */
  4033. + object->hdr_chunk = 0;
  4034. + yaffs_free_tnode(object->my_dev,
  4035. + object->
  4036. + variant.file_variant.
  4037. + top);
  4038. + object->variant.file_variant.top = NULL;
  4039. + yaffs_generic_obj_del(object);
  4040. +
  4041. + } else if (object) {
  4042. + /* It's either a data chunk in a live file or
  4043. + * an ObjectHeader, so we're interested in it.
  4044. + * NB Need to keep the ObjectHeaders of deleted files
  4045. + * until the whole file has been deleted off
  4046. + */
  4047. + tags.serial_number++;
  4048. +
  4049. + dev->n_gc_copies++;
  4050. +
  4051. + if (tags.chunk_id == 0) {
  4052. + /* It is an object Id,
  4053. + * We need to nuke the shrinkheader flags first
  4054. + * Also need to clean up shadowing.
  4055. + * We no longer want the shrink_header flag since its work is done
  4056. + * and if it is left in place it will mess up scanning.
  4057. + */
  4058. +
  4059. + struct yaffs_obj_hdr *oh;
  4060. + oh = (struct yaffs_obj_hdr *)
  4061. + buffer;
  4062. +
  4063. + oh->is_shrink = 0;
  4064. + tags.extra_is_shrink = 0;
  4065. +
  4066. + oh->shadows_obj = 0;
  4067. + oh->inband_shadowed_obj_id = 0;
  4068. + tags.extra_shadows = 0;
  4069. +
  4070. + /* Update file size */
  4071. + if (object->variant_type ==
  4072. + YAFFS_OBJECT_TYPE_FILE) {
  4073. + oh->file_size =
  4074. + object->variant.
  4075. + file_variant.
  4076. + file_size;
  4077. + tags.extra_length =
  4078. + oh->file_size;
  4079. + }
  4080. +
  4081. + yaffs_verify_oh(object, oh,
  4082. + &tags, 1);
  4083. + new_chunk =
  4084. + yaffs_write_new_chunk(dev,
  4085. + (u8 *)
  4086. + oh,
  4087. + &tags,
  4088. + 1);
  4089. + } else {
  4090. + new_chunk =
  4091. + yaffs_write_new_chunk(dev,
  4092. + buffer,
  4093. + &tags,
  4094. + 1);
  4095. + }
  4096. +
  4097. + if (new_chunk < 0) {
  4098. + ret_val = YAFFS_FAIL;
  4099. + } else {
  4100. +
  4101. + /* Ok, now fix up the Tnodes etc. */
  4102. +
  4103. + if (tags.chunk_id == 0) {
  4104. + /* It's a header */
  4105. + object->hdr_chunk =
  4106. + new_chunk;
  4107. + object->serial =
  4108. + tags.serial_number;
  4109. + } else {
  4110. + /* It's a data chunk */
  4111. + int ok;
  4112. + ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
  4113. + }
  4114. + }
  4115. + }
  4116. +
  4117. + if (ret_val == YAFFS_OK)
  4118. + yaffs_chunk_del(dev, old_chunk,
  4119. + mark_flash, __LINE__);
  4120. +
  4121. + }
  4122. + }
  4123. +
  4124. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  4125. +
  4126. + }
  4127. +
  4128. + yaffs_verify_collected_blk(dev, bi, block);
  4129. +
  4130. + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4131. + /*
  4132. + * The gc did not complete. Set block state back to FULL
  4133. + * because checkpointing does not restore gc.
  4134. + */
  4135. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  4136. + } else {
  4137. + /* The gc completed. */
  4138. + /* Do any required cleanups */
  4139. + for (i = 0; i < dev->n_clean_ups; i++) {
  4140. + /* Time to delete the file too */
  4141. + object =
  4142. + yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
  4143. + if (object) {
  4144. + yaffs_free_tnode(dev,
  4145. + object->variant.
  4146. + file_variant.top);
  4147. + object->variant.file_variant.top = NULL;
  4148. + T(YAFFS_TRACE_GC,
  4149. + (TSTR
  4150. + ("yaffs: About to finally delete object %d"
  4151. + TENDSTR), object->obj_id));
  4152. + yaffs_generic_obj_del(object);
  4153. + object->my_dev->n_deleted_files--;
  4154. + }
  4155. +
  4156. + }
  4157. +
  4158. + chunks_after = yaffs_get_erased_chunks(dev);
  4159. + if (chunks_before >= chunks_after) {
  4160. + T(YAFFS_TRACE_GC,
  4161. + (TSTR
  4162. + ("gc did not increase free chunks before %d after %d"
  4163. + TENDSTR), chunks_before, chunks_after));
  4164. + }
  4165. + dev->gc_block = 0;
  4166. + dev->gc_chunk = 0;
  4167. + dev->n_clean_ups = 0;
  4168. + }
  4169. +
  4170. + dev->gc_disable = 0;
  4171. +
  4172. + return ret_val;
  4173. +}
  4174. +
  4175. +/*
  4176. + * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
  4177. + * for garbage collection.
  4178. + */
  4179. +
  4180. +static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
  4181. + int aggressive, int background)
  4182. +{
  4183. + int i;
  4184. + int iterations;
  4185. + unsigned selected = 0;
  4186. + int prioritised = 0;
  4187. + int prioritised_exist = 0;
  4188. + struct yaffs_block_info *bi;
  4189. + int threshold;
  4190. +
  4191. + /* First let's see if we need to grab a prioritised block */
  4192. + if (dev->has_pending_prioritised_gc && !aggressive) {
  4193. + dev->gc_dirtiest = 0;
  4194. + bi = dev->block_info;
  4195. + for (i = dev->internal_start_block;
  4196. + i <= dev->internal_end_block && !selected; i++) {
  4197. +
  4198. + if (bi->gc_prioritise) {
  4199. + prioritised_exist = 1;
  4200. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4201. + yaffs_block_ok_for_gc(dev, bi)) {
  4202. + selected = i;
  4203. + prioritised = 1;
  4204. + }
  4205. + }
  4206. + bi++;
  4207. + }
  4208. +
  4209. + /*
  4210. + * If there is a prioritised block and none was selected then
  4211. + * this happened because there is at least one old dirty block gumming
  4212. + * up the works. Let's gc the oldest dirty block.
  4213. + */
  4214. +
  4215. + if (prioritised_exist &&
  4216. + !selected && dev->oldest_dirty_block > 0)
  4217. + selected = dev->oldest_dirty_block;
  4218. +
  4219. + if (!prioritised_exist) /* None found, so we can clear this */
  4220. + dev->has_pending_prioritised_gc = 0;
  4221. + }
  4222. +
  4223. + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  4224. + * search harder.
  4225. + * else (we're doing a leasurely gc), then we only bother to do this if the
  4226. + * block has only a few pages in use.
  4227. + */
  4228. +
  4229. + if (!selected) {
  4230. + int pages_used;
  4231. + int n_blocks =
  4232. + dev->internal_end_block - dev->internal_start_block + 1;
  4233. + if (aggressive) {
  4234. + threshold = dev->param.chunks_per_block;
  4235. + iterations = n_blocks;
  4236. + } else {
  4237. + int max_threshold;
  4238. +
  4239. + if (background)
  4240. + max_threshold = dev->param.chunks_per_block / 2;
  4241. + else
  4242. + max_threshold = dev->param.chunks_per_block / 8;
  4243. +
  4244. + if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4245. + max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4246. +
  4247. + threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
  4248. + if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4249. + threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4250. + if (threshold > max_threshold)
  4251. + threshold = max_threshold;
  4252. +
  4253. + iterations = n_blocks / 16 + 1;
  4254. + if (iterations > 100)
  4255. + iterations = 100;
  4256. + }
  4257. +
  4258. + for (i = 0;
  4259. + i < iterations &&
  4260. + (dev->gc_dirtiest < 1 ||
  4261. + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
  4262. + dev->gc_block_finder++;
  4263. + if (dev->gc_block_finder < dev->internal_start_block ||
  4264. + dev->gc_block_finder > dev->internal_end_block)
  4265. + dev->gc_block_finder =
  4266. + dev->internal_start_block;
  4267. +
  4268. + bi = yaffs_get_block_info(dev, dev->gc_block_finder);
  4269. +
  4270. + pages_used = bi->pages_in_use - bi->soft_del_pages;
  4271. +
  4272. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4273. + pages_used < dev->param.chunks_per_block &&
  4274. + (dev->gc_dirtiest < 1
  4275. + || pages_used < dev->gc_pages_in_use)
  4276. + && yaffs_block_ok_for_gc(dev, bi)) {
  4277. + dev->gc_dirtiest = dev->gc_block_finder;
  4278. + dev->gc_pages_in_use = pages_used;
  4279. + }
  4280. + }
  4281. +
  4282. + if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
  4283. + selected = dev->gc_dirtiest;
  4284. + }
  4285. +
  4286. + /*
  4287. + * If nothing has been selected for a while, try selecting the oldest dirty
  4288. + * because that's gumming up the works.
  4289. + */
  4290. +
  4291. + if (!selected && dev->param.is_yaffs2 &&
  4292. + dev->gc_not_done >= (background ? 10 : 20)) {
  4293. + yaffs2_find_oldest_dirty_seq(dev);
  4294. + if (dev->oldest_dirty_block > 0) {
  4295. + selected = dev->oldest_dirty_block;
  4296. + dev->gc_dirtiest = selected;
  4297. + dev->oldest_dirty_gc_count++;
  4298. + bi = yaffs_get_block_info(dev, selected);
  4299. + dev->gc_pages_in_use =
  4300. + bi->pages_in_use - bi->soft_del_pages;
  4301. + } else {
  4302. + dev->gc_not_done = 0;
  4303. + }
  4304. + }
  4305. +
  4306. + if (selected) {
  4307. + T(YAFFS_TRACE_GC,
  4308. + (TSTR
  4309. + ("GC Selected block %d with %d free, prioritised:%d"
  4310. + TENDSTR), selected,
  4311. + dev->param.chunks_per_block - dev->gc_pages_in_use,
  4312. + prioritised));
  4313. +
  4314. + dev->n_gc_blocks++;
  4315. + if (background)
  4316. + dev->bg_gcs++;
  4317. +
  4318. + dev->gc_dirtiest = 0;
  4319. + dev->gc_pages_in_use = 0;
  4320. + dev->gc_not_done = 0;
  4321. + if (dev->refresh_skip > 0)
  4322. + dev->refresh_skip--;
  4323. + } else {
  4324. + dev->gc_not_done++;
  4325. + T(YAFFS_TRACE_GC,
  4326. + (TSTR
  4327. + ("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s"
  4328. + TENDSTR), dev->gc_block_finder, dev->gc_not_done, threshold,
  4329. + dev->gc_dirtiest, dev->gc_pages_in_use,
  4330. + dev->oldest_dirty_block, background ? " bg" : ""));
  4331. + }
  4332. +
  4333. + return selected;
  4334. +}
  4335. +
  4336. +/* New garbage collector
  4337. + * If we're very low on erased blocks then we do aggressive garbage collection
  4338. + * otherwise we do "leasurely" garbage collection.
  4339. + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  4340. + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  4341. + *
  4342. + * The idea is to help clear out space in a more spread-out manner.
  4343. + * Dunno if it really does anything useful.
  4344. + */
  4345. +static int yaffs_check_gc(struct yaffs_dev *dev, int background)
  4346. +{
  4347. + int aggressive = 0;
  4348. + int gc_ok = YAFFS_OK;
  4349. + int max_tries = 0;
  4350. + int min_erased;
  4351. + int erased_chunks;
  4352. + int checkpt_block_adjust;
  4353. +
  4354. + if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
  4355. + return YAFFS_OK;
  4356. +
  4357. + if (dev->gc_disable) {
  4358. + /* Bail out so we don't get recursive gc */
  4359. + return YAFFS_OK;
  4360. + }
  4361. +
  4362. + /* This loop should pass the first time.
  4363. + * We'll only see looping here if the collection does not increase space.
  4364. + */
  4365. +
  4366. + do {
  4367. + max_tries++;
  4368. +
  4369. + checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
  4370. +
  4371. + min_erased =
  4372. + dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
  4373. + erased_chunks =
  4374. + dev->n_erased_blocks * dev->param.chunks_per_block;
  4375. +
  4376. + /* If we need a block soon then do aggressive gc. */
  4377. + if (dev->n_erased_blocks < min_erased)
  4378. + aggressive = 1;
  4379. + else {
  4380. + if (!background
  4381. + && erased_chunks > (dev->n_free_chunks / 4))
  4382. + break;
  4383. +
  4384. + if (dev->gc_skip > 20)
  4385. + dev->gc_skip = 20;
  4386. + if (erased_chunks < dev->n_free_chunks / 2 ||
  4387. + dev->gc_skip < 1 || background)
  4388. + aggressive = 0;
  4389. + else {
  4390. + dev->gc_skip--;
  4391. + break;
  4392. + }
  4393. + }
  4394. +
  4395. + dev->gc_skip = 5;
  4396. +
  4397. + /* If we don't already have a block being gc'd then see if we should start another */
  4398. +
  4399. + if (dev->gc_block < 1 && !aggressive) {
  4400. + dev->gc_block = yaffs2_find_refresh_block(dev);
  4401. + dev->gc_chunk = 0;
  4402. + dev->n_clean_ups = 0;
  4403. + }
  4404. + if (dev->gc_block < 1) {
  4405. + dev->gc_block =
  4406. + yaffs_find_gc_block(dev, aggressive, background);
  4407. + dev->gc_chunk = 0;
  4408. + dev->n_clean_ups = 0;
  4409. + }
  4410. +
  4411. + if (dev->gc_block > 0) {
  4412. + dev->all_gcs++;
  4413. + if (!aggressive)
  4414. + dev->passive_gc_count++;
  4415. +
  4416. + T(YAFFS_TRACE_GC,
  4417. + (TSTR
  4418. + ("yaffs: GC n_erased_blocks %d aggressive %d"
  4419. + TENDSTR), dev->n_erased_blocks, aggressive));
  4420. +
  4421. + gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
  4422. + }
  4423. +
  4424. + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
  4425. + && dev->gc_block > 0) {
  4426. + T(YAFFS_TRACE_GC,
  4427. + (TSTR
  4428. + ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d"
  4429. + TENDSTR), dev->n_erased_blocks, max_tries,
  4430. + dev->gc_block));
  4431. + }
  4432. + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
  4433. + (dev->gc_block > 0) && (max_tries < 2));
  4434. +
  4435. + return aggressive ? gc_ok : YAFFS_OK;
  4436. +}
  4437. +
  4438. +/*
  4439. + * yaffs_bg_gc()
  4440. + * Garbage collects. Intended to be called from a background thread.
  4441. + * Returns non-zero if at least half the free chunks are erased.
  4442. + */
  4443. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
  4444. +{
  4445. + int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
  4446. +
  4447. + T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR), urgency));
  4448. +
  4449. + yaffs_check_gc(dev, 1);
  4450. + return erased_chunks > dev->n_free_chunks / 2;
  4451. +}
  4452. +
  4453. +/*------------------------- TAGS --------------------------------*/
  4454. +
  4455. +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  4456. + int chunk_obj)
  4457. +{
  4458. + return (tags->chunk_id == chunk_obj &&
  4459. + tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
  4460. +
  4461. +}
  4462. +
  4463. +/*-------------------- Data file manipulation -----------------*/
  4464. +
  4465. +static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  4466. + struct yaffs_ext_tags *tags)
  4467. +{
  4468. + /*Get the Tnode, then get the level 0 offset chunk offset */
  4469. + struct yaffs_tnode *tn;
  4470. + int the_chunk = -1;
  4471. + struct yaffs_ext_tags local_tags;
  4472. + int ret_val = -1;
  4473. +
  4474. + struct yaffs_dev *dev = in->my_dev;
  4475. +
  4476. + if (!tags) {
  4477. + /* Passed a NULL, so use our own tags space */
  4478. + tags = &local_tags;
  4479. + }
  4480. +
  4481. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  4482. +
  4483. + if (tn) {
  4484. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4485. +
  4486. + ret_val =
  4487. + yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  4488. + inode_chunk);
  4489. + }
  4490. + return ret_val;
  4491. +}
  4492. +
  4493. +static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  4494. + struct yaffs_ext_tags *tags)
  4495. +{
  4496. + /* Get the Tnode, then get the level 0 offset chunk offset */
  4497. + struct yaffs_tnode *tn;
  4498. + int the_chunk = -1;
  4499. + struct yaffs_ext_tags local_tags;
  4500. +
  4501. + struct yaffs_dev *dev = in->my_dev;
  4502. + int ret_val = -1;
  4503. +
  4504. + if (!tags) {
  4505. + /* Passed a NULL, so use our own tags space */
  4506. + tags = &local_tags;
  4507. + }
  4508. +
  4509. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  4510. +
  4511. + if (tn) {
  4512. +
  4513. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4514. +
  4515. + ret_val =
  4516. + yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  4517. + inode_chunk);
  4518. +
  4519. + /* Delete the entry in the filestructure (if found) */
  4520. + if (ret_val != -1)
  4521. + yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  4522. + }
  4523. +
  4524. + return ret_val;
  4525. +}
  4526. +
  4527. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  4528. + int nand_chunk, int in_scan)
  4529. +{
  4530. + /* NB in_scan is zero unless scanning.
  4531. + * For forward scanning, in_scan is > 0;
  4532. + * for backward scanning in_scan is < 0
  4533. + *
  4534. + * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
  4535. + */
  4536. +
  4537. + struct yaffs_tnode *tn;
  4538. + struct yaffs_dev *dev = in->my_dev;
  4539. + int existing_cunk;
  4540. + struct yaffs_ext_tags existing_tags;
  4541. + struct yaffs_ext_tags new_tags;
  4542. + unsigned existing_serial, new_serial;
  4543. +
  4544. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  4545. + /* Just ignore an attempt at putting a chunk into a non-file during scanning
  4546. + * If it is not during Scanning then something went wrong!
  4547. + */
  4548. + if (!in_scan) {
  4549. + T(YAFFS_TRACE_ERROR,
  4550. + (TSTR
  4551. + ("yaffs tragedy:attempt to put data chunk into a non-file"
  4552. + TENDSTR)));
  4553. + YBUG();
  4554. + }
  4555. +
  4556. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  4557. + return YAFFS_OK;
  4558. + }
  4559. +
  4560. + tn = yaffs_add_find_tnode_0(dev,
  4561. + &in->variant.file_variant,
  4562. + inode_chunk, NULL);
  4563. + if (!tn)
  4564. + return YAFFS_FAIL;
  4565. +
  4566. + if (!nand_chunk)
  4567. + /* Dummy insert, bail now */
  4568. + return YAFFS_OK;
  4569. +
  4570. + existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  4571. +
  4572. + if (in_scan != 0) {
  4573. + /* If we're scanning then we need to test for duplicates
  4574. + * NB This does not need to be efficient since it should only ever
  4575. + * happen when the power fails during a write, then only one
  4576. + * chunk should ever be affected.
  4577. + *
  4578. + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  4579. + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  4580. + */
  4581. +
  4582. + if (existing_cunk > 0) {
  4583. + /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
  4584. + * thus we have to do a FindChunkInFile to get the real chunk id.
  4585. + *
  4586. + * We have a duplicate now we need to decide which one to use:
  4587. + *
  4588. + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  4589. + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  4590. + * YAFFS1: Get both sets of tags and compare serial numbers.
  4591. + */
  4592. +
  4593. + if (in_scan > 0) {
  4594. + /* Only do this for forward scanning */
  4595. + yaffs_rd_chunk_tags_nand(dev,
  4596. + nand_chunk,
  4597. + NULL, &new_tags);
  4598. +
  4599. + /* Do a proper find */
  4600. + existing_cunk =
  4601. + yaffs_find_chunk_in_file(in, inode_chunk,
  4602. + &existing_tags);
  4603. + }
  4604. +
  4605. + if (existing_cunk <= 0) {
  4606. + /*Hoosterman - how did this happen? */
  4607. +
  4608. + T(YAFFS_TRACE_ERROR,
  4609. + (TSTR
  4610. + ("yaffs tragedy: existing chunk < 0 in scan"
  4611. + TENDSTR)));
  4612. +
  4613. + }
  4614. +
  4615. + /* NB The deleted flags should be false, otherwise the chunks will
  4616. + * not be loaded during a scan
  4617. + */
  4618. +
  4619. + if (in_scan > 0) {
  4620. + new_serial = new_tags.serial_number;
  4621. + existing_serial = existing_tags.serial_number;
  4622. + }
  4623. +
  4624. + if ((in_scan > 0) &&
  4625. + (existing_cunk <= 0 ||
  4626. + ((existing_serial + 1) & 3) == new_serial)) {
  4627. + /* Forward scanning.
  4628. + * Use new
  4629. + * Delete the old one and drop through to update the tnode
  4630. + */
  4631. + yaffs_chunk_del(dev, existing_cunk, 1,
  4632. + __LINE__);
  4633. + } else {
  4634. + /* Backward scanning or we want to use the existing one
  4635. + * Use existing.
  4636. + * Delete the new one and return early so that the tnode isn't changed
  4637. + */
  4638. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  4639. + return YAFFS_OK;
  4640. + }
  4641. + }
  4642. +
  4643. + }
  4644. +
  4645. + if (existing_cunk == 0)
  4646. + in->n_data_chunks++;
  4647. +
  4648. + yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  4649. +
  4650. + return YAFFS_OK;
  4651. +}
  4652. +
  4653. +static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
  4654. +{
  4655. + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
  4656. +
  4657. + if (nand_chunk >= 0)
  4658. + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
  4659. + buffer, NULL);
  4660. + else {
  4661. + T(YAFFS_TRACE_NANDACCESS,
  4662. + (TSTR("Chunk %d not found zero instead" TENDSTR),
  4663. + nand_chunk));
  4664. + /* get sane (zero) data if you read a hole */
  4665. + memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
  4666. + return 0;
  4667. + }
  4668. +
  4669. +}
  4670. +
  4671. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  4672. + int lyn)
  4673. +{
  4674. + int block;
  4675. + int page;
  4676. + struct yaffs_ext_tags tags;
  4677. + struct yaffs_block_info *bi;
  4678. +
  4679. + if (chunk_id <= 0)
  4680. + return;
  4681. +
  4682. + dev->n_deletions++;
  4683. + block = chunk_id / dev->param.chunks_per_block;
  4684. + page = chunk_id % dev->param.chunks_per_block;
  4685. +
  4686. + if (!yaffs_check_chunk_bit(dev, block, page))
  4687. + T(YAFFS_TRACE_VERIFY,
  4688. + (TSTR("Deleting invalid chunk %d" TENDSTR), chunk_id));
  4689. +
  4690. + bi = yaffs_get_block_info(dev, block);
  4691. +
  4692. + yaffs2_update_oldest_dirty_seq(dev, block, bi);
  4693. +
  4694. + T(YAFFS_TRACE_DELETION,
  4695. + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
  4696. +
  4697. + if (!dev->param.is_yaffs2 && mark_flash &&
  4698. + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
  4699. +
  4700. + yaffs_init_tags(&tags);
  4701. +
  4702. + tags.is_deleted = 1;
  4703. +
  4704. + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
  4705. + yaffs_handle_chunk_update(dev, chunk_id, &tags);
  4706. + } else {
  4707. + dev->n_unmarked_deletions++;
  4708. + }
  4709. +
  4710. + /* Pull out of the management area.
  4711. + * If the whole block became dirty, this will kick off an erasure.
  4712. + */
  4713. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
  4714. + bi->block_state == YAFFS_BLOCK_STATE_FULL ||
  4715. + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  4716. + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4717. + dev->n_free_chunks++;
  4718. +
  4719. + yaffs_clear_chunk_bit(dev, block, page);
  4720. +
  4721. + bi->pages_in_use--;
  4722. +
  4723. + if (bi->pages_in_use == 0 &&
  4724. + !bi->has_shrink_hdr &&
  4725. + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
  4726. + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  4727. + yaffs_block_became_dirty(dev, block);
  4728. + }
  4729. +
  4730. + }
  4731. +
  4732. +}
  4733. +
  4734. +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  4735. + const u8 * buffer, int n_bytes, int use_reserve)
  4736. +{
  4737. + /* Find old chunk Need to do this to get serial number
  4738. + * Write new one and patch into tree.
  4739. + * Invalidate old tags.
  4740. + */
  4741. +
  4742. + int prev_chunk_id;
  4743. + struct yaffs_ext_tags prev_tags;
  4744. +
  4745. + int new_chunk_id;
  4746. + struct yaffs_ext_tags new_tags;
  4747. +
  4748. + struct yaffs_dev *dev = in->my_dev;
  4749. +
  4750. + yaffs_check_gc(dev, 0);
  4751. +
  4752. + /* Get the previous chunk at this location in the file if it exists.
  4753. + * If it does not exist then put a zero into the tree. This creates
  4754. + * the tnode now, rather than later when it is harder to clean up.
  4755. + */
  4756. + prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
  4757. + if (prev_chunk_id < 1 &&
  4758. + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
  4759. + return 0;
  4760. +
  4761. + /* Set up new tags */
  4762. + yaffs_init_tags(&new_tags);
  4763. +
  4764. + new_tags.chunk_id = inode_chunk;
  4765. + new_tags.obj_id = in->obj_id;
  4766. + new_tags.serial_number =
  4767. + (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
  4768. + new_tags.n_bytes = n_bytes;
  4769. +
  4770. + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
  4771. + T(YAFFS_TRACE_ERROR,
  4772. + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR),
  4773. + n_bytes));
  4774. + YBUG();
  4775. + }
  4776. +
  4777. + new_chunk_id =
  4778. + yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
  4779. +
  4780. + if (new_chunk_id > 0) {
  4781. + yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
  4782. +
  4783. + if (prev_chunk_id > 0)
  4784. + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  4785. +
  4786. + yaffs_verify_file_sane(in);
  4787. + }
  4788. + return new_chunk_id;
  4789. +
  4790. +}
  4791. +
  4792. +/* UpdateObjectHeader updates the header on NAND for an object.
  4793. + * If name is not NULL, then that new name is used.
  4794. + */
  4795. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
  4796. + int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
  4797. +{
  4798. +
  4799. + struct yaffs_block_info *bi;
  4800. +
  4801. + struct yaffs_dev *dev = in->my_dev;
  4802. +
  4803. + int prev_chunk_id;
  4804. + int ret_val = 0;
  4805. + int result = 0;
  4806. +
  4807. + int new_chunk_id;
  4808. + struct yaffs_ext_tags new_tags;
  4809. + struct yaffs_ext_tags old_tags;
  4810. + const YCHAR *alias = NULL;
  4811. +
  4812. + u8 *buffer = NULL;
  4813. + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
  4814. +
  4815. + struct yaffs_obj_hdr *oh = NULL;
  4816. +
  4817. + yaffs_strcpy(old_name, _Y("silly old name"));
  4818. +
  4819. + if (!in->fake || in == dev->root_dir || /* The root_dir should also be saved */
  4820. + force || xmod) {
  4821. +
  4822. + yaffs_check_gc(dev, 0);
  4823. + yaffs_check_obj_details_loaded(in);
  4824. +
  4825. + buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
  4826. + oh = (struct yaffs_obj_hdr *)buffer;
  4827. +
  4828. + prev_chunk_id = in->hdr_chunk;
  4829. +
  4830. + if (prev_chunk_id > 0) {
  4831. + result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
  4832. + buffer, &old_tags);
  4833. +
  4834. + yaffs_verify_oh(in, oh, &old_tags, 0);
  4835. +
  4836. + memcpy(old_name, oh->name, sizeof(oh->name));
  4837. + memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
  4838. + } else {
  4839. + memset(buffer, 0xFF, dev->data_bytes_per_chunk);
  4840. + }
  4841. +
  4842. + oh->type = in->variant_type;
  4843. + oh->yst_mode = in->yst_mode;
  4844. + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
  4845. +
  4846. + yaffs_load_attribs_oh(oh, in);
  4847. +
  4848. + if (in->parent)
  4849. + oh->parent_obj_id = in->parent->obj_id;
  4850. + else
  4851. + oh->parent_obj_id = 0;
  4852. +
  4853. + if (name && *name) {
  4854. + memset(oh->name, 0, sizeof(oh->name));
  4855. + yaffs_load_oh_from_name(dev, oh->name, name);
  4856. + } else if (prev_chunk_id > 0) {
  4857. + memcpy(oh->name, old_name, sizeof(oh->name));
  4858. + } else {
  4859. + memset(oh->name, 0, sizeof(oh->name));
  4860. + }
  4861. +
  4862. + oh->is_shrink = is_shrink;
  4863. +
  4864. + switch (in->variant_type) {
  4865. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  4866. + /* Should not happen */
  4867. + break;
  4868. + case YAFFS_OBJECT_TYPE_FILE:
  4869. + oh->file_size =
  4870. + (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
  4871. + || oh->parent_obj_id ==
  4872. + YAFFS_OBJECTID_UNLINKED) ? 0 : in->
  4873. + variant.file_variant.file_size;
  4874. + break;
  4875. + case YAFFS_OBJECT_TYPE_HARDLINK:
  4876. + oh->equiv_id = in->variant.hardlink_variant.equiv_id;
  4877. + break;
  4878. + case YAFFS_OBJECT_TYPE_SPECIAL:
  4879. + /* Do nothing */
  4880. + break;
  4881. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  4882. + /* Do nothing */
  4883. + break;
  4884. + case YAFFS_OBJECT_TYPE_SYMLINK:
  4885. + alias = in->variant.symlink_variant.alias;
  4886. + if (!alias)
  4887. + alias = _Y("no alias");
  4888. + yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
  4889. + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  4890. + break;
  4891. + }
  4892. +
  4893. + /* process any xattrib modifications */
  4894. + if (xmod)
  4895. + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
  4896. +
  4897. + /* Tags */
  4898. + yaffs_init_tags(&new_tags);
  4899. + in->serial++;
  4900. + new_tags.chunk_id = 0;
  4901. + new_tags.obj_id = in->obj_id;
  4902. + new_tags.serial_number = in->serial;
  4903. +
  4904. + /* Add extra info for file header */
  4905. +
  4906. + new_tags.extra_available = 1;
  4907. + new_tags.extra_parent_id = oh->parent_obj_id;
  4908. + new_tags.extra_length = oh->file_size;
  4909. + new_tags.extra_is_shrink = oh->is_shrink;
  4910. + new_tags.extra_equiv_id = oh->equiv_id;
  4911. + new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
  4912. + new_tags.extra_obj_type = in->variant_type;
  4913. +
  4914. + yaffs_verify_oh(in, oh, &new_tags, 1);
  4915. +
  4916. + /* Create new chunk in NAND */
  4917. + new_chunk_id =
  4918. + yaffs_write_new_chunk(dev, buffer, &new_tags,
  4919. + (prev_chunk_id > 0) ? 1 : 0);
  4920. +
  4921. + if (new_chunk_id >= 0) {
  4922. +
  4923. + in->hdr_chunk = new_chunk_id;
  4924. +
  4925. + if (prev_chunk_id > 0) {
  4926. + yaffs_chunk_del(dev, prev_chunk_id, 1,
  4927. + __LINE__);
  4928. + }
  4929. +
  4930. + if (!yaffs_obj_cache_dirty(in))
  4931. + in->dirty = 0;
  4932. +
  4933. + /* If this was a shrink, then mark the block that the chunk lives on */
  4934. + if (is_shrink) {
  4935. + bi = yaffs_get_block_info(in->my_dev,
  4936. + new_chunk_id /
  4937. + in->my_dev->param.
  4938. + chunks_per_block);
  4939. + bi->has_shrink_hdr = 1;
  4940. + }
  4941. +
  4942. + }
  4943. +
  4944. + ret_val = new_chunk_id;
  4945. +
  4946. + }
  4947. +
  4948. + if (buffer)
  4949. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  4950. +
  4951. + return ret_val;
  4952. +}
  4953. +
  4954. +/*------------------------ Short Operations Cache ----------------------------------------
  4955. + * In many situations where there is no high level buffering a lot of
  4956. + * reads might be short sequential reads, and a lot of writes may be short
  4957. + * sequential writes. eg. scanning/writing a jpeg file.
  4958. + * In these cases, a short read/write cache can provide a huge perfomance
  4959. + * benefit with dumb-as-a-rock code.
  4960. + * In Linux, the page cache provides read buffering and the short op cache
  4961. + * provides write buffering.
  4962. + *
  4963. + * There are a limited number (~10) of cache chunks per device so that we don't
  4964. + * need a very intelligent search.
  4965. + */
  4966. +
  4967. +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  4968. +{
  4969. + struct yaffs_dev *dev = obj->my_dev;
  4970. + int i;
  4971. + struct yaffs_cache *cache;
  4972. + int n_caches = obj->my_dev->param.n_caches;
  4973. +
  4974. + for (i = 0; i < n_caches; i++) {
  4975. + cache = &dev->cache[i];
  4976. + if (cache->object == obj && cache->dirty)
  4977. + return 1;
  4978. + }
  4979. +
  4980. + return 0;
  4981. +}
  4982. +
  4983. +static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  4984. +{
  4985. + struct yaffs_dev *dev = obj->my_dev;
  4986. + int lowest = -99; /* Stop compiler whining. */
  4987. + int i;
  4988. + struct yaffs_cache *cache;
  4989. + int chunk_written = 0;
  4990. + int n_caches = obj->my_dev->param.n_caches;
  4991. +
  4992. + if (n_caches > 0) {
  4993. + do {
  4994. + cache = NULL;
  4995. +
  4996. + /* Find the dirty cache for this object with the lowest chunk id. */
  4997. + for (i = 0; i < n_caches; i++) {
  4998. + if (dev->cache[i].object == obj &&
  4999. + dev->cache[i].dirty) {
  5000. + if (!cache
  5001. + || dev->cache[i].chunk_id <
  5002. + lowest) {
  5003. + cache = &dev->cache[i];
  5004. + lowest = cache->chunk_id;
  5005. + }
  5006. + }
  5007. + }
  5008. +
  5009. + if (cache && !cache->locked) {
  5010. + /* Write it out and free it up */
  5011. +
  5012. + chunk_written =
  5013. + yaffs_wr_data_obj(cache->object,
  5014. + cache->chunk_id,
  5015. + cache->data,
  5016. + cache->n_bytes, 1);
  5017. + cache->dirty = 0;
  5018. + cache->object = NULL;
  5019. + }
  5020. +
  5021. + } while (cache && chunk_written > 0);
  5022. +
  5023. + if (cache) {
  5024. + /* Hoosterman, disk full while writing cache out. */
  5025. + T(YAFFS_TRACE_ERROR,
  5026. + (TSTR
  5027. + ("yaffs tragedy: no space during cache write"
  5028. + TENDSTR)));
  5029. +
  5030. + }
  5031. + }
  5032. +
  5033. +}
  5034. +
  5035. +/*yaffs_flush_whole_cache(dev)
  5036. + *
  5037. + *
  5038. + */
  5039. +
  5040. +void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  5041. +{
  5042. + struct yaffs_obj *obj;
  5043. + int n_caches = dev->param.n_caches;
  5044. + int i;
  5045. +
  5046. + /* Find a dirty object in the cache and flush it...
  5047. + * until there are no further dirty objects.
  5048. + */
  5049. + do {
  5050. + obj = NULL;
  5051. + for (i = 0; i < n_caches && !obj; i++) {
  5052. + if (dev->cache[i].object && dev->cache[i].dirty)
  5053. + obj = dev->cache[i].object;
  5054. +
  5055. + }
  5056. + if (obj)
  5057. + yaffs_flush_file_cache(obj);
  5058. +
  5059. + } while (obj);
  5060. +
  5061. +}
  5062. +
  5063. +/* Grab us a cache chunk for use.
  5064. + * First look for an empty one.
  5065. + * Then look for the least recently used non-dirty one.
  5066. + * Then look for the least recently used dirty one...., flush and look again.
  5067. + */
  5068. +static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  5069. +{
  5070. + int i;
  5071. +
  5072. + if (dev->param.n_caches > 0) {
  5073. + for (i = 0; i < dev->param.n_caches; i++) {
  5074. + if (!dev->cache[i].object)
  5075. + return &dev->cache[i];
  5076. + }
  5077. + }
  5078. +
  5079. + return NULL;
  5080. +}
  5081. +
  5082. +static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  5083. +{
  5084. + struct yaffs_cache *cache;
  5085. + struct yaffs_obj *the_obj;
  5086. + int usage;
  5087. + int i;
  5088. + int pushout;
  5089. +
  5090. + if (dev->param.n_caches > 0) {
  5091. + /* Try find a non-dirty one... */
  5092. +
  5093. + cache = yaffs_grab_chunk_worker(dev);
  5094. +
  5095. + if (!cache) {
  5096. + /* They were all dirty, find the last recently used object and flush
  5097. + * its cache, then find again.
  5098. + * NB what's here is not very accurate, we actually flush the object
  5099. + * the last recently used page.
  5100. + */
  5101. +
  5102. + /* With locking we can't assume we can use entry zero */
  5103. +
  5104. + the_obj = NULL;
  5105. + usage = -1;
  5106. + cache = NULL;
  5107. + pushout = -1;
  5108. +
  5109. + for (i = 0; i < dev->param.n_caches; i++) {
  5110. + if (dev->cache[i].object &&
  5111. + !dev->cache[i].locked &&
  5112. + (dev->cache[i].last_use < usage
  5113. + || !cache)) {
  5114. + usage = dev->cache[i].last_use;
  5115. + the_obj = dev->cache[i].object;
  5116. + cache = &dev->cache[i];
  5117. + pushout = i;
  5118. + }
  5119. + }
  5120. +
  5121. + if (!cache || cache->dirty) {
  5122. + /* Flush and try again */
  5123. + yaffs_flush_file_cache(the_obj);
  5124. + cache = yaffs_grab_chunk_worker(dev);
  5125. + }
  5126. +
  5127. + }
  5128. + return cache;
  5129. + } else {
  5130. + return NULL;
  5131. + }
  5132. +}
  5133. +
  5134. +/* Find a cached chunk */
  5135. +static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  5136. + int chunk_id)
  5137. +{
  5138. + struct yaffs_dev *dev = obj->my_dev;
  5139. + int i;
  5140. + if (dev->param.n_caches > 0) {
  5141. + for (i = 0; i < dev->param.n_caches; i++) {
  5142. + if (dev->cache[i].object == obj &&
  5143. + dev->cache[i].chunk_id == chunk_id) {
  5144. + dev->cache_hits++;
  5145. +
  5146. + return &dev->cache[i];
  5147. + }
  5148. + }
  5149. + }
  5150. + return NULL;
  5151. +}
  5152. +
  5153. +/* Mark the chunk for the least recently used algorithym */
  5154. +static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  5155. + int is_write)
  5156. +{
  5157. +
  5158. + if (dev->param.n_caches > 0) {
  5159. + if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
  5160. + /* Reset the cache usages */
  5161. + int i;
  5162. + for (i = 1; i < dev->param.n_caches; i++)
  5163. + dev->cache[i].last_use = 0;
  5164. +
  5165. + dev->cache_last_use = 0;
  5166. + }
  5167. +
  5168. + dev->cache_last_use++;
  5169. +
  5170. + cache->last_use = dev->cache_last_use;
  5171. +
  5172. + if (is_write)
  5173. + cache->dirty = 1;
  5174. + }
  5175. +}
  5176. +
  5177. +/* Invalidate a single cache page.
  5178. + * Do this when a whole page gets written,
  5179. + * ie the short cache for this page is no longer valid.
  5180. + */
  5181. +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  5182. +{
  5183. + if (object->my_dev->param.n_caches > 0) {
  5184. + struct yaffs_cache *cache =
  5185. + yaffs_find_chunk_cache(object, chunk_id);
  5186. +
  5187. + if (cache)
  5188. + cache->object = NULL;
  5189. + }
  5190. +}
  5191. +
  5192. +/* Invalidate all the cache pages associated with this object
  5193. + * Do this whenever ther file is deleted or resized.
  5194. + */
  5195. +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  5196. +{
  5197. + int i;
  5198. + struct yaffs_dev *dev = in->my_dev;
  5199. +
  5200. + if (dev->param.n_caches > 0) {
  5201. + /* Invalidate it. */
  5202. + for (i = 0; i < dev->param.n_caches; i++) {
  5203. + if (dev->cache[i].object == in)
  5204. + dev->cache[i].object = NULL;
  5205. + }
  5206. + }
  5207. +}
  5208. +
  5209. +/*--------------------- File read/write ------------------------
  5210. + * Read and write have very similar structures.
  5211. + * In general the read/write has three parts to it
  5212. + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  5213. + * Some complete chunks
  5214. + * An incomplete chunk to end off with
  5215. + *
  5216. + * Curve-balls: the first chunk might also be the last chunk.
  5217. + */
  5218. +
  5219. +int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
  5220. +{
  5221. +
  5222. + int chunk;
  5223. + u32 start;
  5224. + int n_copy;
  5225. + int n = n_bytes;
  5226. + int n_done = 0;
  5227. + struct yaffs_cache *cache;
  5228. +
  5229. + struct yaffs_dev *dev;
  5230. +
  5231. + dev = in->my_dev;
  5232. +
  5233. + while (n > 0) {
  5234. + /* chunk = offset / dev->data_bytes_per_chunk + 1; */
  5235. + /* start = offset % dev->data_bytes_per_chunk; */
  5236. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5237. + chunk++;
  5238. +
  5239. + /* OK now check for the curveball where the start and end are in
  5240. + * the same chunk.
  5241. + */
  5242. + if ((start + n) < dev->data_bytes_per_chunk)
  5243. + n_copy = n;
  5244. + else
  5245. + n_copy = dev->data_bytes_per_chunk - start;
  5246. +
  5247. + cache = yaffs_find_chunk_cache(in, chunk);
  5248. +
  5249. + /* If the chunk is already in the cache or it is less than a whole chunk
  5250. + * or we're using inband tags then use the cache (if there is caching)
  5251. + * else bypass the cache.
  5252. + */
  5253. + if (cache || n_copy != dev->data_bytes_per_chunk
  5254. + || dev->param.inband_tags) {
  5255. + if (dev->param.n_caches > 0) {
  5256. +
  5257. + /* If we can't find the data in the cache, then load it up. */
  5258. +
  5259. + if (!cache) {
  5260. + cache =
  5261. + yaffs_grab_chunk_cache(in->my_dev);
  5262. + cache->object = in;
  5263. + cache->chunk_id = chunk;
  5264. + cache->dirty = 0;
  5265. + cache->locked = 0;
  5266. + yaffs_rd_data_obj(in, chunk,
  5267. + cache->data);
  5268. + cache->n_bytes = 0;
  5269. + }
  5270. +
  5271. + yaffs_use_cache(dev, cache, 0);
  5272. +
  5273. + cache->locked = 1;
  5274. +
  5275. + memcpy(buffer, &cache->data[start], n_copy);
  5276. +
  5277. + cache->locked = 0;
  5278. + } else {
  5279. + /* Read into the local buffer then copy.. */
  5280. +
  5281. + u8 *local_buffer =
  5282. + yaffs_get_temp_buffer(dev, __LINE__);
  5283. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5284. +
  5285. + memcpy(buffer, &local_buffer[start], n_copy);
  5286. +
  5287. + yaffs_release_temp_buffer(dev, local_buffer,
  5288. + __LINE__);
  5289. + }
  5290. +
  5291. + } else {
  5292. +
  5293. + /* A full chunk. Read directly into the supplied buffer. */
  5294. + yaffs_rd_data_obj(in, chunk, buffer);
  5295. +
  5296. + }
  5297. +
  5298. + n -= n_copy;
  5299. + offset += n_copy;
  5300. + buffer += n_copy;
  5301. + n_done += n_copy;
  5302. +
  5303. + }
  5304. +
  5305. + return n_done;
  5306. +}
  5307. +
  5308. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  5309. + int n_bytes, int write_trhrough)
  5310. +{
  5311. +
  5312. + int chunk;
  5313. + u32 start;
  5314. + int n_copy;
  5315. + int n = n_bytes;
  5316. + int n_done = 0;
  5317. + int n_writeback;
  5318. + int start_write = offset;
  5319. + int chunk_written = 0;
  5320. + u32 n_bytes_read;
  5321. + u32 chunk_start;
  5322. +
  5323. + struct yaffs_dev *dev;
  5324. +
  5325. + dev = in->my_dev;
  5326. +
  5327. + while (n > 0 && chunk_written >= 0) {
  5328. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5329. +
  5330. + if (chunk * dev->data_bytes_per_chunk + start != offset ||
  5331. + start >= dev->data_bytes_per_chunk) {
  5332. + T(YAFFS_TRACE_ERROR,
  5333. + (TSTR
  5334. + ("AddrToChunk of offset %d gives chunk %d start %d"
  5335. + TENDSTR), (int)offset, chunk, start));
  5336. + }
  5337. + chunk++; /* File pos to chunk in file offset */
  5338. +
  5339. + /* OK now check for the curveball where the start and end are in
  5340. + * the same chunk.
  5341. + */
  5342. +
  5343. + if ((start + n) < dev->data_bytes_per_chunk) {
  5344. + n_copy = n;
  5345. +
  5346. + /* Now folks, to calculate how many bytes to write back....
  5347. + * If we're overwriting and not writing to then end of file then
  5348. + * we need to write back as much as was there before.
  5349. + */
  5350. +
  5351. + chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
  5352. +
  5353. + if (chunk_start > in->variant.file_variant.file_size)
  5354. + n_bytes_read = 0; /* Past end of file */
  5355. + else
  5356. + n_bytes_read =
  5357. + in->variant.file_variant.file_size -
  5358. + chunk_start;
  5359. +
  5360. + if (n_bytes_read > dev->data_bytes_per_chunk)
  5361. + n_bytes_read = dev->data_bytes_per_chunk;
  5362. +
  5363. + n_writeback =
  5364. + (n_bytes_read >
  5365. + (start + n)) ? n_bytes_read : (start + n);
  5366. +
  5367. + if (n_writeback < 0
  5368. + || n_writeback > dev->data_bytes_per_chunk)
  5369. + YBUG();
  5370. +
  5371. + } else {
  5372. + n_copy = dev->data_bytes_per_chunk - start;
  5373. + n_writeback = dev->data_bytes_per_chunk;
  5374. + }
  5375. +
  5376. + if (n_copy != dev->data_bytes_per_chunk
  5377. + || dev->param.inband_tags) {
  5378. + /* An incomplete start or end chunk (or maybe both start and end chunk),
  5379. + * or we're using inband tags, so we want to use the cache buffers.
  5380. + */
  5381. + if (dev->param.n_caches > 0) {
  5382. + struct yaffs_cache *cache;
  5383. + /* If we can't find the data in the cache, then load the cache */
  5384. + cache = yaffs_find_chunk_cache(in, chunk);
  5385. +
  5386. + if (!cache
  5387. + && yaffs_check_alloc_available(dev, 1)) {
  5388. + cache = yaffs_grab_chunk_cache(dev);
  5389. + cache->object = in;
  5390. + cache->chunk_id = chunk;
  5391. + cache->dirty = 0;
  5392. + cache->locked = 0;
  5393. + yaffs_rd_data_obj(in, chunk,
  5394. + cache->data);
  5395. + } else if (cache &&
  5396. + !cache->dirty &&
  5397. + !yaffs_check_alloc_available(dev,
  5398. + 1)) {
  5399. + /* Drop the cache if it was a read cache item and
  5400. + * no space check has been made for it.
  5401. + */
  5402. + cache = NULL;
  5403. + }
  5404. +
  5405. + if (cache) {
  5406. + yaffs_use_cache(dev, cache, 1);
  5407. + cache->locked = 1;
  5408. +
  5409. + memcpy(&cache->data[start], buffer,
  5410. + n_copy);
  5411. +
  5412. + cache->locked = 0;
  5413. + cache->n_bytes = n_writeback;
  5414. +
  5415. + if (write_trhrough) {
  5416. + chunk_written =
  5417. + yaffs_wr_data_obj
  5418. + (cache->object,
  5419. + cache->chunk_id,
  5420. + cache->data,
  5421. + cache->n_bytes, 1);
  5422. + cache->dirty = 0;
  5423. + }
  5424. +
  5425. + } else {
  5426. + chunk_written = -1; /* fail the write */
  5427. + }
  5428. + } else {
  5429. + /* An incomplete start or end chunk (or maybe both start and end chunk)
  5430. + * Read into the local buffer then copy, then copy over and write back.
  5431. + */
  5432. +
  5433. + u8 *local_buffer =
  5434. + yaffs_get_temp_buffer(dev, __LINE__);
  5435. +
  5436. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5437. +
  5438. + memcpy(&local_buffer[start], buffer, n_copy);
  5439. +
  5440. + chunk_written =
  5441. + yaffs_wr_data_obj(in, chunk,
  5442. + local_buffer,
  5443. + n_writeback, 0);
  5444. +
  5445. + yaffs_release_temp_buffer(dev, local_buffer,
  5446. + __LINE__);
  5447. +
  5448. + }
  5449. +
  5450. + } else {
  5451. + /* A full chunk. Write directly from the supplied buffer. */
  5452. +
  5453. + chunk_written =
  5454. + yaffs_wr_data_obj(in, chunk, buffer,
  5455. + dev->data_bytes_per_chunk, 0);
  5456. +
  5457. + /* Since we've overwritten the cached data, we better invalidate it. */
  5458. + yaffs_invalidate_chunk_cache(in, chunk);
  5459. + }
  5460. +
  5461. + if (chunk_written >= 0) {
  5462. + n -= n_copy;
  5463. + offset += n_copy;
  5464. + buffer += n_copy;
  5465. + n_done += n_copy;
  5466. + }
  5467. +
  5468. + }
  5469. +
  5470. + /* Update file object */
  5471. +
  5472. + if ((start_write + n_done) > in->variant.file_variant.file_size)
  5473. + in->variant.file_variant.file_size = (start_write + n_done);
  5474. +
  5475. + in->dirty = 1;
  5476. +
  5477. + return n_done;
  5478. +}
  5479. +
  5480. +int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  5481. + int n_bytes, int write_trhrough)
  5482. +{
  5483. + yaffs2_handle_hole(in, offset);
  5484. + return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
  5485. +}
  5486. +
  5487. +/* ---------------------- File resizing stuff ------------------ */
  5488. +
  5489. +static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
  5490. +{
  5491. +
  5492. + struct yaffs_dev *dev = in->my_dev;
  5493. + int old_size = in->variant.file_variant.file_size;
  5494. +
  5495. + int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
  5496. +
  5497. + int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
  5498. + dev->data_bytes_per_chunk;
  5499. + int i;
  5500. + int chunk_id;
  5501. +
  5502. + /* Delete backwards so that we don't end up with holes if
  5503. + * power is lost part-way through the operation.
  5504. + */
  5505. + for (i = last_del; i >= start_del; i--) {
  5506. + /* NB this could be optimised somewhat,
  5507. + * eg. could retrieve the tags and write them without
  5508. + * using yaffs_chunk_del
  5509. + */
  5510. +
  5511. + chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
  5512. + if (chunk_id > 0) {
  5513. + if (chunk_id <
  5514. + (dev->internal_start_block *
  5515. + dev->param.chunks_per_block)
  5516. + || chunk_id >=
  5517. + ((dev->internal_end_block +
  5518. + 1) * dev->param.chunks_per_block)) {
  5519. + T(YAFFS_TRACE_ALWAYS,
  5520. + (TSTR
  5521. + ("Found daft chunk_id %d for %d" TENDSTR),
  5522. + chunk_id, i));
  5523. + } else {
  5524. + in->n_data_chunks--;
  5525. + yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
  5526. + }
  5527. + }
  5528. + }
  5529. +
  5530. +}
  5531. +
  5532. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
  5533. +{
  5534. + int new_full;
  5535. + u32 new_partial;
  5536. + struct yaffs_dev *dev = obj->my_dev;
  5537. +
  5538. + yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
  5539. +
  5540. + yaffs_prune_chunks(obj, new_size);
  5541. +
  5542. + if (new_partial != 0) {
  5543. + int last_chunk = 1 + new_full;
  5544. + u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
  5545. +
  5546. + /* Got to read and rewrite the last chunk with its new size and zero pad */
  5547. + yaffs_rd_data_obj(obj, last_chunk, local_buffer);
  5548. + memset(local_buffer + new_partial, 0,
  5549. + dev->data_bytes_per_chunk - new_partial);
  5550. +
  5551. + yaffs_wr_data_obj(obj, last_chunk, local_buffer,
  5552. + new_partial, 1);
  5553. +
  5554. + yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
  5555. + }
  5556. +
  5557. + obj->variant.file_variant.file_size = new_size;
  5558. +
  5559. + yaffs_prune_tree(dev, &obj->variant.file_variant);
  5560. +}
  5561. +
  5562. +int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
  5563. +{
  5564. + struct yaffs_dev *dev = in->my_dev;
  5565. + int old_size = in->variant.file_variant.file_size;
  5566. +
  5567. + yaffs_flush_file_cache(in);
  5568. + yaffs_invalidate_whole_cache(in);
  5569. +
  5570. + yaffs_check_gc(dev, 0);
  5571. +
  5572. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
  5573. + return YAFFS_FAIL;
  5574. +
  5575. + if (new_size == old_size)
  5576. + return YAFFS_OK;
  5577. +
  5578. + if (new_size > old_size) {
  5579. + yaffs2_handle_hole(in, new_size);
  5580. + in->variant.file_variant.file_size = new_size;
  5581. + } else {
  5582. + /* new_size < old_size */
  5583. + yaffs_resize_file_down(in, new_size);
  5584. + }
  5585. +
  5586. + /* Write a new object header to reflect the resize.
  5587. + * show we've shrunk the file, if need be
  5588. + * Do this only if the file is not in the deleted directories
  5589. + * and is not shadowed.
  5590. + */
  5591. + if (in->parent &&
  5592. + !in->is_shadowed &&
  5593. + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  5594. + in->parent->obj_id != YAFFS_OBJECTID_DELETED)
  5595. + yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
  5596. +
  5597. + return YAFFS_OK;
  5598. +}
  5599. +
  5600. +int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
  5601. +{
  5602. + int ret_val;
  5603. + if (in->dirty) {
  5604. + yaffs_flush_file_cache(in);
  5605. + if (data_sync) /* Only sync data */
  5606. + ret_val = YAFFS_OK;
  5607. + else {
  5608. + if (update_time)
  5609. + yaffs_load_current_time(in, 0, 0);
  5610. +
  5611. + ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
  5612. + 0) ? YAFFS_OK : YAFFS_FAIL;
  5613. + }
  5614. + } else {
  5615. + ret_val = YAFFS_OK;
  5616. + }
  5617. +
  5618. + return ret_val;
  5619. +
  5620. +}
  5621. +
  5622. +static int yaffs_generic_obj_del(struct yaffs_obj *in)
  5623. +{
  5624. +
  5625. + /* First off, invalidate the file's data in the cache, without flushing. */
  5626. + yaffs_invalidate_whole_cache(in);
  5627. +
  5628. + if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
  5629. + /* Move to the unlinked directory so we have a record that it was deleted. */
  5630. + yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  5631. + 0);
  5632. +
  5633. + }
  5634. +
  5635. + yaffs_remove_obj_from_dir(in);
  5636. + yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  5637. + in->hdr_chunk = 0;
  5638. +
  5639. + yaffs_free_obj(in);
  5640. + return YAFFS_OK;
  5641. +
  5642. +}
  5643. +
  5644. +/* yaffs_del_file deletes the whole file data
  5645. + * and the inode associated with the file.
  5646. + * It does not delete the links associated with the file.
  5647. + */
  5648. +static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
  5649. +{
  5650. +
  5651. + int ret_val;
  5652. + int del_now = 0;
  5653. + struct yaffs_dev *dev = in->my_dev;
  5654. +
  5655. + if (!in->my_inode)
  5656. + del_now = 1;
  5657. +
  5658. + if (del_now) {
  5659. + ret_val =
  5660. + yaffs_change_obj_name(in, in->my_dev->del_dir,
  5661. + _Y("deleted"), 0, 0);
  5662. + T(YAFFS_TRACE_TRACING,
  5663. + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
  5664. + in->obj_id));
  5665. + in->deleted = 1;
  5666. + in->my_dev->n_deleted_files++;
  5667. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5668. + yaffs_resize_file(in, 0);
  5669. + yaffs_soft_del_file(in);
  5670. + } else {
  5671. + ret_val =
  5672. + yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
  5673. + _Y("unlinked"), 0, 0);
  5674. + }
  5675. +
  5676. + return ret_val;
  5677. +}
  5678. +
  5679. +int yaffs_del_file(struct yaffs_obj *in)
  5680. +{
  5681. + int ret_val = YAFFS_OK;
  5682. + int deleted; /* Need to cache value on stack if in is freed */
  5683. + struct yaffs_dev *dev = in->my_dev;
  5684. +
  5685. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5686. + yaffs_resize_file(in, 0);
  5687. +
  5688. + if (in->n_data_chunks > 0) {
  5689. + /* Use soft deletion if there is data in the file.
  5690. + * That won't be the case if it has been resized to zero.
  5691. + */
  5692. + if (!in->unlinked)
  5693. + ret_val = yaffs_unlink_file_if_needed(in);
  5694. +
  5695. + deleted = in->deleted;
  5696. +
  5697. + if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
  5698. + in->deleted = 1;
  5699. + deleted = 1;
  5700. + in->my_dev->n_deleted_files++;
  5701. + yaffs_soft_del_file(in);
  5702. + }
  5703. + return deleted ? YAFFS_OK : YAFFS_FAIL;
  5704. + } else {
  5705. + /* The file has no data chunks so we toss it immediately */
  5706. + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
  5707. + in->variant.file_variant.top = NULL;
  5708. + yaffs_generic_obj_del(in);
  5709. +
  5710. + return YAFFS_OK;
  5711. + }
  5712. +}
  5713. +
  5714. +static int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
  5715. +{
  5716. + return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
  5717. + !(list_empty(&obj->variant.dir_variant.children));
  5718. +}
  5719. +
  5720. +static int yaffs_del_dir(struct yaffs_obj *obj)
  5721. +{
  5722. + /* First check that the directory is empty. */
  5723. + if (yaffs_is_non_empty_dir(obj))
  5724. + return YAFFS_FAIL;
  5725. +
  5726. + return yaffs_generic_obj_del(obj);
  5727. +}
  5728. +
  5729. +static int yaffs_del_symlink(struct yaffs_obj *in)
  5730. +{
  5731. + if (in->variant.symlink_variant.alias)
  5732. + YFREE(in->variant.symlink_variant.alias);
  5733. + in->variant.symlink_variant.alias = NULL;
  5734. +
  5735. + return yaffs_generic_obj_del(in);
  5736. +}
  5737. +
  5738. +static int yaffs_del_link(struct yaffs_obj *in)
  5739. +{
  5740. + /* remove this hardlink from the list assocaited with the equivalent
  5741. + * object
  5742. + */
  5743. + list_del_init(&in->hard_links);
  5744. + return yaffs_generic_obj_del(in);
  5745. +}
  5746. +
  5747. +int yaffs_del_obj(struct yaffs_obj *obj)
  5748. +{
  5749. + int ret_val = -1;
  5750. + switch (obj->variant_type) {
  5751. + case YAFFS_OBJECT_TYPE_FILE:
  5752. + ret_val = yaffs_del_file(obj);
  5753. + break;
  5754. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5755. + if (!list_empty(&obj->variant.dir_variant.dirty)) {
  5756. + T(YAFFS_TRACE_BACKGROUND,
  5757. + (TSTR
  5758. + ("Remove object %d from dirty directories" TENDSTR),
  5759. + obj->obj_id));
  5760. + list_del_init(&obj->variant.dir_variant.dirty);
  5761. + }
  5762. + return yaffs_del_dir(obj);
  5763. + break;
  5764. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5765. + ret_val = yaffs_del_symlink(obj);
  5766. + break;
  5767. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5768. + ret_val = yaffs_del_link(obj);
  5769. + break;
  5770. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5771. + ret_val = yaffs_generic_obj_del(obj);
  5772. + break;
  5773. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5774. + ret_val = 0;
  5775. + break; /* should not happen. */
  5776. + }
  5777. +
  5778. + return ret_val;
  5779. +}
  5780. +
  5781. +static int yaffs_unlink_worker(struct yaffs_obj *obj)
  5782. +{
  5783. +
  5784. + int del_now = 0;
  5785. +
  5786. + if (!obj->my_inode)
  5787. + del_now = 1;
  5788. +
  5789. + if (obj)
  5790. + yaffs_update_parent(obj->parent);
  5791. +
  5792. + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  5793. + return yaffs_del_link(obj);
  5794. + } else if (!list_empty(&obj->hard_links)) {
  5795. + /* Curve ball: We're unlinking an object that has a hardlink.
  5796. + *
  5797. + * This problem arises because we are not strictly following
  5798. + * The Linux link/inode model.
  5799. + *
  5800. + * We can't really delete the object.
  5801. + * Instead, we do the following:
  5802. + * - Select a hardlink.
  5803. + * - Unhook it from the hard links
  5804. + * - Move it from its parent directory (so that the rename can work)
  5805. + * - Rename the object to the hardlink's name.
  5806. + * - Delete the hardlink
  5807. + */
  5808. +
  5809. + struct yaffs_obj *hl;
  5810. + struct yaffs_obj *parent;
  5811. + int ret_val;
  5812. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  5813. +
  5814. + hl = list_entry(obj->hard_links.next, struct yaffs_obj,
  5815. + hard_links);
  5816. +
  5817. + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  5818. + parent = hl->parent;
  5819. +
  5820. + list_del_init(&hl->hard_links);
  5821. +
  5822. + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
  5823. +
  5824. + ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
  5825. +
  5826. + if (ret_val == YAFFS_OK)
  5827. + ret_val = yaffs_generic_obj_del(hl);
  5828. +
  5829. + return ret_val;
  5830. +
  5831. + } else if (del_now) {
  5832. + switch (obj->variant_type) {
  5833. + case YAFFS_OBJECT_TYPE_FILE:
  5834. + return yaffs_del_file(obj);
  5835. + break;
  5836. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5837. + list_del_init(&obj->variant.dir_variant.dirty);
  5838. + return yaffs_del_dir(obj);
  5839. + break;
  5840. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5841. + return yaffs_del_symlink(obj);
  5842. + break;
  5843. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5844. + return yaffs_generic_obj_del(obj);
  5845. + break;
  5846. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5847. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5848. + default:
  5849. + return YAFFS_FAIL;
  5850. + }
  5851. + } else if (yaffs_is_non_empty_dir(obj)) {
  5852. + return YAFFS_FAIL;
  5853. + } else {
  5854. + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
  5855. + _Y("unlinked"), 0, 0);
  5856. + }
  5857. +}
  5858. +
  5859. +static int yaffs_unlink_obj(struct yaffs_obj *obj)
  5860. +{
  5861. +
  5862. + if (obj && obj->unlink_allowed)
  5863. + return yaffs_unlink_worker(obj);
  5864. +
  5865. + return YAFFS_FAIL;
  5866. +
  5867. +}
  5868. +
  5869. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
  5870. +{
  5871. + struct yaffs_obj *obj;
  5872. +
  5873. + obj = yaffs_find_by_name(dir, name);
  5874. + return yaffs_unlink_obj(obj);
  5875. +}
  5876. +
  5877. +/*----------------------- Initialisation Scanning ---------------------- */
  5878. +
  5879. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  5880. + int backward_scanning)
  5881. +{
  5882. + struct yaffs_obj *obj;
  5883. +
  5884. + if (!backward_scanning) {
  5885. + /* Handle YAFFS1 forward scanning case
  5886. + * For YAFFS1 we always do the deletion
  5887. + */
  5888. +
  5889. + } else {
  5890. + /* Handle YAFFS2 case (backward scanning)
  5891. + * If the shadowed object exists then ignore.
  5892. + */
  5893. + obj = yaffs_find_by_number(dev, obj_id);
  5894. + if (obj)
  5895. + return;
  5896. + }
  5897. +
  5898. + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  5899. + * We put it in unlinked dir to be cleaned up after the scanning
  5900. + */
  5901. + obj =
  5902. + yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
  5903. + if (!obj)
  5904. + return;
  5905. + obj->is_shadowed = 1;
  5906. + yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
  5907. + obj->variant.file_variant.shrink_size = 0;
  5908. + obj->valid = 1; /* So that we don't read any other info for this file */
  5909. +
  5910. +}
  5911. +
  5912. +void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
  5913. +{
  5914. + struct yaffs_obj *hl;
  5915. + struct yaffs_obj *in;
  5916. +
  5917. + while (hard_list) {
  5918. + hl = hard_list;
  5919. + hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
  5920. +
  5921. + in = yaffs_find_by_number(dev,
  5922. + hl->variant.
  5923. + hardlink_variant.equiv_id);
  5924. +
  5925. + if (in) {
  5926. + /* Add the hardlink pointers */
  5927. + hl->variant.hardlink_variant.equiv_obj = in;
  5928. + list_add(&hl->hard_links, &in->hard_links);
  5929. + } else {
  5930. + /* Todo Need to report/handle this better.
  5931. + * Got a problem... hardlink to a non-existant object
  5932. + */
  5933. + hl->variant.hardlink_variant.equiv_obj = NULL;
  5934. + INIT_LIST_HEAD(&hl->hard_links);
  5935. +
  5936. + }
  5937. + }
  5938. +}
  5939. +
  5940. +static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
  5941. +{
  5942. + /*
  5943. + * Sort out state of unlinked and deleted objects after scanning.
  5944. + */
  5945. + struct list_head *i;
  5946. + struct list_head *n;
  5947. + struct yaffs_obj *l;
  5948. +
  5949. + if (dev->read_only)
  5950. + return;
  5951. +
  5952. + /* Soft delete all the unlinked files */
  5953. + list_for_each_safe(i, n,
  5954. + &dev->unlinked_dir->variant.dir_variant.children) {
  5955. + if (i) {
  5956. + l = list_entry(i, struct yaffs_obj, siblings);
  5957. + yaffs_del_obj(l);
  5958. + }
  5959. + }
  5960. +
  5961. + list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
  5962. + if (i) {
  5963. + l = list_entry(i, struct yaffs_obj, siblings);
  5964. + yaffs_del_obj(l);
  5965. + }
  5966. + }
  5967. +
  5968. +}
  5969. +
  5970. +/*
  5971. + * This code iterates through all the objects making sure that they are rooted.
  5972. + * Any unrooted objects are re-rooted in lost+found.
  5973. + * An object needs to be in one of:
  5974. + * - Directly under deleted, unlinked
  5975. + * - Directly or indirectly under root.
  5976. + *
  5977. + * Note:
  5978. + * This code assumes that we don't ever change the current relationships between
  5979. + * directories:
  5980. + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
  5981. + * lost-n-found->parent == root_dir
  5982. + *
  5983. + * This fixes the problem where directories might have inadvertently been deleted
  5984. + * leaving the object "hanging" without being rooted in the directory tree.
  5985. + */
  5986. +
  5987. +static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
  5988. +{
  5989. + return (obj == dev->del_dir ||
  5990. + obj == dev->unlinked_dir || obj == dev->root_dir);
  5991. +}
  5992. +
  5993. +static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
  5994. +{
  5995. + struct yaffs_obj *obj;
  5996. + struct yaffs_obj *parent;
  5997. + int i;
  5998. + struct list_head *lh;
  5999. + struct list_head *n;
  6000. + int depth_limit;
  6001. + int hanging;
  6002. +
  6003. + if (dev->read_only)
  6004. + return;
  6005. +
  6006. + /* Iterate through the objects in each hash entry,
  6007. + * looking at each object.
  6008. + * Make sure it is rooted.
  6009. + */
  6010. +
  6011. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  6012. + list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
  6013. + if (lh) {
  6014. + obj =
  6015. + list_entry(lh, struct yaffs_obj, hash_link);
  6016. + parent = obj->parent;
  6017. +
  6018. + if (yaffs_has_null_parent(dev, obj)) {
  6019. + /* These directories are not hanging */
  6020. + hanging = 0;
  6021. + } else if (!parent
  6022. + || parent->variant_type !=
  6023. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  6024. + hanging = 1;
  6025. + } else if (yaffs_has_null_parent(dev, parent)) {
  6026. + hanging = 0;
  6027. + } else {
  6028. + /*
  6029. + * Need to follow the parent chain to see if it is hanging.
  6030. + */
  6031. + hanging = 0;
  6032. + depth_limit = 100;
  6033. +
  6034. + while (parent != dev->root_dir &&
  6035. + parent->parent &&
  6036. + parent->parent->variant_type ==
  6037. + YAFFS_OBJECT_TYPE_DIRECTORY
  6038. + && depth_limit > 0) {
  6039. + parent = parent->parent;
  6040. + depth_limit--;
  6041. + }
  6042. + if (parent != dev->root_dir)
  6043. + hanging = 1;
  6044. + }
  6045. + if (hanging) {
  6046. + T(YAFFS_TRACE_SCAN,
  6047. + (TSTR
  6048. + ("Hanging object %d moved to lost and found"
  6049. + TENDSTR), obj->obj_id));
  6050. + yaffs_add_obj_to_dir(dev->lost_n_found,
  6051. + obj);
  6052. + }
  6053. + }
  6054. + }
  6055. + }
  6056. +}
  6057. +
  6058. +/*
  6059. + * Delete directory contents for cleaning up lost and found.
  6060. + */
  6061. +static void yaffs_del_dir_contents(struct yaffs_obj *dir)
  6062. +{
  6063. + struct yaffs_obj *obj;
  6064. + struct list_head *lh;
  6065. + struct list_head *n;
  6066. +
  6067. + if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  6068. + YBUG();
  6069. +
  6070. + list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
  6071. + if (lh) {
  6072. + obj = list_entry(lh, struct yaffs_obj, siblings);
  6073. + if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  6074. + yaffs_del_dir_contents(obj);
  6075. +
  6076. + T(YAFFS_TRACE_SCAN,
  6077. + (TSTR("Deleting lost_found object %d" TENDSTR),
  6078. + obj->obj_id));
  6079. +
  6080. + /* Need to use UnlinkObject since Delete would not handle
  6081. + * hardlinked objects correctly.
  6082. + */
  6083. + yaffs_unlink_obj(obj);
  6084. + }
  6085. + }
  6086. +
  6087. +}
  6088. +
  6089. +static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
  6090. +{
  6091. + yaffs_del_dir_contents(dev->lost_n_found);
  6092. +}
  6093. +
  6094. +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
  6095. +{
  6096. + u8 *chunk_data;
  6097. + struct yaffs_obj_hdr *oh;
  6098. + struct yaffs_dev *dev;
  6099. + struct yaffs_ext_tags tags;
  6100. + int result;
  6101. + int alloc_failed = 0;
  6102. +
  6103. + if (!in)
  6104. + return;
  6105. +
  6106. + dev = in->my_dev;
  6107. +
  6108. + if (in->lazy_loaded && in->hdr_chunk > 0) {
  6109. + in->lazy_loaded = 0;
  6110. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  6111. +
  6112. + result =
  6113. + yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
  6114. + &tags);
  6115. + oh = (struct yaffs_obj_hdr *)chunk_data;
  6116. +
  6117. + in->yst_mode = oh->yst_mode;
  6118. + yaffs_load_attribs(in, oh);
  6119. + yaffs_set_obj_name_from_oh(in, oh);
  6120. +
  6121. + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6122. + in->variant.symlink_variant.alias =
  6123. + yaffs_clone_str(oh->alias);
  6124. + if (!in->variant.symlink_variant.alias)
  6125. + alloc_failed = 1; /* Not returned to caller */
  6126. + }
  6127. +
  6128. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  6129. + }
  6130. +}
  6131. +
  6132. +/*------------------------------ Directory Functions ----------------------------- */
  6133. +
  6134. +/*
  6135. + *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
  6136. + * link (ie. name) is created or deleted in the directory.
  6137. + *
  6138. + * ie.
  6139. + * create dir/a : update dir's mtime/ctime
  6140. + * rm dir/a: update dir's mtime/ctime
  6141. + * modify dir/a: don't update dir's mtimme/ctime
  6142. + *
  6143. + * This can be handled immediately or defered. Defering helps reduce the number
  6144. + * of updates when many files in a directory are changed within a brief period.
  6145. + *
  6146. + * If the directory updating is defered then yaffs_update_dirty_dirs must be
  6147. + * called periodically.
  6148. + */
  6149. +
  6150. +static void yaffs_update_parent(struct yaffs_obj *obj)
  6151. +{
  6152. + struct yaffs_dev *dev;
  6153. + if (!obj)
  6154. + return;
  6155. + dev = obj->my_dev;
  6156. + obj->dirty = 1;
  6157. + yaffs_load_current_time(obj, 0, 1);
  6158. + if (dev->param.defered_dir_update) {
  6159. + struct list_head *link = &obj->variant.dir_variant.dirty;
  6160. +
  6161. + if (list_empty(link)) {
  6162. + list_add(link, &dev->dirty_dirs);
  6163. + T(YAFFS_TRACE_BACKGROUND,
  6164. + (TSTR("Added object %d to dirty directories" TENDSTR),
  6165. + obj->obj_id));
  6166. + }
  6167. +
  6168. + } else {
  6169. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  6170. + }
  6171. +}
  6172. +
  6173. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  6174. +{
  6175. + struct list_head *link;
  6176. + struct yaffs_obj *obj;
  6177. + struct yaffs_dir_var *d_s;
  6178. + union yaffs_obj_var *o_v;
  6179. +
  6180. + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
  6181. +
  6182. + while (!list_empty(&dev->dirty_dirs)) {
  6183. + link = dev->dirty_dirs.next;
  6184. + list_del_init(link);
  6185. +
  6186. + d_s = list_entry(link, struct yaffs_dir_var, dirty);
  6187. + o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  6188. + obj = list_entry(o_v, struct yaffs_obj, variant);
  6189. +
  6190. + T(YAFFS_TRACE_BACKGROUND,
  6191. + (TSTR("Update directory %d" TENDSTR), obj->obj_id));
  6192. +
  6193. + if (obj->dirty)
  6194. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  6195. + }
  6196. +}
  6197. +
  6198. +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  6199. +{
  6200. + struct yaffs_dev *dev = obj->my_dev;
  6201. + struct yaffs_obj *parent;
  6202. +
  6203. + yaffs_verify_obj_in_dir(obj);
  6204. + parent = obj->parent;
  6205. +
  6206. + yaffs_verify_dir(parent);
  6207. +
  6208. + if (dev && dev->param.remove_obj_fn)
  6209. + dev->param.remove_obj_fn(obj);
  6210. +
  6211. + list_del_init(&obj->siblings);
  6212. + obj->parent = NULL;
  6213. +
  6214. + yaffs_verify_dir(parent);
  6215. +}
  6216. +
  6217. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  6218. +{
  6219. + if (!directory) {
  6220. + T(YAFFS_TRACE_ALWAYS,
  6221. + (TSTR
  6222. + ("tragedy: Trying to add an object to a null pointer directory"
  6223. + TENDSTR)));
  6224. + YBUG();
  6225. + return;
  6226. + }
  6227. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6228. + T(YAFFS_TRACE_ALWAYS,
  6229. + (TSTR
  6230. + ("tragedy: Trying to add an object to a non-directory"
  6231. + TENDSTR)));
  6232. + YBUG();
  6233. + }
  6234. +
  6235. + if (obj->siblings.prev == NULL) {
  6236. + /* Not initialised */
  6237. + YBUG();
  6238. + }
  6239. +
  6240. + yaffs_verify_dir(directory);
  6241. +
  6242. + yaffs_remove_obj_from_dir(obj);
  6243. +
  6244. + /* Now add it */
  6245. + list_add(&obj->siblings, &directory->variant.dir_variant.children);
  6246. + obj->parent = directory;
  6247. +
  6248. + if (directory == obj->my_dev->unlinked_dir
  6249. + || directory == obj->my_dev->del_dir) {
  6250. + obj->unlinked = 1;
  6251. + obj->my_dev->n_unlinked_files++;
  6252. + obj->rename_allowed = 0;
  6253. + }
  6254. +
  6255. + yaffs_verify_dir(directory);
  6256. + yaffs_verify_obj_in_dir(obj);
  6257. +}
  6258. +
  6259. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
  6260. + const YCHAR * name)
  6261. +{
  6262. + int sum;
  6263. +
  6264. + struct list_head *i;
  6265. + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  6266. +
  6267. + struct yaffs_obj *l;
  6268. +
  6269. + if (!name)
  6270. + return NULL;
  6271. +
  6272. + if (!directory) {
  6273. + T(YAFFS_TRACE_ALWAYS,
  6274. + (TSTR
  6275. + ("tragedy: yaffs_find_by_name: null pointer directory"
  6276. + TENDSTR)));
  6277. + YBUG();
  6278. + return NULL;
  6279. + }
  6280. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6281. + T(YAFFS_TRACE_ALWAYS,
  6282. + (TSTR("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
  6283. + YBUG();
  6284. + }
  6285. +
  6286. + sum = yaffs_calc_name_sum(name);
  6287. +
  6288. + list_for_each(i, &directory->variant.dir_variant.children) {
  6289. + if (i) {
  6290. + l = list_entry(i, struct yaffs_obj, siblings);
  6291. +
  6292. + if (l->parent != directory)
  6293. + YBUG();
  6294. +
  6295. + yaffs_check_obj_details_loaded(l);
  6296. +
  6297. + /* Special case for lost-n-found */
  6298. + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6299. + if (!yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME))
  6300. + return l;
  6301. + } else if (yaffs_sum_cmp(l->sum, sum)
  6302. + || l->hdr_chunk <= 0) {
  6303. + /* LostnFound chunk called Objxxx
  6304. + * Do a real check
  6305. + */
  6306. + yaffs_get_obj_name(l, buffer,
  6307. + YAFFS_MAX_NAME_LENGTH + 1);
  6308. + if (yaffs_strncmp
  6309. + (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
  6310. + return l;
  6311. + }
  6312. + }
  6313. + }
  6314. +
  6315. + return NULL;
  6316. +}
  6317. +
  6318. +/* GetEquivalentObject dereferences any hard links to get to the
  6319. + * actual object.
  6320. + */
  6321. +
  6322. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
  6323. +{
  6324. + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  6325. + /* We want the object id of the equivalent object, not this one */
  6326. + obj = obj->variant.hardlink_variant.equiv_obj;
  6327. + yaffs_check_obj_details_loaded(obj);
  6328. + }
  6329. + return obj;
  6330. +}
  6331. +
  6332. +/*
  6333. + * A note or two on object names.
  6334. + * * If the object name is missing, we then make one up in the form objnnn
  6335. + *
  6336. + * * ASCII names are stored in the object header's name field from byte zero
  6337. + * * Unicode names are historically stored starting from byte zero.
  6338. + *
  6339. + * Then there are automatic Unicode names...
  6340. + * The purpose of these is to save names in a way that can be read as
  6341. + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
  6342. + * system to share files.
  6343. + *
  6344. + * These automatic unicode are stored slightly differently...
  6345. + * - If the name can fit in the ASCII character space then they are saved as
  6346. + * ascii names as per above.
  6347. + * - If the name needs Unicode then the name is saved in Unicode
  6348. + * starting at oh->name[1].
  6349. +
  6350. + */
  6351. +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
  6352. + int buffer_size)
  6353. +{
  6354. + /* Create an object name if we could not find one. */
  6355. + if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
  6356. + YCHAR local_name[20];
  6357. + YCHAR num_string[20];
  6358. + YCHAR *x = &num_string[19];
  6359. + unsigned v = obj->obj_id;
  6360. + num_string[19] = 0;
  6361. + while (v > 0) {
  6362. + x--;
  6363. + *x = '0' + (v % 10);
  6364. + v /= 10;
  6365. + }
  6366. + /* make up a name */
  6367. + yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
  6368. + yaffs_strcat(local_name, x);
  6369. + yaffs_strncpy(name, local_name, buffer_size - 1);
  6370. + }
  6371. +}
  6372. +
  6373. +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
  6374. + const YCHAR * oh_name, int buff_size)
  6375. +{
  6376. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  6377. + if (dev->param.auto_unicode) {
  6378. + if (*oh_name) {
  6379. + /* It is an ASCII name, so do an ASCII to unicode conversion */
  6380. + const char *ascii_oh_name = (const char *)oh_name;
  6381. + int n = buff_size - 1;
  6382. + while (n > 0 && *ascii_oh_name) {
  6383. + *name = *ascii_oh_name;
  6384. + name++;
  6385. + ascii_oh_name++;
  6386. + n--;
  6387. + }
  6388. + } else {
  6389. + yaffs_strncpy(name, oh_name + 1, buff_size - 1);
  6390. + }
  6391. + } else {
  6392. +#else
  6393. + {
  6394. +#endif
  6395. + yaffs_strncpy(name, oh_name, buff_size - 1);
  6396. + }
  6397. +}
  6398. +
  6399. +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
  6400. + const YCHAR * name)
  6401. +{
  6402. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  6403. +
  6404. + int is_ascii;
  6405. + YCHAR *w;
  6406. +
  6407. + if (dev->param.auto_unicode) {
  6408. +
  6409. + is_ascii = 1;
  6410. + w = name;
  6411. +
  6412. + /* Figure out if the name will fit in ascii character set */
  6413. + while (is_ascii && *w) {
  6414. + if ((*w) & 0xff00)
  6415. + is_ascii = 0;
  6416. + w++;
  6417. + }
  6418. +
  6419. + if (is_ascii) {
  6420. + /* It is an ASCII name, so do a unicode to ascii conversion */
  6421. + char *ascii_oh_name = (char *)oh_name;
  6422. + int n = YAFFS_MAX_NAME_LENGTH - 1;
  6423. + while (n > 0 && *name) {
  6424. + *ascii_oh_name = *name;
  6425. + name++;
  6426. + ascii_oh_name++;
  6427. + n--;
  6428. + }
  6429. + } else {
  6430. + /* It is a unicode name, so save starting at the second YCHAR */
  6431. + *oh_name = 0;
  6432. + yaffs_strncpy(oh_name + 1, name,
  6433. + YAFFS_MAX_NAME_LENGTH - 2);
  6434. + }
  6435. + } else {
  6436. +#else
  6437. + {
  6438. +#endif
  6439. + yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
  6440. + }
  6441. +
  6442. +}
  6443. +
  6444. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
  6445. +{
  6446. + memset(name, 0, buffer_size * sizeof(YCHAR));
  6447. +
  6448. + yaffs_check_obj_details_loaded(obj);
  6449. +
  6450. + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6451. + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
  6452. + }
  6453. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  6454. + else if (obj->short_name[0]) {
  6455. + yaffs_strcpy(name, obj->short_name);
  6456. + }
  6457. +#endif
  6458. + else if (obj->hdr_chunk > 0) {
  6459. + int result;
  6460. + u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
  6461. +
  6462. + struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
  6463. +
  6464. + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
  6465. +
  6466. + if (obj->hdr_chunk > 0) {
  6467. + result = yaffs_rd_chunk_tags_nand(obj->my_dev,
  6468. + obj->hdr_chunk,
  6469. + buffer, NULL);
  6470. + }
  6471. + yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
  6472. + buffer_size);
  6473. +
  6474. + yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
  6475. + }
  6476. +
  6477. + yaffs_fix_null_name(obj, name, buffer_size);
  6478. +
  6479. + return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH);
  6480. +}
  6481. +
  6482. +int yaffs_get_obj_length(struct yaffs_obj *obj)
  6483. +{
  6484. + /* Dereference any hard linking */
  6485. + obj = yaffs_get_equivalent_obj(obj);
  6486. +
  6487. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  6488. + return obj->variant.file_variant.file_size;
  6489. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6490. + if (!obj->variant.symlink_variant.alias)
  6491. + return 0;
  6492. + return yaffs_strnlen(obj->variant.symlink_variant.alias,
  6493. + YAFFS_MAX_ALIAS_LENGTH);
  6494. + } else {
  6495. + /* Only a directory should drop through to here */
  6496. + return obj->my_dev->data_bytes_per_chunk;
  6497. + }
  6498. +}
  6499. +
  6500. +int yaffs_get_obj_link_count(struct yaffs_obj *obj)
  6501. +{
  6502. + int count = 0;
  6503. + struct list_head *i;
  6504. +
  6505. + if (!obj->unlinked)
  6506. + count++; /* the object itself */
  6507. +
  6508. + list_for_each(i, &obj->hard_links)
  6509. + count++; /* add the hard links; */
  6510. +
  6511. + return count;
  6512. +}
  6513. +
  6514. +int yaffs_get_obj_inode(struct yaffs_obj *obj)
  6515. +{
  6516. + obj = yaffs_get_equivalent_obj(obj);
  6517. +
  6518. + return obj->obj_id;
  6519. +}
  6520. +
  6521. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
  6522. +{
  6523. + obj = yaffs_get_equivalent_obj(obj);
  6524. +
  6525. + switch (obj->variant_type) {
  6526. + case YAFFS_OBJECT_TYPE_FILE:
  6527. + return DT_REG;
  6528. + break;
  6529. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6530. + return DT_DIR;
  6531. + break;
  6532. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6533. + return DT_LNK;
  6534. + break;
  6535. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6536. + return DT_REG;
  6537. + break;
  6538. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6539. + if (S_ISFIFO(obj->yst_mode))
  6540. + return DT_FIFO;
  6541. + if (S_ISCHR(obj->yst_mode))
  6542. + return DT_CHR;
  6543. + if (S_ISBLK(obj->yst_mode))
  6544. + return DT_BLK;
  6545. + if (S_ISSOCK(obj->yst_mode))
  6546. + return DT_SOCK;
  6547. + default:
  6548. + return DT_REG;
  6549. + break;
  6550. + }
  6551. +}
  6552. +
  6553. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
  6554. +{
  6555. + obj = yaffs_get_equivalent_obj(obj);
  6556. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  6557. + return yaffs_clone_str(obj->variant.symlink_variant.alias);
  6558. + else
  6559. + return yaffs_clone_str(_Y(""));
  6560. +}
  6561. +
  6562. +static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
  6563. + const YCHAR * name, const void *value, int size,
  6564. + int flags)
  6565. +{
  6566. + struct yaffs_xattr_mod xmod;
  6567. +
  6568. + int result;
  6569. +
  6570. + xmod.set = set;
  6571. + xmod.name = name;
  6572. + xmod.data = value;
  6573. + xmod.size = size;
  6574. + xmod.flags = flags;
  6575. + xmod.result = -ENOSPC;
  6576. +
  6577. + result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
  6578. +
  6579. + if (result > 0)
  6580. + return xmod.result;
  6581. + else
  6582. + return -ENOSPC;
  6583. +}
  6584. +
  6585. +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  6586. + struct yaffs_xattr_mod *xmod)
  6587. +{
  6588. + int retval = 0;
  6589. + int x_offs = sizeof(struct yaffs_obj_hdr);
  6590. + struct yaffs_dev *dev = obj->my_dev;
  6591. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  6592. +
  6593. + char *x_buffer = buffer + x_offs;
  6594. +
  6595. + if (xmod->set)
  6596. + retval =
  6597. + nval_set(x_buffer, x_size, xmod->name, xmod->data,
  6598. + xmod->size, xmod->flags);
  6599. + else
  6600. + retval = nval_del(x_buffer, x_size, xmod->name);
  6601. +
  6602. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  6603. + obj->xattr_known = 1;
  6604. +
  6605. + xmod->result = retval;
  6606. +
  6607. + return retval;
  6608. +}
  6609. +
  6610. +static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
  6611. + void *value, int size)
  6612. +{
  6613. + char *buffer = NULL;
  6614. + int result;
  6615. + struct yaffs_ext_tags tags;
  6616. + struct yaffs_dev *dev = obj->my_dev;
  6617. + int x_offs = sizeof(struct yaffs_obj_hdr);
  6618. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  6619. +
  6620. + char *x_buffer;
  6621. +
  6622. + int retval = 0;
  6623. +
  6624. + if (obj->hdr_chunk < 1)
  6625. + return -ENODATA;
  6626. +
  6627. + /* If we know that the object has no xattribs then don't do all the
  6628. + * reading and parsing.
  6629. + */
  6630. + if (obj->xattr_known && !obj->has_xattr) {
  6631. + if (name)
  6632. + return -ENODATA;
  6633. + else
  6634. + return 0;
  6635. + }
  6636. +
  6637. + buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
  6638. + if (!buffer)
  6639. + return -ENOMEM;
  6640. +
  6641. + result =
  6642. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
  6643. +
  6644. + if (result != YAFFS_OK)
  6645. + retval = -ENOENT;
  6646. + else {
  6647. + x_buffer = buffer + x_offs;
  6648. +
  6649. + if (!obj->xattr_known) {
  6650. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  6651. + obj->xattr_known = 1;
  6652. + }
  6653. +
  6654. + if (name)
  6655. + retval = nval_get(x_buffer, x_size, name, value, size);
  6656. + else
  6657. + retval = nval_list(x_buffer, x_size, value, size);
  6658. + }
  6659. + yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
  6660. + return retval;
  6661. +}
  6662. +
  6663. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  6664. + const void *value, int size, int flags)
  6665. +{
  6666. + return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
  6667. +}
  6668. +
  6669. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
  6670. +{
  6671. + return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
  6672. +}
  6673. +
  6674. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  6675. + int size)
  6676. +{
  6677. + return yaffs_do_xattrib_fetch(obj, name, value, size);
  6678. +}
  6679. +
  6680. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
  6681. +{
  6682. + return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
  6683. +}
  6684. +
  6685. +/*---------------------------- Initialisation code -------------------------------------- */
  6686. +
  6687. +static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
  6688. +{
  6689. +
  6690. + /* Common functions, gotta have */
  6691. + if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
  6692. + return 0;
  6693. +
  6694. +#ifdef CONFIG_YAFFS_YAFFS2
  6695. +
  6696. + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  6697. + if (dev->param.write_chunk_tags_fn &&
  6698. + dev->param.read_chunk_tags_fn &&
  6699. + !dev->param.write_chunk_fn &&
  6700. + !dev->param.read_chunk_fn &&
  6701. + dev->param.bad_block_fn && dev->param.query_block_fn)
  6702. + return 1;
  6703. +#endif
  6704. +
  6705. + /* Can use the "spare" style interface for yaffs1 */
  6706. + if (!dev->param.is_yaffs2 &&
  6707. + !dev->param.write_chunk_tags_fn &&
  6708. + !dev->param.read_chunk_tags_fn &&
  6709. + dev->param.write_chunk_fn &&
  6710. + dev->param.read_chunk_fn &&
  6711. + !dev->param.bad_block_fn && !dev->param.query_block_fn)
  6712. + return 1;
  6713. +
  6714. + return 0; /* bad */
  6715. +}
  6716. +
  6717. +static int yaffs_create_initial_dir(struct yaffs_dev *dev)
  6718. +{
  6719. + /* Initialise the unlinked, deleted, root and lost and found directories */
  6720. +
  6721. + dev->lost_n_found = dev->root_dir = NULL;
  6722. + dev->unlinked_dir = dev->del_dir = NULL;
  6723. +
  6724. + dev->unlinked_dir =
  6725. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  6726. +
  6727. + dev->del_dir =
  6728. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  6729. +
  6730. + dev->root_dir =
  6731. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
  6732. + YAFFS_ROOT_MODE | S_IFDIR);
  6733. + dev->lost_n_found =
  6734. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
  6735. + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  6736. +
  6737. + if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
  6738. + && dev->del_dir) {
  6739. + yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
  6740. + return YAFFS_OK;
  6741. + }
  6742. +
  6743. + return YAFFS_FAIL;
  6744. +}
  6745. +
  6746. +int yaffs_guts_initialise(struct yaffs_dev *dev)
  6747. +{
  6748. + int init_failed = 0;
  6749. + unsigned x;
  6750. + int bits;
  6751. +
  6752. + T(YAFFS_TRACE_TRACING,
  6753. + (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
  6754. +
  6755. + /* Check stuff that must be set */
  6756. +
  6757. + if (!dev) {
  6758. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
  6759. + return YAFFS_FAIL;
  6760. + }
  6761. +
  6762. + dev->internal_start_block = dev->param.start_block;
  6763. + dev->internal_end_block = dev->param.end_block;
  6764. + dev->block_offset = 0;
  6765. + dev->chunk_offset = 0;
  6766. + dev->n_free_chunks = 0;
  6767. +
  6768. + dev->gc_block = 0;
  6769. +
  6770. + if (dev->param.start_block == 0) {
  6771. + dev->internal_start_block = dev->param.start_block + 1;
  6772. + dev->internal_end_block = dev->param.end_block + 1;
  6773. + dev->block_offset = 1;
  6774. + dev->chunk_offset = dev->param.chunks_per_block;
  6775. + }
  6776. +
  6777. + /* Check geometry parameters. */
  6778. +
  6779. + 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 */
  6780. + T(YAFFS_TRACE_ALWAYS,
  6781. + (TSTR
  6782. + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
  6783. + TENDSTR), dev->param.total_bytes_per_chunk,
  6784. + dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
  6785. + return YAFFS_FAIL;
  6786. + }
  6787. +
  6788. + if (yaffs_init_nand(dev) != YAFFS_OK) {
  6789. + T(YAFFS_TRACE_ALWAYS,
  6790. + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
  6791. + return YAFFS_FAIL;
  6792. + }
  6793. +
  6794. + /* Sort out space for inband tags, if required */
  6795. + if (dev->param.inband_tags)
  6796. + dev->data_bytes_per_chunk =
  6797. + dev->param.total_bytes_per_chunk -
  6798. + sizeof(struct yaffs_packed_tags2_tags_only);
  6799. + else
  6800. + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
  6801. +
  6802. + /* Got the right mix of functions? */
  6803. + if (!yaffs_check_dev_fns(dev)) {
  6804. + /* Function missing */
  6805. + T(YAFFS_TRACE_ALWAYS,
  6806. + (TSTR
  6807. + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
  6808. +
  6809. + return YAFFS_FAIL;
  6810. + }
  6811. +
  6812. + if (dev->is_mounted) {
  6813. + T(YAFFS_TRACE_ALWAYS,
  6814. + (TSTR("yaffs: device already mounted\n" TENDSTR)));
  6815. + return YAFFS_FAIL;
  6816. + }
  6817. +
  6818. + /* Finished with most checks. One or two more checks happen later on too. */
  6819. +
  6820. + dev->is_mounted = 1;
  6821. +
  6822. + /* OK now calculate a few things for the device */
  6823. +
  6824. + /*
  6825. + * Calculate all the chunk size manipulation numbers:
  6826. + */
  6827. + x = dev->data_bytes_per_chunk;
  6828. + /* We always use dev->chunk_shift and dev->chunk_div */
  6829. + dev->chunk_shift = calc_shifts(x);
  6830. + x >>= dev->chunk_shift;
  6831. + dev->chunk_div = x;
  6832. + /* We only use chunk mask if chunk_div is 1 */
  6833. + dev->chunk_mask = (1 << dev->chunk_shift) - 1;
  6834. +
  6835. + /*
  6836. + * Calculate chunk_grp_bits.
  6837. + * We need to find the next power of 2 > than internal_end_block
  6838. + */
  6839. +
  6840. + x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
  6841. +
  6842. + bits = calc_shifts_ceiling(x);
  6843. +
  6844. + /* Set up tnode width if wide tnodes are enabled. */
  6845. + if (!dev->param.wide_tnodes_disabled) {
  6846. + /* bits must be even so that we end up with 32-bit words */
  6847. + if (bits & 1)
  6848. + bits++;
  6849. + if (bits < 16)
  6850. + dev->tnode_width = 16;
  6851. + else
  6852. + dev->tnode_width = bits;
  6853. + } else {
  6854. + dev->tnode_width = 16;
  6855. + }
  6856. +
  6857. + dev->tnode_mask = (1 << dev->tnode_width) - 1;
  6858. +
  6859. + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  6860. + * so if the bitwidth of the
  6861. + * chunk range we're using is greater than 16 we need
  6862. + * to figure out chunk shift and chunk_grp_size
  6863. + */
  6864. +
  6865. + if (bits <= dev->tnode_width)
  6866. + dev->chunk_grp_bits = 0;
  6867. + else
  6868. + dev->chunk_grp_bits = bits - dev->tnode_width;
  6869. +
  6870. + dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
  6871. + if (dev->tnode_size < sizeof(struct yaffs_tnode))
  6872. + dev->tnode_size = sizeof(struct yaffs_tnode);
  6873. +
  6874. + dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
  6875. +
  6876. + if (dev->param.chunks_per_block < dev->chunk_grp_size) {
  6877. + /* We have a problem because the soft delete won't work if
  6878. + * the chunk group size > chunks per block.
  6879. + * This can be remedied by using larger "virtual blocks".
  6880. + */
  6881. + T(YAFFS_TRACE_ALWAYS,
  6882. + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
  6883. +
  6884. + return YAFFS_FAIL;
  6885. + }
  6886. +
  6887. + /* OK, we've finished verifying the device, lets continue with initialisation */
  6888. +
  6889. + /* More device initialisation */
  6890. + dev->all_gcs = 0;
  6891. + dev->passive_gc_count = 0;
  6892. + dev->oldest_dirty_gc_count = 0;
  6893. + dev->bg_gcs = 0;
  6894. + dev->gc_block_finder = 0;
  6895. + dev->buffered_block = -1;
  6896. + dev->doing_buffered_block_rewrite = 0;
  6897. + dev->n_deleted_files = 0;
  6898. + dev->n_bg_deletions = 0;
  6899. + dev->n_unlinked_files = 0;
  6900. + dev->n_ecc_fixed = 0;
  6901. + dev->n_ecc_unfixed = 0;
  6902. + dev->n_tags_ecc_fixed = 0;
  6903. + dev->n_tags_ecc_unfixed = 0;
  6904. + dev->n_erase_failures = 0;
  6905. + dev->n_erased_blocks = 0;
  6906. + dev->gc_disable = 0;
  6907. + dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
  6908. + INIT_LIST_HEAD(&dev->dirty_dirs);
  6909. + dev->oldest_dirty_seq = 0;
  6910. + dev->oldest_dirty_block = 0;
  6911. +
  6912. + /* Initialise temporary buffers and caches. */
  6913. + if (!yaffs_init_tmp_buffers(dev))
  6914. + init_failed = 1;
  6915. +
  6916. + dev->cache = NULL;
  6917. + dev->gc_cleanup_list = NULL;
  6918. +
  6919. + if (!init_failed && dev->param.n_caches > 0) {
  6920. + int i;
  6921. + void *buf;
  6922. + int cache_bytes =
  6923. + dev->param.n_caches * sizeof(struct yaffs_cache);
  6924. +
  6925. + if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
  6926. + dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
  6927. +
  6928. + dev->cache = YMALLOC(cache_bytes);
  6929. +
  6930. + buf = (u8 *) dev->cache;
  6931. +
  6932. + if (dev->cache)
  6933. + memset(dev->cache, 0, cache_bytes);
  6934. +
  6935. + for (i = 0; i < dev->param.n_caches && buf; i++) {
  6936. + dev->cache[i].object = NULL;
  6937. + dev->cache[i].last_use = 0;
  6938. + dev->cache[i].dirty = 0;
  6939. + dev->cache[i].data = buf =
  6940. + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
  6941. + }
  6942. + if (!buf)
  6943. + init_failed = 1;
  6944. +
  6945. + dev->cache_last_use = 0;
  6946. + }
  6947. +
  6948. + dev->cache_hits = 0;
  6949. +
  6950. + if (!init_failed) {
  6951. + dev->gc_cleanup_list =
  6952. + YMALLOC(dev->param.chunks_per_block * sizeof(u32));
  6953. + if (!dev->gc_cleanup_list)
  6954. + init_failed = 1;
  6955. + }
  6956. +
  6957. + if (dev->param.is_yaffs2)
  6958. + dev->param.use_header_file_size = 1;
  6959. +
  6960. + if (!init_failed && !yaffs_init_blocks(dev))
  6961. + init_failed = 1;
  6962. +
  6963. + yaffs_init_tnodes_and_objs(dev);
  6964. +
  6965. + if (!init_failed && !yaffs_create_initial_dir(dev))
  6966. + init_failed = 1;
  6967. +
  6968. + if (!init_failed) {
  6969. + /* Now scan the flash. */
  6970. + if (dev->param.is_yaffs2) {
  6971. + if (yaffs2_checkpt_restore(dev)) {
  6972. + yaffs_check_obj_details_loaded(dev->root_dir);
  6973. + T(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  6974. + (TSTR
  6975. + ("yaffs: restored from checkpoint"
  6976. + TENDSTR)));
  6977. + } else {
  6978. +
  6979. + /* Clean up the mess caused by an aborted checkpoint load
  6980. + * and scan backwards.
  6981. + */
  6982. + yaffs_deinit_blocks(dev);
  6983. +
  6984. + yaffs_deinit_tnodes_and_objs(dev);
  6985. +
  6986. + dev->n_erased_blocks = 0;
  6987. + dev->n_free_chunks = 0;
  6988. + dev->alloc_block = -1;
  6989. + dev->alloc_page = -1;
  6990. + dev->n_deleted_files = 0;
  6991. + dev->n_unlinked_files = 0;
  6992. + dev->n_bg_deletions = 0;
  6993. +
  6994. + if (!init_failed && !yaffs_init_blocks(dev))
  6995. + init_failed = 1;
  6996. +
  6997. + yaffs_init_tnodes_and_objs(dev);
  6998. +
  6999. + if (!init_failed
  7000. + && !yaffs_create_initial_dir(dev))
  7001. + init_failed = 1;
  7002. +
  7003. + if (!init_failed && !yaffs2_scan_backwards(dev))
  7004. + init_failed = 1;
  7005. + }
  7006. + } else if (!yaffs1_scan(dev)) {
  7007. + init_failed = 1;
  7008. + }
  7009. +
  7010. + yaffs_strip_deleted_objs(dev);
  7011. + yaffs_fix_hanging_objs(dev);
  7012. + if (dev->param.empty_lost_n_found)
  7013. + yaffs_empty_l_n_f(dev);
  7014. + }
  7015. +
  7016. + if (init_failed) {
  7017. + /* Clean up the mess */
  7018. + T(YAFFS_TRACE_TRACING,
  7019. + (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
  7020. +
  7021. + yaffs_deinitialise(dev);
  7022. + return YAFFS_FAIL;
  7023. + }
  7024. +
  7025. + /* Zero out stats */
  7026. + dev->n_page_reads = 0;
  7027. + dev->n_page_writes = 0;
  7028. + dev->n_erasures = 0;
  7029. + dev->n_gc_copies = 0;
  7030. + dev->n_retired_writes = 0;
  7031. +
  7032. + dev->n_retired_blocks = 0;
  7033. +
  7034. + yaffs_verify_free_chunks(dev);
  7035. + yaffs_verify_blocks(dev);
  7036. +
  7037. + /* Clean up any aborted checkpoint data */
  7038. + if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
  7039. + yaffs2_checkpt_invalidate(dev);
  7040. +
  7041. + T(YAFFS_TRACE_TRACING,
  7042. + (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
  7043. + return YAFFS_OK;
  7044. +
  7045. +}
  7046. +
  7047. +void yaffs_deinitialise(struct yaffs_dev *dev)
  7048. +{
  7049. + if (dev->is_mounted) {
  7050. + int i;
  7051. +
  7052. + yaffs_deinit_blocks(dev);
  7053. + yaffs_deinit_tnodes_and_objs(dev);
  7054. + if (dev->param.n_caches > 0 && dev->cache) {
  7055. +
  7056. + for (i = 0; i < dev->param.n_caches; i++) {
  7057. + if (dev->cache[i].data)
  7058. + YFREE(dev->cache[i].data);
  7059. + dev->cache[i].data = NULL;
  7060. + }
  7061. +
  7062. + YFREE(dev->cache);
  7063. + dev->cache = NULL;
  7064. + }
  7065. +
  7066. + YFREE(dev->gc_cleanup_list);
  7067. +
  7068. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  7069. + YFREE(dev->temp_buffer[i].buffer);
  7070. +
  7071. + dev->is_mounted = 0;
  7072. +
  7073. + if (dev->param.deinitialise_flash_fn)
  7074. + dev->param.deinitialise_flash_fn(dev);
  7075. + }
  7076. +}
  7077. +
  7078. +int yaffs_count_free_chunks(struct yaffs_dev *dev)
  7079. +{
  7080. + int n_free = 0;
  7081. + int b;
  7082. +
  7083. + struct yaffs_block_info *blk;
  7084. +
  7085. + blk = dev->block_info;
  7086. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  7087. + switch (blk->block_state) {
  7088. + case YAFFS_BLOCK_STATE_EMPTY:
  7089. + case YAFFS_BLOCK_STATE_ALLOCATING:
  7090. + case YAFFS_BLOCK_STATE_COLLECTING:
  7091. + case YAFFS_BLOCK_STATE_FULL:
  7092. + n_free +=
  7093. + (dev->param.chunks_per_block - blk->pages_in_use +
  7094. + blk->soft_del_pages);
  7095. + break;
  7096. + default:
  7097. + break;
  7098. + }
  7099. + blk++;
  7100. + }
  7101. +
  7102. + return n_free;
  7103. +}
  7104. +
  7105. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
  7106. +{
  7107. + /* This is what we report to the outside world */
  7108. +
  7109. + int n_free;
  7110. + int n_dirty_caches;
  7111. + int blocks_for_checkpt;
  7112. + int i;
  7113. +
  7114. + n_free = dev->n_free_chunks;
  7115. + n_free += dev->n_deleted_files;
  7116. +
  7117. + /* Now count the number of dirty chunks in the cache and subtract those */
  7118. +
  7119. + for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
  7120. + if (dev->cache[i].dirty)
  7121. + n_dirty_caches++;
  7122. + }
  7123. +
  7124. + n_free -= n_dirty_caches;
  7125. +
  7126. + n_free -=
  7127. + ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
  7128. +
  7129. + /* Now we figure out how much to reserve for the checkpoint and report that... */
  7130. + blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
  7131. +
  7132. + n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
  7133. +
  7134. + if (n_free < 0)
  7135. + n_free = 0;
  7136. +
  7137. + return n_free;
  7138. +
  7139. +}
  7140. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_guts.h linux-2.6.36/fs/yaffs2/yaffs_guts.h
  7141. --- linux-2.6.36.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  7142. +++ linux-2.6.36/fs/yaffs2/yaffs_guts.h 2011-01-10 19:29:29.000000000 +0100
  7143. @@ -0,0 +1,914 @@
  7144. +/*
  7145. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  7146. + *
  7147. + * Copyright (C) 2002-2010 Aleph One Ltd.
  7148. + * for Toby Churchill Ltd and Brightstar Engineering
  7149. + *
  7150. + * Created by Charles Manning <charles@aleph1.co.uk>
  7151. + *
  7152. + * This program is free software; you can redistribute it and/or modify
  7153. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  7154. + * published by the Free Software Foundation.
  7155. + *
  7156. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  7157. + */
  7158. +
  7159. +#ifndef __YAFFS_GUTS_H__
  7160. +#define __YAFFS_GUTS_H__
  7161. +
  7162. +#include "yportenv.h"
  7163. +
  7164. +#define YAFFS_OK 1
  7165. +#define YAFFS_FAIL 0
  7166. +
  7167. +/* Give us a Y=0x59,
  7168. + * Give us an A=0x41,
  7169. + * Give us an FF=0xFF
  7170. + * Give us an S=0x53
  7171. + * And what have we got...
  7172. + */
  7173. +#define YAFFS_MAGIC 0x5941FF53
  7174. +
  7175. +#define YAFFS_NTNODES_LEVEL0 16
  7176. +#define YAFFS_TNODES_LEVEL0_BITS 4
  7177. +#define YAFFS_TNODES_LEVEL0_MASK 0xf
  7178. +
  7179. +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  7180. +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  7181. +#define YAFFS_TNODES_INTERNAL_MASK 0x7
  7182. +#define YAFFS_TNODES_MAX_LEVEL 6
  7183. +
  7184. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  7185. +#define YAFFS_BYTES_PER_SPARE 16
  7186. +#define YAFFS_BYTES_PER_CHUNK 512
  7187. +#define YAFFS_CHUNK_SIZE_SHIFT 9
  7188. +#define YAFFS_CHUNKS_PER_BLOCK 32
  7189. +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  7190. +#endif
  7191. +
  7192. +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  7193. +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  7194. +
  7195. +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
  7196. +
  7197. +#define YAFFS_ALLOCATION_NOBJECTS 100
  7198. +#define YAFFS_ALLOCATION_NTNODES 100
  7199. +#define YAFFS_ALLOCATION_NLINKS 100
  7200. +
  7201. +#define YAFFS_NOBJECT_BUCKETS 256
  7202. +
  7203. +#define YAFFS_OBJECT_SPACE 0x40000
  7204. +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1)
  7205. +
  7206. +#define YAFFS_CHECKPOINT_VERSION 4
  7207. +
  7208. +#ifdef CONFIG_YAFFS_UNICODE
  7209. +#define YAFFS_MAX_NAME_LENGTH 127
  7210. +#define YAFFS_MAX_ALIAS_LENGTH 79
  7211. +#else
  7212. +#define YAFFS_MAX_NAME_LENGTH 255
  7213. +#define YAFFS_MAX_ALIAS_LENGTH 159
  7214. +#endif
  7215. +
  7216. +#define YAFFS_SHORT_NAME_LENGTH 15
  7217. +
  7218. +/* Some special object ids for pseudo objects */
  7219. +#define YAFFS_OBJECTID_ROOT 1
  7220. +#define YAFFS_OBJECTID_LOSTNFOUND 2
  7221. +#define YAFFS_OBJECTID_UNLINKED 3
  7222. +#define YAFFS_OBJECTID_DELETED 4
  7223. +
  7224. +/* Pseudo object ids for checkpointing */
  7225. +#define YAFFS_OBJECTID_SB_HEADER 0x10
  7226. +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  7227. +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  7228. +
  7229. +#define YAFFS_MAX_SHORT_OP_CACHES 20
  7230. +
  7231. +#define YAFFS_N_TEMP_BUFFERS 6
  7232. +
  7233. +/* We limit the number attempts at sucessfully saving a chunk of data.
  7234. + * Small-page devices have 32 pages per block; large-page devices have 64.
  7235. + * Default to something in the order of 5 to 10 blocks worth of chunks.
  7236. + */
  7237. +#define YAFFS_WR_ATTEMPTS (5*64)
  7238. +
  7239. +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
  7240. + * The range is limited slightly to help distinguish bad numbers from good.
  7241. + * This also allows us to perhaps in the future use special numbers for
  7242. + * special purposes.
  7243. + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
  7244. + * and is a larger number than the lifetime of a 2GB device.
  7245. + */
  7246. +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  7247. +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
  7248. +
  7249. +/* Special sequence number for bad block that failed to be marked bad */
  7250. +#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
  7251. +
  7252. +/* ChunkCache is used for short read/write operations.*/
  7253. +struct yaffs_cache {
  7254. + struct yaffs_obj *object;
  7255. + int chunk_id;
  7256. + int last_use;
  7257. + int dirty;
  7258. + int n_bytes; /* Only valid if the cache is dirty */
  7259. + int locked; /* Can't push out or flush while locked. */
  7260. + u8 *data;
  7261. +};
  7262. +
  7263. +/* Tags structures in RAM
  7264. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
  7265. + * the structure size will get blown out.
  7266. + */
  7267. +
  7268. +#ifndef CONFIG_YAFFS_NO_YAFFS1
  7269. +struct yaffs_tags {
  7270. + unsigned chunk_id:20;
  7271. + unsigned serial_number:2;
  7272. + unsigned n_bytes_lsb:10;
  7273. + unsigned obj_id:18;
  7274. + unsigned ecc:12;
  7275. + unsigned n_bytes_msb:2;
  7276. +};
  7277. +
  7278. +union yaffs_tags_union {
  7279. + struct yaffs_tags as_tags;
  7280. + u8 as_bytes[8];
  7281. +};
  7282. +
  7283. +#endif
  7284. +
  7285. +/* Stuff used for extended tags in YAFFS2 */
  7286. +
  7287. +enum yaffs_ecc_result {
  7288. + YAFFS_ECC_RESULT_UNKNOWN,
  7289. + YAFFS_ECC_RESULT_NO_ERROR,
  7290. + YAFFS_ECC_RESULT_FIXED,
  7291. + YAFFS_ECC_RESULT_UNFIXED
  7292. +};
  7293. +
  7294. +enum yaffs_obj_type {
  7295. + YAFFS_OBJECT_TYPE_UNKNOWN,
  7296. + YAFFS_OBJECT_TYPE_FILE,
  7297. + YAFFS_OBJECT_TYPE_SYMLINK,
  7298. + YAFFS_OBJECT_TYPE_DIRECTORY,
  7299. + YAFFS_OBJECT_TYPE_HARDLINK,
  7300. + YAFFS_OBJECT_TYPE_SPECIAL
  7301. +};
  7302. +
  7303. +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  7304. +
  7305. +struct yaffs_ext_tags {
  7306. +
  7307. + unsigned validity0;
  7308. + unsigned chunk_used; /* Status of the chunk: used or unused */
  7309. + unsigned obj_id; /* If 0 then this is not part of an object (unused) */
  7310. + unsigned chunk_id; /* If 0 then this is a header, else a data chunk */
  7311. + unsigned n_bytes; /* Only valid for data chunks */
  7312. +
  7313. + /* The following stuff only has meaning when we read */
  7314. + enum yaffs_ecc_result ecc_result;
  7315. + unsigned block_bad;
  7316. +
  7317. + /* YAFFS 1 stuff */
  7318. + unsigned is_deleted; /* The chunk is marked deleted */
  7319. + unsigned serial_number; /* Yaffs1 2-bit serial number */
  7320. +
  7321. + /* YAFFS2 stuff */
  7322. + unsigned seq_number; /* The sequence number of this block */
  7323. +
  7324. + /* Extra info if this is an object header (YAFFS2 only) */
  7325. +
  7326. + unsigned extra_available; /* There is extra info available if this is not zero */
  7327. + unsigned extra_parent_id; /* The parent object */
  7328. + unsigned extra_is_shrink; /* Is it a shrink header? */
  7329. + unsigned extra_shadows; /* Does this shadow another object? */
  7330. +
  7331. + enum yaffs_obj_type extra_obj_type; /* What object type? */
  7332. +
  7333. + unsigned extra_length; /* Length if it is a file */
  7334. + unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */
  7335. +
  7336. + unsigned validity1;
  7337. +
  7338. +};
  7339. +
  7340. +/* Spare structure for YAFFS1 */
  7341. +struct yaffs_spare {
  7342. + u8 tb0;
  7343. + u8 tb1;
  7344. + u8 tb2;
  7345. + u8 tb3;
  7346. + u8 page_status; /* set to 0 to delete the chunk */
  7347. + u8 block_status;
  7348. + u8 tb4;
  7349. + u8 tb5;
  7350. + u8 ecc1[3];
  7351. + u8 tb6;
  7352. + u8 tb7;
  7353. + u8 ecc2[3];
  7354. +};
  7355. +
  7356. +/*Special structure for passing through to mtd */
  7357. +struct yaffs_nand_spare {
  7358. + struct yaffs_spare spare;
  7359. + int eccres1;
  7360. + int eccres2;
  7361. +};
  7362. +
  7363. +/* Block data in RAM */
  7364. +
  7365. +enum yaffs_block_state {
  7366. + YAFFS_BLOCK_STATE_UNKNOWN = 0,
  7367. +
  7368. + YAFFS_BLOCK_STATE_SCANNING,
  7369. + /* Being scanned */
  7370. +
  7371. + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
  7372. + /* The block might have something on it (ie it is allocating or full, perhaps empty)
  7373. + * but it needs to be scanned to determine its true state.
  7374. + * This state is only valid during scanning.
  7375. + * NB We tolerate empty because the pre-scanner might be incapable of deciding
  7376. + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
  7377. + */
  7378. +
  7379. + YAFFS_BLOCK_STATE_EMPTY,
  7380. + /* This block is empty */
  7381. +
  7382. + YAFFS_BLOCK_STATE_ALLOCATING,
  7383. + /* This block is partially allocated.
  7384. + * At least one page holds valid data.
  7385. + * This is the one currently being used for page
  7386. + * allocation. Should never be more than one of these.
  7387. + * If a block is only partially allocated at mount it is treated as full.
  7388. + */
  7389. +
  7390. + YAFFS_BLOCK_STATE_FULL,
  7391. + /* All the pages in this block have been allocated.
  7392. + * If a block was only partially allocated when mounted we treat
  7393. + * it as fully allocated.
  7394. + */
  7395. +
  7396. + YAFFS_BLOCK_STATE_DIRTY,
  7397. + /* The block was full and now all chunks have been deleted.
  7398. + * Erase me, reuse me.
  7399. + */
  7400. +
  7401. + YAFFS_BLOCK_STATE_CHECKPOINT,
  7402. + /* This block is assigned to holding checkpoint data. */
  7403. +
  7404. + YAFFS_BLOCK_STATE_COLLECTING,
  7405. + /* This block is being garbage collected */
  7406. +
  7407. + YAFFS_BLOCK_STATE_DEAD
  7408. + /* This block has failed and is not in use */
  7409. +};
  7410. +
  7411. +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  7412. +
  7413. +struct yaffs_block_info {
  7414. +
  7415. + int soft_del_pages:10; /* number of soft deleted pages */
  7416. + int pages_in_use:10; /* number of pages in use */
  7417. + unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
  7418. + u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */
  7419. + /* and retire the block. */
  7420. + u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */
  7421. + u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block.
  7422. + It should be prioritised for GC */
  7423. + u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
  7424. +
  7425. +#ifdef CONFIG_YAFFS_YAFFS2
  7426. + u32 has_shrink_hdr:1; /* This block has at least one shrink object header */
  7427. + u32 seq_number; /* block sequence number for yaffs2 */
  7428. +#endif
  7429. +
  7430. +};
  7431. +
  7432. +/* -------------------------- Object structure -------------------------------*/
  7433. +/* This is the object structure as stored on NAND */
  7434. +
  7435. +struct yaffs_obj_hdr {
  7436. + enum yaffs_obj_type type;
  7437. +
  7438. + /* Apply to everything */
  7439. + int parent_obj_id;
  7440. + u16 sum_no_longer_used; /* checksum of name. No longer used */
  7441. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  7442. +
  7443. + /* The following apply to directories, files, symlinks - not hard links */
  7444. + u32 yst_mode; /* protection */
  7445. +
  7446. + u32 yst_uid;
  7447. + u32 yst_gid;
  7448. + u32 yst_atime;
  7449. + u32 yst_mtime;
  7450. + u32 yst_ctime;
  7451. +
  7452. + /* File size applies to files only */
  7453. + int file_size;
  7454. +
  7455. + /* Equivalent object id applies to hard links only. */
  7456. + int equiv_id;
  7457. +
  7458. + /* Alias is for symlinks only. */
  7459. + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  7460. +
  7461. + u32 yst_rdev; /* device stuff for block and char devices (major/min) */
  7462. +
  7463. + u32 win_ctime[2];
  7464. + u32 win_atime[2];
  7465. + u32 win_mtime[2];
  7466. +
  7467. + u32 inband_shadowed_obj_id;
  7468. + u32 inband_is_shrink;
  7469. +
  7470. + u32 reserved[2];
  7471. + int shadows_obj; /* This object header shadows the specified object if > 0 */
  7472. +
  7473. + /* is_shrink applies to object headers written when we shrink the file (ie resize) */
  7474. + u32 is_shrink;
  7475. +
  7476. +};
  7477. +
  7478. +/*--------------------------- Tnode -------------------------- */
  7479. +
  7480. +struct yaffs_tnode {
  7481. + struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
  7482. +};
  7483. +
  7484. +/*------------------------ Object -----------------------------*/
  7485. +/* An object can be one of:
  7486. + * - a directory (no data, has children links
  7487. + * - a regular file (data.... not prunes :->).
  7488. + * - a symlink [symbolic link] (the alias).
  7489. + * - a hard link
  7490. + */
  7491. +
  7492. +struct yaffs_file_var {
  7493. + u32 file_size;
  7494. + u32 scanned_size;
  7495. + u32 shrink_size;
  7496. + int top_level;
  7497. + struct yaffs_tnode *top;
  7498. +};
  7499. +
  7500. +struct yaffs_dir_var {
  7501. + struct list_head children; /* list of child links */
  7502. + struct list_head dirty; /* Entry for list of dirty directories */
  7503. +};
  7504. +
  7505. +struct yaffs_symlink_var {
  7506. + YCHAR *alias;
  7507. +};
  7508. +
  7509. +struct yaffs_hardlink_var {
  7510. + struct yaffs_obj *equiv_obj;
  7511. + u32 equiv_id;
  7512. +};
  7513. +
  7514. +union yaffs_obj_var {
  7515. + struct yaffs_file_var file_variant;
  7516. + struct yaffs_dir_var dir_variant;
  7517. + struct yaffs_symlink_var symlink_variant;
  7518. + struct yaffs_hardlink_var hardlink_variant;
  7519. +};
  7520. +
  7521. +struct yaffs_obj {
  7522. + u8 deleted:1; /* This should only apply to unlinked files. */
  7523. + u8 soft_del:1; /* it has also been soft deleted */
  7524. + u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory. */
  7525. + u8 fake:1; /* A fake object has no presence on NAND. */
  7526. + u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */
  7527. + u8 unlink_allowed:1;
  7528. + u8 dirty:1; /* the object needs to be written to flash */
  7529. + u8 valid:1; /* When the file system is being loaded up, this
  7530. + * object might be created before the data
  7531. + * is available (ie. file data records appear before the header).
  7532. + */
  7533. + u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */
  7534. +
  7535. + u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is
  7536. + * still in the inode cache. Free of object is defered.
  7537. + * until the inode is released.
  7538. + */
  7539. + u8 being_created:1; /* This object is still being created so skip some checks. */
  7540. + u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */
  7541. +
  7542. + u8 xattr_known:1; /* We know if this has object has xattribs or not. */
  7543. + u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */
  7544. +
  7545. + u8 serial; /* serial number of chunk in NAND. Cached here */
  7546. + u16 sum; /* sum of the name to speed searching */
  7547. +
  7548. + struct yaffs_dev *my_dev; /* The device I'm on */
  7549. +
  7550. + struct list_head hash_link; /* list of objects in this hash bucket */
  7551. +
  7552. + struct list_head hard_links; /* all the equivalent hard linked objects */
  7553. +
  7554. + /* directory structure stuff */
  7555. + /* also used for linking up the free list */
  7556. + struct yaffs_obj *parent;
  7557. + struct list_head siblings;
  7558. +
  7559. + /* Where's my object header in NAND? */
  7560. + int hdr_chunk;
  7561. +
  7562. + int n_data_chunks; /* Number of data chunks attached to the file. */
  7563. +
  7564. + u32 obj_id; /* the object id value */
  7565. +
  7566. + u32 yst_mode;
  7567. +
  7568. +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  7569. + YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
  7570. +#endif
  7571. +
  7572. +#ifdef CONFIG_YAFFS_WINCE
  7573. + u32 win_ctime[2];
  7574. + u32 win_mtime[2];
  7575. + u32 win_atime[2];
  7576. +#else
  7577. + u32 yst_uid;
  7578. + u32 yst_gid;
  7579. + u32 yst_atime;
  7580. + u32 yst_mtime;
  7581. + u32 yst_ctime;
  7582. +#endif
  7583. +
  7584. + u32 yst_rdev;
  7585. +
  7586. + void *my_inode;
  7587. +
  7588. + enum yaffs_obj_type variant_type;
  7589. +
  7590. + union yaffs_obj_var variant;
  7591. +
  7592. +};
  7593. +
  7594. +struct yaffs_obj_bucket {
  7595. + struct list_head list;
  7596. + int count;
  7597. +};
  7598. +
  7599. +/* yaffs_checkpt_obj holds the definition of an object as dumped
  7600. + * by checkpointing.
  7601. + */
  7602. +
  7603. +struct yaffs_checkpt_obj {
  7604. + int struct_type;
  7605. + u32 obj_id;
  7606. + u32 parent_id;
  7607. + int hdr_chunk;
  7608. + enum yaffs_obj_type variant_type:3;
  7609. + u8 deleted:1;
  7610. + u8 soft_del:1;
  7611. + u8 unlinked:1;
  7612. + u8 fake:1;
  7613. + u8 rename_allowed:1;
  7614. + u8 unlink_allowed:1;
  7615. + u8 serial;
  7616. + int n_data_chunks;
  7617. + u32 size_or_equiv_obj;
  7618. +};
  7619. +
  7620. +/*--------------------- Temporary buffers ----------------
  7621. + *
  7622. + * These are chunk-sized working buffers. Each device has a few
  7623. + */
  7624. +
  7625. +struct yaffs_buffer {
  7626. + u8 *buffer;
  7627. + int line; /* track from whence this buffer was allocated */
  7628. + int max_line;
  7629. +};
  7630. +
  7631. +/*----------------- Device ---------------------------------*/
  7632. +
  7633. +struct yaffs_param {
  7634. + const YCHAR *name;
  7635. +
  7636. + /*
  7637. + * Entry parameters set up way early. Yaffs sets up the rest.
  7638. + * The structure should be zeroed out before use so that unused
  7639. + * and defualt values are zero.
  7640. + */
  7641. +
  7642. + int inband_tags; /* Use unband tags */
  7643. + u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */
  7644. + int chunks_per_block; /* does not need to be a power of 2 */
  7645. + int spare_bytes_per_chunk; /* spare area size */
  7646. + int start_block; /* Start block we're allowed to use */
  7647. + int end_block; /* End block we're allowed to use */
  7648. + int n_reserved_blocks; /* We want this tuneable so that we can reduce */
  7649. + /* reserved blocks on NOR and RAM. */
  7650. +
  7651. + int n_caches; /* If <= 0, then short op caching is disabled, else
  7652. + * the number of short op caches (don't use too many).
  7653. + * 10 to 20 is a good bet.
  7654. + */
  7655. + int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */
  7656. + int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */
  7657. +
  7658. + int is_yaffs2; /* Use yaffs2 mode on this device */
  7659. +
  7660. + int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
  7661. +
  7662. + int refresh_period; /* How often we should check to do a block refresh */
  7663. +
  7664. + /* Checkpoint control. Can be set before or after initialisation */
  7665. + u8 skip_checkpt_rd;
  7666. + u8 skip_checkpt_wr;
  7667. +
  7668. + int enable_xattr; /* Enable xattribs */
  7669. +
  7670. + /* NAND access functions (Must be set before calling YAFFS) */
  7671. +
  7672. + int (*write_chunk_fn) (struct yaffs_dev * dev,
  7673. + int nand_chunk, const u8 * data,
  7674. + const struct yaffs_spare * spare);
  7675. + int (*read_chunk_fn) (struct yaffs_dev * dev,
  7676. + int nand_chunk, u8 * data,
  7677. + struct yaffs_spare * spare);
  7678. + int (*erase_fn) (struct yaffs_dev * dev, int flash_block);
  7679. + int (*initialise_flash_fn) (struct yaffs_dev * dev);
  7680. + int (*deinitialise_flash_fn) (struct yaffs_dev * dev);
  7681. +
  7682. +#ifdef CONFIG_YAFFS_YAFFS2
  7683. + int (*write_chunk_tags_fn) (struct yaffs_dev * dev,
  7684. + int nand_chunk, const u8 * data,
  7685. + const struct yaffs_ext_tags * tags);
  7686. + int (*read_chunk_tags_fn) (struct yaffs_dev * dev,
  7687. + int nand_chunk, u8 * data,
  7688. + struct yaffs_ext_tags * tags);
  7689. + int (*bad_block_fn) (struct yaffs_dev * dev, int block_no);
  7690. + int (*query_block_fn) (struct yaffs_dev * dev, int block_no,
  7691. + enum yaffs_block_state * state,
  7692. + u32 * seq_number);
  7693. +#endif
  7694. +
  7695. + /* The remove_obj_fn function must be supplied by OS flavours that
  7696. + * need it.
  7697. + * yaffs direct uses it to implement the faster readdir.
  7698. + * Linux uses it to protect the directory during unlocking.
  7699. + */
  7700. + void (*remove_obj_fn) (struct yaffs_obj * obj);
  7701. +
  7702. + /* Callback to mark the superblock dirty */
  7703. + void (*sb_dirty_fn) (struct yaffs_dev * dev);
  7704. +
  7705. + /* Callback to control garbage collection. */
  7706. + unsigned (*gc_control) (struct yaffs_dev * dev);
  7707. +
  7708. + /* Debug control flags. Don't use unless you know what you're doing */
  7709. + int use_header_file_size; /* Flag to determine if we should use file sizes from the header */
  7710. + int disable_lazy_load; /* Disable lazy loading on this device */
  7711. + int wide_tnodes_disabled; /* Set to disable wide tnodes */
  7712. + int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */
  7713. +
  7714. + int defered_dir_update; /* Set to defer directory updates */
  7715. +
  7716. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  7717. + int auto_unicode;
  7718. +#endif
  7719. + int always_check_erased; /* Force chunk erased check always on */
  7720. +};
  7721. +
  7722. +struct yaffs_dev {
  7723. + struct yaffs_param param;
  7724. +
  7725. + /* Context storage. Holds extra OS specific data for this device */
  7726. +
  7727. + void *os_context;
  7728. + void *driver_context;
  7729. +
  7730. + struct list_head dev_list;
  7731. +
  7732. + /* Runtime parameters. Set up by YAFFS. */
  7733. + int data_bytes_per_chunk;
  7734. +
  7735. + /* Non-wide tnode stuff */
  7736. + u16 chunk_grp_bits; /* Number of bits that need to be resolved if
  7737. + * the tnodes are not wide enough.
  7738. + */
  7739. + u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
  7740. +
  7741. + /* Stuff to support wide tnodes */
  7742. + u32 tnode_width;
  7743. + u32 tnode_mask;
  7744. + u32 tnode_size;
  7745. +
  7746. + /* Stuff for figuring out file offset to chunk conversions */
  7747. + u32 chunk_shift; /* Shift value */
  7748. + u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */
  7749. + u32 chunk_mask; /* Mask to use for power-of-2 case */
  7750. +
  7751. + int is_mounted;
  7752. + int read_only;
  7753. + int is_checkpointed;
  7754. +
  7755. + /* Stuff to support block offsetting to support start block zero */
  7756. + int internal_start_block;
  7757. + int internal_end_block;
  7758. + int block_offset;
  7759. + int chunk_offset;
  7760. +
  7761. + /* Runtime checkpointing stuff */
  7762. + int checkpt_page_seq; /* running sequence number of checkpoint pages */
  7763. + int checkpt_byte_count;
  7764. + int checkpt_byte_offs;
  7765. + u8 *checkpt_buffer;
  7766. + int checkpt_open_write;
  7767. + int blocks_in_checkpt;
  7768. + int checkpt_cur_chunk;
  7769. + int checkpt_cur_block;
  7770. + int checkpt_next_block;
  7771. + int *checkpt_block_list;
  7772. + int checkpt_max_blocks;
  7773. + u32 checkpt_sum;
  7774. + u32 checkpt_xor;
  7775. +
  7776. + int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */
  7777. +
  7778. + /* Block Info */
  7779. + struct yaffs_block_info *block_info;
  7780. + u8 *chunk_bits; /* bitmap of chunks in use */
  7781. + unsigned block_info_alt:1; /* was allocated using alternative strategy */
  7782. + unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */
  7783. + int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
  7784. + * Must be consistent with chunks_per_block.
  7785. + */
  7786. +
  7787. + int n_erased_blocks;
  7788. + int alloc_block; /* Current block being allocated off */
  7789. + u32 alloc_page;
  7790. + int alloc_block_finder; /* Used to search for next allocation block */
  7791. +
  7792. + /* Object and Tnode memory management */
  7793. + void *allocator;
  7794. + int n_obj;
  7795. + int n_tnodes;
  7796. +
  7797. + int n_hardlinks;
  7798. +
  7799. + struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
  7800. + u32 bucket_finder;
  7801. +
  7802. + int n_free_chunks;
  7803. +
  7804. + /* Garbage collection control */
  7805. + u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
  7806. + u32 n_clean_ups;
  7807. +
  7808. + unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */
  7809. + unsigned gc_disable;
  7810. + unsigned gc_block_finder;
  7811. + unsigned gc_dirtiest;
  7812. + unsigned gc_pages_in_use;
  7813. + unsigned gc_not_done;
  7814. + unsigned gc_block;
  7815. + unsigned gc_chunk;
  7816. + unsigned gc_skip;
  7817. +
  7818. + /* Special directories */
  7819. + struct yaffs_obj *root_dir;
  7820. + struct yaffs_obj *lost_n_found;
  7821. +
  7822. + /* Buffer areas for storing data to recover from write failures TODO
  7823. + * u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
  7824. + * struct yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK];
  7825. + */
  7826. +
  7827. + int buffered_block; /* Which block is buffered here? */
  7828. + int doing_buffered_block_rewrite;
  7829. +
  7830. + struct yaffs_cache *cache;
  7831. + int cache_last_use;
  7832. +
  7833. + /* Stuff for background deletion and unlinked files. */
  7834. + struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted files live. */
  7835. + struct yaffs_obj *del_dir; /* Directory where deleted objects are sent to disappear. */
  7836. + struct yaffs_obj *unlinked_deletion; /* Current file being background deleted. */
  7837. + int n_deleted_files; /* Count of files awaiting deletion; */
  7838. + int n_unlinked_files; /* Count of unlinked files. */
  7839. + int n_bg_deletions; /* Count of background deletions. */
  7840. +
  7841. + /* Temporary buffer management */
  7842. + struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
  7843. + int max_temp;
  7844. + int temp_in_use;
  7845. + int unmanaged_buffer_allocs;
  7846. + int unmanaged_buffer_deallocs;
  7847. +
  7848. + /* yaffs2 runtime stuff */
  7849. + unsigned seq_number; /* Sequence number of currently allocating block */
  7850. + unsigned oldest_dirty_seq;
  7851. + unsigned oldest_dirty_block;
  7852. +
  7853. + /* Block refreshing */
  7854. + int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */
  7855. +
  7856. + /* Dirty directory handling */
  7857. + struct list_head dirty_dirs; /* List of dirty directories */
  7858. +
  7859. + /* Statistcs */
  7860. + u32 n_page_writes;
  7861. + u32 n_page_reads;
  7862. + u32 n_erasures;
  7863. + u32 n_erase_failures;
  7864. + u32 n_gc_copies;
  7865. + u32 all_gcs;
  7866. + u32 passive_gc_count;
  7867. + u32 oldest_dirty_gc_count;
  7868. + u32 n_gc_blocks;
  7869. + u32 bg_gcs;
  7870. + u32 n_retired_writes;
  7871. + u32 n_retired_blocks;
  7872. + u32 n_ecc_fixed;
  7873. + u32 n_ecc_unfixed;
  7874. + u32 n_tags_ecc_fixed;
  7875. + u32 n_tags_ecc_unfixed;
  7876. + u32 n_deletions;
  7877. + u32 n_unmarked_deletions;
  7878. + u32 refresh_count;
  7879. + u32 cache_hits;
  7880. +
  7881. +};
  7882. +
  7883. +/* The CheckpointDevice structure holds the device information that changes at runtime and
  7884. + * must be preserved over unmount/mount cycles.
  7885. + */
  7886. +struct yaffs_checkpt_dev {
  7887. + int struct_type;
  7888. + int n_erased_blocks;
  7889. + int alloc_block; /* Current block being allocated off */
  7890. + u32 alloc_page;
  7891. + int n_free_chunks;
  7892. +
  7893. + int n_deleted_files; /* Count of files awaiting deletion; */
  7894. + int n_unlinked_files; /* Count of unlinked files. */
  7895. + int n_bg_deletions; /* Count of background deletions. */
  7896. +
  7897. + /* yaffs2 runtime stuff */
  7898. + unsigned seq_number; /* Sequence number of currently allocating block */
  7899. +
  7900. +};
  7901. +
  7902. +struct yaffs_checkpt_validity {
  7903. + int struct_type;
  7904. + u32 magic;
  7905. + u32 version;
  7906. + u32 head;
  7907. +};
  7908. +
  7909. +struct yaffs_shadow_fixer {
  7910. + int obj_id;
  7911. + int shadowed_id;
  7912. + struct yaffs_shadow_fixer *next;
  7913. +};
  7914. +
  7915. +/* Structure for doing xattr modifications */
  7916. +struct yaffs_xattr_mod {
  7917. + int set; /* If 0 then this is a deletion */
  7918. + const YCHAR *name;
  7919. + const void *data;
  7920. + int size;
  7921. + int flags;
  7922. + int result;
  7923. +};
  7924. +
  7925. +/*----------------------- YAFFS Functions -----------------------*/
  7926. +
  7927. +int yaffs_guts_initialise(struct yaffs_dev *dev);
  7928. +void yaffs_deinitialise(struct yaffs_dev *dev);
  7929. +
  7930. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
  7931. +
  7932. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  7933. + struct yaffs_obj *new_dir, const YCHAR * new_name);
  7934. +
  7935. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
  7936. +int yaffs_del_obj(struct yaffs_obj *obj);
  7937. +
  7938. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
  7939. +int yaffs_get_obj_length(struct yaffs_obj *obj);
  7940. +int yaffs_get_obj_inode(struct yaffs_obj *obj);
  7941. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
  7942. +int yaffs_get_obj_link_count(struct yaffs_obj *obj);
  7943. +
  7944. +/* File operations */
  7945. +int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
  7946. + int n_bytes);
  7947. +int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
  7948. + int n_bytes, int write_trhrough);
  7949. +int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
  7950. +
  7951. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  7952. + const YCHAR * name, u32 mode, u32 uid,
  7953. + u32 gid);
  7954. +
  7955. +int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync);
  7956. +
  7957. +/* Flushing and checkpointing */
  7958. +void yaffs_flush_whole_cache(struct yaffs_dev *dev);
  7959. +
  7960. +int yaffs_checkpoint_save(struct yaffs_dev *dev);
  7961. +int yaffs_checkpoint_restore(struct yaffs_dev *dev);
  7962. +
  7963. +/* Directory operations */
  7964. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  7965. + u32 mode, u32 uid, u32 gid);
  7966. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
  7967. + const YCHAR * name);
  7968. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
  7969. +
  7970. +/* Link operations */
  7971. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  7972. + struct yaffs_obj *equiv_obj);
  7973. +
  7974. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
  7975. +
  7976. +/* Symlink operations */
  7977. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  7978. + const YCHAR * name, u32 mode, u32 uid,
  7979. + u32 gid, const YCHAR * alias);
  7980. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
  7981. +
  7982. +/* Special inodes (fifos, sockets and devices) */
  7983. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  7984. + const YCHAR * name, u32 mode, u32 uid,
  7985. + u32 gid, u32 rdev);
  7986. +
  7987. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  7988. + const void *value, int size, int flags);
  7989. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  7990. + int size);
  7991. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
  7992. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name);
  7993. +
  7994. +/* Special directories */
  7995. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
  7996. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
  7997. +
  7998. +void yaffs_handle_defered_free(struct yaffs_obj *obj);
  7999. +
  8000. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
  8001. +
  8002. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
  8003. +
  8004. +/* Debug dump */
  8005. +int yaffs_dump_obj(struct yaffs_obj *obj);
  8006. +
  8007. +void yaffs_guts_test(struct yaffs_dev *dev);
  8008. +
  8009. +/* A few useful functions to be used within the core files*/
  8010. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  8011. + int lyn);
  8012. +int yaffs_check_ff(u8 * buffer, int n_bytes);
  8013. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  8014. + struct yaffs_block_info *bi);
  8015. +
  8016. +u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no);
  8017. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no);
  8018. +
  8019. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  8020. + int number,
  8021. + enum yaffs_obj_type type);
  8022. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  8023. + int nand_chunk, int in_scan);
  8024. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name);
  8025. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  8026. + const struct yaffs_obj_hdr *oh);
  8027. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
  8028. +YCHAR *yaffs_clone_str(const YCHAR * str);
  8029. +void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list);
  8030. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
  8031. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name,
  8032. + int force, int is_shrink, int shadows,
  8033. + struct yaffs_xattr_mod *xop);
  8034. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  8035. + int backward_scanning);
  8036. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
  8037. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
  8038. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  8039. + struct yaffs_file_var *file_struct,
  8040. + u32 chunk_id,
  8041. + struct yaffs_tnode *passed_tn);
  8042. +
  8043. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  8044. + int n_bytes, int write_trhrough);
  8045. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
  8046. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
  8047. +
  8048. +int yaffs_count_free_chunks(struct yaffs_dev *dev);
  8049. +
  8050. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  8051. + struct yaffs_file_var *file_struct,
  8052. + u32 chunk_id);
  8053. +
  8054. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  8055. + unsigned pos);
  8056. +
  8057. +#endif
  8058. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_linux.h linux-2.6.36/fs/yaffs2/yaffs_linux.h
  8059. --- linux-2.6.36.orig/fs/yaffs2/yaffs_linux.h 1970-01-01 01:00:00.000000000 +0100
  8060. +++ linux-2.6.36/fs/yaffs2/yaffs_linux.h 2011-01-10 19:29:29.000000000 +0100
  8061. @@ -0,0 +1,41 @@
  8062. +/*
  8063. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8064. + *
  8065. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8066. + * for Toby Churchill Ltd and Brightstar Engineering
  8067. + *
  8068. + * Created by Charles Manning <charles@aleph1.co.uk>
  8069. + *
  8070. + * This program is free software; you can redistribute it and/or modify
  8071. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8072. + * published by the Free Software Foundation.
  8073. + *
  8074. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8075. + */
  8076. +
  8077. +#ifndef __YAFFS_LINUX_H__
  8078. +#define __YAFFS_LINUX_H__
  8079. +
  8080. +#include "yportenv.h"
  8081. +
  8082. +struct yaffs_linux_context {
  8083. + struct list_head context_list; /* List of these we have mounted */
  8084. + struct yaffs_dev *dev;
  8085. + struct super_block *super;
  8086. + struct task_struct *bg_thread; /* Background thread for this device */
  8087. + int bg_running;
  8088. + struct mutex gross_lock; /* Gross locking mutex*/
  8089. + u8 *spare_buffer; /* For mtdif2 use. Don't know the size of the buffer
  8090. + * at compile time so we have to allocate it.
  8091. + */
  8092. + struct list_head search_contexts;
  8093. + void (*put_super_fn) (struct super_block * sb);
  8094. +
  8095. + struct task_struct *readdir_process;
  8096. + unsigned mount_id;
  8097. +};
  8098. +
  8099. +#define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
  8100. +#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
  8101. +
  8102. +#endif
  8103. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.c linux-2.6.36/fs/yaffs2/yaffs_mtdif.c
  8104. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  8105. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif.c 2011-01-10 19:29:29.000000000 +0100
  8106. @@ -0,0 +1,54 @@
  8107. +/*
  8108. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8109. + *
  8110. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8111. + * for Toby Churchill Ltd and Brightstar Engineering
  8112. + *
  8113. + * Created by Charles Manning <charles@aleph1.co.uk>
  8114. + *
  8115. + * This program is free software; you can redistribute it and/or modify
  8116. + * it under the terms of the GNU General Public License version 2 as
  8117. + * published by the Free Software Foundation.
  8118. + */
  8119. +
  8120. +#include "yportenv.h"
  8121. +
  8122. +#include "yaffs_mtdif.h"
  8123. +
  8124. +#include "linux/mtd/mtd.h"
  8125. +#include "linux/types.h"
  8126. +#include "linux/time.h"
  8127. +#include "linux/mtd/nand.h"
  8128. +
  8129. +#include "yaffs_linux.h"
  8130. +
  8131. +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
  8132. +{
  8133. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8134. + u32 addr =
  8135. + ((loff_t) block_no) * dev->param.total_bytes_per_chunk
  8136. + * dev->param.chunks_per_block;
  8137. + struct erase_info ei;
  8138. +
  8139. + int retval = 0;
  8140. +
  8141. + ei.mtd = mtd;
  8142. + ei.addr = addr;
  8143. + ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
  8144. + ei.time = 1000;
  8145. + ei.retries = 2;
  8146. + ei.callback = NULL;
  8147. + ei.priv = (u_long) dev;
  8148. +
  8149. + retval = mtd->erase(mtd, &ei);
  8150. +
  8151. + if (retval == 0)
  8152. + return YAFFS_OK;
  8153. + else
  8154. + return YAFFS_FAIL;
  8155. +}
  8156. +
  8157. +int nandmtd_initialise(struct yaffs_dev *dev)
  8158. +{
  8159. + return YAFFS_OK;
  8160. +}
  8161. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.h linux-2.6.36/fs/yaffs2/yaffs_mtdif.h
  8162. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  8163. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif.h 2011-01-10 19:29:29.000000000 +0100
  8164. @@ -0,0 +1,23 @@
  8165. +/*
  8166. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8167. + *
  8168. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8169. + * for Toby Churchill Ltd and Brightstar Engineering
  8170. + *
  8171. + * Created by Charles Manning <charles@aleph1.co.uk>
  8172. + *
  8173. + * This program is free software; you can redistribute it and/or modify
  8174. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8175. + * published by the Free Software Foundation.
  8176. + *
  8177. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8178. + */
  8179. +
  8180. +#ifndef __YAFFS_MTDIF_H__
  8181. +#define __YAFFS_MTDIF_H__
  8182. +
  8183. +#include "yaffs_guts.h"
  8184. +
  8185. +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no);
  8186. +int nandmtd_initialise(struct yaffs_dev *dev);
  8187. +#endif
  8188. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.c linux-2.6.36/fs/yaffs2/yaffs_mtdif1.c
  8189. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
  8190. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif1.c 2011-01-10 19:29:29.000000000 +0100
  8191. @@ -0,0 +1,365 @@
  8192. +/*
  8193. + * YAFFS: Yet another FFS. A NAND-flash specific file system.
  8194. + *
  8195. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8196. + * for Toby Churchill Ltd and Brightstar Engineering
  8197. + *
  8198. + * Created by Charles Manning <charles@aleph1.co.uk>
  8199. + *
  8200. + * This program is free software; you can redistribute it and/or modify
  8201. + * it under the terms of the GNU General Public License version 2 as
  8202. + * published by the Free Software Foundation.
  8203. + */
  8204. +
  8205. +/*
  8206. + * This module provides the interface between yaffs_nand.c and the
  8207. + * MTD API. This version is used when the MTD interface supports the
  8208. + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
  8209. + * and we have small-page NAND device.
  8210. + *
  8211. + * These functions are invoked via function pointers in yaffs_nand.c.
  8212. + * This replaces functionality provided by functions in yaffs_mtdif.c
  8213. + * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are
  8214. + * called in yaffs_mtdif.c when the function pointers are NULL.
  8215. + * We assume the MTD layer is performing ECC (use_nand_ecc is true).
  8216. + */
  8217. +
  8218. +#include "yportenv.h"
  8219. +#include "yaffs_trace.h"
  8220. +#include "yaffs_guts.h"
  8221. +#include "yaffs_packedtags1.h"
  8222. +#include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
  8223. +#include "yaffs_linux.h"
  8224. +
  8225. +#include "linux/kernel.h"
  8226. +#include "linux/version.h"
  8227. +#include "linux/types.h"
  8228. +#include "linux/mtd/mtd.h"
  8229. +
  8230. +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
  8231. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8232. +
  8233. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8234. +# define YTAG1_SIZE 8
  8235. +#else
  8236. +# define YTAG1_SIZE 9
  8237. +#endif
  8238. +
  8239. +#if 0
  8240. +/* Use the following nand_ecclayout with MTD when using
  8241. + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
  8242. + * If you have existing Yaffs images and the byte order differs from this,
  8243. + * adjust 'oobfree' to match your existing Yaffs data.
  8244. + *
  8245. + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
  8246. + * page_status byte (at NAND spare offset 4) scattered/gathered from/to
  8247. + * the 9th byte.
  8248. + *
  8249. + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
  8250. + * We have/need packed_tags1 plus page_status: T0,T1,T2,T3,T4,T5,T6,T7,P
  8251. + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the page_status
  8252. + * byte and B is the small-page bad-block indicator byte.
  8253. + */
  8254. +static struct nand_ecclayout nand_oob_16 = {
  8255. + .eccbytes = 6,
  8256. + .eccpos = {8, 9, 10, 13, 14, 15},
  8257. + .oobavail = 9,
  8258. + .oobfree = {{0, 4}, {6, 2}, {11, 2}, {4, 1}}
  8259. +};
  8260. +#endif
  8261. +
  8262. +/* Write a chunk (page) of data to NAND.
  8263. + *
  8264. + * Caller always provides ExtendedTags data which are converted to a more
  8265. + * compact (packed) form for storage in NAND. A mini-ECC runs over the
  8266. + * contents of the tags meta-data; used to valid the tags when read.
  8267. + *
  8268. + * - Pack ExtendedTags to packed_tags1 form
  8269. + * - Compute mini-ECC for packed_tags1
  8270. + * - Write data and packed tags to NAND.
  8271. + *
  8272. + * Note: Due to the use of the packed_tags1 meta-data which does not include
  8273. + * a full sequence number (as found in the larger packed_tags2 form) it is
  8274. + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
  8275. + * discarded and dirty. This is not ideal: newer NAND parts are supposed
  8276. + * to be written just once. When Yaffs performs this operation, this
  8277. + * function is called with a NULL data pointer -- calling MTD write_oob
  8278. + * without data is valid usage (2.6.17).
  8279. + *
  8280. + * Any underlying MTD error results in YAFFS_FAIL.
  8281. + * Returns YAFFS_OK or YAFFS_FAIL.
  8282. + */
  8283. +int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
  8284. + int nand_chunk, const u8 * data,
  8285. + const struct yaffs_ext_tags *etags)
  8286. +{
  8287. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8288. + int chunk_bytes = dev->data_bytes_per_chunk;
  8289. + loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
  8290. + struct mtd_oob_ops ops;
  8291. + struct yaffs_packed_tags1 pt1;
  8292. + int retval;
  8293. +
  8294. + /* we assume that packed_tags1 and struct yaffs_tags are compatible */
  8295. + compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
  8296. + compile_time_assertion(sizeof(struct yaffs_tags) == 8);
  8297. +
  8298. + yaffs_pack_tags1(&pt1, etags);
  8299. + yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
  8300. +
  8301. + /* When deleting a chunk, the upper layer provides only skeletal
  8302. + * etags, one with is_deleted set. However, we need to update the
  8303. + * tags, not erase them completely. So we use the NAND write property
  8304. + * that only zeroed-bits stick and set tag bytes to all-ones and
  8305. + * zero just the (not) deleted bit.
  8306. + */
  8307. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8308. + if (etags->is_deleted) {
  8309. + memset(&pt1, 0xff, 8);
  8310. + /* clear delete status bit to indicate deleted */
  8311. + pt1.deleted = 0;
  8312. + }
  8313. +#else
  8314. + ((u8 *) & pt1)[8] = 0xff;
  8315. + if (etags->is_deleted) {
  8316. + memset(&pt1, 0xff, 8);
  8317. + /* zero page_status byte to indicate deleted */
  8318. + ((u8 *) & pt1)[8] = 0;
  8319. + }
  8320. +#endif
  8321. +
  8322. + memset(&ops, 0, sizeof(ops));
  8323. + ops.mode = MTD_OOB_AUTO;
  8324. + ops.len = (data) ? chunk_bytes : 0;
  8325. + ops.ooblen = YTAG1_SIZE;
  8326. + ops.datbuf = (u8 *) data;
  8327. + ops.oobbuf = (u8 *) & pt1;
  8328. +
  8329. + retval = mtd->write_oob(mtd, addr, &ops);
  8330. + if (retval) {
  8331. + T(YAFFS_TRACE_MTD,
  8332. + (TSTR("write_oob failed, chunk %d, mtd error %d" TENDSTR),
  8333. + nand_chunk, retval));
  8334. + }
  8335. + return retval ? YAFFS_FAIL : YAFFS_OK;
  8336. +}
  8337. +
  8338. +/* Return with empty ExtendedTags but add ecc_result.
  8339. + */
  8340. +static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
  8341. +{
  8342. + if (etags) {
  8343. + memset(etags, 0, sizeof(*etags));
  8344. + etags->ecc_result = ecc_result;
  8345. + }
  8346. + return retval;
  8347. +}
  8348. +
  8349. +/* Read a chunk (page) from NAND.
  8350. + *
  8351. + * Caller expects ExtendedTags data to be usable even on error; that is,
  8352. + * all members except ecc_result and block_bad are zeroed.
  8353. + *
  8354. + * - Check ECC results for data (if applicable)
  8355. + * - Check for blank/erased block (return empty ExtendedTags if blank)
  8356. + * - Check the packed_tags1 mini-ECC (correct if necessary/possible)
  8357. + * - Convert packed_tags1 to ExtendedTags
  8358. + * - Update ecc_result and block_bad members to refect state.
  8359. + *
  8360. + * Returns YAFFS_OK or YAFFS_FAIL.
  8361. + */
  8362. +int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
  8363. + int nand_chunk, u8 * data,
  8364. + struct yaffs_ext_tags *etags)
  8365. +{
  8366. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8367. + int chunk_bytes = dev->data_bytes_per_chunk;
  8368. + loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
  8369. + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
  8370. + struct mtd_oob_ops ops;
  8371. + struct yaffs_packed_tags1 pt1;
  8372. + int retval;
  8373. + int deleted;
  8374. +
  8375. + memset(&ops, 0, sizeof(ops));
  8376. + ops.mode = MTD_OOB_AUTO;
  8377. + ops.len = (data) ? chunk_bytes : 0;
  8378. + ops.ooblen = YTAG1_SIZE;
  8379. + ops.datbuf = data;
  8380. + ops.oobbuf = (u8 *) & pt1;
  8381. +
  8382. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
  8383. + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
  8384. + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
  8385. + */
  8386. + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
  8387. +#endif
  8388. + /* Read page and oob using MTD.
  8389. + * Check status and determine ECC result.
  8390. + */
  8391. + retval = mtd->read_oob(mtd, addr, &ops);
  8392. + if (retval) {
  8393. + T(YAFFS_TRACE_MTD,
  8394. + (TSTR("read_oob failed, chunk %d, mtd error %d" TENDSTR),
  8395. + nand_chunk, retval));
  8396. + }
  8397. +
  8398. + switch (retval) {
  8399. + case 0:
  8400. + /* no error */
  8401. + break;
  8402. +
  8403. + case -EUCLEAN:
  8404. + /* MTD's ECC fixed the data */
  8405. + eccres = YAFFS_ECC_RESULT_FIXED;
  8406. + dev->n_ecc_fixed++;
  8407. + break;
  8408. +
  8409. + case -EBADMSG:
  8410. + /* MTD's ECC could not fix the data */
  8411. + dev->n_ecc_unfixed++;
  8412. + /* fall into... */
  8413. + default:
  8414. + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
  8415. + etags->block_bad = (mtd->block_isbad) (mtd, addr);
  8416. + return YAFFS_FAIL;
  8417. + }
  8418. +
  8419. + /* Check for a blank/erased chunk.
  8420. + */
  8421. + if (yaffs_check_ff((u8 *) & pt1, 8)) {
  8422. + /* when blank, upper layers want ecc_result to be <= NO_ERROR */
  8423. + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
  8424. + }
  8425. +#ifndef CONFIG_YAFFS_9BYTE_TAGS
  8426. + /* Read deleted status (bit) then return it to it's non-deleted
  8427. + * state before performing tags mini-ECC check. pt1.deleted is
  8428. + * inverted.
  8429. + */
  8430. + deleted = !pt1.deleted;
  8431. + pt1.deleted = 1;
  8432. +#else
  8433. + deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7);
  8434. +#endif
  8435. +
  8436. + /* Check the packed tags mini-ECC and correct if necessary/possible.
  8437. + */
  8438. + retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1);
  8439. + switch (retval) {
  8440. + case 0:
  8441. + /* no tags error, use MTD result */
  8442. + break;
  8443. + case 1:
  8444. + /* recovered tags-ECC error */
  8445. + dev->n_tags_ecc_fixed++;
  8446. + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
  8447. + eccres = YAFFS_ECC_RESULT_FIXED;
  8448. + break;
  8449. + default:
  8450. + /* unrecovered tags-ECC error */
  8451. + dev->n_tags_ecc_unfixed++;
  8452. + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
  8453. + }
  8454. +
  8455. + /* Unpack the tags to extended form and set ECC result.
  8456. + * [set should_be_ff just to keep yaffs_unpack_tags1 happy]
  8457. + */
  8458. + pt1.should_be_ff = 0xFFFFFFFF;
  8459. + yaffs_unpack_tags1(etags, &pt1);
  8460. + etags->ecc_result = eccres;
  8461. +
  8462. + /* Set deleted state */
  8463. + etags->is_deleted = deleted;
  8464. + return YAFFS_OK;
  8465. +}
  8466. +
  8467. +/* Mark a block bad.
  8468. + *
  8469. + * This is a persistant state.
  8470. + * Use of this function should be rare.
  8471. + *
  8472. + * Returns YAFFS_OK or YAFFS_FAIL.
  8473. + */
  8474. +int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no)
  8475. +{
  8476. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8477. + int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
  8478. + int retval;
  8479. +
  8480. + T(YAFFS_TRACE_BAD_BLOCKS,
  8481. + (TSTR("marking block %d bad" TENDSTR), block_no));
  8482. +
  8483. + retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no);
  8484. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  8485. +}
  8486. +
  8487. +/* Check any MTD prerequists.
  8488. + *
  8489. + * Returns YAFFS_OK or YAFFS_FAIL.
  8490. + */
  8491. +static int nandmtd1_test_prerequists(struct mtd_info *mtd)
  8492. +{
  8493. + /* 2.6.18 has mtd->ecclayout->oobavail */
  8494. + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
  8495. + int oobavail = mtd->ecclayout->oobavail;
  8496. +
  8497. + if (oobavail < YTAG1_SIZE) {
  8498. + T(YAFFS_TRACE_ERROR,
  8499. + (TSTR
  8500. + ("mtd device has only %d bytes for tags, need %d" TENDSTR),
  8501. + oobavail, YTAG1_SIZE));
  8502. + return YAFFS_FAIL;
  8503. + }
  8504. + return YAFFS_OK;
  8505. +}
  8506. +
  8507. +/* Query for the current state of a specific block.
  8508. + *
  8509. + * Examine the tags of the first chunk of the block and return the state:
  8510. + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
  8511. + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
  8512. + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
  8513. + *
  8514. + * Always returns YAFFS_OK.
  8515. + */
  8516. +int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
  8517. + enum yaffs_block_state *state_ptr, u32 * seq_ptr)
  8518. +{
  8519. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8520. + int chunk_num = block_no * dev->param.chunks_per_block;
  8521. + loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
  8522. + struct yaffs_ext_tags etags;
  8523. + int state = YAFFS_BLOCK_STATE_DEAD;
  8524. + int seqnum = 0;
  8525. + int retval;
  8526. +
  8527. + /* We don't yet have a good place to test for MTD config prerequists.
  8528. + * Do it here as we are called during the initial scan.
  8529. + */
  8530. + if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
  8531. + return YAFFS_FAIL;
  8532. +
  8533. + retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
  8534. + etags.block_bad = (mtd->block_isbad) (mtd, addr);
  8535. + if (etags.block_bad) {
  8536. + T(YAFFS_TRACE_BAD_BLOCKS,
  8537. + (TSTR("block %d is marked bad" TENDSTR), block_no));
  8538. + state = YAFFS_BLOCK_STATE_DEAD;
  8539. + } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
  8540. + /* bad tags, need to look more closely */
  8541. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8542. + } else if (etags.chunk_used) {
  8543. + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8544. + seqnum = etags.seq_number;
  8545. + } else {
  8546. + state = YAFFS_BLOCK_STATE_EMPTY;
  8547. + }
  8548. +
  8549. + *state_ptr = state;
  8550. + *seq_ptr = seqnum;
  8551. +
  8552. + /* query always succeeds */
  8553. + return YAFFS_OK;
  8554. +}
  8555. +
  8556. +#endif /*MTD_VERSION */
  8557. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.h linux-2.6.36/fs/yaffs2/yaffs_mtdif1.h
  8558. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
  8559. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif1.h 2011-01-10 19:29:29.000000000 +0100
  8560. @@ -0,0 +1,29 @@
  8561. +/*
  8562. + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
  8563. + *
  8564. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8565. + * for Toby Churchill Ltd and Brightstar Engineering
  8566. + *
  8567. + * This program is free software; you can redistribute it and/or modify
  8568. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8569. + * published by the Free Software Foundation.
  8570. + *
  8571. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8572. + */
  8573. +
  8574. +#ifndef __YAFFS_MTDIF1_H__
  8575. +#define __YAFFS_MTDIF1_H__
  8576. +
  8577. +int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8578. + const u8 * data,
  8579. + const struct yaffs_ext_tags *tags);
  8580. +
  8581. +int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8582. + u8 * data, struct yaffs_ext_tags *tags);
  8583. +
  8584. +int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no);
  8585. +
  8586. +int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
  8587. + enum yaffs_block_state *state, u32 * seq_number);
  8588. +
  8589. +#endif
  8590. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.c linux-2.6.36/fs/yaffs2/yaffs_mtdif2.c
  8591. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
  8592. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif2.c 2011-01-10 19:29:29.000000000 +0100
  8593. @@ -0,0 +1,261 @@
  8594. +/*
  8595. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8596. + *
  8597. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8598. + * for Toby Churchill Ltd and Brightstar Engineering
  8599. + *
  8600. + * Created by Charles Manning <charles@aleph1.co.uk>
  8601. + *
  8602. + * This program is free software; you can redistribute it and/or modify
  8603. + * it under the terms of the GNU General Public License version 2 as
  8604. + * published by the Free Software Foundation.
  8605. + */
  8606. +
  8607. +/* mtd interface for YAFFS2 */
  8608. +
  8609. +#include "yportenv.h"
  8610. +#include "yaffs_trace.h"
  8611. +
  8612. +#include "yaffs_mtdif2.h"
  8613. +
  8614. +#include "linux/mtd/mtd.h"
  8615. +#include "linux/types.h"
  8616. +#include "linux/time.h"
  8617. +
  8618. +#include "yaffs_packedtags2.h"
  8619. +
  8620. +#include "yaffs_linux.h"
  8621. +
  8622. +/* NB For use with inband tags....
  8623. + * We assume that the data buffer is of size total_bytes_per_chunk so that we can also
  8624. + * use it to load the tags.
  8625. + */
  8626. +int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8627. + const u8 * data,
  8628. + const struct yaffs_ext_tags *tags)
  8629. +{
  8630. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8631. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8632. + struct mtd_oob_ops ops;
  8633. +#else
  8634. + size_t dummy;
  8635. +#endif
  8636. + int retval = 0;
  8637. +
  8638. + loff_t addr;
  8639. +
  8640. + struct yaffs_packed_tags2 pt;
  8641. +
  8642. + int packed_tags_size =
  8643. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  8644. + void *packed_tags_ptr =
  8645. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  8646. +
  8647. + T(YAFFS_TRACE_MTD,
  8648. + (TSTR
  8649. + ("nandmtd2_write_chunk_tags chunk %d data %p tags %p"
  8650. + TENDSTR), nand_chunk, data, tags));
  8651. +
  8652. + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8653. +
  8654. + /* For yaffs2 writing there must be both data and tags.
  8655. + * If we're using inband tags, then the tags are stuffed into
  8656. + * the end of the data buffer.
  8657. + */
  8658. + if (!data || !tags)
  8659. + BUG();
  8660. + else if (dev->param.inband_tags) {
  8661. + struct yaffs_packed_tags2_tags_only *pt2tp;
  8662. + pt2tp =
  8663. + (struct yaffs_packed_tags2_tags_only *)(data +
  8664. + dev->
  8665. + data_bytes_per_chunk);
  8666. + yaffs_pack_tags2_tags_only(pt2tp, tags);
  8667. + } else {
  8668. + yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
  8669. + }
  8670. +
  8671. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  8672. + ops.mode = MTD_OOB_AUTO;
  8673. + ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
  8674. + ops.len = dev->param.total_bytes_per_chunk;
  8675. + ops.ooboffs = 0;
  8676. + ops.datbuf = (u8 *) data;
  8677. + ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
  8678. + retval = mtd->write_oob(mtd, addr, &ops);
  8679. +
  8680. +#else
  8681. + if (!dev->param.inband_tags) {
  8682. + retval =
  8683. + mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
  8684. + &dummy, data, (u8 *) packed_tags_ptr, NULL);
  8685. + } else {
  8686. + retval =
  8687. + mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
  8688. + &dummy, data);
  8689. + }
  8690. +#endif
  8691. +
  8692. + if (retval == 0)
  8693. + return YAFFS_OK;
  8694. + else
  8695. + return YAFFS_FAIL;
  8696. +}
  8697. +
  8698. +int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8699. + u8 * data, struct yaffs_ext_tags *tags)
  8700. +{
  8701. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8702. +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
  8703. + struct mtd_oob_ops ops;
  8704. +#endif
  8705. + size_t dummy;
  8706. + int retval = 0;
  8707. + int local_data = 0;
  8708. +
  8709. + loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8710. +
  8711. + struct yaffs_packed_tags2 pt;
  8712. +
  8713. + int packed_tags_size =
  8714. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  8715. + void *packed_tags_ptr =
  8716. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  8717. +
  8718. + T(YAFFS_TRACE_MTD,
  8719. + (TSTR
  8720. + ("nandmtd2_read_chunk_tags chunk %d data %p tags %p"
  8721. + TENDSTR), nand_chunk, data, tags));
  8722. +
  8723. + if (dev->param.inband_tags) {
  8724. +
  8725. + if (!data) {
  8726. + local_data = 1;
  8727. + data = yaffs_get_temp_buffer(dev, __LINE__);
  8728. + }
  8729. +
  8730. + }
  8731. +
  8732. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  8733. + if (dev->param.inband_tags || (data && !tags))
  8734. + retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
  8735. + &dummy, data);
  8736. + else if (tags) {
  8737. + ops.mode = MTD_OOB_AUTO;
  8738. + ops.ooblen = packed_tags_size;
  8739. + ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
  8740. + ops.ooboffs = 0;
  8741. + ops.datbuf = data;
  8742. + ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
  8743. + retval = mtd->read_oob(mtd, addr, &ops);
  8744. + }
  8745. +#else
  8746. + if (!dev->param.inband_tags && data && tags) {
  8747. +
  8748. + retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
  8749. + &dummy, data, dev->spare_buffer, NULL);
  8750. + } else {
  8751. + if (data)
  8752. + retval =
  8753. + mtd->read(mtd, addr, dev->data_bytes_per_chunk,
  8754. + &dummy, data);
  8755. + if (!dev->param.inband_tags && tags)
  8756. + retval =
  8757. + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
  8758. + dev->spare_buffer);
  8759. + }
  8760. +#endif
  8761. +
  8762. + if (dev->param.inband_tags) {
  8763. + if (tags) {
  8764. + struct yaffs_packed_tags2_tags_only *pt2tp;
  8765. + pt2tp =
  8766. + (struct yaffs_packed_tags2_tags_only *)&data[dev->
  8767. + data_bytes_per_chunk];
  8768. + yaffs_unpack_tags2_tags_only(tags, pt2tp);
  8769. + }
  8770. + } else {
  8771. + if (tags) {
  8772. + memcpy(packed_tags_ptr,
  8773. + yaffs_dev_to_lc(dev)->spare_buffer,
  8774. + packed_tags_size);
  8775. + yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  8776. + }
  8777. + }
  8778. +
  8779. + if (local_data)
  8780. + yaffs_release_temp_buffer(dev, data, __LINE__);
  8781. +
  8782. + if (tags && retval == -EBADMSG
  8783. + && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  8784. + tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  8785. + dev->n_ecc_unfixed++;
  8786. + }
  8787. + if (tags && retval == -EUCLEAN
  8788. + && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  8789. + tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  8790. + dev->n_ecc_fixed++;
  8791. + }
  8792. + if (retval == 0)
  8793. + return YAFFS_OK;
  8794. + else
  8795. + return YAFFS_FAIL;
  8796. +}
  8797. +
  8798. +int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
  8799. +{
  8800. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8801. + int retval;
  8802. + T(YAFFS_TRACE_MTD,
  8803. + (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no));
  8804. +
  8805. + retval =
  8806. + mtd->block_markbad(mtd,
  8807. + block_no * dev->param.chunks_per_block *
  8808. + dev->param.total_bytes_per_chunk);
  8809. +
  8810. + if (retval == 0)
  8811. + return YAFFS_OK;
  8812. + else
  8813. + return YAFFS_FAIL;
  8814. +
  8815. +}
  8816. +
  8817. +int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  8818. + enum yaffs_block_state *state, u32 * seq_number)
  8819. +{
  8820. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8821. + int retval;
  8822. +
  8823. + T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no));
  8824. + retval =
  8825. + mtd->block_isbad(mtd,
  8826. + block_no * dev->param.chunks_per_block *
  8827. + dev->param.total_bytes_per_chunk);
  8828. +
  8829. + if (retval) {
  8830. + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
  8831. +
  8832. + *state = YAFFS_BLOCK_STATE_DEAD;
  8833. + *seq_number = 0;
  8834. + } else {
  8835. + struct yaffs_ext_tags t;
  8836. + nandmtd2_read_chunk_tags(dev, block_no *
  8837. + dev->param.chunks_per_block, NULL, &t);
  8838. +
  8839. + if (t.chunk_used) {
  8840. + *seq_number = t.seq_number;
  8841. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  8842. + } else {
  8843. + *seq_number = 0;
  8844. + *state = YAFFS_BLOCK_STATE_EMPTY;
  8845. + }
  8846. + }
  8847. + T(YAFFS_TRACE_MTD,
  8848. + (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state));
  8849. +
  8850. + if (retval == 0)
  8851. + return YAFFS_OK;
  8852. + else
  8853. + return YAFFS_FAIL;
  8854. +}
  8855. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.h linux-2.6.36/fs/yaffs2/yaffs_mtdif2.h
  8856. --- linux-2.6.36.orig/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
  8857. +++ linux-2.6.36/fs/yaffs2/yaffs_mtdif2.h 2011-01-10 19:29:29.000000000 +0100
  8858. @@ -0,0 +1,29 @@
  8859. +/*
  8860. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8861. + *
  8862. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8863. + * for Toby Churchill Ltd and Brightstar Engineering
  8864. + *
  8865. + * Created by Charles Manning <charles@aleph1.co.uk>
  8866. + *
  8867. + * This program is free software; you can redistribute it and/or modify
  8868. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8869. + * published by the Free Software Foundation.
  8870. + *
  8871. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8872. + */
  8873. +
  8874. +#ifndef __YAFFS_MTDIF2_H__
  8875. +#define __YAFFS_MTDIF2_H__
  8876. +
  8877. +#include "yaffs_guts.h"
  8878. +int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8879. + const u8 * data,
  8880. + const struct yaffs_ext_tags *tags);
  8881. +int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  8882. + u8 * data, struct yaffs_ext_tags *tags);
  8883. +int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no);
  8884. +int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
  8885. + enum yaffs_block_state *state, u32 * seq_number);
  8886. +
  8887. +#endif
  8888. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.c linux-2.6.36/fs/yaffs2/yaffs_nameval.c
  8889. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.c 1970-01-01 01:00:00.000000000 +0100
  8890. +++ linux-2.6.36/fs/yaffs2/yaffs_nameval.c 2011-01-10 19:29:29.000000000 +0100
  8891. @@ -0,0 +1,201 @@
  8892. +/*
  8893. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8894. + *
  8895. + * Copyright (C) 2002-2010 Aleph One Ltd.
  8896. + * for Toby Churchill Ltd and Brightstar Engineering
  8897. + *
  8898. + * Created by Charles Manning <charles@aleph1.co.uk>
  8899. + *
  8900. + * This program is free software; you can redistribute it and/or modify
  8901. + * it under the terms of the GNU General Public License version 2 as
  8902. + * published by the Free Software Foundation.
  8903. + */
  8904. +
  8905. +/*
  8906. + * This simple implementation of a name-value store assumes a small number of values and fits
  8907. + * into a small finite buffer.
  8908. + *
  8909. + * Each attribute is stored as a record:
  8910. + * sizeof(int) bytes record size.
  8911. + * strnlen+1 bytes name null terminated.
  8912. + * nbytes value.
  8913. + * ----------
  8914. + * total size stored in record size
  8915. + *
  8916. + * This code has not been tested with unicode yet.
  8917. + */
  8918. +
  8919. +#include "yaffs_nameval.h"
  8920. +
  8921. +#include "yportenv.h"
  8922. +
  8923. +static int nval_find(const char *xb, int xb_size, const YCHAR * name,
  8924. + int *exist_size)
  8925. +{
  8926. + int pos = 0;
  8927. + int size;
  8928. +
  8929. + memcpy(&size, xb, sizeof(int));
  8930. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8931. + if (yaffs_strncmp
  8932. + ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) {
  8933. + if (exist_size)
  8934. + *exist_size = size;
  8935. + return pos;
  8936. + }
  8937. + pos += size;
  8938. + if (pos < xb_size - sizeof(int))
  8939. + memcpy(&size, xb + pos, sizeof(int));
  8940. + else
  8941. + size = 0;
  8942. + }
  8943. + if (exist_size)
  8944. + *exist_size = 0;
  8945. + return -1;
  8946. +}
  8947. +
  8948. +static int nval_used(const char *xb, int xb_size)
  8949. +{
  8950. + int pos = 0;
  8951. + int size;
  8952. +
  8953. + memcpy(&size, xb + pos, sizeof(int));
  8954. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8955. + pos += size;
  8956. + if (pos < xb_size - sizeof(int))
  8957. + memcpy(&size, xb + pos, sizeof(int));
  8958. + else
  8959. + size = 0;
  8960. + }
  8961. + return pos;
  8962. +}
  8963. +
  8964. +int nval_del(char *xb, int xb_size, const YCHAR * name)
  8965. +{
  8966. + int pos = nval_find(xb, xb_size, name, NULL);
  8967. + int size;
  8968. +
  8969. + if (pos >= 0 && pos < xb_size) {
  8970. + /* Find size, shift rest over this record, then zero out the rest of buffer */
  8971. + memcpy(&size, xb + pos, sizeof(int));
  8972. + memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
  8973. + memset(xb + (xb_size - size), 0, size);
  8974. + return 0;
  8975. + } else {
  8976. + return -ENODATA;
  8977. + }
  8978. +}
  8979. +
  8980. +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
  8981. + int bsize, int flags)
  8982. +{
  8983. + int pos;
  8984. + int namelen = yaffs_strnlen(name, xb_size);
  8985. + int reclen;
  8986. + int size_exist = 0;
  8987. + int space;
  8988. + int start;
  8989. +
  8990. + pos = nval_find(xb, xb_size, name, &size_exist);
  8991. +
  8992. + if (flags & XATTR_CREATE && pos >= 0)
  8993. + return -EEXIST;
  8994. + if (flags & XATTR_REPLACE && pos < 0)
  8995. + return -ENODATA;
  8996. +
  8997. + start = nval_used(xb, xb_size);
  8998. + space = xb_size - start + size_exist;
  8999. +
  9000. + reclen = (sizeof(int) + namelen + 1 + bsize);
  9001. +
  9002. + if (reclen > space)
  9003. + return -ENOSPC;
  9004. +
  9005. + if (pos >= 0) {
  9006. + nval_del(xb, xb_size, name);
  9007. + start = nval_used(xb, xb_size);
  9008. + }
  9009. +
  9010. + pos = start;
  9011. +
  9012. + memcpy(xb + pos, &reclen, sizeof(int));
  9013. + pos += sizeof(int);
  9014. + yaffs_strncpy((YCHAR *) (xb + pos), name, reclen);
  9015. + pos += (namelen + 1);
  9016. + memcpy(xb + pos, buf, bsize);
  9017. + return 0;
  9018. +}
  9019. +
  9020. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  9021. + int bsize)
  9022. +{
  9023. + int pos = nval_find(xb, xb_size, name, NULL);
  9024. + int size;
  9025. +
  9026. + if (pos >= 0 && pos < xb_size) {
  9027. +
  9028. + memcpy(&size, xb + pos, sizeof(int));
  9029. + pos += sizeof(int); /* advance past record length */
  9030. + size -= sizeof(int);
  9031. +
  9032. + /* Advance over name string */
  9033. + while (xb[pos] && size > 0 && pos < xb_size) {
  9034. + pos++;
  9035. + size--;
  9036. + }
  9037. + /*Advance over NUL */
  9038. + pos++;
  9039. + size--;
  9040. +
  9041. + if (size <= bsize) {
  9042. + memcpy(buf, xb + pos, size);
  9043. + return size;
  9044. + }
  9045. +
  9046. + }
  9047. + if (pos >= 0)
  9048. + return -ERANGE;
  9049. + else
  9050. + return -ENODATA;
  9051. +}
  9052. +
  9053. +int nval_list(const char *xb, int xb_size, char *buf, int bsize)
  9054. +{
  9055. + int pos = 0;
  9056. + int size;
  9057. + int name_len;
  9058. + int ncopied = 0;
  9059. + int filled = 0;
  9060. +
  9061. + memcpy(&size, xb + pos, sizeof(int));
  9062. + while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size
  9063. + && !filled) {
  9064. + pos += sizeof(int);
  9065. + size -= sizeof(int);
  9066. + name_len = yaffs_strnlen((YCHAR *) (xb + pos), size);
  9067. + if (ncopied + name_len + 1 < bsize) {
  9068. + memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
  9069. + buf += name_len;
  9070. + *buf = '\0';
  9071. + buf++;
  9072. + if (sizeof(YCHAR) > 1) {
  9073. + *buf = '\0';
  9074. + buf++;
  9075. + }
  9076. + ncopied += (name_len + 1);
  9077. + } else {
  9078. + filled = 1;
  9079. + }
  9080. + pos += size;
  9081. + if (pos < xb_size - sizeof(int))
  9082. + memcpy(&size, xb + pos, sizeof(int));
  9083. + else
  9084. + size = 0;
  9085. + }
  9086. + return ncopied;
  9087. +}
  9088. +
  9089. +int nval_hasvalues(const char *xb, int xb_size)
  9090. +{
  9091. + return nval_used(xb, xb_size) > 0;
  9092. +}
  9093. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.h linux-2.6.36/fs/yaffs2/yaffs_nameval.h
  9094. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nameval.h 1970-01-01 01:00:00.000000000 +0100
  9095. +++ linux-2.6.36/fs/yaffs2/yaffs_nameval.h 2011-01-10 19:29:29.000000000 +0100
  9096. @@ -0,0 +1,28 @@
  9097. +/*
  9098. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9099. + *
  9100. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9101. + * for Toby Churchill Ltd and Brightstar Engineering
  9102. + *
  9103. + * Created by Charles Manning <charles@aleph1.co.uk>
  9104. + *
  9105. + * This program is free software; you can redistribute it and/or modify
  9106. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9107. + * published by the Free Software Foundation.
  9108. + *
  9109. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9110. + */
  9111. +
  9112. +#ifndef __NAMEVAL_H__
  9113. +#define __NAMEVAL_H__
  9114. +
  9115. +#include "yportenv.h"
  9116. +
  9117. +int nval_del(char *xb, int xb_size, const YCHAR * name);
  9118. +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
  9119. + int bsize, int flags);
  9120. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  9121. + int bsize);
  9122. +int nval_list(const char *xb, int xb_size, char *buf, int bsize);
  9123. +int nval_hasvalues(const char *xb, int xb_size);
  9124. +#endif
  9125. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nand.c linux-2.6.36/fs/yaffs2/yaffs_nand.c
  9126. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  9127. +++ linux-2.6.36/fs/yaffs2/yaffs_nand.c 2011-01-10 19:29:29.000000000 +0100
  9128. @@ -0,0 +1,128 @@
  9129. +/*
  9130. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9131. + *
  9132. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9133. + * for Toby Churchill Ltd and Brightstar Engineering
  9134. + *
  9135. + * Created by Charles Manning <charles@aleph1.co.uk>
  9136. + *
  9137. + * This program is free software; you can redistribute it and/or modify
  9138. + * it under the terms of the GNU General Public License version 2 as
  9139. + * published by the Free Software Foundation.
  9140. + */
  9141. +
  9142. +#include "yaffs_nand.h"
  9143. +#include "yaffs_tagscompat.h"
  9144. +#include "yaffs_tagsvalidity.h"
  9145. +
  9146. +#include "yaffs_getblockinfo.h"
  9147. +
  9148. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  9149. + u8 * buffer, struct yaffs_ext_tags *tags)
  9150. +{
  9151. + int result;
  9152. + struct yaffs_ext_tags local_tags;
  9153. +
  9154. + int realigned_chunk = nand_chunk - dev->chunk_offset;
  9155. +
  9156. + dev->n_page_reads++;
  9157. +
  9158. + /* If there are no tags provided, use local tags to get prioritised gc working */
  9159. + if (!tags)
  9160. + tags = &local_tags;
  9161. +
  9162. + if (dev->param.read_chunk_tags_fn)
  9163. + result =
  9164. + dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer,
  9165. + tags);
  9166. + else
  9167. + result = yaffs_tags_compat_rd(dev,
  9168. + realigned_chunk, buffer, tags);
  9169. + if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
  9170. +
  9171. + struct yaffs_block_info *bi;
  9172. + bi = yaffs_get_block_info(dev,
  9173. + nand_chunk /
  9174. + dev->param.chunks_per_block);
  9175. + yaffs_handle_chunk_error(dev, bi);
  9176. + }
  9177. +
  9178. + return result;
  9179. +}
  9180. +
  9181. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  9182. + int nand_chunk,
  9183. + const u8 * buffer, struct yaffs_ext_tags *tags)
  9184. +{
  9185. +
  9186. + dev->n_page_writes++;
  9187. +
  9188. + nand_chunk -= dev->chunk_offset;
  9189. +
  9190. + if (tags) {
  9191. + tags->seq_number = dev->seq_number;
  9192. + tags->chunk_used = 1;
  9193. + if (!yaffs_validate_tags(tags)) {
  9194. + T(YAFFS_TRACE_ERROR,
  9195. + (TSTR("Writing uninitialised tags" TENDSTR)));
  9196. + YBUG();
  9197. + }
  9198. + T(YAFFS_TRACE_WRITE,
  9199. + (TSTR("Writing chunk %d tags %d %d" TENDSTR), nand_chunk,
  9200. + tags->obj_id, tags->chunk_id));
  9201. + } else {
  9202. + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
  9203. + YBUG();
  9204. + }
  9205. +
  9206. + if (dev->param.write_chunk_tags_fn)
  9207. + return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
  9208. + tags);
  9209. + else
  9210. + return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags);
  9211. +}
  9212. +
  9213. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
  9214. +{
  9215. + block_no -= dev->block_offset;
  9216. +
  9217. + if (dev->param.bad_block_fn)
  9218. + return dev->param.bad_block_fn(dev, block_no);
  9219. + else
  9220. + return yaffs_tags_compat_mark_bad(dev, block_no);
  9221. +}
  9222. +
  9223. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  9224. + int block_no,
  9225. + enum yaffs_block_state *state,
  9226. + u32 * seq_number)
  9227. +{
  9228. + block_no -= dev->block_offset;
  9229. +
  9230. + if (dev->param.query_block_fn)
  9231. + return dev->param.query_block_fn(dev, block_no, state,
  9232. + seq_number);
  9233. + else
  9234. + return yaffs_tags_compat_query_block(dev, block_no,
  9235. + state, seq_number);
  9236. +}
  9237. +
  9238. +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block)
  9239. +{
  9240. + int result;
  9241. +
  9242. + flash_block -= dev->block_offset;
  9243. +
  9244. + dev->n_erasures++;
  9245. +
  9246. + result = dev->param.erase_fn(dev, flash_block);
  9247. +
  9248. + return result;
  9249. +}
  9250. +
  9251. +int yaffs_init_nand(struct yaffs_dev *dev)
  9252. +{
  9253. + if (dev->param.initialise_flash_fn)
  9254. + return dev->param.initialise_flash_fn(dev);
  9255. + return YAFFS_OK;
  9256. +}
  9257. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_nand.h linux-2.6.36/fs/yaffs2/yaffs_nand.h
  9258. --- linux-2.6.36.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  9259. +++ linux-2.6.36/fs/yaffs2/yaffs_nand.h 2011-01-10 19:29:29.000000000 +0100
  9260. @@ -0,0 +1,38 @@
  9261. +/*
  9262. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9263. + *
  9264. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9265. + * for Toby Churchill Ltd and Brightstar Engineering
  9266. + *
  9267. + * Created by Charles Manning <charles@aleph1.co.uk>
  9268. + *
  9269. + * This program is free software; you can redistribute it and/or modify
  9270. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9271. + * published by the Free Software Foundation.
  9272. + *
  9273. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9274. + */
  9275. +
  9276. +#ifndef __YAFFS_NAND_H__
  9277. +#define __YAFFS_NAND_H__
  9278. +#include "yaffs_guts.h"
  9279. +
  9280. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  9281. + u8 * buffer, struct yaffs_ext_tags *tags);
  9282. +
  9283. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  9284. + int nand_chunk,
  9285. + const u8 * buffer, struct yaffs_ext_tags *tags);
  9286. +
  9287. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no);
  9288. +
  9289. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  9290. + int block_no,
  9291. + enum yaffs_block_state *state,
  9292. + unsigned *seq_number);
  9293. +
  9294. +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block);
  9295. +
  9296. +int yaffs_init_nand(struct yaffs_dev *dev);
  9297. +
  9298. +#endif
  9299. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.c linux-2.6.36/fs/yaffs2/yaffs_packedtags1.c
  9300. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  9301. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags1.c 2011-01-10 19:29:29.000000000 +0100
  9302. @@ -0,0 +1,53 @@
  9303. +/*
  9304. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9305. + *
  9306. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9307. + * for Toby Churchill Ltd and Brightstar Engineering
  9308. + *
  9309. + * Created by Charles Manning <charles@aleph1.co.uk>
  9310. + *
  9311. + * This program is free software; you can redistribute it and/or modify
  9312. + * it under the terms of the GNU General Public License version 2 as
  9313. + * published by the Free Software Foundation.
  9314. + */
  9315. +
  9316. +#include "yaffs_packedtags1.h"
  9317. +#include "yportenv.h"
  9318. +
  9319. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  9320. + const struct yaffs_ext_tags *t)
  9321. +{
  9322. + pt->chunk_id = t->chunk_id;
  9323. + pt->serial_number = t->serial_number;
  9324. + pt->n_bytes = t->n_bytes;
  9325. + pt->obj_id = t->obj_id;
  9326. + pt->ecc = 0;
  9327. + pt->deleted = (t->is_deleted) ? 0 : 1;
  9328. + pt->unused_stuff = 0;
  9329. + pt->should_be_ff = 0xFFFFFFFF;
  9330. +
  9331. +}
  9332. +
  9333. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  9334. + const struct yaffs_packed_tags1 *pt)
  9335. +{
  9336. + static const u8 all_ff[] =
  9337. + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9338. + 0xff
  9339. + };
  9340. +
  9341. + if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) {
  9342. + t->block_bad = 0;
  9343. + if (pt->should_be_ff != 0xFFFFFFFF)
  9344. + t->block_bad = 1;
  9345. + t->chunk_used = 1;
  9346. + t->obj_id = pt->obj_id;
  9347. + t->chunk_id = pt->chunk_id;
  9348. + t->n_bytes = pt->n_bytes;
  9349. + t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9350. + t->is_deleted = (pt->deleted) ? 0 : 1;
  9351. + t->serial_number = pt->serial_number;
  9352. + } else {
  9353. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  9354. + }
  9355. +}
  9356. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.h linux-2.6.36/fs/yaffs2/yaffs_packedtags1.h
  9357. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  9358. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags1.h 2011-01-10 19:29:29.000000000 +0100
  9359. @@ -0,0 +1,39 @@
  9360. +/*
  9361. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9362. + *
  9363. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9364. + * for Toby Churchill Ltd and Brightstar Engineering
  9365. + *
  9366. + * Created by Charles Manning <charles@aleph1.co.uk>
  9367. + *
  9368. + * This program is free software; you can redistribute it and/or modify
  9369. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9370. + * published by the Free Software Foundation.
  9371. + *
  9372. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9373. + */
  9374. +
  9375. +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
  9376. +
  9377. +#ifndef __YAFFS_PACKEDTAGS1_H__
  9378. +#define __YAFFS_PACKEDTAGS1_H__
  9379. +
  9380. +#include "yaffs_guts.h"
  9381. +
  9382. +struct yaffs_packed_tags1 {
  9383. + unsigned chunk_id:20;
  9384. + unsigned serial_number:2;
  9385. + unsigned n_bytes:10;
  9386. + unsigned obj_id:18;
  9387. + unsigned ecc:12;
  9388. + unsigned deleted:1;
  9389. + unsigned unused_stuff:1;
  9390. + unsigned should_be_ff;
  9391. +
  9392. +};
  9393. +
  9394. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  9395. + const struct yaffs_ext_tags *t);
  9396. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  9397. + const struct yaffs_packed_tags1 *pt);
  9398. +#endif
  9399. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.c linux-2.6.36/fs/yaffs2/yaffs_packedtags2.c
  9400. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  9401. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags2.c 2011-01-10 19:29:29.000000000 +0100
  9402. @@ -0,0 +1,197 @@
  9403. +/*
  9404. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9405. + *
  9406. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9407. + * for Toby Churchill Ltd and Brightstar Engineering
  9408. + *
  9409. + * Created by Charles Manning <charles@aleph1.co.uk>
  9410. + *
  9411. + * This program is free software; you can redistribute it and/or modify
  9412. + * it under the terms of the GNU General Public License version 2 as
  9413. + * published by the Free Software Foundation.
  9414. + */
  9415. +
  9416. +#include "yaffs_packedtags2.h"
  9417. +#include "yportenv.h"
  9418. +#include "yaffs_trace.h"
  9419. +#include "yaffs_tagsvalidity.h"
  9420. +
  9421. +/* This code packs a set of extended tags into a binary structure for
  9422. + * NAND storage
  9423. + */
  9424. +
  9425. +/* Some of the information is "extra" struff which can be packed in to
  9426. + * speed scanning
  9427. + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
  9428. + */
  9429. +
  9430. +/* Extra flags applied to chunk_id */
  9431. +
  9432. +#define EXTRA_HEADER_INFO_FLAG 0x80000000
  9433. +#define EXTRA_SHRINK_FLAG 0x40000000
  9434. +#define EXTRA_SHADOWS_FLAG 0x20000000
  9435. +#define EXTRA_SPARE_FLAGS 0x10000000
  9436. +
  9437. +#define ALL_EXTRA_FLAGS 0xF0000000
  9438. +
  9439. +/* Also, the top 4 bits of the object Id are set to the object type. */
  9440. +#define EXTRA_OBJECT_TYPE_SHIFT (28)
  9441. +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
  9442. +
  9443. +static void yaffs_dump_packed_tags2_tags_only(const struct
  9444. + yaffs_packed_tags2_tags_only *ptt)
  9445. +{
  9446. + T(YAFFS_TRACE_MTD,
  9447. + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
  9448. + ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number));
  9449. +}
  9450. +
  9451. +static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
  9452. +{
  9453. + yaffs_dump_packed_tags2_tags_only(&pt->t);
  9454. +}
  9455. +
  9456. +static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
  9457. +{
  9458. + T(YAFFS_TRACE_MTD,
  9459. + (TSTR
  9460. + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
  9461. + TENDSTR), t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
  9462. + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
  9463. + t->seq_number));
  9464. +
  9465. +}
  9466. +
  9467. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
  9468. + const struct yaffs_ext_tags *t)
  9469. +{
  9470. + ptt->chunk_id = t->chunk_id;
  9471. + ptt->seq_number = t->seq_number;
  9472. + ptt->n_bytes = t->n_bytes;
  9473. + ptt->obj_id = t->obj_id;
  9474. +
  9475. + if (t->chunk_id == 0 && t->extra_available) {
  9476. + /* Store the extra header info instead */
  9477. + /* We save the parent object in the chunk_id */
  9478. + ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
  9479. + if (t->extra_is_shrink)
  9480. + ptt->chunk_id |= EXTRA_SHRINK_FLAG;
  9481. + if (t->extra_shadows)
  9482. + ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
  9483. +
  9484. + ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9485. + ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
  9486. +
  9487. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9488. + ptt->n_bytes = t->extra_equiv_id;
  9489. + else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  9490. + ptt->n_bytes = t->extra_length;
  9491. + else
  9492. + ptt->n_bytes = 0;
  9493. + }
  9494. +
  9495. + yaffs_dump_packed_tags2_tags_only(ptt);
  9496. + yaffs_dump_tags2(t);
  9497. +}
  9498. +
  9499. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9500. + const struct yaffs_ext_tags *t, int tags_ecc)
  9501. +{
  9502. + yaffs_pack_tags2_tags_only(&pt->t, t);
  9503. +
  9504. + if (tags_ecc)
  9505. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9506. + sizeof(struct
  9507. + yaffs_packed_tags2_tags_only),
  9508. + &pt->ecc);
  9509. +}
  9510. +
  9511. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9512. + struct yaffs_packed_tags2_tags_only *ptt)
  9513. +{
  9514. +
  9515. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  9516. +
  9517. + yaffs_init_tags(t);
  9518. +
  9519. + if (ptt->seq_number != 0xFFFFFFFF) {
  9520. + t->block_bad = 0;
  9521. + t->chunk_used = 1;
  9522. + t->obj_id = ptt->obj_id;
  9523. + t->chunk_id = ptt->chunk_id;
  9524. + t->n_bytes = ptt->n_bytes;
  9525. + t->is_deleted = 0;
  9526. + t->serial_number = 0;
  9527. + t->seq_number = ptt->seq_number;
  9528. +
  9529. + /* Do extra header info stuff */
  9530. +
  9531. + if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
  9532. + t->chunk_id = 0;
  9533. + t->n_bytes = 0;
  9534. +
  9535. + t->extra_available = 1;
  9536. + t->extra_parent_id =
  9537. + ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
  9538. + t->extra_is_shrink =
  9539. + (ptt->chunk_id & EXTRA_SHRINK_FLAG) ? 1 : 0;
  9540. + t->extra_shadows =
  9541. + (ptt->chunk_id & EXTRA_SHADOWS_FLAG) ? 1 : 0;
  9542. + t->extra_obj_type =
  9543. + ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
  9544. + t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9545. +
  9546. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9547. + t->extra_equiv_id = ptt->n_bytes;
  9548. + else
  9549. + t->extra_length = ptt->n_bytes;
  9550. + }
  9551. + }
  9552. +
  9553. + yaffs_dump_packed_tags2_tags_only(ptt);
  9554. + yaffs_dump_tags2(t);
  9555. +
  9556. +}
  9557. +
  9558. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9559. + int tags_ecc)
  9560. +{
  9561. +
  9562. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9563. +
  9564. + if (pt->t.seq_number != 0xFFFFFFFF && tags_ecc) {
  9565. + /* Chunk is in use and we need to do ECC */
  9566. +
  9567. + struct yaffs_ecc_other ecc;
  9568. + int result;
  9569. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9570. + sizeof(struct
  9571. + yaffs_packed_tags2_tags_only),
  9572. + &ecc);
  9573. + result =
  9574. + yaffs_ecc_correct_other((unsigned char *)&pt->t,
  9575. + sizeof(struct
  9576. + yaffs_packed_tags2_tags_only),
  9577. + &pt->ecc, &ecc);
  9578. + switch (result) {
  9579. + case 0:
  9580. + ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9581. + break;
  9582. + case 1:
  9583. + ecc_result = YAFFS_ECC_RESULT_FIXED;
  9584. + break;
  9585. + case -1:
  9586. + ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9587. + break;
  9588. + default:
  9589. + ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  9590. + }
  9591. + }
  9592. +
  9593. + yaffs_unpack_tags2_tags_only(t, &pt->t);
  9594. +
  9595. + t->ecc_result = ecc_result;
  9596. +
  9597. + yaffs_dump_packed_tags2(pt);
  9598. + yaffs_dump_tags2(t);
  9599. +}
  9600. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.h linux-2.6.36/fs/yaffs2/yaffs_packedtags2.h
  9601. --- linux-2.6.36.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  9602. +++ linux-2.6.36/fs/yaffs2/yaffs_packedtags2.h 2011-01-10 19:29:29.000000000 +0100
  9603. @@ -0,0 +1,47 @@
  9604. +/*
  9605. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9606. + *
  9607. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9608. + * for Toby Churchill Ltd and Brightstar Engineering
  9609. + *
  9610. + * Created by Charles Manning <charles@aleph1.co.uk>
  9611. + *
  9612. + * This program is free software; you can redistribute it and/or modify
  9613. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9614. + * published by the Free Software Foundation.
  9615. + *
  9616. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9617. + */
  9618. +
  9619. +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
  9620. +
  9621. +#ifndef __YAFFS_PACKEDTAGS2_H__
  9622. +#define __YAFFS_PACKEDTAGS2_H__
  9623. +
  9624. +#include "yaffs_guts.h"
  9625. +#include "yaffs_ecc.h"
  9626. +
  9627. +struct yaffs_packed_tags2_tags_only {
  9628. + unsigned seq_number;
  9629. + unsigned obj_id;
  9630. + unsigned chunk_id;
  9631. + unsigned n_bytes;
  9632. +};
  9633. +
  9634. +struct yaffs_packed_tags2 {
  9635. + struct yaffs_packed_tags2_tags_only t;
  9636. + struct yaffs_ecc_other ecc;
  9637. +};
  9638. +
  9639. +/* Full packed tags with ECC, used for oob tags */
  9640. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9641. + const struct yaffs_ext_tags *t, int tags_ecc);
  9642. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9643. + int tags_ecc);
  9644. +
  9645. +/* Only the tags part (no ECC for use with inband tags */
  9646. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt,
  9647. + const struct yaffs_ext_tags *t);
  9648. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9649. + struct yaffs_packed_tags2_tags_only *pt);
  9650. +#endif
  9651. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.c linux-2.6.36/fs/yaffs2/yaffs_tagscompat.c
  9652. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  9653. +++ linux-2.6.36/fs/yaffs2/yaffs_tagscompat.c 2011-01-10 19:29:29.000000000 +0100
  9654. @@ -0,0 +1,454 @@
  9655. +/*
  9656. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9657. + *
  9658. + * Copyright (C) 2002-2010 Aleph One Ltd.
  9659. + * for Toby Churchill Ltd and Brightstar Engineering
  9660. + *
  9661. + * Created by Charles Manning <charles@aleph1.co.uk>
  9662. + *
  9663. + * This program is free software; you can redistribute it and/or modify
  9664. + * it under the terms of the GNU General Public License version 2 as
  9665. + * published by the Free Software Foundation.
  9666. + */
  9667. +
  9668. +#include "yaffs_guts.h"
  9669. +#include "yaffs_tagscompat.h"
  9670. +#include "yaffs_ecc.h"
  9671. +#include "yaffs_getblockinfo.h"
  9672. +#include "yaffs_trace.h"
  9673. +
  9674. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
  9675. +
  9676. +static const char yaffs_count_bits_table[256] = {
  9677. + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  9678. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9679. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9680. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9681. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9682. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9683. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9684. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9685. + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  9686. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9687. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9688. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9689. + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  9690. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9691. + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  9692. + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  9693. +};
  9694. +
  9695. +int yaffs_count_bits(u8 x)
  9696. +{
  9697. + int ret_val;
  9698. + ret_val = yaffs_count_bits_table[x];
  9699. + return ret_val;
  9700. +}
  9701. +
  9702. +/********** Tags ECC calculations *********/
  9703. +
  9704. +void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare)
  9705. +{
  9706. + yaffs_ecc_cacl(data, spare->ecc1);
  9707. + yaffs_ecc_cacl(&data[256], spare->ecc2);
  9708. +}
  9709. +
  9710. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
  9711. +{
  9712. + /* Calculate an ecc */
  9713. +
  9714. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9715. + unsigned i, j;
  9716. + unsigned ecc = 0;
  9717. + unsigned bit = 0;
  9718. +
  9719. + tags->ecc = 0;
  9720. +
  9721. + for (i = 0; i < 8; i++) {
  9722. + for (j = 1; j & 0xff; j <<= 1) {
  9723. + bit++;
  9724. + if (b[i] & j)
  9725. + ecc ^= bit;
  9726. + }
  9727. + }
  9728. +
  9729. + tags->ecc = ecc;
  9730. +
  9731. +}
  9732. +
  9733. +int yaffs_check_tags_ecc(struct yaffs_tags *tags)
  9734. +{
  9735. + unsigned ecc = tags->ecc;
  9736. +
  9737. + yaffs_calc_tags_ecc(tags);
  9738. +
  9739. + ecc ^= tags->ecc;
  9740. +
  9741. + if (ecc && ecc <= 64) {
  9742. + /* TODO: Handle the failure better. Retire? */
  9743. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9744. +
  9745. + ecc--;
  9746. +
  9747. + b[ecc / 8] ^= (1 << (ecc & 7));
  9748. +
  9749. + /* Now recvalc the ecc */
  9750. + yaffs_calc_tags_ecc(tags);
  9751. +
  9752. + return 1; /* recovered error */
  9753. + } else if (ecc) {
  9754. + /* Wierd ecc failure value */
  9755. + /* TODO Need to do somethiong here */
  9756. + return -1; /* unrecovered error */
  9757. + }
  9758. +
  9759. + return 0;
  9760. +}
  9761. +
  9762. +/********** Tags **********/
  9763. +
  9764. +static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
  9765. + struct yaffs_tags *tags_ptr)
  9766. +{
  9767. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9768. +
  9769. + yaffs_calc_tags_ecc(tags_ptr);
  9770. +
  9771. + spare_ptr->tb0 = tu->as_bytes[0];
  9772. + spare_ptr->tb1 = tu->as_bytes[1];
  9773. + spare_ptr->tb2 = tu->as_bytes[2];
  9774. + spare_ptr->tb3 = tu->as_bytes[3];
  9775. + spare_ptr->tb4 = tu->as_bytes[4];
  9776. + spare_ptr->tb5 = tu->as_bytes[5];
  9777. + spare_ptr->tb6 = tu->as_bytes[6];
  9778. + spare_ptr->tb7 = tu->as_bytes[7];
  9779. +}
  9780. +
  9781. +static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
  9782. + struct yaffs_spare *spare_ptr,
  9783. + struct yaffs_tags *tags_ptr)
  9784. +{
  9785. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9786. + int result;
  9787. +
  9788. + tu->as_bytes[0] = spare_ptr->tb0;
  9789. + tu->as_bytes[1] = spare_ptr->tb1;
  9790. + tu->as_bytes[2] = spare_ptr->tb2;
  9791. + tu->as_bytes[3] = spare_ptr->tb3;
  9792. + tu->as_bytes[4] = spare_ptr->tb4;
  9793. + tu->as_bytes[5] = spare_ptr->tb5;
  9794. + tu->as_bytes[6] = spare_ptr->tb6;
  9795. + tu->as_bytes[7] = spare_ptr->tb7;
  9796. +
  9797. + result = yaffs_check_tags_ecc(tags_ptr);
  9798. + if (result > 0)
  9799. + dev->n_tags_ecc_fixed++;
  9800. + else if (result < 0)
  9801. + dev->n_tags_ecc_unfixed++;
  9802. +}
  9803. +
  9804. +static void yaffs_spare_init(struct yaffs_spare *spare)
  9805. +{
  9806. + memset(spare, 0xFF, sizeof(struct yaffs_spare));
  9807. +}
  9808. +
  9809. +static int yaffs_wr_nand(struct yaffs_dev *dev,
  9810. + int nand_chunk, const u8 * data,
  9811. + struct yaffs_spare *spare)
  9812. +{
  9813. + if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
  9814. + T(YAFFS_TRACE_ERROR,
  9815. + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
  9816. + nand_chunk));
  9817. + return YAFFS_FAIL;
  9818. + }
  9819. +
  9820. + return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
  9821. +}
  9822. +
  9823. +static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
  9824. + int nand_chunk,
  9825. + u8 * data,
  9826. + struct yaffs_spare *spare,
  9827. + enum yaffs_ecc_result *ecc_result,
  9828. + int correct_errors)
  9829. +{
  9830. + int ret_val;
  9831. + struct yaffs_spare local_spare;
  9832. +
  9833. + if (!spare && data) {
  9834. + /* If we don't have a real spare, then we use a local one. */
  9835. + /* Need this for the calculation of the ecc */
  9836. + spare = &local_spare;
  9837. + }
  9838. +
  9839. + if (!dev->param.use_nand_ecc) {
  9840. + ret_val =
  9841. + dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
  9842. + if (data && correct_errors) {
  9843. + /* Do ECC correction */
  9844. + /* Todo handle any errors */
  9845. + int ecc_result1, ecc_result2;
  9846. + u8 calc_ecc[3];
  9847. +
  9848. + yaffs_ecc_cacl(data, calc_ecc);
  9849. + ecc_result1 =
  9850. + yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
  9851. + yaffs_ecc_cacl(&data[256], calc_ecc);
  9852. + ecc_result2 =
  9853. + yaffs_ecc_correct(&data[256], spare->ecc2,
  9854. + calc_ecc);
  9855. +
  9856. + if (ecc_result1 > 0) {
  9857. + T(YAFFS_TRACE_ERROR,
  9858. + (TSTR
  9859. + ("**>>yaffs ecc error fix performed on chunk %d:0"
  9860. + TENDSTR), nand_chunk));
  9861. + dev->n_ecc_fixed++;
  9862. + } else if (ecc_result1 < 0) {
  9863. + T(YAFFS_TRACE_ERROR,
  9864. + (TSTR
  9865. + ("**>>yaffs ecc error unfixed on chunk %d:0"
  9866. + TENDSTR), nand_chunk));
  9867. + dev->n_ecc_unfixed++;
  9868. + }
  9869. +
  9870. + if (ecc_result2 > 0) {
  9871. + T(YAFFS_TRACE_ERROR,
  9872. + (TSTR
  9873. + ("**>>yaffs ecc error fix performed on chunk %d:1"
  9874. + TENDSTR), nand_chunk));
  9875. + dev->n_ecc_fixed++;
  9876. + } else if (ecc_result2 < 0) {
  9877. + T(YAFFS_TRACE_ERROR,
  9878. + (TSTR
  9879. + ("**>>yaffs ecc error unfixed on chunk %d:1"
  9880. + TENDSTR), nand_chunk));
  9881. + dev->n_ecc_unfixed++;
  9882. + }
  9883. +
  9884. + if (ecc_result1 || ecc_result2) {
  9885. + /* We had a data problem on this page */
  9886. + yaffs_handle_rd_data_error(dev, nand_chunk);
  9887. + }
  9888. +
  9889. + if (ecc_result1 < 0 || ecc_result2 < 0)
  9890. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9891. + else if (ecc_result1 > 0 || ecc_result2 > 0)
  9892. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  9893. + else
  9894. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9895. + }
  9896. + } else {
  9897. + /* Must allocate enough memory for spare+2*sizeof(int) */
  9898. + /* for ecc results from device. */
  9899. + struct yaffs_nand_spare nspare;
  9900. +
  9901. + memset(&nspare, 0, sizeof(nspare));
  9902. +
  9903. + ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
  9904. + (struct yaffs_spare *)
  9905. + &nspare);
  9906. + memcpy(spare, &nspare, sizeof(struct yaffs_spare));
  9907. + if (data && correct_errors) {
  9908. + if (nspare.eccres1 > 0) {
  9909. + T(YAFFS_TRACE_ERROR,
  9910. + (TSTR
  9911. + ("**>>mtd ecc error fix performed on chunk %d:0"
  9912. + TENDSTR), nand_chunk));
  9913. + } else if (nspare.eccres1 < 0) {
  9914. + T(YAFFS_TRACE_ERROR,
  9915. + (TSTR
  9916. + ("**>>mtd ecc error unfixed on chunk %d:0"
  9917. + TENDSTR), nand_chunk));
  9918. + }
  9919. +
  9920. + if (nspare.eccres2 > 0) {
  9921. + T(YAFFS_TRACE_ERROR,
  9922. + (TSTR
  9923. + ("**>>mtd ecc error fix performed on chunk %d:1"
  9924. + TENDSTR), nand_chunk));
  9925. + } else if (nspare.eccres2 < 0) {
  9926. + T(YAFFS_TRACE_ERROR,
  9927. + (TSTR
  9928. + ("**>>mtd ecc error unfixed on chunk %d:1"
  9929. + TENDSTR), nand_chunk));
  9930. + }
  9931. +
  9932. + if (nspare.eccres1 || nspare.eccres2) {
  9933. + /* We had a data problem on this page */
  9934. + yaffs_handle_rd_data_error(dev, nand_chunk);
  9935. + }
  9936. +
  9937. + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
  9938. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9939. + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
  9940. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  9941. + else
  9942. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9943. +
  9944. + }
  9945. + }
  9946. + return ret_val;
  9947. +}
  9948. +
  9949. +/*
  9950. + * Functions for robustisizing
  9951. + */
  9952. +
  9953. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
  9954. +{
  9955. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  9956. +
  9957. + /* Mark the block for retirement */
  9958. + yaffs_get_block_info(dev,
  9959. + flash_block + dev->block_offset)->needs_retiring =
  9960. + 1;
  9961. + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  9962. + (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
  9963. +
  9964. + /* TODO:
  9965. + * Just do a garbage collection on the affected block
  9966. + * then retire the block
  9967. + * NB recursion
  9968. + */
  9969. +}
  9970. +
  9971. +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  9972. + int nand_chunk,
  9973. + const u8 * data, const struct yaffs_ext_tags *ext_tags)
  9974. +{
  9975. + struct yaffs_spare spare;
  9976. + struct yaffs_tags tags;
  9977. +
  9978. + yaffs_spare_init(&spare);
  9979. +
  9980. + if (ext_tags->is_deleted)
  9981. + spare.page_status = 0;
  9982. + else {
  9983. + tags.obj_id = ext_tags->obj_id;
  9984. + tags.chunk_id = ext_tags->chunk_id;
  9985. +
  9986. + tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
  9987. +
  9988. + if (dev->data_bytes_per_chunk >= 1024)
  9989. + tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
  9990. + else
  9991. + tags.n_bytes_msb = 3;
  9992. +
  9993. + tags.serial_number = ext_tags->serial_number;
  9994. +
  9995. + if (!dev->param.use_nand_ecc && data)
  9996. + yaffs_calc_ecc(data, &spare);
  9997. +
  9998. + yaffs_load_tags_to_spare(&spare, &tags);
  9999. +
  10000. + }
  10001. +
  10002. + return yaffs_wr_nand(dev, nand_chunk, data, &spare);
  10003. +}
  10004. +
  10005. +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  10006. + int nand_chunk,
  10007. + u8 * data, struct yaffs_ext_tags *ext_tags)
  10008. +{
  10009. +
  10010. + struct yaffs_spare spare;
  10011. + struct yaffs_tags tags;
  10012. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  10013. +
  10014. + static struct yaffs_spare spare_ff;
  10015. + static int init;
  10016. +
  10017. + if (!init) {
  10018. + memset(&spare_ff, 0xFF, sizeof(spare_ff));
  10019. + init = 1;
  10020. + }
  10021. +
  10022. + if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) {
  10023. + /* ext_tags may be NULL */
  10024. + if (ext_tags) {
  10025. +
  10026. + int deleted =
  10027. + (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
  10028. +
  10029. + ext_tags->is_deleted = deleted;
  10030. + ext_tags->ecc_result = ecc_result;
  10031. + ext_tags->block_bad = 0; /* We're reading it */
  10032. + /* therefore it is not a bad block */
  10033. + ext_tags->chunk_used =
  10034. + (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
  10035. + 0) ? 1 : 0;
  10036. +
  10037. + if (ext_tags->chunk_used) {
  10038. + yaffs_get_tags_from_spare(dev, &spare, &tags);
  10039. +
  10040. + ext_tags->obj_id = tags.obj_id;
  10041. + ext_tags->chunk_id = tags.chunk_id;
  10042. + ext_tags->n_bytes = tags.n_bytes_lsb;
  10043. +
  10044. + if (dev->data_bytes_per_chunk >= 1024)
  10045. + ext_tags->n_bytes |=
  10046. + (((unsigned)tags.
  10047. + n_bytes_msb) << 10);
  10048. +
  10049. + ext_tags->serial_number = tags.serial_number;
  10050. + }
  10051. + }
  10052. +
  10053. + return YAFFS_OK;
  10054. + } else {
  10055. + return YAFFS_FAIL;
  10056. + }
  10057. +}
  10058. +
  10059. +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
  10060. +{
  10061. +
  10062. + struct yaffs_spare spare;
  10063. +
  10064. + memset(&spare, 0xff, sizeof(struct yaffs_spare));
  10065. +
  10066. + spare.block_status = 'Y';
  10067. +
  10068. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
  10069. + &spare);
  10070. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
  10071. + NULL, &spare);
  10072. +
  10073. + return YAFFS_OK;
  10074. +
  10075. +}
  10076. +
  10077. +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  10078. + int block_no,
  10079. + enum yaffs_block_state *state,
  10080. + u32 * seq_number)
  10081. +{
  10082. +
  10083. + struct yaffs_spare spare0, spare1;
  10084. + static struct yaffs_spare spare_ff;
  10085. + static int init;
  10086. + enum yaffs_ecc_result dummy;
  10087. +
  10088. + if (!init) {
  10089. + memset(&spare_ff, 0xFF, sizeof(spare_ff));
  10090. + init = 1;
  10091. + }
  10092. +
  10093. + *seq_number = 0;
  10094. +
  10095. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
  10096. + &spare0, &dummy, 1);
  10097. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
  10098. + NULL, &spare1, &dummy, 1);
  10099. +
  10100. + if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7)
  10101. + *state = YAFFS_BLOCK_STATE_DEAD;
  10102. + else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
  10103. + *state = YAFFS_BLOCK_STATE_EMPTY;
  10104. + else
  10105. + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
  10106. +
  10107. + return YAFFS_OK;
  10108. +}
  10109. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.h linux-2.6.36/fs/yaffs2/yaffs_tagscompat.h
  10110. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  10111. +++ linux-2.6.36/fs/yaffs2/yaffs_tagscompat.h 2011-01-10 19:29:29.000000000 +0100
  10112. @@ -0,0 +1,36 @@
  10113. +/*
  10114. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10115. + *
  10116. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10117. + * for Toby Churchill Ltd and Brightstar Engineering
  10118. + *
  10119. + * Created by Charles Manning <charles@aleph1.co.uk>
  10120. + *
  10121. + * This program is free software; you can redistribute it and/or modify
  10122. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10123. + * published by the Free Software Foundation.
  10124. + *
  10125. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10126. + */
  10127. +
  10128. +#ifndef __YAFFS_TAGSCOMPAT_H__
  10129. +#define __YAFFS_TAGSCOMPAT_H__
  10130. +
  10131. +#include "yaffs_guts.h"
  10132. +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  10133. + int nand_chunk,
  10134. + const u8 * data, const struct yaffs_ext_tags *tags);
  10135. +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  10136. + int nand_chunk,
  10137. + u8 * data, struct yaffs_ext_tags *tags);
  10138. +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no);
  10139. +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  10140. + int block_no,
  10141. + enum yaffs_block_state *state,
  10142. + u32 * seq_number);
  10143. +
  10144. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags);
  10145. +int yaffs_check_tags_ecc(struct yaffs_tags *tags);
  10146. +int yaffs_count_bits(u8 byte);
  10147. +
  10148. +#endif
  10149. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.c
  10150. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
  10151. +++ linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.c 2011-01-10 19:29:29.000000000 +0100
  10152. @@ -0,0 +1,27 @@
  10153. +/*
  10154. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10155. + *
  10156. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10157. + * for Toby Churchill Ltd and Brightstar Engineering
  10158. + *
  10159. + * Created by Charles Manning <charles@aleph1.co.uk>
  10160. + *
  10161. + * This program is free software; you can redistribute it and/or modify
  10162. + * it under the terms of the GNU General Public License version 2 as
  10163. + * published by the Free Software Foundation.
  10164. + */
  10165. +
  10166. +#include "yaffs_tagsvalidity.h"
  10167. +
  10168. +void yaffs_init_tags(struct yaffs_ext_tags *tags)
  10169. +{
  10170. + memset(tags, 0, sizeof(struct yaffs_ext_tags));
  10171. + tags->validity0 = 0xAAAAAAAA;
  10172. + tags->validity1 = 0x55555555;
  10173. +}
  10174. +
  10175. +int yaffs_validate_tags(struct yaffs_ext_tags *tags)
  10176. +{
  10177. + return (tags->validity0 == 0xAAAAAAAA && tags->validity1 == 0x55555555);
  10178. +
  10179. +}
  10180. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.h
  10181. --- linux-2.6.36.orig/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
  10182. +++ linux-2.6.36/fs/yaffs2/yaffs_tagsvalidity.h 2011-01-10 19:29:29.000000000 +0100
  10183. @@ -0,0 +1,23 @@
  10184. +/*
  10185. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10186. + *
  10187. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10188. + * for Toby Churchill Ltd and Brightstar Engineering
  10189. + *
  10190. + * Created by Charles Manning <charles@aleph1.co.uk>
  10191. + *
  10192. + * This program is free software; you can redistribute it and/or modify
  10193. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10194. + * published by the Free Software Foundation.
  10195. + *
  10196. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10197. + */
  10198. +
  10199. +#ifndef __YAFFS_TAGS_VALIDITY_H__
  10200. +#define __YAFFS_TAGS_VALIDITY_H__
  10201. +
  10202. +#include "yaffs_guts.h"
  10203. +
  10204. +void yaffs_init_tags(struct yaffs_ext_tags *tags);
  10205. +int yaffs_validate_tags(struct yaffs_ext_tags *tags);
  10206. +#endif
  10207. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_trace.h linux-2.6.36/fs/yaffs2/yaffs_trace.h
  10208. --- linux-2.6.36.orig/fs/yaffs2/yaffs_trace.h 1970-01-01 01:00:00.000000000 +0100
  10209. +++ linux-2.6.36/fs/yaffs2/yaffs_trace.h 2011-01-10 19:29:29.000000000 +0100
  10210. @@ -0,0 +1,59 @@
  10211. +/*
  10212. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10213. + *
  10214. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10215. + * for Toby Churchill Ltd and Brightstar Engineering
  10216. + *
  10217. + * Created by Charles Manning <charles@aleph1.co.uk>
  10218. + *
  10219. + * This program is free software; you can redistribute it and/or modify
  10220. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10221. + * published by the Free Software Foundation.
  10222. + *
  10223. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10224. + */
  10225. +
  10226. +#ifndef __YTRACE_H__
  10227. +#define __YTRACE_H__
  10228. +
  10229. +extern unsigned int yaffs_trace_mask;
  10230. +extern unsigned int yaffs_wr_attempts;
  10231. +
  10232. +/*
  10233. + * Tracing flags.
  10234. + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
  10235. + */
  10236. +
  10237. +#define YAFFS_TRACE_OS 0x00000002
  10238. +#define YAFFS_TRACE_ALLOCATE 0x00000004
  10239. +#define YAFFS_TRACE_SCAN 0x00000008
  10240. +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
  10241. +#define YAFFS_TRACE_ERASE 0x00000020
  10242. +#define YAFFS_TRACE_GC 0x00000040
  10243. +#define YAFFS_TRACE_WRITE 0x00000080
  10244. +#define YAFFS_TRACE_TRACING 0x00000100
  10245. +#define YAFFS_TRACE_DELETION 0x00000200
  10246. +#define YAFFS_TRACE_BUFFERS 0x00000400
  10247. +#define YAFFS_TRACE_NANDACCESS 0x00000800
  10248. +#define YAFFS_TRACE_GC_DETAIL 0x00001000
  10249. +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
  10250. +#define YAFFS_TRACE_MTD 0x00004000
  10251. +#define YAFFS_TRACE_CHECKPOINT 0x00008000
  10252. +
  10253. +#define YAFFS_TRACE_VERIFY 0x00010000
  10254. +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
  10255. +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
  10256. +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
  10257. +
  10258. +#define YAFFS_TRACE_SYNC 0x00100000
  10259. +#define YAFFS_TRACE_BACKGROUND 0x00200000
  10260. +#define YAFFS_TRACE_LOCK 0x00400000
  10261. +#define YAFFS_TRACE_MOUNT 0x00800000
  10262. +
  10263. +#define YAFFS_TRACE_ERROR 0x40000000
  10264. +#define YAFFS_TRACE_BUG 0x80000000
  10265. +#define YAFFS_TRACE_ALWAYS 0xF0000000
  10266. +
  10267. +#define T(mask, p) do { if ((mask) & (yaffs_trace_mask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
  10268. +
  10269. +#endif
  10270. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_verify.c linux-2.6.36/fs/yaffs2/yaffs_verify.c
  10271. --- linux-2.6.36.orig/fs/yaffs2/yaffs_verify.c 1970-01-01 01:00:00.000000000 +0100
  10272. +++ linux-2.6.36/fs/yaffs2/yaffs_verify.c 2011-01-10 19:29:29.000000000 +0100
  10273. @@ -0,0 +1,546 @@
  10274. +/*
  10275. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10276. + *
  10277. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10278. + * for Toby Churchill Ltd and Brightstar Engineering
  10279. + *
  10280. + * Created by Charles Manning <charles@aleph1.co.uk>
  10281. + *
  10282. + * This program is free software; you can redistribute it and/or modify
  10283. + * it under the terms of the GNU General Public License version 2 as
  10284. + * published by the Free Software Foundation.
  10285. + */
  10286. +
  10287. +#include "yaffs_verify.h"
  10288. +#include "yaffs_trace.h"
  10289. +#include "yaffs_bitmap.h"
  10290. +#include "yaffs_getblockinfo.h"
  10291. +#include "yaffs_nand.h"
  10292. +
  10293. +int yaffs_skip_verification(struct yaffs_dev *dev)
  10294. +{
  10295. + dev = dev;
  10296. + return !(yaffs_trace_mask &
  10297. + (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  10298. +}
  10299. +
  10300. +static int yaffs_skip_full_verification(struct yaffs_dev *dev)
  10301. +{
  10302. + dev = dev;
  10303. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
  10304. +}
  10305. +
  10306. +static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
  10307. +{
  10308. + dev = dev;
  10309. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
  10310. +}
  10311. +
  10312. +static const char *block_state_name[] = {
  10313. + "Unknown",
  10314. + "Needs scanning",
  10315. + "Scanning",
  10316. + "Empty",
  10317. + "Allocating",
  10318. + "Full",
  10319. + "Dirty",
  10320. + "Checkpoint",
  10321. + "Collecting",
  10322. + "Dead"
  10323. +};
  10324. +
  10325. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
  10326. +{
  10327. + int actually_used;
  10328. + int in_use;
  10329. +
  10330. + if (yaffs_skip_verification(dev))
  10331. + return;
  10332. +
  10333. + /* Report illegal runtime states */
  10334. + if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
  10335. + T(YAFFS_TRACE_VERIFY,
  10336. + (TSTR("Block %d has undefined state %d" TENDSTR), n,
  10337. + bi->block_state));
  10338. +
  10339. + switch (bi->block_state) {
  10340. + case YAFFS_BLOCK_STATE_UNKNOWN:
  10341. + case YAFFS_BLOCK_STATE_SCANNING:
  10342. + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
  10343. + T(YAFFS_TRACE_VERIFY,
  10344. + (TSTR("Block %d has bad run-state %s" TENDSTR), n,
  10345. + block_state_name[bi->block_state]));
  10346. + }
  10347. +
  10348. + /* Check pages in use and soft deletions are legal */
  10349. +
  10350. + actually_used = bi->pages_in_use - bi->soft_del_pages;
  10351. +
  10352. + if (bi->pages_in_use < 0
  10353. + || bi->pages_in_use > dev->param.chunks_per_block
  10354. + || bi->soft_del_pages < 0
  10355. + || bi->soft_del_pages > dev->param.chunks_per_block
  10356. + || actually_used < 0 || actually_used > dev->param.chunks_per_block)
  10357. + T(YAFFS_TRACE_VERIFY,
  10358. + (TSTR
  10359. + ("Block %d has illegal values pages_in_used %d soft_del_pages %d"
  10360. + TENDSTR), n, bi->pages_in_use, bi->soft_del_pages));
  10361. +
  10362. + /* Check chunk bitmap legal */
  10363. + in_use = yaffs_count_chunk_bits(dev, n);
  10364. + if (in_use != bi->pages_in_use)
  10365. + T(YAFFS_TRACE_VERIFY,
  10366. + (TSTR
  10367. + ("Block %d has inconsistent values pages_in_use %d counted chunk bits %d"
  10368. + TENDSTR), n, bi->pages_in_use, in_use));
  10369. +
  10370. +}
  10371. +
  10372. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10373. + struct yaffs_block_info *bi, int n)
  10374. +{
  10375. + yaffs_verify_blk(dev, bi, n);
  10376. +
  10377. + /* After collection the block should be in the erased state */
  10378. +
  10379. + if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
  10380. + bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
  10381. + T(YAFFS_TRACE_ERROR,
  10382. + (TSTR
  10383. + ("Block %d is in state %d after gc, should be erased"
  10384. + TENDSTR), n, bi->block_state));
  10385. + }
  10386. +}
  10387. +
  10388. +void yaffs_verify_blocks(struct yaffs_dev *dev)
  10389. +{
  10390. + int i;
  10391. + int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
  10392. + int illegal_states = 0;
  10393. +
  10394. + if (yaffs_skip_verification(dev))
  10395. + return;
  10396. +
  10397. + memset(state_count, 0, sizeof(state_count));
  10398. +
  10399. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  10400. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  10401. + yaffs_verify_blk(dev, bi, i);
  10402. +
  10403. + if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
  10404. + state_count[bi->block_state]++;
  10405. + else
  10406. + illegal_states++;
  10407. + }
  10408. +
  10409. + T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR)));
  10410. + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary" TENDSTR)));
  10411. +
  10412. + T(YAFFS_TRACE_VERIFY,
  10413. + (TSTR("%d blocks have illegal states" TENDSTR), illegal_states));
  10414. + if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  10415. + T(YAFFS_TRACE_VERIFY,
  10416. + (TSTR("Too many allocating blocks" TENDSTR)));
  10417. +
  10418. + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  10419. + T(YAFFS_TRACE_VERIFY,
  10420. + (TSTR("%s %d blocks" TENDSTR),
  10421. + block_state_name[i], state_count[i]));
  10422. +
  10423. + if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
  10424. + T(YAFFS_TRACE_VERIFY,
  10425. + (TSTR("Checkpoint block count wrong dev %d count %d" TENDSTR),
  10426. + dev->blocks_in_checkpt,
  10427. + state_count[YAFFS_BLOCK_STATE_CHECKPOINT]));
  10428. +
  10429. + if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
  10430. + T(YAFFS_TRACE_VERIFY,
  10431. + (TSTR("Erased block count wrong dev %d count %d" TENDSTR),
  10432. + dev->n_erased_blocks, state_count[YAFFS_BLOCK_STATE_EMPTY]));
  10433. +
  10434. + if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  10435. + T(YAFFS_TRACE_VERIFY,
  10436. + (TSTR("Too many collecting blocks %d (max is 1)" TENDSTR),
  10437. + state_count[YAFFS_BLOCK_STATE_COLLECTING]));
  10438. +
  10439. + T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR)));
  10440. +
  10441. +}
  10442. +
  10443. +/*
  10444. + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
  10445. + * case those tests will not be performed.
  10446. + */
  10447. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10448. + struct yaffs_ext_tags *tags, int parent_check)
  10449. +{
  10450. + if (obj && yaffs_skip_verification(obj->my_dev))
  10451. + return;
  10452. +
  10453. + if (!(tags && obj && oh)) {
  10454. + T(YAFFS_TRACE_VERIFY,
  10455. + (TSTR("Verifying object header tags %p obj %p oh %p" TENDSTR),
  10456. + tags, obj, oh));
  10457. + return;
  10458. + }
  10459. +
  10460. + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  10461. + oh->type > YAFFS_OBJECT_TYPE_MAX)
  10462. + T(YAFFS_TRACE_VERIFY,
  10463. + (TSTR("Obj %d header type is illegal value 0x%x" TENDSTR),
  10464. + tags->obj_id, oh->type));
  10465. +
  10466. + if (tags->obj_id != obj->obj_id)
  10467. + T(YAFFS_TRACE_VERIFY,
  10468. + (TSTR("Obj %d header mismatch obj_id %d" TENDSTR),
  10469. + tags->obj_id, obj->obj_id));
  10470. +
  10471. + /*
  10472. + * Check that the object's parent ids match if parent_check requested.
  10473. + *
  10474. + * Tests do not apply to the root object.
  10475. + */
  10476. +
  10477. + if (parent_check && tags->obj_id > 1 && !obj->parent)
  10478. + T(YAFFS_TRACE_VERIFY,
  10479. + (TSTR
  10480. + ("Obj %d header mismatch parent_id %d obj->parent is NULL"
  10481. + TENDSTR), tags->obj_id, oh->parent_obj_id));
  10482. +
  10483. + if (parent_check && obj->parent &&
  10484. + oh->parent_obj_id != obj->parent->obj_id &&
  10485. + (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
  10486. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
  10487. + T(YAFFS_TRACE_VERIFY,
  10488. + (TSTR
  10489. + ("Obj %d header mismatch parent_id %d parent_obj_id %d"
  10490. + TENDSTR), tags->obj_id, oh->parent_obj_id,
  10491. + obj->parent->obj_id));
  10492. +
  10493. + if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
  10494. + T(YAFFS_TRACE_VERIFY,
  10495. + (TSTR("Obj %d header name is NULL" TENDSTR), obj->obj_id));
  10496. +
  10497. + if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Trashed name */
  10498. + T(YAFFS_TRACE_VERIFY,
  10499. + (TSTR("Obj %d header name is 0xFF" TENDSTR), obj->obj_id));
  10500. +}
  10501. +
  10502. +void yaffs_verify_file(struct yaffs_obj *obj)
  10503. +{
  10504. + int required_depth;
  10505. + int actual_depth;
  10506. + u32 last_chunk;
  10507. + u32 x;
  10508. + u32 i;
  10509. + struct yaffs_dev *dev;
  10510. + struct yaffs_ext_tags tags;
  10511. + struct yaffs_tnode *tn;
  10512. + u32 obj_id;
  10513. +
  10514. + if (!obj)
  10515. + return;
  10516. +
  10517. + if (yaffs_skip_verification(obj->my_dev))
  10518. + return;
  10519. +
  10520. + dev = obj->my_dev;
  10521. + obj_id = obj->obj_id;
  10522. +
  10523. + /* Check file size is consistent with tnode depth */
  10524. + last_chunk =
  10525. + obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1;
  10526. + x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
  10527. + required_depth = 0;
  10528. + while (x > 0) {
  10529. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  10530. + required_depth++;
  10531. + }
  10532. +
  10533. + actual_depth = obj->variant.file_variant.top_level;
  10534. +
  10535. + /* Check that the chunks in the tnode tree are all correct.
  10536. + * We do this by scanning through the tnode tree and
  10537. + * checking the tags for every chunk match.
  10538. + */
  10539. +
  10540. + if (yaffs_skip_nand_verification(dev))
  10541. + return;
  10542. +
  10543. + for (i = 1; i <= last_chunk; i++) {
  10544. + tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
  10545. +
  10546. + if (tn) {
  10547. + u32 the_chunk = yaffs_get_group_base(dev, tn, i);
  10548. + if (the_chunk > 0) {
  10549. + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),obj_id,i,the_chunk)); */
  10550. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  10551. + &tags);
  10552. + if (tags.obj_id != obj_id || tags.chunk_id != i) {
  10553. + T(~0,
  10554. + (TSTR
  10555. + ("Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)"
  10556. + TENDSTR), obj_id, i, the_chunk,
  10557. + tags.obj_id, tags.chunk_id));
  10558. + }
  10559. + }
  10560. + }
  10561. + }
  10562. +}
  10563. +
  10564. +void yaffs_verify_link(struct yaffs_obj *obj)
  10565. +{
  10566. + if (obj && yaffs_skip_verification(obj->my_dev))
  10567. + return;
  10568. +
  10569. + /* Verify sane equivalent object */
  10570. +}
  10571. +
  10572. +void yaffs_verify_symlink(struct yaffs_obj *obj)
  10573. +{
  10574. + if (obj && yaffs_skip_verification(obj->my_dev))
  10575. + return;
  10576. +
  10577. + /* Verify symlink string */
  10578. +}
  10579. +
  10580. +void yaffs_verify_special(struct yaffs_obj *obj)
  10581. +{
  10582. + if (obj && yaffs_skip_verification(obj->my_dev))
  10583. + return;
  10584. +}
  10585. +
  10586. +void yaffs_verify_obj(struct yaffs_obj *obj)
  10587. +{
  10588. + struct yaffs_dev *dev;
  10589. +
  10590. + u32 chunk_min;
  10591. + u32 chunk_max;
  10592. +
  10593. + u32 chunk_id_ok;
  10594. + u32 chunk_in_range;
  10595. + u32 chunk_wrongly_deleted;
  10596. + u32 chunk_valid;
  10597. +
  10598. + if (!obj)
  10599. + return;
  10600. +
  10601. + if (obj->being_created)
  10602. + return;
  10603. +
  10604. + dev = obj->my_dev;
  10605. +
  10606. + if (yaffs_skip_verification(dev))
  10607. + return;
  10608. +
  10609. + /* Check sane object header chunk */
  10610. +
  10611. + chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
  10612. + chunk_max =
  10613. + (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
  10614. +
  10615. + chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
  10616. + ((unsigned)(obj->hdr_chunk)) <= chunk_max);
  10617. + chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
  10618. + chunk_valid = chunk_in_range &&
  10619. + yaffs_check_chunk_bit(dev,
  10620. + obj->hdr_chunk / dev->param.chunks_per_block,
  10621. + obj->hdr_chunk % dev->param.chunks_per_block);
  10622. + chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
  10623. +
  10624. + if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted)) {
  10625. + T(YAFFS_TRACE_VERIFY,
  10626. + (TSTR("Obj %d has chunk_id %d %s %s" TENDSTR),
  10627. + obj->obj_id, obj->hdr_chunk,
  10628. + chunk_id_ok ? "" : ",out of range",
  10629. + chunk_wrongly_deleted ? ",marked as deleted" : ""));
  10630. + }
  10631. +
  10632. + if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
  10633. + struct yaffs_ext_tags tags;
  10634. + struct yaffs_obj_hdr *oh;
  10635. + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  10636. +
  10637. + oh = (struct yaffs_obj_hdr *)buffer;
  10638. +
  10639. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
  10640. +
  10641. + yaffs_verify_oh(obj, oh, &tags, 1);
  10642. +
  10643. + yaffs_release_temp_buffer(dev, buffer, __LINE__);
  10644. + }
  10645. +
  10646. + /* Verify it has a parent */
  10647. + if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
  10648. + T(YAFFS_TRACE_VERIFY,
  10649. + (TSTR
  10650. + ("Obj %d has parent pointer %p which does not look like an object"
  10651. + TENDSTR), obj->obj_id, obj->parent));
  10652. + }
  10653. +
  10654. + /* Verify parent is a directory */
  10655. + if (obj->parent
  10656. + && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10657. + T(YAFFS_TRACE_VERIFY,
  10658. + (TSTR("Obj %d's parent is not a directory (type %d)" TENDSTR),
  10659. + obj->obj_id, obj->parent->variant_type));
  10660. + }
  10661. +
  10662. + switch (obj->variant_type) {
  10663. + case YAFFS_OBJECT_TYPE_FILE:
  10664. + yaffs_verify_file(obj);
  10665. + break;
  10666. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10667. + yaffs_verify_symlink(obj);
  10668. + break;
  10669. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10670. + yaffs_verify_dir(obj);
  10671. + break;
  10672. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10673. + yaffs_verify_link(obj);
  10674. + break;
  10675. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10676. + yaffs_verify_special(obj);
  10677. + break;
  10678. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  10679. + default:
  10680. + T(YAFFS_TRACE_VERIFY,
  10681. + (TSTR("Obj %d has illegaltype %d" TENDSTR),
  10682. + obj->obj_id, obj->variant_type));
  10683. + break;
  10684. + }
  10685. +}
  10686. +
  10687. +void yaffs_verify_objects(struct yaffs_dev *dev)
  10688. +{
  10689. + struct yaffs_obj *obj;
  10690. + int i;
  10691. + struct list_head *lh;
  10692. +
  10693. + if (yaffs_skip_verification(dev))
  10694. + return;
  10695. +
  10696. + /* Iterate through the objects in each hash entry */
  10697. +
  10698. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  10699. + list_for_each(lh, &dev->obj_bucket[i].list) {
  10700. + if (lh) {
  10701. + obj =
  10702. + list_entry(lh, struct yaffs_obj, hash_link);
  10703. + yaffs_verify_obj(obj);
  10704. + }
  10705. + }
  10706. + }
  10707. +}
  10708. +
  10709. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
  10710. +{
  10711. + struct list_head *lh;
  10712. + struct yaffs_obj *list_obj;
  10713. +
  10714. + int count = 0;
  10715. +
  10716. + if (!obj) {
  10717. + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
  10718. + YBUG();
  10719. + return;
  10720. + }
  10721. +
  10722. + if (yaffs_skip_verification(obj->my_dev))
  10723. + return;
  10724. +
  10725. + if (!obj->parent) {
  10726. + T(YAFFS_TRACE_ALWAYS,
  10727. + (TSTR("Object does not have parent" TENDSTR)));
  10728. + YBUG();
  10729. + return;
  10730. + }
  10731. +
  10732. + if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10733. + T(YAFFS_TRACE_ALWAYS,
  10734. + (TSTR("Parent is not directory" TENDSTR)));
  10735. + YBUG();
  10736. + }
  10737. +
  10738. + /* Iterate through the objects in each hash entry */
  10739. +
  10740. + list_for_each(lh, &obj->parent->variant.dir_variant.children) {
  10741. + if (lh) {
  10742. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10743. + yaffs_verify_obj(list_obj);
  10744. + if (obj == list_obj)
  10745. + count++;
  10746. + }
  10747. + }
  10748. +
  10749. + if (count != 1) {
  10750. + T(YAFFS_TRACE_ALWAYS,
  10751. + (TSTR("Object in directory %d times" TENDSTR), count));
  10752. + YBUG();
  10753. + }
  10754. +}
  10755. +
  10756. +void yaffs_verify_dir(struct yaffs_obj *directory)
  10757. +{
  10758. + struct list_head *lh;
  10759. + struct yaffs_obj *list_obj;
  10760. +
  10761. + if (!directory) {
  10762. + YBUG();
  10763. + return;
  10764. + }
  10765. +
  10766. + if (yaffs_skip_full_verification(directory->my_dev))
  10767. + return;
  10768. +
  10769. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10770. + T(YAFFS_TRACE_ALWAYS,
  10771. + (TSTR("Directory has wrong type: %d" TENDSTR),
  10772. + directory->variant_type));
  10773. + YBUG();
  10774. + }
  10775. +
  10776. + /* Iterate through the objects in each hash entry */
  10777. +
  10778. + list_for_each(lh, &directory->variant.dir_variant.children) {
  10779. + if (lh) {
  10780. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10781. + if (list_obj->parent != directory) {
  10782. + T(YAFFS_TRACE_ALWAYS,
  10783. + (TSTR
  10784. + ("Object in directory list has wrong parent %p"
  10785. + TENDSTR), list_obj->parent));
  10786. + YBUG();
  10787. + }
  10788. + yaffs_verify_obj_in_dir(list_obj);
  10789. + }
  10790. + }
  10791. +}
  10792. +
  10793. +static int yaffs_free_verification_failures;
  10794. +
  10795. +void yaffs_verify_free_chunks(struct yaffs_dev *dev)
  10796. +{
  10797. + int counted;
  10798. + int difference;
  10799. +
  10800. + if (yaffs_skip_verification(dev))
  10801. + return;
  10802. +
  10803. + counted = yaffs_count_free_chunks(dev);
  10804. +
  10805. + difference = dev->n_free_chunks - counted;
  10806. +
  10807. + if (difference) {
  10808. + T(YAFFS_TRACE_ALWAYS,
  10809. + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
  10810. + dev->n_free_chunks, counted, difference));
  10811. + yaffs_free_verification_failures++;
  10812. + }
  10813. +}
  10814. +
  10815. +int yaffs_verify_file_sane(struct yaffs_obj *in)
  10816. +{
  10817. + in = in;
  10818. + return YAFFS_OK;
  10819. +}
  10820. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_verify.h linux-2.6.36/fs/yaffs2/yaffs_verify.h
  10821. --- linux-2.6.36.orig/fs/yaffs2/yaffs_verify.h 1970-01-01 01:00:00.000000000 +0100
  10822. +++ linux-2.6.36/fs/yaffs2/yaffs_verify.h 2011-01-10 19:29:29.000000000 +0100
  10823. @@ -0,0 +1,43 @@
  10824. +/*
  10825. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10826. + *
  10827. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10828. + * for Toby Churchill Ltd and Brightstar Engineering
  10829. + *
  10830. + * Created by Charles Manning <charles@aleph1.co.uk>
  10831. + *
  10832. + * This program is free software; you can redistribute it and/or modify
  10833. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10834. + * published by the Free Software Foundation.
  10835. + *
  10836. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10837. + */
  10838. +
  10839. +#ifndef __YAFFS_VERIFY_H__
  10840. +#define __YAFFS_VERIFY_H__
  10841. +
  10842. +#include "yaffs_guts.h"
  10843. +
  10844. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi,
  10845. + int n);
  10846. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10847. + struct yaffs_block_info *bi, int n);
  10848. +void yaffs_verify_blocks(struct yaffs_dev *dev);
  10849. +
  10850. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10851. + struct yaffs_ext_tags *tags, int parent_check);
  10852. +void yaffs_verify_file(struct yaffs_obj *obj);
  10853. +void yaffs_verify_link(struct yaffs_obj *obj);
  10854. +void yaffs_verify_symlink(struct yaffs_obj *obj);
  10855. +void yaffs_verify_special(struct yaffs_obj *obj);
  10856. +void yaffs_verify_obj(struct yaffs_obj *obj);
  10857. +void yaffs_verify_objects(struct yaffs_dev *dev);
  10858. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj);
  10859. +void yaffs_verify_dir(struct yaffs_obj *directory);
  10860. +void yaffs_verify_free_chunks(struct yaffs_dev *dev);
  10861. +
  10862. +int yaffs_verify_file_sane(struct yaffs_obj *obj);
  10863. +
  10864. +int yaffs_skip_verification(struct yaffs_dev *dev);
  10865. +
  10866. +#endif
  10867. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_vfs.c linux-2.6.36/fs/yaffs2/yaffs_vfs.c
  10868. --- linux-2.6.36.orig/fs/yaffs2/yaffs_vfs.c 1970-01-01 01:00:00.000000000 +0100
  10869. +++ linux-2.6.36/fs/yaffs2/yaffs_vfs.c 2011-01-10 19:29:29.000000000 +0100
  10870. @@ -0,0 +1,3565 @@
  10871. +/*
  10872. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10873. + *
  10874. + * Copyright (C) 2002-2010 Aleph One Ltd.
  10875. + * for Toby Churchill Ltd and Brightstar Engineering
  10876. + *
  10877. + * Created by Charles Manning <charles@aleph1.co.uk>
  10878. + * Acknowledgements:
  10879. + * Luc van OostenRyck for numerous patches.
  10880. + * Nick Bane for numerous patches.
  10881. + * Nick Bane for 2.5/2.6 integration.
  10882. + * Andras Toth for mknod rdev issue.
  10883. + * Michael Fischer for finding the problem with inode inconsistency.
  10884. + * Some code bodily lifted from JFFS
  10885. + *
  10886. + * This program is free software; you can redistribute it and/or modify
  10887. + * it under the terms of the GNU General Public License version 2 as
  10888. + * published by the Free Software Foundation.
  10889. + */
  10890. +
  10891. +/*
  10892. + *
  10893. + * This is the file system front-end to YAFFS that hooks it up to
  10894. + * the VFS.
  10895. + *
  10896. + * Special notes:
  10897. + * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
  10898. + * this superblock
  10899. + * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
  10900. + * superblock
  10901. + * >> inode->u.generic_ip points to the associated struct yaffs_obj.
  10902. + */
  10903. +
  10904. +/*
  10905. + * There are two variants of the VFS glue code. This variant should compile
  10906. + * for any version of Linux.
  10907. + */
  10908. +#include <linux/version.h>
  10909. +
  10910. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
  10911. +#define YAFFS_COMPILE_BACKGROUND
  10912. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23))
  10913. +#define YAFFS_COMPILE_FREEZER
  10914. +#endif
  10915. +#endif
  10916. +
  10917. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
  10918. +#define YAFFS_COMPILE_EXPORTFS
  10919. +#endif
  10920. +
  10921. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
  10922. +#define YAFFS_USE_SETATTR_COPY
  10923. +#define YAFFS_USE_TRUNCATE_SETSIZE
  10924. +#endif
  10925. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
  10926. +#define YAFFS_HAS_EVICT_INODE
  10927. +#endif
  10928. +
  10929. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
  10930. +#define YAFFS_NEW_FOLLOW_LINK 1
  10931. +#else
  10932. +#define YAFFS_NEW_FOLLOW_LINK 0
  10933. +#endif
  10934. +
  10935. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  10936. +#include <linux/config.h>
  10937. +#endif
  10938. +
  10939. +#include <linux/kernel.h>
  10940. +#include <linux/module.h>
  10941. +#include <linux/slab.h>
  10942. +#include <linux/init.h>
  10943. +#include <linux/fs.h>
  10944. +#include <linux/proc_fs.h>
  10945. +#include <linux/smp_lock.h>
  10946. +#include <linux/pagemap.h>
  10947. +#include <linux/mtd/mtd.h>
  10948. +#include <linux/interrupt.h>
  10949. +#include <linux/string.h>
  10950. +#include <linux/ctype.h>
  10951. +
  10952. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  10953. +#include <linux/namei.h>
  10954. +#endif
  10955. +
  10956. +#ifdef YAFFS_COMPILE_EXPORTFS
  10957. +#include <linux/exportfs.h>
  10958. +#endif
  10959. +
  10960. +#ifdef YAFFS_COMPILE_BACKGROUND
  10961. +#include <linux/kthread.h>
  10962. +#include <linux/delay.h>
  10963. +#endif
  10964. +#ifdef YAFFS_COMPILE_FREEZER
  10965. +#include <linux/freezer.h>
  10966. +#endif
  10967. +
  10968. +#include <asm/div64.h>
  10969. +
  10970. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  10971. +
  10972. +#include <linux/statfs.h>
  10973. +
  10974. +#define UnlockPage(p) unlock_page(p)
  10975. +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  10976. +
  10977. +/* FIXME: use sb->s_id instead ? */
  10978. +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  10979. +
  10980. +#else
  10981. +
  10982. +#include <linux/locks.h>
  10983. +#define BDEVNAME_SIZE 0
  10984. +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
  10985. +
  10986. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
  10987. +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
  10988. +#define __user
  10989. +#endif
  10990. +
  10991. +#endif
  10992. +
  10993. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  10994. +#define YPROC_ROOT (&proc_root)
  10995. +#else
  10996. +#define YPROC_ROOT NULL
  10997. +#endif
  10998. +
  10999. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
  11000. +#define Y_INIT_TIMER(a) init_timer(a)
  11001. +#else
  11002. +#define Y_INIT_TIMER(a) init_timer_on_stack(a)
  11003. +#endif
  11004. +
  11005. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11006. +#define WRITE_SIZE_STR "writesize"
  11007. +#define WRITE_SIZE(mtd) ((mtd)->writesize)
  11008. +#else
  11009. +#define WRITE_SIZE_STR "oobblock"
  11010. +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
  11011. +#endif
  11012. +
  11013. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
  11014. +#define YAFFS_USE_WRITE_BEGIN_END 1
  11015. +#else
  11016. +#define YAFFS_USE_WRITE_BEGIN_END 0
  11017. +#endif
  11018. +
  11019. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
  11020. +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  11021. +{
  11022. + uint64_t result = partition_size;
  11023. + do_div(result, block_size);
  11024. + return (uint32_t) result;
  11025. +}
  11026. +#else
  11027. +#define YCALCBLOCKS(s, b) ((s)/(b))
  11028. +#endif
  11029. +
  11030. +#include <linux/uaccess.h>
  11031. +#include <linux/mtd/mtd.h>
  11032. +
  11033. +#include "yportenv.h"
  11034. +#include "yaffs_trace.h"
  11035. +#include "yaffs_guts.h"
  11036. +#include "yaffs_attribs.h"
  11037. +
  11038. +#include "yaffs_linux.h"
  11039. +
  11040. +#include "yaffs_mtdif.h"
  11041. +#include "yaffs_mtdif1.h"
  11042. +#include "yaffs_mtdif2.h"
  11043. +
  11044. +unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
  11045. +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  11046. +unsigned int yaffs_auto_checkpoint = 1;
  11047. +unsigned int yaffs_gc_control = 1;
  11048. +unsigned int yaffs_bg_enable = 1;
  11049. +
  11050. +/* Module Parameters */
  11051. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11052. +module_param(yaffs_trace_mask, uint, 0644);
  11053. +module_param(yaffs_wr_attempts, uint, 0644);
  11054. +module_param(yaffs_auto_checkpoint, uint, 0644);
  11055. +module_param(yaffs_gc_control, uint, 0644);
  11056. +module_param(yaffs_bg_enable, uint, 0644);
  11057. +#else
  11058. +MODULE_PARM(yaffs_trace_mask, "i");
  11059. +MODULE_PARM(yaffs_wr_attempts, "i");
  11060. +MODULE_PARM(yaffs_auto_checkpoint, "i");
  11061. +MODULE_PARM(yaffs_gc_control, "i");
  11062. +#endif
  11063. +
  11064. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  11065. +/* use iget and read_inode */
  11066. +#define Y_IGET(sb, inum) iget((sb), (inum))
  11067. +static void yaffs_read_inode(struct inode *inode);
  11068. +
  11069. +#else
  11070. +/* Call local equivalent */
  11071. +#define YAFFS_USE_OWN_IGET
  11072. +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
  11073. +
  11074. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
  11075. +#endif
  11076. +
  11077. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11078. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
  11079. +#else
  11080. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
  11081. +#endif
  11082. +
  11083. +#define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
  11084. +#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
  11085. +
  11086. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11087. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
  11088. +#else
  11089. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
  11090. +#endif
  11091. +
  11092. +#define update_dir_time(dir) do {\
  11093. + (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
  11094. + } while(0)
  11095. +
  11096. +static void yaffs_put_super(struct super_block *sb);
  11097. +
  11098. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  11099. + loff_t * pos);
  11100. +static ssize_t yaffs_hold_space(struct file *f);
  11101. +static void yaffs_release_space(struct file *f);
  11102. +
  11103. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11104. +static int yaffs_file_flush(struct file *file, fl_owner_t id);
  11105. +#else
  11106. +static int yaffs_file_flush(struct file *file);
  11107. +#endif
  11108. +
  11109. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  11110. +static int yaffs_sync_object(struct file *file, int datasync);
  11111. +#else
  11112. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  11113. + int datasync);
  11114. +#endif
  11115. +
  11116. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
  11117. +
  11118. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11119. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  11120. + struct nameidata *n);
  11121. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  11122. + struct nameidata *n);
  11123. +#else
  11124. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
  11125. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
  11126. +#endif
  11127. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  11128. + struct dentry *dentry);
  11129. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
  11130. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  11131. + const char *symname);
  11132. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  11133. +
  11134. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11135. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  11136. + dev_t dev);
  11137. +#else
  11138. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  11139. + int dev);
  11140. +#endif
  11141. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  11142. + struct inode *new_dir, struct dentry *new_dentry);
  11143. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
  11144. +
  11145. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11146. +static int yaffs_sync_fs(struct super_block *sb, int wait);
  11147. +static void yaffs_write_super(struct super_block *sb);
  11148. +#else
  11149. +static int yaffs_sync_fs(struct super_block *sb);
  11150. +static int yaffs_write_super(struct super_block *sb);
  11151. +#endif
  11152. +
  11153. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11154. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
  11155. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11156. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
  11157. +#else
  11158. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
  11159. +#endif
  11160. +
  11161. +#ifdef YAFFS_HAS_PUT_INODE
  11162. +static void yaffs_put_inode(struct inode *inode);
  11163. +#endif
  11164. +
  11165. +#ifdef YAFFS_HAS_EVICT_INODE
  11166. +static void yaffs_evict_inode(struct inode *);
  11167. +#else
  11168. +static void yaffs_delete_inode(struct inode *);
  11169. +static void yaffs_clear_inode(struct inode *);
  11170. +#endif
  11171. +
  11172. +static int yaffs_readpage(struct file *file, struct page *page);
  11173. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11174. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
  11175. +#else
  11176. +static int yaffs_writepage(struct page *page);
  11177. +#endif
  11178. +
  11179. +#ifdef CONFIG_YAFFS_XATTR
  11180. +int yaffs_setxattr(struct dentry *dentry, const char *name,
  11181. + const void *value, size_t size, int flags);
  11182. +ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
  11183. + size_t size);
  11184. +int yaffs_removexattr(struct dentry *dentry, const char *name);
  11185. +ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
  11186. +#endif
  11187. +
  11188. +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
  11189. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  11190. + loff_t pos, unsigned len, unsigned flags,
  11191. + struct page **pagep, void **fsdata);
  11192. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  11193. + loff_t pos, unsigned len, unsigned copied,
  11194. + struct page *pg, void *fsdadata);
  11195. +#else
  11196. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  11197. + unsigned offset, unsigned to);
  11198. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  11199. + unsigned to);
  11200. +
  11201. +#endif
  11202. +
  11203. +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
  11204. + int buflen);
  11205. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11206. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
  11207. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  11208. +#else
  11209. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
  11210. +#endif
  11211. +
  11212. +static void yaffs_touch_super(struct yaffs_dev *dev);
  11213. +
  11214. +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
  11215. +
  11216. +static int yaffs_vfs_setattr(struct inode *, struct iattr *);
  11217. +
  11218. +static struct address_space_operations yaffs_file_address_operations = {
  11219. + .readpage = yaffs_readpage,
  11220. + .writepage = yaffs_writepage,
  11221. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  11222. + .write_begin = yaffs_write_begin,
  11223. + .write_end = yaffs_write_end,
  11224. +#else
  11225. + .prepare_write = yaffs_prepare_write,
  11226. + .commit_write = yaffs_commit_write,
  11227. +#endif
  11228. +};
  11229. +
  11230. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
  11231. +static const struct file_operations yaffs_file_operations = {
  11232. + .read = do_sync_read,
  11233. + .write = do_sync_write,
  11234. + .aio_read = generic_file_aio_read,
  11235. + .aio_write = generic_file_aio_write,
  11236. + .mmap = generic_file_mmap,
  11237. + .flush = yaffs_file_flush,
  11238. + .fsync = yaffs_sync_object,
  11239. + .splice_read = generic_file_splice_read,
  11240. + .splice_write = generic_file_splice_write,
  11241. + .llseek = generic_file_llseek,
  11242. +};
  11243. +
  11244. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11245. +
  11246. +static const struct file_operations yaffs_file_operations = {
  11247. + .read = do_sync_read,
  11248. + .write = do_sync_write,
  11249. + .aio_read = generic_file_aio_read,
  11250. + .aio_write = generic_file_aio_write,
  11251. + .mmap = generic_file_mmap,
  11252. + .flush = yaffs_file_flush,
  11253. + .fsync = yaffs_sync_object,
  11254. + .sendfile = generic_file_sendfile,
  11255. +};
  11256. +
  11257. +#else
  11258. +
  11259. +static const struct file_operations yaffs_file_operations = {
  11260. + .read = generic_file_read,
  11261. + .write = generic_file_write,
  11262. + .mmap = generic_file_mmap,
  11263. + .flush = yaffs_file_flush,
  11264. + .fsync = yaffs_sync_object,
  11265. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11266. + .sendfile = generic_file_sendfile,
  11267. +#endif
  11268. +};
  11269. +#endif
  11270. +
  11271. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
  11272. +static void zero_user_segment(struct page *page, unsigned start, unsigned end)
  11273. +{
  11274. + void *kaddr = kmap_atomic(page, KM_USER0);
  11275. + memset(kaddr + start, 0, end - start);
  11276. + kunmap_atomic(kaddr, KM_USER0);
  11277. + flush_dcache_page(page);
  11278. +}
  11279. +#endif
  11280. +
  11281. +static const struct inode_operations yaffs_file_inode_operations = {
  11282. + .setattr = yaffs_setattr,
  11283. +#ifdef CONFIG_YAFFS_XATTR
  11284. + .setxattr = yaffs_setxattr,
  11285. + .getxattr = yaffs_getxattr,
  11286. + .listxattr = yaffs_listxattr,
  11287. + .removexattr = yaffs_removexattr,
  11288. +#endif
  11289. +};
  11290. +
  11291. +static const struct inode_operations yaffs_symlink_inode_operations = {
  11292. + .readlink = yaffs_readlink,
  11293. + .follow_link = yaffs_follow_link,
  11294. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11295. + .put_link = yaffs_put_link,
  11296. +#endif
  11297. + .setattr = yaffs_setattr,
  11298. +#ifdef CONFIG_YAFFS_XATTR
  11299. + .setxattr = yaffs_setxattr,
  11300. + .getxattr = yaffs_getxattr,
  11301. + .listxattr = yaffs_listxattr,
  11302. + .removexattr = yaffs_removexattr,
  11303. +#endif
  11304. +};
  11305. +
  11306. +static const struct inode_operations yaffs_dir_inode_operations = {
  11307. + .create = yaffs_create,
  11308. + .lookup = yaffs_lookup,
  11309. + .link = yaffs_link,
  11310. + .unlink = yaffs_unlink,
  11311. + .symlink = yaffs_symlink,
  11312. + .mkdir = yaffs_mkdir,
  11313. + .rmdir = yaffs_unlink,
  11314. + .mknod = yaffs_mknod,
  11315. + .rename = yaffs_rename,
  11316. + .setattr = yaffs_setattr,
  11317. +#ifdef CONFIG_YAFFS_XATTR
  11318. + .setxattr = yaffs_setxattr,
  11319. + .getxattr = yaffs_getxattr,
  11320. + .listxattr = yaffs_listxattr,
  11321. + .removexattr = yaffs_removexattr,
  11322. +#endif
  11323. +};
  11324. +
  11325. +static const struct file_operations yaffs_dir_operations = {
  11326. + .read = generic_read_dir,
  11327. + .readdir = yaffs_readdir,
  11328. + .fsync = yaffs_sync_object,
  11329. + .llseek = yaffs_dir_llseek,
  11330. +};
  11331. +
  11332. +static const struct super_operations yaffs_super_ops = {
  11333. + .statfs = yaffs_statfs,
  11334. +
  11335. +#ifndef YAFFS_USE_OWN_IGET
  11336. + .read_inode = yaffs_read_inode,
  11337. +#endif
  11338. +#ifdef YAFFS_HAS_PUT_INODE
  11339. + .put_inode = yaffs_put_inode,
  11340. +#endif
  11341. + .put_super = yaffs_put_super,
  11342. +#ifdef YAFFS_HAS_EVICT_INODE
  11343. + .evict_inode = yaffs_evict_inode,
  11344. +#else
  11345. + .delete_inode = yaffs_delete_inode,
  11346. + .clear_inode = yaffs_clear_inode,
  11347. +#endif
  11348. + .sync_fs = yaffs_sync_fs,
  11349. + .write_super = yaffs_write_super,
  11350. +};
  11351. +
  11352. +static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
  11353. +{
  11354. +#ifdef YAFFS_USE_SETATTR_COPY
  11355. + setattr_copy(inode, attr);
  11356. + return 0;
  11357. +#else
  11358. + return inode_setattr(inode, attr);
  11359. +#endif
  11360. +
  11361. +}
  11362. +
  11363. +static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
  11364. +{
  11365. +#ifdef YAFFS_USE_TRUNCATE_SETSIZE
  11366. + truncate_setsize(inode, newsize);
  11367. + return 0;
  11368. +#else
  11369. + truncate_inode_pages(&inode->i_data, newsize);
  11370. + return 0;
  11371. +#endif
  11372. +
  11373. +}
  11374. +
  11375. +static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
  11376. +{
  11377. + return yaffs_gc_control;
  11378. +}
  11379. +
  11380. +static void yaffs_gross_lock(struct yaffs_dev *dev)
  11381. +{
  11382. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
  11383. + mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11384. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
  11385. +}
  11386. +
  11387. +static void yaffs_gross_unlock(struct yaffs_dev *dev)
  11388. +{
  11389. + T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
  11390. + mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11391. +}
  11392. +
  11393. +#ifdef YAFFS_COMPILE_EXPORTFS
  11394. +
  11395. +static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  11396. + uint32_t generation)
  11397. +{
  11398. + return Y_IGET(sb, ino);
  11399. +}
  11400. +
  11401. +static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
  11402. + struct fid *fid, int fh_len,
  11403. + int fh_type)
  11404. +{
  11405. + return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  11406. + yaffs2_nfs_get_inode);
  11407. +}
  11408. +
  11409. +static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
  11410. + struct fid *fid, int fh_len,
  11411. + int fh_type)
  11412. +{
  11413. + return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  11414. + yaffs2_nfs_get_inode);
  11415. +}
  11416. +
  11417. +struct dentry *yaffs2_get_parent(struct dentry *dentry)
  11418. +{
  11419. +
  11420. + struct super_block *sb = dentry->d_inode->i_sb;
  11421. + struct dentry *parent = ERR_PTR(-ENOENT);
  11422. + struct inode *inode;
  11423. + unsigned long parent_ino;
  11424. + struct yaffs_obj *d_obj;
  11425. + struct yaffs_obj *parent_obj;
  11426. +
  11427. + d_obj = yaffs_inode_to_obj(dentry->d_inode);
  11428. +
  11429. + if (d_obj) {
  11430. + parent_obj = d_obj->parent;
  11431. + if (parent_obj) {
  11432. + parent_ino = yaffs_get_obj_inode(parent_obj);
  11433. + inode = Y_IGET(sb, parent_ino);
  11434. +
  11435. + if (IS_ERR(inode)) {
  11436. + parent = ERR_CAST(inode);
  11437. + } else {
  11438. + parent = d_obtain_alias(inode);
  11439. + if (!IS_ERR(parent)) {
  11440. + parent = ERR_PTR(-ENOMEM);
  11441. + iput(inode);
  11442. + }
  11443. + }
  11444. + }
  11445. + }
  11446. +
  11447. + return parent;
  11448. +}
  11449. +
  11450. +/* Just declare a zero structure as a NULL value implies
  11451. + * using the default functions of exportfs.
  11452. + */
  11453. +
  11454. +static struct export_operations yaffs_export_ops = {
  11455. + .fh_to_dentry = yaffs2_fh_to_dentry,
  11456. + .fh_to_parent = yaffs2_fh_to_parent,
  11457. + .get_parent = yaffs2_get_parent,
  11458. +};
  11459. +
  11460. +#endif
  11461. +
  11462. +/*-----------------------------------------------------------------*/
  11463. +/* Directory search context allows us to unlock access to yaffs during
  11464. + * filldir without causing problems with the directory being modified.
  11465. + * This is similar to the tried and tested mechanism used in yaffs direct.
  11466. + *
  11467. + * A search context iterates along a doubly linked list of siblings in the
  11468. + * directory. If the iterating object is deleted then this would corrupt
  11469. + * the list iteration, likely causing a crash. The search context avoids
  11470. + * this by using the remove_obj_fn to move the search context to the
  11471. + * next object before the object is deleted.
  11472. + *
  11473. + * Many readdirs (and thus seach conexts) may be alive simulateously so
  11474. + * each struct yaffs_dev has a list of these.
  11475. + *
  11476. + * A seach context lives for the duration of a readdir.
  11477. + *
  11478. + * All these functions must be called while yaffs is locked.
  11479. + */
  11480. +
  11481. +struct yaffs_search_context {
  11482. + struct yaffs_dev *dev;
  11483. + struct yaffs_obj *dir_obj;
  11484. + struct yaffs_obj *next_return;
  11485. + struct list_head others;
  11486. +};
  11487. +
  11488. +/*
  11489. + * yaffs_new_search() creates a new search context, initialises it and
  11490. + * adds it to the device's search context list.
  11491. + *
  11492. + * Called at start of readdir.
  11493. + */
  11494. +static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
  11495. +{
  11496. + struct yaffs_dev *dev = dir->my_dev;
  11497. + struct yaffs_search_context *sc =
  11498. + YMALLOC(sizeof(struct yaffs_search_context));
  11499. + if (sc) {
  11500. + sc->dir_obj = dir;
  11501. + sc->dev = dev;
  11502. + if (list_empty(&sc->dir_obj->variant.dir_variant.children))
  11503. + sc->next_return = NULL;
  11504. + else
  11505. + sc->next_return =
  11506. + list_entry(dir->variant.dir_variant.children.next,
  11507. + struct yaffs_obj, siblings);
  11508. + INIT_LIST_HEAD(&sc->others);
  11509. + list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
  11510. + }
  11511. + return sc;
  11512. +}
  11513. +
  11514. +/*
  11515. + * yaffs_search_end() disposes of a search context and cleans up.
  11516. + */
  11517. +static void yaffs_search_end(struct yaffs_search_context *sc)
  11518. +{
  11519. + if (sc) {
  11520. + list_del(&sc->others);
  11521. + YFREE(sc);
  11522. + }
  11523. +}
  11524. +
  11525. +/*
  11526. + * yaffs_search_advance() moves a search context to the next object.
  11527. + * Called when the search iterates or when an object removal causes
  11528. + * the search context to be moved to the next object.
  11529. + */
  11530. +static void yaffs_search_advance(struct yaffs_search_context *sc)
  11531. +{
  11532. + if (!sc)
  11533. + return;
  11534. +
  11535. + if (sc->next_return == NULL ||
  11536. + list_empty(&sc->dir_obj->variant.dir_variant.children))
  11537. + sc->next_return = NULL;
  11538. + else {
  11539. + struct list_head *next = sc->next_return->siblings.next;
  11540. +
  11541. + if (next == &sc->dir_obj->variant.dir_variant.children)
  11542. + sc->next_return = NULL; /* end of list */
  11543. + else
  11544. + sc->next_return =
  11545. + list_entry(next, struct yaffs_obj, siblings);
  11546. + }
  11547. +}
  11548. +
  11549. +/*
  11550. + * yaffs_remove_obj_callback() is called when an object is unlinked.
  11551. + * We check open search contexts and advance any which are currently
  11552. + * on the object being iterated.
  11553. + */
  11554. +static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
  11555. +{
  11556. +
  11557. + struct list_head *i;
  11558. + struct yaffs_search_context *sc;
  11559. + struct list_head *search_contexts =
  11560. + &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
  11561. +
  11562. + /* Iterate through the directory search contexts.
  11563. + * If any are currently on the object being removed, then advance
  11564. + * the search context to the next object to prevent a hanging pointer.
  11565. + */
  11566. + list_for_each(i, search_contexts) {
  11567. + if (i) {
  11568. + sc = list_entry(i, struct yaffs_search_context, others);
  11569. + if (sc->next_return == obj)
  11570. + yaffs_search_advance(sc);
  11571. + }
  11572. + }
  11573. +
  11574. +}
  11575. +
  11576. +/*-----------------------------------------------------------------*/
  11577. +
  11578. +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
  11579. + int buflen)
  11580. +{
  11581. + unsigned char *alias;
  11582. + int ret;
  11583. +
  11584. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11585. +
  11586. + yaffs_gross_lock(dev);
  11587. +
  11588. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11589. +
  11590. + yaffs_gross_unlock(dev);
  11591. +
  11592. + if (!alias)
  11593. + return -ENOMEM;
  11594. +
  11595. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  11596. + kfree(alias);
  11597. + return ret;
  11598. +}
  11599. +
  11600. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11601. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11602. +{
  11603. + void *ret;
  11604. +#else
  11605. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11606. +{
  11607. + int ret
  11608. +#endif
  11609. + unsigned char *alias;
  11610. + int ret_int = 0;
  11611. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11612. +
  11613. + yaffs_gross_lock(dev);
  11614. +
  11615. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11616. + yaffs_gross_unlock(dev);
  11617. +
  11618. + if (!alias) {
  11619. + ret_int = -ENOMEM;
  11620. + goto out;
  11621. + }
  11622. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11623. + nd_set_link(nd, alias);
  11624. + ret = alias;
  11625. +out:
  11626. + if (ret_int)
  11627. + ret = ERR_PTR(ret_int);
  11628. + return ret;
  11629. +#else
  11630. + ret = vfs_follow_link(nd, alias);
  11631. + kfree(alias);
  11632. +out:
  11633. + if (ret_int)
  11634. + ret = ret_int;
  11635. + return ret;
  11636. +#endif
  11637. +}
  11638. +
  11639. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11640. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
  11641. +{
  11642. + kfree(alias);
  11643. +}
  11644. +#endif
  11645. +
  11646. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  11647. + struct yaffs_obj *obj);
  11648. +
  11649. +/*
  11650. + * Lookup is used to find objects in the fs
  11651. + */
  11652. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11653. +
  11654. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  11655. + struct nameidata *n)
  11656. +#else
  11657. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  11658. +#endif
  11659. +{
  11660. + struct yaffs_obj *obj;
  11661. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  11662. +
  11663. + struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
  11664. +
  11665. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  11666. + yaffs_gross_lock(dev);
  11667. +
  11668. + T(YAFFS_TRACE_OS,
  11669. + (TSTR("yaffs_lookup for %d:%s\n"),
  11670. + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name));
  11671. +
  11672. + obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
  11673. +
  11674. + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
  11675. +
  11676. + /* Can't hold gross lock when calling yaffs_get_inode() */
  11677. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  11678. + yaffs_gross_unlock(dev);
  11679. +
  11680. + if (obj) {
  11681. + T(YAFFS_TRACE_OS,
  11682. + (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
  11683. +
  11684. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  11685. +
  11686. + if (inode) {
  11687. + T(YAFFS_TRACE_OS, (TSTR("yaffs_loookup dentry \n")));
  11688. +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
  11689. + * d_add even if NULL inode */
  11690. +#if 0
  11691. + /*dget(dentry); // try to solve directory bug */
  11692. + d_add(dentry, inode);
  11693. +
  11694. + /* return dentry; */
  11695. + return NULL;
  11696. +#endif
  11697. + }
  11698. +
  11699. + } else {
  11700. + T(YAFFS_TRACE_OS, (TSTR("yaffs_lookup not found\n")));
  11701. +
  11702. + }
  11703. +
  11704. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  11705. + * NULL which creates dentry hash */
  11706. + d_add(dentry, inode);
  11707. +
  11708. + return NULL;
  11709. +}
  11710. +
  11711. +#ifdef YAFFS_HAS_PUT_INODE
  11712. +
  11713. +/* For now put inode is just for debugging
  11714. + * Put inode is called when the inode **structure** is put.
  11715. + */
  11716. +static void yaffs_put_inode(struct inode *inode)
  11717. +{
  11718. + T(YAFFS_TRACE_OS,
  11719. + (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
  11720. + atomic_read(&inode->i_count)));
  11721. +
  11722. +}
  11723. +#endif
  11724. +
  11725. +static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
  11726. +{
  11727. + /* Clear the association between the inode and
  11728. + * the struct yaffs_obj.
  11729. + */
  11730. + obj->my_inode = NULL;
  11731. + yaffs_inode_to_obj_lv(inode) = NULL;
  11732. +
  11733. + /* If the object freeing was deferred, then the real
  11734. + * free happens now.
  11735. + * This should fix the inode inconsistency problem.
  11736. + */
  11737. + yaffs_handle_defered_free(obj);
  11738. +}
  11739. +
  11740. +#ifdef YAFFS_HAS_EVICT_INODE
  11741. +/* yaffs_evict_inode combines into one operation what was previously done in
  11742. + * yaffs_clear_inode() and yaffs_delete_inode()
  11743. + *
  11744. + */
  11745. +static void yaffs_evict_inode(struct inode *inode)
  11746. +{
  11747. + struct yaffs_obj *obj;
  11748. + struct yaffs_dev *dev;
  11749. + int deleteme = 0;
  11750. +
  11751. + obj = yaffs_inode_to_obj(inode);
  11752. +
  11753. + T(YAFFS_TRACE_OS,
  11754. + (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11755. + atomic_read(&inode->i_count),
  11756. + obj ? "object exists" : "null object"));
  11757. +
  11758. + if (!inode->i_nlink && !is_bad_inode(inode))
  11759. + deleteme = 1;
  11760. + truncate_inode_pages(&inode->i_data, 0);
  11761. + end_writeback(inode);
  11762. +
  11763. + if (deleteme && obj) {
  11764. + dev = obj->my_dev;
  11765. + yaffs_gross_lock(dev);
  11766. + yaffs_del_obj(obj);
  11767. + yaffs_gross_unlock(dev);
  11768. + }
  11769. + if (obj) {
  11770. + dev = obj->my_dev;
  11771. + yaffs_gross_lock(dev);
  11772. + yaffs_unstitch_obj(inode, obj);
  11773. + yaffs_gross_unlock(dev);
  11774. + }
  11775. +
  11776. +}
  11777. +#else
  11778. +
  11779. +/* clear is called to tell the fs to release any per-inode data it holds.
  11780. + * The object might still exist on disk and is just being thrown out of the cache
  11781. + * or else the object has actually been deleted and we're being called via
  11782. + * the chain
  11783. + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
  11784. + */
  11785. +
  11786. +static void yaffs_clear_inode(struct inode *inode)
  11787. +{
  11788. + struct yaffs_obj *obj;
  11789. + struct yaffs_dev *dev;
  11790. +
  11791. + obj = yaffs_inode_to_obj(inode);
  11792. +
  11793. + T(YAFFS_TRACE_OS,
  11794. + (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11795. + atomic_read(&inode->i_count),
  11796. + obj ? "object exists" : "null object"));
  11797. +
  11798. + if (obj) {
  11799. + dev = obj->my_dev;
  11800. + yaffs_gross_lock(dev);
  11801. + yaffs_unstitch_obj(inode, obj);
  11802. + yaffs_gross_unlock(dev);
  11803. + }
  11804. +
  11805. +}
  11806. +
  11807. +/* delete is called when the link count is zero and the inode
  11808. + * is put (ie. nobody wants to know about it anymore, time to
  11809. + * delete the file).
  11810. + * NB Must call clear_inode()
  11811. + */
  11812. +static void yaffs_delete_inode(struct inode *inode)
  11813. +{
  11814. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11815. + struct yaffs_dev *dev;
  11816. +
  11817. + T(YAFFS_TRACE_OS,
  11818. + (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
  11819. + atomic_read(&inode->i_count),
  11820. + obj ? "object exists" : "null object"));
  11821. +
  11822. + if (obj) {
  11823. + dev = obj->my_dev;
  11824. + yaffs_gross_lock(dev);
  11825. + yaffs_del_obj(obj);
  11826. + yaffs_gross_unlock(dev);
  11827. + }
  11828. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  11829. + truncate_inode_pages(&inode->i_data, 0);
  11830. +#endif
  11831. + clear_inode(inode);
  11832. +}
  11833. +#endif
  11834. +
  11835. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11836. +static int yaffs_file_flush(struct file *file, fl_owner_t id)
  11837. +#else
  11838. +static int yaffs_file_flush(struct file *file)
  11839. +#endif
  11840. +{
  11841. + struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
  11842. +
  11843. + struct yaffs_dev *dev = obj->my_dev;
  11844. +
  11845. + T(YAFFS_TRACE_OS,
  11846. + (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
  11847. + obj->dirty ? "dirty" : "clean"));
  11848. +
  11849. + yaffs_gross_lock(dev);
  11850. +
  11851. + yaffs_flush_file(obj, 1, 0);
  11852. +
  11853. + yaffs_gross_unlock(dev);
  11854. +
  11855. + return 0;
  11856. +}
  11857. +
  11858. +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  11859. +{
  11860. + /* Lifted from jffs2 */
  11861. +
  11862. + struct yaffs_obj *obj;
  11863. + unsigned char *pg_buf;
  11864. + int ret;
  11865. +
  11866. + struct yaffs_dev *dev;
  11867. +
  11868. + T(YAFFS_TRACE_OS,
  11869. + (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
  11870. + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
  11871. + (unsigned)PAGE_CACHE_SIZE));
  11872. +
  11873. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11874. +
  11875. + dev = obj->my_dev;
  11876. +
  11877. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11878. + BUG_ON(!PageLocked(pg));
  11879. +#else
  11880. + if (!PageLocked(pg))
  11881. + PAGE_BUG(pg);
  11882. +#endif
  11883. +
  11884. + pg_buf = kmap(pg);
  11885. + /* FIXME: Can kmap fail? */
  11886. +
  11887. + yaffs_gross_lock(dev);
  11888. +
  11889. + ret = yaffs_file_rd(obj, pg_buf,
  11890. + pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE);
  11891. +
  11892. + yaffs_gross_unlock(dev);
  11893. +
  11894. + if (ret >= 0)
  11895. + ret = 0;
  11896. +
  11897. + if (ret) {
  11898. + ClearPageUptodate(pg);
  11899. + SetPageError(pg);
  11900. + } else {
  11901. + SetPageUptodate(pg);
  11902. + ClearPageError(pg);
  11903. + }
  11904. +
  11905. + flush_dcache_page(pg);
  11906. + kunmap(pg);
  11907. +
  11908. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
  11909. + return ret;
  11910. +}
  11911. +
  11912. +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  11913. +{
  11914. + int ret = yaffs_readpage_nolock(f, pg);
  11915. + UnlockPage(pg);
  11916. + return ret;
  11917. +}
  11918. +
  11919. +static int yaffs_readpage(struct file *f, struct page *pg)
  11920. +{
  11921. + int ret;
  11922. +
  11923. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
  11924. + ret = yaffs_readpage_unlock(f, pg);
  11925. + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
  11926. + return ret;
  11927. +}
  11928. +
  11929. +/* writepage inspired by/stolen from smbfs */
  11930. +
  11931. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11932. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  11933. +#else
  11934. +static int yaffs_writepage(struct page *page)
  11935. +#endif
  11936. +{
  11937. + struct yaffs_dev *dev;
  11938. + struct address_space *mapping = page->mapping;
  11939. + struct inode *inode;
  11940. + unsigned long end_index;
  11941. + char *buffer;
  11942. + struct yaffs_obj *obj;
  11943. + int n_written = 0;
  11944. + unsigned n_bytes;
  11945. + loff_t i_size;
  11946. +
  11947. + if (!mapping)
  11948. + BUG();
  11949. + inode = mapping->host;
  11950. + if (!inode)
  11951. + BUG();
  11952. + i_size = i_size_read(inode);
  11953. +
  11954. + end_index = i_size >> PAGE_CACHE_SHIFT;
  11955. +
  11956. + if (page->index < end_index)
  11957. + n_bytes = PAGE_CACHE_SIZE;
  11958. + else {
  11959. + n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
  11960. +
  11961. + if (page->index > end_index || !n_bytes) {
  11962. + T(YAFFS_TRACE_OS,
  11963. + (TSTR
  11964. + ("yaffs_writepage at %08x, inode size = %08x!!!\n"),
  11965. + (unsigned)(page->index << PAGE_CACHE_SHIFT),
  11966. + (unsigned)inode->i_size));
  11967. + T(YAFFS_TRACE_OS,
  11968. + (TSTR(" -> don't care!!\n")));
  11969. +
  11970. + zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  11971. + set_page_writeback(page);
  11972. + unlock_page(page);
  11973. + end_page_writeback(page);
  11974. + return 0;
  11975. + }
  11976. + }
  11977. +
  11978. + if (n_bytes != PAGE_CACHE_SIZE)
  11979. + zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
  11980. +
  11981. + get_page(page);
  11982. +
  11983. + buffer = kmap(page);
  11984. +
  11985. + obj = yaffs_inode_to_obj(inode);
  11986. + dev = obj->my_dev;
  11987. + yaffs_gross_lock(dev);
  11988. +
  11989. + T(YAFFS_TRACE_OS,
  11990. + (TSTR("yaffs_writepage at %08x, size %08x\n"),
  11991. + (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
  11992. + T(YAFFS_TRACE_OS,
  11993. + (TSTR("writepag0: obj = %05x, ino = %05x\n"),
  11994. + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
  11995. +
  11996. + n_written = yaffs_wr_file(obj, buffer,
  11997. + page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
  11998. +
  11999. + yaffs_touch_super(dev);
  12000. +
  12001. + T(YAFFS_TRACE_OS,
  12002. + (TSTR("writepag1: obj = %05x, ino = %05x\n"),
  12003. + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
  12004. +
  12005. + yaffs_gross_unlock(dev);
  12006. +
  12007. + kunmap(page);
  12008. + set_page_writeback(page);
  12009. + unlock_page(page);
  12010. + end_page_writeback(page);
  12011. + put_page(page);
  12012. +
  12013. + return (n_written == n_bytes) ? 0 : -ENOSPC;
  12014. +}
  12015. +
  12016. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  12017. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  12018. + loff_t pos, unsigned len, unsigned flags,
  12019. + struct page **pagep, void **fsdata)
  12020. +{
  12021. + struct page *pg = NULL;
  12022. + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  12023. +
  12024. + int ret = 0;
  12025. + int space_held = 0;
  12026. +
  12027. + /* Get a page */
  12028. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
  12029. + pg = grab_cache_page_write_begin(mapping, index, flags);
  12030. +#else
  12031. + pg = __grab_cache_page(mapping, index);
  12032. +#endif
  12033. +
  12034. + *pagep = pg;
  12035. + if (!pg) {
  12036. + ret = -ENOMEM;
  12037. + goto out;
  12038. + }
  12039. + T(YAFFS_TRACE_OS,
  12040. + (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
  12041. + (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0));
  12042. +
  12043. + /* Get fs space */
  12044. + space_held = yaffs_hold_space(filp);
  12045. +
  12046. + if (!space_held) {
  12047. + ret = -ENOSPC;
  12048. + goto out;
  12049. + }
  12050. +
  12051. + /* Update page if required */
  12052. +
  12053. + if (!Page_Uptodate(pg))
  12054. + ret = yaffs_readpage_nolock(filp, pg);
  12055. +
  12056. + if (ret)
  12057. + goto out;
  12058. +
  12059. + /* Happy path return */
  12060. + T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
  12061. +
  12062. + return 0;
  12063. +
  12064. +out:
  12065. + T(YAFFS_TRACE_OS,
  12066. + (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
  12067. + if (space_held)
  12068. + yaffs_release_space(filp);
  12069. + if (pg) {
  12070. + unlock_page(pg);
  12071. + page_cache_release(pg);
  12072. + }
  12073. + return ret;
  12074. +}
  12075. +
  12076. +#else
  12077. +
  12078. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  12079. + unsigned offset, unsigned to)
  12080. +{
  12081. + T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
  12082. +
  12083. + if (!Page_Uptodate(pg))
  12084. + return yaffs_readpage_nolock(f, pg);
  12085. + return 0;
  12086. +}
  12087. +#endif
  12088. +
  12089. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  12090. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  12091. + loff_t pos, unsigned len, unsigned copied,
  12092. + struct page *pg, void *fsdadata)
  12093. +{
  12094. + int ret = 0;
  12095. + void *addr, *kva;
  12096. + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  12097. +
  12098. + kva = kmap(pg);
  12099. + addr = kva + offset_into_page;
  12100. +
  12101. + T(YAFFS_TRACE_OS,
  12102. + ("yaffs_write_end addr %p pos %x n_bytes %d\n",
  12103. + addr, (unsigned)pos, copied));
  12104. +
  12105. + ret = yaffs_file_write(filp, addr, copied, &pos);
  12106. +
  12107. + if (ret != copied) {
  12108. + T(YAFFS_TRACE_OS,
  12109. + (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
  12110. + ret, copied));
  12111. + SetPageError(pg);
  12112. + } else {
  12113. + /* Nothing */
  12114. + }
  12115. +
  12116. + kunmap(pg);
  12117. +
  12118. + yaffs_release_space(filp);
  12119. + unlock_page(pg);
  12120. + page_cache_release(pg);
  12121. + return ret;
  12122. +}
  12123. +#else
  12124. +
  12125. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  12126. + unsigned to)
  12127. +{
  12128. + void *addr, *kva;
  12129. +
  12130. + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
  12131. + int n_bytes = to - offset;
  12132. + int n_written;
  12133. +
  12134. + unsigned spos = pos;
  12135. + unsigned saddr;
  12136. +
  12137. + kva = kmap(pg);
  12138. + addr = kva + offset;
  12139. +
  12140. + saddr = (unsigned)addr;
  12141. +
  12142. + T(YAFFS_TRACE_OS,
  12143. + (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
  12144. + saddr, spos, n_bytes));
  12145. +
  12146. + n_written = yaffs_file_write(f, addr, n_bytes, &pos);
  12147. +
  12148. + if (n_written != n_bytes) {
  12149. + T(YAFFS_TRACE_OS,
  12150. + (TSTR
  12151. + ("yaffs_commit_write not same size n_written %d n_bytes %d\n"),
  12152. + n_written, n_bytes));
  12153. + SetPageError(pg);
  12154. + } else {
  12155. + /* Nothing */
  12156. + }
  12157. +
  12158. + kunmap(pg);
  12159. +
  12160. + T(YAFFS_TRACE_OS,
  12161. + (TSTR("yaffs_commit_write returning %d\n"),
  12162. + n_written == n_bytes ? 0 : n_written));
  12163. +
  12164. + return n_written == n_bytes ? 0 : n_written;
  12165. +}
  12166. +#endif
  12167. +
  12168. +static void yaffs_fill_inode_from_obj(struct inode *inode,
  12169. + struct yaffs_obj *obj)
  12170. +{
  12171. + if (inode && obj) {
  12172. +
  12173. + /* Check mode against the variant type and attempt to repair if broken. */
  12174. + u32 mode = obj->yst_mode;
  12175. + switch (obj->variant_type) {
  12176. + case YAFFS_OBJECT_TYPE_FILE:
  12177. + if (!S_ISREG(mode)) {
  12178. + obj->yst_mode &= ~S_IFMT;
  12179. + obj->yst_mode |= S_IFREG;
  12180. + }
  12181. +
  12182. + break;
  12183. + case YAFFS_OBJECT_TYPE_SYMLINK:
  12184. + if (!S_ISLNK(mode)) {
  12185. + obj->yst_mode &= ~S_IFMT;
  12186. + obj->yst_mode |= S_IFLNK;
  12187. + }
  12188. +
  12189. + break;
  12190. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  12191. + if (!S_ISDIR(mode)) {
  12192. + obj->yst_mode &= ~S_IFMT;
  12193. + obj->yst_mode |= S_IFDIR;
  12194. + }
  12195. +
  12196. + break;
  12197. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  12198. + case YAFFS_OBJECT_TYPE_HARDLINK:
  12199. + case YAFFS_OBJECT_TYPE_SPECIAL:
  12200. + default:
  12201. + /* TODO? */
  12202. + break;
  12203. + }
  12204. +
  12205. + inode->i_flags |= S_NOATIME;
  12206. +
  12207. + inode->i_ino = obj->obj_id;
  12208. + inode->i_mode = obj->yst_mode;
  12209. + inode->i_uid = obj->yst_uid;
  12210. + inode->i_gid = obj->yst_gid;
  12211. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  12212. + inode->i_blksize = inode->i_sb->s_blocksize;
  12213. +#endif
  12214. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12215. +
  12216. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  12217. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  12218. + inode->i_atime.tv_nsec = 0;
  12219. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  12220. + inode->i_mtime.tv_nsec = 0;
  12221. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  12222. + inode->i_ctime.tv_nsec = 0;
  12223. +#else
  12224. + inode->i_rdev = obj->yst_rdev;
  12225. + inode->i_atime = obj->yst_atime;
  12226. + inode->i_mtime = obj->yst_mtime;
  12227. + inode->i_ctime = obj->yst_ctime;
  12228. +#endif
  12229. + inode->i_size = yaffs_get_obj_length(obj);
  12230. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12231. +
  12232. + inode->i_nlink = yaffs_get_obj_link_count(obj);
  12233. +
  12234. + T(YAFFS_TRACE_OS,
  12235. + (TSTR
  12236. + ("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
  12237. + inode->i_mode, inode->i_uid, inode->i_gid,
  12238. + (int)inode->i_size, atomic_read(&inode->i_count)));
  12239. +
  12240. + switch (obj->yst_mode & S_IFMT) {
  12241. + default: /* fifo, device or socket */
  12242. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12243. + init_special_inode(inode, obj->yst_mode,
  12244. + old_decode_dev(obj->yst_rdev));
  12245. +#else
  12246. + init_special_inode(inode, obj->yst_mode,
  12247. + (dev_t) (obj->yst_rdev));
  12248. +#endif
  12249. + break;
  12250. + case S_IFREG: /* file */
  12251. + inode->i_op = &yaffs_file_inode_operations;
  12252. + inode->i_fop = &yaffs_file_operations;
  12253. + inode->i_mapping->a_ops =
  12254. + &yaffs_file_address_operations;
  12255. + break;
  12256. + case S_IFDIR: /* directory */
  12257. + inode->i_op = &yaffs_dir_inode_operations;
  12258. + inode->i_fop = &yaffs_dir_operations;
  12259. + break;
  12260. + case S_IFLNK: /* symlink */
  12261. + inode->i_op = &yaffs_symlink_inode_operations;
  12262. + break;
  12263. + }
  12264. +
  12265. + yaffs_inode_to_obj_lv(inode) = obj;
  12266. +
  12267. + obj->my_inode = inode;
  12268. +
  12269. + } else {
  12270. + T(YAFFS_TRACE_OS,
  12271. + (TSTR("yaffs_fill_inode invalid parameters\n")));
  12272. + }
  12273. +
  12274. +}
  12275. +
  12276. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  12277. + struct yaffs_obj *obj)
  12278. +{
  12279. + struct inode *inode;
  12280. +
  12281. + if (!sb) {
  12282. + T(YAFFS_TRACE_OS,
  12283. + (TSTR("yaffs_get_inode for NULL super_block!!\n")));
  12284. + return NULL;
  12285. +
  12286. + }
  12287. +
  12288. + if (!obj) {
  12289. + T(YAFFS_TRACE_OS,
  12290. + (TSTR("yaffs_get_inode for NULL object!!\n")));
  12291. + return NULL;
  12292. +
  12293. + }
  12294. +
  12295. + T(YAFFS_TRACE_OS,
  12296. + (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
  12297. +
  12298. + inode = Y_IGET(sb, obj->obj_id);
  12299. + if (IS_ERR(inode))
  12300. + return NULL;
  12301. +
  12302. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  12303. + /* iget also increments the inode's i_count */
  12304. + /* NB You can't be holding gross_lock or deadlock will happen! */
  12305. +
  12306. + return inode;
  12307. +}
  12308. +
  12309. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  12310. + loff_t * pos)
  12311. +{
  12312. + struct yaffs_obj *obj;
  12313. + int n_written, ipos;
  12314. + struct inode *inode;
  12315. + struct yaffs_dev *dev;
  12316. +
  12317. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12318. +
  12319. + dev = obj->my_dev;
  12320. +
  12321. + yaffs_gross_lock(dev);
  12322. +
  12323. + inode = f->f_dentry->d_inode;
  12324. +
  12325. + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  12326. + ipos = inode->i_size;
  12327. + else
  12328. + ipos = *pos;
  12329. +
  12330. + if (!obj)
  12331. + T(YAFFS_TRACE_OS,
  12332. + (TSTR("yaffs_file_write: hey obj is null!\n")));
  12333. + else
  12334. + T(YAFFS_TRACE_OS,
  12335. + (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
  12336. + "to object %d at %d(%x)\n"),
  12337. + (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos));
  12338. +
  12339. + n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
  12340. +
  12341. + yaffs_touch_super(dev);
  12342. +
  12343. + T(YAFFS_TRACE_OS,
  12344. + (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
  12345. + (unsigned)n, (unsigned)n));
  12346. +
  12347. + if (n_written > 0) {
  12348. + ipos += n_written;
  12349. + *pos = ipos;
  12350. + if (ipos > inode->i_size) {
  12351. + inode->i_size = ipos;
  12352. + inode->i_blocks = (ipos + 511) >> 9;
  12353. +
  12354. + T(YAFFS_TRACE_OS,
  12355. + (TSTR("yaffs_file_write size updated to %d bytes, "
  12356. + "%d blocks\n"), ipos, (int)(inode->i_blocks)));
  12357. + }
  12358. +
  12359. + }
  12360. + yaffs_gross_unlock(dev);
  12361. + return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
  12362. +}
  12363. +
  12364. +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
  12365. +/* For now we just assume few parallel writes and check against a small number. */
  12366. +/* Todo: need to do this with a counter to handle parallel reads better */
  12367. +
  12368. +static ssize_t yaffs_hold_space(struct file *f)
  12369. +{
  12370. + struct yaffs_obj *obj;
  12371. + struct yaffs_dev *dev;
  12372. +
  12373. + int n_free_chunks;
  12374. +
  12375. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12376. +
  12377. + dev = obj->my_dev;
  12378. +
  12379. + yaffs_gross_lock(dev);
  12380. +
  12381. + n_free_chunks = yaffs_get_n_free_chunks(dev);
  12382. +
  12383. + yaffs_gross_unlock(dev);
  12384. +
  12385. + return (n_free_chunks > 20) ? 1 : 0;
  12386. +}
  12387. +
  12388. +static void yaffs_release_space(struct file *f)
  12389. +{
  12390. + struct yaffs_obj *obj;
  12391. + struct yaffs_dev *dev;
  12392. +
  12393. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12394. +
  12395. + dev = obj->my_dev;
  12396. +
  12397. + yaffs_gross_lock(dev);
  12398. +
  12399. + yaffs_gross_unlock(dev);
  12400. +}
  12401. +
  12402. +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
  12403. +{
  12404. + long long retval;
  12405. +
  12406. + lock_kernel();
  12407. +
  12408. + switch (origin) {
  12409. + case 2:
  12410. + offset += i_size_read(file->f_path.dentry->d_inode);
  12411. + break;
  12412. + case 1:
  12413. + offset += file->f_pos;
  12414. + }
  12415. + retval = -EINVAL;
  12416. +
  12417. + if (offset >= 0) {
  12418. + if (offset != file->f_pos)
  12419. + file->f_pos = offset;
  12420. +
  12421. + retval = offset;
  12422. + }
  12423. + unlock_kernel();
  12424. + return retval;
  12425. +}
  12426. +
  12427. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  12428. +{
  12429. + struct yaffs_obj *obj;
  12430. + struct yaffs_dev *dev;
  12431. + struct yaffs_search_context *sc;
  12432. + struct inode *inode = f->f_dentry->d_inode;
  12433. + unsigned long offset, curoffs;
  12434. + struct yaffs_obj *l;
  12435. + int ret_val = 0;
  12436. +
  12437. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12438. +
  12439. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12440. + dev = obj->my_dev;
  12441. +
  12442. + yaffs_gross_lock(dev);
  12443. +
  12444. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12445. +
  12446. + offset = f->f_pos;
  12447. +
  12448. + sc = yaffs_new_search(obj);
  12449. + if (!sc) {
  12450. + ret_val = -ENOMEM;
  12451. + goto out;
  12452. + }
  12453. +
  12454. + T(YAFFS_TRACE_OS,
  12455. + (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
  12456. +
  12457. + if (offset == 0) {
  12458. + T(YAFFS_TRACE_OS,
  12459. + (TSTR("yaffs_readdir: entry . ino %d \n"),
  12460. + (int)inode->i_ino));
  12461. + yaffs_gross_unlock(dev);
  12462. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
  12463. + yaffs_gross_lock(dev);
  12464. + goto out;
  12465. + }
  12466. + yaffs_gross_lock(dev);
  12467. + offset++;
  12468. + f->f_pos++;
  12469. + }
  12470. + if (offset == 1) {
  12471. + T(YAFFS_TRACE_OS,
  12472. + (TSTR("yaffs_readdir: entry .. ino %d \n"),
  12473. + (int)f->f_dentry->d_parent->d_inode->i_ino));
  12474. + yaffs_gross_unlock(dev);
  12475. + if (filldir(dirent, "..", 2, offset,
  12476. + f->f_dentry->d_parent->d_inode->i_ino,
  12477. + DT_DIR) < 0) {
  12478. + yaffs_gross_lock(dev);
  12479. + goto out;
  12480. + }
  12481. + yaffs_gross_lock(dev);
  12482. + offset++;
  12483. + f->f_pos++;
  12484. + }
  12485. +
  12486. + curoffs = 1;
  12487. +
  12488. + /* If the directory has changed since the open or last call to
  12489. + readdir, rewind to after the 2 canned entries. */
  12490. + if (f->f_version != inode->i_version) {
  12491. + offset = 2;
  12492. + f->f_pos = offset;
  12493. + f->f_version = inode->i_version;
  12494. + }
  12495. +
  12496. + while (sc->next_return) {
  12497. + curoffs++;
  12498. + l = sc->next_return;
  12499. + if (curoffs >= offset) {
  12500. + int this_inode = yaffs_get_obj_inode(l);
  12501. + int this_type = yaffs_get_obj_type(l);
  12502. +
  12503. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12504. + T(YAFFS_TRACE_OS,
  12505. + (TSTR("yaffs_readdir: %s inode %d\n"),
  12506. + name, yaffs_get_obj_inode(l)));
  12507. +
  12508. + yaffs_gross_unlock(dev);
  12509. +
  12510. + if (filldir(dirent,
  12511. + name,
  12512. + strlen(name),
  12513. + offset, this_inode, this_type) < 0) {
  12514. + yaffs_gross_lock(dev);
  12515. + goto out;
  12516. + }
  12517. +
  12518. + yaffs_gross_lock(dev);
  12519. +
  12520. + offset++;
  12521. + f->f_pos++;
  12522. + }
  12523. + yaffs_search_advance(sc);
  12524. + }
  12525. +
  12526. +out:
  12527. + yaffs_search_end(sc);
  12528. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12529. + yaffs_gross_unlock(dev);
  12530. +
  12531. + return ret_val;
  12532. +}
  12533. +
  12534. +/*
  12535. + * File creation. Allocate an inode, and we're done..
  12536. + */
  12537. +
  12538. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  12539. +#define YCRED(x) x
  12540. +#else
  12541. +#define YCRED(x) (x->cred)
  12542. +#endif
  12543. +
  12544. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12545. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12546. + dev_t rdev)
  12547. +#else
  12548. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12549. + int rdev)
  12550. +#endif
  12551. +{
  12552. + struct inode *inode;
  12553. +
  12554. + struct yaffs_obj *obj = NULL;
  12555. + struct yaffs_dev *dev;
  12556. +
  12557. + struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
  12558. +
  12559. + int error = -ENOSPC;
  12560. + uid_t uid = YCRED(current)->fsuid;
  12561. + gid_t gid =
  12562. + (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  12563. +
  12564. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  12565. + mode |= S_ISGID;
  12566. +
  12567. + if (parent) {
  12568. + T(YAFFS_TRACE_OS,
  12569. + (TSTR("yaffs_mknod: parent object %d type %d\n"),
  12570. + parent->obj_id, parent->variant_type));
  12571. + } else {
  12572. + T(YAFFS_TRACE_OS,
  12573. + (TSTR("yaffs_mknod: could not get parent object\n")));
  12574. + return -EPERM;
  12575. + }
  12576. +
  12577. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
  12578. + "mode %x dev %x\n"),
  12579. + dentry->d_name.name, mode, rdev));
  12580. +
  12581. + dev = parent->my_dev;
  12582. +
  12583. + yaffs_gross_lock(dev);
  12584. +
  12585. + switch (mode & S_IFMT) {
  12586. + default:
  12587. + /* Special (socket, fifo, device...) */
  12588. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
  12589. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12590. + obj =
  12591. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12592. + gid, old_encode_dev(rdev));
  12593. +#else
  12594. + obj =
  12595. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12596. + gid, rdev);
  12597. +#endif
  12598. + break;
  12599. + case S_IFREG: /* file */
  12600. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
  12601. + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
  12602. + gid);
  12603. + break;
  12604. + case S_IFDIR: /* directory */
  12605. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making directory\n")));
  12606. + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
  12607. + uid, gid);
  12608. + break;
  12609. + case S_IFLNK: /* symlink */
  12610. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
  12611. + obj = NULL; /* Do we ever get here? */
  12612. + break;
  12613. + }
  12614. +
  12615. + /* Can not call yaffs_get_inode() with gross lock held */
  12616. + yaffs_gross_unlock(dev);
  12617. +
  12618. + if (obj) {
  12619. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  12620. + d_instantiate(dentry, inode);
  12621. + update_dir_time(dir);
  12622. + T(YAFFS_TRACE_OS,
  12623. + (TSTR("yaffs_mknod created object %d count = %d\n"),
  12624. + obj->obj_id, atomic_read(&inode->i_count)));
  12625. + error = 0;
  12626. + yaffs_fill_inode_from_obj(dir, parent);
  12627. + } else {
  12628. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod failed making object\n")));
  12629. + error = -ENOMEM;
  12630. + }
  12631. +
  12632. + return error;
  12633. +}
  12634. +
  12635. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  12636. +{
  12637. + int ret_val;
  12638. + T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
  12639. + ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  12640. + return ret_val;
  12641. +}
  12642. +
  12643. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12644. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  12645. + struct nameidata *n)
  12646. +#else
  12647. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  12648. +#endif
  12649. +{
  12650. + T(YAFFS_TRACE_OS, (TSTR("yaffs_create\n")));
  12651. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  12652. +}
  12653. +
  12654. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  12655. +{
  12656. + int ret_val;
  12657. +
  12658. + struct yaffs_dev *dev;
  12659. + struct yaffs_obj *obj;
  12660. +
  12661. + T(YAFFS_TRACE_OS,
  12662. + (TSTR("yaffs_unlink %d:%s\n"),
  12663. + (int)(dir->i_ino), dentry->d_name.name));
  12664. + obj = yaffs_inode_to_obj(dir);
  12665. + dev = obj->my_dev;
  12666. +
  12667. + yaffs_gross_lock(dev);
  12668. +
  12669. + ret_val = yaffs_unlinker(obj, dentry->d_name.name);
  12670. +
  12671. + if (ret_val == YAFFS_OK) {
  12672. + dentry->d_inode->i_nlink--;
  12673. + dir->i_version++;
  12674. + yaffs_gross_unlock(dev);
  12675. + mark_inode_dirty(dentry->d_inode);
  12676. + update_dir_time(dir);
  12677. + return 0;
  12678. + }
  12679. + yaffs_gross_unlock(dev);
  12680. + return -ENOTEMPTY;
  12681. +}
  12682. +
  12683. +/*
  12684. + * Create a link...
  12685. + */
  12686. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  12687. + struct dentry *dentry)
  12688. +{
  12689. + struct inode *inode = old_dentry->d_inode;
  12690. + struct yaffs_obj *obj = NULL;
  12691. + struct yaffs_obj *link = NULL;
  12692. + struct yaffs_dev *dev;
  12693. +
  12694. + T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
  12695. +
  12696. + obj = yaffs_inode_to_obj(inode);
  12697. + dev = obj->my_dev;
  12698. +
  12699. + yaffs_gross_lock(dev);
  12700. +
  12701. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  12702. + link =
  12703. + yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12704. + obj);
  12705. +
  12706. + if (link) {
  12707. + old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
  12708. + d_instantiate(dentry, old_dentry->d_inode);
  12709. + atomic_inc(&old_dentry->d_inode->i_count);
  12710. + T(YAFFS_TRACE_OS,
  12711. + (TSTR("yaffs_link link count %d i_count %d\n"),
  12712. + old_dentry->d_inode->i_nlink,
  12713. + atomic_read(&old_dentry->d_inode->i_count)));
  12714. + }
  12715. +
  12716. + yaffs_gross_unlock(dev);
  12717. +
  12718. + if (link) {
  12719. + update_dir_time(dir);
  12720. + return 0;
  12721. + }
  12722. +
  12723. + return -EPERM;
  12724. +}
  12725. +
  12726. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  12727. + const char *symname)
  12728. +{
  12729. + struct yaffs_obj *obj;
  12730. + struct yaffs_dev *dev;
  12731. + uid_t uid = YCRED(current)->fsuid;
  12732. + gid_t gid =
  12733. + (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
  12734. +
  12735. + T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
  12736. +
  12737. + dev = yaffs_inode_to_obj(dir)->my_dev;
  12738. + yaffs_gross_lock(dev);
  12739. + obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12740. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  12741. + yaffs_gross_unlock(dev);
  12742. +
  12743. + if (obj) {
  12744. + struct inode *inode;
  12745. +
  12746. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12747. + d_instantiate(dentry, inode);
  12748. + update_dir_time(dir);
  12749. + T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
  12750. + return 0;
  12751. + } else {
  12752. + T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
  12753. + }
  12754. +
  12755. + return -ENOMEM;
  12756. +}
  12757. +
  12758. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  12759. +static int yaffs_sync_object(struct file *file, int datasync)
  12760. +#else
  12761. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  12762. + int datasync)
  12763. +#endif
  12764. +{
  12765. +
  12766. + struct yaffs_obj *obj;
  12767. + struct yaffs_dev *dev;
  12768. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  12769. + struct dentry *dentry = file->f_path.dentry;
  12770. +#endif
  12771. +
  12772. + obj = yaffs_dentry_to_obj(dentry);
  12773. +
  12774. + dev = obj->my_dev;
  12775. +
  12776. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, (TSTR("yaffs_sync_object\n")));
  12777. + yaffs_gross_lock(dev);
  12778. + yaffs_flush_file(obj, 1, datasync);
  12779. + yaffs_gross_unlock(dev);
  12780. + return 0;
  12781. +}
  12782. +
  12783. +/*
  12784. + * The VFS layer already does all the dentry stuff for rename.
  12785. + *
  12786. + * NB: POSIX says you can rename an object over an old object of the same name
  12787. + */
  12788. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  12789. + struct inode *new_dir, struct dentry *new_dentry)
  12790. +{
  12791. + struct yaffs_dev *dev;
  12792. + int ret_val = YAFFS_FAIL;
  12793. + struct yaffs_obj *target;
  12794. +
  12795. + T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
  12796. + dev = yaffs_inode_to_obj(old_dir)->my_dev;
  12797. +
  12798. + yaffs_gross_lock(dev);
  12799. +
  12800. + /* Check if the target is an existing directory that is not empty. */
  12801. + target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
  12802. + new_dentry->d_name.name);
  12803. +
  12804. + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
  12805. + !list_empty(&target->variant.dir_variant.children)) {
  12806. +
  12807. + T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
  12808. +
  12809. + ret_val = YAFFS_FAIL;
  12810. + } else {
  12811. + /* Now does unlinking internally using shadowing mechanism */
  12812. + T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
  12813. +
  12814. + ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
  12815. + old_dentry->d_name.name,
  12816. + yaffs_inode_to_obj(new_dir),
  12817. + new_dentry->d_name.name);
  12818. + }
  12819. + yaffs_gross_unlock(dev);
  12820. +
  12821. + if (ret_val == YAFFS_OK) {
  12822. + if (target) {
  12823. + new_dentry->d_inode->i_nlink--;
  12824. + mark_inode_dirty(new_dentry->d_inode);
  12825. + }
  12826. +
  12827. + update_dir_time(old_dir);
  12828. + if (old_dir != new_dir)
  12829. + update_dir_time(new_dir);
  12830. + return 0;
  12831. + } else {
  12832. + return -ENOTEMPTY;
  12833. + }
  12834. +}
  12835. +
  12836. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  12837. +{
  12838. + struct inode *inode = dentry->d_inode;
  12839. + int error = 0;
  12840. + struct yaffs_dev *dev;
  12841. +
  12842. + T(YAFFS_TRACE_OS,
  12843. + (TSTR("yaffs_setattr of object %d\n"),
  12844. + yaffs_inode_to_obj(inode)->obj_id));
  12845. +
  12846. + /* Fail if a requested resize >= 2GB */
  12847. + if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
  12848. + error = -EINVAL;
  12849. +
  12850. + if (error == 0)
  12851. + error = inode_change_ok(inode, attr);
  12852. + if (error == 0) {
  12853. + int result;
  12854. + if (!error) {
  12855. + error = yaffs_vfs_setattr(inode, attr);
  12856. + T(YAFFS_TRACE_OS, (TSTR("inode_setattr called\n")));
  12857. + if (attr->ia_valid & ATTR_SIZE) {
  12858. + yaffs_vfs_setsize(inode, attr->ia_size);
  12859. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12860. + }
  12861. + }
  12862. + dev = yaffs_inode_to_obj(inode)->my_dev;
  12863. + if (attr->ia_valid & ATTR_SIZE) {
  12864. + T(YAFFS_TRACE_OS, (TSTR("resize to %d(%x)\n"),
  12865. + (int)(attr->ia_size),
  12866. + (int)(attr->ia_size)));
  12867. + }
  12868. + yaffs_gross_lock(dev);
  12869. + result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
  12870. + if (result == YAFFS_OK) {
  12871. + error = 0;
  12872. + } else {
  12873. + error = -EPERM;
  12874. + }
  12875. + yaffs_gross_unlock(dev);
  12876. +
  12877. + }
  12878. +
  12879. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setattr done returning %d\n"), error));
  12880. +
  12881. + return error;
  12882. +}
  12883. +
  12884. +#ifdef CONFIG_YAFFS_XATTR
  12885. +int yaffs_setxattr(struct dentry *dentry, const char *name,
  12886. + const void *value, size_t size, int flags)
  12887. +{
  12888. + struct inode *inode = dentry->d_inode;
  12889. + int error = 0;
  12890. + struct yaffs_dev *dev;
  12891. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12892. +
  12893. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr of object %d\n"), obj->obj_id));
  12894. +
  12895. + if (error == 0) {
  12896. + int result;
  12897. + dev = obj->my_dev;
  12898. + yaffs_gross_lock(dev);
  12899. + result = yaffs_set_xattrib(obj, name, value, size, flags);
  12900. + if (result == YAFFS_OK)
  12901. + error = 0;
  12902. + else if (result < 0)
  12903. + error = result;
  12904. + yaffs_gross_unlock(dev);
  12905. +
  12906. + }
  12907. + T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr done returning %d\n"), error));
  12908. +
  12909. + return error;
  12910. +}
  12911. +
  12912. +ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff,
  12913. + size_t size)
  12914. +{
  12915. + struct inode *inode = dentry->d_inode;
  12916. + int error = 0;
  12917. + struct yaffs_dev *dev;
  12918. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12919. +
  12920. + T(YAFFS_TRACE_OS,
  12921. + (TSTR("yaffs_getxattr \"%s\" from object %d\n"), name, obj->obj_id));
  12922. +
  12923. + if (error == 0) {
  12924. + dev = obj->my_dev;
  12925. + yaffs_gross_lock(dev);
  12926. + error = yaffs_get_xattrib(obj, name, buff, size);
  12927. + yaffs_gross_unlock(dev);
  12928. +
  12929. + }
  12930. + T(YAFFS_TRACE_OS, (TSTR("yaffs_getxattr done returning %d\n"), error));
  12931. +
  12932. + return error;
  12933. +}
  12934. +
  12935. +int yaffs_removexattr(struct dentry *dentry, const char *name)
  12936. +{
  12937. + struct inode *inode = dentry->d_inode;
  12938. + int error = 0;
  12939. + struct yaffs_dev *dev;
  12940. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12941. +
  12942. + T(YAFFS_TRACE_OS,
  12943. + (TSTR("yaffs_removexattr of object %d\n"), obj->obj_id));
  12944. +
  12945. + if (error == 0) {
  12946. + int result;
  12947. + dev = obj->my_dev;
  12948. + yaffs_gross_lock(dev);
  12949. + result = yaffs_remove_xattrib(obj, name);
  12950. + if (result == YAFFS_OK)
  12951. + error = 0;
  12952. + else if (result < 0)
  12953. + error = result;
  12954. + yaffs_gross_unlock(dev);
  12955. +
  12956. + }
  12957. + T(YAFFS_TRACE_OS,
  12958. + (TSTR("yaffs_removexattr done returning %d\n"), error));
  12959. +
  12960. + return error;
  12961. +}
  12962. +
  12963. +ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
  12964. +{
  12965. + struct inode *inode = dentry->d_inode;
  12966. + int error = 0;
  12967. + struct yaffs_dev *dev;
  12968. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  12969. +
  12970. + T(YAFFS_TRACE_OS,
  12971. + (TSTR("yaffs_listxattr of object %d\n"), obj->obj_id));
  12972. +
  12973. + if (error == 0) {
  12974. + dev = obj->my_dev;
  12975. + yaffs_gross_lock(dev);
  12976. + error = yaffs_list_xattrib(obj, buff, size);
  12977. + yaffs_gross_unlock(dev);
  12978. +
  12979. + }
  12980. + T(YAFFS_TRACE_OS, (TSTR("yaffs_listxattr done returning %d\n"), error));
  12981. +
  12982. + return error;
  12983. +}
  12984. +
  12985. +#endif
  12986. +
  12987. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  12988. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  12989. +{
  12990. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  12991. + struct super_block *sb = dentry->d_sb;
  12992. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12993. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  12994. +{
  12995. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  12996. +#else
  12997. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  12998. +{
  12999. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13000. +#endif
  13001. +
  13002. + T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
  13003. +
  13004. + yaffs_gross_lock(dev);
  13005. +
  13006. + buf->f_type = YAFFS_MAGIC;
  13007. + buf->f_bsize = sb->s_blocksize;
  13008. + buf->f_namelen = 255;
  13009. +
  13010. + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
  13011. + /* Do this if chunk size is not a power of 2 */
  13012. +
  13013. + uint64_t bytes_in_dev;
  13014. + uint64_t bytes_free;
  13015. +
  13016. + bytes_in_dev =
  13017. + ((uint64_t)
  13018. + ((dev->param.end_block - dev->param.start_block +
  13019. + 1))) * ((uint64_t) (dev->param.chunks_per_block *
  13020. + dev->data_bytes_per_chunk));
  13021. +
  13022. + do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
  13023. + buf->f_blocks = bytes_in_dev;
  13024. +
  13025. + bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
  13026. + ((uint64_t) (dev->data_bytes_per_chunk));
  13027. +
  13028. + do_div(bytes_free, sb->s_blocksize);
  13029. +
  13030. + buf->f_bfree = bytes_free;
  13031. +
  13032. + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
  13033. +
  13034. + buf->f_blocks =
  13035. + (dev->param.end_block - dev->param.start_block + 1) *
  13036. + dev->param.chunks_per_block /
  13037. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13038. + buf->f_bfree =
  13039. + yaffs_get_n_free_chunks(dev) /
  13040. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13041. + } else {
  13042. + buf->f_blocks =
  13043. + (dev->param.end_block - dev->param.start_block + 1) *
  13044. + dev->param.chunks_per_block *
  13045. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13046. +
  13047. + buf->f_bfree =
  13048. + yaffs_get_n_free_chunks(dev) *
  13049. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13050. + }
  13051. +
  13052. + buf->f_files = 0;
  13053. + buf->f_ffree = 0;
  13054. + buf->f_bavail = buf->f_bfree;
  13055. +
  13056. + yaffs_gross_unlock(dev);
  13057. + return 0;
  13058. +}
  13059. +
  13060. +static void yaffs_flush_inodes(struct super_block *sb)
  13061. +{
  13062. + struct inode *iptr;
  13063. + struct yaffs_obj *obj;
  13064. +
  13065. + list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
  13066. + obj = yaffs_inode_to_obj(iptr);
  13067. + if (obj) {
  13068. + T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
  13069. + obj->obj_id));
  13070. + yaffs_flush_file(obj, 1, 0);
  13071. + }
  13072. + }
  13073. +}
  13074. +
  13075. +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
  13076. +{
  13077. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13078. + if (!dev)
  13079. + return;
  13080. +
  13081. + yaffs_flush_inodes(sb);
  13082. + yaffs_update_dirty_dirs(dev);
  13083. + yaffs_flush_whole_cache(dev);
  13084. + if (do_checkpoint)
  13085. + yaffs_checkpoint_save(dev);
  13086. +}
  13087. +
  13088. +static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
  13089. +{
  13090. + unsigned erased_chunks =
  13091. + dev->n_erased_blocks * dev->param.chunks_per_block;
  13092. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13093. + unsigned scattered = 0; /* Free chunks not in an erased block */
  13094. +
  13095. + if (erased_chunks < dev->n_free_chunks)
  13096. + scattered = (dev->n_free_chunks - erased_chunks);
  13097. +
  13098. + if (!context->bg_running)
  13099. + return 0;
  13100. + else if (scattered < (dev->param.chunks_per_block * 2))
  13101. + return 0;
  13102. + else if (erased_chunks > dev->n_free_chunks / 2)
  13103. + return 0;
  13104. + else if (erased_chunks > dev->n_free_chunks / 4)
  13105. + return 1;
  13106. + else
  13107. + return 2;
  13108. +}
  13109. +
  13110. +static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
  13111. +{
  13112. +
  13113. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13114. + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
  13115. + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
  13116. + int do_checkpoint;
  13117. +
  13118. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13119. + (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
  13120. + gc_urgent,
  13121. + sb->s_dirt ? "dirty" : "clean",
  13122. + request_checkpoint ? "checkpoint requested" : "no checkpoint",
  13123. + oneshot_checkpoint ? " one-shot" : ""));
  13124. +
  13125. + yaffs_gross_lock(dev);
  13126. + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
  13127. + oneshot_checkpoint) && !dev->is_checkpointed;
  13128. +
  13129. + if (sb->s_dirt || do_checkpoint) {
  13130. + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
  13131. + sb->s_dirt = 0;
  13132. + if (oneshot_checkpoint)
  13133. + yaffs_auto_checkpoint &= ~4;
  13134. + }
  13135. + yaffs_gross_unlock(dev);
  13136. +
  13137. + return 0;
  13138. +}
  13139. +
  13140. +/*
  13141. + * yaffs background thread functions .
  13142. + * yaffs_bg_thread_fn() the thread function
  13143. + * yaffs_bg_start() launches the background thread.
  13144. + * yaffs_bg_stop() cleans up the background thread.
  13145. + *
  13146. + * NB:
  13147. + * The thread should only run after the yaffs is initialised
  13148. + * The thread should be stopped before yaffs is unmounted.
  13149. + * The thread should not do any writing while the fs is in read only.
  13150. + */
  13151. +
  13152. +#ifdef YAFFS_COMPILE_BACKGROUND
  13153. +
  13154. +void yaffs_background_waker(unsigned long data)
  13155. +{
  13156. + wake_up_process((struct task_struct *)data);
  13157. +}
  13158. +
  13159. +static int yaffs_bg_thread_fn(void *data)
  13160. +{
  13161. + struct yaffs_dev *dev = (struct yaffs_dev *)data;
  13162. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13163. + unsigned long now = jiffies;
  13164. + unsigned long next_dir_update = now;
  13165. + unsigned long next_gc = now;
  13166. + unsigned long expires;
  13167. + unsigned int urgency;
  13168. +
  13169. + int gc_result;
  13170. + struct timer_list timer;
  13171. +
  13172. + T(YAFFS_TRACE_BACKGROUND,
  13173. + (TSTR("yaffs_background starting for dev %p\n"), (void *)dev));
  13174. +
  13175. +#ifdef YAFFS_COMPILE_FREEZER
  13176. + set_freezable();
  13177. +#endif
  13178. + while (context->bg_running) {
  13179. + T(YAFFS_TRACE_BACKGROUND, (TSTR("yaffs_background\n")));
  13180. +
  13181. + if (kthread_should_stop())
  13182. + break;
  13183. +
  13184. +#ifdef YAFFS_COMPILE_FREEZER
  13185. + if (try_to_freeze())
  13186. + continue;
  13187. +#endif
  13188. + yaffs_gross_lock(dev);
  13189. +
  13190. + now = jiffies;
  13191. +
  13192. + if (time_after(now, next_dir_update) && yaffs_bg_enable) {
  13193. + yaffs_update_dirty_dirs(dev);
  13194. + next_dir_update = now + HZ;
  13195. + }
  13196. +
  13197. + if (time_after(now, next_gc) && yaffs_bg_enable) {
  13198. + if (!dev->is_checkpointed) {
  13199. + urgency = yaffs_bg_gc_urgency(dev);
  13200. + gc_result = yaffs_bg_gc(dev, urgency);
  13201. + if (urgency > 1)
  13202. + next_gc = now + HZ / 20 + 1;
  13203. + else if (urgency > 0)
  13204. + next_gc = now + HZ / 10 + 1;
  13205. + else
  13206. + next_gc = now + HZ * 2;
  13207. + } else {
  13208. + /*
  13209. + * gc not running so set to next_dir_update
  13210. + * to cut down on wake ups
  13211. + */
  13212. + next_gc = next_dir_update;
  13213. + }
  13214. + }
  13215. + yaffs_gross_unlock(dev);
  13216. +#if 1
  13217. + expires = next_dir_update;
  13218. + if (time_before(next_gc, expires))
  13219. + expires = next_gc;
  13220. + if (time_before(expires, now))
  13221. + expires = now + HZ;
  13222. +
  13223. + Y_INIT_TIMER(&timer);
  13224. + timer.expires = expires + 1;
  13225. + timer.data = (unsigned long)current;
  13226. + timer.function = yaffs_background_waker;
  13227. +
  13228. + set_current_state(TASK_INTERRUPTIBLE);
  13229. + add_timer(&timer);
  13230. + schedule();
  13231. + del_timer_sync(&timer);
  13232. +#else
  13233. + msleep(10);
  13234. +#endif
  13235. + }
  13236. +
  13237. + return 0;
  13238. +}
  13239. +
  13240. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13241. +{
  13242. + int retval = 0;
  13243. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  13244. +
  13245. + if (dev->read_only)
  13246. + return -1;
  13247. +
  13248. + context->bg_running = 1;
  13249. +
  13250. + context->bg_thread = kthread_run(yaffs_bg_thread_fn,
  13251. + (void *)dev, "yaffs-bg-%d",
  13252. + context->mount_id);
  13253. +
  13254. + if (IS_ERR(context->bg_thread)) {
  13255. + retval = PTR_ERR(context->bg_thread);
  13256. + context->bg_thread = NULL;
  13257. + context->bg_running = 0;
  13258. + }
  13259. + return retval;
  13260. +}
  13261. +
  13262. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13263. +{
  13264. + struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
  13265. +
  13266. + ctxt->bg_running = 0;
  13267. +
  13268. + if (ctxt->bg_thread) {
  13269. + kthread_stop(ctxt->bg_thread);
  13270. + ctxt->bg_thread = NULL;
  13271. + }
  13272. +}
  13273. +#else
  13274. +static int yaffs_bg_thread_fn(void *data)
  13275. +{
  13276. + return 0;
  13277. +}
  13278. +
  13279. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13280. +{
  13281. + return 0;
  13282. +}
  13283. +
  13284. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13285. +{
  13286. +}
  13287. +#endif
  13288. +
  13289. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13290. +static void yaffs_write_super(struct super_block *sb)
  13291. +#else
  13292. +static int yaffs_write_super(struct super_block *sb)
  13293. +#endif
  13294. +{
  13295. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
  13296. +
  13297. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13298. + (TSTR("yaffs_write_super%s\n"),
  13299. + request_checkpoint ? " checkpt" : ""));
  13300. +
  13301. + yaffs_do_sync_fs(sb, request_checkpoint);
  13302. +
  13303. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  13304. + return 0;
  13305. +#endif
  13306. +}
  13307. +
  13308. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13309. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  13310. +#else
  13311. +static int yaffs_sync_fs(struct super_block *sb)
  13312. +#endif
  13313. +{
  13314. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
  13315. +
  13316. + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  13317. + (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : ""));
  13318. +
  13319. + yaffs_do_sync_fs(sb, request_checkpoint);
  13320. +
  13321. + return 0;
  13322. +}
  13323. +
  13324. +#ifdef YAFFS_USE_OWN_IGET
  13325. +
  13326. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  13327. +{
  13328. + struct inode *inode;
  13329. + struct yaffs_obj *obj;
  13330. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13331. +
  13332. + T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino));
  13333. +
  13334. + inode = iget_locked(sb, ino);
  13335. + if (!inode)
  13336. + return ERR_PTR(-ENOMEM);
  13337. + if (!(inode->i_state & I_NEW))
  13338. + return inode;
  13339. +
  13340. + /* NB This is called as a side effect of other functions, but
  13341. + * we had to release the lock to prevent deadlocks, so
  13342. + * need to lock again.
  13343. + */
  13344. +
  13345. + yaffs_gross_lock(dev);
  13346. +
  13347. + obj = yaffs_find_by_number(dev, inode->i_ino);
  13348. +
  13349. + yaffs_fill_inode_from_obj(inode, obj);
  13350. +
  13351. + yaffs_gross_unlock(dev);
  13352. +
  13353. + unlock_new_inode(inode);
  13354. + return inode;
  13355. +}
  13356. +
  13357. +#else
  13358. +
  13359. +static void yaffs_read_inode(struct inode *inode)
  13360. +{
  13361. + /* NB This is called as a side effect of other functions, but
  13362. + * we had to release the lock to prevent deadlocks, so
  13363. + * need to lock again.
  13364. + */
  13365. +
  13366. + struct yaffs_obj *obj;
  13367. + struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
  13368. +
  13369. + T(YAFFS_TRACE_OS,
  13370. + (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
  13371. +
  13372. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  13373. + yaffs_gross_lock(dev);
  13374. +
  13375. + obj = yaffs_find_by_number(dev, inode->i_ino);
  13376. +
  13377. + yaffs_fill_inode_from_obj(inode, obj);
  13378. +
  13379. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  13380. + yaffs_gross_unlock(dev);
  13381. +}
  13382. +
  13383. +#endif
  13384. +
  13385. +static LIST_HEAD(yaffs_context_list);
  13386. +struct mutex yaffs_context_lock;
  13387. +
  13388. +static void yaffs_put_super(struct super_block *sb)
  13389. +{
  13390. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13391. +
  13392. + T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
  13393. +
  13394. + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13395. + (TSTR("Shutting down yaffs background thread\n")));
  13396. + yaffs_bg_stop(dev);
  13397. + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13398. + (TSTR("yaffs background thread shut down\n")));
  13399. +
  13400. + yaffs_gross_lock(dev);
  13401. +
  13402. + yaffs_flush_super(sb, 1);
  13403. +
  13404. + if (yaffs_dev_to_lc(dev)->put_super_fn)
  13405. + yaffs_dev_to_lc(dev)->put_super_fn(sb);
  13406. +
  13407. + yaffs_deinitialise(dev);
  13408. +
  13409. + yaffs_gross_unlock(dev);
  13410. +
  13411. + mutex_lock(&yaffs_context_lock);
  13412. + list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
  13413. + mutex_unlock(&yaffs_context_lock);
  13414. +
  13415. + if (yaffs_dev_to_lc(dev)->spare_buffer) {
  13416. + YFREE(yaffs_dev_to_lc(dev)->spare_buffer);
  13417. + yaffs_dev_to_lc(dev)->spare_buffer = NULL;
  13418. + }
  13419. +
  13420. + kfree(dev);
  13421. +}
  13422. +
  13423. +static void yaffs_mtd_put_super(struct super_block *sb)
  13424. +{
  13425. + struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb));
  13426. +
  13427. + if (mtd->sync)
  13428. + mtd->sync(mtd);
  13429. +
  13430. + put_mtd_device(mtd);
  13431. +}
  13432. +
  13433. +static void yaffs_touch_super(struct yaffs_dev *dev)
  13434. +{
  13435. + struct super_block *sb = yaffs_dev_to_lc(dev)->super;
  13436. +
  13437. + T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
  13438. + if (sb)
  13439. + sb->s_dirt = 1;
  13440. +}
  13441. +
  13442. +struct yaffs_options {
  13443. + int inband_tags;
  13444. + int skip_checkpoint_read;
  13445. + int skip_checkpoint_write;
  13446. + int no_cache;
  13447. + int tags_ecc_on;
  13448. + int tags_ecc_overridden;
  13449. + int lazy_loading_enabled;
  13450. + int lazy_loading_overridden;
  13451. + int empty_lost_and_found;
  13452. + int empty_lost_and_found_overridden;
  13453. +};
  13454. +
  13455. +#define MAX_OPT_LEN 30
  13456. +static int yaffs_parse_options(struct yaffs_options *options,
  13457. + const char *options_str)
  13458. +{
  13459. + char cur_opt[MAX_OPT_LEN + 1];
  13460. + int p;
  13461. + int error = 0;
  13462. +
  13463. + /* Parse through the options which is a comma seperated list */
  13464. +
  13465. + while (options_str && *options_str && !error) {
  13466. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  13467. + p = 0;
  13468. +
  13469. + while (*options_str == ',')
  13470. + options_str++;
  13471. +
  13472. + while (*options_str && *options_str != ',') {
  13473. + if (p < MAX_OPT_LEN) {
  13474. + cur_opt[p] = *options_str;
  13475. + p++;
  13476. + }
  13477. + options_str++;
  13478. + }
  13479. +
  13480. + if (!strcmp(cur_opt, "inband-tags")) {
  13481. + options->inband_tags = 1;
  13482. + } else if (!strcmp(cur_opt, "tags-ecc-off")) {
  13483. + options->tags_ecc_on = 0;
  13484. + options->tags_ecc_overridden = 1;
  13485. + } else if (!strcmp(cur_opt, "tags-ecc-on")) {
  13486. + options->tags_ecc_on = 1;
  13487. + options->tags_ecc_overridden = 1;
  13488. + } else if (!strcmp(cur_opt, "lazy-loading-off")) {
  13489. + options->lazy_loading_enabled = 0;
  13490. + options->lazy_loading_overridden = 1;
  13491. + } else if (!strcmp(cur_opt, "lazy-loading-on")) {
  13492. + options->lazy_loading_enabled = 1;
  13493. + options->lazy_loading_overridden = 1;
  13494. + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
  13495. + options->empty_lost_and_found = 0;
  13496. + options->empty_lost_and_found_overridden = 1;
  13497. + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
  13498. + options->empty_lost_and_found = 1;
  13499. + options->empty_lost_and_found_overridden = 1;
  13500. + } else if (!strcmp(cur_opt, "no-cache")) {
  13501. + options->no_cache = 1;
  13502. + } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
  13503. + options->skip_checkpoint_read = 1;
  13504. + } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
  13505. + options->skip_checkpoint_write = 1;
  13506. + } else if (!strcmp(cur_opt, "no-checkpoint")) {
  13507. + options->skip_checkpoint_read = 1;
  13508. + options->skip_checkpoint_write = 1;
  13509. + } else {
  13510. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  13511. + cur_opt);
  13512. + error = 1;
  13513. + }
  13514. + }
  13515. +
  13516. + return error;
  13517. +}
  13518. +
  13519. +static struct super_block *yaffs_internal_read_super(int yaffs_version,
  13520. + struct super_block *sb,
  13521. + void *data, int silent)
  13522. +{
  13523. + int n_blocks;
  13524. + struct inode *inode = NULL;
  13525. + struct dentry *root;
  13526. + struct yaffs_dev *dev = 0;
  13527. + char devname_buf[BDEVNAME_SIZE + 1];
  13528. + struct mtd_info *mtd;
  13529. + int err;
  13530. + char *data_str = (char *)data;
  13531. + struct yaffs_linux_context *context = NULL;
  13532. + struct yaffs_param *param;
  13533. +
  13534. + int read_only = 0;
  13535. +
  13536. + struct yaffs_options options;
  13537. +
  13538. + unsigned mount_id;
  13539. + int found;
  13540. + struct yaffs_linux_context *context_iterator;
  13541. + struct list_head *l;
  13542. +
  13543. + sb->s_magic = YAFFS_MAGIC;
  13544. + sb->s_op = &yaffs_super_ops;
  13545. + sb->s_flags |= MS_NOATIME;
  13546. +
  13547. + read_only = ((sb->s_flags & MS_RDONLY) != 0);
  13548. +
  13549. +#ifdef YAFFS_COMPILE_EXPORTFS
  13550. + sb->s_export_op = &yaffs_export_ops;
  13551. +#endif
  13552. +
  13553. + if (!sb)
  13554. + printk(KERN_INFO "yaffs: sb is NULL\n");
  13555. + else if (!sb->s_dev)
  13556. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  13557. + else if (!yaffs_devname(sb, devname_buf))
  13558. + printk(KERN_INFO "yaffs: devname is NULL\n");
  13559. + else
  13560. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
  13561. + sb->s_dev,
  13562. + yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
  13563. +
  13564. + if (!data_str)
  13565. + data_str = "";
  13566. +
  13567. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  13568. +
  13569. + memset(&options, 0, sizeof(options));
  13570. +
  13571. + if (yaffs_parse_options(&options, data_str)) {
  13572. + /* Option parsing failed */
  13573. + return NULL;
  13574. + }
  13575. +
  13576. + sb->s_blocksize = PAGE_CACHE_SIZE;
  13577. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  13578. +
  13579. + T(YAFFS_TRACE_OS,
  13580. + (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
  13581. + T(YAFFS_TRACE_OS,
  13582. + (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize)));
  13583. +
  13584. + T(YAFFS_TRACE_ALWAYS,
  13585. + (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
  13586. + MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf)));
  13587. +
  13588. + /* Check it's an mtd device..... */
  13589. + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
  13590. + return NULL; /* This isn't an mtd device */
  13591. +
  13592. + /* Get the device */
  13593. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  13594. + if (!mtd) {
  13595. + T(YAFFS_TRACE_ALWAYS,
  13596. + (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
  13597. + MINOR(sb->s_dev)));
  13598. + return NULL;
  13599. + }
  13600. + /* Check it's NAND */
  13601. + if (mtd->type != MTD_NANDFLASH) {
  13602. + T(YAFFS_TRACE_ALWAYS,
  13603. + (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
  13604. + mtd->type));
  13605. + return NULL;
  13606. + }
  13607. +
  13608. + T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
  13609. + T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
  13610. + T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
  13611. + T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
  13612. + T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
  13613. + T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
  13614. + T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
  13615. + T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
  13616. + T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
  13617. + T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
  13618. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  13619. + T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
  13620. +#else
  13621. + T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
  13622. +#endif
  13623. +
  13624. +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
  13625. +
  13626. + if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
  13627. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n")));
  13628. + yaffs_version = 2;
  13629. + }
  13630. +
  13631. + /* Added NCB 26/5/2006 for completeness */
  13632. + if (yaffs_version == 2 && !options.inband_tags
  13633. + && WRITE_SIZE(mtd) == 512) {
  13634. + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n")));
  13635. + yaffs_version = 1;
  13636. + }
  13637. +#endif
  13638. +
  13639. + if (yaffs_version == 2) {
  13640. + /* Check for version 2 style functions */
  13641. + if (!mtd->erase ||
  13642. + !mtd->block_isbad ||
  13643. + !mtd->block_markbad || !mtd->read || !mtd->write ||
  13644. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13645. + !mtd->read_oob || !mtd->write_oob) {
  13646. +#else
  13647. + !mtd->write_ecc ||
  13648. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  13649. +#endif
  13650. + T(YAFFS_TRACE_ALWAYS,
  13651. + (TSTR("yaffs: MTD device does not support required "
  13652. + "functions\n")));
  13653. + return NULL;
  13654. + }
  13655. +
  13656. + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  13657. + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  13658. + !options.inband_tags) {
  13659. + T(YAFFS_TRACE_ALWAYS,
  13660. + (TSTR("yaffs: MTD device does not have the "
  13661. + "right page sizes\n")));
  13662. + return NULL;
  13663. + }
  13664. + } else {
  13665. + /* Check for V1 style functions */
  13666. + if (!mtd->erase || !mtd->read || !mtd->write ||
  13667. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13668. + !mtd->read_oob || !mtd->write_oob) {
  13669. +#else
  13670. + !mtd->write_ecc ||
  13671. + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
  13672. +#endif
  13673. + T(YAFFS_TRACE_ALWAYS,
  13674. + (TSTR("yaffs: MTD device does not support required "
  13675. + "functions\n")));
  13676. + return NULL;
  13677. + }
  13678. +
  13679. + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  13680. + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  13681. + T(YAFFS_TRACE_ALWAYS,
  13682. + (TSTR("yaffs: MTD device does not support have the "
  13683. + "right page sizes\n")));
  13684. + return NULL;
  13685. + }
  13686. + }
  13687. +
  13688. + /* OK, so if we got here, we have an MTD that's NAND and looks
  13689. + * like it has the right capabilities
  13690. + * Set the struct yaffs_dev up for mtd
  13691. + */
  13692. +
  13693. + if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
  13694. + read_only = 1;
  13695. + printk(KERN_INFO
  13696. + "yaffs: mtd is read only, setting superblock read only");
  13697. + sb->s_flags |= MS_RDONLY;
  13698. + }
  13699. +
  13700. + dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
  13701. + context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
  13702. +
  13703. + if (!dev || !context) {
  13704. + if (dev)
  13705. + kfree(dev);
  13706. + if (context)
  13707. + kfree(context);
  13708. + dev = NULL;
  13709. + context = NULL;
  13710. + }
  13711. +
  13712. + if (!dev) {
  13713. + /* Deep shit could not allocate device structure */
  13714. + T(YAFFS_TRACE_ALWAYS,
  13715. + (TSTR("yaffs_read_super: Failed trying to allocate "
  13716. + "struct yaffs_dev. \n")));
  13717. + return NULL;
  13718. + }
  13719. + memset(dev, 0, sizeof(struct yaffs_dev));
  13720. + param = &(dev->param);
  13721. +
  13722. + memset(context, 0, sizeof(struct yaffs_linux_context));
  13723. + dev->os_context = context;
  13724. + INIT_LIST_HEAD(&(context->context_list));
  13725. + context->dev = dev;
  13726. + context->super = sb;
  13727. +
  13728. + dev->read_only = read_only;
  13729. +
  13730. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13731. + sb->s_fs_info = dev;
  13732. +#else
  13733. + sb->u.generic_sbp = dev;
  13734. +#endif
  13735. +
  13736. + dev->driver_context = mtd;
  13737. + param->name = mtd->name;
  13738. +
  13739. + /* Set up the memory size parameters.... */
  13740. +
  13741. + n_blocks =
  13742. + YCALCBLOCKS(mtd->size,
  13743. + (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
  13744. +
  13745. + param->start_block = 0;
  13746. + param->end_block = n_blocks - 1;
  13747. + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
  13748. + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
  13749. + param->n_reserved_blocks = 5;
  13750. + param->n_caches = (options.no_cache) ? 0 : 10;
  13751. + param->inband_tags = options.inband_tags;
  13752. +
  13753. +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
  13754. + param->disable_lazy_load = 1;
  13755. +#endif
  13756. +#ifdef CONFIG_YAFFS_XATTR
  13757. + param->enable_xattr = 1;
  13758. +#endif
  13759. + if (options.lazy_loading_overridden)
  13760. + param->disable_lazy_load = !options.lazy_loading_enabled;
  13761. +
  13762. +#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
  13763. + param->no_tags_ecc = 1;
  13764. +#endif
  13765. +
  13766. +#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
  13767. +#else
  13768. + param->defered_dir_update = 1;
  13769. +#endif
  13770. +
  13771. + if (options.tags_ecc_overridden)
  13772. + param->no_tags_ecc = !options.tags_ecc_on;
  13773. +
  13774. +#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
  13775. + param->empty_lost_n_found = 1;
  13776. +#endif
  13777. +
  13778. +#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
  13779. + param->refresh_period = 0;
  13780. +#else
  13781. + param->refresh_period = 500;
  13782. +#endif
  13783. +
  13784. +#ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
  13785. + param->always_check_erased = 1;
  13786. +#endif
  13787. +
  13788. + if (options.empty_lost_and_found_overridden)
  13789. + param->empty_lost_n_found = options.empty_lost_and_found;
  13790. +
  13791. + /* ... and the functions. */
  13792. + if (yaffs_version == 2) {
  13793. + param->write_chunk_tags_fn = nandmtd2_write_chunk_tags;
  13794. + param->read_chunk_tags_fn = nandmtd2_read_chunk_tags;
  13795. + param->bad_block_fn = nandmtd2_mark_block_bad;
  13796. + param->query_block_fn = nandmtd2_query_block;
  13797. + yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize);
  13798. + param->is_yaffs2 = 1;
  13799. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13800. + param->total_bytes_per_chunk = mtd->writesize;
  13801. + param->chunks_per_block = mtd->erasesize / mtd->writesize;
  13802. +#else
  13803. + param->total_bytes_per_chunk = mtd->oobblock;
  13804. + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
  13805. +#endif
  13806. + n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  13807. +
  13808. + param->start_block = 0;
  13809. + param->end_block = n_blocks - 1;
  13810. + } else {
  13811. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13812. + /* use the MTD interface in yaffs_mtdif1.c */
  13813. + param->write_chunk_tags_fn = nandmtd1_write_chunk_tags;
  13814. + param->read_chunk_tags_fn = nandmtd1_read_chunk_tags;
  13815. + param->bad_block_fn = nandmtd1_mark_block_bad;
  13816. + param->query_block_fn = nandmtd1_query_block;
  13817. +#else
  13818. + param->write_chunk_fn = nandmtd_write_chunk;
  13819. + param->read_chunk_fn = nandmtd_read_chunk;
  13820. +#endif
  13821. + param->is_yaffs2 = 0;
  13822. + }
  13823. + /* ... and common functions */
  13824. + param->erase_fn = nandmtd_erase_block;
  13825. + param->initialise_flash_fn = nandmtd_initialise;
  13826. +
  13827. + yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super;
  13828. +
  13829. + param->sb_dirty_fn = yaffs_touch_super;
  13830. + param->gc_control = yaffs_gc_control_callback;
  13831. +
  13832. + yaffs_dev_to_lc(dev)->super = sb;
  13833. +
  13834. +#ifndef CONFIG_YAFFS_DOES_ECC
  13835. + param->use_nand_ecc = 1;
  13836. +#endif
  13837. +
  13838. +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
  13839. + param->wide_tnodes_disabled = 1;
  13840. +#endif
  13841. +
  13842. + param->skip_checkpt_rd = options.skip_checkpoint_read;
  13843. + param->skip_checkpt_wr = options.skip_checkpoint_write;
  13844. +
  13845. + mutex_lock(&yaffs_context_lock);
  13846. + /* Get a mount id */
  13847. + found = 0;
  13848. + for (mount_id = 0; !found; mount_id++) {
  13849. + found = 1;
  13850. + list_for_each(l, &yaffs_context_list) {
  13851. + context_iterator =
  13852. + list_entry(l, struct yaffs_linux_context,
  13853. + context_list);
  13854. + if (context_iterator->mount_id == mount_id)
  13855. + found = 0;
  13856. + }
  13857. + }
  13858. + context->mount_id = mount_id;
  13859. +
  13860. + list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
  13861. + &yaffs_context_list);
  13862. + mutex_unlock(&yaffs_context_lock);
  13863. +
  13864. + /* Directory search handling... */
  13865. + INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
  13866. + param->remove_obj_fn = yaffs_remove_obj_callback;
  13867. +
  13868. + mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
  13869. +
  13870. + yaffs_gross_lock(dev);
  13871. +
  13872. + err = yaffs_guts_initialise(dev);
  13873. +
  13874. + T(YAFFS_TRACE_OS,
  13875. + (TSTR("yaffs_read_super: guts initialised %s\n"),
  13876. + (err == YAFFS_OK) ? "OK" : "FAILED"));
  13877. +
  13878. + if (err == YAFFS_OK)
  13879. + yaffs_bg_start(dev);
  13880. +
  13881. + if (!context->bg_thread)
  13882. + param->defered_dir_update = 0;
  13883. +
  13884. + /* Release lock before yaffs_get_inode() */
  13885. + yaffs_gross_unlock(dev);
  13886. +
  13887. + /* Create root inode */
  13888. + if (err == YAFFS_OK)
  13889. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
  13890. +
  13891. + if (!inode)
  13892. + return NULL;
  13893. +
  13894. + inode->i_op = &yaffs_dir_inode_operations;
  13895. + inode->i_fop = &yaffs_dir_operations;
  13896. +
  13897. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
  13898. +
  13899. + root = d_alloc_root(inode);
  13900. +
  13901. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
  13902. +
  13903. + if (!root) {
  13904. + iput(inode);
  13905. + return NULL;
  13906. + }
  13907. + sb->s_root = root;
  13908. + sb->s_dirt = !dev->is_checkpointed;
  13909. + T(YAFFS_TRACE_ALWAYS,
  13910. + (TSTR("yaffs_read_super: is_checkpointed %d\n"),
  13911. + dev->is_checkpointed));
  13912. +
  13913. + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
  13914. + return sb;
  13915. +}
  13916. +
  13917. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13918. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  13919. + int silent)
  13920. +{
  13921. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  13922. +}
  13923. +
  13924. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13925. +static int yaffs_read_super(struct file_system_type *fs,
  13926. + int flags, const char *dev_name,
  13927. + void *data, struct vfsmount *mnt)
  13928. +{
  13929. +
  13930. + return get_sb_bdev(fs, flags, dev_name, data,
  13931. + yaffs_internal_read_super_mtd, mnt);
  13932. +}
  13933. +#else
  13934. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  13935. + int flags, const char *dev_name,
  13936. + void *data)
  13937. +{
  13938. +
  13939. + return get_sb_bdev(fs, flags, dev_name, data,
  13940. + yaffs_internal_read_super_mtd);
  13941. +}
  13942. +#endif
  13943. +
  13944. +static struct file_system_type yaffs_fs_type = {
  13945. + .owner = THIS_MODULE,
  13946. + .name = "yaffs",
  13947. + .get_sb = yaffs_read_super,
  13948. + .kill_sb = kill_block_super,
  13949. + .fs_flags = FS_REQUIRES_DEV,
  13950. +};
  13951. +#else
  13952. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  13953. + int silent)
  13954. +{
  13955. + return yaffs_internal_read_super(1, sb, data, silent);
  13956. +}
  13957. +
  13958. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  13959. + FS_REQUIRES_DEV);
  13960. +#endif
  13961. +
  13962. +#ifdef CONFIG_YAFFS_YAFFS2
  13963. +
  13964. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13965. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  13966. + int silent)
  13967. +{
  13968. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  13969. +}
  13970. +
  13971. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13972. +static int yaffs2_read_super(struct file_system_type *fs,
  13973. + int flags, const char *dev_name, void *data,
  13974. + struct vfsmount *mnt)
  13975. +{
  13976. + return get_sb_bdev(fs, flags, dev_name, data,
  13977. + yaffs2_internal_read_super_mtd, mnt);
  13978. +}
  13979. +#else
  13980. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  13981. + int flags, const char *dev_name,
  13982. + void *data)
  13983. +{
  13984. +
  13985. + return get_sb_bdev(fs, flags, dev_name, data,
  13986. + yaffs2_internal_read_super_mtd);
  13987. +}
  13988. +#endif
  13989. +
  13990. +static struct file_system_type yaffs2_fs_type = {
  13991. + .owner = THIS_MODULE,
  13992. + .name = "yaffs2",
  13993. + .get_sb = yaffs2_read_super,
  13994. + .kill_sb = kill_block_super,
  13995. + .fs_flags = FS_REQUIRES_DEV,
  13996. +};
  13997. +#else
  13998. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  13999. + void *data, int silent)
  14000. +{
  14001. + return yaffs_internal_read_super(2, sb, data, silent);
  14002. +}
  14003. +
  14004. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  14005. + FS_REQUIRES_DEV);
  14006. +#endif
  14007. +
  14008. +#endif /* CONFIG_YAFFS_YAFFS2 */
  14009. +
  14010. +static struct proc_dir_entry *my_proc_entry;
  14011. +static struct proc_dir_entry *debug_proc_entry;
  14012. +
  14013. +static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
  14014. +{
  14015. + buf +=
  14016. + sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
  14017. + buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
  14018. + buf +=
  14019. + sprintf(buf, "total_bytes_per_chunk %d\n",
  14020. + dev->param.total_bytes_per_chunk);
  14021. + buf +=
  14022. + sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
  14023. + buf +=
  14024. + sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
  14025. + buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
  14026. + buf +=
  14027. + sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
  14028. + buf +=
  14029. + sprintf(buf, "empty_lost_n_found... %d\n",
  14030. + dev->param.empty_lost_n_found);
  14031. + buf +=
  14032. + sprintf(buf, "disable_lazy_load.... %d\n",
  14033. + dev->param.disable_lazy_load);
  14034. + buf +=
  14035. + sprintf(buf, "refresh_period....... %d\n",
  14036. + dev->param.refresh_period);
  14037. + buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
  14038. + buf +=
  14039. + sprintf(buf, "n_reserved_blocks.... %d\n",
  14040. + dev->param.n_reserved_blocks);
  14041. + buf +=
  14042. + sprintf(buf, "always_check_erased.. %d\n",
  14043. + dev->param.always_check_erased);
  14044. +
  14045. + buf += sprintf(buf, "\n");
  14046. +
  14047. + return buf;
  14048. +}
  14049. +
  14050. +static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
  14051. +{
  14052. + buf +=
  14053. + sprintf(buf, "data_bytes_per_chunk. %d\n",
  14054. + dev->data_bytes_per_chunk);
  14055. + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
  14056. + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
  14057. + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
  14058. + buf +=
  14059. + sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
  14060. + buf += sprintf(buf, "\n");
  14061. + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
  14062. + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
  14063. + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
  14064. + buf += sprintf(buf, "\n");
  14065. + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
  14066. + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
  14067. + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
  14068. + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
  14069. + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
  14070. + buf +=
  14071. + sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
  14072. + buf +=
  14073. + sprintf(buf, "oldest_dirty_gc_count %u\n",
  14074. + dev->oldest_dirty_gc_count);
  14075. + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
  14076. + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
  14077. + buf +=
  14078. + sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
  14079. + buf +=
  14080. + sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks);
  14081. + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
  14082. + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
  14083. + buf +=
  14084. + sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
  14085. + buf +=
  14086. + sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
  14087. + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
  14088. + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
  14089. + buf +=
  14090. + sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
  14091. + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
  14092. + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
  14093. +
  14094. + return buf;
  14095. +}
  14096. +
  14097. +static int yaffs_proc_read(char *page,
  14098. + char **start,
  14099. + off_t offset, int count, int *eof, void *data)
  14100. +{
  14101. + struct list_head *item;
  14102. + char *buf = page;
  14103. + int step = offset;
  14104. + int n = 0;
  14105. +
  14106. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  14107. + * We use 'offset' (*ppos) to indicate where we are in dev_list.
  14108. + * This also assumes the user has posted a read buffer large
  14109. + * enough to hold the complete output; but that's life in /proc.
  14110. + */
  14111. +
  14112. + *(int *)start = 1;
  14113. +
  14114. + /* Print header first */
  14115. + if (step == 0)
  14116. + buf +=
  14117. + sprintf(buf,
  14118. + "Multi-version YAFFS built:" __DATE__ " " __TIME__
  14119. + "\n");
  14120. + else if (step == 1)
  14121. + buf += sprintf(buf, "\n");
  14122. + else {
  14123. + step -= 2;
  14124. +
  14125. + mutex_lock(&yaffs_context_lock);
  14126. +
  14127. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14128. + list_for_each(item, &yaffs_context_list) {
  14129. + struct yaffs_linux_context *dc =
  14130. + list_entry(item, struct yaffs_linux_context,
  14131. + context_list);
  14132. + struct yaffs_dev *dev = dc->dev;
  14133. +
  14134. + if (n < (step & ~1)) {
  14135. + n += 2;
  14136. + continue;
  14137. + }
  14138. + if ((step & 1) == 0) {
  14139. + buf +=
  14140. + sprintf(buf, "\nDevice %d \"%s\"\n", n,
  14141. + dev->param.name);
  14142. + buf = yaffs_dump_dev_part0(buf, dev);
  14143. + } else {
  14144. + buf = yaffs_dump_dev_part1(buf, dev);
  14145. + }
  14146. +
  14147. + break;
  14148. + }
  14149. + mutex_unlock(&yaffs_context_lock);
  14150. + }
  14151. +
  14152. + return buf - page < count ? buf - page : count;
  14153. +}
  14154. +
  14155. +static int yaffs_stats_proc_read(char *page,
  14156. + char **start,
  14157. + off_t offset, int count, int *eof, void *data)
  14158. +{
  14159. + struct list_head *item;
  14160. + char *buf = page;
  14161. + int n = 0;
  14162. +
  14163. + mutex_lock(&yaffs_context_lock);
  14164. +
  14165. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14166. + list_for_each(item, &yaffs_context_list) {
  14167. + struct yaffs_linux_context *dc =
  14168. + list_entry(item, struct yaffs_linux_context, context_list);
  14169. + struct yaffs_dev *dev = dc->dev;
  14170. +
  14171. + int erased_chunks;
  14172. +
  14173. + erased_chunks =
  14174. + dev->n_erased_blocks * dev->param.chunks_per_block;
  14175. +
  14176. + buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n",
  14177. + n, dev->n_free_chunks, erased_chunks,
  14178. + dev->bg_gcs, dev->oldest_dirty_gc_count,
  14179. + dev->n_obj, dev->n_tnodes);
  14180. + }
  14181. + mutex_unlock(&yaffs_context_lock);
  14182. +
  14183. + return buf - page < count ? buf - page : count;
  14184. +}
  14185. +
  14186. +/**
  14187. + * Set the verbosity of the warnings and error messages.
  14188. + *
  14189. + * Note that the names can only be a..z or _ with the current code.
  14190. + */
  14191. +
  14192. +static struct {
  14193. + char *mask_name;
  14194. + unsigned mask_bitfield;
  14195. +} mask_flags[] = {
  14196. + {"allocate", YAFFS_TRACE_ALLOCATE},
  14197. + {"always", YAFFS_TRACE_ALWAYS},
  14198. + {"background", YAFFS_TRACE_BACKGROUND},
  14199. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  14200. + {"buffers", YAFFS_TRACE_BUFFERS},
  14201. + {"bug", YAFFS_TRACE_BUG},
  14202. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  14203. + {"deletion", YAFFS_TRACE_DELETION},
  14204. + {"erase", YAFFS_TRACE_ERASE},
  14205. + {"error", YAFFS_TRACE_ERROR},
  14206. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  14207. + {"gc", YAFFS_TRACE_GC},
  14208. + {"lock", YAFFS_TRACE_LOCK},
  14209. + {"mtd", YAFFS_TRACE_MTD},
  14210. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  14211. + {"os", YAFFS_TRACE_OS},
  14212. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  14213. + {"scan", YAFFS_TRACE_SCAN},
  14214. + {"mount", YAFFS_TRACE_MOUNT},
  14215. + {"tracing", YAFFS_TRACE_TRACING},
  14216. + {"sync", YAFFS_TRACE_SYNC},
  14217. + {"write", YAFFS_TRACE_WRITE},
  14218. + {"verify", YAFFS_TRACE_VERIFY},
  14219. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  14220. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  14221. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  14222. + {"all", 0xffffffff},
  14223. + {"none", 0},
  14224. + {NULL, 0},
  14225. +};
  14226. +
  14227. +#define MAX_MASK_NAME_LENGTH 40
  14228. +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
  14229. + unsigned long count, void *data)
  14230. +{
  14231. + unsigned rg = 0, mask_bitfield;
  14232. + char *end;
  14233. + char *mask_name;
  14234. + const char *x;
  14235. + char substring[MAX_MASK_NAME_LENGTH + 1];
  14236. + int i;
  14237. + int done = 0;
  14238. + int add, len = 0;
  14239. + int pos = 0;
  14240. +
  14241. + rg = yaffs_trace_mask;
  14242. +
  14243. + while (!done && (pos < count)) {
  14244. + done = 1;
  14245. + while ((pos < count) && isspace(buf[pos]))
  14246. + pos++;
  14247. +
  14248. + switch (buf[pos]) {
  14249. + case '+':
  14250. + case '-':
  14251. + case '=':
  14252. + add = buf[pos];
  14253. + pos++;
  14254. + break;
  14255. +
  14256. + default:
  14257. + add = ' ';
  14258. + break;
  14259. + }
  14260. + mask_name = NULL;
  14261. +
  14262. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  14263. +
  14264. + if (end > buf + pos) {
  14265. + mask_name = "numeral";
  14266. + len = end - (buf + pos);
  14267. + pos += len;
  14268. + done = 0;
  14269. + } else {
  14270. + for (x = buf + pos, i = 0;
  14271. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  14272. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  14273. + substring[i] = *x;
  14274. + substring[i] = '\0';
  14275. +
  14276. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14277. + if (strcmp(substring, mask_flags[i].mask_name)
  14278. + == 0) {
  14279. + mask_name = mask_flags[i].mask_name;
  14280. + mask_bitfield =
  14281. + mask_flags[i].mask_bitfield;
  14282. + done = 0;
  14283. + break;
  14284. + }
  14285. + }
  14286. + }
  14287. +
  14288. + if (mask_name != NULL) {
  14289. + done = 0;
  14290. + switch (add) {
  14291. + case '-':
  14292. + rg &= ~mask_bitfield;
  14293. + break;
  14294. + case '+':
  14295. + rg |= mask_bitfield;
  14296. + break;
  14297. + case '=':
  14298. + rg = mask_bitfield;
  14299. + break;
  14300. + default:
  14301. + rg |= mask_bitfield;
  14302. + break;
  14303. + }
  14304. + }
  14305. + }
  14306. +
  14307. + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
  14308. +
  14309. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
  14310. +
  14311. + if (rg & YAFFS_TRACE_ALWAYS) {
  14312. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14313. + char flag;
  14314. + flag = ((rg & mask_flags[i].mask_bitfield) ==
  14315. + mask_flags[i].mask_bitfield) ? '+' : '-';
  14316. + printk(KERN_DEBUG "%c%s\n", flag,
  14317. + mask_flags[i].mask_name);
  14318. + }
  14319. + }
  14320. +
  14321. + return count;
  14322. +}
  14323. +
  14324. +static int yaffs_proc_write(struct file *file, const char *buf,
  14325. + unsigned long count, void *data)
  14326. +{
  14327. + return yaffs_proc_write_trace_options(file, buf, count, data);
  14328. +}
  14329. +
  14330. +/* Stuff to handle installation of file systems */
  14331. +struct file_system_to_install {
  14332. + struct file_system_type *fst;
  14333. + int installed;
  14334. +};
  14335. +
  14336. +static struct file_system_to_install fs_to_install[] = {
  14337. + {&yaffs_fs_type, 0},
  14338. + {&yaffs2_fs_type, 0},
  14339. + {NULL, 0}
  14340. +};
  14341. +
  14342. +static int __init init_yaffs_fs(void)
  14343. +{
  14344. + int error = 0;
  14345. + struct file_system_to_install *fsinst;
  14346. +
  14347. + T(YAFFS_TRACE_ALWAYS,
  14348. + (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
  14349. +
  14350. +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  14351. + T(YAFFS_TRACE_ALWAYS,
  14352. + (TSTR
  14353. + (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
  14354. +#endif
  14355. +
  14356. + mutex_init(&yaffs_context_lock);
  14357. +
  14358. + /* Install the proc_fs entries */
  14359. + my_proc_entry = create_proc_entry("yaffs",
  14360. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14361. +
  14362. + if (my_proc_entry) {
  14363. + my_proc_entry->write_proc = yaffs_proc_write;
  14364. + my_proc_entry->read_proc = yaffs_proc_read;
  14365. + my_proc_entry->data = NULL;
  14366. + } else {
  14367. + return -ENOMEM;
  14368. + }
  14369. +
  14370. + debug_proc_entry = create_proc_entry("yaffs_stats",
  14371. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14372. +
  14373. + if (debug_proc_entry) {
  14374. + debug_proc_entry->write_proc = NULL;
  14375. + debug_proc_entry->read_proc = yaffs_stats_proc_read;
  14376. + debug_proc_entry->data = NULL;
  14377. + } else {
  14378. + return -ENOMEM;
  14379. + }
  14380. +
  14381. + /* Now add the file system entries */
  14382. +
  14383. + fsinst = fs_to_install;
  14384. +
  14385. + while (fsinst->fst && !error) {
  14386. + error = register_filesystem(fsinst->fst);
  14387. + if (!error)
  14388. + fsinst->installed = 1;
  14389. + fsinst++;
  14390. + }
  14391. +
  14392. + /* Any errors? uninstall */
  14393. + if (error) {
  14394. + fsinst = fs_to_install;
  14395. +
  14396. + while (fsinst->fst) {
  14397. + if (fsinst->installed) {
  14398. + unregister_filesystem(fsinst->fst);
  14399. + fsinst->installed = 0;
  14400. + }
  14401. + fsinst++;
  14402. + }
  14403. + }
  14404. +
  14405. + return error;
  14406. +}
  14407. +
  14408. +static void __exit exit_yaffs_fs(void)
  14409. +{
  14410. +
  14411. + struct file_system_to_install *fsinst;
  14412. +
  14413. + T(YAFFS_TRACE_ALWAYS,
  14414. + (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
  14415. +
  14416. + remove_proc_entry("yaffs", YPROC_ROOT);
  14417. + remove_proc_entry("yaffs_stats", YPROC_ROOT);
  14418. +
  14419. + fsinst = fs_to_install;
  14420. +
  14421. + while (fsinst->fst) {
  14422. + if (fsinst->installed) {
  14423. + unregister_filesystem(fsinst->fst);
  14424. + fsinst->installed = 0;
  14425. + }
  14426. + fsinst++;
  14427. + }
  14428. +}
  14429. +
  14430. +module_init(init_yaffs_fs)
  14431. + module_exit(exit_yaffs_fs)
  14432. +
  14433. + MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  14434. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
  14435. +MODULE_LICENSE("GPL");
  14436. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.c linux-2.6.36/fs/yaffs2/yaffs_yaffs1.c
  14437. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.c 1970-01-01 01:00:00.000000000 +0100
  14438. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs1.c 2011-01-10 19:29:29.000000000 +0100
  14439. @@ -0,0 +1,437 @@
  14440. +/*
  14441. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14442. + *
  14443. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14444. + * for Toby Churchill Ltd and Brightstar Engineering
  14445. + *
  14446. + * Created by Charles Manning <charles@aleph1.co.uk>
  14447. + *
  14448. + * This program is free software; you can redistribute it and/or modify
  14449. + * it under the terms of the GNU General Public License version 2 as
  14450. + * published by the Free Software Foundation.
  14451. + */
  14452. +
  14453. +#include "yaffs_yaffs1.h"
  14454. +#include "yportenv.h"
  14455. +#include "yaffs_trace.h"
  14456. +#include "yaffs_bitmap.h"
  14457. +#include "yaffs_getblockinfo.h"
  14458. +#include "yaffs_nand.h"
  14459. +#include "yaffs_attribs.h"
  14460. +
  14461. +int yaffs1_scan(struct yaffs_dev *dev)
  14462. +{
  14463. + struct yaffs_ext_tags tags;
  14464. + int blk;
  14465. + int result;
  14466. +
  14467. + int chunk;
  14468. + int c;
  14469. + int deleted;
  14470. + enum yaffs_block_state state;
  14471. + struct yaffs_obj *hard_list = NULL;
  14472. + struct yaffs_block_info *bi;
  14473. + u32 seq_number;
  14474. + struct yaffs_obj_hdr *oh;
  14475. + struct yaffs_obj *in;
  14476. + struct yaffs_obj *parent;
  14477. +
  14478. + int alloc_failed = 0;
  14479. +
  14480. + struct yaffs_shadow_fixer *shadow_fixers = NULL;
  14481. +
  14482. + u8 *chunk_data;
  14483. +
  14484. + T(YAFFS_TRACE_SCAN,
  14485. + (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
  14486. + dev->internal_start_block, dev->internal_end_block));
  14487. +
  14488. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  14489. +
  14490. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  14491. +
  14492. + /* Scan all the blocks to determine their state */
  14493. + bi = dev->block_info;
  14494. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  14495. + blk++) {
  14496. + yaffs_clear_chunk_bits(dev, blk);
  14497. + bi->pages_in_use = 0;
  14498. + bi->soft_del_pages = 0;
  14499. +
  14500. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  14501. +
  14502. + bi->block_state = state;
  14503. + bi->seq_number = seq_number;
  14504. +
  14505. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  14506. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  14507. +
  14508. + T(YAFFS_TRACE_SCAN_DEBUG,
  14509. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  14510. + state, seq_number));
  14511. +
  14512. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  14513. + T(YAFFS_TRACE_BAD_BLOCKS,
  14514. + (TSTR("block %d is bad" TENDSTR), blk));
  14515. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  14516. + T(YAFFS_TRACE_SCAN_DEBUG,
  14517. + (TSTR("Block empty " TENDSTR)));
  14518. + dev->n_erased_blocks++;
  14519. + dev->n_free_chunks += dev->param.chunks_per_block;
  14520. + }
  14521. + bi++;
  14522. + }
  14523. +
  14524. + /* For each block.... */
  14525. + for (blk = dev->internal_start_block;
  14526. + !alloc_failed && blk <= dev->internal_end_block; blk++) {
  14527. +
  14528. + YYIELD();
  14529. +
  14530. + bi = yaffs_get_block_info(dev, blk);
  14531. + state = bi->block_state;
  14532. +
  14533. + deleted = 0;
  14534. +
  14535. + /* For each chunk in each block that needs scanning.... */
  14536. + for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
  14537. + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
  14538. + /* Read the tags and decide what to do */
  14539. + chunk = blk * dev->param.chunks_per_block + c;
  14540. +
  14541. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  14542. + &tags);
  14543. +
  14544. + /* Let's have a good look at this chunk... */
  14545. +
  14546. + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
  14547. + || tags.is_deleted) {
  14548. + /* YAFFS1 only...
  14549. + * A deleted chunk
  14550. + */
  14551. + deleted++;
  14552. + dev->n_free_chunks++;
  14553. + /*T((" %d %d deleted\n",blk,c)); */
  14554. + } else if (!tags.chunk_used) {
  14555. + /* An unassigned chunk in the block
  14556. + * This means that either the block is empty or
  14557. + * this is the one being allocated from
  14558. + */
  14559. +
  14560. + if (c == 0) {
  14561. + /* We're looking at the first chunk in the block so the block is unused */
  14562. + state = YAFFS_BLOCK_STATE_EMPTY;
  14563. + dev->n_erased_blocks++;
  14564. + } else {
  14565. + /* this is the block being allocated from */
  14566. + T(YAFFS_TRACE_SCAN,
  14567. + (TSTR
  14568. + (" Allocating from %d %d" TENDSTR),
  14569. + blk, c));
  14570. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  14571. + dev->alloc_block = blk;
  14572. + dev->alloc_page = c;
  14573. + dev->alloc_block_finder = blk;
  14574. + /* Set block finder here to encourage the allocator to go forth from here. */
  14575. +
  14576. + }
  14577. +
  14578. + dev->n_free_chunks +=
  14579. + (dev->param.chunks_per_block - c);
  14580. + } else if (tags.chunk_id > 0) {
  14581. + /* chunk_id > 0 so it is a data chunk... */
  14582. + unsigned int endpos;
  14583. +
  14584. + yaffs_set_chunk_bit(dev, blk, c);
  14585. + bi->pages_in_use++;
  14586. +
  14587. + in = yaffs_find_or_create_by_number(dev,
  14588. + tags.obj_id,
  14589. + YAFFS_OBJECT_TYPE_FILE);
  14590. + /* PutChunkIntoFile checks for a clash (two data chunks with
  14591. + * the same chunk_id).
  14592. + */
  14593. +
  14594. + if (!in)
  14595. + alloc_failed = 1;
  14596. +
  14597. + if (in) {
  14598. + if (!yaffs_put_chunk_in_file
  14599. + (in, tags.chunk_id, chunk, 1))
  14600. + alloc_failed = 1;
  14601. + }
  14602. +
  14603. + endpos =
  14604. + (tags.chunk_id -
  14605. + 1) * dev->data_bytes_per_chunk +
  14606. + tags.n_bytes;
  14607. + if (in
  14608. + && in->variant_type ==
  14609. + YAFFS_OBJECT_TYPE_FILE
  14610. + && in->variant.file_variant.scanned_size <
  14611. + endpos) {
  14612. + in->variant.file_variant.scanned_size =
  14613. + endpos;
  14614. + if (!dev->param.use_header_file_size) {
  14615. + in->variant.
  14616. + file_variant.file_size =
  14617. + in->variant.
  14618. + file_variant.scanned_size;
  14619. + }
  14620. +
  14621. + }
  14622. + /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
  14623. + } else {
  14624. + /* chunk_id == 0, so it is an ObjectHeader.
  14625. + * Thus, we read in the object header and make the object
  14626. + */
  14627. + yaffs_set_chunk_bit(dev, blk, c);
  14628. + bi->pages_in_use++;
  14629. +
  14630. + result = yaffs_rd_chunk_tags_nand(dev, chunk,
  14631. + chunk_data,
  14632. + NULL);
  14633. +
  14634. + oh = (struct yaffs_obj_hdr *)chunk_data;
  14635. +
  14636. + in = yaffs_find_by_number(dev, tags.obj_id);
  14637. + if (in && in->variant_type != oh->type) {
  14638. + /* This should not happen, but somehow
  14639. + * Wev'e ended up with an obj_id that has been reused but not yet
  14640. + * deleted, and worse still it has changed type. Delete the old object.
  14641. + */
  14642. +
  14643. + yaffs_del_obj(in);
  14644. +
  14645. + in = 0;
  14646. + }
  14647. +
  14648. + in = yaffs_find_or_create_by_number(dev,
  14649. + tags.obj_id,
  14650. + oh->type);
  14651. +
  14652. + if (!in)
  14653. + alloc_failed = 1;
  14654. +
  14655. + if (in && oh->shadows_obj > 0) {
  14656. +
  14657. + struct yaffs_shadow_fixer *fixer;
  14658. + fixer =
  14659. + YMALLOC(sizeof
  14660. + (struct
  14661. + yaffs_shadow_fixer));
  14662. + if (fixer) {
  14663. + fixer->next = shadow_fixers;
  14664. + shadow_fixers = fixer;
  14665. + fixer->obj_id = tags.obj_id;
  14666. + fixer->shadowed_id =
  14667. + oh->shadows_obj;
  14668. + T(YAFFS_TRACE_SCAN,
  14669. + (TSTR
  14670. + (" Shadow fixer: %d shadows %d"
  14671. + TENDSTR), fixer->obj_id,
  14672. + fixer->shadowed_id));
  14673. +
  14674. + }
  14675. +
  14676. + }
  14677. +
  14678. + if (in && in->valid) {
  14679. + /* We have already filled this one. We have a duplicate and need to resolve it. */
  14680. +
  14681. + unsigned existing_serial = in->serial;
  14682. + unsigned new_serial =
  14683. + tags.serial_number;
  14684. +
  14685. + if (((existing_serial + 1) & 3) ==
  14686. + new_serial) {
  14687. + /* Use new one - destroy the exisiting one */
  14688. + yaffs_chunk_del(dev,
  14689. + in->hdr_chunk,
  14690. + 1, __LINE__);
  14691. + in->valid = 0;
  14692. + } else {
  14693. + /* Use existing - destroy this one. */
  14694. + yaffs_chunk_del(dev, chunk, 1,
  14695. + __LINE__);
  14696. + }
  14697. + }
  14698. +
  14699. + if (in && !in->valid &&
  14700. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  14701. + tags.obj_id ==
  14702. + YAFFS_OBJECTID_LOSTNFOUND)) {
  14703. + /* We only load some info, don't fiddle with directory structure */
  14704. + in->valid = 1;
  14705. + in->variant_type = oh->type;
  14706. +
  14707. + in->yst_mode = oh->yst_mode;
  14708. + yaffs_load_attribs(in, oh);
  14709. + in->hdr_chunk = chunk;
  14710. + in->serial = tags.serial_number;
  14711. +
  14712. + } else if (in && !in->valid) {
  14713. + /* we need to load this info */
  14714. +
  14715. + in->valid = 1;
  14716. + in->variant_type = oh->type;
  14717. +
  14718. + in->yst_mode = oh->yst_mode;
  14719. + yaffs_load_attribs(in, oh);
  14720. + in->hdr_chunk = chunk;
  14721. + in->serial = tags.serial_number;
  14722. +
  14723. + yaffs_set_obj_name_from_oh(in, oh);
  14724. + in->dirty = 0;
  14725. +
  14726. + /* directory stuff...
  14727. + * hook up to parent
  14728. + */
  14729. +
  14730. + parent =
  14731. + yaffs_find_or_create_by_number
  14732. + (dev, oh->parent_obj_id,
  14733. + YAFFS_OBJECT_TYPE_DIRECTORY);
  14734. + if (!parent)
  14735. + alloc_failed = 1;
  14736. + if (parent && parent->variant_type ==
  14737. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  14738. + /* Set up as a directory */
  14739. + parent->variant_type =
  14740. + YAFFS_OBJECT_TYPE_DIRECTORY;
  14741. + INIT_LIST_HEAD(&parent->
  14742. + variant.dir_variant.children);
  14743. + } else if (!parent
  14744. + || parent->variant_type !=
  14745. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  14746. + /* Hoosterman, another problem....
  14747. + * We're trying to use a non-directory as a directory
  14748. + */
  14749. +
  14750. + T(YAFFS_TRACE_ERROR,
  14751. + (TSTR
  14752. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  14753. + TENDSTR)));
  14754. + parent = dev->lost_n_found;
  14755. + }
  14756. +
  14757. + yaffs_add_obj_to_dir(parent, in);
  14758. +
  14759. + if (0 && (parent == dev->del_dir ||
  14760. + parent ==
  14761. + dev->unlinked_dir)) {
  14762. + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
  14763. + dev->n_deleted_files++;
  14764. + }
  14765. + /* Note re hardlinks.
  14766. + * Since we might scan a hardlink before its equivalent object is scanned
  14767. + * we put them all in a list.
  14768. + * After scanning is complete, we should have all the objects, so we run through this
  14769. + * list and fix up all the chains.
  14770. + */
  14771. +
  14772. + switch (in->variant_type) {
  14773. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  14774. + /* Todo got a problem */
  14775. + break;
  14776. + case YAFFS_OBJECT_TYPE_FILE:
  14777. + if (dev->param.
  14778. + use_header_file_size)
  14779. +
  14780. + in->variant.
  14781. + file_variant.file_size
  14782. + = oh->file_size;
  14783. +
  14784. + break;
  14785. + case YAFFS_OBJECT_TYPE_HARDLINK:
  14786. + in->variant.
  14787. + hardlink_variant.equiv_id =
  14788. + oh->equiv_id;
  14789. + in->hard_links.next =
  14790. + (struct list_head *)
  14791. + hard_list;
  14792. + hard_list = in;
  14793. + break;
  14794. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  14795. + /* Do nothing */
  14796. + break;
  14797. + case YAFFS_OBJECT_TYPE_SPECIAL:
  14798. + /* Do nothing */
  14799. + break;
  14800. + case YAFFS_OBJECT_TYPE_SYMLINK:
  14801. + in->variant.symlink_variant.
  14802. + alias =
  14803. + yaffs_clone_str(oh->alias);
  14804. + if (!in->variant.
  14805. + symlink_variant.alias)
  14806. + alloc_failed = 1;
  14807. + break;
  14808. + }
  14809. +
  14810. + }
  14811. + }
  14812. + }
  14813. +
  14814. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  14815. + /* If we got this far while scanning, then the block is fully allocated. */
  14816. + state = YAFFS_BLOCK_STATE_FULL;
  14817. + }
  14818. +
  14819. + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  14820. + /* If the block was partially allocated then treat it as fully allocated. */
  14821. + state = YAFFS_BLOCK_STATE_FULL;
  14822. + dev->alloc_block = -1;
  14823. + }
  14824. +
  14825. + bi->block_state = state;
  14826. +
  14827. + /* Now let's see if it was dirty */
  14828. + if (bi->pages_in_use == 0 &&
  14829. + !bi->has_shrink_hdr &&
  14830. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  14831. + yaffs_block_became_dirty(dev, blk);
  14832. + }
  14833. +
  14834. + }
  14835. +
  14836. + /* Ok, we've done all the scanning.
  14837. + * Fix up the hard link chains.
  14838. + * We should now have scanned all the objects, now it's time to add these
  14839. + * hardlinks.
  14840. + */
  14841. +
  14842. + yaffs_link_fixup(dev, hard_list);
  14843. +
  14844. + /* Fix up any shadowed objects */
  14845. + {
  14846. + struct yaffs_shadow_fixer *fixer;
  14847. + struct yaffs_obj *obj;
  14848. +
  14849. + while (shadow_fixers) {
  14850. + fixer = shadow_fixers;
  14851. + shadow_fixers = fixer->next;
  14852. + /* Complete the rename transaction by deleting the shadowed object
  14853. + * then setting the object header to unshadowed.
  14854. + */
  14855. + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  14856. + if (obj)
  14857. + yaffs_del_obj(obj);
  14858. +
  14859. + obj = yaffs_find_by_number(dev, fixer->obj_id);
  14860. +
  14861. + if (obj)
  14862. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  14863. +
  14864. + YFREE(fixer);
  14865. + }
  14866. + }
  14867. +
  14868. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  14869. +
  14870. + if (alloc_failed)
  14871. + return YAFFS_FAIL;
  14872. +
  14873. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
  14874. +
  14875. + return YAFFS_OK;
  14876. +}
  14877. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.h linux-2.6.36/fs/yaffs2/yaffs_yaffs1.h
  14878. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs1.h 1970-01-01 01:00:00.000000000 +0100
  14879. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs1.h 2011-01-10 19:29:29.000000000 +0100
  14880. @@ -0,0 +1,22 @@
  14881. +/*
  14882. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14883. + *
  14884. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14885. + * for Toby Churchill Ltd and Brightstar Engineering
  14886. + *
  14887. + * Created by Charles Manning <charles@aleph1.co.uk>
  14888. + *
  14889. + * This program is free software; you can redistribute it and/or modify
  14890. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14891. + * published by the Free Software Foundation.
  14892. + *
  14893. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14894. + */
  14895. +
  14896. +#ifndef __YAFFS_YAFFS1_H__
  14897. +#define __YAFFS_YAFFS1_H__
  14898. +
  14899. +#include "yaffs_guts.h"
  14900. +int yaffs1_scan(struct yaffs_dev *dev);
  14901. +
  14902. +#endif
  14903. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.c linux-2.6.36/fs/yaffs2/yaffs_yaffs2.c
  14904. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.c 1970-01-01 01:00:00.000000000 +0100
  14905. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs2.c 2011-01-10 19:29:29.000000000 +0100
  14906. @@ -0,0 +1,1620 @@
  14907. +/*
  14908. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14909. + *
  14910. + * Copyright (C) 2002-2010 Aleph One Ltd.
  14911. + * for Toby Churchill Ltd and Brightstar Engineering
  14912. + *
  14913. + * Created by Charles Manning <charles@aleph1.co.uk>
  14914. + *
  14915. + * This program is free software; you can redistribute it and/or modify
  14916. + * it under the terms of the GNU General Public License version 2 as
  14917. + * published by the Free Software Foundation.
  14918. + */
  14919. +
  14920. +#include "yaffs_guts.h"
  14921. +#include "yaffs_trace.h"
  14922. +#include "yaffs_yaffs2.h"
  14923. +#include "yaffs_checkptrw.h"
  14924. +#include "yaffs_bitmap.h"
  14925. +#include "yaffs_nand.h"
  14926. +#include "yaffs_getblockinfo.h"
  14927. +#include "yaffs_verify.h"
  14928. +#include "yaffs_attribs.h"
  14929. +
  14930. +/*
  14931. + * Checkpoints are really no benefit on very small partitions.
  14932. + *
  14933. + * To save space on small partitions don't bother with checkpoints unless
  14934. + * the partition is at least this big.
  14935. + */
  14936. +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
  14937. +
  14938. +#define YAFFS_SMALL_HOLE_THRESHOLD 4
  14939. +
  14940. +/*
  14941. + * Oldest Dirty Sequence Number handling.
  14942. + */
  14943. +
  14944. +/* yaffs_calc_oldest_dirty_seq()
  14945. + * yaffs2_find_oldest_dirty_seq()
  14946. + * Calculate the oldest dirty sequence number if we don't know it.
  14947. + */
  14948. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
  14949. +{
  14950. + int i;
  14951. + unsigned seq;
  14952. + unsigned block_no = 0;
  14953. + struct yaffs_block_info *b;
  14954. +
  14955. + if (!dev->param.is_yaffs2)
  14956. + return;
  14957. +
  14958. + /* Find the oldest dirty sequence number. */
  14959. + seq = dev->seq_number + 1;
  14960. + b = dev->block_info;
  14961. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  14962. + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
  14963. + (b->pages_in_use - b->soft_del_pages) <
  14964. + dev->param.chunks_per_block && b->seq_number < seq) {
  14965. + seq = b->seq_number;
  14966. + block_no = i;
  14967. + }
  14968. + b++;
  14969. + }
  14970. +
  14971. + if (block_no) {
  14972. + dev->oldest_dirty_seq = seq;
  14973. + dev->oldest_dirty_block = block_no;
  14974. + }
  14975. +
  14976. +}
  14977. +
  14978. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
  14979. +{
  14980. + if (!dev->param.is_yaffs2)
  14981. + return;
  14982. +
  14983. + if (!dev->oldest_dirty_seq)
  14984. + yaffs_calc_oldest_dirty_seq(dev);
  14985. +}
  14986. +
  14987. +/*
  14988. + * yaffs_clear_oldest_dirty_seq()
  14989. + * Called when a block is erased or marked bad. (ie. when its seq_number
  14990. + * becomes invalid). If the value matches the oldest then we clear
  14991. + * dev->oldest_dirty_seq to force its recomputation.
  14992. + */
  14993. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  14994. + struct yaffs_block_info *bi)
  14995. +{
  14996. +
  14997. + if (!dev->param.is_yaffs2)
  14998. + return;
  14999. +
  15000. + if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
  15001. + dev->oldest_dirty_seq = 0;
  15002. + dev->oldest_dirty_block = 0;
  15003. + }
  15004. +}
  15005. +
  15006. +/*
  15007. + * yaffs2_update_oldest_dirty_seq()
  15008. + * Update the oldest dirty sequence number whenever we dirty a block.
  15009. + * Only do this if the oldest_dirty_seq is actually being tracked.
  15010. + */
  15011. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  15012. + struct yaffs_block_info *bi)
  15013. +{
  15014. + if (!dev->param.is_yaffs2)
  15015. + return;
  15016. +
  15017. + if (dev->oldest_dirty_seq) {
  15018. + if (dev->oldest_dirty_seq > bi->seq_number) {
  15019. + dev->oldest_dirty_seq = bi->seq_number;
  15020. + dev->oldest_dirty_block = block_no;
  15021. + }
  15022. + }
  15023. +}
  15024. +
  15025. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
  15026. +{
  15027. +
  15028. + if (!dev->param.is_yaffs2)
  15029. + return 1; /* disqualification only applies to yaffs2. */
  15030. +
  15031. + if (!bi->has_shrink_hdr)
  15032. + return 1; /* can gc */
  15033. +
  15034. + yaffs2_find_oldest_dirty_seq(dev);
  15035. +
  15036. + /* Can't do gc of this block if there are any blocks older than this one that have
  15037. + * discarded pages.
  15038. + */
  15039. + return (bi->seq_number <= dev->oldest_dirty_seq);
  15040. +}
  15041. +
  15042. +/*
  15043. + * yaffs2_find_refresh_block()
  15044. + * periodically finds the oldest full block by sequence number for refreshing.
  15045. + * Only for yaffs2.
  15046. + */
  15047. +u32 yaffs2_find_refresh_block(struct yaffs_dev * dev)
  15048. +{
  15049. + u32 b;
  15050. +
  15051. + u32 oldest = 0;
  15052. + u32 oldest_seq = 0;
  15053. +
  15054. + struct yaffs_block_info *bi;
  15055. +
  15056. + if (!dev->param.is_yaffs2)
  15057. + return oldest;
  15058. +
  15059. + /*
  15060. + * If refresh period < 10 then refreshing is disabled.
  15061. + */
  15062. + if (dev->param.refresh_period < 10)
  15063. + return oldest;
  15064. +
  15065. + /*
  15066. + * Fix broken values.
  15067. + */
  15068. + if (dev->refresh_skip > dev->param.refresh_period)
  15069. + dev->refresh_skip = dev->param.refresh_period;
  15070. +
  15071. + if (dev->refresh_skip > 0)
  15072. + return oldest;
  15073. +
  15074. + /*
  15075. + * Refresh skip is now zero.
  15076. + * We'll do a refresh this time around....
  15077. + * Update the refresh skip and find the oldest block.
  15078. + */
  15079. + dev->refresh_skip = dev->param.refresh_period;
  15080. + dev->refresh_count++;
  15081. + bi = dev->block_info;
  15082. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  15083. +
  15084. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  15085. +
  15086. + if (oldest < 1 || bi->seq_number < oldest_seq) {
  15087. + oldest = b;
  15088. + oldest_seq = bi->seq_number;
  15089. + }
  15090. + }
  15091. + bi++;
  15092. + }
  15093. +
  15094. + if (oldest > 0) {
  15095. + T(YAFFS_TRACE_GC,
  15096. + (TSTR
  15097. + ("GC refresh count %d selected block %d with seq_number %d"
  15098. + TENDSTR), dev->refresh_count, oldest, oldest_seq));
  15099. + }
  15100. +
  15101. + return oldest;
  15102. +}
  15103. +
  15104. +int yaffs2_checkpt_required(struct yaffs_dev *dev)
  15105. +{
  15106. + int nblocks;
  15107. +
  15108. + if (!dev->param.is_yaffs2)
  15109. + return 0;
  15110. +
  15111. + nblocks = dev->internal_end_block - dev->internal_start_block + 1;
  15112. +
  15113. + return !dev->param.skip_checkpt_wr &&
  15114. + !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
  15115. +}
  15116. +
  15117. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
  15118. +{
  15119. + int retval;
  15120. +
  15121. + if (!dev->param.is_yaffs2)
  15122. + return 0;
  15123. +
  15124. + if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
  15125. + /* Not a valid value so recalculate */
  15126. + int n_bytes = 0;
  15127. + int n_blocks;
  15128. + int dev_blocks =
  15129. + (dev->param.end_block - dev->param.start_block + 1);
  15130. +
  15131. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15132. + n_bytes += sizeof(struct yaffs_checkpt_dev);
  15133. + n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
  15134. + n_bytes += dev_blocks * dev->chunk_bit_stride;
  15135. + n_bytes +=
  15136. + (sizeof(struct yaffs_checkpt_obj) +
  15137. + sizeof(u32)) * (dev->n_obj);
  15138. + n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes);
  15139. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15140. + n_bytes += sizeof(u32); /* checksum */
  15141. +
  15142. + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
  15143. +
  15144. + n_blocks =
  15145. + (n_bytes /
  15146. + (dev->data_bytes_per_chunk *
  15147. + dev->param.chunks_per_block)) + 3;
  15148. +
  15149. + dev->checkpoint_blocks_required = n_blocks;
  15150. + }
  15151. +
  15152. + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
  15153. + if (retval < 0)
  15154. + retval = 0;
  15155. + return retval;
  15156. +}
  15157. +
  15158. +/*--------------------- Checkpointing --------------------*/
  15159. +
  15160. +static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15161. +{
  15162. + struct yaffs_checkpt_validity cp;
  15163. +
  15164. + memset(&cp, 0, sizeof(cp));
  15165. +
  15166. + cp.struct_type = sizeof(cp);
  15167. + cp.magic = YAFFS_MAGIC;
  15168. + cp.version = YAFFS_CHECKPOINT_VERSION;
  15169. + cp.head = (head) ? 1 : 0;
  15170. +
  15171. + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
  15172. +}
  15173. +
  15174. +static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15175. +{
  15176. + struct yaffs_checkpt_validity cp;
  15177. + int ok;
  15178. +
  15179. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15180. +
  15181. + if (ok)
  15182. + ok = (cp.struct_type == sizeof(cp)) &&
  15183. + (cp.magic == YAFFS_MAGIC) &&
  15184. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  15185. + (cp.head == ((head) ? 1 : 0));
  15186. + return ok ? 1 : 0;
  15187. +}
  15188. +
  15189. +static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
  15190. + struct yaffs_dev *dev)
  15191. +{
  15192. + cp->n_erased_blocks = dev->n_erased_blocks;
  15193. + cp->alloc_block = dev->alloc_block;
  15194. + cp->alloc_page = dev->alloc_page;
  15195. + cp->n_free_chunks = dev->n_free_chunks;
  15196. +
  15197. + cp->n_deleted_files = dev->n_deleted_files;
  15198. + cp->n_unlinked_files = dev->n_unlinked_files;
  15199. + cp->n_bg_deletions = dev->n_bg_deletions;
  15200. + cp->seq_number = dev->seq_number;
  15201. +
  15202. +}
  15203. +
  15204. +static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
  15205. + struct yaffs_checkpt_dev *cp)
  15206. +{
  15207. + dev->n_erased_blocks = cp->n_erased_blocks;
  15208. + dev->alloc_block = cp->alloc_block;
  15209. + dev->alloc_page = cp->alloc_page;
  15210. + dev->n_free_chunks = cp->n_free_chunks;
  15211. +
  15212. + dev->n_deleted_files = cp->n_deleted_files;
  15213. + dev->n_unlinked_files = cp->n_unlinked_files;
  15214. + dev->n_bg_deletions = cp->n_bg_deletions;
  15215. + dev->seq_number = cp->seq_number;
  15216. +}
  15217. +
  15218. +static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
  15219. +{
  15220. + struct yaffs_checkpt_dev cp;
  15221. + u32 n_bytes;
  15222. + u32 n_blocks =
  15223. + (dev->internal_end_block - dev->internal_start_block + 1);
  15224. +
  15225. + int ok;
  15226. +
  15227. + /* Write device runtime values */
  15228. + yaffs2_dev_to_checkpt_dev(&cp, dev);
  15229. + cp.struct_type = sizeof(cp);
  15230. +
  15231. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15232. +
  15233. + /* Write block info */
  15234. + if (ok) {
  15235. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15236. + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
  15237. + n_bytes);
  15238. + }
  15239. +
  15240. + /* Write chunk bits */
  15241. + if (ok) {
  15242. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15243. + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
  15244. + n_bytes);
  15245. + }
  15246. + return ok ? 1 : 0;
  15247. +
  15248. +}
  15249. +
  15250. +static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
  15251. +{
  15252. + struct yaffs_checkpt_dev cp;
  15253. + u32 n_bytes;
  15254. + u32 n_blocks =
  15255. + (dev->internal_end_block - dev->internal_start_block + 1);
  15256. +
  15257. + int ok;
  15258. +
  15259. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15260. + if (!ok)
  15261. + return 0;
  15262. +
  15263. + if (cp.struct_type != sizeof(cp))
  15264. + return 0;
  15265. +
  15266. + yaffs_checkpt_dev_to_dev(dev, &cp);
  15267. +
  15268. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15269. +
  15270. + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
  15271. +
  15272. + if (!ok)
  15273. + return 0;
  15274. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15275. +
  15276. + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15277. +
  15278. + return ok ? 1 : 0;
  15279. +}
  15280. +
  15281. +static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
  15282. + struct yaffs_obj *obj)
  15283. +{
  15284. +
  15285. + cp->obj_id = obj->obj_id;
  15286. + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
  15287. + cp->hdr_chunk = obj->hdr_chunk;
  15288. + cp->variant_type = obj->variant_type;
  15289. + cp->deleted = obj->deleted;
  15290. + cp->soft_del = obj->soft_del;
  15291. + cp->unlinked = obj->unlinked;
  15292. + cp->fake = obj->fake;
  15293. + cp->rename_allowed = obj->rename_allowed;
  15294. + cp->unlink_allowed = obj->unlink_allowed;
  15295. + cp->serial = obj->serial;
  15296. + cp->n_data_chunks = obj->n_data_chunks;
  15297. +
  15298. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15299. + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
  15300. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15301. + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
  15302. +}
  15303. +
  15304. +static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
  15305. + struct yaffs_checkpt_obj *cp)
  15306. +{
  15307. +
  15308. + struct yaffs_obj *parent;
  15309. +
  15310. + if (obj->variant_type != cp->variant_type) {
  15311. + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
  15312. + TCONT
  15313. + ("chunk %d does not match existing object type %d")
  15314. + TENDSTR), cp->obj_id,
  15315. + cp->variant_type, cp->hdr_chunk,
  15316. + obj->variant_type));
  15317. + return 0;
  15318. + }
  15319. +
  15320. + obj->obj_id = cp->obj_id;
  15321. +
  15322. + if (cp->parent_id)
  15323. + parent = yaffs_find_or_create_by_number(obj->my_dev,
  15324. + cp->parent_id,
  15325. + YAFFS_OBJECT_TYPE_DIRECTORY);
  15326. + else
  15327. + parent = NULL;
  15328. +
  15329. + if (parent) {
  15330. + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  15331. + T(YAFFS_TRACE_ALWAYS,
  15332. + (TSTR
  15333. + ("Checkpoint read object %d parent %d type %d"
  15334. + TCONT(" chunk %d Parent type, %d, not directory")
  15335. + TENDSTR), cp->obj_id, cp->parent_id,
  15336. + cp->variant_type, cp->hdr_chunk,
  15337. + parent->variant_type));
  15338. + return 0;
  15339. + }
  15340. + yaffs_add_obj_to_dir(parent, obj);
  15341. + }
  15342. +
  15343. + obj->hdr_chunk = cp->hdr_chunk;
  15344. + obj->variant_type = cp->variant_type;
  15345. + obj->deleted = cp->deleted;
  15346. + obj->soft_del = cp->soft_del;
  15347. + obj->unlinked = cp->unlinked;
  15348. + obj->fake = cp->fake;
  15349. + obj->rename_allowed = cp->rename_allowed;
  15350. + obj->unlink_allowed = cp->unlink_allowed;
  15351. + obj->serial = cp->serial;
  15352. + obj->n_data_chunks = cp->n_data_chunks;
  15353. +
  15354. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15355. + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
  15356. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15357. + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
  15358. +
  15359. + if (obj->hdr_chunk > 0)
  15360. + obj->lazy_loaded = 1;
  15361. + return 1;
  15362. +}
  15363. +
  15364. +static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
  15365. + struct yaffs_tnode *tn, u32 level,
  15366. + int chunk_offset)
  15367. +{
  15368. + int i;
  15369. + struct yaffs_dev *dev = in->my_dev;
  15370. + int ok = 1;
  15371. +
  15372. + if (tn) {
  15373. + if (level > 0) {
  15374. +
  15375. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  15376. + if (tn->internal[i]) {
  15377. + ok = yaffs2_checkpt_tnode_worker(in,
  15378. + tn->
  15379. + internal
  15380. + [i],
  15381. + level -
  15382. + 1,
  15383. + (chunk_offset
  15384. + <<
  15385. + YAFFS_TNODES_INTERNAL_BITS)
  15386. + + i);
  15387. + }
  15388. + }
  15389. + } else if (level == 0) {
  15390. + u32 base_offset =
  15391. + chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
  15392. + ok = (yaffs2_checkpt_wr
  15393. + (dev, &base_offset,
  15394. + sizeof(base_offset)) == sizeof(base_offset));
  15395. + if (ok)
  15396. + ok = (yaffs2_checkpt_wr
  15397. + (dev, tn,
  15398. + dev->tnode_size) == dev->tnode_size);
  15399. + }
  15400. + }
  15401. +
  15402. + return ok;
  15403. +
  15404. +}
  15405. +
  15406. +static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
  15407. +{
  15408. + u32 end_marker = ~0;
  15409. + int ok = 1;
  15410. +
  15411. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  15412. + ok = yaffs2_checkpt_tnode_worker(obj,
  15413. + obj->variant.file_variant.top,
  15414. + obj->variant.file_variant.
  15415. + top_level, 0);
  15416. + if (ok)
  15417. + ok = (yaffs2_checkpt_wr
  15418. + (obj->my_dev, &end_marker,
  15419. + sizeof(end_marker)) == sizeof(end_marker));
  15420. + }
  15421. +
  15422. + return ok ? 1 : 0;
  15423. +}
  15424. +
  15425. +static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
  15426. +{
  15427. + u32 base_chunk;
  15428. + int ok = 1;
  15429. + struct yaffs_dev *dev = obj->my_dev;
  15430. + struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
  15431. + struct yaffs_tnode *tn;
  15432. + int nread = 0;
  15433. +
  15434. + ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
  15435. + sizeof(base_chunk));
  15436. +
  15437. + while (ok && (~base_chunk)) {
  15438. + nread++;
  15439. + /* Read level 0 tnode */
  15440. +
  15441. + tn = yaffs_get_tnode(dev);
  15442. + if (tn) {
  15443. + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
  15444. + dev->tnode_size);
  15445. + } else {
  15446. + ok = 0;
  15447. + }
  15448. +
  15449. + if (tn && ok)
  15450. + ok = yaffs_add_find_tnode_0(dev,
  15451. + file_stuct_ptr,
  15452. + base_chunk, tn) ? 1 : 0;
  15453. +
  15454. + if (ok)
  15455. + ok = (yaffs2_checkpt_rd
  15456. + (dev, &base_chunk,
  15457. + sizeof(base_chunk)) == sizeof(base_chunk));
  15458. +
  15459. + }
  15460. +
  15461. + T(YAFFS_TRACE_CHECKPOINT,
  15462. + (TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
  15463. + nread, base_chunk, ok));
  15464. +
  15465. + return ok ? 1 : 0;
  15466. +}
  15467. +
  15468. +static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
  15469. +{
  15470. + struct yaffs_obj *obj;
  15471. + struct yaffs_checkpt_obj cp;
  15472. + int i;
  15473. + int ok = 1;
  15474. + struct list_head *lh;
  15475. +
  15476. + /* Iterate through the objects in each hash entry,
  15477. + * dumping them to the checkpointing stream.
  15478. + */
  15479. +
  15480. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  15481. + list_for_each(lh, &dev->obj_bucket[i].list) {
  15482. + if (lh) {
  15483. + obj =
  15484. + list_entry(lh, struct yaffs_obj, hash_link);
  15485. + if (!obj->defered_free) {
  15486. + yaffs2_obj_checkpt_obj(&cp, obj);
  15487. + cp.struct_type = sizeof(cp);
  15488. +
  15489. + T(YAFFS_TRACE_CHECKPOINT,
  15490. + (TSTR
  15491. + ("Checkpoint write object %d parent %d type %d chunk %d obj addr %p"
  15492. + TENDSTR), cp.obj_id, cp.parent_id,
  15493. + cp.variant_type, cp.hdr_chunk, obj));
  15494. +
  15495. + ok = (yaffs2_checkpt_wr
  15496. + (dev, &cp,
  15497. + sizeof(cp)) == sizeof(cp));
  15498. +
  15499. + if (ok
  15500. + && obj->variant_type ==
  15501. + YAFFS_OBJECT_TYPE_FILE)
  15502. + ok = yaffs2_wr_checkpt_tnodes
  15503. + (obj);
  15504. + }
  15505. + }
  15506. + }
  15507. + }
  15508. +
  15509. + /* Dump end of list */
  15510. + memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
  15511. + cp.struct_type = sizeof(cp);
  15512. +
  15513. + if (ok)
  15514. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15515. +
  15516. + return ok ? 1 : 0;
  15517. +}
  15518. +
  15519. +static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
  15520. +{
  15521. + struct yaffs_obj *obj;
  15522. + struct yaffs_checkpt_obj cp;
  15523. + int ok = 1;
  15524. + int done = 0;
  15525. + struct yaffs_obj *hard_list = NULL;
  15526. +
  15527. + while (ok && !done) {
  15528. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15529. + if (cp.struct_type != sizeof(cp)) {
  15530. + T(YAFFS_TRACE_CHECKPOINT,
  15531. + (TSTR("struct size %d instead of %d ok %d" TENDSTR),
  15532. + cp.struct_type, (int)sizeof(cp), ok));
  15533. + ok = 0;
  15534. + }
  15535. +
  15536. + T(YAFFS_TRACE_CHECKPOINT,
  15537. + (TSTR
  15538. + ("Checkpoint read object %d parent %d type %d chunk %d "
  15539. + TENDSTR), cp.obj_id, cp.parent_id, cp.variant_type,
  15540. + cp.hdr_chunk));
  15541. +
  15542. + if (ok && cp.obj_id == ~0) {
  15543. + done = 1;
  15544. + } else if (ok) {
  15545. + obj =
  15546. + yaffs_find_or_create_by_number(dev, cp.obj_id,
  15547. + cp.variant_type);
  15548. + if (obj) {
  15549. + ok = taffs2_checkpt_obj_to_obj(obj, &cp);
  15550. + if (!ok)
  15551. + break;
  15552. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  15553. + ok = yaffs2_rd_checkpt_tnodes(obj);
  15554. + } else if (obj->variant_type ==
  15555. + YAFFS_OBJECT_TYPE_HARDLINK) {
  15556. + obj->hard_links.next =
  15557. + (struct list_head *)hard_list;
  15558. + hard_list = obj;
  15559. + }
  15560. + } else {
  15561. + ok = 0;
  15562. + }
  15563. + }
  15564. + }
  15565. +
  15566. + if (ok)
  15567. + yaffs_link_fixup(dev, hard_list);
  15568. +
  15569. + return ok ? 1 : 0;
  15570. +}
  15571. +
  15572. +static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
  15573. +{
  15574. + u32 checkpt_sum;
  15575. + int ok;
  15576. +
  15577. + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
  15578. +
  15579. + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
  15580. + sizeof(checkpt_sum));
  15581. +
  15582. + if (!ok)
  15583. + return 0;
  15584. +
  15585. + return 1;
  15586. +}
  15587. +
  15588. +static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
  15589. +{
  15590. + u32 checkpt_sum0;
  15591. + u32 checkpt_sum1;
  15592. + int ok;
  15593. +
  15594. + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
  15595. +
  15596. + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
  15597. + sizeof(checkpt_sum1));
  15598. +
  15599. + if (!ok)
  15600. + return 0;
  15601. +
  15602. + if (checkpt_sum0 != checkpt_sum1)
  15603. + return 0;
  15604. +
  15605. + return 1;
  15606. +}
  15607. +
  15608. +static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
  15609. +{
  15610. + int ok = 1;
  15611. +
  15612. + if (!yaffs2_checkpt_required(dev)) {
  15613. + T(YAFFS_TRACE_CHECKPOINT,
  15614. + (TSTR("skipping checkpoint write" TENDSTR)));
  15615. + ok = 0;
  15616. + }
  15617. +
  15618. + if (ok)
  15619. + ok = yaffs2_checkpt_open(dev, 1);
  15620. +
  15621. + if (ok) {
  15622. + T(YAFFS_TRACE_CHECKPOINT,
  15623. + (TSTR("write checkpoint validity" TENDSTR)));
  15624. + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
  15625. + }
  15626. + if (ok) {
  15627. + T(YAFFS_TRACE_CHECKPOINT,
  15628. + (TSTR("write checkpoint device" TENDSTR)));
  15629. + ok = yaffs2_wr_checkpt_dev(dev);
  15630. + }
  15631. + if (ok) {
  15632. + T(YAFFS_TRACE_CHECKPOINT,
  15633. + (TSTR("write checkpoint objects" TENDSTR)));
  15634. + ok = yaffs2_wr_checkpt_objs(dev);
  15635. + }
  15636. + if (ok) {
  15637. + T(YAFFS_TRACE_CHECKPOINT,
  15638. + (TSTR("write checkpoint validity" TENDSTR)));
  15639. + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
  15640. + }
  15641. +
  15642. + if (ok)
  15643. + ok = yaffs2_wr_checkpt_sum(dev);
  15644. +
  15645. + if (!yaffs_checkpt_close(dev))
  15646. + ok = 0;
  15647. +
  15648. + if (ok)
  15649. + dev->is_checkpointed = 1;
  15650. + else
  15651. + dev->is_checkpointed = 0;
  15652. +
  15653. + return dev->is_checkpointed;
  15654. +}
  15655. +
  15656. +static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
  15657. +{
  15658. + int ok = 1;
  15659. +
  15660. + if (!dev->param.is_yaffs2)
  15661. + ok = 0;
  15662. +
  15663. + if (ok && dev->param.skip_checkpt_rd) {
  15664. + T(YAFFS_TRACE_CHECKPOINT,
  15665. + (TSTR("skipping checkpoint read" TENDSTR)));
  15666. + ok = 0;
  15667. + }
  15668. +
  15669. + if (ok)
  15670. + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
  15671. +
  15672. + if (ok) {
  15673. + T(YAFFS_TRACE_CHECKPOINT,
  15674. + (TSTR("read checkpoint validity" TENDSTR)));
  15675. + ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
  15676. + }
  15677. + if (ok) {
  15678. + T(YAFFS_TRACE_CHECKPOINT,
  15679. + (TSTR("read checkpoint device" TENDSTR)));
  15680. + ok = yaffs2_rd_checkpt_dev(dev);
  15681. + }
  15682. + if (ok) {
  15683. + T(YAFFS_TRACE_CHECKPOINT,
  15684. + (TSTR("read checkpoint objects" TENDSTR)));
  15685. + ok = yaffs2_rd_checkpt_objs(dev);
  15686. + }
  15687. + if (ok) {
  15688. + T(YAFFS_TRACE_CHECKPOINT,
  15689. + (TSTR("read checkpoint validity" TENDSTR)));
  15690. + ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
  15691. + }
  15692. +
  15693. + if (ok) {
  15694. + ok = yaffs2_rd_checkpt_sum(dev);
  15695. + T(YAFFS_TRACE_CHECKPOINT,
  15696. + (TSTR("read checkpoint checksum %d" TENDSTR), ok));
  15697. + }
  15698. +
  15699. + if (!yaffs_checkpt_close(dev))
  15700. + ok = 0;
  15701. +
  15702. + if (ok)
  15703. + dev->is_checkpointed = 1;
  15704. + else
  15705. + dev->is_checkpointed = 0;
  15706. +
  15707. + return ok ? 1 : 0;
  15708. +
  15709. +}
  15710. +
  15711. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
  15712. +{
  15713. + if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
  15714. + dev->is_checkpointed = 0;
  15715. + yaffs2_checkpt_invalidate_stream(dev);
  15716. + }
  15717. + if (dev->param.sb_dirty_fn)
  15718. + dev->param.sb_dirty_fn(dev);
  15719. +}
  15720. +
  15721. +int yaffs_checkpoint_save(struct yaffs_dev *dev)
  15722. +{
  15723. +
  15724. + T(YAFFS_TRACE_CHECKPOINT,
  15725. + (TSTR("save entry: is_checkpointed %d" TENDSTR),
  15726. + dev->is_checkpointed));
  15727. +
  15728. + yaffs_verify_objects(dev);
  15729. + yaffs_verify_blocks(dev);
  15730. + yaffs_verify_free_chunks(dev);
  15731. +
  15732. + if (!dev->is_checkpointed) {
  15733. + yaffs2_checkpt_invalidate(dev);
  15734. + yaffs2_wr_checkpt_data(dev);
  15735. + }
  15736. +
  15737. + T(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  15738. + (TSTR("save exit: is_checkpointed %d" TENDSTR),
  15739. + dev->is_checkpointed));
  15740. +
  15741. + return dev->is_checkpointed;
  15742. +}
  15743. +
  15744. +int yaffs2_checkpt_restore(struct yaffs_dev *dev)
  15745. +{
  15746. + int retval;
  15747. + T(YAFFS_TRACE_CHECKPOINT,
  15748. + (TSTR("restore entry: is_checkpointed %d" TENDSTR),
  15749. + dev->is_checkpointed));
  15750. +
  15751. + retval = yaffs2_rd_checkpt_data(dev);
  15752. +
  15753. + if (dev->is_checkpointed) {
  15754. + yaffs_verify_objects(dev);
  15755. + yaffs_verify_blocks(dev);
  15756. + yaffs_verify_free_chunks(dev);
  15757. + }
  15758. +
  15759. + T(YAFFS_TRACE_CHECKPOINT,
  15760. + (TSTR("restore exit: is_checkpointed %d" TENDSTR),
  15761. + dev->is_checkpointed));
  15762. +
  15763. + return retval;
  15764. +}
  15765. +
  15766. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
  15767. +{
  15768. + /* if new_size > old_file_size.
  15769. + * We're going to be writing a hole.
  15770. + * If the hole is small then write zeros otherwise write a start of hole marker.
  15771. + */
  15772. +
  15773. + loff_t old_file_size;
  15774. + int increase;
  15775. + int small_hole;
  15776. + int result = YAFFS_OK;
  15777. + struct yaffs_dev *dev = NULL;
  15778. +
  15779. + u8 *local_buffer = NULL;
  15780. +
  15781. + int small_increase_ok = 0;
  15782. +
  15783. + if (!obj)
  15784. + return YAFFS_FAIL;
  15785. +
  15786. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15787. + return YAFFS_FAIL;
  15788. +
  15789. + dev = obj->my_dev;
  15790. +
  15791. + /* Bail out if not yaffs2 mode */
  15792. + if (!dev->param.is_yaffs2)
  15793. + return YAFFS_OK;
  15794. +
  15795. + old_file_size = obj->variant.file_variant.file_size;
  15796. +
  15797. + if (new_size <= old_file_size)
  15798. + return YAFFS_OK;
  15799. +
  15800. + increase = new_size - old_file_size;
  15801. +
  15802. + if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
  15803. + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
  15804. + small_hole = 1;
  15805. + else
  15806. + small_hole = 0;
  15807. +
  15808. + if (small_hole)
  15809. + local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
  15810. +
  15811. + if (local_buffer) {
  15812. + /* fill hole with zero bytes */
  15813. + int pos = old_file_size;
  15814. + int this_write;
  15815. + int written;
  15816. + memset(local_buffer, 0, dev->data_bytes_per_chunk);
  15817. + small_increase_ok = 1;
  15818. +
  15819. + while (increase > 0 && small_increase_ok) {
  15820. + this_write = increase;
  15821. + if (this_write > dev->data_bytes_per_chunk)
  15822. + this_write = dev->data_bytes_per_chunk;
  15823. + written =
  15824. + yaffs_do_file_wr(obj, local_buffer, pos, this_write,
  15825. + 0);
  15826. + if (written == this_write) {
  15827. + pos += this_write;
  15828. + increase -= this_write;
  15829. + } else {
  15830. + small_increase_ok = 0;
  15831. + }
  15832. + }
  15833. +
  15834. + yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
  15835. +
  15836. + /* If we were out of space then reverse any chunks we've added */
  15837. + if (!small_increase_ok)
  15838. + yaffs_resize_file_down(obj, old_file_size);
  15839. + }
  15840. +
  15841. + if (!small_increase_ok &&
  15842. + obj->parent &&
  15843. + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  15844. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
  15845. + /* Write a hole start header with the old file size */
  15846. + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
  15847. + }
  15848. +
  15849. + return result;
  15850. +
  15851. +}
  15852. +
  15853. +struct yaffs_block_index {
  15854. + int seq;
  15855. + int block;
  15856. +};
  15857. +
  15858. +static int yaffs2_ybicmp(const void *a, const void *b)
  15859. +{
  15860. + int aseq = ((struct yaffs_block_index *)a)->seq;
  15861. + int bseq = ((struct yaffs_block_index *)b)->seq;
  15862. + int ablock = ((struct yaffs_block_index *)a)->block;
  15863. + int bblock = ((struct yaffs_block_index *)b)->block;
  15864. + if (aseq == bseq)
  15865. + return ablock - bblock;
  15866. + else
  15867. + return aseq - bseq;
  15868. +}
  15869. +
  15870. +int yaffs2_scan_backwards(struct yaffs_dev *dev)
  15871. +{
  15872. + struct yaffs_ext_tags tags;
  15873. + int blk;
  15874. + int block_iter;
  15875. + int start_iter;
  15876. + int end_iter;
  15877. + int n_to_scan = 0;
  15878. +
  15879. + int chunk;
  15880. + int result;
  15881. + int c;
  15882. + int deleted;
  15883. + enum yaffs_block_state state;
  15884. + struct yaffs_obj *hard_list = NULL;
  15885. + struct yaffs_block_info *bi;
  15886. + u32 seq_number;
  15887. + struct yaffs_obj_hdr *oh;
  15888. + struct yaffs_obj *in;
  15889. + struct yaffs_obj *parent;
  15890. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  15891. + int is_unlinked;
  15892. + u8 *chunk_data;
  15893. +
  15894. + int file_size;
  15895. + int is_shrink;
  15896. + int found_chunks;
  15897. + int equiv_id;
  15898. + int alloc_failed = 0;
  15899. +
  15900. + struct yaffs_block_index *block_index = NULL;
  15901. + int alt_block_index = 0;
  15902. +
  15903. + T(YAFFS_TRACE_SCAN,
  15904. + (TSTR
  15905. + ("yaffs2_scan_backwards starts intstartblk %d intendblk %d..."
  15906. + TENDSTR), dev->internal_start_block, dev->internal_end_block));
  15907. +
  15908. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  15909. +
  15910. + block_index = YMALLOC(n_blocks * sizeof(struct yaffs_block_index));
  15911. +
  15912. + if (!block_index) {
  15913. + block_index =
  15914. + YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_index));
  15915. + alt_block_index = 1;
  15916. + }
  15917. +
  15918. + if (!block_index) {
  15919. + T(YAFFS_TRACE_SCAN,
  15920. + (TSTR
  15921. + ("yaffs2_scan_backwards() could not allocate block index!"
  15922. + TENDSTR)));
  15923. + return YAFFS_FAIL;
  15924. + }
  15925. +
  15926. + dev->blocks_in_checkpt = 0;
  15927. +
  15928. + chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  15929. +
  15930. + /* Scan all the blocks to determine their state */
  15931. + bi = dev->block_info;
  15932. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  15933. + blk++) {
  15934. + yaffs_clear_chunk_bits(dev, blk);
  15935. + bi->pages_in_use = 0;
  15936. + bi->soft_del_pages = 0;
  15937. +
  15938. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  15939. +
  15940. + bi->block_state = state;
  15941. + bi->seq_number = seq_number;
  15942. +
  15943. + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  15944. + bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
  15945. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  15946. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  15947. +
  15948. + T(YAFFS_TRACE_SCAN_DEBUG,
  15949. + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
  15950. + state, seq_number));
  15951. +
  15952. + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  15953. + dev->blocks_in_checkpt++;
  15954. +
  15955. + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
  15956. + T(YAFFS_TRACE_BAD_BLOCKS,
  15957. + (TSTR("block %d is bad" TENDSTR), blk));
  15958. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  15959. + T(YAFFS_TRACE_SCAN_DEBUG,
  15960. + (TSTR("Block empty " TENDSTR)));
  15961. + dev->n_erased_blocks++;
  15962. + dev->n_free_chunks += dev->param.chunks_per_block;
  15963. + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  15964. +
  15965. + /* Determine the highest sequence number */
  15966. + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  15967. + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  15968. +
  15969. + block_index[n_to_scan].seq = seq_number;
  15970. + block_index[n_to_scan].block = blk;
  15971. +
  15972. + n_to_scan++;
  15973. +
  15974. + if (seq_number >= dev->seq_number)
  15975. + dev->seq_number = seq_number;
  15976. + } else {
  15977. + /* TODO: Nasty sequence number! */
  15978. + T(YAFFS_TRACE_SCAN,
  15979. + (TSTR
  15980. + ("Block scanning block %d has bad sequence number %d"
  15981. + TENDSTR), blk, seq_number));
  15982. +
  15983. + }
  15984. + }
  15985. + bi++;
  15986. + }
  15987. +
  15988. + T(YAFFS_TRACE_SCAN,
  15989. + (TSTR("%d blocks to be sorted..." TENDSTR), n_to_scan));
  15990. +
  15991. + YYIELD();
  15992. +
  15993. + /* Sort the blocks by sequence number */
  15994. + yaffs_sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  15995. + yaffs2_ybicmp);
  15996. +
  15997. + YYIELD();
  15998. +
  15999. + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
  16000. +
  16001. + /* Now scan the blocks looking at the data. */
  16002. + start_iter = 0;
  16003. + end_iter = n_to_scan - 1;
  16004. + T(YAFFS_TRACE_SCAN_DEBUG,
  16005. + (TSTR("%d blocks to be scanned" TENDSTR), n_to_scan));
  16006. +
  16007. + /* For each block.... backwards */
  16008. + for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
  16009. + block_iter--) {
  16010. + /* Cooperative multitasking! This loop can run for so
  16011. + long that watchdog timers expire. */
  16012. + YYIELD();
  16013. +
  16014. + /* get the block to scan in the correct order */
  16015. + blk = block_index[block_iter].block;
  16016. +
  16017. + bi = yaffs_get_block_info(dev, blk);
  16018. +
  16019. + state = bi->block_state;
  16020. +
  16021. + deleted = 0;
  16022. +
  16023. + /* For each chunk in each block that needs scanning.... */
  16024. + found_chunks = 0;
  16025. + for (c = dev->param.chunks_per_block - 1;
  16026. + !alloc_failed && c >= 0 &&
  16027. + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  16028. + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
  16029. + /* Scan backwards...
  16030. + * Read the tags and decide what to do
  16031. + */
  16032. +
  16033. + chunk = blk * dev->param.chunks_per_block + c;
  16034. +
  16035. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  16036. + &tags);
  16037. +
  16038. + /* Let's have a good look at this chunk... */
  16039. +
  16040. + if (!tags.chunk_used) {
  16041. + /* An unassigned chunk in the block.
  16042. + * If there are used chunks after this one, then
  16043. + * it is a chunk that was skipped due to failing the erased
  16044. + * check. Just skip it so that it can be deleted.
  16045. + * But, more typically, We get here when this is an unallocated
  16046. + * chunk and his means that either the block is empty or
  16047. + * this is the one being allocated from
  16048. + */
  16049. +
  16050. + if (found_chunks) {
  16051. + /* This is a chunk that was skipped due to failing the erased check */
  16052. + } else if (c == 0) {
  16053. + /* We're looking at the first chunk in the block so the block is unused */
  16054. + state = YAFFS_BLOCK_STATE_EMPTY;
  16055. + dev->n_erased_blocks++;
  16056. + } else {
  16057. + if (state ==
  16058. + YAFFS_BLOCK_STATE_NEEDS_SCANNING
  16059. + || state ==
  16060. + YAFFS_BLOCK_STATE_ALLOCATING) {
  16061. + if (dev->seq_number ==
  16062. + bi->seq_number) {
  16063. + /* this is the block being allocated from */
  16064. +
  16065. + T(YAFFS_TRACE_SCAN,
  16066. + (TSTR
  16067. + (" Allocating from %d %d"
  16068. + TENDSTR), blk, c));
  16069. +
  16070. + state =
  16071. + YAFFS_BLOCK_STATE_ALLOCATING;
  16072. + dev->alloc_block = blk;
  16073. + dev->alloc_page = c;
  16074. + dev->
  16075. + alloc_block_finder =
  16076. + blk;
  16077. + } else {
  16078. + /* This is a partially written block that is not
  16079. + * the current allocation block.
  16080. + */
  16081. +
  16082. + T(YAFFS_TRACE_SCAN,
  16083. + (TSTR
  16084. + ("Partially written block %d detected"
  16085. + TENDSTR), blk));
  16086. + }
  16087. + }
  16088. + }
  16089. +
  16090. + dev->n_free_chunks++;
  16091. +
  16092. + } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
  16093. + T(YAFFS_TRACE_SCAN,
  16094. + (TSTR
  16095. + (" Unfixed ECC in chunk(%d:%d), chunk ignored"
  16096. + TENDSTR), blk, c));
  16097. +
  16098. + dev->n_free_chunks++;
  16099. +
  16100. + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
  16101. + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
  16102. + (tags.chunk_id > 0
  16103. + && tags.n_bytes > dev->data_bytes_per_chunk)
  16104. + || tags.seq_number != bi->seq_number) {
  16105. + T(YAFFS_TRACE_SCAN,
  16106. + (TSTR
  16107. + ("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"
  16108. + TENDSTR), blk, c, tags.obj_id,
  16109. + tags.chunk_id, tags.n_bytes));
  16110. +
  16111. + dev->n_free_chunks++;
  16112. +
  16113. + } else if (tags.chunk_id > 0) {
  16114. + /* chunk_id > 0 so it is a data chunk... */
  16115. + unsigned int endpos;
  16116. + u32 chunk_base =
  16117. + (tags.chunk_id -
  16118. + 1) * dev->data_bytes_per_chunk;
  16119. +
  16120. + found_chunks = 1;
  16121. +
  16122. + yaffs_set_chunk_bit(dev, blk, c);
  16123. + bi->pages_in_use++;
  16124. +
  16125. + in = yaffs_find_or_create_by_number(dev,
  16126. + tags.obj_id,
  16127. + YAFFS_OBJECT_TYPE_FILE);
  16128. + if (!in) {
  16129. + /* Out of memory */
  16130. + alloc_failed = 1;
  16131. + }
  16132. +
  16133. + if (in &&
  16134. + in->variant_type == YAFFS_OBJECT_TYPE_FILE
  16135. + && chunk_base <
  16136. + in->variant.file_variant.shrink_size) {
  16137. + /* This has not been invalidated by a resize */
  16138. + if (!yaffs_put_chunk_in_file
  16139. + (in, tags.chunk_id, chunk, -1)) {
  16140. + alloc_failed = 1;
  16141. + }
  16142. +
  16143. + /* File size is calculated by looking at the data chunks if we have not
  16144. + * seen an object header yet. Stop this practice once we find an object header.
  16145. + */
  16146. + endpos = chunk_base + tags.n_bytes;
  16147. +
  16148. + if (!in->valid && /* have not got an object header yet */
  16149. + in->variant.file_variant.
  16150. + scanned_size < endpos) {
  16151. + in->variant.file_variant.
  16152. + scanned_size = endpos;
  16153. + in->variant.file_variant.
  16154. + file_size = endpos;
  16155. + }
  16156. +
  16157. + } else if (in) {
  16158. + /* This chunk has been invalidated by a resize, or a past file deletion
  16159. + * so delete the chunk*/
  16160. + yaffs_chunk_del(dev, chunk, 1,
  16161. + __LINE__);
  16162. +
  16163. + }
  16164. + } else {
  16165. + /* chunk_id == 0, so it is an ObjectHeader.
  16166. + * Thus, we read in the object header and make the object
  16167. + */
  16168. + found_chunks = 1;
  16169. +
  16170. + yaffs_set_chunk_bit(dev, blk, c);
  16171. + bi->pages_in_use++;
  16172. +
  16173. + oh = NULL;
  16174. + in = NULL;
  16175. +
  16176. + if (tags.extra_available) {
  16177. + in = yaffs_find_or_create_by_number(dev,
  16178. + tags.
  16179. + obj_id,
  16180. + tags.
  16181. + extra_obj_type);
  16182. + if (!in)
  16183. + alloc_failed = 1;
  16184. + }
  16185. +
  16186. + if (!in ||
  16187. + (!in->valid && dev->param.disable_lazy_load)
  16188. + || tags.extra_shadows || (!in->valid
  16189. + && (tags.obj_id ==
  16190. + YAFFS_OBJECTID_ROOT
  16191. + || tags.
  16192. + obj_id ==
  16193. + YAFFS_OBJECTID_LOSTNFOUND)))
  16194. + {
  16195. +
  16196. + /* If we don't have valid info then we need to read the chunk
  16197. + * TODO In future we can probably defer reading the chunk and
  16198. + * living with invalid data until needed.
  16199. + */
  16200. +
  16201. + result = yaffs_rd_chunk_tags_nand(dev,
  16202. + chunk,
  16203. + chunk_data,
  16204. + NULL);
  16205. +
  16206. + oh = (struct yaffs_obj_hdr *)chunk_data;
  16207. +
  16208. + if (dev->param.inband_tags) {
  16209. + /* Fix up the header if they got corrupted by inband tags */
  16210. + oh->shadows_obj =
  16211. + oh->inband_shadowed_obj_id;
  16212. + oh->is_shrink =
  16213. + oh->inband_is_shrink;
  16214. + }
  16215. +
  16216. + if (!in) {
  16217. + in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
  16218. + if (!in)
  16219. + alloc_failed = 1;
  16220. + }
  16221. +
  16222. + }
  16223. +
  16224. + if (!in) {
  16225. + /* TODO Hoosterman we have a problem! */
  16226. + T(YAFFS_TRACE_ERROR,
  16227. + (TSTR
  16228. + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
  16229. + TENDSTR), tags.obj_id, chunk));
  16230. + continue;
  16231. + }
  16232. +
  16233. + if (in->valid) {
  16234. + /* We have already filled this one.
  16235. + * We have a duplicate that will be discarded, but
  16236. + * we first have to suck out resize info if it is a file.
  16237. + */
  16238. +
  16239. + if ((in->variant_type ==
  16240. + YAFFS_OBJECT_TYPE_FILE) && ((oh
  16241. + &&
  16242. + oh->
  16243. + type
  16244. + ==
  16245. + YAFFS_OBJECT_TYPE_FILE)
  16246. + ||
  16247. + (tags.
  16248. + extra_available
  16249. + &&
  16250. + tags.
  16251. + extra_obj_type
  16252. + ==
  16253. + YAFFS_OBJECT_TYPE_FILE)))
  16254. + {
  16255. + u32 this_size =
  16256. + (oh) ? oh->
  16257. + file_size :
  16258. + tags.extra_length;
  16259. + u32 parent_obj_id =
  16260. + (oh) ? oh->parent_obj_id :
  16261. + tags.extra_parent_id;
  16262. +
  16263. + is_shrink =
  16264. + (oh) ? oh->
  16265. + is_shrink :
  16266. + tags.extra_is_shrink;
  16267. +
  16268. + /* If it is deleted (unlinked at start also means deleted)
  16269. + * we treat the file size as being zeroed at this point.
  16270. + */
  16271. + if (parent_obj_id ==
  16272. + YAFFS_OBJECTID_DELETED
  16273. + || parent_obj_id ==
  16274. + YAFFS_OBJECTID_UNLINKED) {
  16275. + this_size = 0;
  16276. + is_shrink = 1;
  16277. + }
  16278. +
  16279. + if (is_shrink
  16280. + && in->variant.file_variant.
  16281. + shrink_size > this_size)
  16282. + in->variant.
  16283. + file_variant.
  16284. + shrink_size =
  16285. + this_size;
  16286. +
  16287. + if (is_shrink)
  16288. + bi->has_shrink_hdr = 1;
  16289. +
  16290. + }
  16291. + /* Use existing - destroy this one. */
  16292. + yaffs_chunk_del(dev, chunk, 1,
  16293. + __LINE__);
  16294. +
  16295. + }
  16296. +
  16297. + if (!in->valid && in->variant_type !=
  16298. + (oh ? oh->type : tags.extra_obj_type))
  16299. + T(YAFFS_TRACE_ERROR,
  16300. + (TSTR
  16301. + ("yaffs tragedy: Bad object type, "
  16302. + TCONT
  16303. + ("%d != %d, for object %d at chunk ")
  16304. + TCONT("%d during scan")
  16305. + TENDSTR), oh ?
  16306. + oh->type : tags.extra_obj_type,
  16307. + in->variant_type, tags.obj_id,
  16308. + chunk));
  16309. +
  16310. + if (!in->valid &&
  16311. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  16312. + tags.obj_id ==
  16313. + YAFFS_OBJECTID_LOSTNFOUND)) {
  16314. + /* We only load some info, don't fiddle with directory structure */
  16315. + in->valid = 1;
  16316. +
  16317. + if (oh) {
  16318. +
  16319. + in->yst_mode = oh->yst_mode;
  16320. + yaffs_load_attribs(in, oh);
  16321. + in->lazy_loaded = 0;
  16322. + } else {
  16323. + in->lazy_loaded = 1;
  16324. + }
  16325. + in->hdr_chunk = chunk;
  16326. +
  16327. + } else if (!in->valid) {
  16328. + /* we need to load this info */
  16329. +
  16330. + in->valid = 1;
  16331. + in->hdr_chunk = chunk;
  16332. +
  16333. + if (oh) {
  16334. + in->variant_type = oh->type;
  16335. +
  16336. + in->yst_mode = oh->yst_mode;
  16337. + yaffs_load_attribs(in, oh);
  16338. +
  16339. + if (oh->shadows_obj > 0)
  16340. + yaffs_handle_shadowed_obj
  16341. + (dev,
  16342. + oh->shadows_obj,
  16343. + 1);
  16344. +
  16345. + yaffs_set_obj_name_from_oh(in,
  16346. + oh);
  16347. + parent =
  16348. + yaffs_find_or_create_by_number
  16349. + (dev, oh->parent_obj_id,
  16350. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16351. +
  16352. + file_size = oh->file_size;
  16353. + is_shrink = oh->is_shrink;
  16354. + equiv_id = oh->equiv_id;
  16355. +
  16356. + } else {
  16357. + in->variant_type =
  16358. + tags.extra_obj_type;
  16359. + parent =
  16360. + yaffs_find_or_create_by_number
  16361. + (dev, tags.extra_parent_id,
  16362. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16363. + file_size = tags.extra_length;
  16364. + is_shrink =
  16365. + tags.extra_is_shrink;
  16366. + equiv_id = tags.extra_equiv_id;
  16367. + in->lazy_loaded = 1;
  16368. +
  16369. + }
  16370. + in->dirty = 0;
  16371. +
  16372. + if (!parent)
  16373. + alloc_failed = 1;
  16374. +
  16375. + /* directory stuff...
  16376. + * hook up to parent
  16377. + */
  16378. +
  16379. + if (parent && parent->variant_type ==
  16380. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  16381. + /* Set up as a directory */
  16382. + parent->variant_type =
  16383. + YAFFS_OBJECT_TYPE_DIRECTORY;
  16384. + INIT_LIST_HEAD(&parent->
  16385. + variant.dir_variant.children);
  16386. + } else if (!parent
  16387. + || parent->variant_type !=
  16388. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  16389. + /* Hoosterman, another problem....
  16390. + * We're trying to use a non-directory as a directory
  16391. + */
  16392. +
  16393. + T(YAFFS_TRACE_ERROR,
  16394. + (TSTR
  16395. + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  16396. + TENDSTR)));
  16397. + parent = dev->lost_n_found;
  16398. + }
  16399. +
  16400. + yaffs_add_obj_to_dir(parent, in);
  16401. +
  16402. + is_unlinked = (parent == dev->del_dir)
  16403. + || (parent == dev->unlinked_dir);
  16404. +
  16405. + if (is_shrink) {
  16406. + /* Mark the block as having a shrink header */
  16407. + bi->has_shrink_hdr = 1;
  16408. + }
  16409. +
  16410. + /* Note re hardlinks.
  16411. + * Since we might scan a hardlink before its equivalent object is scanned
  16412. + * we put them all in a list.
  16413. + * After scanning is complete, we should have all the objects, so we run
  16414. + * through this list and fix up all the chains.
  16415. + */
  16416. +
  16417. + switch (in->variant_type) {
  16418. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  16419. + /* Todo got a problem */
  16420. + break;
  16421. + case YAFFS_OBJECT_TYPE_FILE:
  16422. +
  16423. + if (in->variant.
  16424. + file_variant.scanned_size <
  16425. + file_size) {
  16426. + /* This covers the case where the file size is greater
  16427. + * than where the data is
  16428. + * This will happen if the file is resized to be larger
  16429. + * than its current data extents.
  16430. + */
  16431. + in->variant.
  16432. + file_variant.
  16433. + file_size =
  16434. + file_size;
  16435. + in->variant.
  16436. + file_variant.
  16437. + scanned_size =
  16438. + file_size;
  16439. + }
  16440. +
  16441. + if (in->variant.file_variant.
  16442. + shrink_size > file_size)
  16443. + in->variant.
  16444. + file_variant.
  16445. + shrink_size =
  16446. + file_size;
  16447. +
  16448. + break;
  16449. + case YAFFS_OBJECT_TYPE_HARDLINK:
  16450. + if (!is_unlinked) {
  16451. + in->variant.
  16452. + hardlink_variant.
  16453. + equiv_id = equiv_id;
  16454. + in->hard_links.next =
  16455. + (struct list_head *)
  16456. + hard_list;
  16457. + hard_list = in;
  16458. + }
  16459. + break;
  16460. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  16461. + /* Do nothing */
  16462. + break;
  16463. + case YAFFS_OBJECT_TYPE_SPECIAL:
  16464. + /* Do nothing */
  16465. + break;
  16466. + case YAFFS_OBJECT_TYPE_SYMLINK:
  16467. + if (oh) {
  16468. + in->variant.
  16469. + symlink_variant.
  16470. + alias =
  16471. + yaffs_clone_str(oh->
  16472. + alias);
  16473. + if (!in->variant.
  16474. + symlink_variant.
  16475. + alias)
  16476. + alloc_failed =
  16477. + 1;
  16478. + }
  16479. + break;
  16480. + }
  16481. +
  16482. + }
  16483. +
  16484. + }
  16485. +
  16486. + } /* End of scanning for each chunk */
  16487. +
  16488. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  16489. + /* If we got this far while scanning, then the block is fully allocated. */
  16490. + state = YAFFS_BLOCK_STATE_FULL;
  16491. + }
  16492. +
  16493. + bi->block_state = state;
  16494. +
  16495. + /* Now let's see if it was dirty */
  16496. + if (bi->pages_in_use == 0 &&
  16497. + !bi->has_shrink_hdr &&
  16498. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  16499. + yaffs_block_became_dirty(dev, blk);
  16500. + }
  16501. +
  16502. + }
  16503. +
  16504. + yaffs_skip_rest_of_block(dev);
  16505. +
  16506. + if (alt_block_index)
  16507. + YFREE_ALT(block_index);
  16508. + else
  16509. + YFREE(block_index);
  16510. +
  16511. + /* Ok, we've done all the scanning.
  16512. + * Fix up the hard link chains.
  16513. + * We should now have scanned all the objects, now it's time to add these
  16514. + * hardlinks.
  16515. + */
  16516. + yaffs_link_fixup(dev, hard_list);
  16517. +
  16518. + yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  16519. +
  16520. + if (alloc_failed)
  16521. + return YAFFS_FAIL;
  16522. +
  16523. + T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR)));
  16524. +
  16525. + return YAFFS_OK;
  16526. +}
  16527. diff -Nur linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.h linux-2.6.36/fs/yaffs2/yaffs_yaffs2.h
  16528. --- linux-2.6.36.orig/fs/yaffs2/yaffs_yaffs2.h 1970-01-01 01:00:00.000000000 +0100
  16529. +++ linux-2.6.36/fs/yaffs2/yaffs_yaffs2.h 2011-01-10 19:29:29.000000000 +0100
  16530. @@ -0,0 +1,39 @@
  16531. +/*
  16532. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16533. + *
  16534. + * Copyright (C) 2002-2010 Aleph One Ltd.
  16535. + * for Toby Churchill Ltd and Brightstar Engineering
  16536. + *
  16537. + * Created by Charles Manning <charles@aleph1.co.uk>
  16538. + *
  16539. + * This program is free software; you can redistribute it and/or modify
  16540. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16541. + * published by the Free Software Foundation.
  16542. + *
  16543. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16544. + */
  16545. +
  16546. +#ifndef __YAFFS_YAFFS2_H__
  16547. +#define __YAFFS_YAFFS2_H__
  16548. +
  16549. +#include "yaffs_guts.h"
  16550. +
  16551. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
  16552. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
  16553. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  16554. + struct yaffs_block_info *bi);
  16555. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  16556. + struct yaffs_block_info *bi);
  16557. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
  16558. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
  16559. +int yaffs2_checkpt_required(struct yaffs_dev *dev);
  16560. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
  16561. +
  16562. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
  16563. +int yaffs2_checkpt_save(struct yaffs_dev *dev);
  16564. +int yaffs2_checkpt_restore(struct yaffs_dev *dev);
  16565. +
  16566. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
  16567. +int yaffs2_scan_backwards(struct yaffs_dev *dev);
  16568. +
  16569. +#endif
  16570. diff -Nur linux-2.6.36.orig/fs/yaffs2/yportenv.h linux-2.6.36/fs/yaffs2/yportenv.h
  16571. --- linux-2.6.36.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  16572. +++ linux-2.6.36/fs/yaffs2/yportenv.h 2011-01-10 19:29:29.000000000 +0100
  16573. @@ -0,0 +1,339 @@
  16574. +/*
  16575. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16576. + *
  16577. + * Copyright (C) 2002-2010 Aleph One Ltd.
  16578. + * for Toby Churchill Ltd and Brightstar Engineering
  16579. + *
  16580. + * Created by Charles Manning <charles@aleph1.co.uk>
  16581. + *
  16582. + * This program is free software; you can redistribute it and/or modify
  16583. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16584. + * published by the Free Software Foundation.
  16585. + *
  16586. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16587. + */
  16588. +
  16589. +#ifndef __YPORTENV_H__
  16590. +#define __YPORTENV_H__
  16591. +
  16592. +/*
  16593. + * Define the MTD version in terms of Linux Kernel versions
  16594. + * This allows yaffs to be used independantly of the kernel
  16595. + * as well as with it.
  16596. + */
  16597. +
  16598. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  16599. +
  16600. +#if defined CONFIG_YAFFS_WINCE
  16601. +
  16602. +#include "ywinceenv.h"
  16603. +
  16604. +#elif defined __KERNEL__
  16605. +
  16606. +#include "moduleconfig.h"
  16607. +
  16608. +/* Linux kernel */
  16609. +
  16610. +#include <linux/version.h>
  16611. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  16612. +
  16613. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  16614. +#include <linux/config.h>
  16615. +#endif
  16616. +#include <linux/version.h>
  16617. +#include <linux/kernel.h>
  16618. +#include <linux/mm.h>
  16619. +#include <linux/sched.h>
  16620. +#include <linux/string.h>
  16621. +#include <linux/slab.h>
  16622. +#include <linux/vmalloc.h>
  16623. +#include <linux/xattr.h>
  16624. +#include <linux/list.h>
  16625. +#include <linux/types.h>
  16626. +#include <linux/fs.h>
  16627. +#include <linux/stat.h>
  16628. +#include <linux/sort.h>
  16629. +
  16630. +#define YCHAR char
  16631. +#define YUCHAR unsigned char
  16632. +#define _Y(x) x
  16633. +#define yaffs_strcat(a, b) strcat(a, b)
  16634. +#define yaffs_strcpy(a, b) strcpy(a, b)
  16635. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  16636. +#define yaffs_strncmp(a, b, c) strncmp(a, b, c)
  16637. +#define yaffs_strnlen(s,m) strnlen(s,m)
  16638. +#define yaffs_sprintf sprintf
  16639. +#define yaffs_toupper(a) toupper(a)
  16640. +#define yaffs_sort(base, n, sz, cmp_fn) sort(base, n, sz, cmp_fn, NULL)
  16641. +
  16642. +#define Y_INLINE __inline__
  16643. +
  16644. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16645. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16646. +
  16647. +/* #define YPRINTF(x) printk x */
  16648. +#define YMALLOC(x) kmalloc(x, GFP_NOFS)
  16649. +#define YFREE(x) kfree(x)
  16650. +#define YMALLOC_ALT(x) vmalloc(x)
  16651. +#define YFREE_ALT(x) vfree(x)
  16652. +#define YMALLOC_DMA(x) YMALLOC(x)
  16653. +
  16654. +#define YYIELD() schedule()
  16655. +#define Y_DUMP_STACK() dump_stack()
  16656. +
  16657. +#define YAFFS_ROOT_MODE 0755
  16658. +#define YAFFS_LOSTNFOUND_MODE 0700
  16659. +
  16660. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  16661. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  16662. +#define Y_TIME_CONVERT(x) (x).tv_sec
  16663. +#else
  16664. +#define Y_CURRENT_TIME CURRENT_TIME
  16665. +#define Y_TIME_CONVERT(x) (x)
  16666. +#endif
  16667. +
  16668. +#define yaffs_sum_cmp(x, y) ((x) == (y))
  16669. +#define yaffs_strcmp(a, b) strcmp(a, b)
  16670. +
  16671. +#define TENDSTR "\n"
  16672. +#define TSTR(x) KERN_DEBUG x
  16673. +#define TCONT(x) x
  16674. +#define TOUT(p) printk p
  16675. +
  16676. +#define compile_time_assertion(assertion) \
  16677. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  16678. +
  16679. +#elif defined CONFIG_YAFFS_DIRECT
  16680. +
  16681. +#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22)
  16682. +
  16683. +/* Direct interface */
  16684. +#include "ydirectenv.h"
  16685. +
  16686. +#elif defined CONFIG_YAFFS_UTIL
  16687. +
  16688. +/* Stuff for YAFFS utilities */
  16689. +
  16690. +#include "stdlib.h"
  16691. +#include "stdio.h"
  16692. +#include "string.h"
  16693. +
  16694. +#define YMALLOC(x) malloc(x)
  16695. +#define YFREE(x) free(x)
  16696. +#define YMALLOC_ALT(x) malloc(x)
  16697. +#define YFREE_ALT(x) free(x)
  16698. +
  16699. +#define YCHAR char
  16700. +#define YUCHAR unsigned char
  16701. +#define _Y(x) x
  16702. +#define yaffs_strcat(a, b) strcat(a, b)
  16703. +#define yaffs_strcpy(a, b) strcpy(a, b)
  16704. +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
  16705. +#define yaffs_strnlen(s,m) strnlen(s,m)
  16706. +#define yaffs_sprintf sprintf
  16707. +#define yaffs_toupper(a) toupper(a)
  16708. +
  16709. +#define Y_INLINE inline
  16710. +
  16711. +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
  16712. +/* #define YALERT(s) YINFO(s) */
  16713. +
  16714. +#define TENDSTR "\n"
  16715. +#define TSTR(x) x
  16716. +#define TOUT(p) printf p
  16717. +
  16718. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16719. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16720. +/* #define YPRINTF(x) printf x */
  16721. +
  16722. +#define YAFFS_ROOT_MODE 0755
  16723. +#define YAFFS_LOSTNFOUND_MODE 0700
  16724. +
  16725. +#define yaffs_sum_cmp(x, y) ((x) == (y))
  16726. +#define yaffs_strcmp(a, b) strcmp(a, b)
  16727. +
  16728. +#else
  16729. +/* Should have specified a configuration type */
  16730. +#error Unknown configuration
  16731. +
  16732. +#endif
  16733. +
  16734. +#if defined(CONFIG_YAFFS_DIRECT) || defined(CONFIG_YAFFS_WINCE)
  16735. +
  16736. +#ifdef CONFIG_YAFFSFS_PROVIDE_VALUES
  16737. +
  16738. +#ifndef O_RDONLY
  16739. +#define O_RDONLY 00
  16740. +#endif
  16741. +
  16742. +#ifndef O_WRONLY
  16743. +#define O_WRONLY 01
  16744. +#endif
  16745. +
  16746. +#ifndef O_RDWR
  16747. +#define O_RDWR 02
  16748. +#endif
  16749. +
  16750. +#ifndef O_CREAT
  16751. +#define O_CREAT 0100
  16752. +#endif
  16753. +
  16754. +#ifndef O_EXCL
  16755. +#define O_EXCL 0200
  16756. +#endif
  16757. +
  16758. +#ifndef O_TRUNC
  16759. +#define O_TRUNC 01000
  16760. +#endif
  16761. +
  16762. +#ifndef O_APPEND
  16763. +#define O_APPEND 02000
  16764. +#endif
  16765. +
  16766. +#ifndef SEEK_SET
  16767. +#define SEEK_SET 0
  16768. +#endif
  16769. +
  16770. +#ifndef SEEK_CUR
  16771. +#define SEEK_CUR 1
  16772. +#endif
  16773. +
  16774. +#ifndef SEEK_END
  16775. +#define SEEK_END 2
  16776. +#endif
  16777. +
  16778. +#ifndef EBUSY
  16779. +#define EBUSY 16
  16780. +#endif
  16781. +
  16782. +#ifndef ENODEV
  16783. +#define ENODEV 19
  16784. +#endif
  16785. +
  16786. +#ifndef EINVAL
  16787. +#define EINVAL 22
  16788. +#endif
  16789. +
  16790. +#ifndef ENFILE
  16791. +#define ENFILE 23
  16792. +#endif
  16793. +
  16794. +#ifndef EBADF
  16795. +#define EBADF 9
  16796. +#endif
  16797. +
  16798. +#ifndef EACCES
  16799. +#define EACCES 13
  16800. +#endif
  16801. +
  16802. +#ifndef EXDEV
  16803. +#define EXDEV 18
  16804. +#endif
  16805. +
  16806. +#ifndef ENOENT
  16807. +#define ENOENT 2
  16808. +#endif
  16809. +
  16810. +#ifndef ENOSPC
  16811. +#define ENOSPC 28
  16812. +#endif
  16813. +
  16814. +#ifndef ERANGE
  16815. +#define ERANGE 34
  16816. +#endif
  16817. +
  16818. +#ifndef ENODATA
  16819. +#define ENODATA 61
  16820. +#endif
  16821. +
  16822. +#ifndef ENOTEMPTY
  16823. +#define ENOTEMPTY 39
  16824. +#endif
  16825. +
  16826. +#ifndef ENAMETOOLONG
  16827. +#define ENAMETOOLONG 36
  16828. +#endif
  16829. +
  16830. +#ifndef ENOMEM
  16831. +#define ENOMEM 12
  16832. +#endif
  16833. +
  16834. +#ifndef EEXIST
  16835. +#define EEXIST 17
  16836. +#endif
  16837. +
  16838. +#ifndef ENOTDIR
  16839. +#define ENOTDIR 20
  16840. +#endif
  16841. +
  16842. +#ifndef EISDIR
  16843. +#define EISDIR 21
  16844. +#endif
  16845. +
  16846. +// Mode flags
  16847. +
  16848. +#ifndef S_IFMT
  16849. +#define S_IFMT 0170000
  16850. +#endif
  16851. +
  16852. +#ifndef S_IFLNK
  16853. +#define S_IFLNK 0120000
  16854. +#endif
  16855. +
  16856. +#ifndef S_IFDIR
  16857. +#define S_IFDIR 0040000
  16858. +#endif
  16859. +
  16860. +#ifndef S_IFREG
  16861. +#define S_IFREG 0100000
  16862. +#endif
  16863. +
  16864. +#ifndef S_IREAD
  16865. +#define S_IREAD 0000400
  16866. +#endif
  16867. +
  16868. +#ifndef S_IWRITE
  16869. +#define S_IWRITE 0000200
  16870. +#endif
  16871. +
  16872. +#ifndef S_IEXEC
  16873. +#define S_IEXEC 0000100
  16874. +#endif
  16875. +
  16876. +#ifndef XATTR_CREATE
  16877. +#define XATTR_CREATE 1
  16878. +#endif
  16879. +
  16880. +#ifndef XATTR_REPLACE
  16881. +#define XATTR_REPLACE 2
  16882. +#endif
  16883. +
  16884. +#ifndef R_OK
  16885. +#define R_OK 4
  16886. +#define W_OK 2
  16887. +#define X_OK 1
  16888. +#define F_OK 0
  16889. +#endif
  16890. +
  16891. +#else
  16892. +#include <errno.h>
  16893. +#include <sys/stat.h>
  16894. +#include <fcntl.h>
  16895. +#endif
  16896. +
  16897. +#endif
  16898. +
  16899. +#ifndef Y_DUMP_STACK
  16900. +#define Y_DUMP_STACK() do { } while (0)
  16901. +#endif
  16902. +
  16903. +#ifndef YBUG
  16904. +#define YBUG() do {\
  16905. + T(YAFFS_TRACE_BUG,\
  16906. + (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),\
  16907. + __LINE__));\
  16908. + Y_DUMP_STACK();\
  16909. +} while (0)
  16910. +#endif
  16911. +
  16912. +#endif