yaffs2.patch 432 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551
  1. diff -Nur linux-3.15-rc5.orig/fs/Kconfig linux-3.15-rc5/fs/Kconfig
  2. --- linux-3.15-rc5.orig/fs/Kconfig 2014-05-09 22:10:52.000000000 +0200
  3. +++ linux-3.15-rc5/fs/Kconfig 2014-05-17 01:53:17.000000000 +0200
  4. @@ -190,6 +190,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-3.15-rc5.orig/fs/Makefile linux-3.15-rc5/fs/Makefile
  13. --- linux-3.15-rc5.orig/fs/Makefile 2014-05-09 22:10:52.000000000 +0200
  14. +++ linux-3.15-rc5/fs/Makefile 2014-05-17 01:53:25.000000000 +0200
  15. @@ -126,3 +126,4 @@
  16. obj-$(CONFIG_CEPH_FS) += ceph/
  17. obj-$(CONFIG_PSTORE) += pstore/
  18. obj-$(CONFIG_EFIVAR_FS) += efivarfs/
  19. +obj-$(CONFIG_YAFFS_FS) += yaffs2/
  20. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/Kconfig linux-3.15-rc5/fs/yaffs2/Kconfig
  21. --- linux-3.15-rc5.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  22. +++ linux-3.15-rc5/fs/yaffs2/Kconfig 2014-05-17 01:53:27.000000000 +0200
  23. @@ -0,0 +1,171 @@
  24. +#
  25. +# yaffs file system configurations
  26. +#
  27. +
  28. +config YAFFS_FS
  29. + tristate "yaffs2 file system support"
  30. + default n
  31. + depends on MTD_BLOCK
  32. + select YAFFS_YAFFS1
  33. + select YAFFS_YAFFS2
  34. + help
  35. + yaffs2, or Yet Another Flash File System, is a file system
  36. + optimised for NAND Flash chips.
  37. +
  38. + To compile the yaffs2 file system support as a module, choose M
  39. + here: the module will be called yaffs2.
  40. +
  41. + If unsure, say N.
  42. +
  43. + Further information on yaffs2 is available at
  44. + <http://www.aleph1.co.uk/yaffs/>.
  45. +
  46. +config YAFFS_YAFFS1
  47. + bool "512 byte / page devices"
  48. + depends on YAFFS_FS
  49. + default y
  50. + help
  51. + Enable yaffs1 support -- yaffs for 512 byte / page devices
  52. +
  53. + Not needed for 2K-page devices.
  54. +
  55. + If unsure, say Y.
  56. +
  57. +config YAFFS_9BYTE_TAGS
  58. + bool "Use older-style on-NAND data format with pageStatus byte"
  59. + depends on YAFFS_YAFFS1
  60. + default n
  61. + help
  62. +
  63. + Older-style on-NAND data format has a "pageStatus" byte to record
  64. + chunk/page state. This byte is zero when the page is discarded.
  65. + Choose this option if you have existing on-NAND data using this
  66. + format that you need to continue to support. New data written
  67. + also uses the older-style format. Note: Use of this option
  68. + generally requires that MTD's oob layout be adjusted to use the
  69. + older-style format. See notes on tags formats and MTD versions
  70. + in yaffs_mtdif1.c.
  71. +
  72. + If unsure, say N.
  73. +
  74. +config YAFFS_DOES_ECC
  75. + bool "Lets yaffs do its own ECC"
  76. + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
  77. + default n
  78. + help
  79. + This enables yaffs to use its own ECC functions instead of using
  80. + the ones from the generic MTD-NAND driver.
  81. +
  82. + If unsure, say N.
  83. +
  84. +config YAFFS_ECC_WRONG_ORDER
  85. + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
  86. + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
  87. + default n
  88. + help
  89. + This makes yaffs_ecc.c use the same ecc byte order as Steven
  90. + Hill's nand_ecc.c. If not set, then you get the same ecc byte
  91. + order as SmartMedia.
  92. +
  93. + If unsure, say N.
  94. +
  95. +config YAFFS_YAFFS2
  96. + bool "2048 byte (or larger) / page devices"
  97. + depends on YAFFS_FS
  98. + default y
  99. + help
  100. + Enable yaffs2 support -- yaffs for >= 2K bytes per page devices
  101. +
  102. + If unsure, say Y.
  103. +
  104. +config YAFFS_AUTO_YAFFS2
  105. + bool "Autoselect yaffs2 format"
  106. + depends on YAFFS_YAFFS2
  107. + default y
  108. + help
  109. + Without this, you need to explicitely use yaffs2 as the file
  110. + system type. With this, you can say "yaffs" and yaffs or yaffs2
  111. + will be used depending on the device page size (yaffs on
  112. + 512-byte page devices, yaffs2 on 2K page devices).
  113. +
  114. + If unsure, say Y.
  115. +
  116. +config YAFFS_DISABLE_TAGS_ECC
  117. + bool "Disable yaffs from doing ECC on tags by default"
  118. + depends on YAFFS_FS && YAFFS_YAFFS2
  119. + default n
  120. + help
  121. + This defaults yaffs to using its own ECC calculations on tags instead of
  122. + just relying on the MTD.
  123. + This behavior can also be overridden with tags_ecc_on and
  124. + tags_ecc_off mount options.
  125. +
  126. + If unsure, say N.
  127. +
  128. +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
  129. + bool "Force chunk erase check"
  130. + depends on YAFFS_FS
  131. + default n
  132. + help
  133. + Normally yaffs only checks chunks before writing until an erased
  134. + chunk is found. This helps to detect any partially written
  135. + chunks that might have happened due to power loss.
  136. +
  137. + Enabling this forces on the test that chunks are erased in flash
  138. + before writing to them. This takes more time but is potentially
  139. + a bit more secure.
  140. +
  141. + Suggest setting Y during development and ironing out driver
  142. + issues etc. Suggest setting to N if you want faster writing.
  143. +
  144. + If unsure, say Y.
  145. +
  146. +config YAFFS_EMPTY_LOST_AND_FOUND
  147. + bool "Empty lost and found on boot"
  148. + depends on YAFFS_FS
  149. + default n
  150. + help
  151. + If this is enabled then the contents of lost and found is
  152. + automatically dumped at mount.
  153. +
  154. + If unsure, say N.
  155. +
  156. +config YAFFS_DISABLE_BLOCK_REFRESHING
  157. + bool "Disable yaffs2 block refreshing"
  158. + depends on YAFFS_FS
  159. + default n
  160. + help
  161. + If this is set, then block refreshing is disabled.
  162. + Block refreshing infrequently refreshes the oldest block in
  163. + a yaffs2 file system. This mechanism helps to refresh flash to
  164. + mitigate against data loss. This is particularly useful for MLC.
  165. +
  166. + If unsure, say N.
  167. +
  168. +config YAFFS_DISABLE_BACKGROUND
  169. + bool "Disable yaffs2 background processing"
  170. + depends on YAFFS_FS
  171. + default n
  172. + help
  173. + If this is set, then background processing is disabled.
  174. + Background processing makes many foreground activities faster.
  175. +
  176. + If unsure, say N.
  177. +
  178. +config YAFFS_DISABLE_BAD_BLOCK_MARKING
  179. + bool "Disable yaffs2 bad block marking"
  180. + depends on YAFFS_FS
  181. + default n
  182. + help
  183. + Useful during early flash bring up to prevent problems causing
  184. + lots of bad block marking.
  185. +
  186. + If unsure, say N.
  187. +
  188. +config YAFFS_XATTR
  189. + bool "Enable yaffs2 xattr support"
  190. + depends on YAFFS_FS
  191. + default y
  192. + help
  193. + If this is set then yaffs2 will provide xattr support.
  194. + If unsure, say Y.
  195. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/Makefile linux-3.15-rc5/fs/yaffs2/Makefile
  196. --- linux-3.15-rc5.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  197. +++ linux-3.15-rc5/fs/yaffs2/Makefile 2014-05-17 01:53:27.000000000 +0200
  198. @@ -0,0 +1,18 @@
  199. +#
  200. +# Makefile for the linux YAFFS filesystem routines.
  201. +#
  202. +
  203. +obj-$(CONFIG_YAFFS_FS) += yaffs.o
  204. +
  205. +yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o
  206. +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
  207. +yaffs-y += yaffs_tagscompat.o yaffs_tagsmarshall.o
  208. +yaffs-y += yaffs_mtdif.o
  209. +yaffs-y += yaffs_nameval.o yaffs_attribs.o
  210. +yaffs-y += yaffs_allocator.o
  211. +yaffs-y += yaffs_yaffs1.o
  212. +yaffs-y += yaffs_yaffs2.o
  213. +yaffs-y += yaffs_bitmap.o
  214. +yaffs-y += yaffs_summary.o
  215. +yaffs-y += yaffs_verify.o
  216. +
  217. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_allocator.c linux-3.15-rc5/fs/yaffs2/yaffs_allocator.c
  218. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_allocator.c 1970-01-01 01:00:00.000000000 +0100
  219. +++ linux-3.15-rc5/fs/yaffs2/yaffs_allocator.c 2014-05-17 01:53:27.000000000 +0200
  220. @@ -0,0 +1,357 @@
  221. +/*
  222. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  223. + *
  224. + * Copyright (C) 2002-2011 Aleph One Ltd.
  225. + * for Toby Churchill Ltd and Brightstar Engineering
  226. + *
  227. + * Created by Charles Manning <charles@aleph1.co.uk>
  228. + *
  229. + * This program is free software; you can redistribute it and/or modify
  230. + * it under the terms of the GNU General Public License version 2 as
  231. + * published by the Free Software Foundation.
  232. + */
  233. +
  234. +#include "yaffs_allocator.h"
  235. +#include "yaffs_guts.h"
  236. +#include "yaffs_trace.h"
  237. +#include "yportenv.h"
  238. +
  239. +/*
  240. + * Each entry in yaffs_tnode_list and yaffs_obj_list hold blocks
  241. + * of approx 100 objects that are themn allocated singly.
  242. + * This is basically a simplified slab allocator.
  243. + *
  244. + * We don't use the Linux slab allocator because slab does not allow
  245. + * us to dump all the objects in one hit when we do a umount and tear
  246. + * down all the tnodes and objects. slab requires that we first free
  247. + * the individual objects.
  248. + *
  249. + * Once yaffs has been mainlined I shall try to motivate for a change
  250. + * to slab to provide the extra features we need here.
  251. + */
  252. +
  253. +struct yaffs_tnode_list {
  254. + struct yaffs_tnode_list *next;
  255. + struct yaffs_tnode *tnodes;
  256. +};
  257. +
  258. +struct yaffs_obj_list {
  259. + struct yaffs_obj_list *next;
  260. + struct yaffs_obj *objects;
  261. +};
  262. +
  263. +struct yaffs_allocator {
  264. + int n_tnodes_created;
  265. + struct yaffs_tnode *free_tnodes;
  266. + int n_free_tnodes;
  267. + struct yaffs_tnode_list *alloc_tnode_list;
  268. +
  269. + int n_obj_created;
  270. + struct list_head free_objs;
  271. + int n_free_objects;
  272. +
  273. + struct yaffs_obj_list *allocated_obj_list;
  274. +};
  275. +
  276. +static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
  277. +{
  278. + struct yaffs_allocator *allocator =
  279. + (struct yaffs_allocator *)dev->allocator;
  280. + struct yaffs_tnode_list *tmp;
  281. +
  282. + if (!allocator) {
  283. + BUG();
  284. + return;
  285. + }
  286. +
  287. + while (allocator->alloc_tnode_list) {
  288. + tmp = allocator->alloc_tnode_list->next;
  289. +
  290. + kfree(allocator->alloc_tnode_list->tnodes);
  291. + kfree(allocator->alloc_tnode_list);
  292. + allocator->alloc_tnode_list = tmp;
  293. + }
  294. +
  295. + allocator->free_tnodes = NULL;
  296. + allocator->n_free_tnodes = 0;
  297. + allocator->n_tnodes_created = 0;
  298. +}
  299. +
  300. +static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
  301. +{
  302. + struct yaffs_allocator *allocator = dev->allocator;
  303. +
  304. + if (!allocator) {
  305. + BUG();
  306. + return;
  307. + }
  308. +
  309. + allocator->alloc_tnode_list = NULL;
  310. + allocator->free_tnodes = NULL;
  311. + allocator->n_free_tnodes = 0;
  312. + allocator->n_tnodes_created = 0;
  313. +}
  314. +
  315. +static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
  316. +{
  317. + struct yaffs_allocator *allocator =
  318. + (struct yaffs_allocator *)dev->allocator;
  319. + int i;
  320. + struct yaffs_tnode *new_tnodes;
  321. + u8 *mem;
  322. + struct yaffs_tnode *curr;
  323. + struct yaffs_tnode *next;
  324. + struct yaffs_tnode_list *tnl;
  325. +
  326. + if (!allocator) {
  327. + BUG();
  328. + return YAFFS_FAIL;
  329. + }
  330. +
  331. + if (n_tnodes < 1)
  332. + return YAFFS_OK;
  333. +
  334. + /* make these things */
  335. + new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
  336. + mem = (u8 *) new_tnodes;
  337. +
  338. + if (!new_tnodes) {
  339. + yaffs_trace(YAFFS_TRACE_ERROR,
  340. + "yaffs: Could not allocate Tnodes");
  341. + return YAFFS_FAIL;
  342. + }
  343. +
  344. + /* New hookup for wide tnodes */
  345. + for (i = 0; i < n_tnodes - 1; i++) {
  346. + curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
  347. + next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
  348. + curr->internal[0] = next;
  349. + }
  350. +
  351. + curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
  352. + curr->internal[0] = allocator->free_tnodes;
  353. + allocator->free_tnodes = (struct yaffs_tnode *)mem;
  354. +
  355. + allocator->n_free_tnodes += n_tnodes;
  356. + allocator->n_tnodes_created += n_tnodes;
  357. +
  358. + /* Now add this bunch of tnodes to a list for freeing up.
  359. + * NB If we can't add this to the management list it isn't fatal
  360. + * but it just means we can't free this bunch of tnodes later.
  361. + */
  362. + tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
  363. + if (!tnl) {
  364. + yaffs_trace(YAFFS_TRACE_ERROR,
  365. + "Could not add tnodes to management list");
  366. + return YAFFS_FAIL;
  367. + } else {
  368. + tnl->tnodes = new_tnodes;
  369. + tnl->next = allocator->alloc_tnode_list;
  370. + allocator->alloc_tnode_list = tnl;
  371. + }
  372. +
  373. + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Tnodes added");
  374. +
  375. + return YAFFS_OK;
  376. +}
  377. +
  378. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  379. +{
  380. + struct yaffs_allocator *allocator =
  381. + (struct yaffs_allocator *)dev->allocator;
  382. + struct yaffs_tnode *tn = NULL;
  383. +
  384. + if (!allocator) {
  385. + BUG();
  386. + return NULL;
  387. + }
  388. +
  389. + /* If there are none left make more */
  390. + if (!allocator->free_tnodes)
  391. + yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
  392. +
  393. + if (allocator->free_tnodes) {
  394. + tn = allocator->free_tnodes;
  395. + allocator->free_tnodes = allocator->free_tnodes->internal[0];
  396. + allocator->n_free_tnodes--;
  397. + }
  398. +
  399. + return tn;
  400. +}
  401. +
  402. +/* FreeTnode frees up a tnode and puts it back on the free list */
  403. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  404. +{
  405. + struct yaffs_allocator *allocator = dev->allocator;
  406. +
  407. + if (!allocator) {
  408. + BUG();
  409. + return;
  410. + }
  411. +
  412. + if (tn) {
  413. + tn->internal[0] = allocator->free_tnodes;
  414. + allocator->free_tnodes = tn;
  415. + allocator->n_free_tnodes++;
  416. + }
  417. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  418. +}
  419. +
  420. +/*--------------- yaffs_obj alloaction ------------------------
  421. + *
  422. + * Free yaffs_objs are stored in a list using obj->siblings.
  423. + * The blocks of allocated objects are stored in a linked list.
  424. + */
  425. +
  426. +static void yaffs_init_raw_objs(struct yaffs_dev *dev)
  427. +{
  428. + struct yaffs_allocator *allocator = dev->allocator;
  429. +
  430. + if (!allocator) {
  431. + BUG();
  432. + return;
  433. + }
  434. +
  435. + allocator->allocated_obj_list = NULL;
  436. + INIT_LIST_HEAD(&allocator->free_objs);
  437. + allocator->n_free_objects = 0;
  438. +}
  439. +
  440. +static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  441. +{
  442. + struct yaffs_allocator *allocator = dev->allocator;
  443. + struct yaffs_obj_list *tmp;
  444. +
  445. + if (!allocator) {
  446. + BUG();
  447. + return;
  448. + }
  449. +
  450. + while (allocator->allocated_obj_list) {
  451. + tmp = allocator->allocated_obj_list->next;
  452. + kfree(allocator->allocated_obj_list->objects);
  453. + kfree(allocator->allocated_obj_list);
  454. + allocator->allocated_obj_list = tmp;
  455. + }
  456. +
  457. + INIT_LIST_HEAD(&allocator->free_objs);
  458. + allocator->n_free_objects = 0;
  459. + allocator->n_obj_created = 0;
  460. +}
  461. +
  462. +static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
  463. +{
  464. + struct yaffs_allocator *allocator = dev->allocator;
  465. + int i;
  466. + struct yaffs_obj *new_objs;
  467. + struct yaffs_obj_list *list;
  468. +
  469. + if (!allocator) {
  470. + BUG();
  471. + return YAFFS_FAIL;
  472. + }
  473. +
  474. + if (n_obj < 1)
  475. + return YAFFS_OK;
  476. +
  477. + /* make these things */
  478. + new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
  479. + list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
  480. +
  481. + if (!new_objs || !list) {
  482. + kfree(new_objs);
  483. + new_objs = NULL;
  484. + kfree(list);
  485. + list = NULL;
  486. + yaffs_trace(YAFFS_TRACE_ALLOCATE,
  487. + "Could not allocate more objects");
  488. + return YAFFS_FAIL;
  489. + }
  490. +
  491. + /* Hook them into the free list */
  492. + for (i = 0; i < n_obj; i++)
  493. + list_add(&new_objs[i].siblings, &allocator->free_objs);
  494. +
  495. + allocator->n_free_objects += n_obj;
  496. + allocator->n_obj_created += n_obj;
  497. +
  498. + /* Now add this bunch of Objects to a list for freeing up. */
  499. +
  500. + list->objects = new_objs;
  501. + list->next = allocator->allocated_obj_list;
  502. + allocator->allocated_obj_list = list;
  503. +
  504. + return YAFFS_OK;
  505. +}
  506. +
  507. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  508. +{
  509. + struct yaffs_obj *obj = NULL;
  510. + struct list_head *lh;
  511. + struct yaffs_allocator *allocator = dev->allocator;
  512. +
  513. + if (!allocator) {
  514. + BUG();
  515. + return obj;
  516. + }
  517. +
  518. + /* If there are none left make more */
  519. + if (list_empty(&allocator->free_objs))
  520. + yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
  521. +
  522. + if (!list_empty(&allocator->free_objs)) {
  523. + lh = allocator->free_objs.next;
  524. + obj = list_entry(lh, struct yaffs_obj, siblings);
  525. + list_del_init(lh);
  526. + allocator->n_free_objects--;
  527. + }
  528. +
  529. + return obj;
  530. +}
  531. +
  532. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  533. +{
  534. +
  535. + struct yaffs_allocator *allocator = dev->allocator;
  536. +
  537. + if (!allocator) {
  538. + BUG();
  539. + return;
  540. + }
  541. +
  542. + /* Link into the free list. */
  543. + list_add(&obj->siblings, &allocator->free_objs);
  544. + allocator->n_free_objects++;
  545. +}
  546. +
  547. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  548. +{
  549. +
  550. + if (!dev->allocator) {
  551. + BUG();
  552. + return;
  553. + }
  554. +
  555. + yaffs_deinit_raw_tnodes(dev);
  556. + yaffs_deinit_raw_objs(dev);
  557. + kfree(dev->allocator);
  558. + dev->allocator = NULL;
  559. +}
  560. +
  561. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  562. +{
  563. + struct yaffs_allocator *allocator;
  564. +
  565. + if (dev->allocator) {
  566. + BUG();
  567. + return;
  568. + }
  569. +
  570. + allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
  571. + if (allocator) {
  572. + dev->allocator = allocator;
  573. + yaffs_init_raw_tnodes(dev);
  574. + yaffs_init_raw_objs(dev);
  575. + }
  576. +}
  577. +
  578. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_allocator.h linux-3.15-rc5/fs/yaffs2/yaffs_allocator.h
  579. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_allocator.h 1970-01-01 01:00:00.000000000 +0100
  580. +++ linux-3.15-rc5/fs/yaffs2/yaffs_allocator.h 2014-05-17 01:53:27.000000000 +0200
  581. @@ -0,0 +1,30 @@
  582. +/*
  583. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  584. + *
  585. + * Copyright (C) 2002-2011 Aleph One Ltd.
  586. + * for Toby Churchill Ltd and Brightstar Engineering
  587. + *
  588. + * Created by Charles Manning <charles@aleph1.co.uk>
  589. + *
  590. + * This program is free software; you can redistribute it and/or modify
  591. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  592. + * published by the Free Software Foundation.
  593. + *
  594. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  595. + */
  596. +
  597. +#ifndef __YAFFS_ALLOCATOR_H__
  598. +#define __YAFFS_ALLOCATOR_H__
  599. +
  600. +#include "yaffs_guts.h"
  601. +
  602. +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev);
  603. +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev);
  604. +
  605. +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev);
  606. +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn);
  607. +
  608. +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev);
  609. +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj);
  610. +
  611. +#endif
  612. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_attribs.c linux-3.15-rc5/fs/yaffs2/yaffs_attribs.c
  613. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_attribs.c 1970-01-01 01:00:00.000000000 +0100
  614. +++ linux-3.15-rc5/fs/yaffs2/yaffs_attribs.c 2014-05-17 01:53:27.000000000 +0200
  615. @@ -0,0 +1,166 @@
  616. +/*
  617. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  618. + *
  619. + * Copyright (C) 2002-2011 Aleph One Ltd.
  620. + * for Toby Churchill Ltd and Brightstar Engineering
  621. + *
  622. + * Created by Charles Manning <charles@aleph1.co.uk>
  623. + *
  624. + * This program is free software; you can redistribute it and/or modify
  625. + * it under the terms of the GNU General Public License version 2 as
  626. + * published by the Free Software Foundation.
  627. + */
  628. +
  629. +#include "yaffs_guts.h"
  630. +#include "yaffs_attribs.h"
  631. +
  632. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
  633. +static inline uid_t ia_uid_read(const struct iattr *iattr)
  634. +{
  635. + return from_kuid(&init_user_ns, iattr->ia_uid);
  636. +}
  637. +
  638. +static inline gid_t ia_gid_read(const struct iattr *iattr)
  639. +{
  640. + return from_kgid(&init_user_ns, iattr->ia_gid);
  641. +}
  642. +
  643. +static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
  644. +{
  645. + iattr->ia_uid = make_kuid(&init_user_ns, uid);
  646. +}
  647. +
  648. +static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
  649. +{
  650. + iattr->ia_gid = make_kgid(&init_user_ns, gid);
  651. +}
  652. +#else
  653. +static inline uid_t ia_uid_read(const struct iattr *iattr)
  654. +{
  655. + return iattr->ia_uid;
  656. +}
  657. +
  658. +static inline gid_t ia_gid_read(const struct iattr *inode)
  659. +{
  660. + return iattr->ia_gid;
  661. +}
  662. +
  663. +static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
  664. +{
  665. + iattr->ia_uid = uid;
  666. +}
  667. +
  668. +static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
  669. +{
  670. + iattr->ia_gid = gid;
  671. +}
  672. +#endif
  673. +
  674. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
  675. +{
  676. + obj->yst_uid = oh->yst_uid;
  677. + obj->yst_gid = oh->yst_gid;
  678. + obj->yst_atime = oh->yst_atime;
  679. + obj->yst_mtime = oh->yst_mtime;
  680. + obj->yst_ctime = oh->yst_ctime;
  681. + obj->yst_rdev = oh->yst_rdev;
  682. +}
  683. +
  684. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
  685. +{
  686. + oh->yst_uid = obj->yst_uid;
  687. + oh->yst_gid = obj->yst_gid;
  688. + oh->yst_atime = obj->yst_atime;
  689. + oh->yst_mtime = obj->yst_mtime;
  690. + oh->yst_ctime = obj->yst_ctime;
  691. + oh->yst_rdev = obj->yst_rdev;
  692. +
  693. +}
  694. +
  695. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
  696. +{
  697. + obj->yst_mtime = Y_CURRENT_TIME;
  698. + if (do_a)
  699. + obj->yst_atime = obj->yst_mtime;
  700. + if (do_c)
  701. + obj->yst_ctime = obj->yst_mtime;
  702. +}
  703. +
  704. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
  705. +{
  706. + yaffs_load_current_time(obj, 1, 1);
  707. + obj->yst_rdev = rdev;
  708. + obj->yst_uid = uid;
  709. + obj->yst_gid = gid;
  710. +}
  711. +
  712. +static loff_t yaffs_get_file_size(struct yaffs_obj *obj)
  713. +{
  714. + YCHAR *alias = NULL;
  715. + obj = yaffs_get_equivalent_obj(obj);
  716. +
  717. + switch (obj->variant_type) {
  718. + case YAFFS_OBJECT_TYPE_FILE:
  719. + return obj->variant.file_variant.file_size;
  720. + case YAFFS_OBJECT_TYPE_SYMLINK:
  721. + alias = obj->variant.symlink_variant.alias;
  722. + if (!alias)
  723. + return 0;
  724. + return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
  725. + default:
  726. + return 0;
  727. + }
  728. +}
  729. +
  730. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
  731. +{
  732. + unsigned int valid = attr->ia_valid;
  733. +
  734. + if (valid & ATTR_MODE)
  735. + obj->yst_mode = attr->ia_mode;
  736. + if (valid & ATTR_UID)
  737. + obj->yst_uid = ia_uid_read(attr);
  738. + if (valid & ATTR_GID)
  739. + obj->yst_gid = ia_gid_read(attr);
  740. +
  741. + if (valid & ATTR_ATIME)
  742. + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
  743. + if (valid & ATTR_CTIME)
  744. + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
  745. + if (valid & ATTR_MTIME)
  746. + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
  747. +
  748. + if (valid & ATTR_SIZE)
  749. + yaffs_resize_file(obj, attr->ia_size);
  750. +
  751. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  752. +
  753. + return YAFFS_OK;
  754. +
  755. +}
  756. +
  757. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
  758. +{
  759. + unsigned int valid = 0;
  760. +
  761. + attr->ia_mode = obj->yst_mode;
  762. + valid |= ATTR_MODE;
  763. + ia_uid_write(attr, obj->yst_uid);
  764. + valid |= ATTR_UID;
  765. + ia_gid_write(attr, obj->yst_gid);
  766. + valid |= ATTR_GID;
  767. +
  768. + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
  769. + valid |= ATTR_ATIME;
  770. + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
  771. + valid |= ATTR_CTIME;
  772. + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
  773. + valid |= ATTR_MTIME;
  774. +
  775. + attr->ia_size = yaffs_get_file_size(obj);
  776. + valid |= ATTR_SIZE;
  777. +
  778. + attr->ia_valid = valid;
  779. +
  780. + return YAFFS_OK;
  781. +}
  782. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_attribs.h linux-3.15-rc5/fs/yaffs2/yaffs_attribs.h
  783. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_attribs.h 1970-01-01 01:00:00.000000000 +0100
  784. +++ linux-3.15-rc5/fs/yaffs2/yaffs_attribs.h 2014-05-17 01:53:27.000000000 +0200
  785. @@ -0,0 +1,28 @@
  786. +/*
  787. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  788. + *
  789. + * Copyright (C) 2002-2011 Aleph One Ltd.
  790. + * for Toby Churchill Ltd and Brightstar Engineering
  791. + *
  792. + * Created by Charles Manning <charles@aleph1.co.uk>
  793. + *
  794. + * This program is free software; you can redistribute it and/or modify
  795. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  796. + * published by the Free Software Foundation.
  797. + *
  798. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  799. + */
  800. +
  801. +#ifndef __YAFFS_ATTRIBS_H__
  802. +#define __YAFFS_ATTRIBS_H__
  803. +
  804. +#include "yaffs_guts.h"
  805. +
  806. +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh);
  807. +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj);
  808. +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev);
  809. +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
  810. +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
  811. +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
  812. +
  813. +#endif
  814. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_bitmap.c linux-3.15-rc5/fs/yaffs2/yaffs_bitmap.c
  815. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_bitmap.c 1970-01-01 01:00:00.000000000 +0100
  816. +++ linux-3.15-rc5/fs/yaffs2/yaffs_bitmap.c 2014-05-17 01:53:27.000000000 +0200
  817. @@ -0,0 +1,97 @@
  818. +/*
  819. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  820. + *
  821. + * Copyright (C) 2002-2011 Aleph One Ltd.
  822. + * for Toby Churchill Ltd and Brightstar Engineering
  823. + *
  824. + * Created by Charles Manning <charles@aleph1.co.uk>
  825. + *
  826. + * This program is free software; you can redistribute it and/or modify
  827. + * it under the terms of the GNU General Public License version 2 as
  828. + * published by the Free Software Foundation.
  829. + */
  830. +
  831. +#include "yaffs_bitmap.h"
  832. +#include "yaffs_trace.h"
  833. +/*
  834. + * Chunk bitmap manipulations
  835. + */
  836. +
  837. +static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
  838. +{
  839. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  840. + yaffs_trace(YAFFS_TRACE_ERROR,
  841. + "BlockBits block %d is not valid",
  842. + blk);
  843. + BUG();
  844. + }
  845. + return dev->chunk_bits +
  846. + (dev->chunk_bit_stride * (blk - dev->internal_start_block));
  847. +}
  848. +
  849. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
  850. +{
  851. + if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
  852. + chunk < 0 || chunk >= dev->param.chunks_per_block) {
  853. + yaffs_trace(YAFFS_TRACE_ERROR,
  854. + "Chunk Id (%d:%d) invalid",
  855. + blk, chunk);
  856. + BUG();
  857. + }
  858. +}
  859. +
  860. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
  861. +{
  862. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  863. +
  864. + memset(blk_bits, 0, dev->chunk_bit_stride);
  865. +}
  866. +
  867. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  868. +{
  869. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  870. +
  871. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  872. + blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
  873. +}
  874. +
  875. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  876. +{
  877. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  878. +
  879. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  880. + blk_bits[chunk / 8] |= (1 << (chunk & 7));
  881. +}
  882. +
  883. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
  884. +{
  885. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  886. +
  887. + yaffs_verify_chunk_bit_id(dev, blk, chunk);
  888. + return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
  889. +}
  890. +
  891. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
  892. +{
  893. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  894. + int i;
  895. +
  896. + for (i = 0; i < dev->chunk_bit_stride; i++) {
  897. + if (*blk_bits)
  898. + return 1;
  899. + blk_bits++;
  900. + }
  901. + return 0;
  902. +}
  903. +
  904. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
  905. +{
  906. + u8 *blk_bits = yaffs_block_bits(dev, blk);
  907. + int i;
  908. + int n = 0;
  909. +
  910. + for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++)
  911. + n += hweight8(*blk_bits);
  912. +
  913. + return n;
  914. +}
  915. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_bitmap.h linux-3.15-rc5/fs/yaffs2/yaffs_bitmap.h
  916. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_bitmap.h 1970-01-01 01:00:00.000000000 +0100
  917. +++ linux-3.15-rc5/fs/yaffs2/yaffs_bitmap.h 2014-05-17 01:53:27.000000000 +0200
  918. @@ -0,0 +1,33 @@
  919. +/*
  920. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  921. + *
  922. + * Copyright (C) 2002-2011 Aleph One Ltd.
  923. + * for Toby Churchill Ltd and Brightstar Engineering
  924. + *
  925. + * Created by Charles Manning <charles@aleph1.co.uk>
  926. + *
  927. + * This program is free software; you can redistribute it and/or modify
  928. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  929. + * published by the Free Software Foundation.
  930. + *
  931. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  932. + */
  933. +
  934. +/*
  935. + * Chunk bitmap manipulations
  936. + */
  937. +
  938. +#ifndef __YAFFS_BITMAP_H__
  939. +#define __YAFFS_BITMAP_H__
  940. +
  941. +#include "yaffs_guts.h"
  942. +
  943. +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk);
  944. +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk);
  945. +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  946. +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  947. +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
  948. +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk);
  949. +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk);
  950. +
  951. +#endif
  952. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_checkptrw.c linux-3.15-rc5/fs/yaffs2/yaffs_checkptrw.c
  953. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  954. +++ linux-3.15-rc5/fs/yaffs2/yaffs_checkptrw.c 2014-05-17 01:53:27.000000000 +0200
  955. @@ -0,0 +1,474 @@
  956. +/*
  957. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  958. + *
  959. + * Copyright (C) 2002-2011 Aleph One Ltd.
  960. + * for Toby Churchill Ltd and Brightstar Engineering
  961. + *
  962. + * Created by Charles Manning <charles@aleph1.co.uk>
  963. + *
  964. + * This program is free software; you can redistribute it and/or modify
  965. + * it under the terms of the GNU General Public License version 2 as
  966. + * published by the Free Software Foundation.
  967. + */
  968. +
  969. +#include "yaffs_checkptrw.h"
  970. +#include "yaffs_getblockinfo.h"
  971. +
  972. +struct yaffs_checkpt_chunk_hdr {
  973. + int version;
  974. + int seq;
  975. + u32 sum;
  976. + u32 xor;
  977. +} ;
  978. +
  979. +
  980. +static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
  981. +{
  982. + return chunk - dev->chunk_offset;
  983. +}
  984. +
  985. +static int apply_block_offset(struct yaffs_dev *dev, int block)
  986. +{
  987. + return block - dev->block_offset;
  988. +}
  989. +
  990. +static void yaffs2_checkpt_init_chunk_hdr(struct yaffs_dev *dev)
  991. +{
  992. + struct yaffs_checkpt_chunk_hdr hdr;
  993. +
  994. + hdr.version = YAFFS_CHECKPOINT_VERSION;
  995. + hdr.seq = dev->checkpt_page_seq;
  996. + hdr.sum = dev->checkpt_sum;
  997. + hdr.xor = dev->checkpt_xor;
  998. +
  999. + dev->checkpt_byte_offs = sizeof(hdr);
  1000. +
  1001. + memcpy(dev->checkpt_buffer, &hdr, sizeof(hdr));
  1002. +}
  1003. +
  1004. +static int yaffs2_checkpt_check_chunk_hdr(struct yaffs_dev *dev)
  1005. +{
  1006. + struct yaffs_checkpt_chunk_hdr hdr;
  1007. +
  1008. + memcpy(&hdr, dev->checkpt_buffer, sizeof(hdr));
  1009. +
  1010. + dev->checkpt_byte_offs = sizeof(hdr);
  1011. +
  1012. + return hdr.version == YAFFS_CHECKPOINT_VERSION &&
  1013. + hdr.seq == dev->checkpt_page_seq &&
  1014. + hdr.sum == dev->checkpt_sum &&
  1015. + hdr.xor == dev->checkpt_xor;
  1016. +}
  1017. +
  1018. +static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
  1019. +{
  1020. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1021. +
  1022. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1023. + "checkpt blocks_avail = %d", blocks_avail);
  1024. +
  1025. + return (blocks_avail <= 0) ? 0 : 1;
  1026. +}
  1027. +
  1028. +static int yaffs_checkpt_erase(struct yaffs_dev *dev)
  1029. +{
  1030. + int i;
  1031. +
  1032. + if (!dev->drv.drv_erase_fn)
  1033. + return 0;
  1034. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1035. + "checking blocks %d to %d",
  1036. + dev->internal_start_block, dev->internal_end_block);
  1037. +
  1038. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  1039. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  1040. + int offset_i = apply_block_offset(dev, i);
  1041. + int result;
  1042. +
  1043. + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  1044. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1045. + "erasing checkpt block %d", i);
  1046. +
  1047. + dev->n_erasures++;
  1048. +
  1049. + result = dev->drv.drv_erase_fn(dev, offset_i);
  1050. + if(result) {
  1051. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1052. + dev->n_erased_blocks++;
  1053. + dev->n_free_chunks +=
  1054. + dev->param.chunks_per_block;
  1055. + } else {
  1056. + dev->drv.drv_mark_bad_fn(dev, offset_i);
  1057. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  1058. + }
  1059. + }
  1060. + }
  1061. +
  1062. + dev->blocks_in_checkpt = 0;
  1063. +
  1064. + return 1;
  1065. +}
  1066. +
  1067. +static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
  1068. +{
  1069. + int i;
  1070. + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  1071. +
  1072. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1073. + "allocating checkpt block: erased %d reserved %d avail %d next %d ",
  1074. + dev->n_erased_blocks, dev->param.n_reserved_blocks,
  1075. + blocks_avail, dev->checkpt_next_block);
  1076. +
  1077. + if (dev->checkpt_next_block >= 0 &&
  1078. + dev->checkpt_next_block <= dev->internal_end_block &&
  1079. + blocks_avail > 0) {
  1080. +
  1081. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1082. + i++) {
  1083. + struct yaffs_block_info *bi;
  1084. +
  1085. + bi = yaffs_get_block_info(dev, i);
  1086. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  1087. + dev->checkpt_next_block = i + 1;
  1088. + dev->checkpt_cur_block = i;
  1089. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1090. + "allocating checkpt block %d", i);
  1091. + return;
  1092. + }
  1093. + }
  1094. + }
  1095. + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
  1096. +
  1097. + dev->checkpt_next_block = -1;
  1098. + dev->checkpt_cur_block = -1;
  1099. +}
  1100. +
  1101. +static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
  1102. +{
  1103. + int i;
  1104. + struct yaffs_ext_tags tags;
  1105. +
  1106. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1107. + "find next checkpt block: start: blocks %d next %d",
  1108. + dev->blocks_in_checkpt, dev->checkpt_next_block);
  1109. +
  1110. + if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
  1111. + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  1112. + i++) {
  1113. + int chunk = i * dev->param.chunks_per_block;
  1114. + enum yaffs_block_state state;
  1115. + u32 seq;
  1116. +
  1117. + dev->tagger.read_chunk_tags_fn(dev,
  1118. + apply_chunk_offset(dev, chunk),
  1119. + NULL, &tags);
  1120. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1121. + "find next checkpt block: search: block %d state %d oid %d seq %d eccr %d",
  1122. + i, (int) state,
  1123. + tags.obj_id, tags.seq_number,
  1124. + tags.ecc_result);
  1125. +
  1126. + if (tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
  1127. + continue;
  1128. +
  1129. + dev->tagger.query_block_fn(dev,
  1130. + apply_block_offset(dev, i),
  1131. + &state, &seq);
  1132. + if (state == YAFFS_BLOCK_STATE_DEAD)
  1133. + continue;
  1134. +
  1135. + /* Right kind of block */
  1136. + dev->checkpt_next_block = tags.obj_id;
  1137. + dev->checkpt_cur_block = i;
  1138. + dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
  1139. + dev->blocks_in_checkpt++;
  1140. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1141. + "found checkpt block %d", i);
  1142. + return;
  1143. + }
  1144. +
  1145. + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
  1146. +
  1147. + dev->checkpt_next_block = -1;
  1148. + dev->checkpt_cur_block = -1;
  1149. +}
  1150. +
  1151. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
  1152. +{
  1153. + int i;
  1154. +
  1155. + dev->checkpt_open_write = writing;
  1156. +
  1157. + /* Got the functions we need? */
  1158. + if (!dev->tagger.write_chunk_tags_fn ||
  1159. + !dev->tagger.read_chunk_tags_fn ||
  1160. + !dev->drv.drv_erase_fn ||
  1161. + !dev->drv.drv_mark_bad_fn)
  1162. + return 0;
  1163. +
  1164. + if (writing && !yaffs2_checkpt_space_ok(dev))
  1165. + return 0;
  1166. +
  1167. + if (!dev->checkpt_buffer)
  1168. + dev->checkpt_buffer =
  1169. + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  1170. + if (!dev->checkpt_buffer)
  1171. + return 0;
  1172. +
  1173. + dev->checkpt_page_seq = 0;
  1174. + dev->checkpt_byte_count = 0;
  1175. + dev->checkpt_sum = 0;
  1176. + dev->checkpt_xor = 0;
  1177. + dev->checkpt_cur_block = -1;
  1178. + dev->checkpt_cur_chunk = -1;
  1179. + dev->checkpt_next_block = dev->internal_start_block;
  1180. +
  1181. + if (writing) {
  1182. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1183. + yaffs2_checkpt_init_chunk_hdr(dev);
  1184. + return yaffs_checkpt_erase(dev);
  1185. + }
  1186. +
  1187. + /* Opening for a read */
  1188. + /* Set to a value that will kick off a read */
  1189. + dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
  1190. + /* A checkpoint block list of 1 checkpoint block per 16 block is
  1191. + * (hopefully) going to be way more than we need */
  1192. + dev->blocks_in_checkpt = 0;
  1193. + dev->checkpt_max_blocks =
  1194. + (dev->internal_end_block - dev->internal_start_block) / 16 + 2;
  1195. + dev->checkpt_block_list =
  1196. + kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
  1197. +
  1198. + if (!dev->checkpt_block_list)
  1199. + return 0;
  1200. +
  1201. + for (i = 0; i < dev->checkpt_max_blocks; i++)
  1202. + dev->checkpt_block_list[i] = -1;
  1203. +
  1204. + return 1;
  1205. +}
  1206. +
  1207. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  1208. +{
  1209. + u32 composite_sum;
  1210. +
  1211. + composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
  1212. + *sum = composite_sum;
  1213. + return 1;
  1214. +}
  1215. +
  1216. +static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  1217. +{
  1218. + int chunk;
  1219. + int offset_chunk;
  1220. + struct yaffs_ext_tags tags;
  1221. +
  1222. + if (dev->checkpt_cur_block < 0) {
  1223. + yaffs2_checkpt_find_erased_block(dev);
  1224. + dev->checkpt_cur_chunk = 0;
  1225. + }
  1226. +
  1227. + if (dev->checkpt_cur_block < 0)
  1228. + return 0;
  1229. +
  1230. + tags.is_deleted = 0;
  1231. + tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  1232. + tags.chunk_id = dev->checkpt_page_seq + 1;
  1233. + tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  1234. + tags.n_bytes = dev->data_bytes_per_chunk;
  1235. + if (dev->checkpt_cur_chunk == 0) {
  1236. + /* First chunk we write for the block? Set block state to
  1237. + checkpoint */
  1238. + struct yaffs_block_info *bi =
  1239. + yaffs_get_block_info(dev, dev->checkpt_cur_block);
  1240. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1241. + dev->blocks_in_checkpt++;
  1242. + }
  1243. +
  1244. + chunk =
  1245. + dev->checkpt_cur_block * dev->param.chunks_per_block +
  1246. + dev->checkpt_cur_chunk;
  1247. +
  1248. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1249. + "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
  1250. + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
  1251. + tags.obj_id, tags.chunk_id);
  1252. +
  1253. + offset_chunk = apply_chunk_offset(dev, chunk);
  1254. +
  1255. + dev->n_page_writes++;
  1256. +
  1257. + dev->tagger.write_chunk_tags_fn(dev, offset_chunk,
  1258. + dev->checkpt_buffer, &tags);
  1259. + dev->checkpt_page_seq++;
  1260. + dev->checkpt_cur_chunk++;
  1261. + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  1262. + dev->checkpt_cur_chunk = 0;
  1263. + dev->checkpt_cur_block = -1;
  1264. + }
  1265. + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  1266. +
  1267. + yaffs2_checkpt_init_chunk_hdr(dev);
  1268. +
  1269. +
  1270. + return 1;
  1271. +}
  1272. +
  1273. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  1274. +{
  1275. + int i = 0;
  1276. + int ok = 1;
  1277. + u8 *data_bytes = (u8 *) data;
  1278. +
  1279. + if (!dev->checkpt_buffer)
  1280. + return 0;
  1281. +
  1282. + if (!dev->checkpt_open_write)
  1283. + return -1;
  1284. +
  1285. + while (i < n_bytes && ok) {
  1286. + dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  1287. + dev->checkpt_sum += *data_bytes;
  1288. + dev->checkpt_xor ^= *data_bytes;
  1289. +
  1290. + dev->checkpt_byte_offs++;
  1291. + i++;
  1292. + data_bytes++;
  1293. + dev->checkpt_byte_count++;
  1294. +
  1295. + if (dev->checkpt_byte_offs < 0 ||
  1296. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  1297. + ok = yaffs2_checkpt_flush_buffer(dev);
  1298. + }
  1299. +
  1300. + return i;
  1301. +}
  1302. +
  1303. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  1304. +{
  1305. + int i = 0;
  1306. + int ok = 1;
  1307. + struct yaffs_ext_tags tags;
  1308. + int chunk;
  1309. + int offset_chunk;
  1310. + u8 *data_bytes = (u8 *) data;
  1311. +
  1312. + if (!dev->checkpt_buffer)
  1313. + return 0;
  1314. +
  1315. + if (dev->checkpt_open_write)
  1316. + return -1;
  1317. +
  1318. + while (i < n_bytes && ok) {
  1319. +
  1320. + if (dev->checkpt_byte_offs < 0 ||
  1321. + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  1322. +
  1323. + if (dev->checkpt_cur_block < 0) {
  1324. + yaffs2_checkpt_find_block(dev);
  1325. + dev->checkpt_cur_chunk = 0;
  1326. + }
  1327. +
  1328. + if (dev->checkpt_cur_block < 0) {
  1329. + ok = 0;
  1330. + break;
  1331. + }
  1332. +
  1333. + chunk = dev->checkpt_cur_block *
  1334. + dev->param.chunks_per_block +
  1335. + dev->checkpt_cur_chunk;
  1336. +
  1337. + offset_chunk = apply_chunk_offset(dev, chunk);
  1338. + dev->n_page_reads++;
  1339. +
  1340. + /* read in the next chunk */
  1341. + dev->tagger.read_chunk_tags_fn(dev,
  1342. + offset_chunk,
  1343. + dev->checkpt_buffer,
  1344. + &tags);
  1345. +
  1346. + if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
  1347. + tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  1348. + tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  1349. + ok = 0;
  1350. + break;
  1351. + }
  1352. + if(!yaffs2_checkpt_check_chunk_hdr(dev)) {
  1353. + ok = 0;
  1354. + break;
  1355. + }
  1356. +
  1357. + dev->checkpt_page_seq++;
  1358. + dev->checkpt_cur_chunk++;
  1359. +
  1360. + if (dev->checkpt_cur_chunk >=
  1361. + dev->param.chunks_per_block)
  1362. + dev->checkpt_cur_block = -1;
  1363. +
  1364. + }
  1365. +
  1366. + *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
  1367. + dev->checkpt_sum += *data_bytes;
  1368. + dev->checkpt_xor ^= *data_bytes;
  1369. + dev->checkpt_byte_offs++;
  1370. + i++;
  1371. + data_bytes++;
  1372. + dev->checkpt_byte_count++;
  1373. + }
  1374. +
  1375. + return i;
  1376. +}
  1377. +
  1378. +int yaffs_checkpt_close(struct yaffs_dev *dev)
  1379. +{
  1380. + int i;
  1381. +
  1382. + if (dev->checkpt_open_write) {
  1383. + if (dev->checkpt_byte_offs !=
  1384. + sizeof(sizeof(struct yaffs_checkpt_chunk_hdr)))
  1385. + yaffs2_checkpt_flush_buffer(dev);
  1386. + } else if (dev->checkpt_block_list) {
  1387. + for (i = 0;
  1388. + i < dev->blocks_in_checkpt &&
  1389. + dev->checkpt_block_list[i] >= 0; i++) {
  1390. + int blk = dev->checkpt_block_list[i];
  1391. + struct yaffs_block_info *bi = NULL;
  1392. +
  1393. + if (dev->internal_start_block <= blk &&
  1394. + blk <= dev->internal_end_block)
  1395. + bi = yaffs_get_block_info(dev, blk);
  1396. + if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  1397. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1398. + }
  1399. + kfree(dev->checkpt_block_list);
  1400. + dev->checkpt_block_list = NULL;
  1401. + }
  1402. +
  1403. + dev->n_free_chunks -=
  1404. + dev->blocks_in_checkpt * dev->param.chunks_per_block;
  1405. + dev->n_erased_blocks -= dev->blocks_in_checkpt;
  1406. +
  1407. + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
  1408. + dev->checkpt_byte_count);
  1409. +
  1410. + if (dev->checkpt_buffer) {
  1411. + /* free the buffer */
  1412. + kfree(dev->checkpt_buffer);
  1413. + dev->checkpt_buffer = NULL;
  1414. + return 1;
  1415. + } else {
  1416. + return 0;
  1417. + }
  1418. +}
  1419. +
  1420. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  1421. +{
  1422. + /* Erase the checkpoint data */
  1423. +
  1424. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  1425. + "checkpoint invalidate of %d blocks",
  1426. + dev->blocks_in_checkpt);
  1427. +
  1428. + return yaffs_checkpt_erase(dev);
  1429. +}
  1430. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_checkptrw.h linux-3.15-rc5/fs/yaffs2/yaffs_checkptrw.h
  1431. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  1432. +++ linux-3.15-rc5/fs/yaffs2/yaffs_checkptrw.h 2014-05-17 01:53:27.000000000 +0200
  1433. @@ -0,0 +1,33 @@
  1434. +/*
  1435. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1436. + *
  1437. + * Copyright (C) 2002-2011 Aleph One Ltd.
  1438. + * for Toby Churchill Ltd and Brightstar Engineering
  1439. + *
  1440. + * Created by Charles Manning <charles@aleph1.co.uk>
  1441. + *
  1442. + * This program is free software; you can redistribute it and/or modify
  1443. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1444. + * published by the Free Software Foundation.
  1445. + *
  1446. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1447. + */
  1448. +
  1449. +#ifndef __YAFFS_CHECKPTRW_H__
  1450. +#define __YAFFS_CHECKPTRW_H__
  1451. +
  1452. +#include "yaffs_guts.h"
  1453. +
  1454. +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing);
  1455. +
  1456. +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes);
  1457. +
  1458. +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes);
  1459. +
  1460. +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum);
  1461. +
  1462. +int yaffs_checkpt_close(struct yaffs_dev *dev);
  1463. +
  1464. +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev);
  1465. +
  1466. +#endif
  1467. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_ecc.c linux-3.15-rc5/fs/yaffs2/yaffs_ecc.c
  1468. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1469. +++ linux-3.15-rc5/fs/yaffs2/yaffs_ecc.c 2014-05-17 01:53:27.000000000 +0200
  1470. @@ -0,0 +1,281 @@
  1471. +/*
  1472. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1473. + *
  1474. + * Copyright (C) 2002-2011 Aleph One Ltd.
  1475. + * for Toby Churchill Ltd and Brightstar Engineering
  1476. + *
  1477. + * Created by Charles Manning <charles@aleph1.co.uk>
  1478. + *
  1479. + * This program is free software; you can redistribute it and/or modify
  1480. + * it under the terms of the GNU General Public License version 2 as
  1481. + * published by the Free Software Foundation.
  1482. + */
  1483. +
  1484. +/*
  1485. + * This code implements the ECC algorithm used in SmartMedia.
  1486. + *
  1487. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1488. + * The two unused bit are set to 1.
  1489. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two
  1490. + * such ECC blocks are used on a 512-byte NAND page.
  1491. + *
  1492. + */
  1493. +
  1494. +#include "yportenv.h"
  1495. +
  1496. +#include "yaffs_ecc.h"
  1497. +
  1498. +/* Table generated by gen-ecc.c
  1499. + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  1500. + * for each byte of data. These are instead provided in a table in bits7..2.
  1501. + * Bit 0 of each entry indicates whether the entry has an odd or even parity,
  1502. + * and therefore this bytes influence on the line parity.
  1503. + */
  1504. +
  1505. +static const unsigned char column_parity_table[] = {
  1506. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1507. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1508. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1509. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1510. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1511. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1512. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1513. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1514. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1515. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1516. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1517. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1518. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1519. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1520. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1521. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1522. + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  1523. + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  1524. + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  1525. + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  1526. + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  1527. + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  1528. + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  1529. + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  1530. + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  1531. + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  1532. + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  1533. + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  1534. + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  1535. + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  1536. + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  1537. + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  1538. +};
  1539. +
  1540. +
  1541. +/* Calculate the ECC for a 256-byte block of data */
  1542. +void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc)
  1543. +{
  1544. + unsigned int i;
  1545. + unsigned char col_parity = 0;
  1546. + unsigned char line_parity = 0;
  1547. + unsigned char line_parity_prime = 0;
  1548. + unsigned char t;
  1549. + unsigned char b;
  1550. +
  1551. + for (i = 0; i < 256; i++) {
  1552. + b = column_parity_table[*data++];
  1553. + col_parity ^= b;
  1554. +
  1555. + if (b & 0x01) { /* odd number of bits in the byte */
  1556. + line_parity ^= i;
  1557. + line_parity_prime ^= ~i;
  1558. + }
  1559. + }
  1560. +
  1561. + ecc[2] = (~col_parity) | 0x03;
  1562. +
  1563. + t = 0;
  1564. + if (line_parity & 0x80)
  1565. + t |= 0x80;
  1566. + if (line_parity_prime & 0x80)
  1567. + t |= 0x40;
  1568. + if (line_parity & 0x40)
  1569. + t |= 0x20;
  1570. + if (line_parity_prime & 0x40)
  1571. + t |= 0x10;
  1572. + if (line_parity & 0x20)
  1573. + t |= 0x08;
  1574. + if (line_parity_prime & 0x20)
  1575. + t |= 0x04;
  1576. + if (line_parity & 0x10)
  1577. + t |= 0x02;
  1578. + if (line_parity_prime & 0x10)
  1579. + t |= 0x01;
  1580. + ecc[1] = ~t;
  1581. +
  1582. + t = 0;
  1583. + if (line_parity & 0x08)
  1584. + t |= 0x80;
  1585. + if (line_parity_prime & 0x08)
  1586. + t |= 0x40;
  1587. + if (line_parity & 0x04)
  1588. + t |= 0x20;
  1589. + if (line_parity_prime & 0x04)
  1590. + t |= 0x10;
  1591. + if (line_parity & 0x02)
  1592. + t |= 0x08;
  1593. + if (line_parity_prime & 0x02)
  1594. + t |= 0x04;
  1595. + if (line_parity & 0x01)
  1596. + t |= 0x02;
  1597. + if (line_parity_prime & 0x01)
  1598. + t |= 0x01;
  1599. + ecc[0] = ~t;
  1600. +
  1601. +}
  1602. +
  1603. +/* Correct the ECC on a 256 byte block of data */
  1604. +
  1605. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1606. + const unsigned char *test_ecc)
  1607. +{
  1608. + unsigned char d0, d1, d2; /* deltas */
  1609. +
  1610. + d0 = read_ecc[0] ^ test_ecc[0];
  1611. + d1 = read_ecc[1] ^ test_ecc[1];
  1612. + d2 = read_ecc[2] ^ test_ecc[2];
  1613. +
  1614. + if ((d0 | d1 | d2) == 0)
  1615. + return 0; /* no error */
  1616. +
  1617. + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  1618. + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  1619. + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  1620. + /* Single bit (recoverable) error in data */
  1621. +
  1622. + unsigned byte;
  1623. + unsigned bit;
  1624. +
  1625. + bit = byte = 0;
  1626. +
  1627. + if (d1 & 0x80)
  1628. + byte |= 0x80;
  1629. + if (d1 & 0x20)
  1630. + byte |= 0x40;
  1631. + if (d1 & 0x08)
  1632. + byte |= 0x20;
  1633. + if (d1 & 0x02)
  1634. + byte |= 0x10;
  1635. + if (d0 & 0x80)
  1636. + byte |= 0x08;
  1637. + if (d0 & 0x20)
  1638. + byte |= 0x04;
  1639. + if (d0 & 0x08)
  1640. + byte |= 0x02;
  1641. + if (d0 & 0x02)
  1642. + byte |= 0x01;
  1643. +
  1644. + if (d2 & 0x80)
  1645. + bit |= 0x04;
  1646. + if (d2 & 0x20)
  1647. + bit |= 0x02;
  1648. + if (d2 & 0x08)
  1649. + bit |= 0x01;
  1650. +
  1651. + data[byte] ^= (1 << bit);
  1652. +
  1653. + return 1; /* Corrected the error */
  1654. + }
  1655. +
  1656. + if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
  1657. + /* Reccoverable error in ecc */
  1658. +
  1659. + read_ecc[0] = test_ecc[0];
  1660. + read_ecc[1] = test_ecc[1];
  1661. + read_ecc[2] = test_ecc[2];
  1662. +
  1663. + return 1; /* Corrected the error */
  1664. + }
  1665. +
  1666. + /* Unrecoverable error */
  1667. +
  1668. + return -1;
  1669. +
  1670. +}
  1671. +
  1672. +/*
  1673. + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  1674. + */
  1675. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1676. + struct yaffs_ecc_other *ecc_other)
  1677. +{
  1678. + unsigned int i;
  1679. + unsigned char col_parity = 0;
  1680. + unsigned line_parity = 0;
  1681. + unsigned line_parity_prime = 0;
  1682. + unsigned char b;
  1683. +
  1684. + for (i = 0; i < n_bytes; i++) {
  1685. + b = column_parity_table[*data++];
  1686. + col_parity ^= b;
  1687. +
  1688. + if (b & 0x01) {
  1689. + /* odd number of bits in the byte */
  1690. + line_parity ^= i;
  1691. + line_parity_prime ^= ~i;
  1692. + }
  1693. +
  1694. + }
  1695. +
  1696. + ecc_other->col_parity = (col_parity >> 2) & 0x3f;
  1697. + ecc_other->line_parity = line_parity;
  1698. + ecc_other->line_parity_prime = line_parity_prime;
  1699. +}
  1700. +
  1701. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1702. + struct yaffs_ecc_other *read_ecc,
  1703. + const struct yaffs_ecc_other *test_ecc)
  1704. +{
  1705. + unsigned char delta_col; /* column parity delta */
  1706. + unsigned delta_line; /* line parity delta */
  1707. + unsigned delta_line_prime; /* line parity delta */
  1708. + unsigned bit;
  1709. +
  1710. + delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
  1711. + delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
  1712. + delta_line_prime =
  1713. + read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
  1714. +
  1715. + if ((delta_col | delta_line | delta_line_prime) == 0)
  1716. + return 0; /* no error */
  1717. +
  1718. + if (delta_line == ~delta_line_prime &&
  1719. + (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
  1720. + /* Single bit (recoverable) error in data */
  1721. +
  1722. + bit = 0;
  1723. +
  1724. + if (delta_col & 0x20)
  1725. + bit |= 0x04;
  1726. + if (delta_col & 0x08)
  1727. + bit |= 0x02;
  1728. + if (delta_col & 0x02)
  1729. + bit |= 0x01;
  1730. +
  1731. + if (delta_line >= n_bytes)
  1732. + return -1;
  1733. +
  1734. + data[delta_line] ^= (1 << bit);
  1735. +
  1736. + return 1; /* corrected */
  1737. + }
  1738. +
  1739. + if ((hweight32(delta_line) +
  1740. + hweight32(delta_line_prime) +
  1741. + hweight8(delta_col)) == 1) {
  1742. + /* Reccoverable error in ecc */
  1743. +
  1744. + *read_ecc = *test_ecc;
  1745. + return 1; /* corrected */
  1746. + }
  1747. +
  1748. + /* Unrecoverable error */
  1749. +
  1750. + return -1;
  1751. +}
  1752. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_ecc.h linux-3.15-rc5/fs/yaffs2/yaffs_ecc.h
  1753. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1754. +++ linux-3.15-rc5/fs/yaffs2/yaffs_ecc.h 2014-05-17 01:53:27.000000000 +0200
  1755. @@ -0,0 +1,44 @@
  1756. +/*
  1757. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1758. + *
  1759. + * Copyright (C) 2002-2011 Aleph One Ltd.
  1760. + * for Toby Churchill Ltd and Brightstar Engineering
  1761. + *
  1762. + * Created by Charles Manning <charles@aleph1.co.uk>
  1763. + *
  1764. + * This program is free software; you can redistribute it and/or modify
  1765. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1766. + * published by the Free Software Foundation.
  1767. + *
  1768. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1769. + */
  1770. +
  1771. +/*
  1772. + * This code implements the ECC algorithm used in SmartMedia.
  1773. + *
  1774. + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
  1775. + * The two unused bit are set to 1.
  1776. + * The ECC can correct single bit errors in a 256-byte page of data.
  1777. + * Thus, two such ECC blocks are used on a 512-byte NAND page.
  1778. + *
  1779. + */
  1780. +
  1781. +#ifndef __YAFFS_ECC_H__
  1782. +#define __YAFFS_ECC_H__
  1783. +
  1784. +struct yaffs_ecc_other {
  1785. + unsigned char col_parity;
  1786. + unsigned line_parity;
  1787. + unsigned line_parity_prime;
  1788. +};
  1789. +
  1790. +void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc);
  1791. +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
  1792. + const unsigned char *test_ecc);
  1793. +
  1794. +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
  1795. + struct yaffs_ecc_other *ecc);
  1796. +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
  1797. + struct yaffs_ecc_other *read_ecc,
  1798. + const struct yaffs_ecc_other *test_ecc);
  1799. +#endif
  1800. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_getblockinfo.h linux-3.15-rc5/fs/yaffs2/yaffs_getblockinfo.h
  1801. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  1802. +++ linux-3.15-rc5/fs/yaffs2/yaffs_getblockinfo.h 2014-05-17 01:53:27.000000000 +0200
  1803. @@ -0,0 +1,35 @@
  1804. +/*
  1805. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  1806. + *
  1807. + * Copyright (C) 2002-2011 Aleph One Ltd.
  1808. + * for Toby Churchill Ltd and Brightstar Engineering
  1809. + *
  1810. + * Created by Charles Manning <charles@aleph1.co.uk>
  1811. + *
  1812. + * This program is free software; you can redistribute it and/or modify
  1813. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  1814. + * published by the Free Software Foundation.
  1815. + *
  1816. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  1817. + */
  1818. +
  1819. +#ifndef __YAFFS_GETBLOCKINFO_H__
  1820. +#define __YAFFS_GETBLOCKINFO_H__
  1821. +
  1822. +#include "yaffs_guts.h"
  1823. +#include "yaffs_trace.h"
  1824. +
  1825. +/* Function to manipulate block info */
  1826. +static inline struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev
  1827. + *dev, int blk)
  1828. +{
  1829. + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
  1830. + yaffs_trace(YAFFS_TRACE_ERROR,
  1831. + "**>> yaffs: get_block_info block %d is not valid",
  1832. + blk);
  1833. + BUG();
  1834. + }
  1835. + return &dev->block_info[blk - dev->internal_start_block];
  1836. +}
  1837. +
  1838. +#endif
  1839. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_guts.c linux-3.15-rc5/fs/yaffs2/yaffs_guts.c
  1840. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  1841. +++ linux-3.15-rc5/fs/yaffs2/yaffs_guts.c 2014-05-17 01:53:27.000000000 +0200
  1842. @@ -0,0 +1,5146 @@
  1843. +/*
  1844. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  1845. + *
  1846. + * Copyright (C) 2002-2011 Aleph One Ltd.
  1847. + * for Toby Churchill Ltd and Brightstar Engineering
  1848. + *
  1849. + * Created by Charles Manning <charles@aleph1.co.uk>
  1850. + *
  1851. + * This program is free software; you can redistribute it and/or modify
  1852. + * it under the terms of the GNU General Public License version 2 as
  1853. + * published by the Free Software Foundation.
  1854. + */
  1855. +
  1856. +#include "yportenv.h"
  1857. +#include "yaffs_trace.h"
  1858. +
  1859. +#include "yaffs_guts.h"
  1860. +#include "yaffs_getblockinfo.h"
  1861. +#include "yaffs_tagscompat.h"
  1862. +#include "yaffs_tagsmarshall.h"
  1863. +#include "yaffs_nand.h"
  1864. +#include "yaffs_yaffs1.h"
  1865. +#include "yaffs_yaffs2.h"
  1866. +#include "yaffs_bitmap.h"
  1867. +#include "yaffs_verify.h"
  1868. +#include "yaffs_nand.h"
  1869. +#include "yaffs_packedtags2.h"
  1870. +#include "yaffs_nameval.h"
  1871. +#include "yaffs_allocator.h"
  1872. +#include "yaffs_attribs.h"
  1873. +#include "yaffs_summary.h"
  1874. +
  1875. +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
  1876. +#define YAFFS_GC_GOOD_ENOUGH 2
  1877. +#define YAFFS_GC_PASSIVE_THRESHOLD 4
  1878. +
  1879. +#include "yaffs_ecc.h"
  1880. +
  1881. +/* Forward declarations */
  1882. +
  1883. +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  1884. + const u8 *buffer, int n_bytes, int use_reserve);
  1885. +
  1886. +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
  1887. + int buffer_size);
  1888. +
  1889. +/* Function to calculate chunk and offset */
  1890. +
  1891. +void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  1892. + int *chunk_out, u32 *offset_out)
  1893. +{
  1894. + int chunk;
  1895. + u32 offset;
  1896. +
  1897. + chunk = (u32) (addr >> dev->chunk_shift);
  1898. +
  1899. + if (dev->chunk_div == 1) {
  1900. + /* easy power of 2 case */
  1901. + offset = (u32) (addr & dev->chunk_mask);
  1902. + } else {
  1903. + /* Non power-of-2 case */
  1904. +
  1905. + loff_t chunk_base;
  1906. +
  1907. + chunk /= dev->chunk_div;
  1908. +
  1909. + chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  1910. + offset = (u32) (addr - chunk_base);
  1911. + }
  1912. +
  1913. + *chunk_out = chunk;
  1914. + *offset_out = offset;
  1915. +}
  1916. +
  1917. +/* Function to return the number of shifts for a power of 2 greater than or
  1918. + * equal to the given number
  1919. + * Note we don't try to cater for all possible numbers and this does not have to
  1920. + * be hellishly efficient.
  1921. + */
  1922. +
  1923. +static inline u32 calc_shifts_ceiling(u32 x)
  1924. +{
  1925. + int extra_bits;
  1926. + int shifts;
  1927. +
  1928. + shifts = extra_bits = 0;
  1929. +
  1930. + while (x > 1) {
  1931. + if (x & 1)
  1932. + extra_bits++;
  1933. + x >>= 1;
  1934. + shifts++;
  1935. + }
  1936. +
  1937. + if (extra_bits)
  1938. + shifts++;
  1939. +
  1940. + return shifts;
  1941. +}
  1942. +
  1943. +/* Function to return the number of shifts to get a 1 in bit 0
  1944. + */
  1945. +
  1946. +static inline u32 calc_shifts(u32 x)
  1947. +{
  1948. + u32 shifts;
  1949. +
  1950. + shifts = 0;
  1951. +
  1952. + if (!x)
  1953. + return 0;
  1954. +
  1955. + while (!(x & 1)) {
  1956. + x >>= 1;
  1957. + shifts++;
  1958. + }
  1959. +
  1960. + return shifts;
  1961. +}
  1962. +
  1963. +/*
  1964. + * Temporary buffer manipulations.
  1965. + */
  1966. +
  1967. +static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  1968. +{
  1969. + int i;
  1970. + u8 *buf = (u8 *) 1;
  1971. +
  1972. + memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  1973. +
  1974. + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  1975. + dev->temp_buffer[i].in_use = 0;
  1976. + buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  1977. + dev->temp_buffer[i].buffer = buf;
  1978. + }
  1979. +
  1980. + return buf ? YAFFS_OK : YAFFS_FAIL;
  1981. +}
  1982. +
  1983. +u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
  1984. +{
  1985. + int i;
  1986. +
  1987. + dev->temp_in_use++;
  1988. + if (dev->temp_in_use > dev->max_temp)
  1989. + dev->max_temp = dev->temp_in_use;
  1990. +
  1991. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  1992. + if (dev->temp_buffer[i].in_use == 0) {
  1993. + dev->temp_buffer[i].in_use = 1;
  1994. + return dev->temp_buffer[i].buffer;
  1995. + }
  1996. + }
  1997. +
  1998. + yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
  1999. + /*
  2000. + * If we got here then we have to allocate an unmanaged one
  2001. + * This is not good.
  2002. + */
  2003. +
  2004. + dev->unmanaged_buffer_allocs++;
  2005. + return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
  2006. +
  2007. +}
  2008. +
  2009. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
  2010. +{
  2011. + int i;
  2012. +
  2013. + dev->temp_in_use--;
  2014. +
  2015. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  2016. + if (dev->temp_buffer[i].buffer == buffer) {
  2017. + dev->temp_buffer[i].in_use = 0;
  2018. + return;
  2019. + }
  2020. + }
  2021. +
  2022. + if (buffer) {
  2023. + /* assume it is an unmanaged one. */
  2024. + yaffs_trace(YAFFS_TRACE_BUFFERS,
  2025. + "Releasing unmanaged temp buffer");
  2026. + kfree(buffer);
  2027. + dev->unmanaged_buffer_deallocs++;
  2028. + }
  2029. +
  2030. +}
  2031. +
  2032. +/*
  2033. + * Functions for robustisizing TODO
  2034. + *
  2035. + */
  2036. +
  2037. +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  2038. + const u8 *data,
  2039. + const struct yaffs_ext_tags *tags)
  2040. +{
  2041. + (void) dev;
  2042. + (void) nand_chunk;
  2043. + (void) data;
  2044. + (void) tags;
  2045. +}
  2046. +
  2047. +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  2048. + const struct yaffs_ext_tags *tags)
  2049. +{
  2050. + (void) dev;
  2051. + (void) nand_chunk;
  2052. + (void) tags;
  2053. +}
  2054. +
  2055. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  2056. + struct yaffs_block_info *bi)
  2057. +{
  2058. + if (!bi->gc_prioritise) {
  2059. + bi->gc_prioritise = 1;
  2060. + dev->has_pending_prioritised_gc = 1;
  2061. + bi->chunk_error_strikes++;
  2062. +
  2063. + if (bi->chunk_error_strikes > 3) {
  2064. + bi->needs_retiring = 1; /* Too many stikes, so retire */
  2065. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  2066. + "yaffs: Block struck out");
  2067. +
  2068. + }
  2069. + }
  2070. +}
  2071. +
  2072. +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  2073. + int erased_ok)
  2074. +{
  2075. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  2076. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2077. +
  2078. + yaffs_handle_chunk_error(dev, bi);
  2079. +
  2080. + if (erased_ok) {
  2081. + /* Was an actual write failure,
  2082. + * so mark the block for retirement.*/
  2083. + bi->needs_retiring = 1;
  2084. + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  2085. + "**>> Block %d needs retiring", flash_block);
  2086. + }
  2087. +
  2088. + /* Delete the chunk */
  2089. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  2090. + yaffs_skip_rest_of_block(dev);
  2091. +}
  2092. +
  2093. +/*
  2094. + * Verification code
  2095. + */
  2096. +
  2097. +/*
  2098. + * Simple hash function. Needs to have a reasonable spread
  2099. + */
  2100. +
  2101. +static inline int yaffs_hash_fn(int n)
  2102. +{
  2103. + if (n < 0)
  2104. + n = -n;
  2105. + return n % YAFFS_NOBJECT_BUCKETS;
  2106. +}
  2107. +
  2108. +/*
  2109. + * Access functions to useful fake objects.
  2110. + * Note that root might have a presence in NAND if permissions are set.
  2111. + */
  2112. +
  2113. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  2114. +{
  2115. + return dev->root_dir;
  2116. +}
  2117. +
  2118. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  2119. +{
  2120. + return dev->lost_n_found;
  2121. +}
  2122. +
  2123. +/*
  2124. + * Erased NAND checking functions
  2125. + */
  2126. +
  2127. +int yaffs_check_ff(u8 *buffer, int n_bytes)
  2128. +{
  2129. + /* Horrible, slow implementation */
  2130. + while (n_bytes--) {
  2131. + if (*buffer != 0xff)
  2132. + return 0;
  2133. + buffer++;
  2134. + }
  2135. + return 1;
  2136. +}
  2137. +
  2138. +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  2139. +{
  2140. + int retval = YAFFS_OK;
  2141. + u8 *data = yaffs_get_temp_buffer(dev);
  2142. + struct yaffs_ext_tags tags;
  2143. + int result;
  2144. +
  2145. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  2146. +
  2147. + if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  2148. + retval = YAFFS_FAIL;
  2149. +
  2150. + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
  2151. + tags.chunk_used) {
  2152. + yaffs_trace(YAFFS_TRACE_NANDACCESS,
  2153. + "Chunk %d not erased", nand_chunk);
  2154. + retval = YAFFS_FAIL;
  2155. + }
  2156. +
  2157. + yaffs_release_temp_buffer(dev, data);
  2158. +
  2159. + return retval;
  2160. +
  2161. +}
  2162. +
  2163. +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  2164. + int nand_chunk,
  2165. + const u8 *data,
  2166. + struct yaffs_ext_tags *tags)
  2167. +{
  2168. + int retval = YAFFS_OK;
  2169. + struct yaffs_ext_tags temp_tags;
  2170. + u8 *buffer = yaffs_get_temp_buffer(dev);
  2171. + int result;
  2172. +
  2173. + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  2174. + if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  2175. + temp_tags.obj_id != tags->obj_id ||
  2176. + temp_tags.chunk_id != tags->chunk_id ||
  2177. + temp_tags.n_bytes != tags->n_bytes)
  2178. + retval = YAFFS_FAIL;
  2179. +
  2180. + yaffs_release_temp_buffer(dev, buffer);
  2181. +
  2182. + return retval;
  2183. +}
  2184. +
  2185. +
  2186. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  2187. +{
  2188. + int reserved_chunks;
  2189. + int reserved_blocks = dev->param.n_reserved_blocks;
  2190. + int checkpt_blocks;
  2191. +
  2192. + checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  2193. +
  2194. + reserved_chunks =
  2195. + (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
  2196. +
  2197. + return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  2198. +}
  2199. +
  2200. +static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  2201. +{
  2202. + int i;
  2203. + struct yaffs_block_info *bi;
  2204. +
  2205. + if (dev->n_erased_blocks < 1) {
  2206. + /* Hoosterman we've got a problem.
  2207. + * Can't get space to gc
  2208. + */
  2209. + yaffs_trace(YAFFS_TRACE_ERROR,
  2210. + "yaffs tragedy: no more erased blocks");
  2211. +
  2212. + return -1;
  2213. + }
  2214. +
  2215. + /* Find an empty block. */
  2216. +
  2217. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  2218. + dev->alloc_block_finder++;
  2219. + if (dev->alloc_block_finder < dev->internal_start_block
  2220. + || dev->alloc_block_finder > dev->internal_end_block) {
  2221. + dev->alloc_block_finder = dev->internal_start_block;
  2222. + }
  2223. +
  2224. + bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  2225. +
  2226. + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  2227. + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  2228. + dev->seq_number++;
  2229. + bi->seq_number = dev->seq_number;
  2230. + dev->n_erased_blocks--;
  2231. + yaffs_trace(YAFFS_TRACE_ALLOCATE,
  2232. + "Allocated block %d, seq %d, %d left" ,
  2233. + dev->alloc_block_finder, dev->seq_number,
  2234. + dev->n_erased_blocks);
  2235. + return dev->alloc_block_finder;
  2236. + }
  2237. + }
  2238. +
  2239. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  2240. + "yaffs tragedy: no more erased blocks, but there should have been %d",
  2241. + dev->n_erased_blocks);
  2242. +
  2243. + return -1;
  2244. +}
  2245. +
  2246. +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  2247. + struct yaffs_block_info **block_ptr)
  2248. +{
  2249. + int ret_val;
  2250. + struct yaffs_block_info *bi;
  2251. +
  2252. + if (dev->alloc_block < 0) {
  2253. + /* Get next block to allocate off */
  2254. + dev->alloc_block = yaffs_find_alloc_block(dev);
  2255. + dev->alloc_page = 0;
  2256. + }
  2257. +
  2258. + if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  2259. + /* No space unless we're allowed to use the reserve. */
  2260. + return -1;
  2261. + }
  2262. +
  2263. + if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  2264. + && dev->alloc_page == 0)
  2265. + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
  2266. +
  2267. + /* Next page please.... */
  2268. + if (dev->alloc_block >= 0) {
  2269. + bi = yaffs_get_block_info(dev, dev->alloc_block);
  2270. +
  2271. + ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  2272. + dev->alloc_page;
  2273. + bi->pages_in_use++;
  2274. + yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  2275. +
  2276. + dev->alloc_page++;
  2277. +
  2278. + dev->n_free_chunks--;
  2279. +
  2280. + /* If the block is full set the state to full */
  2281. + if (dev->alloc_page >= dev->param.chunks_per_block) {
  2282. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  2283. + dev->alloc_block = -1;
  2284. + }
  2285. +
  2286. + if (block_ptr)
  2287. + *block_ptr = bi;
  2288. +
  2289. + return ret_val;
  2290. + }
  2291. +
  2292. + yaffs_trace(YAFFS_TRACE_ERROR,
  2293. + "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
  2294. +
  2295. + return -1;
  2296. +}
  2297. +
  2298. +static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  2299. +{
  2300. + int n;
  2301. +
  2302. + n = dev->n_erased_blocks * dev->param.chunks_per_block;
  2303. +
  2304. + if (dev->alloc_block > 0)
  2305. + n += (dev->param.chunks_per_block - dev->alloc_page);
  2306. +
  2307. + return n;
  2308. +
  2309. +}
  2310. +
  2311. +/*
  2312. + * yaffs_skip_rest_of_block() skips over the rest of the allocation block
  2313. + * if we don't want to write to it.
  2314. + */
  2315. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  2316. +{
  2317. + struct yaffs_block_info *bi;
  2318. +
  2319. + if (dev->alloc_block > 0) {
  2320. + bi = yaffs_get_block_info(dev, dev->alloc_block);
  2321. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  2322. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  2323. + dev->alloc_block = -1;
  2324. + }
  2325. + }
  2326. +}
  2327. +
  2328. +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  2329. + const u8 *data,
  2330. + struct yaffs_ext_tags *tags, int use_reserver)
  2331. +{
  2332. + int attempts = 0;
  2333. + int write_ok = 0;
  2334. + int chunk;
  2335. +
  2336. + yaffs2_checkpt_invalidate(dev);
  2337. +
  2338. + do {
  2339. + struct yaffs_block_info *bi = 0;
  2340. + int erased_ok = 0;
  2341. +
  2342. + chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  2343. + if (chunk < 0) {
  2344. + /* no space */
  2345. + break;
  2346. + }
  2347. +
  2348. + /* First check this chunk is erased, if it needs
  2349. + * checking. The checking policy (unless forced
  2350. + * always on) is as follows:
  2351. + *
  2352. + * Check the first page we try to write in a block.
  2353. + * If the check passes then we don't need to check any
  2354. + * more. If the check fails, we check again...
  2355. + * If the block has been erased, we don't need to check.
  2356. + *
  2357. + * However, if the block has been prioritised for gc,
  2358. + * then we think there might be something odd about
  2359. + * this block and stop using it.
  2360. + *
  2361. + * Rationale: We should only ever see chunks that have
  2362. + * not been erased if there was a partially written
  2363. + * chunk due to power loss. This checking policy should
  2364. + * catch that case with very few checks and thus save a
  2365. + * lot of checks that are most likely not needed.
  2366. + *
  2367. + * Mods to the above
  2368. + * If an erase check fails or the write fails we skip the
  2369. + * rest of the block.
  2370. + */
  2371. +
  2372. + /* let's give it a try */
  2373. + attempts++;
  2374. +
  2375. + if (dev->param.always_check_erased)
  2376. + bi->skip_erased_check = 0;
  2377. +
  2378. + if (!bi->skip_erased_check) {
  2379. + erased_ok = yaffs_check_chunk_erased(dev, chunk);
  2380. + if (erased_ok != YAFFS_OK) {
  2381. + yaffs_trace(YAFFS_TRACE_ERROR,
  2382. + "**>> yaffs chunk %d was not erased",
  2383. + chunk);
  2384. +
  2385. + /* If not erased, delete this one,
  2386. + * skip rest of block and
  2387. + * try another chunk */
  2388. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  2389. + yaffs_skip_rest_of_block(dev);
  2390. + continue;
  2391. + }
  2392. + }
  2393. +
  2394. + write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  2395. +
  2396. + if (!bi->skip_erased_check)
  2397. + write_ok =
  2398. + yaffs_verify_chunk_written(dev, chunk, data, tags);
  2399. +
  2400. + if (write_ok != YAFFS_OK) {
  2401. + /* Clean up aborted write, skip to next block and
  2402. + * try another chunk */
  2403. + yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  2404. + continue;
  2405. + }
  2406. +
  2407. + bi->skip_erased_check = 1;
  2408. +
  2409. + /* Copy the data into the robustification buffer */
  2410. + yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  2411. +
  2412. + } while (write_ok != YAFFS_OK &&
  2413. + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  2414. +
  2415. + if (!write_ok)
  2416. + chunk = -1;
  2417. +
  2418. + if (attempts > 1) {
  2419. + yaffs_trace(YAFFS_TRACE_ERROR,
  2420. + "**>> yaffs write required %d attempts",
  2421. + attempts);
  2422. + dev->n_retried_writes += (attempts - 1);
  2423. + }
  2424. +
  2425. + return chunk;
  2426. +}
  2427. +
  2428. +/*
  2429. + * Block retiring for handling a broken block.
  2430. + */
  2431. +
  2432. +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  2433. +{
  2434. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  2435. +
  2436. + yaffs2_checkpt_invalidate(dev);
  2437. +
  2438. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  2439. +
  2440. + if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  2441. + if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  2442. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  2443. + "yaffs: Failed to mark bad and erase block %d",
  2444. + flash_block);
  2445. + } else {
  2446. + struct yaffs_ext_tags tags;
  2447. + int chunk_id =
  2448. + flash_block * dev->param.chunks_per_block;
  2449. +
  2450. + u8 *buffer = yaffs_get_temp_buffer(dev);
  2451. +
  2452. + memset(buffer, 0xff, dev->data_bytes_per_chunk);
  2453. + memset(&tags, 0, sizeof(tags));
  2454. + tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  2455. + if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
  2456. + dev->chunk_offset,
  2457. + buffer,
  2458. + &tags) != YAFFS_OK)
  2459. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  2460. + "yaffs: Failed to write bad block marker to block %d",
  2461. + flash_block);
  2462. +
  2463. + yaffs_release_temp_buffer(dev, buffer);
  2464. + }
  2465. + }
  2466. +
  2467. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  2468. + bi->gc_prioritise = 0;
  2469. + bi->needs_retiring = 0;
  2470. +
  2471. + dev->n_retired_blocks++;
  2472. +}
  2473. +
  2474. +/*---------------- Name handling functions ------------*/
  2475. +
  2476. +static u16 yaffs_calc_name_sum(const YCHAR *name)
  2477. +{
  2478. + u16 sum = 0;
  2479. + u16 i = 1;
  2480. +
  2481. + if (!name)
  2482. + return 0;
  2483. +
  2484. + while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
  2485. +
  2486. + /* 0x1f mask is case insensitive */
  2487. + sum += ((*name) & 0x1f) * i;
  2488. + i++;
  2489. + name++;
  2490. + }
  2491. + return sum;
  2492. +}
  2493. +
  2494. +
  2495. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  2496. +{
  2497. + memset(obj->short_name, 0, sizeof(obj->short_name));
  2498. +
  2499. + if (name && !name[0]) {
  2500. + yaffs_fix_null_name(obj, obj->short_name,
  2501. + YAFFS_SHORT_NAME_LENGTH);
  2502. + name = obj->short_name;
  2503. + } else if (name &&
  2504. + strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  2505. + YAFFS_SHORT_NAME_LENGTH) {
  2506. + strcpy(obj->short_name, name);
  2507. + }
  2508. +
  2509. + obj->sum = yaffs_calc_name_sum(name);
  2510. +}
  2511. +
  2512. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  2513. + const struct yaffs_obj_hdr *oh)
  2514. +{
  2515. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  2516. + YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  2517. + memset(tmp_name, 0, sizeof(tmp_name));
  2518. + yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  2519. + YAFFS_MAX_NAME_LENGTH + 1);
  2520. + yaffs_set_obj_name(obj, tmp_name);
  2521. +#else
  2522. + yaffs_set_obj_name(obj, oh->name);
  2523. +#endif
  2524. +}
  2525. +
  2526. +loff_t yaffs_max_file_size(struct yaffs_dev *dev)
  2527. +{
  2528. + if(sizeof(loff_t) < 8)
  2529. + return YAFFS_MAX_FILE_SIZE_32;
  2530. + else
  2531. + return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
  2532. +}
  2533. +
  2534. +/*-------------------- TNODES -------------------
  2535. +
  2536. + * List of spare tnodes
  2537. + * The list is hooked together using the first pointer
  2538. + * in the tnode.
  2539. + */
  2540. +
  2541. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  2542. +{
  2543. + struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  2544. +
  2545. + if (tn) {
  2546. + memset(tn, 0, dev->tnode_size);
  2547. + dev->n_tnodes++;
  2548. + }
  2549. +
  2550. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2551. +
  2552. + return tn;
  2553. +}
  2554. +
  2555. +/* FreeTnode frees up a tnode and puts it back on the free list */
  2556. +static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  2557. +{
  2558. + yaffs_free_raw_tnode(dev, tn);
  2559. + dev->n_tnodes--;
  2560. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  2561. +}
  2562. +
  2563. +static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  2564. +{
  2565. + yaffs_deinit_raw_tnodes_and_objs(dev);
  2566. + dev->n_obj = 0;
  2567. + dev->n_tnodes = 0;
  2568. +}
  2569. +
  2570. +static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2571. + unsigned pos, unsigned val)
  2572. +{
  2573. + u32 *map = (u32 *) tn;
  2574. + u32 bit_in_map;
  2575. + u32 bit_in_word;
  2576. + u32 word_in_map;
  2577. + u32 mask;
  2578. +
  2579. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2580. + val >>= dev->chunk_grp_bits;
  2581. +
  2582. + bit_in_map = pos * dev->tnode_width;
  2583. + word_in_map = bit_in_map / 32;
  2584. + bit_in_word = bit_in_map & (32 - 1);
  2585. +
  2586. + mask = dev->tnode_mask << bit_in_word;
  2587. +
  2588. + map[word_in_map] &= ~mask;
  2589. + map[word_in_map] |= (mask & (val << bit_in_word));
  2590. +
  2591. + if (dev->tnode_width > (32 - bit_in_word)) {
  2592. + bit_in_word = (32 - bit_in_word);
  2593. + word_in_map++;
  2594. + mask =
  2595. + dev->tnode_mask >> bit_in_word;
  2596. + map[word_in_map] &= ~mask;
  2597. + map[word_in_map] |= (mask & (val >> bit_in_word));
  2598. + }
  2599. +}
  2600. +
  2601. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  2602. + unsigned pos)
  2603. +{
  2604. + u32 *map = (u32 *) tn;
  2605. + u32 bit_in_map;
  2606. + u32 bit_in_word;
  2607. + u32 word_in_map;
  2608. + u32 val;
  2609. +
  2610. + pos &= YAFFS_TNODES_LEVEL0_MASK;
  2611. +
  2612. + bit_in_map = pos * dev->tnode_width;
  2613. + word_in_map = bit_in_map / 32;
  2614. + bit_in_word = bit_in_map & (32 - 1);
  2615. +
  2616. + val = map[word_in_map] >> bit_in_word;
  2617. +
  2618. + if (dev->tnode_width > (32 - bit_in_word)) {
  2619. + bit_in_word = (32 - bit_in_word);
  2620. + word_in_map++;
  2621. + val |= (map[word_in_map] << bit_in_word);
  2622. + }
  2623. +
  2624. + val &= dev->tnode_mask;
  2625. + val <<= dev->chunk_grp_bits;
  2626. +
  2627. + return val;
  2628. +}
  2629. +
  2630. +/* ------------------- End of individual tnode manipulation -----------------*/
  2631. +
  2632. +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  2633. + * The look up tree is represented by the top tnode and the number of top_level
  2634. + * in the tree. 0 means only the level 0 tnode is in the tree.
  2635. + */
  2636. +
  2637. +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  2638. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  2639. + struct yaffs_file_var *file_struct,
  2640. + u32 chunk_id)
  2641. +{
  2642. + struct yaffs_tnode *tn = file_struct->top;
  2643. + u32 i;
  2644. + int required_depth;
  2645. + int level = file_struct->top_level;
  2646. +
  2647. + (void) dev;
  2648. +
  2649. + /* Check sane level and chunk Id */
  2650. + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  2651. + return NULL;
  2652. +
  2653. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2654. + return NULL;
  2655. +
  2656. + /* First check we're tall enough (ie enough top_level) */
  2657. +
  2658. + i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2659. + required_depth = 0;
  2660. + while (i) {
  2661. + i >>= YAFFS_TNODES_INTERNAL_BITS;
  2662. + required_depth++;
  2663. + }
  2664. +
  2665. + if (required_depth > file_struct->top_level)
  2666. + return NULL; /* Not tall enough, so we can't find it */
  2667. +
  2668. + /* Traverse down to level 0 */
  2669. + while (level > 0 && tn) {
  2670. + tn = tn->internal[(chunk_id >>
  2671. + (YAFFS_TNODES_LEVEL0_BITS +
  2672. + (level - 1) *
  2673. + YAFFS_TNODES_INTERNAL_BITS)) &
  2674. + YAFFS_TNODES_INTERNAL_MASK];
  2675. + level--;
  2676. + }
  2677. +
  2678. + return tn;
  2679. +}
  2680. +
  2681. +/* add_find_tnode_0 finds the level 0 tnode if it exists,
  2682. + * otherwise first expands the tree.
  2683. + * This happens in two steps:
  2684. + * 1. If the tree isn't tall enough, then make it taller.
  2685. + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  2686. + *
  2687. + * Used when modifying the tree.
  2688. + *
  2689. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the
  2690. + * specified tn will be plugged into the ttree.
  2691. + */
  2692. +
  2693. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  2694. + struct yaffs_file_var *file_struct,
  2695. + u32 chunk_id,
  2696. + struct yaffs_tnode *passed_tn)
  2697. +{
  2698. + int required_depth;
  2699. + int i;
  2700. + int l;
  2701. + struct yaffs_tnode *tn;
  2702. + u32 x;
  2703. +
  2704. + /* Check sane level and page Id */
  2705. + if (file_struct->top_level < 0 ||
  2706. + file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  2707. + return NULL;
  2708. +
  2709. + if (chunk_id > YAFFS_MAX_CHUNK_ID)
  2710. + return NULL;
  2711. +
  2712. + /* First check we're tall enough (ie enough top_level) */
  2713. +
  2714. + x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  2715. + required_depth = 0;
  2716. + while (x) {
  2717. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  2718. + required_depth++;
  2719. + }
  2720. +
  2721. + if (required_depth > file_struct->top_level) {
  2722. + /* Not tall enough, gotta make the tree taller */
  2723. + for (i = file_struct->top_level; i < required_depth; i++) {
  2724. +
  2725. + tn = yaffs_get_tnode(dev);
  2726. +
  2727. + if (tn) {
  2728. + tn->internal[0] = file_struct->top;
  2729. + file_struct->top = tn;
  2730. + file_struct->top_level++;
  2731. + } else {
  2732. + yaffs_trace(YAFFS_TRACE_ERROR,
  2733. + "yaffs: no more tnodes");
  2734. + return NULL;
  2735. + }
  2736. + }
  2737. + }
  2738. +
  2739. + /* Traverse down to level 0, adding anything we need */
  2740. +
  2741. + l = file_struct->top_level;
  2742. + tn = file_struct->top;
  2743. +
  2744. + if (l > 0) {
  2745. + while (l > 0 && tn) {
  2746. + x = (chunk_id >>
  2747. + (YAFFS_TNODES_LEVEL0_BITS +
  2748. + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  2749. + YAFFS_TNODES_INTERNAL_MASK;
  2750. +
  2751. + if ((l > 1) && !tn->internal[x]) {
  2752. + /* Add missing non-level-zero tnode */
  2753. + tn->internal[x] = yaffs_get_tnode(dev);
  2754. + if (!tn->internal[x])
  2755. + return NULL;
  2756. + } else if (l == 1) {
  2757. + /* Looking from level 1 at level 0 */
  2758. + if (passed_tn) {
  2759. + /* If we already have one, release it */
  2760. + if (tn->internal[x])
  2761. + yaffs_free_tnode(dev,
  2762. + tn->internal[x]);
  2763. + tn->internal[x] = passed_tn;
  2764. +
  2765. + } else if (!tn->internal[x]) {
  2766. + /* Don't have one, none passed in */
  2767. + tn->internal[x] = yaffs_get_tnode(dev);
  2768. + if (!tn->internal[x])
  2769. + return NULL;
  2770. + }
  2771. + }
  2772. +
  2773. + tn = tn->internal[x];
  2774. + l--;
  2775. + }
  2776. + } else {
  2777. + /* top is level 0 */
  2778. + if (passed_tn) {
  2779. + memcpy(tn, passed_tn,
  2780. + (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  2781. + yaffs_free_tnode(dev, passed_tn);
  2782. + }
  2783. + }
  2784. +
  2785. + return tn;
  2786. +}
  2787. +
  2788. +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  2789. + int chunk_obj)
  2790. +{
  2791. + return (tags->chunk_id == chunk_obj &&
  2792. + tags->obj_id == obj_id &&
  2793. + !tags->is_deleted) ? 1 : 0;
  2794. +
  2795. +}
  2796. +
  2797. +static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  2798. + struct yaffs_ext_tags *tags, int obj_id,
  2799. + int inode_chunk)
  2800. +{
  2801. + int j;
  2802. +
  2803. + for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  2804. + if (yaffs_check_chunk_bit
  2805. + (dev, the_chunk / dev->param.chunks_per_block,
  2806. + the_chunk % dev->param.chunks_per_block)) {
  2807. +
  2808. + if (dev->chunk_grp_size == 1)
  2809. + return the_chunk;
  2810. + else {
  2811. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  2812. + tags);
  2813. + if (yaffs_tags_match(tags,
  2814. + obj_id, inode_chunk)) {
  2815. + /* found it; */
  2816. + return the_chunk;
  2817. + }
  2818. + }
  2819. + }
  2820. + the_chunk++;
  2821. + }
  2822. + return -1;
  2823. +}
  2824. +
  2825. +int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  2826. + struct yaffs_ext_tags *tags)
  2827. +{
  2828. + /*Get the Tnode, then get the level 0 offset chunk offset */
  2829. + struct yaffs_tnode *tn;
  2830. + int the_chunk = -1;
  2831. + struct yaffs_ext_tags local_tags;
  2832. + int ret_val = -1;
  2833. + struct yaffs_dev *dev = in->my_dev;
  2834. +
  2835. + if (!tags) {
  2836. + /* Passed a NULL, so use our own tags space */
  2837. + tags = &local_tags;
  2838. + }
  2839. +
  2840. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  2841. +
  2842. + if (!tn)
  2843. + return ret_val;
  2844. +
  2845. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  2846. +
  2847. + ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  2848. + inode_chunk);
  2849. + return ret_val;
  2850. +}
  2851. +
  2852. +static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  2853. + struct yaffs_ext_tags *tags)
  2854. +{
  2855. + /* Get the Tnode, then get the level 0 offset chunk offset */
  2856. + struct yaffs_tnode *tn;
  2857. + int the_chunk = -1;
  2858. + struct yaffs_ext_tags local_tags;
  2859. + struct yaffs_dev *dev = in->my_dev;
  2860. + int ret_val = -1;
  2861. +
  2862. + if (!tags) {
  2863. + /* Passed a NULL, so use our own tags space */
  2864. + tags = &local_tags;
  2865. + }
  2866. +
  2867. + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  2868. +
  2869. + if (!tn)
  2870. + return ret_val;
  2871. +
  2872. + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  2873. +
  2874. + ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  2875. + inode_chunk);
  2876. +
  2877. + /* Delete the entry in the filestructure (if found) */
  2878. + if (ret_val != -1)
  2879. + yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  2880. +
  2881. + return ret_val;
  2882. +}
  2883. +
  2884. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  2885. + int nand_chunk, int in_scan)
  2886. +{
  2887. + /* NB in_scan is zero unless scanning.
  2888. + * For forward scanning, in_scan is > 0;
  2889. + * for backward scanning in_scan is < 0
  2890. + *
  2891. + * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
  2892. + */
  2893. +
  2894. + struct yaffs_tnode *tn;
  2895. + struct yaffs_dev *dev = in->my_dev;
  2896. + int existing_cunk;
  2897. + struct yaffs_ext_tags existing_tags;
  2898. + struct yaffs_ext_tags new_tags;
  2899. + unsigned existing_serial, new_serial;
  2900. +
  2901. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  2902. + /* Just ignore an attempt at putting a chunk into a non-file
  2903. + * during scanning.
  2904. + * If it is not during Scanning then something went wrong!
  2905. + */
  2906. + if (!in_scan) {
  2907. + yaffs_trace(YAFFS_TRACE_ERROR,
  2908. + "yaffs tragedy:attempt to put data chunk into a non-file"
  2909. + );
  2910. + BUG();
  2911. + }
  2912. +
  2913. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  2914. + return YAFFS_OK;
  2915. + }
  2916. +
  2917. + tn = yaffs_add_find_tnode_0(dev,
  2918. + &in->variant.file_variant,
  2919. + inode_chunk, NULL);
  2920. + if (!tn)
  2921. + return YAFFS_FAIL;
  2922. +
  2923. + if (!nand_chunk)
  2924. + /* Dummy insert, bail now */
  2925. + return YAFFS_OK;
  2926. +
  2927. + existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  2928. +
  2929. + if (in_scan != 0) {
  2930. + /* If we're scanning then we need to test for duplicates
  2931. + * NB This does not need to be efficient since it should only
  2932. + * happen when the power fails during a write, then only one
  2933. + * chunk should ever be affected.
  2934. + *
  2935. + * Correction for YAFFS2: This could happen quite a lot and we
  2936. + * need to think about efficiency! TODO
  2937. + * Update: For backward scanning we don't need to re-read tags
  2938. + * so this is quite cheap.
  2939. + */
  2940. +
  2941. + if (existing_cunk > 0) {
  2942. + /* NB Right now existing chunk will not be real
  2943. + * chunk_id if the chunk group size > 1
  2944. + * thus we have to do a FindChunkInFile to get the
  2945. + * real chunk id.
  2946. + *
  2947. + * We have a duplicate now we need to decide which
  2948. + * one to use:
  2949. + *
  2950. + * Backwards scanning YAFFS2: The old one is what
  2951. + * we use, dump the new one.
  2952. + * YAFFS1: Get both sets of tags and compare serial
  2953. + * numbers.
  2954. + */
  2955. +
  2956. + if (in_scan > 0) {
  2957. + /* Only do this for forward scanning */
  2958. + yaffs_rd_chunk_tags_nand(dev,
  2959. + nand_chunk,
  2960. + NULL, &new_tags);
  2961. +
  2962. + /* Do a proper find */
  2963. + existing_cunk =
  2964. + yaffs_find_chunk_in_file(in, inode_chunk,
  2965. + &existing_tags);
  2966. + }
  2967. +
  2968. + if (existing_cunk <= 0) {
  2969. + /*Hoosterman - how did this happen? */
  2970. +
  2971. + yaffs_trace(YAFFS_TRACE_ERROR,
  2972. + "yaffs tragedy: existing chunk < 0 in scan"
  2973. + );
  2974. +
  2975. + }
  2976. +
  2977. + /* NB The deleted flags should be false, otherwise
  2978. + * the chunks will not be loaded during a scan
  2979. + */
  2980. +
  2981. + if (in_scan > 0) {
  2982. + new_serial = new_tags.serial_number;
  2983. + existing_serial = existing_tags.serial_number;
  2984. + }
  2985. +
  2986. + if ((in_scan > 0) &&
  2987. + (existing_cunk <= 0 ||
  2988. + ((existing_serial + 1) & 3) == new_serial)) {
  2989. + /* Forward scanning.
  2990. + * Use new
  2991. + * Delete the old one and drop through to
  2992. + * update the tnode
  2993. + */
  2994. + yaffs_chunk_del(dev, existing_cunk, 1,
  2995. + __LINE__);
  2996. + } else {
  2997. + /* Backward scanning or we want to use the
  2998. + * existing one
  2999. + * Delete the new one and return early so that
  3000. + * the tnode isn't changed
  3001. + */
  3002. + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  3003. + return YAFFS_OK;
  3004. + }
  3005. + }
  3006. +
  3007. + }
  3008. +
  3009. + if (existing_cunk == 0)
  3010. + in->n_data_chunks++;
  3011. +
  3012. + yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  3013. +
  3014. + return YAFFS_OK;
  3015. +}
  3016. +
  3017. +static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  3018. +{
  3019. + struct yaffs_block_info *the_block;
  3020. + unsigned block_no;
  3021. +
  3022. + yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
  3023. +
  3024. + block_no = chunk / dev->param.chunks_per_block;
  3025. + the_block = yaffs_get_block_info(dev, block_no);
  3026. + if (the_block) {
  3027. + the_block->soft_del_pages++;
  3028. + dev->n_free_chunks++;
  3029. + yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  3030. + }
  3031. +}
  3032. +
  3033. +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
  3034. + * the chunks in the file.
  3035. + * All soft deleting does is increment the block's softdelete count and pulls
  3036. + * the chunk out of the tnode.
  3037. + * Thus, essentially this is the same as DeleteWorker except that the chunks
  3038. + * are soft deleted.
  3039. + */
  3040. +
  3041. +static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  3042. + u32 level, int chunk_offset)
  3043. +{
  3044. + int i;
  3045. + int the_chunk;
  3046. + int all_done = 1;
  3047. + struct yaffs_dev *dev = in->my_dev;
  3048. +
  3049. + if (!tn)
  3050. + return 1;
  3051. +
  3052. + if (level > 0) {
  3053. + for (i = YAFFS_NTNODES_INTERNAL - 1;
  3054. + all_done && i >= 0;
  3055. + i--) {
  3056. + if (tn->internal[i]) {
  3057. + all_done =
  3058. + yaffs_soft_del_worker(in,
  3059. + tn->internal[i],
  3060. + level - 1,
  3061. + (chunk_offset <<
  3062. + YAFFS_TNODES_INTERNAL_BITS)
  3063. + + i);
  3064. + if (all_done) {
  3065. + yaffs_free_tnode(dev,
  3066. + tn->internal[i]);
  3067. + tn->internal[i] = NULL;
  3068. + } else {
  3069. + /* Can this happen? */
  3070. + }
  3071. + }
  3072. + }
  3073. + return (all_done) ? 1 : 0;
  3074. + }
  3075. +
  3076. + /* level 0 */
  3077. + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  3078. + the_chunk = yaffs_get_group_base(dev, tn, i);
  3079. + if (the_chunk) {
  3080. + yaffs_soft_del_chunk(dev, the_chunk);
  3081. + yaffs_load_tnode_0(dev, tn, i, 0);
  3082. + }
  3083. + }
  3084. + return 1;
  3085. +}
  3086. +
  3087. +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  3088. +{
  3089. + struct yaffs_dev *dev = obj->my_dev;
  3090. + struct yaffs_obj *parent;
  3091. +
  3092. + yaffs_verify_obj_in_dir(obj);
  3093. + parent = obj->parent;
  3094. +
  3095. + yaffs_verify_dir(parent);
  3096. +
  3097. + if (dev && dev->param.remove_obj_fn)
  3098. + dev->param.remove_obj_fn(obj);
  3099. +
  3100. + list_del_init(&obj->siblings);
  3101. + obj->parent = NULL;
  3102. +
  3103. + yaffs_verify_dir(parent);
  3104. +}
  3105. +
  3106. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  3107. +{
  3108. + if (!directory) {
  3109. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  3110. + "tragedy: Trying to add an object to a null pointer directory"
  3111. + );
  3112. + BUG();
  3113. + return;
  3114. + }
  3115. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3116. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  3117. + "tragedy: Trying to add an object to a non-directory"
  3118. + );
  3119. + BUG();
  3120. + }
  3121. +
  3122. + if (obj->siblings.prev == NULL) {
  3123. + /* Not initialised */
  3124. + BUG();
  3125. + }
  3126. +
  3127. + yaffs_verify_dir(directory);
  3128. +
  3129. + yaffs_remove_obj_from_dir(obj);
  3130. +
  3131. + /* Now add it */
  3132. + list_add(&obj->siblings, &directory->variant.dir_variant.children);
  3133. + obj->parent = directory;
  3134. +
  3135. + if (directory == obj->my_dev->unlinked_dir
  3136. + || directory == obj->my_dev->del_dir) {
  3137. + obj->unlinked = 1;
  3138. + obj->my_dev->n_unlinked_files++;
  3139. + obj->rename_allowed = 0;
  3140. + }
  3141. +
  3142. + yaffs_verify_dir(directory);
  3143. + yaffs_verify_obj_in_dir(obj);
  3144. +}
  3145. +
  3146. +static int yaffs_change_obj_name(struct yaffs_obj *obj,
  3147. + struct yaffs_obj *new_dir,
  3148. + const YCHAR *new_name, int force, int shadows)
  3149. +{
  3150. + int unlink_op;
  3151. + int del_op;
  3152. + struct yaffs_obj *existing_target;
  3153. +
  3154. + if (new_dir == NULL)
  3155. + new_dir = obj->parent; /* use the old directory */
  3156. +
  3157. + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3158. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  3159. + "tragedy: yaffs_change_obj_name: new_dir is not a directory"
  3160. + );
  3161. + BUG();
  3162. + }
  3163. +
  3164. + unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  3165. + del_op = (new_dir == obj->my_dev->del_dir);
  3166. +
  3167. + existing_target = yaffs_find_by_name(new_dir, new_name);
  3168. +
  3169. + /* If the object is a file going into the unlinked directory,
  3170. + * then it is OK to just stuff it in since duplicate names are OK.
  3171. + * else only proceed if the new name does not exist and we're putting
  3172. + * it into a directory.
  3173. + */
  3174. + if (!(unlink_op || del_op || force ||
  3175. + shadows > 0 || !existing_target) ||
  3176. + new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3177. + return YAFFS_FAIL;
  3178. +
  3179. + yaffs_set_obj_name(obj, new_name);
  3180. + obj->dirty = 1;
  3181. + yaffs_add_obj_to_dir(new_dir, obj);
  3182. +
  3183. + if (unlink_op)
  3184. + obj->unlinked = 1;
  3185. +
  3186. + /* If it is a deletion then we mark it as a shrink for gc */
  3187. + if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
  3188. + return YAFFS_OK;
  3189. +
  3190. + return YAFFS_FAIL;
  3191. +}
  3192. +
  3193. +/*------------------------ Short Operations Cache ------------------------------
  3194. + * In many situations where there is no high level buffering a lot of
  3195. + * reads might be short sequential reads, and a lot of writes may be short
  3196. + * sequential writes. eg. scanning/writing a jpeg file.
  3197. + * In these cases, a short read/write cache can provide a huge perfomance
  3198. + * benefit with dumb-as-a-rock code.
  3199. + * In Linux, the page cache provides read buffering and the short op cache
  3200. + * provides write buffering.
  3201. + *
  3202. + * There are a small number (~10) of cache chunks per device so that we don't
  3203. + * need a very intelligent search.
  3204. + */
  3205. +
  3206. +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  3207. +{
  3208. + struct yaffs_dev *dev = obj->my_dev;
  3209. + int i;
  3210. + struct yaffs_cache *cache;
  3211. + int n_caches = obj->my_dev->param.n_caches;
  3212. +
  3213. + for (i = 0; i < n_caches; i++) {
  3214. + cache = &dev->cache[i];
  3215. + if (cache->object == obj && cache->dirty)
  3216. + return 1;
  3217. + }
  3218. +
  3219. + return 0;
  3220. +}
  3221. +
  3222. +static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  3223. +{
  3224. + struct yaffs_dev *dev = obj->my_dev;
  3225. + int lowest = -99; /* Stop compiler whining. */
  3226. + int i;
  3227. + struct yaffs_cache *cache;
  3228. + int chunk_written = 0;
  3229. + int n_caches = obj->my_dev->param.n_caches;
  3230. +
  3231. + if (n_caches < 1)
  3232. + return;
  3233. + do {
  3234. + cache = NULL;
  3235. +
  3236. + /* Find the lowest dirty chunk for this object */
  3237. + for (i = 0; i < n_caches; i++) {
  3238. + if (dev->cache[i].object == obj &&
  3239. + dev->cache[i].dirty) {
  3240. + if (!cache ||
  3241. + dev->cache[i].chunk_id < lowest) {
  3242. + cache = &dev->cache[i];
  3243. + lowest = cache->chunk_id;
  3244. + }
  3245. + }
  3246. + }
  3247. +
  3248. + if (cache && !cache->locked) {
  3249. + /* Write it out and free it up */
  3250. + chunk_written =
  3251. + yaffs_wr_data_obj(cache->object,
  3252. + cache->chunk_id,
  3253. + cache->data,
  3254. + cache->n_bytes, 1);
  3255. + cache->dirty = 0;
  3256. + cache->object = NULL;
  3257. + }
  3258. + } while (cache && chunk_written > 0);
  3259. +
  3260. + if (cache)
  3261. + /* Hoosterman, disk full while writing cache out. */
  3262. + yaffs_trace(YAFFS_TRACE_ERROR,
  3263. + "yaffs tragedy: no space during cache write");
  3264. +}
  3265. +
  3266. +/*yaffs_flush_whole_cache(dev)
  3267. + *
  3268. + *
  3269. + */
  3270. +
  3271. +void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  3272. +{
  3273. + struct yaffs_obj *obj;
  3274. + int n_caches = dev->param.n_caches;
  3275. + int i;
  3276. +
  3277. + /* Find a dirty object in the cache and flush it...
  3278. + * until there are no further dirty objects.
  3279. + */
  3280. + do {
  3281. + obj = NULL;
  3282. + for (i = 0; i < n_caches && !obj; i++) {
  3283. + if (dev->cache[i].object && dev->cache[i].dirty)
  3284. + obj = dev->cache[i].object;
  3285. + }
  3286. + if (obj)
  3287. + yaffs_flush_file_cache(obj);
  3288. + } while (obj);
  3289. +
  3290. +}
  3291. +
  3292. +/* Grab us a cache chunk for use.
  3293. + * First look for an empty one.
  3294. + * Then look for the least recently used non-dirty one.
  3295. + * Then look for the least recently used dirty one...., flush and look again.
  3296. + */
  3297. +static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  3298. +{
  3299. + int i;
  3300. +
  3301. + if (dev->param.n_caches > 0) {
  3302. + for (i = 0; i < dev->param.n_caches; i++) {
  3303. + if (!dev->cache[i].object)
  3304. + return &dev->cache[i];
  3305. + }
  3306. + }
  3307. + return NULL;
  3308. +}
  3309. +
  3310. +static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  3311. +{
  3312. + struct yaffs_cache *cache;
  3313. + struct yaffs_obj *the_obj;
  3314. + int usage;
  3315. + int i;
  3316. + int pushout;
  3317. +
  3318. + if (dev->param.n_caches < 1)
  3319. + return NULL;
  3320. +
  3321. + /* Try find a non-dirty one... */
  3322. +
  3323. + cache = yaffs_grab_chunk_worker(dev);
  3324. +
  3325. + if (!cache) {
  3326. + /* They were all dirty, find the LRU object and flush
  3327. + * its cache, then find again.
  3328. + * NB what's here is not very accurate,
  3329. + * we actually flush the object with the LRU chunk.
  3330. + */
  3331. +
  3332. + /* With locking we can't assume we can use entry zero,
  3333. + * Set the_obj to a valid pointer for Coverity. */
  3334. + the_obj = dev->cache[0].object;
  3335. + usage = -1;
  3336. + cache = NULL;
  3337. + pushout = -1;
  3338. +
  3339. + for (i = 0; i < dev->param.n_caches; i++) {
  3340. + if (dev->cache[i].object &&
  3341. + !dev->cache[i].locked &&
  3342. + (dev->cache[i].last_use < usage ||
  3343. + !cache)) {
  3344. + usage = dev->cache[i].last_use;
  3345. + the_obj = dev->cache[i].object;
  3346. + cache = &dev->cache[i];
  3347. + pushout = i;
  3348. + }
  3349. + }
  3350. +
  3351. + if (!cache || cache->dirty) {
  3352. + /* Flush and try again */
  3353. + yaffs_flush_file_cache(the_obj);
  3354. + cache = yaffs_grab_chunk_worker(dev);
  3355. + }
  3356. + }
  3357. + return cache;
  3358. +}
  3359. +
  3360. +/* Find a cached chunk */
  3361. +static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  3362. + int chunk_id)
  3363. +{
  3364. + struct yaffs_dev *dev = obj->my_dev;
  3365. + int i;
  3366. +
  3367. + if (dev->param.n_caches < 1)
  3368. + return NULL;
  3369. +
  3370. + for (i = 0; i < dev->param.n_caches; i++) {
  3371. + if (dev->cache[i].object == obj &&
  3372. + dev->cache[i].chunk_id == chunk_id) {
  3373. + dev->cache_hits++;
  3374. +
  3375. + return &dev->cache[i];
  3376. + }
  3377. + }
  3378. + return NULL;
  3379. +}
  3380. +
  3381. +/* Mark the chunk for the least recently used algorithym */
  3382. +static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  3383. + int is_write)
  3384. +{
  3385. + int i;
  3386. +
  3387. + if (dev->param.n_caches < 1)
  3388. + return;
  3389. +
  3390. + if (dev->cache_last_use < 0 ||
  3391. + dev->cache_last_use > 100000000) {
  3392. + /* Reset the cache usages */
  3393. + for (i = 1; i < dev->param.n_caches; i++)
  3394. + dev->cache[i].last_use = 0;
  3395. +
  3396. + dev->cache_last_use = 0;
  3397. + }
  3398. + dev->cache_last_use++;
  3399. + cache->last_use = dev->cache_last_use;
  3400. +
  3401. + if (is_write)
  3402. + cache->dirty = 1;
  3403. +}
  3404. +
  3405. +/* Invalidate a single cache page.
  3406. + * Do this when a whole page gets written,
  3407. + * ie the short cache for this page is no longer valid.
  3408. + */
  3409. +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  3410. +{
  3411. + struct yaffs_cache *cache;
  3412. +
  3413. + if (object->my_dev->param.n_caches > 0) {
  3414. + cache = yaffs_find_chunk_cache(object, chunk_id);
  3415. +
  3416. + if (cache)
  3417. + cache->object = NULL;
  3418. + }
  3419. +}
  3420. +
  3421. +/* Invalidate all the cache pages associated with this object
  3422. + * Do this whenever ther file is deleted or resized.
  3423. + */
  3424. +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  3425. +{
  3426. + int i;
  3427. + struct yaffs_dev *dev = in->my_dev;
  3428. +
  3429. + if (dev->param.n_caches > 0) {
  3430. + /* Invalidate it. */
  3431. + for (i = 0; i < dev->param.n_caches; i++) {
  3432. + if (dev->cache[i].object == in)
  3433. + dev->cache[i].object = NULL;
  3434. + }
  3435. + }
  3436. +}
  3437. +
  3438. +static void yaffs_unhash_obj(struct yaffs_obj *obj)
  3439. +{
  3440. + int bucket;
  3441. + struct yaffs_dev *dev = obj->my_dev;
  3442. +
  3443. + /* If it is still linked into the bucket list, free from the list */
  3444. + if (!list_empty(&obj->hash_link)) {
  3445. + list_del_init(&obj->hash_link);
  3446. + bucket = yaffs_hash_fn(obj->obj_id);
  3447. + dev->obj_bucket[bucket].count--;
  3448. + }
  3449. +}
  3450. +
  3451. +/* FreeObject frees up a Object and puts it back on the free list */
  3452. +static void yaffs_free_obj(struct yaffs_obj *obj)
  3453. +{
  3454. + struct yaffs_dev *dev;
  3455. +
  3456. + if (!obj) {
  3457. + BUG();
  3458. + return;
  3459. + }
  3460. + dev = obj->my_dev;
  3461. + yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
  3462. + obj, obj->my_inode);
  3463. + if (obj->parent)
  3464. + BUG();
  3465. + if (!list_empty(&obj->siblings))
  3466. + BUG();
  3467. +
  3468. + if (obj->my_inode) {
  3469. + /* We're still hooked up to a cached inode.
  3470. + * Don't delete now, but mark for later deletion
  3471. + */
  3472. + obj->defered_free = 1;
  3473. + return;
  3474. + }
  3475. +
  3476. + yaffs_unhash_obj(obj);
  3477. +
  3478. + yaffs_free_raw_obj(dev, obj);
  3479. + dev->n_obj--;
  3480. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3481. +}
  3482. +
  3483. +void yaffs_handle_defered_free(struct yaffs_obj *obj)
  3484. +{
  3485. + if (obj->defered_free)
  3486. + yaffs_free_obj(obj);
  3487. +}
  3488. +
  3489. +static int yaffs_generic_obj_del(struct yaffs_obj *in)
  3490. +{
  3491. + /* Iinvalidate the file's data in the cache, without flushing. */
  3492. + yaffs_invalidate_whole_cache(in);
  3493. +
  3494. + if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
  3495. + /* Move to unlinked directory so we have a deletion record */
  3496. + yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  3497. + 0);
  3498. + }
  3499. +
  3500. + yaffs_remove_obj_from_dir(in);
  3501. + yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  3502. + in->hdr_chunk = 0;
  3503. +
  3504. + yaffs_free_obj(in);
  3505. + return YAFFS_OK;
  3506. +
  3507. +}
  3508. +
  3509. +static void yaffs_soft_del_file(struct yaffs_obj *obj)
  3510. +{
  3511. + if (!obj->deleted ||
  3512. + obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
  3513. + obj->soft_del)
  3514. + return;
  3515. +
  3516. + if (obj->n_data_chunks <= 0) {
  3517. + /* Empty file with no duplicate object headers,
  3518. + * just delete it immediately */
  3519. + yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
  3520. + obj->variant.file_variant.top = NULL;
  3521. + yaffs_trace(YAFFS_TRACE_TRACING,
  3522. + "yaffs: Deleting empty file %d",
  3523. + obj->obj_id);
  3524. + yaffs_generic_obj_del(obj);
  3525. + } else {
  3526. + yaffs_soft_del_worker(obj,
  3527. + obj->variant.file_variant.top,
  3528. + obj->variant.
  3529. + file_variant.top_level, 0);
  3530. + obj->soft_del = 1;
  3531. + }
  3532. +}
  3533. +
  3534. +/* Pruning removes any part of the file structure tree that is beyond the
  3535. + * bounds of the file (ie that does not point to chunks).
  3536. + *
  3537. + * A file should only get pruned when its size is reduced.
  3538. + *
  3539. + * Before pruning, the chunks must be pulled from the tree and the
  3540. + * level 0 tnode entries must be zeroed out.
  3541. + * Could also use this for file deletion, but that's probably better handled
  3542. + * by a special case.
  3543. + *
  3544. + * This function is recursive. For levels > 0 the function is called again on
  3545. + * any sub-tree. For level == 0 we just check if the sub-tree has data.
  3546. + * If there is no data in a subtree then it is pruned.
  3547. + */
  3548. +
  3549. +static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  3550. + struct yaffs_tnode *tn, u32 level,
  3551. + int del0)
  3552. +{
  3553. + int i;
  3554. + int has_data;
  3555. +
  3556. + if (!tn)
  3557. + return tn;
  3558. +
  3559. + has_data = 0;
  3560. +
  3561. + if (level > 0) {
  3562. + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  3563. + if (tn->internal[i]) {
  3564. + tn->internal[i] =
  3565. + yaffs_prune_worker(dev,
  3566. + tn->internal[i],
  3567. + level - 1,
  3568. + (i == 0) ? del0 : 1);
  3569. + }
  3570. +
  3571. + if (tn->internal[i])
  3572. + has_data++;
  3573. + }
  3574. + } else {
  3575. + int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  3576. + u32 *map = (u32 *) tn;
  3577. +
  3578. + for (i = 0; !has_data && i < tnode_size_u32; i++) {
  3579. + if (map[i])
  3580. + has_data++;
  3581. + }
  3582. + }
  3583. +
  3584. + if (has_data == 0 && del0) {
  3585. + /* Free and return NULL */
  3586. + yaffs_free_tnode(dev, tn);
  3587. + tn = NULL;
  3588. + }
  3589. + return tn;
  3590. +}
  3591. +
  3592. +static int yaffs_prune_tree(struct yaffs_dev *dev,
  3593. + struct yaffs_file_var *file_struct)
  3594. +{
  3595. + int i;
  3596. + int has_data;
  3597. + int done = 0;
  3598. + struct yaffs_tnode *tn;
  3599. +
  3600. + if (file_struct->top_level < 1)
  3601. + return YAFFS_OK;
  3602. +
  3603. + file_struct->top =
  3604. + yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
  3605. +
  3606. + /* Now we have a tree with all the non-zero branches NULL but
  3607. + * the height is the same as it was.
  3608. + * Let's see if we can trim internal tnodes to shorten the tree.
  3609. + * We can do this if only the 0th element in the tnode is in use
  3610. + * (ie all the non-zero are NULL)
  3611. + */
  3612. +
  3613. + while (file_struct->top_level && !done) {
  3614. + tn = file_struct->top;
  3615. +
  3616. + has_data = 0;
  3617. + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  3618. + if (tn->internal[i])
  3619. + has_data++;
  3620. + }
  3621. +
  3622. + if (!has_data) {
  3623. + file_struct->top = tn->internal[0];
  3624. + file_struct->top_level--;
  3625. + yaffs_free_tnode(dev, tn);
  3626. + } else {
  3627. + done = 1;
  3628. + }
  3629. + }
  3630. +
  3631. + return YAFFS_OK;
  3632. +}
  3633. +
  3634. +/*-------------------- End of File Structure functions.-------------------*/
  3635. +
  3636. +/* alloc_empty_obj gets us a clean Object.*/
  3637. +static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  3638. +{
  3639. + struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  3640. +
  3641. + if (!obj)
  3642. + return obj;
  3643. +
  3644. + dev->n_obj++;
  3645. +
  3646. + /* Now sweeten it up... */
  3647. +
  3648. + memset(obj, 0, sizeof(struct yaffs_obj));
  3649. + obj->being_created = 1;
  3650. +
  3651. + obj->my_dev = dev;
  3652. + obj->hdr_chunk = 0;
  3653. + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  3654. + INIT_LIST_HEAD(&(obj->hard_links));
  3655. + INIT_LIST_HEAD(&(obj->hash_link));
  3656. + INIT_LIST_HEAD(&obj->siblings);
  3657. +
  3658. + /* Now make the directory sane */
  3659. + if (dev->root_dir) {
  3660. + obj->parent = dev->root_dir;
  3661. + list_add(&(obj->siblings),
  3662. + &dev->root_dir->variant.dir_variant.children);
  3663. + }
  3664. +
  3665. + /* Add it to the lost and found directory.
  3666. + * NB Can't put root or lost-n-found in lost-n-found so
  3667. + * check if lost-n-found exists first
  3668. + */
  3669. + if (dev->lost_n_found)
  3670. + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  3671. +
  3672. + obj->being_created = 0;
  3673. +
  3674. + dev->checkpoint_blocks_required = 0; /* force recalculation */
  3675. +
  3676. + return obj;
  3677. +}
  3678. +
  3679. +static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  3680. +{
  3681. + int i;
  3682. + int l = 999;
  3683. + int lowest = 999999;
  3684. +
  3685. + /* Search for the shortest list or one that
  3686. + * isn't too long.
  3687. + */
  3688. +
  3689. + for (i = 0; i < 10 && lowest > 4; i++) {
  3690. + dev->bucket_finder++;
  3691. + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  3692. + if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  3693. + lowest = dev->obj_bucket[dev->bucket_finder].count;
  3694. + l = dev->bucket_finder;
  3695. + }
  3696. + }
  3697. +
  3698. + return l;
  3699. +}
  3700. +
  3701. +static int yaffs_new_obj_id(struct yaffs_dev *dev)
  3702. +{
  3703. + int bucket = yaffs_find_nice_bucket(dev);
  3704. + int found = 0;
  3705. + struct list_head *i;
  3706. + u32 n = (u32) bucket;
  3707. +
  3708. + /* Now find an object value that has not already been taken
  3709. + * by scanning the list.
  3710. + */
  3711. +
  3712. + while (!found) {
  3713. + found = 1;
  3714. + n += YAFFS_NOBJECT_BUCKETS;
  3715. + if (1 || dev->obj_bucket[bucket].count > 0) {
  3716. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3717. + /* If there is already one in the list */
  3718. + if (i && list_entry(i, struct yaffs_obj,
  3719. + hash_link)->obj_id == n) {
  3720. + found = 0;
  3721. + }
  3722. + }
  3723. + }
  3724. + }
  3725. + return n;
  3726. +}
  3727. +
  3728. +static void yaffs_hash_obj(struct yaffs_obj *in)
  3729. +{
  3730. + int bucket = yaffs_hash_fn(in->obj_id);
  3731. + struct yaffs_dev *dev = in->my_dev;
  3732. +
  3733. + list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  3734. + dev->obj_bucket[bucket].count++;
  3735. +}
  3736. +
  3737. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  3738. +{
  3739. + int bucket = yaffs_hash_fn(number);
  3740. + struct list_head *i;
  3741. + struct yaffs_obj *in;
  3742. +
  3743. + list_for_each(i, &dev->obj_bucket[bucket].list) {
  3744. + /* Look if it is in the list */
  3745. + in = list_entry(i, struct yaffs_obj, hash_link);
  3746. + if (in->obj_id == number) {
  3747. + /* Don't show if it is defered free */
  3748. + if (in->defered_free)
  3749. + return NULL;
  3750. + return in;
  3751. + }
  3752. + }
  3753. +
  3754. + return NULL;
  3755. +}
  3756. +
  3757. +static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  3758. + enum yaffs_obj_type type)
  3759. +{
  3760. + struct yaffs_obj *the_obj = NULL;
  3761. + struct yaffs_tnode *tn = NULL;
  3762. +
  3763. + if (number < 0)
  3764. + number = yaffs_new_obj_id(dev);
  3765. +
  3766. + if (type == YAFFS_OBJECT_TYPE_FILE) {
  3767. + tn = yaffs_get_tnode(dev);
  3768. + if (!tn)
  3769. + return NULL;
  3770. + }
  3771. +
  3772. + the_obj = yaffs_alloc_empty_obj(dev);
  3773. + if (!the_obj) {
  3774. + if (tn)
  3775. + yaffs_free_tnode(dev, tn);
  3776. + return NULL;
  3777. + }
  3778. +
  3779. + the_obj->fake = 0;
  3780. + the_obj->rename_allowed = 1;
  3781. + the_obj->unlink_allowed = 1;
  3782. + the_obj->obj_id = number;
  3783. + yaffs_hash_obj(the_obj);
  3784. + the_obj->variant_type = type;
  3785. + yaffs_load_current_time(the_obj, 1, 1);
  3786. +
  3787. + switch (type) {
  3788. + case YAFFS_OBJECT_TYPE_FILE:
  3789. + the_obj->variant.file_variant.file_size = 0;
  3790. + the_obj->variant.file_variant.scanned_size = 0;
  3791. + the_obj->variant.file_variant.shrink_size =
  3792. + yaffs_max_file_size(dev);
  3793. + the_obj->variant.file_variant.top_level = 0;
  3794. + the_obj->variant.file_variant.top = tn;
  3795. + break;
  3796. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  3797. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  3798. + INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  3799. + break;
  3800. + case YAFFS_OBJECT_TYPE_SYMLINK:
  3801. + case YAFFS_OBJECT_TYPE_HARDLINK:
  3802. + case YAFFS_OBJECT_TYPE_SPECIAL:
  3803. + /* No action required */
  3804. + break;
  3805. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  3806. + /* todo this should not happen */
  3807. + break;
  3808. + }
  3809. + return the_obj;
  3810. +}
  3811. +
  3812. +static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  3813. + int number, u32 mode)
  3814. +{
  3815. +
  3816. + struct yaffs_obj *obj =
  3817. + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  3818. +
  3819. + if (!obj)
  3820. + return NULL;
  3821. +
  3822. + obj->fake = 1; /* it is fake so it might not use NAND */
  3823. + obj->rename_allowed = 0;
  3824. + obj->unlink_allowed = 0;
  3825. + obj->deleted = 0;
  3826. + obj->unlinked = 0;
  3827. + obj->yst_mode = mode;
  3828. + obj->my_dev = dev;
  3829. + obj->hdr_chunk = 0; /* Not a valid chunk. */
  3830. + return obj;
  3831. +
  3832. +}
  3833. +
  3834. +
  3835. +static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  3836. +{
  3837. + int i;
  3838. +
  3839. + dev->n_obj = 0;
  3840. + dev->n_tnodes = 0;
  3841. + yaffs_init_raw_tnodes_and_objs(dev);
  3842. +
  3843. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  3844. + INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  3845. + dev->obj_bucket[i].count = 0;
  3846. + }
  3847. +}
  3848. +
  3849. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  3850. + int number,
  3851. + enum yaffs_obj_type type)
  3852. +{
  3853. + struct yaffs_obj *the_obj = NULL;
  3854. +
  3855. + if (number > 0)
  3856. + the_obj = yaffs_find_by_number(dev, number);
  3857. +
  3858. + if (!the_obj)
  3859. + the_obj = yaffs_new_obj(dev, number, type);
  3860. +
  3861. + return the_obj;
  3862. +
  3863. +}
  3864. +
  3865. +YCHAR *yaffs_clone_str(const YCHAR *str)
  3866. +{
  3867. + YCHAR *new_str = NULL;
  3868. + int len;
  3869. +
  3870. + if (!str)
  3871. + str = _Y("");
  3872. +
  3873. + len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  3874. + new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
  3875. + if (new_str) {
  3876. + strncpy(new_str, str, len);
  3877. + new_str[len] = 0;
  3878. + }
  3879. + return new_str;
  3880. +
  3881. +}
  3882. +/*
  3883. + *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
  3884. + * link (ie. name) is created or deleted in the directory.
  3885. + *
  3886. + * ie.
  3887. + * create dir/a : update dir's mtime/ctime
  3888. + * rm dir/a: update dir's mtime/ctime
  3889. + * modify dir/a: don't update dir's mtimme/ctime
  3890. + *
  3891. + * This can be handled immediately or defered. Defering helps reduce the number
  3892. + * of updates when many files in a directory are changed within a brief period.
  3893. + *
  3894. + * If the directory updating is defered then yaffs_update_dirty_dirs must be
  3895. + * called periodically.
  3896. + */
  3897. +
  3898. +static void yaffs_update_parent(struct yaffs_obj *obj)
  3899. +{
  3900. + struct yaffs_dev *dev;
  3901. +
  3902. + if (!obj)
  3903. + return;
  3904. + dev = obj->my_dev;
  3905. + obj->dirty = 1;
  3906. + yaffs_load_current_time(obj, 0, 1);
  3907. + if (dev->param.defered_dir_update) {
  3908. + struct list_head *link = &obj->variant.dir_variant.dirty;
  3909. +
  3910. + if (list_empty(link)) {
  3911. + list_add(link, &dev->dirty_dirs);
  3912. + yaffs_trace(YAFFS_TRACE_BACKGROUND,
  3913. + "Added object %d to dirty directories",
  3914. + obj->obj_id);
  3915. + }
  3916. +
  3917. + } else {
  3918. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  3919. + }
  3920. +}
  3921. +
  3922. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  3923. +{
  3924. + struct list_head *link;
  3925. + struct yaffs_obj *obj;
  3926. + struct yaffs_dir_var *d_s;
  3927. + union yaffs_obj_var *o_v;
  3928. +
  3929. + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
  3930. +
  3931. + while (!list_empty(&dev->dirty_dirs)) {
  3932. + link = dev->dirty_dirs.next;
  3933. + list_del_init(link);
  3934. +
  3935. + d_s = list_entry(link, struct yaffs_dir_var, dirty);
  3936. + o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  3937. + obj = list_entry(o_v, struct yaffs_obj, variant);
  3938. +
  3939. + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
  3940. + obj->obj_id);
  3941. +
  3942. + if (obj->dirty)
  3943. + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  3944. + }
  3945. +}
  3946. +
  3947. +/*
  3948. + * Mknod (create) a new object.
  3949. + * equiv_obj only has meaning for a hard link;
  3950. + * alias_str only has meaning for a symlink.
  3951. + * rdev only has meaning for devices (a subset of special objects)
  3952. + */
  3953. +
  3954. +static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  3955. + struct yaffs_obj *parent,
  3956. + const YCHAR *name,
  3957. + u32 mode,
  3958. + u32 uid,
  3959. + u32 gid,
  3960. + struct yaffs_obj *equiv_obj,
  3961. + const YCHAR *alias_str, u32 rdev)
  3962. +{
  3963. + struct yaffs_obj *in;
  3964. + YCHAR *str = NULL;
  3965. + struct yaffs_dev *dev = parent->my_dev;
  3966. +
  3967. + /* Check if the entry exists.
  3968. + * If it does then fail the call since we don't want a dup. */
  3969. + if (yaffs_find_by_name(parent, name))
  3970. + return NULL;
  3971. +
  3972. + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  3973. + str = yaffs_clone_str(alias_str);
  3974. + if (!str)
  3975. + return NULL;
  3976. + }
  3977. +
  3978. + in = yaffs_new_obj(dev, -1, type);
  3979. +
  3980. + if (!in) {
  3981. + kfree(str);
  3982. + return NULL;
  3983. + }
  3984. +
  3985. + in->hdr_chunk = 0;
  3986. + in->valid = 1;
  3987. + in->variant_type = type;
  3988. +
  3989. + in->yst_mode = mode;
  3990. +
  3991. + yaffs_attribs_init(in, gid, uid, rdev);
  3992. +
  3993. + in->n_data_chunks = 0;
  3994. +
  3995. + yaffs_set_obj_name(in, name);
  3996. + in->dirty = 1;
  3997. +
  3998. + yaffs_add_obj_to_dir(parent, in);
  3999. +
  4000. + in->my_dev = parent->my_dev;
  4001. +
  4002. + switch (type) {
  4003. + case YAFFS_OBJECT_TYPE_SYMLINK:
  4004. + in->variant.symlink_variant.alias = str;
  4005. + break;
  4006. + case YAFFS_OBJECT_TYPE_HARDLINK:
  4007. + in->variant.hardlink_variant.equiv_obj = equiv_obj;
  4008. + in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
  4009. + list_add(&in->hard_links, &equiv_obj->hard_links);
  4010. + break;
  4011. + case YAFFS_OBJECT_TYPE_FILE:
  4012. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  4013. + case YAFFS_OBJECT_TYPE_SPECIAL:
  4014. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  4015. + /* do nothing */
  4016. + break;
  4017. + }
  4018. +
  4019. + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  4020. + /* Could not create the object header, fail */
  4021. + yaffs_del_obj(in);
  4022. + in = NULL;
  4023. + }
  4024. +
  4025. + if (in)
  4026. + yaffs_update_parent(parent);
  4027. +
  4028. + return in;
  4029. +}
  4030. +
  4031. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  4032. + const YCHAR *name, u32 mode, u32 uid,
  4033. + u32 gid)
  4034. +{
  4035. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  4036. + uid, gid, NULL, NULL, 0);
  4037. +}
  4038. +
  4039. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
  4040. + u32 mode, u32 uid, u32 gid)
  4041. +{
  4042. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  4043. + mode, uid, gid, NULL, NULL, 0);
  4044. +}
  4045. +
  4046. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  4047. + const YCHAR *name, u32 mode, u32 uid,
  4048. + u32 gid, u32 rdev)
  4049. +{
  4050. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  4051. + uid, gid, NULL, NULL, rdev);
  4052. +}
  4053. +
  4054. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  4055. + const YCHAR *name, u32 mode, u32 uid,
  4056. + u32 gid, const YCHAR *alias)
  4057. +{
  4058. + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  4059. + uid, gid, NULL, alias, 0);
  4060. +}
  4061. +
  4062. +/* yaffs_link_obj returns the object id of the equivalent object.*/
  4063. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  4064. + struct yaffs_obj *equiv_obj)
  4065. +{
  4066. + /* Get the real object in case we were fed a hard link obj */
  4067. + equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  4068. +
  4069. + if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
  4070. + parent, name, 0, 0, 0,
  4071. + equiv_obj, NULL, 0))
  4072. + return equiv_obj;
  4073. +
  4074. + return NULL;
  4075. +
  4076. +}
  4077. +
  4078. +
  4079. +
  4080. +/*---------------------- Block Management and Page Allocation -------------*/
  4081. +
  4082. +static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  4083. +{
  4084. + if (dev->block_info_alt && dev->block_info)
  4085. + vfree(dev->block_info);
  4086. + else
  4087. + kfree(dev->block_info);
  4088. +
  4089. + dev->block_info_alt = 0;
  4090. +
  4091. + dev->block_info = NULL;
  4092. +
  4093. + if (dev->chunk_bits_alt && dev->chunk_bits)
  4094. + vfree(dev->chunk_bits);
  4095. + else
  4096. + kfree(dev->chunk_bits);
  4097. + dev->chunk_bits_alt = 0;
  4098. + dev->chunk_bits = NULL;
  4099. +}
  4100. +
  4101. +static int yaffs_init_blocks(struct yaffs_dev *dev)
  4102. +{
  4103. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  4104. +
  4105. + dev->block_info = NULL;
  4106. + dev->chunk_bits = NULL;
  4107. + dev->alloc_block = -1; /* force it to get a new one */
  4108. +
  4109. + /* If the first allocation strategy fails, thry the alternate one */
  4110. + dev->block_info =
  4111. + kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
  4112. + if (!dev->block_info) {
  4113. + dev->block_info =
  4114. + vmalloc(n_blocks * sizeof(struct yaffs_block_info));
  4115. + dev->block_info_alt = 1;
  4116. + } else {
  4117. + dev->block_info_alt = 0;
  4118. + }
  4119. +
  4120. + if (!dev->block_info)
  4121. + goto alloc_error;
  4122. +
  4123. + /* Set up dynamic blockinfo stuff. Round up bytes. */
  4124. + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
  4125. + dev->chunk_bits =
  4126. + kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
  4127. + if (!dev->chunk_bits) {
  4128. + dev->chunk_bits =
  4129. + vmalloc(dev->chunk_bit_stride * n_blocks);
  4130. + dev->chunk_bits_alt = 1;
  4131. + } else {
  4132. + dev->chunk_bits_alt = 0;
  4133. + }
  4134. + if (!dev->chunk_bits)
  4135. + goto alloc_error;
  4136. +
  4137. +
  4138. + memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
  4139. + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  4140. + return YAFFS_OK;
  4141. +
  4142. +alloc_error:
  4143. + yaffs_deinit_blocks(dev);
  4144. + return YAFFS_FAIL;
  4145. +}
  4146. +
  4147. +
  4148. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  4149. +{
  4150. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  4151. + int erased_ok = 0;
  4152. + int i;
  4153. +
  4154. + /* If the block is still healthy erase it and mark as clean.
  4155. + * If the block has had a data failure, then retire it.
  4156. + */
  4157. +
  4158. + yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  4159. + "yaffs_block_became_dirty block %d state %d %s",
  4160. + block_no, bi->block_state,
  4161. + (bi->needs_retiring) ? "needs retiring" : "");
  4162. +
  4163. + yaffs2_clear_oldest_dirty_seq(dev, bi);
  4164. +
  4165. + bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  4166. +
  4167. + /* If this is the block being garbage collected then stop gc'ing */
  4168. + if (block_no == dev->gc_block)
  4169. + dev->gc_block = 0;
  4170. +
  4171. + /* If this block is currently the best candidate for gc
  4172. + * then drop as a candidate */
  4173. + if (block_no == dev->gc_dirtiest) {
  4174. + dev->gc_dirtiest = 0;
  4175. + dev->gc_pages_in_use = 0;
  4176. + }
  4177. +
  4178. + if (!bi->needs_retiring) {
  4179. + yaffs2_checkpt_invalidate(dev);
  4180. + erased_ok = yaffs_erase_block(dev, block_no);
  4181. + if (!erased_ok) {
  4182. + dev->n_erase_failures++;
  4183. + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  4184. + "**>> Erasure failed %d", block_no);
  4185. + }
  4186. + }
  4187. +
  4188. + /* Verify erasure if needed */
  4189. + if (erased_ok &&
  4190. + ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
  4191. + !yaffs_skip_verification(dev))) {
  4192. + for (i = 0; i < dev->param.chunks_per_block; i++) {
  4193. + if (!yaffs_check_chunk_erased(dev,
  4194. + block_no * dev->param.chunks_per_block + i)) {
  4195. + yaffs_trace(YAFFS_TRACE_ERROR,
  4196. + ">>Block %d erasure supposedly OK, but chunk %d not erased",
  4197. + block_no, i);
  4198. + }
  4199. + }
  4200. + }
  4201. +
  4202. + if (!erased_ok) {
  4203. + /* We lost a block of free space */
  4204. + dev->n_free_chunks -= dev->param.chunks_per_block;
  4205. + yaffs_retire_block(dev, block_no);
  4206. + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  4207. + "**>> Block %d retired", block_no);
  4208. + return;
  4209. + }
  4210. +
  4211. + /* Clean it up... */
  4212. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  4213. + bi->seq_number = 0;
  4214. + dev->n_erased_blocks++;
  4215. + bi->pages_in_use = 0;
  4216. + bi->soft_del_pages = 0;
  4217. + bi->has_shrink_hdr = 0;
  4218. + bi->skip_erased_check = 1; /* Clean, so no need to check */
  4219. + bi->gc_prioritise = 0;
  4220. + bi->has_summary = 0;
  4221. +
  4222. + yaffs_clear_chunk_bits(dev, block_no);
  4223. +
  4224. + yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
  4225. +}
  4226. +
  4227. +static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
  4228. + struct yaffs_block_info *bi,
  4229. + int old_chunk, u8 *buffer)
  4230. +{
  4231. + int new_chunk;
  4232. + int mark_flash = 1;
  4233. + struct yaffs_ext_tags tags;
  4234. + struct yaffs_obj *object;
  4235. + int matching_chunk;
  4236. + int ret_val = YAFFS_OK;
  4237. +
  4238. + memset(&tags, 0, sizeof(tags));
  4239. + yaffs_rd_chunk_tags_nand(dev, old_chunk,
  4240. + buffer, &tags);
  4241. + object = yaffs_find_by_number(dev, tags.obj_id);
  4242. +
  4243. + yaffs_trace(YAFFS_TRACE_GC_DETAIL,
  4244. + "Collecting chunk in block %d, %d %d %d ",
  4245. + dev->gc_chunk, tags.obj_id,
  4246. + tags.chunk_id, tags.n_bytes);
  4247. +
  4248. + if (object && !yaffs_skip_verification(dev)) {
  4249. + if (tags.chunk_id == 0)
  4250. + matching_chunk =
  4251. + object->hdr_chunk;
  4252. + else if (object->soft_del)
  4253. + /* Defeat the test */
  4254. + matching_chunk = old_chunk;
  4255. + else
  4256. + matching_chunk =
  4257. + yaffs_find_chunk_in_file
  4258. + (object, tags.chunk_id,
  4259. + NULL);
  4260. +
  4261. + if (old_chunk != matching_chunk)
  4262. + yaffs_trace(YAFFS_TRACE_ERROR,
  4263. + "gc: page in gc mismatch: %d %d %d %d",
  4264. + old_chunk,
  4265. + matching_chunk,
  4266. + tags.obj_id,
  4267. + tags.chunk_id);
  4268. + }
  4269. +
  4270. + if (!object) {
  4271. + yaffs_trace(YAFFS_TRACE_ERROR,
  4272. + "page %d in gc has no object: %d %d %d ",
  4273. + old_chunk,
  4274. + tags.obj_id, tags.chunk_id,
  4275. + tags.n_bytes);
  4276. + }
  4277. +
  4278. + if (object &&
  4279. + object->deleted &&
  4280. + object->soft_del && tags.chunk_id != 0) {
  4281. + /* Data chunk in a soft deleted file,
  4282. + * throw it away.
  4283. + * It's a soft deleted data chunk,
  4284. + * No need to copy this, just forget
  4285. + * about it and fix up the object.
  4286. + */
  4287. +
  4288. + /* Free chunks already includes
  4289. + * softdeleted chunks, how ever this
  4290. + * chunk is going to soon be really
  4291. + * deleted which will increment free
  4292. + * chunks. We have to decrement free
  4293. + * chunks so this works out properly.
  4294. + */
  4295. + dev->n_free_chunks--;
  4296. + bi->soft_del_pages--;
  4297. +
  4298. + object->n_data_chunks--;
  4299. + if (object->n_data_chunks <= 0) {
  4300. + /* remeber to clean up obj */
  4301. + dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
  4302. + dev->n_clean_ups++;
  4303. + }
  4304. + mark_flash = 0;
  4305. + } else if (object) {
  4306. + /* It's either a data chunk in a live
  4307. + * file or an ObjectHeader, so we're
  4308. + * interested in it.
  4309. + * NB Need to keep the ObjectHeaders of
  4310. + * deleted files until the whole file
  4311. + * has been deleted off
  4312. + */
  4313. + tags.serial_number++;
  4314. + dev->n_gc_copies++;
  4315. +
  4316. + if (tags.chunk_id == 0) {
  4317. + /* It is an object Id,
  4318. + * We need to nuke the
  4319. + * shrinkheader flags since its
  4320. + * work is done.
  4321. + * Also need to clean up
  4322. + * shadowing.
  4323. + */
  4324. + struct yaffs_obj_hdr *oh;
  4325. + oh = (struct yaffs_obj_hdr *) buffer;
  4326. +
  4327. + oh->is_shrink = 0;
  4328. + tags.extra_is_shrink = 0;
  4329. + oh->shadows_obj = 0;
  4330. + oh->inband_shadowed_obj_id = 0;
  4331. + tags.extra_shadows = 0;
  4332. +
  4333. + /* Update file size */
  4334. + if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  4335. + yaffs_oh_size_load(oh,
  4336. + object->variant.file_variant.file_size);
  4337. + tags.extra_file_size =
  4338. + object->variant.file_variant.file_size;
  4339. + }
  4340. +
  4341. + yaffs_verify_oh(object, oh, &tags, 1);
  4342. + new_chunk =
  4343. + yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
  4344. + } else {
  4345. + new_chunk =
  4346. + yaffs_write_new_chunk(dev, buffer, &tags, 1);
  4347. + }
  4348. +
  4349. + if (new_chunk < 0) {
  4350. + ret_val = YAFFS_FAIL;
  4351. + } else {
  4352. +
  4353. + /* Now fix up the Tnodes etc. */
  4354. +
  4355. + if (tags.chunk_id == 0) {
  4356. + /* It's a header */
  4357. + object->hdr_chunk = new_chunk;
  4358. + object->serial = tags.serial_number;
  4359. + } else {
  4360. + /* It's a data chunk */
  4361. + yaffs_put_chunk_in_file(object, tags.chunk_id,
  4362. + new_chunk, 0);
  4363. + }
  4364. + }
  4365. + }
  4366. + if (ret_val == YAFFS_OK)
  4367. + yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
  4368. + return ret_val;
  4369. +}
  4370. +
  4371. +static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  4372. +{
  4373. + int old_chunk;
  4374. + int ret_val = YAFFS_OK;
  4375. + int i;
  4376. + int is_checkpt_block;
  4377. + int max_copies;
  4378. + int chunks_before = yaffs_get_erased_chunks(dev);
  4379. + int chunks_after;
  4380. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  4381. +
  4382. + is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  4383. +
  4384. + yaffs_trace(YAFFS_TRACE_TRACING,
  4385. + "Collecting block %d, in use %d, shrink %d, whole_block %d",
  4386. + block, bi->pages_in_use, bi->has_shrink_hdr,
  4387. + whole_block);
  4388. +
  4389. + /*yaffs_verify_free_chunks(dev); */
  4390. +
  4391. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  4392. + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  4393. +
  4394. + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
  4395. +
  4396. + dev->gc_disable = 1;
  4397. +
  4398. + yaffs_summary_gc(dev, block);
  4399. +
  4400. + if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  4401. + yaffs_trace(YAFFS_TRACE_TRACING,
  4402. + "Collecting block %d that has no chunks in use",
  4403. + block);
  4404. + yaffs_block_became_dirty(dev, block);
  4405. + } else {
  4406. +
  4407. + u8 *buffer = yaffs_get_temp_buffer(dev);
  4408. +
  4409. + yaffs_verify_blk(dev, bi, block);
  4410. +
  4411. + max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  4412. + old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  4413. +
  4414. + for (/* init already done */ ;
  4415. + ret_val == YAFFS_OK &&
  4416. + dev->gc_chunk < dev->param.chunks_per_block &&
  4417. + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  4418. + max_copies > 0;
  4419. + dev->gc_chunk++, old_chunk++) {
  4420. + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  4421. + /* Page is in use and might need to be copied */
  4422. + max_copies--;
  4423. + ret_val = yaffs_gc_process_chunk(dev, bi,
  4424. + old_chunk, buffer);
  4425. + }
  4426. + }
  4427. + yaffs_release_temp_buffer(dev, buffer);
  4428. + }
  4429. +
  4430. + yaffs_verify_collected_blk(dev, bi, block);
  4431. +
  4432. + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4433. + /*
  4434. + * The gc did not complete. Set block state back to FULL
  4435. + * because checkpointing does not restore gc.
  4436. + */
  4437. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  4438. + } else {
  4439. + /* The gc completed. */
  4440. + /* Do any required cleanups */
  4441. + for (i = 0; i < dev->n_clean_ups; i++) {
  4442. + /* Time to delete the file too */
  4443. + struct yaffs_obj *object =
  4444. + yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
  4445. + if (object) {
  4446. + yaffs_free_tnode(dev,
  4447. + object->variant.file_variant.top);
  4448. + object->variant.file_variant.top = NULL;
  4449. + yaffs_trace(YAFFS_TRACE_GC,
  4450. + "yaffs: About to finally delete object %d",
  4451. + object->obj_id);
  4452. + yaffs_generic_obj_del(object);
  4453. + object->my_dev->n_deleted_files--;
  4454. + }
  4455. +
  4456. + }
  4457. + chunks_after = yaffs_get_erased_chunks(dev);
  4458. + if (chunks_before >= chunks_after)
  4459. + yaffs_trace(YAFFS_TRACE_GC,
  4460. + "gc did not increase free chunks before %d after %d",
  4461. + chunks_before, chunks_after);
  4462. + dev->gc_block = 0;
  4463. + dev->gc_chunk = 0;
  4464. + dev->n_clean_ups = 0;
  4465. + }
  4466. +
  4467. + dev->gc_disable = 0;
  4468. +
  4469. + return ret_val;
  4470. +}
  4471. +
  4472. +/*
  4473. + * find_gc_block() selects the dirtiest block (or close enough)
  4474. + * for garbage collection.
  4475. + */
  4476. +
  4477. +static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
  4478. + int aggressive, int background)
  4479. +{
  4480. + int i;
  4481. + int iterations;
  4482. + unsigned selected = 0;
  4483. + int prioritised = 0;
  4484. + int prioritised_exist = 0;
  4485. + struct yaffs_block_info *bi;
  4486. + int threshold;
  4487. +
  4488. + /* First let's see if we need to grab a prioritised block */
  4489. + if (dev->has_pending_prioritised_gc && !aggressive) {
  4490. + dev->gc_dirtiest = 0;
  4491. + bi = dev->block_info;
  4492. + for (i = dev->internal_start_block;
  4493. + i <= dev->internal_end_block && !selected; i++) {
  4494. +
  4495. + if (bi->gc_prioritise) {
  4496. + prioritised_exist = 1;
  4497. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4498. + yaffs_block_ok_for_gc(dev, bi)) {
  4499. + selected = i;
  4500. + prioritised = 1;
  4501. + }
  4502. + }
  4503. + bi++;
  4504. + }
  4505. +
  4506. + /*
  4507. + * If there is a prioritised block and none was selected then
  4508. + * this happened because there is at least one old dirty block
  4509. + * gumming up the works. Let's gc the oldest dirty block.
  4510. + */
  4511. +
  4512. + if (prioritised_exist &&
  4513. + !selected && dev->oldest_dirty_block > 0)
  4514. + selected = dev->oldest_dirty_block;
  4515. +
  4516. + if (!prioritised_exist) /* None found, so we can clear this */
  4517. + dev->has_pending_prioritised_gc = 0;
  4518. + }
  4519. +
  4520. + /* If we're doing aggressive GC then we are happy to take a less-dirty
  4521. + * block, and search harder.
  4522. + * else (leasurely gc), then we only bother to do this if the
  4523. + * block has only a few pages in use.
  4524. + */
  4525. +
  4526. + if (!selected) {
  4527. + int pages_used;
  4528. + int n_blocks =
  4529. + dev->internal_end_block - dev->internal_start_block + 1;
  4530. + if (aggressive) {
  4531. + threshold = dev->param.chunks_per_block;
  4532. + iterations = n_blocks;
  4533. + } else {
  4534. + int max_threshold;
  4535. +
  4536. + if (background)
  4537. + max_threshold = dev->param.chunks_per_block / 2;
  4538. + else
  4539. + max_threshold = dev->param.chunks_per_block / 8;
  4540. +
  4541. + if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4542. + max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4543. +
  4544. + threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
  4545. + if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  4546. + threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  4547. + if (threshold > max_threshold)
  4548. + threshold = max_threshold;
  4549. +
  4550. + iterations = n_blocks / 16 + 1;
  4551. + if (iterations > 100)
  4552. + iterations = 100;
  4553. + }
  4554. +
  4555. + for (i = 0;
  4556. + i < iterations &&
  4557. + (dev->gc_dirtiest < 1 ||
  4558. + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
  4559. + i++) {
  4560. + dev->gc_block_finder++;
  4561. + if (dev->gc_block_finder < dev->internal_start_block ||
  4562. + dev->gc_block_finder > dev->internal_end_block)
  4563. + dev->gc_block_finder =
  4564. + dev->internal_start_block;
  4565. +
  4566. + bi = yaffs_get_block_info(dev, dev->gc_block_finder);
  4567. +
  4568. + pages_used = bi->pages_in_use - bi->soft_del_pages;
  4569. +
  4570. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  4571. + pages_used < dev->param.chunks_per_block &&
  4572. + (dev->gc_dirtiest < 1 ||
  4573. + pages_used < dev->gc_pages_in_use) &&
  4574. + yaffs_block_ok_for_gc(dev, bi)) {
  4575. + dev->gc_dirtiest = dev->gc_block_finder;
  4576. + dev->gc_pages_in_use = pages_used;
  4577. + }
  4578. + }
  4579. +
  4580. + if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
  4581. + selected = dev->gc_dirtiest;
  4582. + }
  4583. +
  4584. + /*
  4585. + * If nothing has been selected for a while, try the oldest dirty
  4586. + * because that's gumming up the works.
  4587. + */
  4588. +
  4589. + if (!selected && dev->param.is_yaffs2 &&
  4590. + dev->gc_not_done >= (background ? 10 : 20)) {
  4591. + yaffs2_find_oldest_dirty_seq(dev);
  4592. + if (dev->oldest_dirty_block > 0) {
  4593. + selected = dev->oldest_dirty_block;
  4594. + dev->gc_dirtiest = selected;
  4595. + dev->oldest_dirty_gc_count++;
  4596. + bi = yaffs_get_block_info(dev, selected);
  4597. + dev->gc_pages_in_use =
  4598. + bi->pages_in_use - bi->soft_del_pages;
  4599. + } else {
  4600. + dev->gc_not_done = 0;
  4601. + }
  4602. + }
  4603. +
  4604. + if (selected) {
  4605. + yaffs_trace(YAFFS_TRACE_GC,
  4606. + "GC Selected block %d with %d free, prioritised:%d",
  4607. + selected,
  4608. + dev->param.chunks_per_block - dev->gc_pages_in_use,
  4609. + prioritised);
  4610. +
  4611. + dev->n_gc_blocks++;
  4612. + if (background)
  4613. + dev->bg_gcs++;
  4614. +
  4615. + dev->gc_dirtiest = 0;
  4616. + dev->gc_pages_in_use = 0;
  4617. + dev->gc_not_done = 0;
  4618. + if (dev->refresh_skip > 0)
  4619. + dev->refresh_skip--;
  4620. + } else {
  4621. + dev->gc_not_done++;
  4622. + yaffs_trace(YAFFS_TRACE_GC,
  4623. + "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
  4624. + dev->gc_block_finder, dev->gc_not_done, threshold,
  4625. + dev->gc_dirtiest, dev->gc_pages_in_use,
  4626. + dev->oldest_dirty_block, background ? " bg" : "");
  4627. + }
  4628. +
  4629. + return selected;
  4630. +}
  4631. +
  4632. +/* New garbage collector
  4633. + * If we're very low on erased blocks then we do aggressive garbage collection
  4634. + * otherwise we do "leasurely" garbage collection.
  4635. + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  4636. + * Passive gc only inspects smaller areas and only accepts more dirty blocks.
  4637. + *
  4638. + * The idea is to help clear out space in a more spread-out manner.
  4639. + * Dunno if it really does anything useful.
  4640. + */
  4641. +static int yaffs_check_gc(struct yaffs_dev *dev, int background)
  4642. +{
  4643. + int aggressive = 0;
  4644. + int gc_ok = YAFFS_OK;
  4645. + int max_tries = 0;
  4646. + int min_erased;
  4647. + int erased_chunks;
  4648. + int checkpt_block_adjust;
  4649. +
  4650. + if (dev->param.gc_control_fn &&
  4651. + (dev->param.gc_control_fn(dev) & 1) == 0)
  4652. + return YAFFS_OK;
  4653. +
  4654. + if (dev->gc_disable)
  4655. + /* Bail out so we don't get recursive gc */
  4656. + return YAFFS_OK;
  4657. +
  4658. + /* This loop should pass the first time.
  4659. + * Only loops here if the collection does not increase space.
  4660. + */
  4661. +
  4662. + do {
  4663. + max_tries++;
  4664. +
  4665. + checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
  4666. +
  4667. + min_erased =
  4668. + dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
  4669. + erased_chunks =
  4670. + dev->n_erased_blocks * dev->param.chunks_per_block;
  4671. +
  4672. + /* If we need a block soon then do aggressive gc. */
  4673. + if (dev->n_erased_blocks < min_erased)
  4674. + aggressive = 1;
  4675. + else {
  4676. + if (!background
  4677. + && erased_chunks > (dev->n_free_chunks / 4))
  4678. + break;
  4679. +
  4680. + if (dev->gc_skip > 20)
  4681. + dev->gc_skip = 20;
  4682. + if (erased_chunks < dev->n_free_chunks / 2 ||
  4683. + dev->gc_skip < 1 || background)
  4684. + aggressive = 0;
  4685. + else {
  4686. + dev->gc_skip--;
  4687. + break;
  4688. + }
  4689. + }
  4690. +
  4691. + dev->gc_skip = 5;
  4692. +
  4693. + /* If we don't already have a block being gc'd then see if we
  4694. + * should start another */
  4695. +
  4696. + if (dev->gc_block < 1 && !aggressive) {
  4697. + dev->gc_block = yaffs2_find_refresh_block(dev);
  4698. + dev->gc_chunk = 0;
  4699. + dev->n_clean_ups = 0;
  4700. + }
  4701. + if (dev->gc_block < 1) {
  4702. + dev->gc_block =
  4703. + yaffs_find_gc_block(dev, aggressive, background);
  4704. + dev->gc_chunk = 0;
  4705. + dev->n_clean_ups = 0;
  4706. + }
  4707. +
  4708. + if (dev->gc_block > 0) {
  4709. + dev->all_gcs++;
  4710. + if (!aggressive)
  4711. + dev->passive_gc_count++;
  4712. +
  4713. + yaffs_trace(YAFFS_TRACE_GC,
  4714. + "yaffs: GC n_erased_blocks %d aggressive %d",
  4715. + dev->n_erased_blocks, aggressive);
  4716. +
  4717. + gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
  4718. + }
  4719. +
  4720. + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
  4721. + dev->gc_block > 0) {
  4722. + yaffs_trace(YAFFS_TRACE_GC,
  4723. + "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
  4724. + dev->n_erased_blocks, max_tries,
  4725. + dev->gc_block);
  4726. + }
  4727. + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
  4728. + (dev->gc_block > 0) && (max_tries < 2));
  4729. +
  4730. + return aggressive ? gc_ok : YAFFS_OK;
  4731. +}
  4732. +
  4733. +/*
  4734. + * yaffs_bg_gc()
  4735. + * Garbage collects. Intended to be called from a background thread.
  4736. + * Returns non-zero if at least half the free chunks are erased.
  4737. + */
  4738. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
  4739. +{
  4740. + int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
  4741. +
  4742. + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
  4743. +
  4744. + yaffs_check_gc(dev, 1);
  4745. + return erased_chunks > dev->n_free_chunks / 2;
  4746. +}
  4747. +
  4748. +/*-------------------- Data file manipulation -----------------*/
  4749. +
  4750. +static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
  4751. +{
  4752. + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
  4753. +
  4754. + if (nand_chunk >= 0)
  4755. + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
  4756. + buffer, NULL);
  4757. + else {
  4758. + yaffs_trace(YAFFS_TRACE_NANDACCESS,
  4759. + "Chunk %d not found zero instead",
  4760. + nand_chunk);
  4761. + /* get sane (zero) data if you read a hole */
  4762. + memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
  4763. + return 0;
  4764. + }
  4765. +
  4766. +}
  4767. +
  4768. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  4769. + int lyn)
  4770. +{
  4771. + int block;
  4772. + int page;
  4773. + struct yaffs_ext_tags tags;
  4774. + struct yaffs_block_info *bi;
  4775. +
  4776. + if (chunk_id <= 0)
  4777. + return;
  4778. +
  4779. + dev->n_deletions++;
  4780. + block = chunk_id / dev->param.chunks_per_block;
  4781. + page = chunk_id % dev->param.chunks_per_block;
  4782. +
  4783. + if (!yaffs_check_chunk_bit(dev, block, page))
  4784. + yaffs_trace(YAFFS_TRACE_VERIFY,
  4785. + "Deleting invalid chunk %d", chunk_id);
  4786. +
  4787. + bi = yaffs_get_block_info(dev, block);
  4788. +
  4789. + yaffs2_update_oldest_dirty_seq(dev, block, bi);
  4790. +
  4791. + yaffs_trace(YAFFS_TRACE_DELETION,
  4792. + "line %d delete of chunk %d",
  4793. + lyn, chunk_id);
  4794. +
  4795. + if (!dev->param.is_yaffs2 && mark_flash &&
  4796. + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
  4797. +
  4798. + memset(&tags, 0, sizeof(tags));
  4799. + tags.is_deleted = 1;
  4800. + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
  4801. + yaffs_handle_chunk_update(dev, chunk_id, &tags);
  4802. + } else {
  4803. + dev->n_unmarked_deletions++;
  4804. + }
  4805. +
  4806. + /* Pull out of the management area.
  4807. + * If the whole block became dirty, this will kick off an erasure.
  4808. + */
  4809. + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
  4810. + bi->block_state == YAFFS_BLOCK_STATE_FULL ||
  4811. + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  4812. + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  4813. + dev->n_free_chunks++;
  4814. + yaffs_clear_chunk_bit(dev, block, page);
  4815. + bi->pages_in_use--;
  4816. +
  4817. + if (bi->pages_in_use == 0 &&
  4818. + !bi->has_shrink_hdr &&
  4819. + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
  4820. + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  4821. + yaffs_block_became_dirty(dev, block);
  4822. + }
  4823. + }
  4824. +}
  4825. +
  4826. +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  4827. + const u8 *buffer, int n_bytes, int use_reserve)
  4828. +{
  4829. + /* Find old chunk Need to do this to get serial number
  4830. + * Write new one and patch into tree.
  4831. + * Invalidate old tags.
  4832. + */
  4833. +
  4834. + int prev_chunk_id;
  4835. + struct yaffs_ext_tags prev_tags;
  4836. + int new_chunk_id;
  4837. + struct yaffs_ext_tags new_tags;
  4838. + struct yaffs_dev *dev = in->my_dev;
  4839. +
  4840. + yaffs_check_gc(dev, 0);
  4841. +
  4842. + /* Get the previous chunk at this location in the file if it exists.
  4843. + * If it does not exist then put a zero into the tree. This creates
  4844. + * the tnode now, rather than later when it is harder to clean up.
  4845. + */
  4846. + prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
  4847. + if (prev_chunk_id < 1 &&
  4848. + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
  4849. + return 0;
  4850. +
  4851. + /* Set up new tags */
  4852. + memset(&new_tags, 0, sizeof(new_tags));
  4853. +
  4854. + new_tags.chunk_id = inode_chunk;
  4855. + new_tags.obj_id = in->obj_id;
  4856. + new_tags.serial_number =
  4857. + (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
  4858. + new_tags.n_bytes = n_bytes;
  4859. +
  4860. + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
  4861. + yaffs_trace(YAFFS_TRACE_ERROR,
  4862. + "Writing %d bytes to chunk!!!!!!!!!",
  4863. + n_bytes);
  4864. + BUG();
  4865. + }
  4866. +
  4867. + new_chunk_id =
  4868. + yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
  4869. +
  4870. + if (new_chunk_id > 0) {
  4871. + yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
  4872. +
  4873. + if (prev_chunk_id > 0)
  4874. + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  4875. +
  4876. + yaffs_verify_file_sane(in);
  4877. + }
  4878. + return new_chunk_id;
  4879. +
  4880. +}
  4881. +
  4882. +
  4883. +
  4884. +static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
  4885. + const YCHAR *name, const void *value, int size,
  4886. + int flags)
  4887. +{
  4888. + struct yaffs_xattr_mod xmod;
  4889. + int result;
  4890. +
  4891. + xmod.set = set;
  4892. + xmod.name = name;
  4893. + xmod.data = value;
  4894. + xmod.size = size;
  4895. + xmod.flags = flags;
  4896. + xmod.result = -ENOSPC;
  4897. +
  4898. + result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
  4899. +
  4900. + if (result > 0)
  4901. + return xmod.result;
  4902. + else
  4903. + return -ENOSPC;
  4904. +}
  4905. +
  4906. +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  4907. + struct yaffs_xattr_mod *xmod)
  4908. +{
  4909. + int retval = 0;
  4910. + int x_offs = sizeof(struct yaffs_obj_hdr);
  4911. + struct yaffs_dev *dev = obj->my_dev;
  4912. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  4913. + char *x_buffer = buffer + x_offs;
  4914. +
  4915. + if (xmod->set)
  4916. + retval =
  4917. + nval_set(x_buffer, x_size, xmod->name, xmod->data,
  4918. + xmod->size, xmod->flags);
  4919. + else
  4920. + retval = nval_del(x_buffer, x_size, xmod->name);
  4921. +
  4922. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  4923. + obj->xattr_known = 1;
  4924. + xmod->result = retval;
  4925. +
  4926. + return retval;
  4927. +}
  4928. +
  4929. +static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
  4930. + void *value, int size)
  4931. +{
  4932. + char *buffer = NULL;
  4933. + int result;
  4934. + struct yaffs_ext_tags tags;
  4935. + struct yaffs_dev *dev = obj->my_dev;
  4936. + int x_offs = sizeof(struct yaffs_obj_hdr);
  4937. + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  4938. + char *x_buffer;
  4939. + int retval = 0;
  4940. +
  4941. + if (obj->hdr_chunk < 1)
  4942. + return -ENODATA;
  4943. +
  4944. + /* If we know that the object has no xattribs then don't do all the
  4945. + * reading and parsing.
  4946. + */
  4947. + if (obj->xattr_known && !obj->has_xattr) {
  4948. + if (name)
  4949. + return -ENODATA;
  4950. + else
  4951. + return 0;
  4952. + }
  4953. +
  4954. + buffer = (char *)yaffs_get_temp_buffer(dev);
  4955. + if (!buffer)
  4956. + return -ENOMEM;
  4957. +
  4958. + result =
  4959. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
  4960. +
  4961. + if (result != YAFFS_OK)
  4962. + retval = -ENOENT;
  4963. + else {
  4964. + x_buffer = buffer + x_offs;
  4965. +
  4966. + if (!obj->xattr_known) {
  4967. + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  4968. + obj->xattr_known = 1;
  4969. + }
  4970. +
  4971. + if (name)
  4972. + retval = nval_get(x_buffer, x_size, name, value, size);
  4973. + else
  4974. + retval = nval_list(x_buffer, x_size, value, size);
  4975. + }
  4976. + yaffs_release_temp_buffer(dev, (u8 *) buffer);
  4977. + return retval;
  4978. +}
  4979. +
  4980. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  4981. + const void *value, int size, int flags)
  4982. +{
  4983. + return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
  4984. +}
  4985. +
  4986. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
  4987. +{
  4988. + return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
  4989. +}
  4990. +
  4991. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  4992. + int size)
  4993. +{
  4994. + return yaffs_do_xattrib_fetch(obj, name, value, size);
  4995. +}
  4996. +
  4997. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
  4998. +{
  4999. + return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
  5000. +}
  5001. +
  5002. +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
  5003. +{
  5004. + u8 *buf;
  5005. + struct yaffs_obj_hdr *oh;
  5006. + struct yaffs_dev *dev;
  5007. + struct yaffs_ext_tags tags;
  5008. + int result;
  5009. + int alloc_failed = 0;
  5010. +
  5011. + if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
  5012. + return;
  5013. +
  5014. + dev = in->my_dev;
  5015. + in->lazy_loaded = 0;
  5016. + buf = yaffs_get_temp_buffer(dev);
  5017. +
  5018. + result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
  5019. + oh = (struct yaffs_obj_hdr *)buf;
  5020. +
  5021. + in->yst_mode = oh->yst_mode;
  5022. + yaffs_load_attribs(in, oh);
  5023. + yaffs_set_obj_name_from_oh(in, oh);
  5024. +
  5025. + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  5026. + in->variant.symlink_variant.alias =
  5027. + yaffs_clone_str(oh->alias);
  5028. + if (!in->variant.symlink_variant.alias)
  5029. + alloc_failed = 1; /* Not returned */
  5030. + }
  5031. + yaffs_release_temp_buffer(dev, buf);
  5032. +}
  5033. +
  5034. +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
  5035. + const YCHAR *oh_name, int buff_size)
  5036. +{
  5037. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  5038. + if (dev->param.auto_unicode) {
  5039. + if (*oh_name) {
  5040. + /* It is an ASCII name, do an ASCII to
  5041. + * unicode conversion */
  5042. + const char *ascii_oh_name = (const char *)oh_name;
  5043. + int n = buff_size - 1;
  5044. + while (n > 0 && *ascii_oh_name) {
  5045. + *name = *ascii_oh_name;
  5046. + name++;
  5047. + ascii_oh_name++;
  5048. + n--;
  5049. + }
  5050. + } else {
  5051. + strncpy(name, oh_name + 1, buff_size - 1);
  5052. + }
  5053. + } else {
  5054. +#else
  5055. + (void) dev;
  5056. + {
  5057. +#endif
  5058. + strncpy(name, oh_name, buff_size - 1);
  5059. + }
  5060. +}
  5061. +
  5062. +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
  5063. + const YCHAR *name)
  5064. +{
  5065. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  5066. +
  5067. + int is_ascii;
  5068. + YCHAR *w;
  5069. +
  5070. + if (dev->param.auto_unicode) {
  5071. +
  5072. + is_ascii = 1;
  5073. + w = name;
  5074. +
  5075. + /* Figure out if the name will fit in ascii character set */
  5076. + while (is_ascii && *w) {
  5077. + if ((*w) & 0xff00)
  5078. + is_ascii = 0;
  5079. + w++;
  5080. + }
  5081. +
  5082. + if (is_ascii) {
  5083. + /* It is an ASCII name, so convert unicode to ascii */
  5084. + char *ascii_oh_name = (char *)oh_name;
  5085. + int n = YAFFS_MAX_NAME_LENGTH - 1;
  5086. + while (n > 0 && *name) {
  5087. + *ascii_oh_name = *name;
  5088. + name++;
  5089. + ascii_oh_name++;
  5090. + n--;
  5091. + }
  5092. + } else {
  5093. + /* Unicode name, so save starting at the second YCHAR */
  5094. + *oh_name = 0;
  5095. + strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
  5096. + }
  5097. + } else {
  5098. +#else
  5099. + dev = dev;
  5100. + {
  5101. +#endif
  5102. + strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
  5103. + }
  5104. +}
  5105. +
  5106. +/* UpdateObjectHeader updates the header on NAND for an object.
  5107. + * If name is not NULL, then that new name is used.
  5108. + */
  5109. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
  5110. + int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
  5111. +{
  5112. +
  5113. + struct yaffs_block_info *bi;
  5114. + struct yaffs_dev *dev = in->my_dev;
  5115. + int prev_chunk_id;
  5116. + int ret_val = 0;
  5117. + int result = 0;
  5118. + int new_chunk_id;
  5119. + struct yaffs_ext_tags new_tags;
  5120. + struct yaffs_ext_tags old_tags;
  5121. + const YCHAR *alias = NULL;
  5122. + u8 *buffer = NULL;
  5123. + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
  5124. + struct yaffs_obj_hdr *oh = NULL;
  5125. + loff_t file_size = 0;
  5126. +
  5127. + strcpy(old_name, _Y("silly old name"));
  5128. +
  5129. + if (in->fake && in != dev->root_dir && !force && !xmod)
  5130. + return ret_val;
  5131. +
  5132. + yaffs_check_gc(dev, 0);
  5133. + yaffs_check_obj_details_loaded(in);
  5134. +
  5135. + buffer = yaffs_get_temp_buffer(in->my_dev);
  5136. + oh = (struct yaffs_obj_hdr *)buffer;
  5137. +
  5138. + prev_chunk_id = in->hdr_chunk;
  5139. +
  5140. + if (prev_chunk_id > 0) {
  5141. + result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
  5142. + buffer, &old_tags);
  5143. +
  5144. + yaffs_verify_oh(in, oh, &old_tags, 0);
  5145. + memcpy(old_name, oh->name, sizeof(oh->name));
  5146. + memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
  5147. + } else {
  5148. + memset(buffer, 0xff, dev->data_bytes_per_chunk);
  5149. + }
  5150. +
  5151. + oh->type = in->variant_type;
  5152. + oh->yst_mode = in->yst_mode;
  5153. + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
  5154. +
  5155. + yaffs_load_attribs_oh(oh, in);
  5156. +
  5157. + if (in->parent)
  5158. + oh->parent_obj_id = in->parent->obj_id;
  5159. + else
  5160. + oh->parent_obj_id = 0;
  5161. +
  5162. + if (name && *name) {
  5163. + memset(oh->name, 0, sizeof(oh->name));
  5164. + yaffs_load_oh_from_name(dev, oh->name, name);
  5165. + } else if (prev_chunk_id > 0) {
  5166. + memcpy(oh->name, old_name, sizeof(oh->name));
  5167. + } else {
  5168. + memset(oh->name, 0, sizeof(oh->name));
  5169. + }
  5170. +
  5171. + oh->is_shrink = is_shrink;
  5172. +
  5173. + switch (in->variant_type) {
  5174. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5175. + /* Should not happen */
  5176. + break;
  5177. + case YAFFS_OBJECT_TYPE_FILE:
  5178. + if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
  5179. + oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
  5180. + file_size = in->variant.file_variant.file_size;
  5181. + yaffs_oh_size_load(oh, file_size);
  5182. + break;
  5183. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5184. + oh->equiv_id = in->variant.hardlink_variant.equiv_id;
  5185. + break;
  5186. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5187. + /* Do nothing */
  5188. + break;
  5189. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5190. + /* Do nothing */
  5191. + break;
  5192. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5193. + alias = in->variant.symlink_variant.alias;
  5194. + if (!alias)
  5195. + alias = _Y("no alias");
  5196. + strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
  5197. + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  5198. + break;
  5199. + }
  5200. +
  5201. + /* process any xattrib modifications */
  5202. + if (xmod)
  5203. + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
  5204. +
  5205. + /* Tags */
  5206. + memset(&new_tags, 0, sizeof(new_tags));
  5207. + in->serial++;
  5208. + new_tags.chunk_id = 0;
  5209. + new_tags.obj_id = in->obj_id;
  5210. + new_tags.serial_number = in->serial;
  5211. +
  5212. + /* Add extra info for file header */
  5213. + new_tags.extra_available = 1;
  5214. + new_tags.extra_parent_id = oh->parent_obj_id;
  5215. + new_tags.extra_file_size = file_size;
  5216. + new_tags.extra_is_shrink = oh->is_shrink;
  5217. + new_tags.extra_equiv_id = oh->equiv_id;
  5218. + new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
  5219. + new_tags.extra_obj_type = in->variant_type;
  5220. + yaffs_verify_oh(in, oh, &new_tags, 1);
  5221. +
  5222. + /* Create new chunk in NAND */
  5223. + new_chunk_id =
  5224. + yaffs_write_new_chunk(dev, buffer, &new_tags,
  5225. + (prev_chunk_id > 0) ? 1 : 0);
  5226. +
  5227. + if (buffer)
  5228. + yaffs_release_temp_buffer(dev, buffer);
  5229. +
  5230. + if (new_chunk_id < 0)
  5231. + return new_chunk_id;
  5232. +
  5233. + in->hdr_chunk = new_chunk_id;
  5234. +
  5235. + if (prev_chunk_id > 0)
  5236. + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  5237. +
  5238. + if (!yaffs_obj_cache_dirty(in))
  5239. + in->dirty = 0;
  5240. +
  5241. + /* If this was a shrink, then mark the block
  5242. + * that the chunk lives on */
  5243. + if (is_shrink) {
  5244. + bi = yaffs_get_block_info(in->my_dev,
  5245. + new_chunk_id /
  5246. + in->my_dev->param.chunks_per_block);
  5247. + bi->has_shrink_hdr = 1;
  5248. + }
  5249. +
  5250. +
  5251. + return new_chunk_id;
  5252. +}
  5253. +
  5254. +/*--------------------- File read/write ------------------------
  5255. + * Read and write have very similar structures.
  5256. + * In general the read/write has three parts to it
  5257. + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  5258. + * Some complete chunks
  5259. + * An incomplete chunk to end off with
  5260. + *
  5261. + * Curve-balls: the first chunk might also be the last chunk.
  5262. + */
  5263. +
  5264. +int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
  5265. +{
  5266. + int chunk;
  5267. + u32 start;
  5268. + int n_copy;
  5269. + int n = n_bytes;
  5270. + int n_done = 0;
  5271. + struct yaffs_cache *cache;
  5272. + struct yaffs_dev *dev;
  5273. +
  5274. + dev = in->my_dev;
  5275. +
  5276. + while (n > 0) {
  5277. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5278. + chunk++;
  5279. +
  5280. + /* OK now check for the curveball where the start and end are in
  5281. + * the same chunk.
  5282. + */
  5283. + if ((start + n) < dev->data_bytes_per_chunk)
  5284. + n_copy = n;
  5285. + else
  5286. + n_copy = dev->data_bytes_per_chunk - start;
  5287. +
  5288. + cache = yaffs_find_chunk_cache(in, chunk);
  5289. +
  5290. + /* If the chunk is already in the cache or it is less than
  5291. + * a whole chunk or we're using inband tags then use the cache
  5292. + * (if there is caching) else bypass the cache.
  5293. + */
  5294. + if (cache || n_copy != dev->data_bytes_per_chunk ||
  5295. + dev->param.inband_tags) {
  5296. + if (dev->param.n_caches > 0) {
  5297. +
  5298. + /* If we can't find the data in the cache,
  5299. + * then load it up. */
  5300. +
  5301. + if (!cache) {
  5302. + cache =
  5303. + yaffs_grab_chunk_cache(in->my_dev);
  5304. + cache->object = in;
  5305. + cache->chunk_id = chunk;
  5306. + cache->dirty = 0;
  5307. + cache->locked = 0;
  5308. + yaffs_rd_data_obj(in, chunk,
  5309. + cache->data);
  5310. + cache->n_bytes = 0;
  5311. + }
  5312. +
  5313. + yaffs_use_cache(dev, cache, 0);
  5314. +
  5315. + cache->locked = 1;
  5316. +
  5317. + memcpy(buffer, &cache->data[start], n_copy);
  5318. +
  5319. + cache->locked = 0;
  5320. + } else {
  5321. + /* Read into the local buffer then copy.. */
  5322. +
  5323. + u8 *local_buffer =
  5324. + yaffs_get_temp_buffer(dev);
  5325. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5326. +
  5327. + memcpy(buffer, &local_buffer[start], n_copy);
  5328. +
  5329. + yaffs_release_temp_buffer(dev, local_buffer);
  5330. + }
  5331. + } else {
  5332. + /* A full chunk. Read directly into the buffer. */
  5333. + yaffs_rd_data_obj(in, chunk, buffer);
  5334. + }
  5335. + n -= n_copy;
  5336. + offset += n_copy;
  5337. + buffer += n_copy;
  5338. + n_done += n_copy;
  5339. + }
  5340. + return n_done;
  5341. +}
  5342. +
  5343. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  5344. + int n_bytes, int write_through)
  5345. +{
  5346. +
  5347. + int chunk;
  5348. + u32 start;
  5349. + int n_copy;
  5350. + int n = n_bytes;
  5351. + int n_done = 0;
  5352. + int n_writeback;
  5353. + loff_t start_write = offset;
  5354. + int chunk_written = 0;
  5355. + u32 n_bytes_read;
  5356. + loff_t chunk_start;
  5357. + struct yaffs_dev *dev;
  5358. +
  5359. + dev = in->my_dev;
  5360. +
  5361. + while (n > 0 && chunk_written >= 0) {
  5362. + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  5363. +
  5364. + if (((loff_t)chunk) *
  5365. + dev->data_bytes_per_chunk + start != offset ||
  5366. + start >= dev->data_bytes_per_chunk) {
  5367. + yaffs_trace(YAFFS_TRACE_ERROR,
  5368. + "AddrToChunk of offset %lld gives chunk %d start %d",
  5369. + offset, chunk, start);
  5370. + }
  5371. + chunk++; /* File pos to chunk in file offset */
  5372. +
  5373. + /* OK now check for the curveball where the start and end are in
  5374. + * the same chunk.
  5375. + */
  5376. +
  5377. + if ((start + n) < dev->data_bytes_per_chunk) {
  5378. + n_copy = n;
  5379. +
  5380. + /* Now calculate how many bytes to write back....
  5381. + * If we're overwriting and not writing to then end of
  5382. + * file then we need to write back as much as was there
  5383. + * before.
  5384. + */
  5385. +
  5386. + chunk_start = (((loff_t)(chunk - 1)) *
  5387. + dev->data_bytes_per_chunk);
  5388. +
  5389. + if (chunk_start > in->variant.file_variant.file_size)
  5390. + n_bytes_read = 0; /* Past end of file */
  5391. + else
  5392. + n_bytes_read =
  5393. + in->variant.file_variant.file_size -
  5394. + chunk_start;
  5395. +
  5396. + if (n_bytes_read > dev->data_bytes_per_chunk)
  5397. + n_bytes_read = dev->data_bytes_per_chunk;
  5398. +
  5399. + n_writeback =
  5400. + (n_bytes_read >
  5401. + (start + n)) ? n_bytes_read : (start + n);
  5402. +
  5403. + if (n_writeback < 0 ||
  5404. + n_writeback > dev->data_bytes_per_chunk)
  5405. + BUG();
  5406. +
  5407. + } else {
  5408. + n_copy = dev->data_bytes_per_chunk - start;
  5409. + n_writeback = dev->data_bytes_per_chunk;
  5410. + }
  5411. +
  5412. + if (n_copy != dev->data_bytes_per_chunk ||
  5413. + !dev->param.cache_bypass_aligned ||
  5414. + dev->param.inband_tags) {
  5415. + /* An incomplete start or end chunk (or maybe both
  5416. + * start and end chunk), or we're using inband tags,
  5417. + * or we're forcing writes through the cache,
  5418. + * so we want to use the cache buffers.
  5419. + */
  5420. + if (dev->param.n_caches > 0) {
  5421. + struct yaffs_cache *cache;
  5422. +
  5423. + /* If we can't find the data in the cache, then
  5424. + * load the cache */
  5425. + cache = yaffs_find_chunk_cache(in, chunk);
  5426. +
  5427. + if (!cache &&
  5428. + yaffs_check_alloc_available(dev, 1)) {
  5429. + cache = yaffs_grab_chunk_cache(dev);
  5430. + cache->object = in;
  5431. + cache->chunk_id = chunk;
  5432. + cache->dirty = 0;
  5433. + cache->locked = 0;
  5434. + yaffs_rd_data_obj(in, chunk,
  5435. + cache->data);
  5436. + } else if (cache &&
  5437. + !cache->dirty &&
  5438. + !yaffs_check_alloc_available(dev,
  5439. + 1)) {
  5440. + /* Drop the cache if it was a read cache
  5441. + * item and no space check has been made
  5442. + * for it.
  5443. + */
  5444. + cache = NULL;
  5445. + }
  5446. +
  5447. + if (cache) {
  5448. + yaffs_use_cache(dev, cache, 1);
  5449. + cache->locked = 1;
  5450. +
  5451. + memcpy(&cache->data[start], buffer,
  5452. + n_copy);
  5453. +
  5454. + cache->locked = 0;
  5455. + cache->n_bytes = n_writeback;
  5456. +
  5457. + if (write_through) {
  5458. + chunk_written =
  5459. + yaffs_wr_data_obj
  5460. + (cache->object,
  5461. + cache->chunk_id,
  5462. + cache->data,
  5463. + cache->n_bytes, 1);
  5464. + cache->dirty = 0;
  5465. + }
  5466. + } else {
  5467. + chunk_written = -1; /* fail write */
  5468. + }
  5469. + } else {
  5470. + /* An incomplete start or end chunk (or maybe
  5471. + * both start and end chunk). Read into the
  5472. + * local buffer then copy over and write back.
  5473. + */
  5474. +
  5475. + u8 *local_buffer = yaffs_get_temp_buffer(dev);
  5476. +
  5477. + yaffs_rd_data_obj(in, chunk, local_buffer);
  5478. + memcpy(&local_buffer[start], buffer, n_copy);
  5479. +
  5480. + chunk_written =
  5481. + yaffs_wr_data_obj(in, chunk,
  5482. + local_buffer,
  5483. + n_writeback, 0);
  5484. +
  5485. + yaffs_release_temp_buffer(dev, local_buffer);
  5486. + }
  5487. + } else {
  5488. + /* A full chunk. Write directly from the buffer. */
  5489. +
  5490. + chunk_written =
  5491. + yaffs_wr_data_obj(in, chunk, buffer,
  5492. + dev->data_bytes_per_chunk, 0);
  5493. +
  5494. + /* Since we've overwritten the cached data,
  5495. + * we better invalidate it. */
  5496. + yaffs_invalidate_chunk_cache(in, chunk);
  5497. + }
  5498. +
  5499. + if (chunk_written >= 0) {
  5500. + n -= n_copy;
  5501. + offset += n_copy;
  5502. + buffer += n_copy;
  5503. + n_done += n_copy;
  5504. + }
  5505. + }
  5506. +
  5507. + /* Update file object */
  5508. +
  5509. + if ((start_write + n_done) > in->variant.file_variant.file_size)
  5510. + in->variant.file_variant.file_size = (start_write + n_done);
  5511. +
  5512. + in->dirty = 1;
  5513. + return n_done;
  5514. +}
  5515. +
  5516. +int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  5517. + int n_bytes, int write_through)
  5518. +{
  5519. + yaffs2_handle_hole(in, offset);
  5520. + return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
  5521. +}
  5522. +
  5523. +/* ---------------------- File resizing stuff ------------------ */
  5524. +
  5525. +static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
  5526. +{
  5527. +
  5528. + struct yaffs_dev *dev = in->my_dev;
  5529. + loff_t old_size = in->variant.file_variant.file_size;
  5530. + int i;
  5531. + int chunk_id;
  5532. + u32 dummy;
  5533. + int last_del;
  5534. + int start_del;
  5535. +
  5536. + if (old_size > 0)
  5537. + yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
  5538. + else
  5539. + last_del = 0;
  5540. +
  5541. + yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
  5542. + &start_del, &dummy);
  5543. + last_del++;
  5544. + start_del++;
  5545. +
  5546. + /* Delete backwards so that we don't end up with holes if
  5547. + * power is lost part-way through the operation.
  5548. + */
  5549. + for (i = last_del; i >= start_del; i--) {
  5550. + /* NB this could be optimised somewhat,
  5551. + * eg. could retrieve the tags and write them without
  5552. + * using yaffs_chunk_del
  5553. + */
  5554. +
  5555. + chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
  5556. +
  5557. + if (chunk_id < 1)
  5558. + continue;
  5559. +
  5560. + if (chunk_id <
  5561. + (dev->internal_start_block * dev->param.chunks_per_block) ||
  5562. + chunk_id >=
  5563. + ((dev->internal_end_block + 1) *
  5564. + dev->param.chunks_per_block)) {
  5565. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  5566. + "Found daft chunk_id %d for %d",
  5567. + chunk_id, i);
  5568. + } else {
  5569. + in->n_data_chunks--;
  5570. + yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
  5571. + }
  5572. + }
  5573. +}
  5574. +
  5575. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
  5576. +{
  5577. + int new_full;
  5578. + u32 new_partial;
  5579. + struct yaffs_dev *dev = obj->my_dev;
  5580. +
  5581. + yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
  5582. +
  5583. + yaffs_prune_chunks(obj, new_size);
  5584. +
  5585. + if (new_partial != 0) {
  5586. + int last_chunk = 1 + new_full;
  5587. + u8 *local_buffer = yaffs_get_temp_buffer(dev);
  5588. +
  5589. + /* Rewrite the last chunk with its new size and zero pad */
  5590. + yaffs_rd_data_obj(obj, last_chunk, local_buffer);
  5591. + memset(local_buffer + new_partial, 0,
  5592. + dev->data_bytes_per_chunk - new_partial);
  5593. +
  5594. + yaffs_wr_data_obj(obj, last_chunk, local_buffer,
  5595. + new_partial, 1);
  5596. +
  5597. + yaffs_release_temp_buffer(dev, local_buffer);
  5598. + }
  5599. +
  5600. + obj->variant.file_variant.file_size = new_size;
  5601. +
  5602. + yaffs_prune_tree(dev, &obj->variant.file_variant);
  5603. +}
  5604. +
  5605. +int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
  5606. +{
  5607. + struct yaffs_dev *dev = in->my_dev;
  5608. + loff_t old_size = in->variant.file_variant.file_size;
  5609. +
  5610. + yaffs_flush_file_cache(in);
  5611. + yaffs_invalidate_whole_cache(in);
  5612. +
  5613. + yaffs_check_gc(dev, 0);
  5614. +
  5615. + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
  5616. + return YAFFS_FAIL;
  5617. +
  5618. + if (new_size == old_size)
  5619. + return YAFFS_OK;
  5620. +
  5621. + if (new_size > old_size) {
  5622. + yaffs2_handle_hole(in, new_size);
  5623. + in->variant.file_variant.file_size = new_size;
  5624. + } else {
  5625. + /* new_size < old_size */
  5626. + yaffs_resize_file_down(in, new_size);
  5627. + }
  5628. +
  5629. + /* Write a new object header to reflect the resize.
  5630. + * show we've shrunk the file, if need be
  5631. + * Do this only if the file is not in the deleted directories
  5632. + * and is not shadowed.
  5633. + */
  5634. + if (in->parent &&
  5635. + !in->is_shadowed &&
  5636. + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  5637. + in->parent->obj_id != YAFFS_OBJECTID_DELETED)
  5638. + yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
  5639. +
  5640. + return YAFFS_OK;
  5641. +}
  5642. +
  5643. +int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
  5644. +{
  5645. + if (!in->dirty)
  5646. + return YAFFS_OK;
  5647. +
  5648. + yaffs_flush_file_cache(in);
  5649. +
  5650. + if (data_sync)
  5651. + return YAFFS_OK;
  5652. +
  5653. + if (update_time)
  5654. + yaffs_load_current_time(in, 0, 0);
  5655. +
  5656. + return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
  5657. + YAFFS_OK : YAFFS_FAIL;
  5658. +}
  5659. +
  5660. +
  5661. +/* yaffs_del_file deletes the whole file data
  5662. + * and the inode associated with the file.
  5663. + * It does not delete the links associated with the file.
  5664. + */
  5665. +static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
  5666. +{
  5667. + int ret_val;
  5668. + int del_now = 0;
  5669. + struct yaffs_dev *dev = in->my_dev;
  5670. +
  5671. + if (!in->my_inode)
  5672. + del_now = 1;
  5673. +
  5674. + if (del_now) {
  5675. + ret_val =
  5676. + yaffs_change_obj_name(in, in->my_dev->del_dir,
  5677. + _Y("deleted"), 0, 0);
  5678. + yaffs_trace(YAFFS_TRACE_TRACING,
  5679. + "yaffs: immediate deletion of file %d",
  5680. + in->obj_id);
  5681. + in->deleted = 1;
  5682. + in->my_dev->n_deleted_files++;
  5683. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5684. + yaffs_resize_file(in, 0);
  5685. + yaffs_soft_del_file(in);
  5686. + } else {
  5687. + ret_val =
  5688. + yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
  5689. + _Y("unlinked"), 0, 0);
  5690. + }
  5691. + return ret_val;
  5692. +}
  5693. +
  5694. +static int yaffs_del_file(struct yaffs_obj *in)
  5695. +{
  5696. + int ret_val = YAFFS_OK;
  5697. + int deleted; /* Need to cache value on stack if in is freed */
  5698. + struct yaffs_dev *dev = in->my_dev;
  5699. +
  5700. + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  5701. + yaffs_resize_file(in, 0);
  5702. +
  5703. + if (in->n_data_chunks > 0) {
  5704. + /* Use soft deletion if there is data in the file.
  5705. + * That won't be the case if it has been resized to zero.
  5706. + */
  5707. + if (!in->unlinked)
  5708. + ret_val = yaffs_unlink_file_if_needed(in);
  5709. +
  5710. + deleted = in->deleted;
  5711. +
  5712. + if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
  5713. + in->deleted = 1;
  5714. + deleted = 1;
  5715. + in->my_dev->n_deleted_files++;
  5716. + yaffs_soft_del_file(in);
  5717. + }
  5718. + return deleted ? YAFFS_OK : YAFFS_FAIL;
  5719. + } else {
  5720. + /* The file has no data chunks so we toss it immediately */
  5721. + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
  5722. + in->variant.file_variant.top = NULL;
  5723. + yaffs_generic_obj_del(in);
  5724. +
  5725. + return YAFFS_OK;
  5726. + }
  5727. +}
  5728. +
  5729. +int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
  5730. +{
  5731. + return (obj &&
  5732. + obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
  5733. + !(list_empty(&obj->variant.dir_variant.children));
  5734. +}
  5735. +
  5736. +static int yaffs_del_dir(struct yaffs_obj *obj)
  5737. +{
  5738. + /* First check that the directory is empty. */
  5739. + if (yaffs_is_non_empty_dir(obj))
  5740. + return YAFFS_FAIL;
  5741. +
  5742. + return yaffs_generic_obj_del(obj);
  5743. +}
  5744. +
  5745. +static int yaffs_del_symlink(struct yaffs_obj *in)
  5746. +{
  5747. + kfree(in->variant.symlink_variant.alias);
  5748. + in->variant.symlink_variant.alias = NULL;
  5749. +
  5750. + return yaffs_generic_obj_del(in);
  5751. +}
  5752. +
  5753. +static int yaffs_del_link(struct yaffs_obj *in)
  5754. +{
  5755. + /* remove this hardlink from the list associated with the equivalent
  5756. + * object
  5757. + */
  5758. + list_del_init(&in->hard_links);
  5759. + return yaffs_generic_obj_del(in);
  5760. +}
  5761. +
  5762. +int yaffs_del_obj(struct yaffs_obj *obj)
  5763. +{
  5764. + int ret_val = -1;
  5765. +
  5766. + switch (obj->variant_type) {
  5767. + case YAFFS_OBJECT_TYPE_FILE:
  5768. + ret_val = yaffs_del_file(obj);
  5769. + break;
  5770. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5771. + if (!list_empty(&obj->variant.dir_variant.dirty)) {
  5772. + yaffs_trace(YAFFS_TRACE_BACKGROUND,
  5773. + "Remove object %d from dirty directories",
  5774. + obj->obj_id);
  5775. + list_del_init(&obj->variant.dir_variant.dirty);
  5776. + }
  5777. + return yaffs_del_dir(obj);
  5778. + break;
  5779. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5780. + ret_val = yaffs_del_symlink(obj);
  5781. + break;
  5782. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5783. + ret_val = yaffs_del_link(obj);
  5784. + break;
  5785. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5786. + ret_val = yaffs_generic_obj_del(obj);
  5787. + break;
  5788. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5789. + ret_val = 0;
  5790. + break; /* should not happen. */
  5791. + }
  5792. + return ret_val;
  5793. +}
  5794. +
  5795. +
  5796. +static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
  5797. + struct yaffs_obj *to_dir)
  5798. +{
  5799. + struct yaffs_obj *obj;
  5800. + struct list_head *lh;
  5801. + struct list_head *n;
  5802. +
  5803. + list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
  5804. + obj = list_entry(lh, struct yaffs_obj, siblings);
  5805. + yaffs_add_obj_to_dir(to_dir, obj);
  5806. + }
  5807. +}
  5808. +
  5809. +struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
  5810. + enum yaffs_obj_type type)
  5811. +{
  5812. + /* Tear down the old variant */
  5813. + switch (obj->variant_type) {
  5814. + case YAFFS_OBJECT_TYPE_FILE:
  5815. + /* Nuke file data */
  5816. + yaffs_resize_file(obj, 0);
  5817. + yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
  5818. + obj->variant.file_variant.top = NULL;
  5819. + break;
  5820. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5821. + /* Put the children in lost and found. */
  5822. + yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
  5823. + if (!list_empty(&obj->variant.dir_variant.dirty))
  5824. + list_del_init(&obj->variant.dir_variant.dirty);
  5825. + break;
  5826. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5827. + /* Nuke symplink data */
  5828. + kfree(obj->variant.symlink_variant.alias);
  5829. + obj->variant.symlink_variant.alias = NULL;
  5830. + break;
  5831. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5832. + list_del_init(&obj->hard_links);
  5833. + break;
  5834. + default:
  5835. + break;
  5836. + }
  5837. +
  5838. + memset(&obj->variant, 0, sizeof(obj->variant));
  5839. +
  5840. + /*Set up new variant if the memset is not enough. */
  5841. + switch (type) {
  5842. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5843. + INIT_LIST_HEAD(&obj->variant.dir_variant.children);
  5844. + INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
  5845. + break;
  5846. + case YAFFS_OBJECT_TYPE_FILE:
  5847. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5848. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5849. + default:
  5850. + break;
  5851. + }
  5852. +
  5853. + obj->variant_type = type;
  5854. +
  5855. + return obj;
  5856. +
  5857. +}
  5858. +
  5859. +static int yaffs_unlink_worker(struct yaffs_obj *obj)
  5860. +{
  5861. + int del_now = 0;
  5862. +
  5863. + if (!obj)
  5864. + return YAFFS_FAIL;
  5865. +
  5866. + if (!obj->my_inode)
  5867. + del_now = 1;
  5868. +
  5869. + yaffs_update_parent(obj->parent);
  5870. +
  5871. + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  5872. + return yaffs_del_link(obj);
  5873. + } else if (!list_empty(&obj->hard_links)) {
  5874. + /* Curve ball: We're unlinking an object that has a hardlink.
  5875. + *
  5876. + * This problem arises because we are not strictly following
  5877. + * The Linux link/inode model.
  5878. + *
  5879. + * We can't really delete the object.
  5880. + * Instead, we do the following:
  5881. + * - Select a hardlink.
  5882. + * - Unhook it from the hard links
  5883. + * - Move it from its parent directory so that the rename works.
  5884. + * - Rename the object to the hardlink's name.
  5885. + * - Delete the hardlink
  5886. + */
  5887. +
  5888. + struct yaffs_obj *hl;
  5889. + struct yaffs_obj *parent;
  5890. + int ret_val;
  5891. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  5892. +
  5893. + hl = list_entry(obj->hard_links.next, struct yaffs_obj,
  5894. + hard_links);
  5895. +
  5896. + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  5897. + parent = hl->parent;
  5898. +
  5899. + list_del_init(&hl->hard_links);
  5900. +
  5901. + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
  5902. +
  5903. + ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
  5904. +
  5905. + if (ret_val == YAFFS_OK)
  5906. + ret_val = yaffs_generic_obj_del(hl);
  5907. +
  5908. + return ret_val;
  5909. +
  5910. + } else if (del_now) {
  5911. + switch (obj->variant_type) {
  5912. + case YAFFS_OBJECT_TYPE_FILE:
  5913. + return yaffs_del_file(obj);
  5914. + break;
  5915. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  5916. + list_del_init(&obj->variant.dir_variant.dirty);
  5917. + return yaffs_del_dir(obj);
  5918. + break;
  5919. + case YAFFS_OBJECT_TYPE_SYMLINK:
  5920. + return yaffs_del_symlink(obj);
  5921. + break;
  5922. + case YAFFS_OBJECT_TYPE_SPECIAL:
  5923. + return yaffs_generic_obj_del(obj);
  5924. + break;
  5925. + case YAFFS_OBJECT_TYPE_HARDLINK:
  5926. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  5927. + default:
  5928. + return YAFFS_FAIL;
  5929. + }
  5930. + } else if (yaffs_is_non_empty_dir(obj)) {
  5931. + return YAFFS_FAIL;
  5932. + } else {
  5933. + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
  5934. + _Y("unlinked"), 0, 0);
  5935. + }
  5936. +}
  5937. +
  5938. +static int yaffs_unlink_obj(struct yaffs_obj *obj)
  5939. +{
  5940. + if (obj && obj->unlink_allowed)
  5941. + return yaffs_unlink_worker(obj);
  5942. +
  5943. + return YAFFS_FAIL;
  5944. +}
  5945. +
  5946. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
  5947. +{
  5948. + struct yaffs_obj *obj;
  5949. +
  5950. + obj = yaffs_find_by_name(dir, name);
  5951. + return yaffs_unlink_obj(obj);
  5952. +}
  5953. +
  5954. +/* Note:
  5955. + * If old_name is NULL then we take old_dir as the object to be renamed.
  5956. + */
  5957. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
  5958. + struct yaffs_obj *new_dir, const YCHAR *new_name)
  5959. +{
  5960. + struct yaffs_obj *obj = NULL;
  5961. + struct yaffs_obj *existing_target = NULL;
  5962. + int force = 0;
  5963. + int result;
  5964. + struct yaffs_dev *dev;
  5965. +
  5966. + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  5967. + BUG();
  5968. + return YAFFS_FAIL;
  5969. + }
  5970. + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  5971. + BUG();
  5972. + return YAFFS_FAIL;
  5973. + }
  5974. +
  5975. + dev = old_dir->my_dev;
  5976. +
  5977. +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  5978. + /* Special case for case insemsitive systems.
  5979. + * While look-up is case insensitive, the name isn't.
  5980. + * Therefore we might want to change x.txt to X.txt
  5981. + */
  5982. + if (old_dir == new_dir &&
  5983. + old_name && new_name &&
  5984. + strcmp(old_name, new_name) == 0)
  5985. + force = 1;
  5986. +#endif
  5987. +
  5988. + if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
  5989. + YAFFS_MAX_NAME_LENGTH)
  5990. + /* ENAMETOOLONG */
  5991. + return YAFFS_FAIL;
  5992. +
  5993. + if (old_name)
  5994. + obj = yaffs_find_by_name(old_dir, old_name);
  5995. + else{
  5996. + obj = old_dir;
  5997. + old_dir = obj->parent;
  5998. + }
  5999. +
  6000. + if (obj && obj->rename_allowed) {
  6001. + /* Now handle an existing target, if there is one */
  6002. + existing_target = yaffs_find_by_name(new_dir, new_name);
  6003. + if (yaffs_is_non_empty_dir(existing_target)) {
  6004. + return YAFFS_FAIL; /* ENOTEMPTY */
  6005. + } else if (existing_target && existing_target != obj) {
  6006. + /* Nuke the target first, using shadowing,
  6007. + * but only if it isn't the same object.
  6008. + *
  6009. + * Note we must disable gc here otherwise it can mess
  6010. + * up the shadowing.
  6011. + *
  6012. + */
  6013. + dev->gc_disable = 1;
  6014. + yaffs_change_obj_name(obj, new_dir, new_name, force,
  6015. + existing_target->obj_id);
  6016. + existing_target->is_shadowed = 1;
  6017. + yaffs_unlink_obj(existing_target);
  6018. + dev->gc_disable = 0;
  6019. + }
  6020. +
  6021. + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
  6022. +
  6023. + yaffs_update_parent(old_dir);
  6024. + if (new_dir != old_dir)
  6025. + yaffs_update_parent(new_dir);
  6026. +
  6027. + return result;
  6028. + }
  6029. + return YAFFS_FAIL;
  6030. +}
  6031. +
  6032. +/*----------------------- Initialisation Scanning ---------------------- */
  6033. +
  6034. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  6035. + int backward_scanning)
  6036. +{
  6037. + struct yaffs_obj *obj;
  6038. +
  6039. + if (backward_scanning) {
  6040. + /* Handle YAFFS2 case (backward scanning)
  6041. + * If the shadowed object exists then ignore.
  6042. + */
  6043. + obj = yaffs_find_by_number(dev, obj_id);
  6044. + if (obj)
  6045. + return;
  6046. + }
  6047. +
  6048. + /* Let's create it (if it does not exist) assuming it is a file so that
  6049. + * it can do shrinking etc.
  6050. + * We put it in unlinked dir to be cleaned up after the scanning
  6051. + */
  6052. + obj =
  6053. + yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
  6054. + if (!obj)
  6055. + return;
  6056. + obj->is_shadowed = 1;
  6057. + yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
  6058. + obj->variant.file_variant.shrink_size = 0;
  6059. + obj->valid = 1; /* So that we don't read any other info. */
  6060. +}
  6061. +
  6062. +void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
  6063. +{
  6064. + struct list_head *lh;
  6065. + struct list_head *save;
  6066. + struct yaffs_obj *hl;
  6067. + struct yaffs_obj *in;
  6068. +
  6069. + list_for_each_safe(lh, save, hard_list) {
  6070. + hl = list_entry(lh, struct yaffs_obj, hard_links);
  6071. + in = yaffs_find_by_number(dev,
  6072. + hl->variant.hardlink_variant.equiv_id);
  6073. +
  6074. + if (in) {
  6075. + /* Add the hardlink pointers */
  6076. + hl->variant.hardlink_variant.equiv_obj = in;
  6077. + list_add(&hl->hard_links, &in->hard_links);
  6078. + } else {
  6079. + /* Todo Need to report/handle this better.
  6080. + * Got a problem... hardlink to a non-existant object
  6081. + */
  6082. + hl->variant.hardlink_variant.equiv_obj = NULL;
  6083. + INIT_LIST_HEAD(&hl->hard_links);
  6084. + }
  6085. + }
  6086. +}
  6087. +
  6088. +static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
  6089. +{
  6090. + /*
  6091. + * Sort out state of unlinked and deleted objects after scanning.
  6092. + */
  6093. + struct list_head *i;
  6094. + struct list_head *n;
  6095. + struct yaffs_obj *l;
  6096. +
  6097. + if (dev->read_only)
  6098. + return;
  6099. +
  6100. + /* Soft delete all the unlinked files */
  6101. + list_for_each_safe(i, n,
  6102. + &dev->unlinked_dir->variant.dir_variant.children) {
  6103. + l = list_entry(i, struct yaffs_obj, siblings);
  6104. + yaffs_del_obj(l);
  6105. + }
  6106. +
  6107. + list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
  6108. + l = list_entry(i, struct yaffs_obj, siblings);
  6109. + yaffs_del_obj(l);
  6110. + }
  6111. +}
  6112. +
  6113. +/*
  6114. + * This code iterates through all the objects making sure that they are rooted.
  6115. + * Any unrooted objects are re-rooted in lost+found.
  6116. + * An object needs to be in one of:
  6117. + * - Directly under deleted, unlinked
  6118. + * - Directly or indirectly under root.
  6119. + *
  6120. + * Note:
  6121. + * This code assumes that we don't ever change the current relationships
  6122. + * between directories:
  6123. + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
  6124. + * lost-n-found->parent == root_dir
  6125. + *
  6126. + * This fixes the problem where directories might have inadvertently been
  6127. + * deleted leaving the object "hanging" without being rooted in the
  6128. + * directory tree.
  6129. + */
  6130. +
  6131. +static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
  6132. +{
  6133. + return (obj == dev->del_dir ||
  6134. + obj == dev->unlinked_dir || obj == dev->root_dir);
  6135. +}
  6136. +
  6137. +static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
  6138. +{
  6139. + struct yaffs_obj *obj;
  6140. + struct yaffs_obj *parent;
  6141. + int i;
  6142. + struct list_head *lh;
  6143. + struct list_head *n;
  6144. + int depth_limit;
  6145. + int hanging;
  6146. +
  6147. + if (dev->read_only)
  6148. + return;
  6149. +
  6150. + /* Iterate through the objects in each hash entry,
  6151. + * looking at each object.
  6152. + * Make sure it is rooted.
  6153. + */
  6154. +
  6155. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  6156. + list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
  6157. + obj = list_entry(lh, struct yaffs_obj, hash_link);
  6158. + parent = obj->parent;
  6159. +
  6160. + if (yaffs_has_null_parent(dev, obj)) {
  6161. + /* These directories are not hanging */
  6162. + hanging = 0;
  6163. + } else if (!parent ||
  6164. + parent->variant_type !=
  6165. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  6166. + hanging = 1;
  6167. + } else if (yaffs_has_null_parent(dev, parent)) {
  6168. + hanging = 0;
  6169. + } else {
  6170. + /*
  6171. + * Need to follow the parent chain to
  6172. + * see if it is hanging.
  6173. + */
  6174. + hanging = 0;
  6175. + depth_limit = 100;
  6176. +
  6177. + while (parent != dev->root_dir &&
  6178. + parent->parent &&
  6179. + parent->parent->variant_type ==
  6180. + YAFFS_OBJECT_TYPE_DIRECTORY &&
  6181. + depth_limit > 0) {
  6182. + parent = parent->parent;
  6183. + depth_limit--;
  6184. + }
  6185. + if (parent != dev->root_dir)
  6186. + hanging = 1;
  6187. + }
  6188. + if (hanging) {
  6189. + yaffs_trace(YAFFS_TRACE_SCAN,
  6190. + "Hanging object %d moved to lost and found",
  6191. + obj->obj_id);
  6192. + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  6193. + }
  6194. + }
  6195. + }
  6196. +}
  6197. +
  6198. +/*
  6199. + * Delete directory contents for cleaning up lost and found.
  6200. + */
  6201. +static void yaffs_del_dir_contents(struct yaffs_obj *dir)
  6202. +{
  6203. + struct yaffs_obj *obj;
  6204. + struct list_head *lh;
  6205. + struct list_head *n;
  6206. +
  6207. + if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  6208. + BUG();
  6209. +
  6210. + list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
  6211. + obj = list_entry(lh, struct yaffs_obj, siblings);
  6212. + if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  6213. + yaffs_del_dir_contents(obj);
  6214. + yaffs_trace(YAFFS_TRACE_SCAN,
  6215. + "Deleting lost_found object %d",
  6216. + obj->obj_id);
  6217. + yaffs_unlink_obj(obj);
  6218. + }
  6219. +}
  6220. +
  6221. +static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
  6222. +{
  6223. + yaffs_del_dir_contents(dev->lost_n_found);
  6224. +}
  6225. +
  6226. +
  6227. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
  6228. + const YCHAR *name)
  6229. +{
  6230. + int sum;
  6231. + struct list_head *i;
  6232. + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  6233. + struct yaffs_obj *l;
  6234. +
  6235. + if (!name)
  6236. + return NULL;
  6237. +
  6238. + if (!directory) {
  6239. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  6240. + "tragedy: yaffs_find_by_name: null pointer directory"
  6241. + );
  6242. + BUG();
  6243. + return NULL;
  6244. + }
  6245. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  6246. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  6247. + "tragedy: yaffs_find_by_name: non-directory"
  6248. + );
  6249. + BUG();
  6250. + }
  6251. +
  6252. + sum = yaffs_calc_name_sum(name);
  6253. +
  6254. + list_for_each(i, &directory->variant.dir_variant.children) {
  6255. + l = list_entry(i, struct yaffs_obj, siblings);
  6256. +
  6257. + if (l->parent != directory)
  6258. + BUG();
  6259. +
  6260. + yaffs_check_obj_details_loaded(l);
  6261. +
  6262. + /* Special case for lost-n-found */
  6263. + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6264. + if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
  6265. + return l;
  6266. + } else if (l->sum == sum || l->hdr_chunk <= 0) {
  6267. + /* LostnFound chunk called Objxxx
  6268. + * Do a real check
  6269. + */
  6270. + yaffs_get_obj_name(l, buffer,
  6271. + YAFFS_MAX_NAME_LENGTH + 1);
  6272. + if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
  6273. + return l;
  6274. + }
  6275. + }
  6276. + return NULL;
  6277. +}
  6278. +
  6279. +/* GetEquivalentObject dereferences any hard links to get to the
  6280. + * actual object.
  6281. + */
  6282. +
  6283. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
  6284. +{
  6285. + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  6286. + obj = obj->variant.hardlink_variant.equiv_obj;
  6287. + yaffs_check_obj_details_loaded(obj);
  6288. + }
  6289. + return obj;
  6290. +}
  6291. +
  6292. +/*
  6293. + * A note or two on object names.
  6294. + * * If the object name is missing, we then make one up in the form objnnn
  6295. + *
  6296. + * * ASCII names are stored in the object header's name field from byte zero
  6297. + * * Unicode names are historically stored starting from byte zero.
  6298. + *
  6299. + * Then there are automatic Unicode names...
  6300. + * The purpose of these is to save names in a way that can be read as
  6301. + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
  6302. + * system to share files.
  6303. + *
  6304. + * These automatic unicode are stored slightly differently...
  6305. + * - If the name can fit in the ASCII character space then they are saved as
  6306. + * ascii names as per above.
  6307. + * - If the name needs Unicode then the name is saved in Unicode
  6308. + * starting at oh->name[1].
  6309. +
  6310. + */
  6311. +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
  6312. + int buffer_size)
  6313. +{
  6314. + /* Create an object name if we could not find one. */
  6315. + if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
  6316. + YCHAR local_name[20];
  6317. + YCHAR num_string[20];
  6318. + YCHAR *x = &num_string[19];
  6319. + unsigned v = obj->obj_id;
  6320. + num_string[19] = 0;
  6321. + while (v > 0) {
  6322. + x--;
  6323. + *x = '0' + (v % 10);
  6324. + v /= 10;
  6325. + }
  6326. + /* make up a name */
  6327. + strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
  6328. + strcat(local_name, x);
  6329. + strncpy(name, local_name, buffer_size - 1);
  6330. + }
  6331. +}
  6332. +
  6333. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
  6334. +{
  6335. + memset(name, 0, buffer_size * sizeof(YCHAR));
  6336. + yaffs_check_obj_details_loaded(obj);
  6337. + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  6338. + strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
  6339. + } else if (obj->short_name[0]) {
  6340. + strcpy(name, obj->short_name);
  6341. + } else if (obj->hdr_chunk > 0) {
  6342. + int result;
  6343. + u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
  6344. +
  6345. + struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
  6346. +
  6347. + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
  6348. +
  6349. + if (obj->hdr_chunk > 0) {
  6350. + result = yaffs_rd_chunk_tags_nand(obj->my_dev,
  6351. + obj->hdr_chunk,
  6352. + buffer, NULL);
  6353. + }
  6354. + yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
  6355. + buffer_size);
  6356. +
  6357. + yaffs_release_temp_buffer(obj->my_dev, buffer);
  6358. + }
  6359. +
  6360. + yaffs_fix_null_name(obj, name, buffer_size);
  6361. +
  6362. + return strnlen(name, YAFFS_MAX_NAME_LENGTH);
  6363. +}
  6364. +
  6365. +loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
  6366. +{
  6367. + /* Dereference any hard linking */
  6368. + obj = yaffs_get_equivalent_obj(obj);
  6369. +
  6370. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  6371. + return obj->variant.file_variant.file_size;
  6372. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  6373. + if (!obj->variant.symlink_variant.alias)
  6374. + return 0;
  6375. + return strnlen(obj->variant.symlink_variant.alias,
  6376. + YAFFS_MAX_ALIAS_LENGTH);
  6377. + } else {
  6378. + /* Only a directory should drop through to here */
  6379. + return obj->my_dev->data_bytes_per_chunk;
  6380. + }
  6381. +}
  6382. +
  6383. +int yaffs_get_obj_link_count(struct yaffs_obj *obj)
  6384. +{
  6385. + int count = 0;
  6386. + struct list_head *i;
  6387. +
  6388. + if (!obj->unlinked)
  6389. + count++; /* the object itself */
  6390. +
  6391. + list_for_each(i, &obj->hard_links)
  6392. + count++; /* add the hard links; */
  6393. +
  6394. + return count;
  6395. +}
  6396. +
  6397. +int yaffs_get_obj_inode(struct yaffs_obj *obj)
  6398. +{
  6399. + obj = yaffs_get_equivalent_obj(obj);
  6400. +
  6401. + return obj->obj_id;
  6402. +}
  6403. +
  6404. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
  6405. +{
  6406. + obj = yaffs_get_equivalent_obj(obj);
  6407. +
  6408. + switch (obj->variant_type) {
  6409. + case YAFFS_OBJECT_TYPE_FILE:
  6410. + return DT_REG;
  6411. + break;
  6412. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  6413. + return DT_DIR;
  6414. + break;
  6415. + case YAFFS_OBJECT_TYPE_SYMLINK:
  6416. + return DT_LNK;
  6417. + break;
  6418. + case YAFFS_OBJECT_TYPE_HARDLINK:
  6419. + return DT_REG;
  6420. + break;
  6421. + case YAFFS_OBJECT_TYPE_SPECIAL:
  6422. + if (S_ISFIFO(obj->yst_mode))
  6423. + return DT_FIFO;
  6424. + if (S_ISCHR(obj->yst_mode))
  6425. + return DT_CHR;
  6426. + if (S_ISBLK(obj->yst_mode))
  6427. + return DT_BLK;
  6428. + if (S_ISSOCK(obj->yst_mode))
  6429. + return DT_SOCK;
  6430. + return DT_REG;
  6431. + break;
  6432. + default:
  6433. + return DT_REG;
  6434. + break;
  6435. + }
  6436. +}
  6437. +
  6438. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
  6439. +{
  6440. + obj = yaffs_get_equivalent_obj(obj);
  6441. + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  6442. + return yaffs_clone_str(obj->variant.symlink_variant.alias);
  6443. + else
  6444. + return yaffs_clone_str(_Y(""));
  6445. +}
  6446. +
  6447. +/*--------------------------- Initialisation code -------------------------- */
  6448. +
  6449. +static int yaffs_check_dev_fns(struct yaffs_dev *dev)
  6450. +{
  6451. + struct yaffs_driver *drv = &dev->drv;
  6452. + struct yaffs_tags_handler *tagger = &dev->tagger;
  6453. +
  6454. + /* Common functions, gotta have */
  6455. + if (!drv->drv_read_chunk_fn ||
  6456. + !drv->drv_write_chunk_fn ||
  6457. + !drv->drv_erase_fn)
  6458. + return 0;
  6459. +
  6460. + if (dev->param.is_yaffs2 &&
  6461. + (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
  6462. + return 0;
  6463. +
  6464. + /* Install the default tags marshalling functions if needed. */
  6465. + yaffs_tags_compat_install(dev);
  6466. + yaffs_tags_marshall_install(dev);
  6467. +
  6468. + /* Check we now have the marshalling functions required. */
  6469. + if (!tagger->write_chunk_tags_fn ||
  6470. + !tagger->read_chunk_tags_fn ||
  6471. + !tagger->query_block_fn ||
  6472. + !tagger->mark_bad_fn)
  6473. + return 0;
  6474. +
  6475. + return 1;
  6476. +}
  6477. +
  6478. +static int yaffs_create_initial_dir(struct yaffs_dev *dev)
  6479. +{
  6480. + /* Initialise the unlinked, deleted, root and lost+found directories */
  6481. + dev->lost_n_found = dev->root_dir = NULL;
  6482. + dev->unlinked_dir = dev->del_dir = NULL;
  6483. + dev->unlinked_dir =
  6484. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  6485. + dev->del_dir =
  6486. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  6487. + dev->root_dir =
  6488. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
  6489. + YAFFS_ROOT_MODE | S_IFDIR);
  6490. + dev->lost_n_found =
  6491. + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
  6492. + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  6493. +
  6494. + if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
  6495. + && dev->del_dir) {
  6496. + yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
  6497. + return YAFFS_OK;
  6498. + }
  6499. + return YAFFS_FAIL;
  6500. +}
  6501. +
  6502. +/* Low level init.
  6503. + * Typically only used by yaffs_guts_initialise, but also used by the
  6504. + * Low level yaffs driver tests.
  6505. + */
  6506. +
  6507. +int yaffs_guts_ll_init(struct yaffs_dev *dev)
  6508. +{
  6509. +
  6510. +
  6511. + yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
  6512. +
  6513. + if (!dev) {
  6514. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  6515. + "yaffs: Need a device"
  6516. + );
  6517. + return YAFFS_FAIL;
  6518. + }
  6519. +
  6520. + if (dev->ll_init)
  6521. + return YAFFS_OK;
  6522. +
  6523. + dev->internal_start_block = dev->param.start_block;
  6524. + dev->internal_end_block = dev->param.end_block;
  6525. + dev->block_offset = 0;
  6526. + dev->chunk_offset = 0;
  6527. + dev->n_free_chunks = 0;
  6528. +
  6529. + dev->gc_block = 0;
  6530. +
  6531. + if (dev->param.start_block == 0) {
  6532. + dev->internal_start_block = dev->param.start_block + 1;
  6533. + dev->internal_end_block = dev->param.end_block + 1;
  6534. + dev->block_offset = 1;
  6535. + dev->chunk_offset = dev->param.chunks_per_block;
  6536. + }
  6537. +
  6538. + /* Check geometry parameters. */
  6539. +
  6540. + if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
  6541. + dev->param.total_bytes_per_chunk < 1024) ||
  6542. + (!dev->param.is_yaffs2 &&
  6543. + dev->param.total_bytes_per_chunk < 512) ||
  6544. + (dev->param.inband_tags && !dev->param.is_yaffs2) ||
  6545. + dev->param.chunks_per_block < 2 ||
  6546. + dev->param.n_reserved_blocks < 2 ||
  6547. + dev->internal_start_block <= 0 ||
  6548. + dev->internal_end_block <= 0 ||
  6549. + dev->internal_end_block <=
  6550. + (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
  6551. + ) {
  6552. + /* otherwise it is too small */
  6553. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  6554. + "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
  6555. + dev->param.total_bytes_per_chunk,
  6556. + dev->param.is_yaffs2 ? "2" : "",
  6557. + dev->param.inband_tags);
  6558. + return YAFFS_FAIL;
  6559. + }
  6560. +
  6561. + /* Sort out space for inband tags, if required */
  6562. + if (dev->param.inband_tags)
  6563. + dev->data_bytes_per_chunk =
  6564. + dev->param.total_bytes_per_chunk -
  6565. + sizeof(struct yaffs_packed_tags2_tags_only);
  6566. + else
  6567. + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
  6568. +
  6569. + /* Got the right mix of functions? */
  6570. + if (!yaffs_check_dev_fns(dev)) {
  6571. + /* Function missing */
  6572. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  6573. + "device function(s) missing or wrong");
  6574. +
  6575. + return YAFFS_FAIL;
  6576. + }
  6577. +
  6578. + if (yaffs_init_nand(dev) != YAFFS_OK) {
  6579. + yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
  6580. + return YAFFS_FAIL;
  6581. + }
  6582. +
  6583. + return YAFFS_OK;
  6584. +}
  6585. +
  6586. +
  6587. +int yaffs_guts_format_dev(struct yaffs_dev *dev)
  6588. +{
  6589. + int i;
  6590. + enum yaffs_block_state state;
  6591. + u32 dummy;
  6592. +
  6593. + if(yaffs_guts_ll_init(dev) != YAFFS_OK)
  6594. + return YAFFS_FAIL;
  6595. +
  6596. + if(dev->is_mounted)
  6597. + return YAFFS_FAIL;
  6598. +
  6599. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  6600. + yaffs_query_init_block_state(dev, i, &state, &dummy);
  6601. + if (state != YAFFS_BLOCK_STATE_DEAD)
  6602. + yaffs_erase_block(dev, i);
  6603. + }
  6604. +
  6605. + return YAFFS_OK;
  6606. +}
  6607. +
  6608. +
  6609. +int yaffs_guts_initialise(struct yaffs_dev *dev)
  6610. +{
  6611. + int init_failed = 0;
  6612. + unsigned x;
  6613. + int bits;
  6614. +
  6615. + if(yaffs_guts_ll_init(dev) != YAFFS_OK)
  6616. + return YAFFS_FAIL;
  6617. +
  6618. + if (dev->is_mounted) {
  6619. + yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
  6620. + return YAFFS_FAIL;
  6621. + }
  6622. +
  6623. + dev->is_mounted = 1;
  6624. +
  6625. + /* OK now calculate a few things for the device */
  6626. +
  6627. + /*
  6628. + * Calculate all the chunk size manipulation numbers:
  6629. + */
  6630. + x = dev->data_bytes_per_chunk;
  6631. + /* We always use dev->chunk_shift and dev->chunk_div */
  6632. + dev->chunk_shift = calc_shifts(x);
  6633. + x >>= dev->chunk_shift;
  6634. + dev->chunk_div = x;
  6635. + /* We only use chunk mask if chunk_div is 1 */
  6636. + dev->chunk_mask = (1 << dev->chunk_shift) - 1;
  6637. +
  6638. + /*
  6639. + * Calculate chunk_grp_bits.
  6640. + * We need to find the next power of 2 > than internal_end_block
  6641. + */
  6642. +
  6643. + x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
  6644. +
  6645. + bits = calc_shifts_ceiling(x);
  6646. +
  6647. + /* Set up tnode width if wide tnodes are enabled. */
  6648. + if (!dev->param.wide_tnodes_disabled) {
  6649. + /* bits must be even so that we end up with 32-bit words */
  6650. + if (bits & 1)
  6651. + bits++;
  6652. + if (bits < 16)
  6653. + dev->tnode_width = 16;
  6654. + else
  6655. + dev->tnode_width = bits;
  6656. + } else {
  6657. + dev->tnode_width = 16;
  6658. + }
  6659. +
  6660. + dev->tnode_mask = (1 << dev->tnode_width) - 1;
  6661. +
  6662. + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  6663. + * so if the bitwidth of the
  6664. + * chunk range we're using is greater than 16 we need
  6665. + * to figure out chunk shift and chunk_grp_size
  6666. + */
  6667. +
  6668. + if (bits <= dev->tnode_width)
  6669. + dev->chunk_grp_bits = 0;
  6670. + else
  6671. + dev->chunk_grp_bits = bits - dev->tnode_width;
  6672. +
  6673. + dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
  6674. + if (dev->tnode_size < sizeof(struct yaffs_tnode))
  6675. + dev->tnode_size = sizeof(struct yaffs_tnode);
  6676. +
  6677. + dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
  6678. +
  6679. + if (dev->param.chunks_per_block < dev->chunk_grp_size) {
  6680. + /* We have a problem because the soft delete won't work if
  6681. + * the chunk group size > chunks per block.
  6682. + * This can be remedied by using larger "virtual blocks".
  6683. + */
  6684. + yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
  6685. +
  6686. + return YAFFS_FAIL;
  6687. + }
  6688. +
  6689. + /* Finished verifying the device, continue with initialisation */
  6690. +
  6691. + /* More device initialisation */
  6692. + dev->all_gcs = 0;
  6693. + dev->passive_gc_count = 0;
  6694. + dev->oldest_dirty_gc_count = 0;
  6695. + dev->bg_gcs = 0;
  6696. + dev->gc_block_finder = 0;
  6697. + dev->buffered_block = -1;
  6698. + dev->doing_buffered_block_rewrite = 0;
  6699. + dev->n_deleted_files = 0;
  6700. + dev->n_bg_deletions = 0;
  6701. + dev->n_unlinked_files = 0;
  6702. + dev->n_ecc_fixed = 0;
  6703. + dev->n_ecc_unfixed = 0;
  6704. + dev->n_tags_ecc_fixed = 0;
  6705. + dev->n_tags_ecc_unfixed = 0;
  6706. + dev->n_erase_failures = 0;
  6707. + dev->n_erased_blocks = 0;
  6708. + dev->gc_disable = 0;
  6709. + dev->has_pending_prioritised_gc = 1;
  6710. + /* Assume the worst for now, will get fixed on first GC */
  6711. + INIT_LIST_HEAD(&dev->dirty_dirs);
  6712. + dev->oldest_dirty_seq = 0;
  6713. + dev->oldest_dirty_block = 0;
  6714. +
  6715. + /* Initialise temporary buffers and caches. */
  6716. + if (!yaffs_init_tmp_buffers(dev))
  6717. + init_failed = 1;
  6718. +
  6719. + dev->cache = NULL;
  6720. + dev->gc_cleanup_list = NULL;
  6721. +
  6722. + if (!init_failed && dev->param.n_caches > 0) {
  6723. + int i;
  6724. + void *buf;
  6725. + int cache_bytes =
  6726. + dev->param.n_caches * sizeof(struct yaffs_cache);
  6727. +
  6728. + if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
  6729. + dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
  6730. +
  6731. + dev->cache = kmalloc(cache_bytes, GFP_NOFS);
  6732. +
  6733. + buf = (u8 *) dev->cache;
  6734. +
  6735. + if (dev->cache)
  6736. + memset(dev->cache, 0, cache_bytes);
  6737. +
  6738. + for (i = 0; i < dev->param.n_caches && buf; i++) {
  6739. + dev->cache[i].object = NULL;
  6740. + dev->cache[i].last_use = 0;
  6741. + dev->cache[i].dirty = 0;
  6742. + dev->cache[i].data = buf =
  6743. + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  6744. + }
  6745. + if (!buf)
  6746. + init_failed = 1;
  6747. +
  6748. + dev->cache_last_use = 0;
  6749. + }
  6750. +
  6751. + dev->cache_hits = 0;
  6752. +
  6753. + if (!init_failed) {
  6754. + dev->gc_cleanup_list =
  6755. + kmalloc(dev->param.chunks_per_block * sizeof(u32),
  6756. + GFP_NOFS);
  6757. + if (!dev->gc_cleanup_list)
  6758. + init_failed = 1;
  6759. + }
  6760. +
  6761. + if (dev->param.is_yaffs2)
  6762. + dev->param.use_header_file_size = 1;
  6763. +
  6764. + if (!init_failed && !yaffs_init_blocks(dev))
  6765. + init_failed = 1;
  6766. +
  6767. + yaffs_init_tnodes_and_objs(dev);
  6768. +
  6769. + if (!init_failed && !yaffs_create_initial_dir(dev))
  6770. + init_failed = 1;
  6771. +
  6772. + if (!init_failed && dev->param.is_yaffs2 &&
  6773. + !dev->param.disable_summary &&
  6774. + !yaffs_summary_init(dev))
  6775. + init_failed = 1;
  6776. +
  6777. + if (!init_failed) {
  6778. + /* Now scan the flash. */
  6779. + if (dev->param.is_yaffs2) {
  6780. + if (yaffs2_checkpt_restore(dev)) {
  6781. + yaffs_check_obj_details_loaded(dev->root_dir);
  6782. + yaffs_trace(YAFFS_TRACE_CHECKPOINT |
  6783. + YAFFS_TRACE_MOUNT,
  6784. + "yaffs: restored from checkpoint"
  6785. + );
  6786. + } else {
  6787. +
  6788. + /* Clean up the mess caused by an aborted
  6789. + * checkpoint load then scan backwards.
  6790. + */
  6791. + yaffs_deinit_blocks(dev);
  6792. +
  6793. + yaffs_deinit_tnodes_and_objs(dev);
  6794. +
  6795. + dev->n_erased_blocks = 0;
  6796. + dev->n_free_chunks = 0;
  6797. + dev->alloc_block = -1;
  6798. + dev->alloc_page = -1;
  6799. + dev->n_deleted_files = 0;
  6800. + dev->n_unlinked_files = 0;
  6801. + dev->n_bg_deletions = 0;
  6802. +
  6803. + if (!init_failed && !yaffs_init_blocks(dev))
  6804. + init_failed = 1;
  6805. +
  6806. + yaffs_init_tnodes_and_objs(dev);
  6807. +
  6808. + if (!init_failed
  6809. + && !yaffs_create_initial_dir(dev))
  6810. + init_failed = 1;
  6811. +
  6812. + if (!init_failed && !yaffs2_scan_backwards(dev))
  6813. + init_failed = 1;
  6814. + }
  6815. + } else if (!yaffs1_scan(dev)) {
  6816. + init_failed = 1;
  6817. + }
  6818. +
  6819. + yaffs_strip_deleted_objs(dev);
  6820. + yaffs_fix_hanging_objs(dev);
  6821. + if (dev->param.empty_lost_n_found)
  6822. + yaffs_empty_l_n_f(dev);
  6823. + }
  6824. +
  6825. + if (init_failed) {
  6826. + /* Clean up the mess */
  6827. + yaffs_trace(YAFFS_TRACE_TRACING,
  6828. + "yaffs: yaffs_guts_initialise() aborted.");
  6829. +
  6830. + yaffs_deinitialise(dev);
  6831. + return YAFFS_FAIL;
  6832. + }
  6833. +
  6834. + /* Zero out stats */
  6835. + dev->n_page_reads = 0;
  6836. + dev->n_page_writes = 0;
  6837. + dev->n_erasures = 0;
  6838. + dev->n_gc_copies = 0;
  6839. + dev->n_retried_writes = 0;
  6840. +
  6841. + dev->n_retired_blocks = 0;
  6842. +
  6843. + yaffs_verify_free_chunks(dev);
  6844. + yaffs_verify_blocks(dev);
  6845. +
  6846. + /* Clean up any aborted checkpoint data */
  6847. + if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
  6848. + yaffs2_checkpt_invalidate(dev);
  6849. +
  6850. + yaffs_trace(YAFFS_TRACE_TRACING,
  6851. + "yaffs: yaffs_guts_initialise() done.");
  6852. + return YAFFS_OK;
  6853. +}
  6854. +
  6855. +void yaffs_deinitialise(struct yaffs_dev *dev)
  6856. +{
  6857. + if (dev->is_mounted) {
  6858. + int i;
  6859. +
  6860. + yaffs_deinit_blocks(dev);
  6861. + yaffs_deinit_tnodes_and_objs(dev);
  6862. + yaffs_summary_deinit(dev);
  6863. +
  6864. + if (dev->param.n_caches > 0 && dev->cache) {
  6865. +
  6866. + for (i = 0; i < dev->param.n_caches; i++) {
  6867. + kfree(dev->cache[i].data);
  6868. + dev->cache[i].data = NULL;
  6869. + }
  6870. +
  6871. + kfree(dev->cache);
  6872. + dev->cache = NULL;
  6873. + }
  6874. +
  6875. + kfree(dev->gc_cleanup_list);
  6876. +
  6877. + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  6878. + kfree(dev->temp_buffer[i].buffer);
  6879. +
  6880. + dev->is_mounted = 0;
  6881. +
  6882. + yaffs_deinit_nand(dev);
  6883. + }
  6884. +}
  6885. +
  6886. +int yaffs_count_free_chunks(struct yaffs_dev *dev)
  6887. +{
  6888. + int n_free = 0;
  6889. + int b;
  6890. + struct yaffs_block_info *blk;
  6891. +
  6892. + blk = dev->block_info;
  6893. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  6894. + switch (blk->block_state) {
  6895. + case YAFFS_BLOCK_STATE_EMPTY:
  6896. + case YAFFS_BLOCK_STATE_ALLOCATING:
  6897. + case YAFFS_BLOCK_STATE_COLLECTING:
  6898. + case YAFFS_BLOCK_STATE_FULL:
  6899. + n_free +=
  6900. + (dev->param.chunks_per_block - blk->pages_in_use +
  6901. + blk->soft_del_pages);
  6902. + break;
  6903. + default:
  6904. + break;
  6905. + }
  6906. + blk++;
  6907. + }
  6908. + return n_free;
  6909. +}
  6910. +
  6911. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
  6912. +{
  6913. + /* This is what we report to the outside world */
  6914. + int n_free;
  6915. + int n_dirty_caches;
  6916. + int blocks_for_checkpt;
  6917. + int i;
  6918. +
  6919. + n_free = dev->n_free_chunks;
  6920. + n_free += dev->n_deleted_files;
  6921. +
  6922. + /* Now count and subtract the number of dirty chunks in the cache. */
  6923. +
  6924. + for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
  6925. + if (dev->cache[i].dirty)
  6926. + n_dirty_caches++;
  6927. + }
  6928. +
  6929. + n_free -= n_dirty_caches;
  6930. +
  6931. + n_free -=
  6932. + ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
  6933. +
  6934. + /* Now figure checkpoint space and report that... */
  6935. + blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
  6936. +
  6937. + n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
  6938. +
  6939. + if (n_free < 0)
  6940. + n_free = 0;
  6941. +
  6942. + return n_free;
  6943. +}
  6944. +
  6945. +
  6946. +
  6947. +/*
  6948. + * Marshalling functions to get loff_t file sizes into and out of
  6949. + * object headers.
  6950. + */
  6951. +void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
  6952. +{
  6953. + oh->file_size_low = (fsize & 0xFFFFFFFF);
  6954. + oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
  6955. +}
  6956. +
  6957. +loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
  6958. +{
  6959. + loff_t retval;
  6960. +
  6961. + if (sizeof(loff_t) >= 8 && ~(oh->file_size_high))
  6962. + retval = (((loff_t) oh->file_size_high) << 32) |
  6963. + (((loff_t) oh->file_size_low) & 0xFFFFFFFF);
  6964. + else
  6965. + retval = (loff_t) oh->file_size_low;
  6966. +
  6967. + return retval;
  6968. +}
  6969. +
  6970. +
  6971. +void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
  6972. +{
  6973. + int i;
  6974. + struct yaffs_block_info *bi;
  6975. + int s;
  6976. +
  6977. + for(i = 0; i < 10; i++)
  6978. + bs[i] = 0;
  6979. +
  6980. + for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  6981. + bi = yaffs_get_block_info(dev, i);
  6982. + s = bi->block_state;
  6983. + if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)
  6984. + bs[0]++;
  6985. + else
  6986. + bs[s]++;
  6987. + }
  6988. +}
  6989. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_guts.h linux-3.15-rc5/fs/yaffs2/yaffs_guts.h
  6990. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  6991. +++ linux-3.15-rc5/fs/yaffs2/yaffs_guts.h 2014-05-17 01:53:27.000000000 +0200
  6992. @@ -0,0 +1,1007 @@
  6993. +/*
  6994. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  6995. + *
  6996. + * Copyright (C) 2002-2011 Aleph One Ltd.
  6997. + * for Toby Churchill Ltd and Brightstar Engineering
  6998. + *
  6999. + * Created by Charles Manning <charles@aleph1.co.uk>
  7000. + *
  7001. + * This program is free software; you can redistribute it and/or modify
  7002. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  7003. + * published by the Free Software Foundation.
  7004. + *
  7005. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  7006. + */
  7007. +
  7008. +#ifndef __YAFFS_GUTS_H__
  7009. +#define __YAFFS_GUTS_H__
  7010. +
  7011. +#include "yportenv.h"
  7012. +
  7013. +#define YAFFS_OK 1
  7014. +#define YAFFS_FAIL 0
  7015. +
  7016. +/* Give us a Y=0x59,
  7017. + * Give us an A=0x41,
  7018. + * Give us an FF=0xff
  7019. + * Give us an S=0x53
  7020. + * And what have we got...
  7021. + */
  7022. +#define YAFFS_MAGIC 0x5941ff53
  7023. +
  7024. +/*
  7025. + * Tnodes form a tree with the tnodes in "levels"
  7026. + * Levels greater than 0 hold 8 slots which point to other tnodes.
  7027. + * Those at level 0 hold 16 slots which point to chunks in NAND.
  7028. + *
  7029. + * A maximum level of 8 thust supports files of size up to:
  7030. + *
  7031. + * 2^(3*MAX_LEVEL+4)
  7032. + *
  7033. + * Thus a max level of 8 supports files with up to 2^^28 chunks which gives
  7034. + * a maximum file size of around 512Gbytees with 2k chunks.
  7035. + */
  7036. +#define YAFFS_NTNODES_LEVEL0 16
  7037. +#define YAFFS_TNODES_LEVEL0_BITS 4
  7038. +#define YAFFS_TNODES_LEVEL0_MASK 0xf
  7039. +
  7040. +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  7041. +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  7042. +#define YAFFS_TNODES_INTERNAL_MASK 0x7
  7043. +#define YAFFS_TNODES_MAX_LEVEL 8
  7044. +#define YAFFS_TNODES_MAX_BITS (YAFFS_TNODES_LEVEL0_BITS + \
  7045. + YAFFS_TNODES_INTERNAL_BITS * \
  7046. + YAFFS_TNODES_MAX_LEVEL)
  7047. +#define YAFFS_MAX_CHUNK_ID ((1 << YAFFS_TNODES_MAX_BITS) - 1)
  7048. +
  7049. +#define YAFFS_MAX_FILE_SIZE_32 0x7fffffff
  7050. +
  7051. +/* Constants for YAFFS1 mode */
  7052. +#define YAFFS_BYTES_PER_SPARE 16
  7053. +#define YAFFS_BYTES_PER_CHUNK 512
  7054. +#define YAFFS_CHUNK_SIZE_SHIFT 9
  7055. +#define YAFFS_CHUNKS_PER_BLOCK 32
  7056. +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  7057. +
  7058. +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  7059. +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  7060. +
  7061. +
  7062. +
  7063. +#define YAFFS_ALLOCATION_NOBJECTS 100
  7064. +#define YAFFS_ALLOCATION_NTNODES 100
  7065. +#define YAFFS_ALLOCATION_NLINKS 100
  7066. +
  7067. +#define YAFFS_NOBJECT_BUCKETS 256
  7068. +
  7069. +#define YAFFS_OBJECT_SPACE 0x40000
  7070. +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE - 1)
  7071. +
  7072. +/* Binary data version stamps */
  7073. +#define YAFFS_SUMMARY_VERSION 1
  7074. +#define YAFFS_CHECKPOINT_VERSION 7
  7075. +
  7076. +#ifdef CONFIG_YAFFS_UNICODE
  7077. +#define YAFFS_MAX_NAME_LENGTH 127
  7078. +#define YAFFS_MAX_ALIAS_LENGTH 79
  7079. +#else
  7080. +#define YAFFS_MAX_NAME_LENGTH 255
  7081. +#define YAFFS_MAX_ALIAS_LENGTH 159
  7082. +#endif
  7083. +
  7084. +#define YAFFS_SHORT_NAME_LENGTH 15
  7085. +
  7086. +/* Some special object ids for pseudo objects */
  7087. +#define YAFFS_OBJECTID_ROOT 1
  7088. +#define YAFFS_OBJECTID_LOSTNFOUND 2
  7089. +#define YAFFS_OBJECTID_UNLINKED 3
  7090. +#define YAFFS_OBJECTID_DELETED 4
  7091. +
  7092. +/* Fake object Id for summary data */
  7093. +#define YAFFS_OBJECTID_SUMMARY 0x10
  7094. +
  7095. +/* Pseudo object ids for checkpointing */
  7096. +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  7097. +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  7098. +
  7099. +#define YAFFS_MAX_SHORT_OP_CACHES 20
  7100. +
  7101. +#define YAFFS_N_TEMP_BUFFERS 6
  7102. +
  7103. +/* We limit the number attempts at sucessfully saving a chunk of data.
  7104. + * Small-page devices have 32 pages per block; large-page devices have 64.
  7105. + * Default to something in the order of 5 to 10 blocks worth of chunks.
  7106. + */
  7107. +#define YAFFS_WR_ATTEMPTS (5*64)
  7108. +
  7109. +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
  7110. + * The range is limited slightly to help distinguish bad numbers from good.
  7111. + * This also allows us to perhaps in the future use special numbers for
  7112. + * special purposes.
  7113. + * EFFFFF00 allows the allocation of 8 blocks/second (~1Mbytes) for 15 years,
  7114. + * and is a larger number than the lifetime of a 2GB device.
  7115. + */
  7116. +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  7117. +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xefffff00
  7118. +
  7119. +/* Special sequence number for bad block that failed to be marked bad */
  7120. +#define YAFFS_SEQUENCE_BAD_BLOCK 0xffff0000
  7121. +
  7122. +/* ChunkCache is used for short read/write operations.*/
  7123. +struct yaffs_cache {
  7124. + struct yaffs_obj *object;
  7125. + int chunk_id;
  7126. + int last_use;
  7127. + int dirty;
  7128. + int n_bytes; /* Only valid if the cache is dirty */
  7129. + int locked; /* Can't push out or flush while locked. */
  7130. + u8 *data;
  7131. +};
  7132. +
  7133. +/* yaffs1 tags structures in RAM
  7134. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary
  7135. + * otherwise the structure size will get blown out.
  7136. + */
  7137. +
  7138. +struct yaffs_tags {
  7139. + u32 chunk_id:20;
  7140. + u32 serial_number:2;
  7141. + u32 n_bytes_lsb:10;
  7142. + u32 obj_id:18;
  7143. + u32 ecc:12;
  7144. + u32 n_bytes_msb:2;
  7145. +};
  7146. +
  7147. +union yaffs_tags_union {
  7148. + struct yaffs_tags as_tags;
  7149. + u8 as_bytes[8];
  7150. +};
  7151. +
  7152. +
  7153. +/* Stuff used for extended tags in YAFFS2 */
  7154. +
  7155. +enum yaffs_ecc_result {
  7156. + YAFFS_ECC_RESULT_UNKNOWN,
  7157. + YAFFS_ECC_RESULT_NO_ERROR,
  7158. + YAFFS_ECC_RESULT_FIXED,
  7159. + YAFFS_ECC_RESULT_UNFIXED
  7160. +};
  7161. +
  7162. +enum yaffs_obj_type {
  7163. + YAFFS_OBJECT_TYPE_UNKNOWN,
  7164. + YAFFS_OBJECT_TYPE_FILE,
  7165. + YAFFS_OBJECT_TYPE_SYMLINK,
  7166. + YAFFS_OBJECT_TYPE_DIRECTORY,
  7167. + YAFFS_OBJECT_TYPE_HARDLINK,
  7168. + YAFFS_OBJECT_TYPE_SPECIAL
  7169. +};
  7170. +
  7171. +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  7172. +
  7173. +struct yaffs_ext_tags {
  7174. + unsigned chunk_used; /* Status of the chunk: used or unused */
  7175. + unsigned obj_id; /* If 0 this is not used */
  7176. + unsigned chunk_id; /* If 0 this is a header, else a data chunk */
  7177. + unsigned n_bytes; /* Only valid for data chunks */
  7178. +
  7179. + /* The following stuff only has meaning when we read */
  7180. + enum yaffs_ecc_result ecc_result;
  7181. + unsigned block_bad;
  7182. +
  7183. + /* YAFFS 1 stuff */
  7184. + unsigned is_deleted; /* The chunk is marked deleted */
  7185. + unsigned serial_number; /* Yaffs1 2-bit serial number */
  7186. +
  7187. + /* YAFFS2 stuff */
  7188. + unsigned seq_number; /* The sequence number of this block */
  7189. +
  7190. + /* Extra info if this is an object header (YAFFS2 only) */
  7191. +
  7192. + unsigned extra_available; /* Extra info available if not zero */
  7193. + unsigned extra_parent_id; /* The parent object */
  7194. + unsigned extra_is_shrink; /* Is it a shrink header? */
  7195. + unsigned extra_shadows; /* Does this shadow another object? */
  7196. +
  7197. + enum yaffs_obj_type extra_obj_type; /* What object type? */
  7198. +
  7199. + loff_t extra_file_size; /* Length if it is a file */
  7200. + unsigned extra_equiv_id; /* Equivalent object for a hard link */
  7201. +};
  7202. +
  7203. +/* Spare structure for YAFFS1 */
  7204. +struct yaffs_spare {
  7205. + u8 tb0;
  7206. + u8 tb1;
  7207. + u8 tb2;
  7208. + u8 tb3;
  7209. + u8 page_status; /* set to 0 to delete the chunk */
  7210. + u8 block_status;
  7211. + u8 tb4;
  7212. + u8 tb5;
  7213. + u8 ecc1[3];
  7214. + u8 tb6;
  7215. + u8 tb7;
  7216. + u8 ecc2[3];
  7217. +};
  7218. +
  7219. +/*Special structure for passing through to mtd */
  7220. +struct yaffs_nand_spare {
  7221. + struct yaffs_spare spare;
  7222. + int eccres1;
  7223. + int eccres2;
  7224. +};
  7225. +
  7226. +/* Block data in RAM */
  7227. +
  7228. +enum yaffs_block_state {
  7229. + YAFFS_BLOCK_STATE_UNKNOWN = 0,
  7230. +
  7231. + YAFFS_BLOCK_STATE_SCANNING,
  7232. + /* Being scanned */
  7233. +
  7234. + YAFFS_BLOCK_STATE_NEEDS_SCAN,
  7235. + /* The block might have something on it (ie it is allocating or full,
  7236. + * perhaps empty) but it needs to be scanned to determine its true
  7237. + * state.
  7238. + * This state is only valid during scanning.
  7239. + * NB We tolerate empty because the pre-scanner might be incapable of
  7240. + * deciding
  7241. + * However, if this state is returned on a YAFFS2 device,
  7242. + * then we expect a sequence number
  7243. + */
  7244. +
  7245. + YAFFS_BLOCK_STATE_EMPTY,
  7246. + /* This block is empty */
  7247. +
  7248. + YAFFS_BLOCK_STATE_ALLOCATING,
  7249. + /* This block is partially allocated.
  7250. + * At least one page holds valid data.
  7251. + * This is the one currently being used for page
  7252. + * allocation. Should never be more than one of these.
  7253. + * If a block is only partially allocated at mount it is treated as
  7254. + * full.
  7255. + */
  7256. +
  7257. + YAFFS_BLOCK_STATE_FULL,
  7258. + /* All the pages in this block have been allocated.
  7259. + * If a block was only partially allocated when mounted we treat
  7260. + * it as fully allocated.
  7261. + */
  7262. +
  7263. + YAFFS_BLOCK_STATE_DIRTY,
  7264. + /* The block was full and now all chunks have been deleted.
  7265. + * Erase me, reuse me.
  7266. + */
  7267. +
  7268. + YAFFS_BLOCK_STATE_CHECKPOINT,
  7269. + /* This block is assigned to holding checkpoint data. */
  7270. +
  7271. + YAFFS_BLOCK_STATE_COLLECTING,
  7272. + /* This block is being garbage collected */
  7273. +
  7274. + YAFFS_BLOCK_STATE_DEAD
  7275. + /* This block has failed and is not in use */
  7276. +};
  7277. +
  7278. +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  7279. +
  7280. +struct yaffs_block_info {
  7281. +
  7282. + s32 soft_del_pages:10; /* number of soft deleted pages */
  7283. + s32 pages_in_use:10; /* number of pages in use */
  7284. + u32 block_state:4; /* One of the above block states. */
  7285. + /* NB use unsigned because enum is sometimes
  7286. + * an int */
  7287. + u32 needs_retiring:1; /* Data has failed on this block, */
  7288. + /*need to get valid data off and retire*/
  7289. + u32 skip_erased_check:1;/* Skip the erased check on this block */
  7290. + u32 gc_prioritise:1; /* An ECC check or blank check has failed.
  7291. + Block should be prioritised for GC */
  7292. + u32 chunk_error_strikes:3; /* How many times we've had ecc etc
  7293. + failures on this block and tried to reuse it */
  7294. + u32 has_summary:1; /* The block has a summary */
  7295. +
  7296. + u32 has_shrink_hdr:1; /* This block has at least one shrink header */
  7297. + u32 seq_number; /* block sequence number for yaffs2 */
  7298. +
  7299. +};
  7300. +
  7301. +/* -------------------------- Object structure -------------------------------*/
  7302. +/* This is the object structure as stored on NAND */
  7303. +
  7304. +struct yaffs_obj_hdr {
  7305. + enum yaffs_obj_type type;
  7306. +
  7307. + /* Apply to everything */
  7308. + int parent_obj_id;
  7309. + u16 sum_no_longer_used; /* checksum of name. No longer used */
  7310. + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  7311. +
  7312. + /* The following apply to all object types except for hard links */
  7313. + u32 yst_mode; /* protection */
  7314. +
  7315. + u32 yst_uid;
  7316. + u32 yst_gid;
  7317. + u32 yst_atime;
  7318. + u32 yst_mtime;
  7319. + u32 yst_ctime;
  7320. +
  7321. + /* File size applies to files only */
  7322. + u32 file_size_low;
  7323. +
  7324. + /* Equivalent object id applies to hard links only. */
  7325. + int equiv_id;
  7326. +
  7327. + /* Alias is for symlinks only. */
  7328. + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  7329. +
  7330. + u32 yst_rdev; /* stuff for block and char devices (major/min) */
  7331. +
  7332. + u32 win_ctime[2];
  7333. + u32 win_atime[2];
  7334. + u32 win_mtime[2];
  7335. +
  7336. + u32 inband_shadowed_obj_id;
  7337. + u32 inband_is_shrink;
  7338. +
  7339. + u32 file_size_high;
  7340. + u32 reserved[1];
  7341. + int shadows_obj; /* This object header shadows the
  7342. + specified object if > 0 */
  7343. +
  7344. + /* is_shrink applies to object headers written when wemake a hole. */
  7345. + u32 is_shrink;
  7346. +
  7347. +};
  7348. +
  7349. +/*--------------------------- Tnode -------------------------- */
  7350. +
  7351. +struct yaffs_tnode {
  7352. + struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
  7353. +};
  7354. +
  7355. +/*------------------------ Object -----------------------------*/
  7356. +/* An object can be one of:
  7357. + * - a directory (no data, has children links
  7358. + * - a regular file (data.... not prunes :->).
  7359. + * - a symlink [symbolic link] (the alias).
  7360. + * - a hard link
  7361. + */
  7362. +
  7363. +struct yaffs_file_var {
  7364. + loff_t file_size;
  7365. + loff_t scanned_size;
  7366. + loff_t shrink_size;
  7367. + int top_level;
  7368. + struct yaffs_tnode *top;
  7369. +};
  7370. +
  7371. +struct yaffs_dir_var {
  7372. + struct list_head children; /* list of child links */
  7373. + struct list_head dirty; /* Entry for list of dirty directories */
  7374. +};
  7375. +
  7376. +struct yaffs_symlink_var {
  7377. + YCHAR *alias;
  7378. +};
  7379. +
  7380. +struct yaffs_hardlink_var {
  7381. + struct yaffs_obj *equiv_obj;
  7382. + u32 equiv_id;
  7383. +};
  7384. +
  7385. +union yaffs_obj_var {
  7386. + struct yaffs_file_var file_variant;
  7387. + struct yaffs_dir_var dir_variant;
  7388. + struct yaffs_symlink_var symlink_variant;
  7389. + struct yaffs_hardlink_var hardlink_variant;
  7390. +};
  7391. +
  7392. +struct yaffs_obj {
  7393. + u8 deleted:1; /* This should only apply to unlinked files. */
  7394. + u8 soft_del:1; /* it has also been soft deleted */
  7395. + u8 unlinked:1; /* An unlinked file.*/
  7396. + u8 fake:1; /* A fake object has no presence on NAND. */
  7397. + u8 rename_allowed:1; /* Some objects cannot be renamed. */
  7398. + u8 unlink_allowed:1;
  7399. + u8 dirty:1; /* the object needs to be written to flash */
  7400. + u8 valid:1; /* When the file system is being loaded up, this
  7401. + * object might be created before the data
  7402. + * is available
  7403. + * ie. file data chunks encountered before
  7404. + * the header.
  7405. + */
  7406. + u8 lazy_loaded:1; /* This object has been lazy loaded and
  7407. + * is missing some detail */
  7408. +
  7409. + u8 defered_free:1; /* Object is removed from NAND, but is
  7410. + * still in the inode cache.
  7411. + * Free of object is defered.
  7412. + * until the inode is released.
  7413. + */
  7414. + u8 being_created:1; /* This object is still being created
  7415. + * so skip some verification checks. */
  7416. + u8 is_shadowed:1; /* This object is shadowed on the way
  7417. + * to being renamed. */
  7418. +
  7419. + u8 xattr_known:1; /* We know if this has object has xattribs
  7420. + * or not. */
  7421. + u8 has_xattr:1; /* This object has xattribs.
  7422. + * Only valid if xattr_known. */
  7423. +
  7424. + u8 serial; /* serial number of chunk in NAND.*/
  7425. + u16 sum; /* sum of the name to speed searching */
  7426. +
  7427. + struct yaffs_dev *my_dev; /* The device I'm on */
  7428. +
  7429. + struct list_head hash_link; /* list of objects in hash bucket */
  7430. +
  7431. + struct list_head hard_links; /* hard linked object chain*/
  7432. +
  7433. + /* directory structure stuff */
  7434. + /* also used for linking up the free list */
  7435. + struct yaffs_obj *parent;
  7436. + struct list_head siblings;
  7437. +
  7438. + /* Where's my object header in NAND? */
  7439. + int hdr_chunk;
  7440. +
  7441. + int n_data_chunks; /* Number of data chunks for this file. */
  7442. +
  7443. + u32 obj_id; /* the object id value */
  7444. +
  7445. + u32 yst_mode;
  7446. +
  7447. + YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
  7448. +
  7449. +#ifdef CONFIG_YAFFS_WINCE
  7450. + u32 win_ctime[2];
  7451. + u32 win_mtime[2];
  7452. + u32 win_atime[2];
  7453. +#else
  7454. + u32 yst_uid;
  7455. + u32 yst_gid;
  7456. + u32 yst_atime;
  7457. + u32 yst_mtime;
  7458. + u32 yst_ctime;
  7459. +#endif
  7460. +
  7461. + u32 yst_rdev;
  7462. +
  7463. + void *my_inode;
  7464. +
  7465. + enum yaffs_obj_type variant_type;
  7466. +
  7467. + union yaffs_obj_var variant;
  7468. +
  7469. +};
  7470. +
  7471. +struct yaffs_obj_bucket {
  7472. + struct list_head list;
  7473. + int count;
  7474. +};
  7475. +
  7476. +/* yaffs_checkpt_obj holds the definition of an object as dumped
  7477. + * by checkpointing.
  7478. + */
  7479. +
  7480. +struct yaffs_checkpt_obj {
  7481. + int struct_type;
  7482. + u32 obj_id;
  7483. + u32 parent_id;
  7484. + int hdr_chunk;
  7485. + enum yaffs_obj_type variant_type:3;
  7486. + u8 deleted:1;
  7487. + u8 soft_del:1;
  7488. + u8 unlinked:1;
  7489. + u8 fake:1;
  7490. + u8 rename_allowed:1;
  7491. + u8 unlink_allowed:1;
  7492. + u8 serial;
  7493. + int n_data_chunks;
  7494. + loff_t size_or_equiv_obj;
  7495. +};
  7496. +
  7497. +/*--------------------- Temporary buffers ----------------
  7498. + *
  7499. + * These are chunk-sized working buffers. Each device has a few.
  7500. + */
  7501. +
  7502. +struct yaffs_buffer {
  7503. + u8 *buffer;
  7504. + int in_use;
  7505. +};
  7506. +
  7507. +/*----------------- Device ---------------------------------*/
  7508. +
  7509. +struct yaffs_param {
  7510. + const YCHAR *name;
  7511. +
  7512. + /*
  7513. + * Entry parameters set up way early. Yaffs sets up the rest.
  7514. + * The structure should be zeroed out before use so that unused
  7515. + * and default values are zero.
  7516. + */
  7517. +
  7518. + int inband_tags; /* Use unband tags */
  7519. + u32 total_bytes_per_chunk; /* Should be >= 512, does not need to
  7520. + be a power of 2 */
  7521. + int chunks_per_block; /* does not need to be a power of 2 */
  7522. + int spare_bytes_per_chunk; /* spare area size */
  7523. + int start_block; /* Start block we're allowed to use */
  7524. + int end_block; /* End block we're allowed to use */
  7525. + int n_reserved_blocks; /* Tuneable so that we can reduce
  7526. + * reserved blocks on NOR and RAM. */
  7527. +
  7528. + int n_caches; /* If <= 0, then short op caching is disabled,
  7529. + * else the number of short op caches.
  7530. + */
  7531. + int cache_bypass_aligned; /* If non-zero then bypass the cache for
  7532. + * aligned writes.
  7533. + */
  7534. +
  7535. + int use_nand_ecc; /* Flag to decide whether or not to use
  7536. + * NAND driver ECC on data (yaffs1) */
  7537. + int tags_9bytes; /* Use 9 byte tags */
  7538. + int no_tags_ecc; /* Flag to decide whether or not to do ECC
  7539. + * on packed tags (yaffs2) */
  7540. +
  7541. + int is_yaffs2; /* Use yaffs2 mode on this device */
  7542. +
  7543. + int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
  7544. +
  7545. + int refresh_period; /* How often to check for a block refresh */
  7546. +
  7547. + /* Checkpoint control. Can be set before or after initialisation */
  7548. + u8 skip_checkpt_rd;
  7549. + u8 skip_checkpt_wr;
  7550. +
  7551. + int enable_xattr; /* Enable xattribs */
  7552. +
  7553. + int max_objects; /*
  7554. + * Set to limit the number of objects created.
  7555. + * 0 = no limit.
  7556. + */
  7557. +
  7558. + /* The remove_obj_fn function must be supplied by OS flavours that
  7559. + * need it.
  7560. + * yaffs direct uses it to implement the faster readdir.
  7561. + * Linux uses it to protect the directory during unlocking.
  7562. + */
  7563. + void (*remove_obj_fn) (struct yaffs_obj *obj);
  7564. +
  7565. + /* Callback to mark the superblock dirty */
  7566. + void (*sb_dirty_fn) (struct yaffs_dev *dev);
  7567. +
  7568. + /* Callback to control garbage collection. */
  7569. + unsigned (*gc_control_fn) (struct yaffs_dev *dev);
  7570. +
  7571. + /* Debug control flags. Don't use unless you know what you're doing */
  7572. + int use_header_file_size; /* Flag to determine if we should use
  7573. + * file sizes from the header */
  7574. + int disable_lazy_load; /* Disable lazy loading on this device */
  7575. + int wide_tnodes_disabled; /* Set to disable wide tnodes */
  7576. + int disable_soft_del; /* yaffs 1 only: Set to disable the use of
  7577. + * softdeletion. */
  7578. +
  7579. + int defered_dir_update; /* Set to defer directory updates */
  7580. +
  7581. +#ifdef CONFIG_YAFFS_AUTO_UNICODE
  7582. + int auto_unicode;
  7583. +#endif
  7584. + int always_check_erased; /* Force chunk erased check always on */
  7585. +
  7586. + int disable_summary;
  7587. + int disable_bad_block_marking;
  7588. +
  7589. +};
  7590. +
  7591. +struct yaffs_driver {
  7592. + int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  7593. + const u8 *data, int data_len,
  7594. + const u8 *oob, int oob_len);
  7595. + int (*drv_read_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  7596. + u8 *data, int data_len,
  7597. + u8 *oob, int oob_len,
  7598. + enum yaffs_ecc_result *ecc_result);
  7599. + int (*drv_erase_fn) (struct yaffs_dev *dev, int block_no);
  7600. + int (*drv_mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  7601. + int (*drv_check_bad_fn) (struct yaffs_dev *dev, int block_no);
  7602. + int (*drv_initialise_fn) (struct yaffs_dev *dev);
  7603. + int (*drv_deinitialise_fn) (struct yaffs_dev *dev);
  7604. +};
  7605. +
  7606. +struct yaffs_tags_handler {
  7607. + int (*write_chunk_tags_fn) (struct yaffs_dev *dev,
  7608. + int nand_chunk, const u8 *data,
  7609. + const struct yaffs_ext_tags *tags);
  7610. + int (*read_chunk_tags_fn) (struct yaffs_dev *dev,
  7611. + int nand_chunk, u8 *data,
  7612. + struct yaffs_ext_tags *tags);
  7613. +
  7614. + int (*query_block_fn) (struct yaffs_dev *dev, int block_no,
  7615. + enum yaffs_block_state *state,
  7616. + u32 *seq_number);
  7617. + int (*mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  7618. +};
  7619. +
  7620. +struct yaffs_dev {
  7621. + struct yaffs_param param;
  7622. + struct yaffs_driver drv;
  7623. + struct yaffs_tags_handler tagger;
  7624. +
  7625. + /* Context storage. Holds extra OS specific data for this device */
  7626. +
  7627. + void *os_context;
  7628. + void *driver_context;
  7629. +
  7630. + struct list_head dev_list;
  7631. +
  7632. + int ll_init;
  7633. + /* Runtime parameters. Set up by YAFFS. */
  7634. + int data_bytes_per_chunk;
  7635. +
  7636. + /* Non-wide tnode stuff */
  7637. + u16 chunk_grp_bits; /* Number of bits that need to be resolved if
  7638. + * the tnodes are not wide enough.
  7639. + */
  7640. + u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
  7641. +
  7642. + /* Stuff to support wide tnodes */
  7643. + u32 tnode_width;
  7644. + u32 tnode_mask;
  7645. + u32 tnode_size;
  7646. +
  7647. + /* Stuff for figuring out file offset to chunk conversions */
  7648. + u32 chunk_shift; /* Shift value */
  7649. + u32 chunk_div; /* Divisor after shifting: 1 for 2^n sizes */
  7650. + u32 chunk_mask; /* Mask to use for power-of-2 case */
  7651. +
  7652. + int is_mounted;
  7653. + int read_only;
  7654. + int is_checkpointed;
  7655. +
  7656. + /* Stuff to support block offsetting to support start block zero */
  7657. + int internal_start_block;
  7658. + int internal_end_block;
  7659. + int block_offset;
  7660. + int chunk_offset;
  7661. +
  7662. + /* Runtime checkpointing stuff */
  7663. + int checkpt_page_seq; /* running sequence number of checkpt pages */
  7664. + int checkpt_byte_count;
  7665. + int checkpt_byte_offs;
  7666. + u8 *checkpt_buffer;
  7667. + int checkpt_open_write;
  7668. + int blocks_in_checkpt;
  7669. + int checkpt_cur_chunk;
  7670. + int checkpt_cur_block;
  7671. + int checkpt_next_block;
  7672. + int *checkpt_block_list;
  7673. + int checkpt_max_blocks;
  7674. + u32 checkpt_sum;
  7675. + u32 checkpt_xor;
  7676. +
  7677. + int checkpoint_blocks_required; /* Number of blocks needed to store
  7678. + * current checkpoint set */
  7679. +
  7680. + /* Block Info */
  7681. + struct yaffs_block_info *block_info;
  7682. + u8 *chunk_bits; /* bitmap of chunks in use */
  7683. + u8 block_info_alt:1; /* allocated using alternative alloc */
  7684. + u8 chunk_bits_alt:1; /* allocated using alternative alloc */
  7685. + int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
  7686. + * Must be consistent with chunks_per_block.
  7687. + */
  7688. +
  7689. + int n_erased_blocks;
  7690. + int alloc_block; /* Current block being allocated off */
  7691. + u32 alloc_page;
  7692. + int alloc_block_finder; /* Used to search for next allocation block */
  7693. +
  7694. + /* Object and Tnode memory management */
  7695. + void *allocator;
  7696. + int n_obj;
  7697. + int n_tnodes;
  7698. +
  7699. + int n_hardlinks;
  7700. +
  7701. + struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
  7702. + u32 bucket_finder;
  7703. +
  7704. + int n_free_chunks;
  7705. +
  7706. + /* Garbage collection control */
  7707. + u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
  7708. + u32 n_clean_ups;
  7709. +
  7710. + unsigned has_pending_prioritised_gc; /* We think this device might
  7711. + have pending prioritised gcs */
  7712. + unsigned gc_disable;
  7713. + unsigned gc_block_finder;
  7714. + unsigned gc_dirtiest;
  7715. + unsigned gc_pages_in_use;
  7716. + unsigned gc_not_done;
  7717. + unsigned gc_block;
  7718. + unsigned gc_chunk;
  7719. + unsigned gc_skip;
  7720. + struct yaffs_summary_tags *gc_sum_tags;
  7721. +
  7722. + /* Special directories */
  7723. + struct yaffs_obj *root_dir;
  7724. + struct yaffs_obj *lost_n_found;
  7725. +
  7726. + int buffered_block; /* Which block is buffered here? */
  7727. + int doing_buffered_block_rewrite;
  7728. +
  7729. + struct yaffs_cache *cache;
  7730. + int cache_last_use;
  7731. +
  7732. + /* Stuff for background deletion and unlinked files. */
  7733. + struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted
  7734. + files live. */
  7735. + struct yaffs_obj *del_dir; /* Directory where deleted objects are
  7736. + sent to disappear. */
  7737. + struct yaffs_obj *unlinked_deletion; /* Current file being
  7738. + background deleted. */
  7739. + int n_deleted_files; /* Count of files awaiting deletion; */
  7740. + int n_unlinked_files; /* Count of unlinked files. */
  7741. + int n_bg_deletions; /* Count of background deletions. */
  7742. +
  7743. + /* Temporary buffer management */
  7744. + struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
  7745. + int max_temp;
  7746. + int temp_in_use;
  7747. + int unmanaged_buffer_allocs;
  7748. + int unmanaged_buffer_deallocs;
  7749. +
  7750. + /* yaffs2 runtime stuff */
  7751. + unsigned seq_number; /* Sequence number of currently
  7752. + allocating block */
  7753. + unsigned oldest_dirty_seq;
  7754. + unsigned oldest_dirty_block;
  7755. +
  7756. + /* Block refreshing */
  7757. + int refresh_skip; /* A skip down counter.
  7758. + * Refresh happens when this gets to zero. */
  7759. +
  7760. + /* Dirty directory handling */
  7761. + struct list_head dirty_dirs; /* List of dirty directories */
  7762. +
  7763. + /* Summary */
  7764. + int chunks_per_summary;
  7765. + struct yaffs_summary_tags *sum_tags;
  7766. +
  7767. + /* Statistics */
  7768. + u32 n_page_writes;
  7769. + u32 n_page_reads;
  7770. + u32 n_erasures;
  7771. + u32 n_bad_queries;
  7772. + u32 n_bad_markings;
  7773. + u32 n_erase_failures;
  7774. + u32 n_gc_copies;
  7775. + u32 all_gcs;
  7776. + u32 passive_gc_count;
  7777. + u32 oldest_dirty_gc_count;
  7778. + u32 n_gc_blocks;
  7779. + u32 bg_gcs;
  7780. + u32 n_retried_writes;
  7781. + u32 n_retired_blocks;
  7782. + u32 n_ecc_fixed;
  7783. + u32 n_ecc_unfixed;
  7784. + u32 n_tags_ecc_fixed;
  7785. + u32 n_tags_ecc_unfixed;
  7786. + u32 n_deletions;
  7787. + u32 n_unmarked_deletions;
  7788. + u32 refresh_count;
  7789. + u32 cache_hits;
  7790. + u32 tags_used;
  7791. + u32 summary_used;
  7792. +
  7793. +};
  7794. +
  7795. +/* The CheckpointDevice structure holds the device information that changes
  7796. + *at runtime and must be preserved over unmount/mount cycles.
  7797. + */
  7798. +struct yaffs_checkpt_dev {
  7799. + int struct_type;
  7800. + int n_erased_blocks;
  7801. + int alloc_block; /* Current block being allocated off */
  7802. + u32 alloc_page;
  7803. + int n_free_chunks;
  7804. +
  7805. + int n_deleted_files; /* Count of files awaiting deletion; */
  7806. + int n_unlinked_files; /* Count of unlinked files. */
  7807. + int n_bg_deletions; /* Count of background deletions. */
  7808. +
  7809. + /* yaffs2 runtime stuff */
  7810. + unsigned seq_number; /* Sequence number of currently
  7811. + * allocating block */
  7812. +
  7813. +};
  7814. +
  7815. +struct yaffs_checkpt_validity {
  7816. + int struct_type;
  7817. + u32 magic;
  7818. + u32 version;
  7819. + u32 head;
  7820. +};
  7821. +
  7822. +struct yaffs_shadow_fixer {
  7823. + int obj_id;
  7824. + int shadowed_id;
  7825. + struct yaffs_shadow_fixer *next;
  7826. +};
  7827. +
  7828. +/* Structure for doing xattr modifications */
  7829. +struct yaffs_xattr_mod {
  7830. + int set; /* If 0 then this is a deletion */
  7831. + const YCHAR *name;
  7832. + const void *data;
  7833. + int size;
  7834. + int flags;
  7835. + int result;
  7836. +};
  7837. +
  7838. +/*----------------------- YAFFS Functions -----------------------*/
  7839. +
  7840. +int yaffs_guts_initialise(struct yaffs_dev *dev);
  7841. +void yaffs_deinitialise(struct yaffs_dev *dev);
  7842. +
  7843. +int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
  7844. +
  7845. +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  7846. + struct yaffs_obj *new_dir, const YCHAR * new_name);
  7847. +
  7848. +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
  7849. +int yaffs_del_obj(struct yaffs_obj *obj);
  7850. +struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
  7851. + enum yaffs_obj_type type);
  7852. +
  7853. +
  7854. +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
  7855. +loff_t yaffs_get_obj_length(struct yaffs_obj *obj);
  7856. +int yaffs_get_obj_inode(struct yaffs_obj *obj);
  7857. +unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
  7858. +int yaffs_get_obj_link_count(struct yaffs_obj *obj);
  7859. +
  7860. +/* File operations */
  7861. +int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
  7862. + int n_bytes);
  7863. +int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
  7864. + int n_bytes, int write_trhrough);
  7865. +int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
  7866. +
  7867. +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  7868. + const YCHAR *name, u32 mode, u32 uid,
  7869. + u32 gid);
  7870. +
  7871. +int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync);
  7872. +
  7873. +/* Flushing and checkpointing */
  7874. +void yaffs_flush_whole_cache(struct yaffs_dev *dev);
  7875. +
  7876. +int yaffs_checkpoint_save(struct yaffs_dev *dev);
  7877. +int yaffs_checkpoint_restore(struct yaffs_dev *dev);
  7878. +
  7879. +/* Directory operations */
  7880. +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
  7881. + u32 mode, u32 uid, u32 gid);
  7882. +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
  7883. + const YCHAR *name);
  7884. +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
  7885. +
  7886. +/* Link operations */
  7887. +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR *name,
  7888. + struct yaffs_obj *equiv_obj);
  7889. +
  7890. +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
  7891. +
  7892. +/* Symlink operations */
  7893. +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  7894. + const YCHAR *name, u32 mode, u32 uid,
  7895. + u32 gid, const YCHAR *alias);
  7896. +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
  7897. +
  7898. +/* Special inodes (fifos, sockets and devices) */
  7899. +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  7900. + const YCHAR *name, u32 mode, u32 uid,
  7901. + u32 gid, u32 rdev);
  7902. +
  7903. +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR *name,
  7904. + const void *value, int size, int flags);
  7905. +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR *name, void *value,
  7906. + int size);
  7907. +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
  7908. +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR *name);
  7909. +
  7910. +/* Special directories */
  7911. +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
  7912. +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
  7913. +
  7914. +void yaffs_handle_defered_free(struct yaffs_obj *obj);
  7915. +
  7916. +void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
  7917. +
  7918. +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
  7919. +
  7920. +/* Debug dump */
  7921. +int yaffs_dump_obj(struct yaffs_obj *obj);
  7922. +
  7923. +void yaffs_guts_test(struct yaffs_dev *dev);
  7924. +int yaffs_guts_ll_init(struct yaffs_dev *dev);
  7925. +
  7926. +
  7927. +/* A few useful functions to be used within the core files*/
  7928. +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  7929. + int lyn);
  7930. +int yaffs_check_ff(u8 *buffer, int n_bytes);
  7931. +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  7932. + struct yaffs_block_info *bi);
  7933. +
  7934. +u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev);
  7935. +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer);
  7936. +
  7937. +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  7938. + int number,
  7939. + enum yaffs_obj_type type);
  7940. +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  7941. + int nand_chunk, int in_scan);
  7942. +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR *name);
  7943. +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  7944. + const struct yaffs_obj_hdr *oh);
  7945. +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
  7946. +YCHAR *yaffs_clone_str(const YCHAR *str);
  7947. +void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list);
  7948. +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
  7949. +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name,
  7950. + int force, int is_shrink, int shadows,
  7951. + struct yaffs_xattr_mod *xop);
  7952. +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  7953. + int backward_scanning);
  7954. +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
  7955. +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
  7956. +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  7957. + struct yaffs_file_var *file_struct,
  7958. + u32 chunk_id,
  7959. + struct yaffs_tnode *passed_tn);
  7960. +
  7961. +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  7962. + int n_bytes, int write_trhrough);
  7963. +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
  7964. +void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
  7965. +
  7966. +int yaffs_count_free_chunks(struct yaffs_dev *dev);
  7967. +
  7968. +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  7969. + struct yaffs_file_var *file_struct,
  7970. + u32 chunk_id);
  7971. +
  7972. +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  7973. + unsigned pos);
  7974. +
  7975. +int yaffs_is_non_empty_dir(struct yaffs_obj *obj);
  7976. +
  7977. +int yaffs_guts_format_dev(struct yaffs_dev *dev);
  7978. +
  7979. +void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  7980. + int *chunk_out, u32 *offset_out);
  7981. +/*
  7982. + * Marshalling functions to get loff_t file sizes into aand out of
  7983. + * object headers.
  7984. + */
  7985. +void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize);
  7986. +loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh);
  7987. +loff_t yaffs_max_file_size(struct yaffs_dev *dev);
  7988. +
  7989. +/*
  7990. + * Debug function to count number of blocks in each state
  7991. + * NB Needs to be called with correct number of integers
  7992. + */
  7993. +
  7994. +void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10]);
  7995. +
  7996. +int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  7997. + struct yaffs_ext_tags *tags);
  7998. +
  7999. +#endif
  8000. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_linux.h linux-3.15-rc5/fs/yaffs2/yaffs_linux.h
  8001. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_linux.h 1970-01-01 01:00:00.000000000 +0100
  8002. +++ linux-3.15-rc5/fs/yaffs2/yaffs_linux.h 2014-05-17 01:53:27.000000000 +0200
  8003. @@ -0,0 +1,48 @@
  8004. +/*
  8005. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8006. + *
  8007. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8008. + * for Toby Churchill Ltd and Brightstar Engineering
  8009. + *
  8010. + * Created by Charles Manning <charles@aleph1.co.uk>
  8011. + *
  8012. + * This program is free software; you can redistribute it and/or modify
  8013. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8014. + * published by the Free Software Foundation.
  8015. + *
  8016. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8017. + */
  8018. +
  8019. +#ifndef __YAFFS_LINUX_H__
  8020. +#define __YAFFS_LINUX_H__
  8021. +
  8022. +#include "yportenv.h"
  8023. +
  8024. +struct yaffs_linux_context {
  8025. + struct list_head context_list; /* List of these we have mounted */
  8026. + struct yaffs_dev *dev;
  8027. + struct super_block *super;
  8028. + struct task_struct *bg_thread; /* Background thread for this device */
  8029. + int bg_running;
  8030. + struct mutex gross_lock; /* Gross locking mutex*/
  8031. + u8 *spare_buffer; /* For mtdif2 use. Don't know the buffer size
  8032. + * at compile time so we have to allocate it.
  8033. + */
  8034. + struct list_head search_contexts;
  8035. + struct task_struct *readdir_process;
  8036. + unsigned mount_id;
  8037. + int dirty;
  8038. +};
  8039. +
  8040. +#define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
  8041. +#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
  8042. +
  8043. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  8044. +#define WRITE_SIZE_STR "writesize"
  8045. +#define WRITE_SIZE(mtd) ((mtd)->writesize)
  8046. +#else
  8047. +#define WRITE_SIZE_STR "oobblock"
  8048. +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
  8049. +#endif
  8050. +
  8051. +#endif
  8052. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_mtdif.c linux-3.15-rc5/fs/yaffs2/yaffs_mtdif.c
  8053. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  8054. +++ linux-3.15-rc5/fs/yaffs2/yaffs_mtdif.c 2014-05-17 01:53:27.000000000 +0200
  8055. @@ -0,0 +1,309 @@
  8056. +/*
  8057. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8058. + *
  8059. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8060. + * for Toby Churchill Ltd and Brightstar Engineering
  8061. + *
  8062. + * Created by Charles Manning <charles@aleph1.co.uk>
  8063. + *
  8064. + * This program is free software; you can redistribute it and/or modify
  8065. + * it under the terms of the GNU General Public License version 2 as
  8066. + * published by the Free Software Foundation.
  8067. + */
  8068. +
  8069. +#include "yportenv.h"
  8070. +
  8071. +#include "yaffs_mtdif.h"
  8072. +
  8073. +#include "linux/mtd/mtd.h"
  8074. +#include "linux/types.h"
  8075. +#include "linux/time.h"
  8076. +#include "linux/major.h"
  8077. +#include "linux/mtd/nand.h"
  8078. +#include "linux/kernel.h"
  8079. +#include "linux/version.h"
  8080. +#include "linux/types.h"
  8081. +
  8082. +#include "yaffs_trace.h"
  8083. +#include "yaffs_guts.h"
  8084. +#include "yaffs_linux.h"
  8085. +
  8086. +
  8087. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
  8088. +#define MTD_OPS_AUTO_OOB MTD_OOB_AUTO
  8089. +#endif
  8090. +
  8091. +
  8092. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
  8093. +#define mtd_erase(m, ei) (m)->erase(m, ei)
  8094. +#define mtd_write_oob(m, addr, pops) (m)->write_oob(m, addr, pops)
  8095. +#define mtd_read_oob(m, addr, pops) (m)->read_oob(m, addr, pops)
  8096. +#define mtd_block_isbad(m, offs) (m)->block_isbad(m, offs)
  8097. +#define mtd_block_markbad(m, offs) (m)->block_markbad(m, offs)
  8098. +#endif
  8099. +
  8100. +
  8101. +
  8102. +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
  8103. +{
  8104. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8105. + u32 addr =
  8106. + ((loff_t) block_no) * dev->param.total_bytes_per_chunk *
  8107. + dev->param.chunks_per_block;
  8108. + struct erase_info ei;
  8109. + int retval = 0;
  8110. +
  8111. + ei.mtd = mtd;
  8112. + ei.addr = addr;
  8113. + ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
  8114. + ei.time = 1000;
  8115. + ei.retries = 2;
  8116. + ei.callback = NULL;
  8117. + ei.priv = (u_long) dev;
  8118. +
  8119. + retval = mtd_erase(mtd, &ei);
  8120. +
  8121. + if (retval == 0)
  8122. + return YAFFS_OK;
  8123. +
  8124. + return YAFFS_FAIL;
  8125. +}
  8126. +
  8127. +
  8128. +static int yaffs_mtd_write(struct yaffs_dev *dev, int nand_chunk,
  8129. + const u8 *data, int data_len,
  8130. + const u8 *oob, int oob_len)
  8131. +{
  8132. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8133. + loff_t addr;
  8134. + struct mtd_oob_ops ops;
  8135. + int retval;
  8136. +
  8137. + yaffs_trace(YAFFS_TRACE_MTD,
  8138. + "yaffs_mtd_write(%p, %d, %p, %d, %p, %d)\n",
  8139. + dev, nand_chunk, data, data_len, oob, oob_len);
  8140. +
  8141. + if (!data || !data_len) {
  8142. + data = NULL;
  8143. + data_len = 0;
  8144. + }
  8145. +
  8146. + if (!oob || !oob_len) {
  8147. + oob = NULL;
  8148. + oob_len = 0;
  8149. + }
  8150. +
  8151. + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8152. + memset(&ops, 0, sizeof(ops));
  8153. + ops.mode = MTD_OPS_AUTO_OOB;
  8154. + ops.len = (data) ? data_len : 0;
  8155. + ops.ooblen = oob_len;
  8156. + ops.datbuf = (u8 *)data;
  8157. + ops.oobbuf = (u8 *)oob;
  8158. +
  8159. + retval = mtd_write_oob(mtd, addr, &ops);
  8160. + if (retval) {
  8161. + yaffs_trace(YAFFS_TRACE_MTD,
  8162. + "write_oob failed, chunk %d, mtd error %d",
  8163. + nand_chunk, retval);
  8164. + }
  8165. + return retval ? YAFFS_FAIL : YAFFS_OK;
  8166. +}
  8167. +
  8168. +static int yaffs_mtd_read(struct yaffs_dev *dev, int nand_chunk,
  8169. + u8 *data, int data_len,
  8170. + u8 *oob, int oob_len,
  8171. + enum yaffs_ecc_result *ecc_result)
  8172. +{
  8173. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8174. + loff_t addr;
  8175. + struct mtd_oob_ops ops;
  8176. + int retval;
  8177. +
  8178. + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  8179. + memset(&ops, 0, sizeof(ops));
  8180. + ops.mode = MTD_OPS_AUTO_OOB;
  8181. + ops.len = (data) ? data_len : 0;
  8182. + ops.ooblen = oob_len;
  8183. + ops.datbuf = data;
  8184. + ops.oobbuf = oob;
  8185. +
  8186. +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
  8187. + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
  8188. + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
  8189. + */
  8190. + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
  8191. +#endif
  8192. + /* Read page and oob using MTD.
  8193. + * Check status and determine ECC result.
  8194. + */
  8195. + retval = mtd_read_oob(mtd, addr, &ops);
  8196. + if (retval)
  8197. + yaffs_trace(YAFFS_TRACE_MTD,
  8198. + "read_oob failed, chunk %d, mtd error %d",
  8199. + nand_chunk, retval);
  8200. +
  8201. + switch (retval) {
  8202. + case 0:
  8203. + /* no error */
  8204. + if(ecc_result)
  8205. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  8206. + break;
  8207. +
  8208. + case -EUCLEAN:
  8209. + /* MTD's ECC fixed the data */
  8210. + if(ecc_result)
  8211. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  8212. + dev->n_ecc_fixed++;
  8213. + break;
  8214. +
  8215. + case -EBADMSG:
  8216. + default:
  8217. + /* MTD's ECC could not fix the data */
  8218. + dev->n_ecc_unfixed++;
  8219. + if(ecc_result)
  8220. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  8221. + return YAFFS_FAIL;
  8222. + }
  8223. +
  8224. + return YAFFS_OK;
  8225. +}
  8226. +
  8227. +static int yaffs_mtd_erase(struct yaffs_dev *dev, int block_no)
  8228. +{
  8229. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8230. +
  8231. + loff_t addr;
  8232. + struct erase_info ei;
  8233. + int retval = 0;
  8234. + u32 block_size;
  8235. +
  8236. + block_size = dev->param.total_bytes_per_chunk *
  8237. + dev->param.chunks_per_block;
  8238. + addr = ((loff_t) block_no) * block_size;
  8239. +
  8240. + ei.mtd = mtd;
  8241. + ei.addr = addr;
  8242. + ei.len = block_size;
  8243. + ei.time = 1000;
  8244. + ei.retries = 2;
  8245. + ei.callback = NULL;
  8246. + ei.priv = (u_long) dev;
  8247. +
  8248. + retval = mtd_erase(mtd, &ei);
  8249. +
  8250. + if (retval == 0)
  8251. + return YAFFS_OK;
  8252. +
  8253. + return YAFFS_FAIL;
  8254. +}
  8255. +
  8256. +static int yaffs_mtd_mark_bad(struct yaffs_dev *dev, int block_no)
  8257. +{
  8258. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8259. + int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
  8260. + int retval;
  8261. +
  8262. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", block_no);
  8263. +
  8264. + retval = mtd_block_markbad(mtd, (loff_t) blocksize * block_no);
  8265. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  8266. +}
  8267. +
  8268. +static int yaffs_mtd_check_bad(struct yaffs_dev *dev, int block_no)
  8269. +{
  8270. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  8271. + int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
  8272. + int retval;
  8273. +
  8274. + yaffs_trace(YAFFS_TRACE_MTD, "checking block %d bad", block_no);
  8275. +
  8276. + retval = mtd_block_isbad(mtd, (loff_t) blocksize * block_no);
  8277. + return (retval) ? YAFFS_FAIL : YAFFS_OK;
  8278. +}
  8279. +
  8280. +static int yaffs_mtd_initialise(struct yaffs_dev *dev)
  8281. +{
  8282. + return YAFFS_OK;
  8283. +}
  8284. +
  8285. +static int yaffs_mtd_deinitialise(struct yaffs_dev *dev)
  8286. +{
  8287. + return YAFFS_OK;
  8288. +}
  8289. +
  8290. +
  8291. +void yaffs_mtd_drv_install(struct yaffs_dev *dev)
  8292. +{
  8293. + struct yaffs_driver *drv = &dev->drv;
  8294. +
  8295. + drv->drv_write_chunk_fn = yaffs_mtd_write;
  8296. + drv->drv_read_chunk_fn = yaffs_mtd_read;
  8297. + drv->drv_erase_fn = yaffs_mtd_erase;
  8298. + drv->drv_mark_bad_fn = yaffs_mtd_mark_bad;
  8299. + drv->drv_check_bad_fn = yaffs_mtd_check_bad;
  8300. + drv->drv_initialise_fn = yaffs_mtd_initialise;
  8301. + drv->drv_deinitialise_fn = yaffs_mtd_deinitialise;
  8302. +}
  8303. +
  8304. +
  8305. +struct mtd_info * yaffs_get_mtd_device(dev_t sdev)
  8306. +{
  8307. + struct mtd_info *mtd;
  8308. +
  8309. + mtd = yaffs_get_mtd_device(sdev);
  8310. +
  8311. + /* Check it's an mtd device..... */
  8312. + if (MAJOR(sdev) != MTD_BLOCK_MAJOR)
  8313. + return NULL; /* This isn't an mtd device */
  8314. +
  8315. + /* Check it's NAND */
  8316. + if (mtd->type != MTD_NANDFLASH) {
  8317. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  8318. + "yaffs: MTD device is not NAND it's type %d",
  8319. + mtd->type);
  8320. + return NULL;
  8321. + }
  8322. +
  8323. + yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
  8324. + yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
  8325. + yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
  8326. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  8327. + yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
  8328. +#else
  8329. + yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
  8330. +#endif
  8331. +
  8332. + return mtd;
  8333. +}
  8334. +
  8335. +int yaffs_verify_mtd(struct mtd_info *mtd, int yaffs_version, int inband_tags)
  8336. +{
  8337. + if (yaffs_version == 2) {
  8338. + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
  8339. + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
  8340. + !inband_tags) {
  8341. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  8342. + "MTD device does not have the right page sizes"
  8343. + );
  8344. + return -1;
  8345. + }
  8346. + } else {
  8347. + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
  8348. + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
  8349. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  8350. + "MTD device does not support have the right page sizes"
  8351. + );
  8352. + return -1;
  8353. + }
  8354. + }
  8355. +
  8356. + return 0;
  8357. +}
  8358. +
  8359. +
  8360. +void yaffs_put_mtd_device(struct mtd_info *mtd)
  8361. +{
  8362. + if(mtd)
  8363. + put_mtd_device(mtd);
  8364. +}
  8365. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_mtdif.h linux-3.15-rc5/fs/yaffs2/yaffs_mtdif.h
  8366. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  8367. +++ linux-3.15-rc5/fs/yaffs2/yaffs_mtdif.h 2014-05-17 01:53:27.000000000 +0200
  8368. @@ -0,0 +1,25 @@
  8369. +/*
  8370. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8371. + *
  8372. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8373. + * for Toby Churchill Ltd and Brightstar Engineering
  8374. + *
  8375. + * Created by Charles Manning <charles@aleph1.co.uk>
  8376. + *
  8377. + * This program is free software; you can redistribute it and/or modify
  8378. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8379. + * published by the Free Software Foundation.
  8380. + *
  8381. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8382. + */
  8383. +
  8384. +#ifndef __YAFFS_MTDIF_H__
  8385. +#define __YAFFS_MTDIF_H__
  8386. +
  8387. +#include "yaffs_guts.h"
  8388. +
  8389. +void yaffs_mtd_drv_install(struct yaffs_dev *dev);
  8390. +struct mtd_info * yaffs_get_mtd_device(dev_t sdev);
  8391. +void yaffs_put_mtd_device(struct mtd_info *mtd);
  8392. +int yaffs_verify_mtd(struct mtd_info *mtd, int yaffs_version, int inband_tags);
  8393. +#endif
  8394. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_nameval.c linux-3.15-rc5/fs/yaffs2/yaffs_nameval.c
  8395. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_nameval.c 1970-01-01 01:00:00.000000000 +0100
  8396. +++ linux-3.15-rc5/fs/yaffs2/yaffs_nameval.c 2014-05-17 01:53:27.000000000 +0200
  8397. @@ -0,0 +1,208 @@
  8398. +/*
  8399. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8400. + *
  8401. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8402. + * for Toby Churchill Ltd and Brightstar Engineering
  8403. + *
  8404. + * Created by Charles Manning <charles@aleph1.co.uk>
  8405. + *
  8406. + * This program is free software; you can redistribute it and/or modify
  8407. + * it under the terms of the GNU General Public License version 2 as
  8408. + * published by the Free Software Foundation.
  8409. + */
  8410. +
  8411. +/*
  8412. + * This simple implementation of a name-value store assumes a small number of
  8413. +* values and fits into a small finite buffer.
  8414. + *
  8415. + * Each attribute is stored as a record:
  8416. + * sizeof(int) bytes record size.
  8417. + * strnlen+1 bytes name null terminated.
  8418. + * nbytes value.
  8419. + * ----------
  8420. + * total size stored in record size
  8421. + *
  8422. + * This code has not been tested with unicode yet.
  8423. + */
  8424. +
  8425. +#include "yaffs_nameval.h"
  8426. +
  8427. +#include "yportenv.h"
  8428. +
  8429. +static int nval_find(const char *xb, int xb_size, const YCHAR *name,
  8430. + int *exist_size)
  8431. +{
  8432. + int pos = 0;
  8433. + int size;
  8434. +
  8435. + memcpy(&size, xb, sizeof(int));
  8436. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8437. + if (!strncmp((YCHAR *) (xb + pos + sizeof(int)),
  8438. + name, size)) {
  8439. + if (exist_size)
  8440. + *exist_size = size;
  8441. + return pos;
  8442. + }
  8443. + pos += size;
  8444. + if (pos < xb_size - sizeof(int))
  8445. + memcpy(&size, xb + pos, sizeof(int));
  8446. + else
  8447. + size = 0;
  8448. + }
  8449. + if (exist_size)
  8450. + *exist_size = 0;
  8451. + return -ENODATA;
  8452. +}
  8453. +
  8454. +static int nval_used(const char *xb, int xb_size)
  8455. +{
  8456. + int pos = 0;
  8457. + int size;
  8458. +
  8459. + memcpy(&size, xb + pos, sizeof(int));
  8460. + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
  8461. + pos += size;
  8462. + if (pos < xb_size - sizeof(int))
  8463. + memcpy(&size, xb + pos, sizeof(int));
  8464. + else
  8465. + size = 0;
  8466. + }
  8467. + return pos;
  8468. +}
  8469. +
  8470. +int nval_del(char *xb, int xb_size, const YCHAR *name)
  8471. +{
  8472. + int pos = nval_find(xb, xb_size, name, NULL);
  8473. + int size;
  8474. +
  8475. + if (pos < 0 || pos >= xb_size)
  8476. + return -ENODATA;
  8477. +
  8478. + /* Find size, shift rest over this record,
  8479. + * then zero out the rest of buffer */
  8480. + memcpy(&size, xb + pos, sizeof(int));
  8481. + memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
  8482. + memset(xb + (xb_size - size), 0, size);
  8483. + return 0;
  8484. +}
  8485. +
  8486. +int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf,
  8487. + int bsize, int flags)
  8488. +{
  8489. + int pos;
  8490. + int namelen = strnlen(name, xb_size);
  8491. + int reclen;
  8492. + int size_exist = 0;
  8493. + int space;
  8494. + int start;
  8495. +
  8496. + pos = nval_find(xb, xb_size, name, &size_exist);
  8497. +
  8498. + if (flags & XATTR_CREATE && pos >= 0)
  8499. + return -EEXIST;
  8500. + if (flags & XATTR_REPLACE && pos < 0)
  8501. + return -ENODATA;
  8502. +
  8503. + start = nval_used(xb, xb_size);
  8504. + space = xb_size - start + size_exist;
  8505. +
  8506. + reclen = (sizeof(int) + namelen + 1 + bsize);
  8507. +
  8508. + if (reclen > space)
  8509. + return -ENOSPC;
  8510. +
  8511. + if (pos >= 0) {
  8512. + nval_del(xb, xb_size, name);
  8513. + start = nval_used(xb, xb_size);
  8514. + }
  8515. +
  8516. + pos = start;
  8517. +
  8518. + memcpy(xb + pos, &reclen, sizeof(int));
  8519. + pos += sizeof(int);
  8520. + strncpy((YCHAR *) (xb + pos), name, reclen);
  8521. + pos += (namelen + 1);
  8522. + memcpy(xb + pos, buf, bsize);
  8523. + return 0;
  8524. +}
  8525. +
  8526. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  8527. + int bsize)
  8528. +{
  8529. + int pos = nval_find(xb, xb_size, name, NULL);
  8530. + int size;
  8531. +
  8532. + if (pos >= 0 && pos < xb_size) {
  8533. +
  8534. + memcpy(&size, xb + pos, sizeof(int));
  8535. + pos += sizeof(int); /* advance past record length */
  8536. + size -= sizeof(int);
  8537. +
  8538. + /* Advance over name string */
  8539. + while (xb[pos] && size > 0 && pos < xb_size) {
  8540. + pos++;
  8541. + size--;
  8542. + }
  8543. + /*Advance over NUL */
  8544. + pos++;
  8545. + size--;
  8546. +
  8547. + /* If bsize is zero then this is a size query.
  8548. + * Return the size, but don't copy.
  8549. + */
  8550. + if (!bsize)
  8551. + return size;
  8552. +
  8553. + if (size <= bsize) {
  8554. + memcpy(buf, xb + pos, size);
  8555. + return size;
  8556. + }
  8557. + }
  8558. + if (pos >= 0)
  8559. + return -ERANGE;
  8560. +
  8561. + return -ENODATA;
  8562. +}
  8563. +
  8564. +int nval_list(const char *xb, int xb_size, char *buf, int bsize)
  8565. +{
  8566. + int pos = 0;
  8567. + int size;
  8568. + int name_len;
  8569. + int ncopied = 0;
  8570. + int filled = 0;
  8571. +
  8572. + memcpy(&size, xb + pos, sizeof(int));
  8573. + while (size > sizeof(int) &&
  8574. + size <= xb_size &&
  8575. + (pos + size) < xb_size &&
  8576. + !filled) {
  8577. + pos += sizeof(int);
  8578. + size -= sizeof(int);
  8579. + name_len = strnlen((YCHAR *) (xb + pos), size);
  8580. + if (ncopied + name_len + 1 < bsize) {
  8581. + memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
  8582. + buf += name_len;
  8583. + *buf = '\0';
  8584. + buf++;
  8585. + if (sizeof(YCHAR) > 1) {
  8586. + *buf = '\0';
  8587. + buf++;
  8588. + }
  8589. + ncopied += (name_len + 1);
  8590. + } else {
  8591. + filled = 1;
  8592. + }
  8593. + pos += size;
  8594. + if (pos < xb_size - sizeof(int))
  8595. + memcpy(&size, xb + pos, sizeof(int));
  8596. + else
  8597. + size = 0;
  8598. + }
  8599. + return ncopied;
  8600. +}
  8601. +
  8602. +int nval_hasvalues(const char *xb, int xb_size)
  8603. +{
  8604. + return nval_used(xb, xb_size) > 0;
  8605. +}
  8606. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_nameval.h linux-3.15-rc5/fs/yaffs2/yaffs_nameval.h
  8607. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_nameval.h 1970-01-01 01:00:00.000000000 +0100
  8608. +++ linux-3.15-rc5/fs/yaffs2/yaffs_nameval.h 2014-05-17 01:53:27.000000000 +0200
  8609. @@ -0,0 +1,28 @@
  8610. +/*
  8611. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8612. + *
  8613. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8614. + * for Toby Churchill Ltd and Brightstar Engineering
  8615. + *
  8616. + * Created by Charles Manning <charles@aleph1.co.uk>
  8617. + *
  8618. + * This program is free software; you can redistribute it and/or modify
  8619. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8620. + * published by the Free Software Foundation.
  8621. + *
  8622. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8623. + */
  8624. +
  8625. +#ifndef __NAMEVAL_H__
  8626. +#define __NAMEVAL_H__
  8627. +
  8628. +#include "yportenv.h"
  8629. +
  8630. +int nval_del(char *xb, int xb_size, const YCHAR * name);
  8631. +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
  8632. + int bsize, int flags);
  8633. +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
  8634. + int bsize);
  8635. +int nval_list(const char *xb, int xb_size, char *buf, int bsize);
  8636. +int nval_hasvalues(const char *xb, int xb_size);
  8637. +#endif
  8638. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_nand.c linux-3.15-rc5/fs/yaffs2/yaffs_nand.c
  8639. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  8640. +++ linux-3.15-rc5/fs/yaffs2/yaffs_nand.c 2014-05-17 01:53:27.000000000 +0200
  8641. @@ -0,0 +1,122 @@
  8642. +/*
  8643. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8644. + *
  8645. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8646. + * for Toby Churchill Ltd and Brightstar Engineering
  8647. + *
  8648. + * Created by Charles Manning <charles@aleph1.co.uk>
  8649. + *
  8650. + * This program is free software; you can redistribute it and/or modify
  8651. + * it under the terms of the GNU General Public License version 2 as
  8652. + * published by the Free Software Foundation.
  8653. + */
  8654. +
  8655. +#include "yaffs_nand.h"
  8656. +#include "yaffs_tagscompat.h"
  8657. +
  8658. +#include "yaffs_getblockinfo.h"
  8659. +#include "yaffs_summary.h"
  8660. +
  8661. +static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
  8662. +{
  8663. + return chunk - dev->chunk_offset;
  8664. +}
  8665. +
  8666. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  8667. + u8 *buffer, struct yaffs_ext_tags *tags)
  8668. +{
  8669. + int result;
  8670. + struct yaffs_ext_tags local_tags;
  8671. + int flash_chunk = apply_chunk_offset(dev, nand_chunk);
  8672. +
  8673. + dev->n_page_reads++;
  8674. +
  8675. + /* If there are no tags provided use local tags. */
  8676. + if (!tags)
  8677. + tags = &local_tags;
  8678. +
  8679. + result = dev->tagger.read_chunk_tags_fn(dev, flash_chunk, buffer, tags);
  8680. + if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
  8681. +
  8682. + struct yaffs_block_info *bi;
  8683. + bi = yaffs_get_block_info(dev,
  8684. + nand_chunk /
  8685. + dev->param.chunks_per_block);
  8686. + yaffs_handle_chunk_error(dev, bi);
  8687. + }
  8688. + return result;
  8689. +}
  8690. +
  8691. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  8692. + int nand_chunk,
  8693. + const u8 *buffer, struct yaffs_ext_tags *tags)
  8694. +{
  8695. + int result;
  8696. + int flash_chunk = apply_chunk_offset(dev, nand_chunk);
  8697. +
  8698. + dev->n_page_writes++;
  8699. +
  8700. + if (!tags) {
  8701. + yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
  8702. + BUG();
  8703. + return YAFFS_FAIL;
  8704. + }
  8705. +
  8706. + tags->seq_number = dev->seq_number;
  8707. + tags->chunk_used = 1;
  8708. + yaffs_trace(YAFFS_TRACE_WRITE,
  8709. + "Writing chunk %d tags %d %d",
  8710. + nand_chunk, tags->obj_id, tags->chunk_id);
  8711. +
  8712. + result = dev->tagger.write_chunk_tags_fn(dev, flash_chunk,
  8713. + buffer, tags);
  8714. +
  8715. + yaffs_summary_add(dev, tags, nand_chunk);
  8716. +
  8717. + return result;
  8718. +}
  8719. +
  8720. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
  8721. +{
  8722. + block_no -= dev->block_offset;
  8723. + dev->n_bad_markings++;
  8724. +
  8725. + if (dev->param.disable_bad_block_marking)
  8726. + return YAFFS_OK;
  8727. +
  8728. + return dev->tagger.mark_bad_fn(dev, block_no);
  8729. +}
  8730. +
  8731. +
  8732. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  8733. + int block_no,
  8734. + enum yaffs_block_state *state,
  8735. + u32 *seq_number)
  8736. +{
  8737. + block_no -= dev->block_offset;
  8738. + return dev->tagger.query_block_fn(dev, block_no, state, seq_number);
  8739. +}
  8740. +
  8741. +int yaffs_erase_block(struct yaffs_dev *dev, int block_no)
  8742. +{
  8743. + int result;
  8744. +
  8745. + block_no -= dev->block_offset;
  8746. + dev->n_erasures++;
  8747. + result = dev->drv.drv_erase_fn(dev, block_no);
  8748. + return result;
  8749. +}
  8750. +
  8751. +int yaffs_init_nand(struct yaffs_dev *dev)
  8752. +{
  8753. + if (dev->drv.drv_initialise_fn)
  8754. + return dev->drv.drv_initialise_fn(dev);
  8755. + return YAFFS_OK;
  8756. +}
  8757. +
  8758. +int yaffs_deinit_nand(struct yaffs_dev *dev)
  8759. +{
  8760. + if (dev->drv.drv_deinitialise_fn)
  8761. + return dev->drv.drv_deinitialise_fn(dev);
  8762. + return YAFFS_OK;
  8763. +}
  8764. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_nand.h linux-3.15-rc5/fs/yaffs2/yaffs_nand.h
  8765. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  8766. +++ linux-3.15-rc5/fs/yaffs2/yaffs_nand.h 2014-05-17 01:53:27.000000000 +0200
  8767. @@ -0,0 +1,39 @@
  8768. +/*
  8769. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8770. + *
  8771. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8772. + * for Toby Churchill Ltd and Brightstar Engineering
  8773. + *
  8774. + * Created by Charles Manning <charles@aleph1.co.uk>
  8775. + *
  8776. + * This program is free software; you can redistribute it and/or modify
  8777. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8778. + * published by the Free Software Foundation.
  8779. + *
  8780. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8781. + */
  8782. +
  8783. +#ifndef __YAFFS_NAND_H__
  8784. +#define __YAFFS_NAND_H__
  8785. +#include "yaffs_guts.h"
  8786. +
  8787. +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
  8788. + u8 *buffer, struct yaffs_ext_tags *tags);
  8789. +
  8790. +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
  8791. + int nand_chunk,
  8792. + const u8 *buffer, struct yaffs_ext_tags *tags);
  8793. +
  8794. +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no);
  8795. +
  8796. +int yaffs_query_init_block_state(struct yaffs_dev *dev,
  8797. + int block_no,
  8798. + enum yaffs_block_state *state,
  8799. + unsigned *seq_number);
  8800. +
  8801. +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block);
  8802. +
  8803. +int yaffs_init_nand(struct yaffs_dev *dev);
  8804. +int yaffs_deinit_nand(struct yaffs_dev *dev);
  8805. +
  8806. +#endif
  8807. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags1.c linux-3.15-rc5/fs/yaffs2/yaffs_packedtags1.c
  8808. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  8809. +++ linux-3.15-rc5/fs/yaffs2/yaffs_packedtags1.c 2014-05-17 01:53:27.000000000 +0200
  8810. @@ -0,0 +1,56 @@
  8811. +/*
  8812. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8813. + *
  8814. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8815. + * for Toby Churchill Ltd and Brightstar Engineering
  8816. + *
  8817. + * Created by Charles Manning <charles@aleph1.co.uk>
  8818. + *
  8819. + * This program is free software; you can redistribute it and/or modify
  8820. + * it under the terms of the GNU General Public License version 2 as
  8821. + * published by the Free Software Foundation.
  8822. + */
  8823. +
  8824. +#include "yaffs_packedtags1.h"
  8825. +#include "yportenv.h"
  8826. +
  8827. +static const u8 all_ff[20] = {
  8828. + 0xff, 0xff, 0xff, 0xff,
  8829. + 0xff, 0xff, 0xff, 0xff,
  8830. + 0xff, 0xff, 0xff, 0xff,
  8831. + 0xff, 0xff, 0xff, 0xff,
  8832. + 0xff, 0xff, 0xff, 0xff
  8833. +};
  8834. +
  8835. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  8836. + const struct yaffs_ext_tags *t)
  8837. +{
  8838. + pt->chunk_id = t->chunk_id;
  8839. + pt->serial_number = t->serial_number;
  8840. + pt->n_bytes = t->n_bytes;
  8841. + pt->obj_id = t->obj_id;
  8842. + pt->ecc = 0;
  8843. + pt->deleted = (t->is_deleted) ? 0 : 1;
  8844. + pt->unused_stuff = 0;
  8845. + pt->should_be_ff = 0xffffffff;
  8846. +}
  8847. +
  8848. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  8849. + const struct yaffs_packed_tags1 *pt)
  8850. +{
  8851. +
  8852. + if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) {
  8853. + t->block_bad = 0;
  8854. + if (pt->should_be_ff != 0xffffffff)
  8855. + t->block_bad = 1;
  8856. + t->chunk_used = 1;
  8857. + t->obj_id = pt->obj_id;
  8858. + t->chunk_id = pt->chunk_id;
  8859. + t->n_bytes = pt->n_bytes;
  8860. + t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  8861. + t->is_deleted = (pt->deleted) ? 0 : 1;
  8862. + t->serial_number = pt->serial_number;
  8863. + } else {
  8864. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  8865. + }
  8866. +}
  8867. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags1.h linux-3.15-rc5/fs/yaffs2/yaffs_packedtags1.h
  8868. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  8869. +++ linux-3.15-rc5/fs/yaffs2/yaffs_packedtags1.h 2014-05-17 01:53:27.000000000 +0200
  8870. @@ -0,0 +1,39 @@
  8871. +/*
  8872. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  8873. + *
  8874. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8875. + * for Toby Churchill Ltd and Brightstar Engineering
  8876. + *
  8877. + * Created by Charles Manning <charles@aleph1.co.uk>
  8878. + *
  8879. + * This program is free software; you can redistribute it and/or modify
  8880. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  8881. + * published by the Free Software Foundation.
  8882. + *
  8883. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  8884. + */
  8885. +
  8886. +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
  8887. +
  8888. +#ifndef __YAFFS_PACKEDTAGS1_H__
  8889. +#define __YAFFS_PACKEDTAGS1_H__
  8890. +
  8891. +#include "yaffs_guts.h"
  8892. +
  8893. +struct yaffs_packed_tags1 {
  8894. + u32 chunk_id:20;
  8895. + u32 serial_number:2;
  8896. + u32 n_bytes:10;
  8897. + u32 obj_id:18;
  8898. + u32 ecc:12;
  8899. + u32 deleted:1;
  8900. + u32 unused_stuff:1;
  8901. + unsigned should_be_ff;
  8902. +
  8903. +};
  8904. +
  8905. +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
  8906. + const struct yaffs_ext_tags *t);
  8907. +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
  8908. + const struct yaffs_packed_tags1 *pt);
  8909. +#endif
  8910. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags2.c linux-3.15-rc5/fs/yaffs2/yaffs_packedtags2.c
  8911. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  8912. +++ linux-3.15-rc5/fs/yaffs2/yaffs_packedtags2.c 2014-05-17 01:53:27.000000000 +0200
  8913. @@ -0,0 +1,197 @@
  8914. +/*
  8915. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  8916. + *
  8917. + * Copyright (C) 2002-2011 Aleph One Ltd.
  8918. + * for Toby Churchill Ltd and Brightstar Engineering
  8919. + *
  8920. + * Created by Charles Manning <charles@aleph1.co.uk>
  8921. + *
  8922. + * This program is free software; you can redistribute it and/or modify
  8923. + * it under the terms of the GNU General Public License version 2 as
  8924. + * published by the Free Software Foundation.
  8925. + */
  8926. +
  8927. +#include "yaffs_packedtags2.h"
  8928. +#include "yportenv.h"
  8929. +#include "yaffs_trace.h"
  8930. +
  8931. +/* This code packs a set of extended tags into a binary structure for
  8932. + * NAND storage
  8933. + */
  8934. +
  8935. +/* Some of the information is "extra" struff which can be packed in to
  8936. + * speed scanning
  8937. + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
  8938. + */
  8939. +
  8940. +/* Extra flags applied to chunk_id */
  8941. +
  8942. +#define EXTRA_HEADER_INFO_FLAG 0x80000000
  8943. +#define EXTRA_SHRINK_FLAG 0x40000000
  8944. +#define EXTRA_SHADOWS_FLAG 0x20000000
  8945. +#define EXTRA_SPARE_FLAGS 0x10000000
  8946. +
  8947. +#define ALL_EXTRA_FLAGS 0xf0000000
  8948. +
  8949. +/* Also, the top 4 bits of the object Id are set to the object type. */
  8950. +#define EXTRA_OBJECT_TYPE_SHIFT (28)
  8951. +#define EXTRA_OBJECT_TYPE_MASK ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT)
  8952. +
  8953. +static void yaffs_dump_packed_tags2_tags_only(
  8954. + const struct yaffs_packed_tags2_tags_only *ptt)
  8955. +{
  8956. + yaffs_trace(YAFFS_TRACE_MTD,
  8957. + "packed tags obj %d chunk %d byte %d seq %d",
  8958. + ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
  8959. +}
  8960. +
  8961. +static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
  8962. +{
  8963. + yaffs_dump_packed_tags2_tags_only(&pt->t);
  8964. +}
  8965. +
  8966. +static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
  8967. +{
  8968. + yaffs_trace(YAFFS_TRACE_MTD,
  8969. + "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
  8970. + t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
  8971. + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
  8972. + t->seq_number);
  8973. +
  8974. +}
  8975. +
  8976. +static int yaffs_check_tags_extra_packable(const struct yaffs_ext_tags *t)
  8977. +{
  8978. + if (t->chunk_id != 0 || !t->extra_available)
  8979. + return 0;
  8980. +
  8981. + /* Check if the file size is too long to store */
  8982. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE &&
  8983. + (t->extra_file_size >> 31) != 0)
  8984. + return 0;
  8985. + return 1;
  8986. +}
  8987. +
  8988. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
  8989. + const struct yaffs_ext_tags *t)
  8990. +{
  8991. + ptt->chunk_id = t->chunk_id;
  8992. + ptt->seq_number = t->seq_number;
  8993. + ptt->n_bytes = t->n_bytes;
  8994. + ptt->obj_id = t->obj_id;
  8995. +
  8996. + /* Only store extra tags for object headers.
  8997. + * If it is a file then only store if the file size is short\
  8998. + * enough to fit.
  8999. + */
  9000. + if (yaffs_check_tags_extra_packable(t)) {
  9001. + /* Store the extra header info instead */
  9002. + /* We save the parent object in the chunk_id */
  9003. + ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
  9004. + if (t->extra_is_shrink)
  9005. + ptt->chunk_id |= EXTRA_SHRINK_FLAG;
  9006. + if (t->extra_shadows)
  9007. + ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
  9008. +
  9009. + ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9010. + ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
  9011. +
  9012. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9013. + ptt->n_bytes = t->extra_equiv_id;
  9014. + else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  9015. + ptt->n_bytes = (unsigned) t->extra_file_size;
  9016. + else
  9017. + ptt->n_bytes = 0;
  9018. + }
  9019. +
  9020. + yaffs_dump_packed_tags2_tags_only(ptt);
  9021. + yaffs_dump_tags2(t);
  9022. +}
  9023. +
  9024. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9025. + const struct yaffs_ext_tags *t, int tags_ecc)
  9026. +{
  9027. + yaffs_pack_tags2_tags_only(&pt->t, t);
  9028. +
  9029. + if (tags_ecc)
  9030. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9031. + sizeof(struct yaffs_packed_tags2_tags_only),
  9032. + &pt->ecc);
  9033. +}
  9034. +
  9035. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9036. + struct yaffs_packed_tags2_tags_only *ptt)
  9037. +{
  9038. + memset(t, 0, sizeof(struct yaffs_ext_tags));
  9039. +
  9040. + if (ptt->seq_number == 0xffffffff)
  9041. + return;
  9042. +
  9043. + t->block_bad = 0;
  9044. + t->chunk_used = 1;
  9045. + t->obj_id = ptt->obj_id;
  9046. + t->chunk_id = ptt->chunk_id;
  9047. + t->n_bytes = ptt->n_bytes;
  9048. + t->is_deleted = 0;
  9049. + t->serial_number = 0;
  9050. + t->seq_number = ptt->seq_number;
  9051. +
  9052. + /* Do extra header info stuff */
  9053. + if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
  9054. + t->chunk_id = 0;
  9055. + t->n_bytes = 0;
  9056. +
  9057. + t->extra_available = 1;
  9058. + t->extra_parent_id = ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
  9059. + t->extra_is_shrink = ptt->chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
  9060. + t->extra_shadows = ptt->chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
  9061. + t->extra_obj_type = ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
  9062. + t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
  9063. +
  9064. + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
  9065. + t->extra_equiv_id = ptt->n_bytes;
  9066. + else
  9067. + t->extra_file_size = ptt->n_bytes;
  9068. + }
  9069. + yaffs_dump_packed_tags2_tags_only(ptt);
  9070. + yaffs_dump_tags2(t);
  9071. +}
  9072. +
  9073. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9074. + int tags_ecc)
  9075. +{
  9076. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9077. +
  9078. + if (pt->t.seq_number != 0xffffffff && tags_ecc) {
  9079. + /* Chunk is in use and we need to do ECC */
  9080. +
  9081. + struct yaffs_ecc_other ecc;
  9082. + int result;
  9083. + yaffs_ecc_calc_other((unsigned char *)&pt->t,
  9084. + sizeof(struct yaffs_packed_tags2_tags_only),
  9085. + &ecc);
  9086. + result =
  9087. + yaffs_ecc_correct_other((unsigned char *)&pt->t,
  9088. + sizeof(struct yaffs_packed_tags2_tags_only),
  9089. + &pt->ecc, &ecc);
  9090. + switch (result) {
  9091. + case 0:
  9092. + ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9093. + break;
  9094. + case 1:
  9095. + ecc_result = YAFFS_ECC_RESULT_FIXED;
  9096. + break;
  9097. + case -1:
  9098. + ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9099. + break;
  9100. + default:
  9101. + ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  9102. + }
  9103. + }
  9104. + yaffs_unpack_tags2_tags_only(t, &pt->t);
  9105. +
  9106. + t->ecc_result = ecc_result;
  9107. +
  9108. + yaffs_dump_packed_tags2(pt);
  9109. + yaffs_dump_tags2(t);
  9110. +}
  9111. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags2.h linux-3.15-rc5/fs/yaffs2/yaffs_packedtags2.h
  9112. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  9113. +++ linux-3.15-rc5/fs/yaffs2/yaffs_packedtags2.h 2014-05-17 01:53:27.000000000 +0200
  9114. @@ -0,0 +1,47 @@
  9115. +/*
  9116. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9117. + *
  9118. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9119. + * for Toby Churchill Ltd and Brightstar Engineering
  9120. + *
  9121. + * Created by Charles Manning <charles@aleph1.co.uk>
  9122. + *
  9123. + * This program is free software; you can redistribute it and/or modify
  9124. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9125. + * published by the Free Software Foundation.
  9126. + *
  9127. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9128. + */
  9129. +
  9130. +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
  9131. +
  9132. +#ifndef __YAFFS_PACKEDTAGS2_H__
  9133. +#define __YAFFS_PACKEDTAGS2_H__
  9134. +
  9135. +#include "yaffs_guts.h"
  9136. +#include "yaffs_ecc.h"
  9137. +
  9138. +struct yaffs_packed_tags2_tags_only {
  9139. + unsigned seq_number;
  9140. + unsigned obj_id;
  9141. + unsigned chunk_id;
  9142. + unsigned n_bytes;
  9143. +};
  9144. +
  9145. +struct yaffs_packed_tags2 {
  9146. + struct yaffs_packed_tags2_tags_only t;
  9147. + struct yaffs_ecc_other ecc;
  9148. +};
  9149. +
  9150. +/* Full packed tags with ECC, used for oob tags */
  9151. +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
  9152. + const struct yaffs_ext_tags *t, int tags_ecc);
  9153. +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
  9154. + int tags_ecc);
  9155. +
  9156. +/* Only the tags part (no ECC for use with inband tags */
  9157. +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt,
  9158. + const struct yaffs_ext_tags *t);
  9159. +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
  9160. + struct yaffs_packed_tags2_tags_only *pt);
  9161. +#endif
  9162. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_summary.c linux-3.15-rc5/fs/yaffs2/yaffs_summary.c
  9163. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_summary.c 1970-01-01 01:00:00.000000000 +0100
  9164. +++ linux-3.15-rc5/fs/yaffs2/yaffs_summary.c 2014-05-17 01:53:27.000000000 +0200
  9165. @@ -0,0 +1,312 @@
  9166. +/*
  9167. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9168. + *
  9169. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9170. + * for Toby Churchill Ltd and Brightstar Engineering
  9171. + *
  9172. + * Created by Charles Manning <charles@aleph1.co.uk>
  9173. + *
  9174. + * This program is free software; you can redistribute it and/or modify
  9175. + * it under the terms of the GNU General Public License version 2 as
  9176. + * published by the Free Software Foundation.
  9177. + */
  9178. +
  9179. +/* Summaries write the useful part of the tags for the chunks in a block into an
  9180. + * an array which is written to the last n chunks of the block.
  9181. + * Reading the summaries gives all the tags for the block in one read. Much
  9182. + * faster.
  9183. + *
  9184. + * Chunks holding summaries are marked with tags making it look like
  9185. + * they are part of a fake file.
  9186. + *
  9187. + * The summary could also be used during gc.
  9188. + *
  9189. + */
  9190. +
  9191. +#include "yaffs_summary.h"
  9192. +#include "yaffs_packedtags2.h"
  9193. +#include "yaffs_nand.h"
  9194. +#include "yaffs_getblockinfo.h"
  9195. +#include "yaffs_bitmap.h"
  9196. +
  9197. +/*
  9198. + * The summary is built up in an array of summary tags.
  9199. + * This gets written to the last one or two (maybe more) chunks in a block.
  9200. + * A summary header is written as the first part of each chunk of summary data.
  9201. + * The summary header must match or the summary is rejected.
  9202. + */
  9203. +
  9204. +/* Summary tags don't need the sequence number because that is redundant. */
  9205. +struct yaffs_summary_tags {
  9206. + unsigned obj_id;
  9207. + unsigned chunk_id;
  9208. + unsigned n_bytes;
  9209. +};
  9210. +
  9211. +/* Summary header */
  9212. +struct yaffs_summary_header {
  9213. + unsigned version; /* Must match current version */
  9214. + unsigned block; /* Must be this block */
  9215. + unsigned seq; /* Must be this sequence number */
  9216. + unsigned sum; /* Just add up all the bytes in the tags */
  9217. +};
  9218. +
  9219. +
  9220. +static void yaffs_summary_clear(struct yaffs_dev *dev)
  9221. +{
  9222. + if (!dev->sum_tags)
  9223. + return;
  9224. + memset(dev->sum_tags, 0, dev->chunks_per_summary *
  9225. + sizeof(struct yaffs_summary_tags));
  9226. +}
  9227. +
  9228. +
  9229. +void yaffs_summary_deinit(struct yaffs_dev *dev)
  9230. +{
  9231. + kfree(dev->sum_tags);
  9232. + dev->sum_tags = NULL;
  9233. + kfree(dev->gc_sum_tags);
  9234. + dev->gc_sum_tags = NULL;
  9235. + dev->chunks_per_summary = 0;
  9236. +}
  9237. +
  9238. +int yaffs_summary_init(struct yaffs_dev *dev)
  9239. +{
  9240. + int sum_bytes;
  9241. + int chunks_used; /* Number of chunks used by summary */
  9242. + int sum_tags_bytes;
  9243. +
  9244. + sum_bytes = dev->param.chunks_per_block *
  9245. + sizeof(struct yaffs_summary_tags);
  9246. +
  9247. + chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
  9248. + (dev->data_bytes_per_chunk -
  9249. + sizeof(struct yaffs_summary_header));
  9250. +
  9251. + dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
  9252. + sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
  9253. + dev->chunks_per_summary;
  9254. + dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
  9255. + dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
  9256. + if (!dev->sum_tags || !dev->gc_sum_tags) {
  9257. + yaffs_summary_deinit(dev);
  9258. + return YAFFS_FAIL;
  9259. + }
  9260. +
  9261. + yaffs_summary_clear(dev);
  9262. +
  9263. + return YAFFS_OK;
  9264. +}
  9265. +
  9266. +static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
  9267. +{
  9268. + u8 *sum_buffer = (u8 *)dev->sum_tags;
  9269. + int i;
  9270. + unsigned sum = 0;
  9271. +
  9272. + i = sizeof(struct yaffs_summary_tags) *
  9273. + dev->chunks_per_summary;
  9274. + while (i > 0) {
  9275. + sum += *sum_buffer;
  9276. + sum_buffer++;
  9277. + i--;
  9278. + }
  9279. +
  9280. + return sum;
  9281. +}
  9282. +
  9283. +static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
  9284. +{
  9285. + struct yaffs_ext_tags tags;
  9286. + u8 *buffer;
  9287. + u8 *sum_buffer = (u8 *)dev->sum_tags;
  9288. + int n_bytes;
  9289. + int chunk_in_nand;
  9290. + int chunk_in_block;
  9291. + int result;
  9292. + int this_tx;
  9293. + struct yaffs_summary_header hdr;
  9294. + int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
  9295. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  9296. +
  9297. + buffer = yaffs_get_temp_buffer(dev);
  9298. + n_bytes = sizeof(struct yaffs_summary_tags) *
  9299. + dev->chunks_per_summary;
  9300. + memset(&tags, 0, sizeof(struct yaffs_ext_tags));
  9301. + tags.obj_id = YAFFS_OBJECTID_SUMMARY;
  9302. + tags.chunk_id = 1;
  9303. + chunk_in_block = dev->chunks_per_summary;
  9304. + chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
  9305. + dev->chunks_per_summary;
  9306. + hdr.version = YAFFS_SUMMARY_VERSION;
  9307. + hdr.block = blk;
  9308. + hdr.seq = bi->seq_number;
  9309. + hdr.sum = yaffs_summary_sum(dev);
  9310. +
  9311. + do {
  9312. + this_tx = n_bytes;
  9313. + if (this_tx > sum_bytes_per_chunk)
  9314. + this_tx = sum_bytes_per_chunk;
  9315. + memcpy(buffer, &hdr, sizeof(hdr));
  9316. + memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
  9317. + tags.n_bytes = this_tx + sizeof(hdr);
  9318. + result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
  9319. + buffer, &tags);
  9320. +
  9321. + if (result != YAFFS_OK)
  9322. + break;
  9323. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  9324. + bi->pages_in_use++;
  9325. + dev->n_free_chunks--;
  9326. +
  9327. + n_bytes -= this_tx;
  9328. + sum_buffer += this_tx;
  9329. + chunk_in_nand++;
  9330. + chunk_in_block++;
  9331. + tags.chunk_id++;
  9332. + } while (result == YAFFS_OK && n_bytes > 0);
  9333. + yaffs_release_temp_buffer(dev, buffer);
  9334. +
  9335. +
  9336. + if (result == YAFFS_OK)
  9337. + bi->has_summary = 1;
  9338. +
  9339. +
  9340. + return result;
  9341. +}
  9342. +
  9343. +int yaffs_summary_read(struct yaffs_dev *dev,
  9344. + struct yaffs_summary_tags *st,
  9345. + int blk)
  9346. +{
  9347. + struct yaffs_ext_tags tags;
  9348. + u8 *buffer;
  9349. + u8 *sum_buffer = (u8 *)st;
  9350. + int n_bytes;
  9351. + int chunk_id;
  9352. + int chunk_in_nand;
  9353. + int chunk_in_block;
  9354. + int result;
  9355. + int this_tx;
  9356. + struct yaffs_summary_header hdr;
  9357. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  9358. + int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
  9359. + int sum_tags_bytes;
  9360. +
  9361. + sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
  9362. + dev->chunks_per_summary;
  9363. + buffer = yaffs_get_temp_buffer(dev);
  9364. + n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
  9365. + chunk_in_block = dev->chunks_per_summary;
  9366. + chunk_in_nand = blk * dev->param.chunks_per_block +
  9367. + dev->chunks_per_summary;
  9368. + chunk_id = 1;
  9369. + do {
  9370. + this_tx = n_bytes;
  9371. + if (this_tx > sum_bytes_per_chunk)
  9372. + this_tx = sum_bytes_per_chunk;
  9373. + result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
  9374. + buffer, &tags);
  9375. +
  9376. + if (tags.chunk_id != chunk_id ||
  9377. + tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
  9378. + tags.chunk_used == 0 ||
  9379. + tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  9380. + tags.n_bytes != (this_tx + sizeof(hdr)))
  9381. + result = YAFFS_FAIL;
  9382. + if (result != YAFFS_OK)
  9383. + break;
  9384. +
  9385. + if (st == dev->sum_tags) {
  9386. + /* If we're scanning then update the block info */
  9387. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  9388. + bi->pages_in_use++;
  9389. + }
  9390. + memcpy(&hdr, buffer, sizeof(hdr));
  9391. + memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
  9392. + n_bytes -= this_tx;
  9393. + sum_buffer += this_tx;
  9394. + chunk_in_nand++;
  9395. + chunk_in_block++;
  9396. + chunk_id++;
  9397. + } while (result == YAFFS_OK && n_bytes > 0);
  9398. + yaffs_release_temp_buffer(dev, buffer);
  9399. +
  9400. + if (result == YAFFS_OK) {
  9401. + /* Verify header */
  9402. + if (hdr.version != YAFFS_SUMMARY_VERSION ||
  9403. + hdr.seq != bi->seq_number ||
  9404. + hdr.sum != yaffs_summary_sum(dev))
  9405. + result = YAFFS_FAIL;
  9406. + }
  9407. +
  9408. + if (st == dev->sum_tags && result == YAFFS_OK)
  9409. + bi->has_summary = 1;
  9410. +
  9411. + return result;
  9412. +}
  9413. +
  9414. +int yaffs_summary_add(struct yaffs_dev *dev,
  9415. + struct yaffs_ext_tags *tags,
  9416. + int chunk_in_nand)
  9417. +{
  9418. + struct yaffs_packed_tags2_tags_only tags_only;
  9419. + struct yaffs_summary_tags *sum_tags;
  9420. + int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
  9421. + int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
  9422. +
  9423. + if (!dev->sum_tags)
  9424. + return YAFFS_OK;
  9425. +
  9426. + if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  9427. + yaffs_pack_tags2_tags_only(&tags_only, tags);
  9428. + sum_tags = &dev->sum_tags[chunk_in_block];
  9429. + sum_tags->chunk_id = tags_only.chunk_id;
  9430. + sum_tags->n_bytes = tags_only.n_bytes;
  9431. + sum_tags->obj_id = tags_only.obj_id;
  9432. +
  9433. + if (chunk_in_block == dev->chunks_per_summary - 1) {
  9434. + /* Time to write out the summary */
  9435. + yaffs_summary_write(dev, block_in_nand);
  9436. + yaffs_summary_clear(dev);
  9437. + yaffs_skip_rest_of_block(dev);
  9438. + }
  9439. + }
  9440. + return YAFFS_OK;
  9441. +}
  9442. +
  9443. +int yaffs_summary_fetch(struct yaffs_dev *dev,
  9444. + struct yaffs_ext_tags *tags,
  9445. + int chunk_in_block)
  9446. +{
  9447. + struct yaffs_packed_tags2_tags_only tags_only;
  9448. + struct yaffs_summary_tags *sum_tags;
  9449. + if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  9450. + sum_tags = &dev->sum_tags[chunk_in_block];
  9451. + tags_only.chunk_id = sum_tags->chunk_id;
  9452. + tags_only.n_bytes = sum_tags->n_bytes;
  9453. + tags_only.obj_id = sum_tags->obj_id;
  9454. + yaffs_unpack_tags2_tags_only(tags, &tags_only);
  9455. + return YAFFS_OK;
  9456. + }
  9457. + return YAFFS_FAIL;
  9458. +}
  9459. +
  9460. +void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
  9461. +{
  9462. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  9463. + int i;
  9464. +
  9465. + if (!bi->has_summary)
  9466. + return;
  9467. +
  9468. + for (i = dev->chunks_per_summary;
  9469. + i < dev->param.chunks_per_block;
  9470. + i++) {
  9471. + if (yaffs_check_chunk_bit(dev, blk, i)) {
  9472. + yaffs_clear_chunk_bit(dev, blk, i);
  9473. + bi->pages_in_use--;
  9474. + dev->n_free_chunks++;
  9475. + }
  9476. + }
  9477. +}
  9478. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_summary.h linux-3.15-rc5/fs/yaffs2/yaffs_summary.h
  9479. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_summary.h 1970-01-01 01:00:00.000000000 +0100
  9480. +++ linux-3.15-rc5/fs/yaffs2/yaffs_summary.h 2014-05-17 01:53:27.000000000 +0200
  9481. @@ -0,0 +1,37 @@
  9482. +/*
  9483. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9484. + *
  9485. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9486. + * for Toby Churchill Ltd and Brightstar Engineering
  9487. + *
  9488. + * Created by Charles Manning <charles@aleph1.co.uk>
  9489. + *
  9490. + * This program is free software; you can redistribute it and/or modify
  9491. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9492. + * published by the Free Software Foundation.
  9493. + *
  9494. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9495. + */
  9496. +
  9497. +#ifndef __YAFFS_SUMMARY_H__
  9498. +#define __YAFFS_SUMMARY_H__
  9499. +
  9500. +#include "yaffs_packedtags2.h"
  9501. +
  9502. +
  9503. +int yaffs_summary_init(struct yaffs_dev *dev);
  9504. +void yaffs_summary_deinit(struct yaffs_dev *dev);
  9505. +
  9506. +int yaffs_summary_add(struct yaffs_dev *dev,
  9507. + struct yaffs_ext_tags *tags,
  9508. + int chunk_in_block);
  9509. +int yaffs_summary_fetch(struct yaffs_dev *dev,
  9510. + struct yaffs_ext_tags *tags,
  9511. + int chunk_in_block);
  9512. +int yaffs_summary_read(struct yaffs_dev *dev,
  9513. + struct yaffs_summary_tags *st,
  9514. + int blk);
  9515. +void yaffs_summary_gc(struct yaffs_dev *dev, int blk);
  9516. +
  9517. +
  9518. +#endif
  9519. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagscompat.c linux-3.15-rc5/fs/yaffs2/yaffs_tagscompat.c
  9520. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  9521. +++ linux-3.15-rc5/fs/yaffs2/yaffs_tagscompat.c 2014-05-17 01:53:27.000000000 +0200
  9522. @@ -0,0 +1,381 @@
  9523. +/*
  9524. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9525. + *
  9526. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9527. + * for Toby Churchill Ltd and Brightstar Engineering
  9528. + *
  9529. + * Created by Charles Manning <charles@aleph1.co.uk>
  9530. + *
  9531. + * This program is free software; you can redistribute it and/or modify
  9532. + * it under the terms of the GNU General Public License version 2 as
  9533. + * published by the Free Software Foundation.
  9534. + */
  9535. +
  9536. +#include "yaffs_guts.h"
  9537. +#include "yaffs_tagscompat.h"
  9538. +#include "yaffs_ecc.h"
  9539. +#include "yaffs_getblockinfo.h"
  9540. +#include "yaffs_trace.h"
  9541. +
  9542. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
  9543. +
  9544. +
  9545. +/********** Tags ECC calculations *********/
  9546. +
  9547. +
  9548. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
  9549. +{
  9550. + /* Calculate an ecc */
  9551. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9552. + unsigned i, j;
  9553. + unsigned ecc = 0;
  9554. + unsigned bit = 0;
  9555. +
  9556. + tags->ecc = 0;
  9557. +
  9558. + for (i = 0; i < 8; i++) {
  9559. + for (j = 1; j & 0xff; j <<= 1) {
  9560. + bit++;
  9561. + if (b[i] & j)
  9562. + ecc ^= bit;
  9563. + }
  9564. + }
  9565. + tags->ecc = ecc;
  9566. +}
  9567. +
  9568. +int yaffs_check_tags_ecc(struct yaffs_tags *tags)
  9569. +{
  9570. + unsigned ecc = tags->ecc;
  9571. +
  9572. + yaffs_calc_tags_ecc(tags);
  9573. +
  9574. + ecc ^= tags->ecc;
  9575. +
  9576. + if (ecc && ecc <= 64) {
  9577. + /* TODO: Handle the failure better. Retire? */
  9578. + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
  9579. +
  9580. + ecc--;
  9581. +
  9582. + b[ecc / 8] ^= (1 << (ecc & 7));
  9583. +
  9584. + /* Now recvalc the ecc */
  9585. + yaffs_calc_tags_ecc(tags);
  9586. +
  9587. + return 1; /* recovered error */
  9588. + } else if (ecc) {
  9589. + /* Wierd ecc failure value */
  9590. + /* TODO Need to do somethiong here */
  9591. + return -1; /* unrecovered error */
  9592. + }
  9593. + return 0;
  9594. +}
  9595. +
  9596. +/********** Tags **********/
  9597. +
  9598. +static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
  9599. + struct yaffs_tags *tags_ptr)
  9600. +{
  9601. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9602. +
  9603. + yaffs_calc_tags_ecc(tags_ptr);
  9604. +
  9605. + spare_ptr->tb0 = tu->as_bytes[0];
  9606. + spare_ptr->tb1 = tu->as_bytes[1];
  9607. + spare_ptr->tb2 = tu->as_bytes[2];
  9608. + spare_ptr->tb3 = tu->as_bytes[3];
  9609. + spare_ptr->tb4 = tu->as_bytes[4];
  9610. + spare_ptr->tb5 = tu->as_bytes[5];
  9611. + spare_ptr->tb6 = tu->as_bytes[6];
  9612. + spare_ptr->tb7 = tu->as_bytes[7];
  9613. +}
  9614. +
  9615. +static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
  9616. + struct yaffs_spare *spare_ptr,
  9617. + struct yaffs_tags *tags_ptr)
  9618. +{
  9619. + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
  9620. + int result;
  9621. +
  9622. + tu->as_bytes[0] = spare_ptr->tb0;
  9623. + tu->as_bytes[1] = spare_ptr->tb1;
  9624. + tu->as_bytes[2] = spare_ptr->tb2;
  9625. + tu->as_bytes[3] = spare_ptr->tb3;
  9626. + tu->as_bytes[4] = spare_ptr->tb4;
  9627. + tu->as_bytes[5] = spare_ptr->tb5;
  9628. + tu->as_bytes[6] = spare_ptr->tb6;
  9629. + tu->as_bytes[7] = spare_ptr->tb7;
  9630. +
  9631. + result = yaffs_check_tags_ecc(tags_ptr);
  9632. + if (result > 0)
  9633. + dev->n_tags_ecc_fixed++;
  9634. + else if (result < 0)
  9635. + dev->n_tags_ecc_unfixed++;
  9636. +}
  9637. +
  9638. +static void yaffs_spare_init(struct yaffs_spare *spare)
  9639. +{
  9640. + memset(spare, 0xff, sizeof(struct yaffs_spare));
  9641. +}
  9642. +
  9643. +static int yaffs_wr_nand(struct yaffs_dev *dev,
  9644. + int nand_chunk, const u8 *data,
  9645. + struct yaffs_spare *spare)
  9646. +{
  9647. + int data_size = dev->data_bytes_per_chunk;
  9648. +
  9649. + return dev->drv.drv_write_chunk_fn(dev, nand_chunk,
  9650. + data, data_size,
  9651. + (u8 *) spare, sizeof(*spare));
  9652. +}
  9653. +
  9654. +static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
  9655. + int nand_chunk,
  9656. + u8 *data,
  9657. + struct yaffs_spare *spare,
  9658. + enum yaffs_ecc_result *ecc_result,
  9659. + int correct_errors)
  9660. +{
  9661. + int ret_val;
  9662. + struct yaffs_spare local_spare;
  9663. + int data_size;
  9664. + int spare_size;
  9665. + int ecc_result1, ecc_result2;
  9666. + u8 calc_ecc[3];
  9667. +
  9668. + if (!spare) {
  9669. + /* If we don't have a real spare, then we use a local one. */
  9670. + /* Need this for the calculation of the ecc */
  9671. + spare = &local_spare;
  9672. + }
  9673. + data_size = dev->data_bytes_per_chunk;
  9674. + spare_size = sizeof(struct yaffs_spare);
  9675. +
  9676. + if (dev->param.use_nand_ecc)
  9677. + return dev->drv.drv_read_chunk_fn(dev, nand_chunk,
  9678. + data, data_size,
  9679. + (u8 *) spare, spare_size,
  9680. + ecc_result);
  9681. +
  9682. +
  9683. + /* Handle the ECC at this level. */
  9684. +
  9685. + ret_val = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
  9686. + data, data_size,
  9687. + (u8 *)spare, spare_size,
  9688. + NULL);
  9689. + if (!data || !correct_errors)
  9690. + return ret_val;
  9691. +
  9692. + /* Do ECC correction if needed. */
  9693. + yaffs_ecc_calc(data, calc_ecc);
  9694. + ecc_result1 = yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
  9695. + yaffs_ecc_calc(&data[256], calc_ecc);
  9696. + ecc_result2 = yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
  9697. +
  9698. + if (ecc_result1 > 0) {
  9699. + yaffs_trace(YAFFS_TRACE_ERROR,
  9700. + "**>>yaffs ecc error fix performed on chunk %d:0",
  9701. + nand_chunk);
  9702. + dev->n_ecc_fixed++;
  9703. + } else if (ecc_result1 < 0) {
  9704. + yaffs_trace(YAFFS_TRACE_ERROR,
  9705. + "**>>yaffs ecc error unfixed on chunk %d:0",
  9706. + nand_chunk);
  9707. + dev->n_ecc_unfixed++;
  9708. + }
  9709. +
  9710. + if (ecc_result2 > 0) {
  9711. + yaffs_trace(YAFFS_TRACE_ERROR,
  9712. + "**>>yaffs ecc error fix performed on chunk %d:1",
  9713. + nand_chunk);
  9714. + dev->n_ecc_fixed++;
  9715. + } else if (ecc_result2 < 0) {
  9716. + yaffs_trace(YAFFS_TRACE_ERROR,
  9717. + "**>>yaffs ecc error unfixed on chunk %d:1",
  9718. + nand_chunk);
  9719. + dev->n_ecc_unfixed++;
  9720. + }
  9721. +
  9722. + if (ecc_result1 || ecc_result2) {
  9723. + /* We had a data problem on this page */
  9724. + yaffs_handle_rd_data_error(dev, nand_chunk);
  9725. + }
  9726. +
  9727. + if (ecc_result1 < 0 || ecc_result2 < 0)
  9728. + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  9729. + else if (ecc_result1 > 0 || ecc_result2 > 0)
  9730. + *ecc_result = YAFFS_ECC_RESULT_FIXED;
  9731. + else
  9732. + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
  9733. +
  9734. + return ret_val;
  9735. +}
  9736. +
  9737. +/*
  9738. + * Functions for robustisizing
  9739. + */
  9740. +
  9741. +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
  9742. +{
  9743. + int flash_block = nand_chunk / dev->param.chunks_per_block;
  9744. +
  9745. + /* Mark the block for retirement */
  9746. + yaffs_get_block_info(dev, flash_block + dev->block_offset)->
  9747. + needs_retiring = 1;
  9748. + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  9749. + "**>>Block %d marked for retirement",
  9750. + flash_block);
  9751. +
  9752. + /* TODO:
  9753. + * Just do a garbage collection on the affected block
  9754. + * then retire the block
  9755. + * NB recursion
  9756. + */
  9757. +}
  9758. +
  9759. +static int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  9760. + int nand_chunk,
  9761. + const u8 *data, const struct yaffs_ext_tags *ext_tags)
  9762. +{
  9763. + struct yaffs_spare spare;
  9764. + struct yaffs_tags tags;
  9765. +
  9766. + yaffs_spare_init(&spare);
  9767. +
  9768. + if (ext_tags->is_deleted)
  9769. + spare.page_status = 0;
  9770. + else {
  9771. + tags.obj_id = ext_tags->obj_id;
  9772. + tags.chunk_id = ext_tags->chunk_id;
  9773. +
  9774. + tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
  9775. +
  9776. + if (dev->data_bytes_per_chunk >= 1024)
  9777. + tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
  9778. + else
  9779. + tags.n_bytes_msb = 3;
  9780. +
  9781. + tags.serial_number = ext_tags->serial_number;
  9782. +
  9783. + if (!dev->param.use_nand_ecc && data) {
  9784. + yaffs_ecc_calc(data, spare.ecc1);
  9785. + yaffs_ecc_calc(&data[256], spare.ecc2);
  9786. + }
  9787. +
  9788. + yaffs_load_tags_to_spare(&spare, &tags);
  9789. + }
  9790. + return yaffs_wr_nand(dev, nand_chunk, data, &spare);
  9791. +}
  9792. +
  9793. +static int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  9794. + int nand_chunk,
  9795. + u8 *data, struct yaffs_ext_tags *ext_tags)
  9796. +{
  9797. + struct yaffs_spare spare;
  9798. + struct yaffs_tags tags;
  9799. + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
  9800. + static struct yaffs_spare spare_ff;
  9801. + static int init;
  9802. + int deleted;
  9803. +
  9804. + if (!init) {
  9805. + memset(&spare_ff, 0xff, sizeof(spare_ff));
  9806. + init = 1;
  9807. + }
  9808. +
  9809. + if (!yaffs_rd_chunk_nand(dev, nand_chunk,
  9810. + data, &spare, &ecc_result, 1))
  9811. + return YAFFS_FAIL;
  9812. +
  9813. + /* ext_tags may be NULL */
  9814. + if (!ext_tags)
  9815. + return YAFFS_OK;
  9816. +
  9817. + deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
  9818. +
  9819. + ext_tags->is_deleted = deleted;
  9820. + ext_tags->ecc_result = ecc_result;
  9821. + ext_tags->block_bad = 0; /* We're reading it */
  9822. + /* therefore it is not a bad block */
  9823. + ext_tags->chunk_used =
  9824. + memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
  9825. +
  9826. + if (ext_tags->chunk_used) {
  9827. + yaffs_get_tags_from_spare(dev, &spare, &tags);
  9828. + ext_tags->obj_id = tags.obj_id;
  9829. + ext_tags->chunk_id = tags.chunk_id;
  9830. + ext_tags->n_bytes = tags.n_bytes_lsb;
  9831. +
  9832. + if (dev->data_bytes_per_chunk >= 1024)
  9833. + ext_tags->n_bytes |=
  9834. + (((unsigned)tags.n_bytes_msb) << 10);
  9835. +
  9836. + ext_tags->serial_number = tags.serial_number;
  9837. + }
  9838. +
  9839. + return YAFFS_OK;
  9840. +}
  9841. +
  9842. +static int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
  9843. +{
  9844. + struct yaffs_spare spare;
  9845. +
  9846. + memset(&spare, 0xff, sizeof(struct yaffs_spare));
  9847. +
  9848. + spare.block_status = 'Y';
  9849. +
  9850. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
  9851. + &spare);
  9852. + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
  9853. + NULL, &spare);
  9854. +
  9855. + return YAFFS_OK;
  9856. +}
  9857. +
  9858. +static int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  9859. + int block_no,
  9860. + enum yaffs_block_state *state,
  9861. + u32 *seq_number)
  9862. +{
  9863. + struct yaffs_spare spare0, spare1;
  9864. + static struct yaffs_spare spare_ff;
  9865. + static int init;
  9866. + enum yaffs_ecc_result dummy;
  9867. +
  9868. + if (!init) {
  9869. + memset(&spare_ff, 0xff, sizeof(spare_ff));
  9870. + init = 1;
  9871. + }
  9872. +
  9873. + *seq_number = 0;
  9874. +
  9875. + /* Look for bad block markers in the first two chunks */
  9876. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block,
  9877. + NULL, &spare0, &dummy, 0);
  9878. + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
  9879. + NULL, &spare1, &dummy, 0);
  9880. +
  9881. + if (hweight8(spare0.block_status & spare1.block_status) < 7)
  9882. + *state = YAFFS_BLOCK_STATE_DEAD;
  9883. + else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
  9884. + *state = YAFFS_BLOCK_STATE_EMPTY;
  9885. + else
  9886. + *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
  9887. +
  9888. + return YAFFS_OK;
  9889. +}
  9890. +
  9891. +void yaffs_tags_compat_install(struct yaffs_dev *dev)
  9892. +{
  9893. + if(dev->param.is_yaffs2)
  9894. + return;
  9895. + if(!dev->tagger.write_chunk_tags_fn)
  9896. + dev->tagger.write_chunk_tags_fn = yaffs_tags_compat_wr;
  9897. + if(!dev->tagger.read_chunk_tags_fn)
  9898. + dev->tagger.read_chunk_tags_fn = yaffs_tags_compat_rd;
  9899. + if(!dev->tagger.query_block_fn)
  9900. + dev->tagger.query_block_fn = yaffs_tags_compat_query_block;
  9901. + if(!dev->tagger.mark_bad_fn)
  9902. + dev->tagger.mark_bad_fn = yaffs_tags_compat_mark_bad;
  9903. +}
  9904. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagscompat.h linux-3.15-rc5/fs/yaffs2/yaffs_tagscompat.h
  9905. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  9906. +++ linux-3.15-rc5/fs/yaffs2/yaffs_tagscompat.h 2014-05-17 01:53:27.000000000 +0200
  9907. @@ -0,0 +1,44 @@
  9908. +/*
  9909. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  9910. + *
  9911. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9912. + * for Toby Churchill Ltd and Brightstar Engineering
  9913. + *
  9914. + * Created by Charles Manning <charles@aleph1.co.uk>
  9915. + *
  9916. + * This program is free software; you can redistribute it and/or modify
  9917. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  9918. + * published by the Free Software Foundation.
  9919. + *
  9920. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  9921. + */
  9922. +
  9923. +#ifndef __YAFFS_TAGSCOMPAT_H__
  9924. +#define __YAFFS_TAGSCOMPAT_H__
  9925. +
  9926. +
  9927. +#include "yaffs_guts.h"
  9928. +
  9929. +#if 0
  9930. +
  9931. +
  9932. +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
  9933. + int nand_chunk,
  9934. + const u8 *data, const struct yaffs_ext_tags *tags);
  9935. +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
  9936. + int nand_chunk,
  9937. + u8 *data, struct yaffs_ext_tags *tags);
  9938. +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no);
  9939. +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
  9940. + int block_no,
  9941. + enum yaffs_block_state *state,
  9942. + u32 *seq_number);
  9943. +
  9944. +#endif
  9945. +
  9946. +
  9947. +void yaffs_tags_compat_install(struct yaffs_dev *dev);
  9948. +void yaffs_calc_tags_ecc(struct yaffs_tags *tags);
  9949. +int yaffs_check_tags_ecc(struct yaffs_tags *tags);
  9950. +
  9951. +#endif
  9952. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagsmarshall.c linux-3.15-rc5/fs/yaffs2/yaffs_tagsmarshall.c
  9953. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagsmarshall.c 1970-01-01 01:00:00.000000000 +0100
  9954. +++ linux-3.15-rc5/fs/yaffs2/yaffs_tagsmarshall.c 2014-05-17 01:53:27.000000000 +0200
  9955. @@ -0,0 +1,199 @@
  9956. +/*
  9957. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  9958. + *
  9959. + * Copyright (C) 2002-2011 Aleph One Ltd.
  9960. + * for Toby Churchill Ltd and Brightstar Engineering
  9961. + *
  9962. + * Created by Charles Manning <charles@aleph1.co.uk>
  9963. + *
  9964. + * This program is free software; you can redistribute it and/or modify
  9965. + * it under the terms of the GNU General Public License version 2 as
  9966. + * published by the Free Software Foundation.
  9967. + */
  9968. +
  9969. +#include "yaffs_guts.h"
  9970. +#include "yaffs_trace.h"
  9971. +#include "yaffs_packedtags2.h"
  9972. +
  9973. +static int yaffs_tags_marshall_write(struct yaffs_dev *dev,
  9974. + int nand_chunk, const u8 *data,
  9975. + const struct yaffs_ext_tags *tags)
  9976. +{
  9977. + struct yaffs_packed_tags2 pt;
  9978. + int retval;
  9979. +
  9980. + int packed_tags_size =
  9981. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  9982. + void *packed_tags_ptr =
  9983. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  9984. +
  9985. + yaffs_trace(YAFFS_TRACE_MTD,
  9986. + "yaffs_tags_marshall_write chunk %d data %p tags %p",
  9987. + nand_chunk, data, tags);
  9988. +
  9989. + /* For yaffs2 writing there must be both data and tags.
  9990. + * If we're using inband tags, then the tags are stuffed into
  9991. + * the end of the data buffer.
  9992. + */
  9993. + if (!data || !tags)
  9994. + BUG();
  9995. + else if (dev->param.inband_tags) {
  9996. + struct yaffs_packed_tags2_tags_only *pt2tp;
  9997. + pt2tp =
  9998. + (struct yaffs_packed_tags2_tags_only *)(data +
  9999. + dev->
  10000. + data_bytes_per_chunk);
  10001. + yaffs_pack_tags2_tags_only(pt2tp, tags);
  10002. + } else {
  10003. + yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
  10004. + }
  10005. +
  10006. + retval = dev->drv.drv_write_chunk_fn(dev, nand_chunk,
  10007. + data, dev->param.total_bytes_per_chunk,
  10008. + (dev->param.inband_tags) ? NULL : packed_tags_ptr,
  10009. + (dev->param.inband_tags) ? 0 : packed_tags_size);
  10010. +
  10011. + return retval;
  10012. +}
  10013. +
  10014. +static int yaffs_tags_marshall_read(struct yaffs_dev *dev,
  10015. + int nand_chunk, u8 *data,
  10016. + struct yaffs_ext_tags *tags)
  10017. +{
  10018. + int retval = 0;
  10019. + int local_data = 0;
  10020. + u8 spare_buffer[100];
  10021. + enum yaffs_ecc_result ecc_result;
  10022. +
  10023. + struct yaffs_packed_tags2 pt;
  10024. +
  10025. + int packed_tags_size =
  10026. + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  10027. + void *packed_tags_ptr =
  10028. + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  10029. +
  10030. + yaffs_trace(YAFFS_TRACE_MTD,
  10031. + "yaffs_tags_marshall_read chunk %d data %p tags %p",
  10032. + nand_chunk, data, tags);
  10033. +
  10034. + if (dev->param.inband_tags) {
  10035. + if (!data) {
  10036. + local_data = 1;
  10037. + data = yaffs_get_temp_buffer(dev);
  10038. + }
  10039. + }
  10040. +
  10041. + if (dev->param.inband_tags || (data && !tags))
  10042. + retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
  10043. + data, dev->param.total_bytes_per_chunk,
  10044. + NULL, 0,
  10045. + &ecc_result);
  10046. + else if (tags)
  10047. + retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
  10048. + data, dev->param.total_bytes_per_chunk,
  10049. + spare_buffer, packed_tags_size,
  10050. + &ecc_result);
  10051. + else
  10052. + BUG();
  10053. +
  10054. +
  10055. + if (dev->param.inband_tags) {
  10056. + if (tags) {
  10057. + struct yaffs_packed_tags2_tags_only *pt2tp;
  10058. + pt2tp =
  10059. + (struct yaffs_packed_tags2_tags_only *)
  10060. + &data[dev->data_bytes_per_chunk];
  10061. + yaffs_unpack_tags2_tags_only(tags, pt2tp);
  10062. + }
  10063. + } else if (tags) {
  10064. + memcpy(packed_tags_ptr, spare_buffer, packed_tags_size);
  10065. + yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  10066. + }
  10067. +
  10068. + if (local_data)
  10069. + yaffs_release_temp_buffer(dev, data);
  10070. +
  10071. + if (tags && ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
  10072. + tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  10073. + dev->n_ecc_unfixed++;
  10074. + }
  10075. +
  10076. + if (tags && ecc_result == -YAFFS_ECC_RESULT_FIXED) {
  10077. + if (tags->ecc_result <= YAFFS_ECC_RESULT_NO_ERROR)
  10078. + tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  10079. + dev->n_ecc_fixed++;
  10080. + }
  10081. +
  10082. + if (ecc_result < YAFFS_ECC_RESULT_UNFIXED)
  10083. + return YAFFS_OK;
  10084. + else
  10085. + return YAFFS_FAIL;
  10086. +}
  10087. +
  10088. +static int yaffs_tags_marshall_query_block(struct yaffs_dev *dev, int block_no,
  10089. + enum yaffs_block_state *state,
  10090. + u32 *seq_number)
  10091. +{
  10092. + int retval;
  10093. +
  10094. + yaffs_trace(YAFFS_TRACE_MTD, "yaffs_tags_marshall_query_block %d",
  10095. + block_no);
  10096. +
  10097. + retval = dev->drv.drv_check_bad_fn(dev, block_no);
  10098. +
  10099. + if (retval== YAFFS_FAIL) {
  10100. + yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
  10101. +
  10102. + *state = YAFFS_BLOCK_STATE_DEAD;
  10103. + *seq_number = 0;
  10104. + } else {
  10105. + struct yaffs_ext_tags t;
  10106. +
  10107. + yaffs_tags_marshall_read(dev,
  10108. + block_no * dev->param.chunks_per_block,
  10109. + NULL, &t);
  10110. +
  10111. + if (t.chunk_used) {
  10112. + *seq_number = t.seq_number;
  10113. + *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
  10114. + } else {
  10115. + *seq_number = 0;
  10116. + *state = YAFFS_BLOCK_STATE_EMPTY;
  10117. + }
  10118. + }
  10119. +
  10120. + yaffs_trace(YAFFS_TRACE_MTD,
  10121. + "block query returns seq %d state %d",
  10122. + *seq_number, *state);
  10123. +
  10124. + if (retval == 0)
  10125. + return YAFFS_OK;
  10126. + else
  10127. + return YAFFS_FAIL;
  10128. +}
  10129. +
  10130. +static int yaffs_tags_marshall_mark_bad(struct yaffs_dev *dev, int block_no)
  10131. +{
  10132. + return dev->drv.drv_mark_bad_fn(dev, block_no);
  10133. +
  10134. +}
  10135. +
  10136. +
  10137. +void yaffs_tags_marshall_install(struct yaffs_dev *dev)
  10138. +{
  10139. + if (!dev->param.is_yaffs2)
  10140. + return;
  10141. +
  10142. + if (!dev->tagger.write_chunk_tags_fn)
  10143. + dev->tagger.write_chunk_tags_fn = yaffs_tags_marshall_write;
  10144. +
  10145. + if (!dev->tagger.read_chunk_tags_fn)
  10146. + dev->tagger.read_chunk_tags_fn = yaffs_tags_marshall_read;
  10147. +
  10148. + if (!dev->tagger.query_block_fn)
  10149. + dev->tagger.query_block_fn = yaffs_tags_marshall_query_block;
  10150. +
  10151. + if (!dev->tagger.mark_bad_fn)
  10152. + dev->tagger.mark_bad_fn = yaffs_tags_marshall_mark_bad;
  10153. +
  10154. +}
  10155. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagsmarshall.h linux-3.15-rc5/fs/yaffs2/yaffs_tagsmarshall.h
  10156. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_tagsmarshall.h 1970-01-01 01:00:00.000000000 +0100
  10157. +++ linux-3.15-rc5/fs/yaffs2/yaffs_tagsmarshall.h 2014-05-17 01:53:27.000000000 +0200
  10158. @@ -0,0 +1,22 @@
  10159. +/*
  10160. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10161. + *
  10162. + * Copyright (C) 2002-2011 Aleph One Ltd.
  10163. + * for Toby Churchill Ltd and Brightstar Engineering
  10164. + *
  10165. + * Created by Charles Manning <charles@aleph1.co.uk>
  10166. + *
  10167. + * This program is free software; you can redistribute it and/or modify
  10168. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10169. + * published by the Free Software Foundation.
  10170. + *
  10171. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10172. + */
  10173. +
  10174. +#ifndef __YAFFS_TAGSMARSHALL_H__
  10175. +#define __YAFFS_TAGSMARSHALL_H__
  10176. +
  10177. +#include "yaffs_guts.h"
  10178. +void yaffs_tags_marshall_install(struct yaffs_dev *dev);
  10179. +
  10180. +#endif
  10181. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_trace.h linux-3.15-rc5/fs/yaffs2/yaffs_trace.h
  10182. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_trace.h 1970-01-01 01:00:00.000000000 +0100
  10183. +++ linux-3.15-rc5/fs/yaffs2/yaffs_trace.h 2014-05-17 01:53:27.000000000 +0200
  10184. @@ -0,0 +1,57 @@
  10185. +/*
  10186. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10187. + *
  10188. + * Copyright (C) 2002-2011 Aleph One Ltd.
  10189. + * for Toby Churchill Ltd and Brightstar Engineering
  10190. + *
  10191. + * Created by Charles Manning <charles@aleph1.co.uk>
  10192. + *
  10193. + * This program is free software; you can redistribute it and/or modify
  10194. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10195. + * published by the Free Software Foundation.
  10196. + *
  10197. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10198. + */
  10199. +
  10200. +#ifndef __YTRACE_H__
  10201. +#define __YTRACE_H__
  10202. +
  10203. +extern unsigned int yaffs_trace_mask;
  10204. +extern unsigned int yaffs_wr_attempts;
  10205. +
  10206. +/*
  10207. + * Tracing flags.
  10208. + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
  10209. + */
  10210. +
  10211. +#define YAFFS_TRACE_OS 0x00000002
  10212. +#define YAFFS_TRACE_ALLOCATE 0x00000004
  10213. +#define YAFFS_TRACE_SCAN 0x00000008
  10214. +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
  10215. +#define YAFFS_TRACE_ERASE 0x00000020
  10216. +#define YAFFS_TRACE_GC 0x00000040
  10217. +#define YAFFS_TRACE_WRITE 0x00000080
  10218. +#define YAFFS_TRACE_TRACING 0x00000100
  10219. +#define YAFFS_TRACE_DELETION 0x00000200
  10220. +#define YAFFS_TRACE_BUFFERS 0x00000400
  10221. +#define YAFFS_TRACE_NANDACCESS 0x00000800
  10222. +#define YAFFS_TRACE_GC_DETAIL 0x00001000
  10223. +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
  10224. +#define YAFFS_TRACE_MTD 0x00004000
  10225. +#define YAFFS_TRACE_CHECKPOINT 0x00008000
  10226. +
  10227. +#define YAFFS_TRACE_VERIFY 0x00010000
  10228. +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
  10229. +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
  10230. +#define YAFFS_TRACE_VERIFY_ALL 0x000f0000
  10231. +
  10232. +#define YAFFS_TRACE_SYNC 0x00100000
  10233. +#define YAFFS_TRACE_BACKGROUND 0x00200000
  10234. +#define YAFFS_TRACE_LOCK 0x00400000
  10235. +#define YAFFS_TRACE_MOUNT 0x00800000
  10236. +
  10237. +#define YAFFS_TRACE_ERROR 0x40000000
  10238. +#define YAFFS_TRACE_BUG 0x80000000
  10239. +#define YAFFS_TRACE_ALWAYS 0xf0000000
  10240. +
  10241. +#endif
  10242. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_verify.c linux-3.15-rc5/fs/yaffs2/yaffs_verify.c
  10243. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_verify.c 1970-01-01 01:00:00.000000000 +0100
  10244. +++ linux-3.15-rc5/fs/yaffs2/yaffs_verify.c 2014-05-17 01:53:27.000000000 +0200
  10245. @@ -0,0 +1,529 @@
  10246. +/*
  10247. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10248. + *
  10249. + * Copyright (C) 2002-2011 Aleph One Ltd.
  10250. + * for Toby Churchill Ltd and Brightstar Engineering
  10251. + *
  10252. + * Created by Charles Manning <charles@aleph1.co.uk>
  10253. + *
  10254. + * This program is free software; you can redistribute it and/or modify
  10255. + * it under the terms of the GNU General Public License version 2 as
  10256. + * published by the Free Software Foundation.
  10257. + */
  10258. +
  10259. +#include "yaffs_verify.h"
  10260. +#include "yaffs_trace.h"
  10261. +#include "yaffs_bitmap.h"
  10262. +#include "yaffs_getblockinfo.h"
  10263. +#include "yaffs_nand.h"
  10264. +
  10265. +int yaffs_skip_verification(struct yaffs_dev *dev)
  10266. +{
  10267. + (void) dev;
  10268. + return !(yaffs_trace_mask &
  10269. + (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  10270. +}
  10271. +
  10272. +static int yaffs_skip_full_verification(struct yaffs_dev *dev)
  10273. +{
  10274. + (void) dev;
  10275. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
  10276. +}
  10277. +
  10278. +static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
  10279. +{
  10280. + (void) dev;
  10281. + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
  10282. +}
  10283. +
  10284. +static const char * const block_state_name[] = {
  10285. + "Unknown",
  10286. + "Needs scan",
  10287. + "Scanning",
  10288. + "Empty",
  10289. + "Allocating",
  10290. + "Full",
  10291. + "Dirty",
  10292. + "Checkpoint",
  10293. + "Collecting",
  10294. + "Dead"
  10295. +};
  10296. +
  10297. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
  10298. +{
  10299. + int actually_used;
  10300. + int in_use;
  10301. +
  10302. + if (yaffs_skip_verification(dev))
  10303. + return;
  10304. +
  10305. + /* Report illegal runtime states */
  10306. + if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
  10307. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10308. + "Block %d has undefined state %d",
  10309. + n, bi->block_state);
  10310. +
  10311. + switch (bi->block_state) {
  10312. + case YAFFS_BLOCK_STATE_UNKNOWN:
  10313. + case YAFFS_BLOCK_STATE_SCANNING:
  10314. + case YAFFS_BLOCK_STATE_NEEDS_SCAN:
  10315. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10316. + "Block %d has bad run-state %s",
  10317. + n, block_state_name[bi->block_state]);
  10318. + }
  10319. +
  10320. + /* Check pages in use and soft deletions are legal */
  10321. +
  10322. + actually_used = bi->pages_in_use - bi->soft_del_pages;
  10323. +
  10324. + if (bi->pages_in_use < 0 ||
  10325. + bi->pages_in_use > dev->param.chunks_per_block ||
  10326. + bi->soft_del_pages < 0 ||
  10327. + bi->soft_del_pages > dev->param.chunks_per_block ||
  10328. + actually_used < 0 || actually_used > dev->param.chunks_per_block)
  10329. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10330. + "Block %d has illegal values pages_in_used %d soft_del_pages %d",
  10331. + n, bi->pages_in_use, bi->soft_del_pages);
  10332. +
  10333. + /* Check chunk bitmap legal */
  10334. + in_use = yaffs_count_chunk_bits(dev, n);
  10335. + if (in_use != bi->pages_in_use)
  10336. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10337. + "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
  10338. + n, bi->pages_in_use, in_use);
  10339. +}
  10340. +
  10341. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10342. + struct yaffs_block_info *bi, int n)
  10343. +{
  10344. + yaffs_verify_blk(dev, bi, n);
  10345. +
  10346. + /* After collection the block should be in the erased state */
  10347. +
  10348. + if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
  10349. + bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
  10350. + yaffs_trace(YAFFS_TRACE_ERROR,
  10351. + "Block %d is in state %d after gc, should be erased",
  10352. + n, bi->block_state);
  10353. + }
  10354. +}
  10355. +
  10356. +void yaffs_verify_blocks(struct yaffs_dev *dev)
  10357. +{
  10358. + int i;
  10359. + int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
  10360. + int illegal_states = 0;
  10361. +
  10362. + if (yaffs_skip_verification(dev))
  10363. + return;
  10364. +
  10365. + memset(state_count, 0, sizeof(state_count));
  10366. +
  10367. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  10368. + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  10369. + yaffs_verify_blk(dev, bi, i);
  10370. +
  10371. + if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
  10372. + state_count[bi->block_state]++;
  10373. + else
  10374. + illegal_states++;
  10375. + }
  10376. +
  10377. + yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
  10378. +
  10379. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10380. + "%d blocks have illegal states",
  10381. + illegal_states);
  10382. + if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  10383. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10384. + "Too many allocating blocks");
  10385. +
  10386. + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  10387. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10388. + "%s %d blocks",
  10389. + block_state_name[i], state_count[i]);
  10390. +
  10391. + if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
  10392. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10393. + "Checkpoint block count wrong dev %d count %d",
  10394. + dev->blocks_in_checkpt,
  10395. + state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
  10396. +
  10397. + if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
  10398. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10399. + "Erased block count wrong dev %d count %d",
  10400. + dev->n_erased_blocks,
  10401. + state_count[YAFFS_BLOCK_STATE_EMPTY]);
  10402. +
  10403. + if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  10404. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10405. + "Too many collecting blocks %d (max is 1)",
  10406. + state_count[YAFFS_BLOCK_STATE_COLLECTING]);
  10407. +}
  10408. +
  10409. +/*
  10410. + * Verify the object header. oh must be valid, but obj and tags may be NULL in
  10411. + * which case those tests will not be performed.
  10412. + */
  10413. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10414. + struct yaffs_ext_tags *tags, int parent_check)
  10415. +{
  10416. + if (obj && yaffs_skip_verification(obj->my_dev))
  10417. + return;
  10418. +
  10419. + if (!(tags && obj && oh)) {
  10420. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10421. + "Verifying object header tags %p obj %p oh %p",
  10422. + tags, obj, oh);
  10423. + return;
  10424. + }
  10425. +
  10426. + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  10427. + oh->type > YAFFS_OBJECT_TYPE_MAX)
  10428. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10429. + "Obj %d header type is illegal value 0x%x",
  10430. + tags->obj_id, oh->type);
  10431. +
  10432. + if (tags->obj_id != obj->obj_id)
  10433. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10434. + "Obj %d header mismatch obj_id %d",
  10435. + tags->obj_id, obj->obj_id);
  10436. +
  10437. + /*
  10438. + * Check that the object's parent ids match if parent_check requested.
  10439. + *
  10440. + * Tests do not apply to the root object.
  10441. + */
  10442. +
  10443. + if (parent_check && tags->obj_id > 1 && !obj->parent)
  10444. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10445. + "Obj %d header mismatch parent_id %d obj->parent is NULL",
  10446. + tags->obj_id, oh->parent_obj_id);
  10447. +
  10448. + if (parent_check && obj->parent &&
  10449. + oh->parent_obj_id != obj->parent->obj_id &&
  10450. + (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
  10451. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
  10452. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10453. + "Obj %d header mismatch parent_id %d parent_obj_id %d",
  10454. + tags->obj_id, oh->parent_obj_id,
  10455. + obj->parent->obj_id);
  10456. +
  10457. + if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
  10458. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10459. + "Obj %d header name is NULL",
  10460. + obj->obj_id);
  10461. +
  10462. + if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
  10463. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10464. + "Obj %d header name is 0xff",
  10465. + obj->obj_id);
  10466. +}
  10467. +
  10468. +void yaffs_verify_file(struct yaffs_obj *obj)
  10469. +{
  10470. + u32 x;
  10471. + int required_depth;
  10472. + int actual_depth;
  10473. + int last_chunk;
  10474. + u32 offset_in_chunk;
  10475. + u32 the_chunk;
  10476. +
  10477. + u32 i;
  10478. + struct yaffs_dev *dev;
  10479. + struct yaffs_ext_tags tags;
  10480. + struct yaffs_tnode *tn;
  10481. + u32 obj_id;
  10482. +
  10483. + if (!obj)
  10484. + return;
  10485. +
  10486. + if (yaffs_skip_verification(obj->my_dev))
  10487. + return;
  10488. +
  10489. + dev = obj->my_dev;
  10490. + obj_id = obj->obj_id;
  10491. +
  10492. +
  10493. + /* Check file size is consistent with tnode depth */
  10494. + yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
  10495. + &last_chunk, &offset_in_chunk);
  10496. + last_chunk++;
  10497. + x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
  10498. + required_depth = 0;
  10499. + while (x > 0) {
  10500. + x >>= YAFFS_TNODES_INTERNAL_BITS;
  10501. + required_depth++;
  10502. + }
  10503. +
  10504. + actual_depth = obj->variant.file_variant.top_level;
  10505. +
  10506. + /* Check that the chunks in the tnode tree are all correct.
  10507. + * We do this by scanning through the tnode tree and
  10508. + * checking the tags for every chunk match.
  10509. + */
  10510. +
  10511. + if (yaffs_skip_nand_verification(dev))
  10512. + return;
  10513. +
  10514. + for (i = 1; i <= last_chunk; i++) {
  10515. + tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
  10516. +
  10517. + if (!tn)
  10518. + continue;
  10519. +
  10520. + the_chunk = yaffs_get_group_base(dev, tn, i);
  10521. + if (the_chunk > 0) {
  10522. + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  10523. + &tags);
  10524. + if (tags.obj_id != obj_id || tags.chunk_id != i)
  10525. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10526. + "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
  10527. + obj_id, i, the_chunk,
  10528. + tags.obj_id, tags.chunk_id);
  10529. + }
  10530. + }
  10531. +}
  10532. +
  10533. +void yaffs_verify_link(struct yaffs_obj *obj)
  10534. +{
  10535. + if (obj && yaffs_skip_verification(obj->my_dev))
  10536. + return;
  10537. +
  10538. + /* Verify sane equivalent object */
  10539. +}
  10540. +
  10541. +void yaffs_verify_symlink(struct yaffs_obj *obj)
  10542. +{
  10543. + if (obj && yaffs_skip_verification(obj->my_dev))
  10544. + return;
  10545. +
  10546. + /* Verify symlink string */
  10547. +}
  10548. +
  10549. +void yaffs_verify_special(struct yaffs_obj *obj)
  10550. +{
  10551. + if (obj && yaffs_skip_verification(obj->my_dev))
  10552. + return;
  10553. +}
  10554. +
  10555. +void yaffs_verify_obj(struct yaffs_obj *obj)
  10556. +{
  10557. + struct yaffs_dev *dev;
  10558. + u32 chunk_min;
  10559. + u32 chunk_max;
  10560. + u32 chunk_id_ok;
  10561. + u32 chunk_in_range;
  10562. + u32 chunk_wrongly_deleted;
  10563. + u32 chunk_valid;
  10564. +
  10565. + if (!obj)
  10566. + return;
  10567. +
  10568. + if (obj->being_created)
  10569. + return;
  10570. +
  10571. + dev = obj->my_dev;
  10572. +
  10573. + if (yaffs_skip_verification(dev))
  10574. + return;
  10575. +
  10576. + /* Check sane object header chunk */
  10577. +
  10578. + chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
  10579. + chunk_max =
  10580. + (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
  10581. +
  10582. + chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
  10583. + ((unsigned)(obj->hdr_chunk)) <= chunk_max);
  10584. + chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
  10585. + chunk_valid = chunk_in_range &&
  10586. + yaffs_check_chunk_bit(dev,
  10587. + obj->hdr_chunk / dev->param.chunks_per_block,
  10588. + obj->hdr_chunk % dev->param.chunks_per_block);
  10589. + chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
  10590. +
  10591. + if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
  10592. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10593. + "Obj %d has chunk_id %d %s %s",
  10594. + obj->obj_id, obj->hdr_chunk,
  10595. + chunk_id_ok ? "" : ",out of range",
  10596. + chunk_wrongly_deleted ? ",marked as deleted" : "");
  10597. +
  10598. + if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
  10599. + struct yaffs_ext_tags tags;
  10600. + struct yaffs_obj_hdr *oh;
  10601. + u8 *buffer = yaffs_get_temp_buffer(dev);
  10602. +
  10603. + oh = (struct yaffs_obj_hdr *)buffer;
  10604. +
  10605. + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
  10606. +
  10607. + yaffs_verify_oh(obj, oh, &tags, 1);
  10608. +
  10609. + yaffs_release_temp_buffer(dev, buffer);
  10610. + }
  10611. +
  10612. + /* Verify it has a parent */
  10613. + if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
  10614. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10615. + "Obj %d has parent pointer %p which does not look like an object",
  10616. + obj->obj_id, obj->parent);
  10617. + }
  10618. +
  10619. + /* Verify parent is a directory */
  10620. + if (obj->parent &&
  10621. + obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10622. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10623. + "Obj %d's parent is not a directory (type %d)",
  10624. + obj->obj_id, obj->parent->variant_type);
  10625. + }
  10626. +
  10627. + switch (obj->variant_type) {
  10628. + case YAFFS_OBJECT_TYPE_FILE:
  10629. + yaffs_verify_file(obj);
  10630. + break;
  10631. + case YAFFS_OBJECT_TYPE_SYMLINK:
  10632. + yaffs_verify_symlink(obj);
  10633. + break;
  10634. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  10635. + yaffs_verify_dir(obj);
  10636. + break;
  10637. + case YAFFS_OBJECT_TYPE_HARDLINK:
  10638. + yaffs_verify_link(obj);
  10639. + break;
  10640. + case YAFFS_OBJECT_TYPE_SPECIAL:
  10641. + yaffs_verify_special(obj);
  10642. + break;
  10643. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  10644. + default:
  10645. + yaffs_trace(YAFFS_TRACE_VERIFY,
  10646. + "Obj %d has illegaltype %d",
  10647. + obj->obj_id, obj->variant_type);
  10648. + break;
  10649. + }
  10650. +}
  10651. +
  10652. +void yaffs_verify_objects(struct yaffs_dev *dev)
  10653. +{
  10654. + struct yaffs_obj *obj;
  10655. + int i;
  10656. + struct list_head *lh;
  10657. +
  10658. + if (yaffs_skip_verification(dev))
  10659. + return;
  10660. +
  10661. + /* Iterate through the objects in each hash entry */
  10662. +
  10663. + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  10664. + list_for_each(lh, &dev->obj_bucket[i].list) {
  10665. + obj = list_entry(lh, struct yaffs_obj, hash_link);
  10666. + yaffs_verify_obj(obj);
  10667. + }
  10668. + }
  10669. +}
  10670. +
  10671. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
  10672. +{
  10673. + struct list_head *lh;
  10674. + struct yaffs_obj *list_obj;
  10675. + int count = 0;
  10676. +
  10677. + if (!obj) {
  10678. + yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
  10679. + BUG();
  10680. + return;
  10681. + }
  10682. +
  10683. + if (yaffs_skip_verification(obj->my_dev))
  10684. + return;
  10685. +
  10686. + if (!obj->parent) {
  10687. + yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
  10688. + BUG();
  10689. + return;
  10690. + }
  10691. +
  10692. + if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10693. + yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
  10694. + BUG();
  10695. + }
  10696. +
  10697. + /* Iterate through the objects in each hash entry */
  10698. +
  10699. + list_for_each(lh, &obj->parent->variant.dir_variant.children) {
  10700. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10701. + yaffs_verify_obj(list_obj);
  10702. + if (obj == list_obj)
  10703. + count++;
  10704. + }
  10705. +
  10706. + if (count != 1) {
  10707. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  10708. + "Object in directory %d times",
  10709. + count);
  10710. + BUG();
  10711. + }
  10712. +}
  10713. +
  10714. +void yaffs_verify_dir(struct yaffs_obj *directory)
  10715. +{
  10716. + struct list_head *lh;
  10717. + struct yaffs_obj *list_obj;
  10718. +
  10719. + if (!directory) {
  10720. + BUG();
  10721. + return;
  10722. + }
  10723. +
  10724. + if (yaffs_skip_full_verification(directory->my_dev))
  10725. + return;
  10726. +
  10727. + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  10728. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  10729. + "Directory has wrong type: %d",
  10730. + directory->variant_type);
  10731. + BUG();
  10732. + }
  10733. +
  10734. + /* Iterate through the objects in each hash entry */
  10735. +
  10736. + list_for_each(lh, &directory->variant.dir_variant.children) {
  10737. + list_obj = list_entry(lh, struct yaffs_obj, siblings);
  10738. + if (list_obj->parent != directory) {
  10739. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  10740. + "Object in directory list has wrong parent %p",
  10741. + list_obj->parent);
  10742. + BUG();
  10743. + }
  10744. + yaffs_verify_obj_in_dir(list_obj);
  10745. + }
  10746. +}
  10747. +
  10748. +static int yaffs_free_verification_failures;
  10749. +
  10750. +void yaffs_verify_free_chunks(struct yaffs_dev *dev)
  10751. +{
  10752. + int counted;
  10753. + int difference;
  10754. +
  10755. + if (yaffs_skip_verification(dev))
  10756. + return;
  10757. +
  10758. + counted = yaffs_count_free_chunks(dev);
  10759. +
  10760. + difference = dev->n_free_chunks - counted;
  10761. +
  10762. + if (difference) {
  10763. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  10764. + "Freechunks verification failure %d %d %d",
  10765. + dev->n_free_chunks, counted, difference);
  10766. + yaffs_free_verification_failures++;
  10767. + }
  10768. +}
  10769. +
  10770. +int yaffs_verify_file_sane(struct yaffs_obj *in)
  10771. +{
  10772. + (void) in;
  10773. + return YAFFS_OK;
  10774. +}
  10775. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_verify.h linux-3.15-rc5/fs/yaffs2/yaffs_verify.h
  10776. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_verify.h 1970-01-01 01:00:00.000000000 +0100
  10777. +++ linux-3.15-rc5/fs/yaffs2/yaffs_verify.h 2014-05-17 01:53:27.000000000 +0200
  10778. @@ -0,0 +1,43 @@
  10779. +/*
  10780. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  10781. + *
  10782. + * Copyright (C) 2002-2011 Aleph One Ltd.
  10783. + * for Toby Churchill Ltd and Brightstar Engineering
  10784. + *
  10785. + * Created by Charles Manning <charles@aleph1.co.uk>
  10786. + *
  10787. + * This program is free software; you can redistribute it and/or modify
  10788. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  10789. + * published by the Free Software Foundation.
  10790. + *
  10791. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  10792. + */
  10793. +
  10794. +#ifndef __YAFFS_VERIFY_H__
  10795. +#define __YAFFS_VERIFY_H__
  10796. +
  10797. +#include "yaffs_guts.h"
  10798. +
  10799. +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi,
  10800. + int n);
  10801. +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  10802. + struct yaffs_block_info *bi, int n);
  10803. +void yaffs_verify_blocks(struct yaffs_dev *dev);
  10804. +
  10805. +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  10806. + struct yaffs_ext_tags *tags, int parent_check);
  10807. +void yaffs_verify_file(struct yaffs_obj *obj);
  10808. +void yaffs_verify_link(struct yaffs_obj *obj);
  10809. +void yaffs_verify_symlink(struct yaffs_obj *obj);
  10810. +void yaffs_verify_special(struct yaffs_obj *obj);
  10811. +void yaffs_verify_obj(struct yaffs_obj *obj);
  10812. +void yaffs_verify_objects(struct yaffs_dev *dev);
  10813. +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj);
  10814. +void yaffs_verify_dir(struct yaffs_obj *directory);
  10815. +void yaffs_verify_free_chunks(struct yaffs_dev *dev);
  10816. +
  10817. +int yaffs_verify_file_sane(struct yaffs_obj *obj);
  10818. +
  10819. +int yaffs_skip_verification(struct yaffs_dev *dev);
  10820. +
  10821. +#endif
  10822. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_vfs.c linux-3.15-rc5/fs/yaffs2/yaffs_vfs.c
  10823. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_vfs.c 1970-01-01 01:00:00.000000000 +0100
  10824. +++ linux-3.15-rc5/fs/yaffs2/yaffs_vfs.c 2014-05-17 02:52:54.000000000 +0200
  10825. @@ -0,0 +1,3604 @@
  10826. +/*
  10827. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  10828. + *
  10829. + * Copyright (C) 2002-2011 Aleph One Ltd.
  10830. + * for Toby Churchill Ltd and Brightstar Engineering
  10831. + *
  10832. + * Created by Charles Manning <charles@aleph1.co.uk>
  10833. + * Acknowledgements:
  10834. + * Luc van OostenRyck for numerous patches.
  10835. + * Nick Bane for numerous patches.
  10836. + * Nick Bane for 2.5/2.6 integration.
  10837. + * Andras Toth for mknod rdev issue.
  10838. + * Michael Fischer for finding the problem with inode inconsistency.
  10839. + * Some code bodily lifted from JFFS
  10840. + *
  10841. + * This program is free software; you can redistribute it and/or modify
  10842. + * it under the terms of the GNU General Public License version 2 as
  10843. + * published by the Free Software Foundation.
  10844. + */
  10845. +
  10846. +/*
  10847. + *
  10848. + * This is the file system front-end to YAFFS that hooks it up to
  10849. + * the VFS.
  10850. + *
  10851. + * Special notes:
  10852. + * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
  10853. + * this superblock
  10854. + * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
  10855. + * superblock
  10856. + * >> inode->u.generic_ip points to the associated struct yaffs_obj.
  10857. + */
  10858. +
  10859. +/*
  10860. + * There are two variants of the VFS glue code. This variant should compile
  10861. + * for any version of Linux.
  10862. + */
  10863. +#include <linux/version.h>
  10864. +
  10865. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
  10866. +#define YAFFS_COMPILE_BACKGROUND
  10867. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
  10868. +#define YAFFS_COMPILE_FREEZER
  10869. +#endif
  10870. +#endif
  10871. +
  10872. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
  10873. +#define YAFFS_COMPILE_EXPORTFS
  10874. +#endif
  10875. +
  10876. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
  10877. +#define YAFFS_USE_SETATTR_COPY
  10878. +#define YAFFS_USE_TRUNCATE_SETSIZE
  10879. +#endif
  10880. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
  10881. +#define YAFFS_HAS_EVICT_INODE
  10882. +#endif
  10883. +
  10884. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  10885. +#define YAFFS_NEW_FOLLOW_LINK 1
  10886. +#else
  10887. +#define YAFFS_NEW_FOLLOW_LINK 0
  10888. +#endif
  10889. +
  10890. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
  10891. +#define YAFFS_HAS_WRITE_SUPER
  10892. +#endif
  10893. +
  10894. +
  10895. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  10896. +#include <linux/config.h>
  10897. +#endif
  10898. +
  10899. +#include <linux/kernel.h>
  10900. +#include <linux/module.h>
  10901. +#include <linux/slab.h>
  10902. +#include <linux/init.h>
  10903. +#include <linux/fs.h>
  10904. +#include <linux/proc_fs.h>
  10905. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
  10906. +#include <linux/smp_lock.h>
  10907. +#endif
  10908. +#include <linux/pagemap.h>
  10909. +#include <linux/mtd/mtd.h>
  10910. +#include <linux/interrupt.h>
  10911. +#include <linux/string.h>
  10912. +#include <linux/ctype.h>
  10913. +
  10914. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  10915. +#include <linux/namei.h>
  10916. +#endif
  10917. +
  10918. +#ifdef YAFFS_COMPILE_EXPORTFS
  10919. +#include <linux/exportfs.h>
  10920. +#endif
  10921. +
  10922. +#ifdef YAFFS_COMPILE_BACKGROUND
  10923. +#include <linux/kthread.h>
  10924. +#include <linux/delay.h>
  10925. +#endif
  10926. +#ifdef YAFFS_COMPILE_FREEZER
  10927. +#include <linux/freezer.h>
  10928. +#endif
  10929. +
  10930. +#include <asm/div64.h>
  10931. +
  10932. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  10933. +
  10934. +#include <linux/statfs.h>
  10935. +
  10936. +#define UnlockPage(p) unlock_page(p)
  10937. +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
  10938. +
  10939. +/* FIXME: use sb->s_id instead ? */
  10940. +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
  10941. +
  10942. +#else
  10943. +
  10944. +#include <linux/locks.h>
  10945. +#define BDEVNAME_SIZE 0
  10946. +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
  10947. +
  10948. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
  10949. +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
  10950. +#define __user
  10951. +#endif
  10952. +
  10953. +#endif
  10954. +
  10955. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  10956. +#define YPROC_ROOT (&proc_root)
  10957. +#else
  10958. +#define YPROC_ROOT NULL
  10959. +#endif
  10960. +
  10961. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
  10962. +#define Y_INIT_TIMER(a) init_timer(a)
  10963. +#else
  10964. +#define Y_INIT_TIMER(a) init_timer_on_stack(a)
  10965. +#endif
  10966. +
  10967. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
  10968. +#define YAFFS_USE_WRITE_BEGIN_END 1
  10969. +#else
  10970. +#define YAFFS_USE_WRITE_BEGIN_END 0
  10971. +#endif
  10972. +
  10973. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
  10974. +#define YAFFS_SUPER_HAS_DIRTY
  10975. +#endif
  10976. +
  10977. +
  10978. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
  10979. +#define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
  10980. +#endif
  10981. +
  10982. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
  10983. +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
  10984. +{
  10985. + uint64_t result = partition_size;
  10986. + do_div(result, block_size);
  10987. + return (uint32_t) result;
  10988. +}
  10989. +#else
  10990. +#define YCALCBLOCKS(s, b) ((s)/(b))
  10991. +#endif
  10992. +
  10993. +#include <linux/uaccess.h>
  10994. +#include <linux/mtd/mtd.h>
  10995. +
  10996. +#include "yportenv.h"
  10997. +#include "yaffs_trace.h"
  10998. +#include "yaffs_guts.h"
  10999. +#include "yaffs_attribs.h"
  11000. +
  11001. +#include "yaffs_linux.h"
  11002. +
  11003. +#include "yaffs_mtdif.h"
  11004. +#include "yaffs_packedtags2.h"
  11005. +#include "yaffs_getblockinfo.h"
  11006. +
  11007. +unsigned int yaffs_trace_mask =
  11008. + YAFFS_TRACE_BAD_BLOCKS |
  11009. + YAFFS_TRACE_ALWAYS |
  11010. + 0;
  11011. +
  11012. +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
  11013. +unsigned int yaffs_auto_checkpoint = 1;
  11014. +unsigned int yaffs_gc_control = 1;
  11015. +unsigned int yaffs_bg_enable = 1;
  11016. +unsigned int yaffs_auto_select = 1;
  11017. +/* Module Parameters */
  11018. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11019. +module_param(yaffs_trace_mask, uint, 0644);
  11020. +module_param(yaffs_wr_attempts, uint, 0644);
  11021. +module_param(yaffs_auto_checkpoint, uint, 0644);
  11022. +module_param(yaffs_gc_control, uint, 0644);
  11023. +module_param(yaffs_bg_enable, uint, 0644);
  11024. +#else
  11025. +MODULE_PARM(yaffs_trace_mask, "i");
  11026. +MODULE_PARM(yaffs_wr_attempts, "i");
  11027. +MODULE_PARM(yaffs_auto_checkpoint, "i");
  11028. +MODULE_PARM(yaffs_gc_control, "i");
  11029. +#endif
  11030. +
  11031. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  11032. +/* use iget and read_inode */
  11033. +#define Y_IGET(sb, inum) iget((sb), (inum))
  11034. +
  11035. +#else
  11036. +/* Call local equivalent */
  11037. +#define YAFFS_USE_OWN_IGET
  11038. +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
  11039. +
  11040. +#endif
  11041. +
  11042. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11043. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
  11044. +#else
  11045. +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
  11046. +#endif
  11047. +
  11048. +#define yaffs_inode_to_obj(iptr) \
  11049. + ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
  11050. +#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
  11051. +
  11052. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11053. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
  11054. +#else
  11055. +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
  11056. +#endif
  11057. +
  11058. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
  11059. +#define Y_CLEAR_INODE(i) clear_inode(i)
  11060. +#else
  11061. +#define Y_CLEAR_INODE(i) end_writeback(i)
  11062. +#endif
  11063. +
  11064. +
  11065. +#define update_dir_time(dir) do {\
  11066. + (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
  11067. + } while (0)
  11068. +
  11069. +static void yaffs_fill_inode_from_obj(struct inode *inode,
  11070. + struct yaffs_obj *obj);
  11071. +
  11072. +
  11073. +static void yaffs_gross_lock(struct yaffs_dev *dev)
  11074. +{
  11075. + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
  11076. + mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11077. + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
  11078. +}
  11079. +
  11080. +static void yaffs_gross_unlock(struct yaffs_dev *dev)
  11081. +{
  11082. + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
  11083. + mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
  11084. +}
  11085. +
  11086. +
  11087. +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
  11088. +{
  11089. + /* Lifted from jffs2 */
  11090. +
  11091. + struct yaffs_obj *obj;
  11092. + unsigned char *pg_buf;
  11093. + int ret;
  11094. + loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
  11095. + struct yaffs_dev *dev;
  11096. +
  11097. + yaffs_trace(YAFFS_TRACE_OS,
  11098. + "yaffs_readpage_nolock at %lld, size %08x",
  11099. + (long long)pos,
  11100. + (unsigned)PAGE_CACHE_SIZE);
  11101. +
  11102. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11103. +
  11104. + dev = obj->my_dev;
  11105. +
  11106. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11107. + BUG_ON(!PageLocked(pg));
  11108. +#else
  11109. + if (!PageLocked(pg))
  11110. + PAGE_BUG(pg);
  11111. +#endif
  11112. +
  11113. + pg_buf = kmap(pg);
  11114. + /* FIXME: Can kmap fail? */
  11115. +
  11116. + yaffs_gross_lock(dev);
  11117. +
  11118. + ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
  11119. +
  11120. + yaffs_gross_unlock(dev);
  11121. +
  11122. + if (ret >= 0)
  11123. + ret = 0;
  11124. +
  11125. + if (ret) {
  11126. + ClearPageUptodate(pg);
  11127. + SetPageError(pg);
  11128. + } else {
  11129. + SetPageUptodate(pg);
  11130. + ClearPageError(pg);
  11131. + }
  11132. +
  11133. + flush_dcache_page(pg);
  11134. + kunmap(pg);
  11135. +
  11136. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
  11137. + return ret;
  11138. +}
  11139. +
  11140. +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
  11141. +{
  11142. + int ret = yaffs_readpage_nolock(f, pg);
  11143. + UnlockPage(pg);
  11144. + return ret;
  11145. +}
  11146. +
  11147. +static int yaffs_readpage(struct file *f, struct page *pg)
  11148. +{
  11149. + int ret;
  11150. +
  11151. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
  11152. + ret = yaffs_readpage_unlock(f, pg);
  11153. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
  11154. + return ret;
  11155. +}
  11156. +
  11157. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
  11158. +#define YCRED_FSUID() from_kuid(&init_user_ns, current_fsuid())
  11159. +#define YCRED_FSGID() from_kgid(&init_user_ns, current_fsgid())
  11160. +#else
  11161. +#define YCRED_FSUID() YCRED(current)->fsuid
  11162. +#define YCRED_FSGID() YCRED(current)->fsgid
  11163. +
  11164. +static inline uid_t i_uid_read(const struct inode *inode)
  11165. +{
  11166. + return inode->i_uid;
  11167. +}
  11168. +
  11169. +static inline gid_t i_gid_read(const struct inode *inode)
  11170. +{
  11171. + return inode->i_gid;
  11172. +}
  11173. +
  11174. +static inline void i_uid_write(struct inode *inode, uid_t uid)
  11175. +{
  11176. + inode->i_uid = uid;
  11177. +}
  11178. +
  11179. +static inline void i_gid_write(struct inode *inode, gid_t gid)
  11180. +{
  11181. + inode->i_gid = gid;
  11182. +}
  11183. +#endif
  11184. +
  11185. +static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
  11186. +{
  11187. + struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
  11188. +
  11189. + if (lc)
  11190. + lc->dirty = val;
  11191. +
  11192. +# ifdef YAFFS_SUPER_HAS_DIRTY
  11193. + {
  11194. + struct super_block *sb = lc->super;
  11195. +
  11196. + if (sb)
  11197. + sb->s_dirt = val;
  11198. + }
  11199. +#endif
  11200. +
  11201. +}
  11202. +
  11203. +static void yaffs_set_super_dirty(struct yaffs_dev *dev)
  11204. +{
  11205. + yaffs_set_super_dirty_val(dev, 1);
  11206. +}
  11207. +
  11208. +static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
  11209. +{
  11210. + yaffs_set_super_dirty_val(dev, 0);
  11211. +}
  11212. +
  11213. +static int yaffs_check_super_dirty(struct yaffs_dev *dev)
  11214. +{
  11215. + struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
  11216. +
  11217. + if (lc && lc->dirty)
  11218. + return 1;
  11219. +
  11220. +# ifdef YAFFS_SUPER_HAS_DIRTY
  11221. + {
  11222. + struct super_block *sb = lc->super;
  11223. +
  11224. + if (sb && sb->s_dirt)
  11225. + return 1;
  11226. + }
  11227. +#endif
  11228. + return 0;
  11229. +
  11230. +}
  11231. +
  11232. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11233. +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
  11234. +#else
  11235. +static int yaffs_writepage(struct page *page)
  11236. +#endif
  11237. +{
  11238. + struct yaffs_dev *dev;
  11239. + struct address_space *mapping = page->mapping;
  11240. + struct inode *inode;
  11241. + unsigned long end_index;
  11242. + char *buffer;
  11243. + struct yaffs_obj *obj;
  11244. + int n_written = 0;
  11245. + unsigned n_bytes;
  11246. + loff_t i_size;
  11247. +
  11248. + if (!mapping)
  11249. + BUG();
  11250. + inode = mapping->host;
  11251. + if (!inode)
  11252. + BUG();
  11253. + i_size = i_size_read(inode);
  11254. +
  11255. + end_index = i_size >> PAGE_CACHE_SHIFT;
  11256. +
  11257. + if (page->index < end_index)
  11258. + n_bytes = PAGE_CACHE_SIZE;
  11259. + else {
  11260. + n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
  11261. +
  11262. + if (page->index > end_index || !n_bytes) {
  11263. + yaffs_trace(YAFFS_TRACE_OS,
  11264. + "yaffs_writepage at %lld, inode size = %lld!!",
  11265. + ((loff_t)page->index) << PAGE_CACHE_SHIFT,
  11266. + inode->i_size);
  11267. + yaffs_trace(YAFFS_TRACE_OS,
  11268. + " -> don't care!!");
  11269. +
  11270. + zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  11271. + set_page_writeback(page);
  11272. + unlock_page(page);
  11273. + end_page_writeback(page);
  11274. + return 0;
  11275. + }
  11276. + }
  11277. +
  11278. + if (n_bytes != PAGE_CACHE_SIZE)
  11279. + zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
  11280. +
  11281. + get_page(page);
  11282. +
  11283. + buffer = kmap(page);
  11284. +
  11285. + obj = yaffs_inode_to_obj(inode);
  11286. + dev = obj->my_dev;
  11287. + yaffs_gross_lock(dev);
  11288. +
  11289. + yaffs_trace(YAFFS_TRACE_OS,
  11290. + "yaffs_writepage at %lld, size %08x",
  11291. + ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
  11292. + yaffs_trace(YAFFS_TRACE_OS,
  11293. + "writepag0: obj = %lld, ino = %lld",
  11294. + obj->variant.file_variant.file_size, inode->i_size);
  11295. +
  11296. + n_written = yaffs_wr_file(obj, buffer,
  11297. + ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
  11298. +
  11299. + yaffs_set_super_dirty(dev);
  11300. +
  11301. + yaffs_trace(YAFFS_TRACE_OS,
  11302. + "writepag1: obj = %lld, ino = %lld",
  11303. + obj->variant.file_variant.file_size, inode->i_size);
  11304. +
  11305. + yaffs_gross_unlock(dev);
  11306. +
  11307. + kunmap(page);
  11308. + set_page_writeback(page);
  11309. + unlock_page(page);
  11310. + end_page_writeback(page);
  11311. + put_page(page);
  11312. +
  11313. + return (n_written == n_bytes) ? 0 : -ENOSPC;
  11314. +}
  11315. +
  11316. +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
  11317. +/* For now we just assume few parallel writes and check against a small number. */
  11318. +/* Todo: need to do this with a counter to handle parallel reads better */
  11319. +
  11320. +static ssize_t yaffs_hold_space(struct file *f)
  11321. +{
  11322. + struct yaffs_obj *obj;
  11323. + struct yaffs_dev *dev;
  11324. +
  11325. + int n_free_chunks;
  11326. +
  11327. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11328. +
  11329. + dev = obj->my_dev;
  11330. +
  11331. + yaffs_gross_lock(dev);
  11332. +
  11333. + n_free_chunks = yaffs_get_n_free_chunks(dev);
  11334. +
  11335. + yaffs_gross_unlock(dev);
  11336. +
  11337. + return (n_free_chunks > 20) ? 1 : 0;
  11338. +}
  11339. +
  11340. +static void yaffs_release_space(struct file *f)
  11341. +{
  11342. + struct yaffs_obj *obj;
  11343. + struct yaffs_dev *dev;
  11344. +
  11345. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11346. +
  11347. + dev = obj->my_dev;
  11348. +
  11349. + yaffs_gross_lock(dev);
  11350. +
  11351. + yaffs_gross_unlock(dev);
  11352. +}
  11353. +
  11354. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  11355. +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
  11356. + loff_t pos, unsigned len, unsigned flags,
  11357. + struct page **pagep, void **fsdata)
  11358. +{
  11359. + struct page *pg = NULL;
  11360. + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  11361. +
  11362. + int ret = 0;
  11363. + int space_held = 0;
  11364. +
  11365. + /* Get a page */
  11366. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
  11367. + pg = grab_cache_page_write_begin(mapping, index, flags);
  11368. +#else
  11369. + pg = __grab_cache_page(mapping, index);
  11370. +#endif
  11371. +
  11372. + *pagep = pg;
  11373. + if (!pg) {
  11374. + ret = -ENOMEM;
  11375. + goto out;
  11376. + }
  11377. + yaffs_trace(YAFFS_TRACE_OS,
  11378. + "start yaffs_write_begin index %d(%x) uptodate %d",
  11379. + (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
  11380. +
  11381. + /* Get fs space */
  11382. + space_held = yaffs_hold_space(filp);
  11383. +
  11384. + if (!space_held) {
  11385. + ret = -ENOSPC;
  11386. + goto out;
  11387. + }
  11388. +
  11389. + /* Update page if required */
  11390. +
  11391. + if (!Page_Uptodate(pg))
  11392. + ret = yaffs_readpage_nolock(filp, pg);
  11393. +
  11394. + if (ret)
  11395. + goto out;
  11396. +
  11397. + /* Happy path return */
  11398. + yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
  11399. +
  11400. + return 0;
  11401. +
  11402. +out:
  11403. + yaffs_trace(YAFFS_TRACE_OS,
  11404. + "end yaffs_write_begin fail returning %d", ret);
  11405. + if (space_held)
  11406. + yaffs_release_space(filp);
  11407. + if (pg) {
  11408. + unlock_page(pg);
  11409. + page_cache_release(pg);
  11410. + }
  11411. + return ret;
  11412. +}
  11413. +
  11414. +#else
  11415. +
  11416. +static int yaffs_prepare_write(struct file *f, struct page *pg,
  11417. + unsigned offset, unsigned to)
  11418. +{
  11419. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
  11420. +
  11421. + if (!Page_Uptodate(pg))
  11422. + return yaffs_readpage_nolock(f, pg);
  11423. + return 0;
  11424. +}
  11425. +#endif
  11426. +
  11427. +
  11428. +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
  11429. + loff_t * pos)
  11430. +{
  11431. + struct yaffs_obj *obj;
  11432. + int n_written;
  11433. + loff_t ipos;
  11434. + struct inode *inode;
  11435. + struct yaffs_dev *dev;
  11436. +
  11437. + obj = yaffs_dentry_to_obj(f->f_dentry);
  11438. +
  11439. + if (!obj) {
  11440. + yaffs_trace(YAFFS_TRACE_OS,
  11441. + "yaffs_file_write: hey obj is null!");
  11442. + return -EINVAL;
  11443. + }
  11444. +
  11445. + dev = obj->my_dev;
  11446. +
  11447. + yaffs_gross_lock(dev);
  11448. +
  11449. + inode = f->f_dentry->d_inode;
  11450. +
  11451. + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
  11452. + ipos = inode->i_size;
  11453. + else
  11454. + ipos = *pos;
  11455. +
  11456. + yaffs_trace(YAFFS_TRACE_OS,
  11457. + "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
  11458. + (unsigned)n, (unsigned)n, obj->obj_id, ipos);
  11459. +
  11460. + n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
  11461. +
  11462. + yaffs_set_super_dirty(dev);
  11463. +
  11464. + yaffs_trace(YAFFS_TRACE_OS,
  11465. + "yaffs_file_write: %d(%x) bytes written",
  11466. + (unsigned)n, (unsigned)n);
  11467. +
  11468. + if (n_written > 0) {
  11469. + ipos += n_written;
  11470. + *pos = ipos;
  11471. + if (ipos > inode->i_size) {
  11472. + inode->i_size = ipos;
  11473. + inode->i_blocks = (ipos + 511) >> 9;
  11474. +
  11475. + yaffs_trace(YAFFS_TRACE_OS,
  11476. + "yaffs_file_write size updated to %lld bytes, %d blocks",
  11477. + ipos, (int)(inode->i_blocks));
  11478. + }
  11479. +
  11480. + }
  11481. + yaffs_gross_unlock(dev);
  11482. + return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
  11483. +}
  11484. +
  11485. +
  11486. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  11487. +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
  11488. + loff_t pos, unsigned len, unsigned copied,
  11489. + struct page *pg, void *fsdadata)
  11490. +{
  11491. + int ret = 0;
  11492. + void *addr, *kva;
  11493. + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
  11494. +
  11495. + kva = kmap(pg);
  11496. + addr = kva + offset_into_page;
  11497. +
  11498. + yaffs_trace(YAFFS_TRACE_OS,
  11499. + "yaffs_write_end addr %p pos %lld n_bytes %d",
  11500. + addr, pos, copied);
  11501. +
  11502. + ret = yaffs_file_write(filp, addr, copied, &pos);
  11503. +
  11504. + if (ret != copied) {
  11505. + yaffs_trace(YAFFS_TRACE_OS,
  11506. + "yaffs_write_end not same size ret %d copied %d",
  11507. + ret, copied);
  11508. + SetPageError(pg);
  11509. + }
  11510. +
  11511. + kunmap(pg);
  11512. +
  11513. + yaffs_release_space(filp);
  11514. + unlock_page(pg);
  11515. + page_cache_release(pg);
  11516. + return ret;
  11517. +}
  11518. +#else
  11519. +
  11520. +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
  11521. + unsigned to)
  11522. +{
  11523. + void *addr, *kva;
  11524. +
  11525. + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
  11526. + int n_bytes = to - offset;
  11527. + int n_written;
  11528. +
  11529. + kva = kmap(pg);
  11530. + addr = kva + offset;
  11531. +
  11532. + yaffs_trace(YAFFS_TRACE_OS,
  11533. + "yaffs_commit_write addr %p pos %lld n_bytes %d",
  11534. + addr, pos, n_bytes);
  11535. +
  11536. + n_written = yaffs_file_write(f, addr, n_bytes, &pos);
  11537. +
  11538. + if (n_written != n_bytes) {
  11539. + yaffs_trace(YAFFS_TRACE_OS,
  11540. + "yaffs_commit_write not same size n_written %d n_bytes %d",
  11541. + n_written, n_bytes);
  11542. + SetPageError(pg);
  11543. + }
  11544. + kunmap(pg);
  11545. +
  11546. + yaffs_trace(YAFFS_TRACE_OS,
  11547. + "yaffs_commit_write returning %d",
  11548. + n_written == n_bytes ? 0 : n_written);
  11549. +
  11550. + return n_written == n_bytes ? 0 : n_written;
  11551. +}
  11552. +#endif
  11553. +
  11554. +static struct address_space_operations yaffs_file_address_operations = {
  11555. + .readpage = yaffs_readpage,
  11556. + .writepage = yaffs_writepage,
  11557. +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
  11558. + .write_begin = yaffs_write_begin,
  11559. + .write_end = yaffs_write_end,
  11560. +#else
  11561. + .prepare_write = yaffs_prepare_write,
  11562. + .commit_write = yaffs_commit_write,
  11563. +#endif
  11564. +};
  11565. +
  11566. +
  11567. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  11568. +static int yaffs_file_flush(struct file *file, fl_owner_t id)
  11569. +#else
  11570. +static int yaffs_file_flush(struct file *file)
  11571. +#endif
  11572. +{
  11573. + struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
  11574. +
  11575. + struct yaffs_dev *dev = obj->my_dev;
  11576. +
  11577. + yaffs_trace(YAFFS_TRACE_OS,
  11578. + "yaffs_file_flush object %d (%s)",
  11579. + obj->obj_id,
  11580. + obj->dirty ? "dirty" : "clean");
  11581. +
  11582. + yaffs_gross_lock(dev);
  11583. +
  11584. + yaffs_flush_file(obj, 1, 0);
  11585. +
  11586. + yaffs_gross_unlock(dev);
  11587. +
  11588. + return 0;
  11589. +}
  11590. +
  11591. +
  11592. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  11593. +static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
  11594. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  11595. +static int yaffs_sync_object(struct file *file, int datasync)
  11596. +#else
  11597. +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
  11598. + int datasync)
  11599. +#endif
  11600. +{
  11601. + struct yaffs_obj *obj;
  11602. + struct yaffs_dev *dev;
  11603. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
  11604. + struct dentry *dentry = file->f_path.dentry;
  11605. +#endif
  11606. +
  11607. + obj = yaffs_dentry_to_obj(dentry);
  11608. +
  11609. + dev = obj->my_dev;
  11610. +
  11611. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  11612. + "yaffs_sync_object");
  11613. + yaffs_gross_lock(dev);
  11614. + yaffs_flush_file(obj, 1, datasync);
  11615. + yaffs_gross_unlock(dev);
  11616. + return 0;
  11617. +}
  11618. +
  11619. +
  11620. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
  11621. +static const struct file_operations yaffs_file_operations = {
  11622. + .read = do_sync_read,
  11623. + .write = do_sync_write,
  11624. + .aio_read = generic_file_aio_read,
  11625. + .aio_write = generic_file_aio_write,
  11626. + .mmap = generic_file_mmap,
  11627. + .flush = yaffs_file_flush,
  11628. + .fsync = yaffs_sync_object,
  11629. + .splice_read = generic_file_splice_read,
  11630. + .splice_write = generic_file_splice_write,
  11631. + .llseek = generic_file_llseek,
  11632. +};
  11633. +
  11634. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
  11635. +
  11636. +static const struct file_operations yaffs_file_operations = {
  11637. + .read = do_sync_read,
  11638. + .write = do_sync_write,
  11639. + .aio_read = generic_file_aio_read,
  11640. + .aio_write = generic_file_aio_write,
  11641. + .mmap = generic_file_mmap,
  11642. + .flush = yaffs_file_flush,
  11643. + .fsync = yaffs_sync_object,
  11644. + .sendfile = generic_file_sendfile,
  11645. +};
  11646. +
  11647. +#else
  11648. +
  11649. +static const struct file_operations yaffs_file_operations = {
  11650. + .read = generic_file_read,
  11651. + .write = generic_file_write,
  11652. + .mmap = generic_file_mmap,
  11653. + .flush = yaffs_file_flush,
  11654. + .fsync = yaffs_sync_object,
  11655. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  11656. + .sendfile = generic_file_sendfile,
  11657. +#endif
  11658. +};
  11659. +#endif
  11660. +
  11661. +
  11662. +
  11663. +
  11664. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
  11665. +static void zero_user_segment(struct page *page, unsigned start, unsigned end)
  11666. +{
  11667. + void *kaddr = kmap_atomic(page, KM_USER0);
  11668. + memset(kaddr + start, 0, end - start);
  11669. + kunmap_atomic(kaddr, KM_USER0);
  11670. + flush_dcache_page(page);
  11671. +}
  11672. +#endif
  11673. +
  11674. +
  11675. +static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
  11676. +{
  11677. +#ifdef YAFFS_USE_TRUNCATE_SETSIZE
  11678. + truncate_setsize(inode, newsize);
  11679. + return 0;
  11680. +#else
  11681. + truncate_inode_pages(&inode->i_data, newsize);
  11682. + return 0;
  11683. +#endif
  11684. +
  11685. +}
  11686. +
  11687. +
  11688. +static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
  11689. +{
  11690. +#ifdef YAFFS_USE_SETATTR_COPY
  11691. + setattr_copy(inode, attr);
  11692. + return 0;
  11693. +#else
  11694. + return inode_setattr(inode, attr);
  11695. +#endif
  11696. +
  11697. +}
  11698. +
  11699. +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
  11700. +{
  11701. + struct inode *inode = dentry->d_inode;
  11702. + int error = 0;
  11703. + struct yaffs_dev *dev;
  11704. +
  11705. + yaffs_trace(YAFFS_TRACE_OS,
  11706. + "yaffs_setattr of object %d",
  11707. + yaffs_inode_to_obj(inode)->obj_id);
  11708. +#if 0
  11709. + /* Fail if a requested resize >= 2GB */
  11710. + if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
  11711. + error = -EINVAL;
  11712. +#endif
  11713. +
  11714. + if (error == 0)
  11715. + error = inode_change_ok(inode, attr);
  11716. + if (error == 0) {
  11717. + int result;
  11718. + if (!error) {
  11719. + error = yaffs_vfs_setattr(inode, attr);
  11720. + yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
  11721. + if (attr->ia_valid & ATTR_SIZE) {
  11722. + yaffs_vfs_setsize(inode, attr->ia_size);
  11723. + inode->i_blocks = (inode->i_size + 511) >> 9;
  11724. + }
  11725. + }
  11726. + dev = yaffs_inode_to_obj(inode)->my_dev;
  11727. + if (attr->ia_valid & ATTR_SIZE) {
  11728. + yaffs_trace(YAFFS_TRACE_OS,
  11729. + "resize to %d(%x)",
  11730. + (int)(attr->ia_size),
  11731. + (int)(attr->ia_size));
  11732. + }
  11733. + yaffs_gross_lock(dev);
  11734. + result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
  11735. + if (result == YAFFS_OK) {
  11736. + error = 0;
  11737. + } else {
  11738. + error = -EPERM;
  11739. + }
  11740. + yaffs_gross_unlock(dev);
  11741. +
  11742. + }
  11743. +
  11744. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
  11745. +
  11746. + return error;
  11747. +}
  11748. +
  11749. +static int yaffs_setxattr(struct dentry *dentry, const char *name,
  11750. + const void *value, size_t size, int flags)
  11751. +{
  11752. + struct inode *inode = dentry->d_inode;
  11753. + int error = 0;
  11754. + struct yaffs_dev *dev;
  11755. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11756. +
  11757. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
  11758. +
  11759. + if (error == 0) {
  11760. + int result;
  11761. + dev = obj->my_dev;
  11762. + yaffs_gross_lock(dev);
  11763. + result = yaffs_set_xattrib(obj, name, value, size, flags);
  11764. + if (result == YAFFS_OK)
  11765. + error = 0;
  11766. + else if (result < 0)
  11767. + error = result;
  11768. + yaffs_gross_unlock(dev);
  11769. +
  11770. + }
  11771. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
  11772. +
  11773. + return error;
  11774. +}
  11775. +
  11776. +static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
  11777. + void *buff, size_t size)
  11778. +{
  11779. + struct inode *inode = dentry->d_inode;
  11780. + int error = 0;
  11781. + struct yaffs_dev *dev;
  11782. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11783. +
  11784. + yaffs_trace(YAFFS_TRACE_OS,
  11785. + "yaffs_getxattr \"%s\" from object %d",
  11786. + name, obj->obj_id);
  11787. +
  11788. + if (error == 0) {
  11789. + dev = obj->my_dev;
  11790. + yaffs_gross_lock(dev);
  11791. + error = yaffs_get_xattrib(obj, name, buff, size);
  11792. + yaffs_gross_unlock(dev);
  11793. +
  11794. + }
  11795. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
  11796. +
  11797. + return error;
  11798. +}
  11799. +
  11800. +static int yaffs_removexattr(struct dentry *dentry, const char *name)
  11801. +{
  11802. + struct inode *inode = dentry->d_inode;
  11803. + int error = 0;
  11804. + struct yaffs_dev *dev;
  11805. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11806. +
  11807. + yaffs_trace(YAFFS_TRACE_OS,
  11808. + "yaffs_removexattr of object %d", obj->obj_id);
  11809. +
  11810. + if (error == 0) {
  11811. + int result;
  11812. + dev = obj->my_dev;
  11813. + yaffs_gross_lock(dev);
  11814. + result = yaffs_remove_xattrib(obj, name);
  11815. + if (result == YAFFS_OK)
  11816. + error = 0;
  11817. + else if (result < 0)
  11818. + error = result;
  11819. + yaffs_gross_unlock(dev);
  11820. +
  11821. + }
  11822. + yaffs_trace(YAFFS_TRACE_OS,
  11823. + "yaffs_removexattr done returning %d", error);
  11824. +
  11825. + return error;
  11826. +}
  11827. +
  11828. +static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
  11829. +{
  11830. + struct inode *inode = dentry->d_inode;
  11831. + int error = 0;
  11832. + struct yaffs_dev *dev;
  11833. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  11834. +
  11835. + yaffs_trace(YAFFS_TRACE_OS,
  11836. + "yaffs_listxattr of object %d", obj->obj_id);
  11837. +
  11838. + if (error == 0) {
  11839. + dev = obj->my_dev;
  11840. + yaffs_gross_lock(dev);
  11841. + error = yaffs_list_xattrib(obj, buff, size);
  11842. + yaffs_gross_unlock(dev);
  11843. +
  11844. + }
  11845. + yaffs_trace(YAFFS_TRACE_OS,
  11846. + "yaffs_listxattr done returning %d", error);
  11847. +
  11848. + return error;
  11849. +}
  11850. +
  11851. +
  11852. +static const struct inode_operations yaffs_file_inode_operations = {
  11853. + .setattr = yaffs_setattr,
  11854. + .setxattr = yaffs_setxattr,
  11855. + .getxattr = yaffs_getxattr,
  11856. + .listxattr = yaffs_listxattr,
  11857. + .removexattr = yaffs_removexattr,
  11858. +};
  11859. +
  11860. +
  11861. +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
  11862. + int buflen)
  11863. +{
  11864. + unsigned char *alias;
  11865. + int ret;
  11866. +
  11867. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11868. +
  11869. + yaffs_gross_lock(dev);
  11870. +
  11871. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11872. +
  11873. + yaffs_gross_unlock(dev);
  11874. +
  11875. + if (!alias)
  11876. + return -ENOMEM;
  11877. +
  11878. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0))
  11879. + ret = readlink_copy(buffer, buflen, alias);
  11880. +#else
  11881. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  11882. +#endif
  11883. + kfree(alias);
  11884. + return ret;
  11885. +}
  11886. +
  11887. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11888. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11889. +{
  11890. + void *ret;
  11891. +#else
  11892. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11893. +{
  11894. + int ret
  11895. +#endif
  11896. + unsigned char *alias;
  11897. + int ret_int = 0;
  11898. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11899. +
  11900. + yaffs_gross_lock(dev);
  11901. +
  11902. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11903. + yaffs_gross_unlock(dev);
  11904. +
  11905. + if (!alias) {
  11906. + ret_int = -ENOMEM;
  11907. + goto out;
  11908. + }
  11909. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11910. + nd_set_link(nd, alias);
  11911. + ret = alias;
  11912. +out:
  11913. + if (ret_int)
  11914. + ret = ERR_PTR(ret_int);
  11915. + return ret;
  11916. +#else
  11917. + ret = vfs_follow_link(nd, alias);
  11918. + kfree(alias);
  11919. +out:
  11920. + if (ret_int)
  11921. + ret = ret_int;
  11922. + return ret;
  11923. +#endif
  11924. +}
  11925. +
  11926. +
  11927. +#ifdef YAFFS_HAS_PUT_INODE
  11928. +
  11929. +/* For now put inode is just for debugging
  11930. + * Put inode is called when the inode **structure** is put.
  11931. + */
  11932. +static void yaffs_put_inode(struct inode *inode)
  11933. +{
  11934. + yaffs_trace(YAFFS_TRACE_OS,
  11935. + "yaffs_put_inode: ino %d, count %d"),
  11936. + (int)inode->i_ino, atomic_read(&inode->i_count);
  11937. +
  11938. +}
  11939. +#endif
  11940. +
  11941. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11942. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
  11943. +{
  11944. + kfree(alias);
  11945. +}
  11946. +#endif
  11947. +
  11948. +static const struct inode_operations yaffs_symlink_inode_operations = {
  11949. + .readlink = yaffs_readlink,
  11950. + .follow_link = yaffs_follow_link,
  11951. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11952. + .put_link = yaffs_put_link,
  11953. +#endif
  11954. + .setattr = yaffs_setattr,
  11955. + .setxattr = yaffs_setxattr,
  11956. + .getxattr = yaffs_getxattr,
  11957. + .listxattr = yaffs_listxattr,
  11958. + .removexattr = yaffs_removexattr,
  11959. +};
  11960. +
  11961. +#ifdef YAFFS_USE_OWN_IGET
  11962. +
  11963. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  11964. +{
  11965. + struct inode *inode;
  11966. + struct yaffs_obj *obj;
  11967. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  11968. +
  11969. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
  11970. +
  11971. + inode = iget_locked(sb, ino);
  11972. + if (!inode)
  11973. + return ERR_PTR(-ENOMEM);
  11974. + if (!(inode->i_state & I_NEW))
  11975. + return inode;
  11976. +
  11977. + /* NB This is called as a side effect of other functions, but
  11978. + * we had to release the lock to prevent deadlocks, so
  11979. + * need to lock again.
  11980. + */
  11981. +
  11982. + yaffs_gross_lock(dev);
  11983. +
  11984. + obj = yaffs_find_by_number(dev, inode->i_ino);
  11985. +
  11986. + yaffs_fill_inode_from_obj(inode, obj);
  11987. +
  11988. + yaffs_gross_unlock(dev);
  11989. +
  11990. + unlock_new_inode(inode);
  11991. + return inode;
  11992. +}
  11993. +
  11994. +#else
  11995. +
  11996. +static void yaffs_read_inode(struct inode *inode)
  11997. +{
  11998. + /* NB This is called as a side effect of other functions, but
  11999. + * we had to release the lock to prevent deadlocks, so
  12000. + * need to lock again.
  12001. + */
  12002. +
  12003. + struct yaffs_obj *obj;
  12004. + struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
  12005. +
  12006. + yaffs_trace(YAFFS_TRACE_OS,
  12007. + "yaffs_read_inode for %d", (int)inode->i_ino);
  12008. +
  12009. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12010. + yaffs_gross_lock(dev);
  12011. +
  12012. + obj = yaffs_find_by_number(dev, inode->i_ino);
  12013. +
  12014. + yaffs_fill_inode_from_obj(inode, obj);
  12015. +
  12016. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12017. + yaffs_gross_unlock(dev);
  12018. +}
  12019. +
  12020. +#endif
  12021. +
  12022. +
  12023. +
  12024. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  12025. + struct yaffs_obj *obj)
  12026. +{
  12027. + struct inode *inode;
  12028. +
  12029. + if (!sb) {
  12030. + yaffs_trace(YAFFS_TRACE_OS,
  12031. + "yaffs_get_inode for NULL super_block!!");
  12032. + return NULL;
  12033. +
  12034. + }
  12035. +
  12036. + if (!obj) {
  12037. + yaffs_trace(YAFFS_TRACE_OS,
  12038. + "yaffs_get_inode for NULL object!!");
  12039. + return NULL;
  12040. +
  12041. + }
  12042. +
  12043. + yaffs_trace(YAFFS_TRACE_OS,
  12044. + "yaffs_get_inode for object %d", obj->obj_id);
  12045. +
  12046. + inode = Y_IGET(sb, obj->obj_id);
  12047. + if (IS_ERR(inode))
  12048. + return NULL;
  12049. +
  12050. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  12051. + /* iget also increments the inode's i_count */
  12052. + /* NB You can't be holding gross_lock or deadlock will happen! */
  12053. +
  12054. + return inode;
  12055. +}
  12056. +
  12057. +
  12058. +
  12059. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  12060. +#define YCRED(x) x
  12061. +#else
  12062. +#define YCRED(x) (x->cred)
  12063. +#endif
  12064. +
  12065. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12066. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  12067. + dev_t rdev)
  12068. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12069. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12070. + dev_t rdev)
  12071. +#else
  12072. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12073. + int rdev)
  12074. +#endif
  12075. +{
  12076. + struct inode *inode;
  12077. +
  12078. + struct yaffs_obj *obj = NULL;
  12079. + struct yaffs_dev *dev;
  12080. +
  12081. + struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
  12082. +
  12083. + int error = -ENOSPC;
  12084. + uid_t uid = YCRED_FSUID();
  12085. + gid_t gid =
  12086. + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
  12087. +
  12088. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  12089. + mode |= S_ISGID;
  12090. +
  12091. + if (parent) {
  12092. + yaffs_trace(YAFFS_TRACE_OS,
  12093. + "yaffs_mknod: parent object %d type %d",
  12094. + parent->obj_id, parent->variant_type);
  12095. + } else {
  12096. + yaffs_trace(YAFFS_TRACE_OS,
  12097. + "yaffs_mknod: could not get parent object");
  12098. + return -EPERM;
  12099. + }
  12100. +
  12101. + yaffs_trace(YAFFS_TRACE_OS,
  12102. + "yaffs_mknod: making oject for %s, mode %x dev %x",
  12103. + dentry->d_name.name, mode, rdev);
  12104. +
  12105. + dev = parent->my_dev;
  12106. +
  12107. + yaffs_gross_lock(dev);
  12108. +
  12109. + switch (mode & S_IFMT) {
  12110. + default:
  12111. + /* Special (socket, fifo, device...) */
  12112. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
  12113. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12114. + obj =
  12115. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12116. + gid, old_encode_dev(rdev));
  12117. +#else
  12118. + obj =
  12119. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12120. + gid, rdev);
  12121. +#endif
  12122. + break;
  12123. + case S_IFREG: /* file */
  12124. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
  12125. + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
  12126. + gid);
  12127. + break;
  12128. + case S_IFDIR: /* directory */
  12129. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
  12130. + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
  12131. + uid, gid);
  12132. + break;
  12133. + case S_IFLNK: /* symlink */
  12134. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
  12135. + obj = NULL; /* Do we ever get here? */
  12136. + break;
  12137. + }
  12138. +
  12139. + /* Can not call yaffs_get_inode() with gross lock held */
  12140. + yaffs_gross_unlock(dev);
  12141. +
  12142. + if (obj) {
  12143. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  12144. + d_instantiate(dentry, inode);
  12145. + update_dir_time(dir);
  12146. + yaffs_trace(YAFFS_TRACE_OS,
  12147. + "yaffs_mknod created object %d count = %d",
  12148. + obj->obj_id, atomic_read(&inode->i_count));
  12149. + error = 0;
  12150. + yaffs_fill_inode_from_obj(dir, parent);
  12151. + } else {
  12152. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
  12153. + error = -ENOMEM;
  12154. + }
  12155. +
  12156. + return error;
  12157. +}
  12158. +
  12159. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12160. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  12161. +#else
  12162. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  12163. +#endif
  12164. +{
  12165. + int ret_val;
  12166. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
  12167. + ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  12168. + return ret_val;
  12169. +}
  12170. +
  12171. +
  12172. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
  12173. +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  12174. + bool dummy)
  12175. +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12176. +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  12177. + struct nameidata *n)
  12178. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12179. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  12180. + struct nameidata *n)
  12181. +#else
  12182. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  12183. +#endif
  12184. +{
  12185. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
  12186. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  12187. +}
  12188. +
  12189. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
  12190. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  12191. + unsigned int dummy)
  12192. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12193. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  12194. + struct nameidata *n)
  12195. +#else
  12196. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  12197. +#endif
  12198. +{
  12199. + struct yaffs_obj *obj;
  12200. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  12201. +
  12202. + struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
  12203. +
  12204. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12205. + yaffs_gross_lock(dev);
  12206. +
  12207. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
  12208. + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
  12209. +
  12210. + obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
  12211. +
  12212. + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
  12213. +
  12214. + /* Can't hold gross lock when calling yaffs_get_inode() */
  12215. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12216. + yaffs_gross_unlock(dev);
  12217. +
  12218. + if (obj) {
  12219. + yaffs_trace(YAFFS_TRACE_OS,
  12220. + "yaffs_lookup found %d", obj->obj_id);
  12221. +
  12222. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12223. + } else {
  12224. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
  12225. +
  12226. + }
  12227. +
  12228. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  12229. + * NULL which creates dentry hash */
  12230. + d_add(dentry, inode);
  12231. +
  12232. + return NULL;
  12233. +}
  12234. +
  12235. +/*
  12236. + * Create a link...
  12237. + */
  12238. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  12239. + struct dentry *dentry)
  12240. +{
  12241. + struct inode *inode = old_dentry->d_inode;
  12242. + struct yaffs_obj *obj = NULL;
  12243. + struct yaffs_obj *link = NULL;
  12244. + struct yaffs_dev *dev;
  12245. +
  12246. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
  12247. +
  12248. + obj = yaffs_inode_to_obj(inode);
  12249. + dev = obj->my_dev;
  12250. +
  12251. + yaffs_gross_lock(dev);
  12252. +
  12253. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  12254. + link =
  12255. + yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12256. + obj);
  12257. +
  12258. + if (link) {
  12259. + set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
  12260. + d_instantiate(dentry, old_dentry->d_inode);
  12261. + atomic_inc(&old_dentry->d_inode->i_count);
  12262. + yaffs_trace(YAFFS_TRACE_OS,
  12263. + "yaffs_link link count %d i_count %d",
  12264. + old_dentry->d_inode->i_nlink,
  12265. + atomic_read(&old_dentry->d_inode->i_count));
  12266. + }
  12267. +
  12268. + yaffs_gross_unlock(dev);
  12269. +
  12270. + if (link) {
  12271. + update_dir_time(dir);
  12272. + return 0;
  12273. + }
  12274. +
  12275. + return -EPERM;
  12276. +}
  12277. +
  12278. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  12279. + const char *symname)
  12280. +{
  12281. + struct yaffs_obj *obj;
  12282. + struct yaffs_dev *dev;
  12283. + uid_t uid = YCRED_FSUID();
  12284. + gid_t gid =
  12285. + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
  12286. +
  12287. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
  12288. +
  12289. + if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
  12290. + YAFFS_MAX_NAME_LENGTH)
  12291. + return -ENAMETOOLONG;
  12292. +
  12293. + if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
  12294. + YAFFS_MAX_ALIAS_LENGTH)
  12295. + return -ENAMETOOLONG;
  12296. +
  12297. + dev = yaffs_inode_to_obj(dir)->my_dev;
  12298. + yaffs_gross_lock(dev);
  12299. + obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12300. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  12301. + yaffs_gross_unlock(dev);
  12302. +
  12303. + if (obj) {
  12304. + struct inode *inode;
  12305. +
  12306. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12307. + d_instantiate(dentry, inode);
  12308. + update_dir_time(dir);
  12309. + yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
  12310. + return 0;
  12311. + } else {
  12312. + yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
  12313. + }
  12314. +
  12315. + return -ENOMEM;
  12316. +}
  12317. +
  12318. +/*
  12319. + * The VFS layer already does all the dentry stuff for rename.
  12320. + *
  12321. + * NB: POSIX says you can rename an object over an old object of the same name
  12322. + */
  12323. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  12324. + struct inode *new_dir, struct dentry *new_dentry)
  12325. +{
  12326. + struct yaffs_dev *dev;
  12327. + int ret_val = YAFFS_FAIL;
  12328. + struct yaffs_obj *target;
  12329. +
  12330. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
  12331. + dev = yaffs_inode_to_obj(old_dir)->my_dev;
  12332. +
  12333. + yaffs_gross_lock(dev);
  12334. +
  12335. + /* Check if the target is an existing directory that is not empty. */
  12336. + target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
  12337. + new_dentry->d_name.name);
  12338. +
  12339. + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
  12340. + !list_empty(&target->variant.dir_variant.children)) {
  12341. +
  12342. + yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
  12343. +
  12344. + ret_val = YAFFS_FAIL;
  12345. + } else {
  12346. + /* Now does unlinking internally using shadowing mechanism */
  12347. + yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
  12348. +
  12349. + ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
  12350. + old_dentry->d_name.name,
  12351. + yaffs_inode_to_obj(new_dir),
  12352. + new_dentry->d_name.name);
  12353. + }
  12354. + yaffs_gross_unlock(dev);
  12355. +
  12356. + if (ret_val == YAFFS_OK) {
  12357. + if (target)
  12358. + inode_dec_link_count(new_dentry->d_inode);
  12359. +
  12360. + update_dir_time(old_dir);
  12361. + if (old_dir != new_dir)
  12362. + update_dir_time(new_dir);
  12363. + return 0;
  12364. + } else {
  12365. + return -ENOTEMPTY;
  12366. + }
  12367. +}
  12368. +
  12369. +
  12370. +
  12371. +
  12372. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  12373. +{
  12374. + int ret_val;
  12375. +
  12376. + struct yaffs_dev *dev;
  12377. + struct yaffs_obj *obj;
  12378. +
  12379. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
  12380. + (int)(dir->i_ino), dentry->d_name.name);
  12381. + obj = yaffs_inode_to_obj(dir);
  12382. + dev = obj->my_dev;
  12383. +
  12384. + yaffs_gross_lock(dev);
  12385. +
  12386. + ret_val = yaffs_unlinker(obj, dentry->d_name.name);
  12387. +
  12388. + if (ret_val == YAFFS_OK) {
  12389. + inode_dec_link_count(dentry->d_inode);
  12390. + dir->i_version++;
  12391. + yaffs_gross_unlock(dev);
  12392. + update_dir_time(dir);
  12393. + return 0;
  12394. + }
  12395. + yaffs_gross_unlock(dev);
  12396. + return -ENOTEMPTY;
  12397. +}
  12398. +
  12399. +
  12400. +
  12401. +static const struct inode_operations yaffs_dir_inode_operations = {
  12402. + .create = yaffs_create,
  12403. + .lookup = yaffs_lookup,
  12404. + .link = yaffs_link,
  12405. + .unlink = yaffs_unlink,
  12406. + .symlink = yaffs_symlink,
  12407. + .mkdir = yaffs_mkdir,
  12408. + .rmdir = yaffs_unlink,
  12409. + .mknod = yaffs_mknod,
  12410. + .rename = yaffs_rename,
  12411. + .setattr = yaffs_setattr,
  12412. + .setxattr = yaffs_setxattr,
  12413. + .getxattr = yaffs_getxattr,
  12414. + .listxattr = yaffs_listxattr,
  12415. + .removexattr = yaffs_removexattr,
  12416. +};
  12417. +
  12418. +/*-----------------------------------------------------------------*/
  12419. +/* Directory search context allows us to unlock access to yaffs during
  12420. + * filldir without causing problems with the directory being modified.
  12421. + * This is similar to the tried and tested mechanism used in yaffs direct.
  12422. + *
  12423. + * A search context iterates along a doubly linked list of siblings in the
  12424. + * directory. If the iterating object is deleted then this would corrupt
  12425. + * the list iteration, likely causing a crash. The search context avoids
  12426. + * this by using the remove_obj_fn to move the search context to the
  12427. + * next object before the object is deleted.
  12428. + *
  12429. + * Many readdirs (and thus seach conexts) may be alive simulateously so
  12430. + * each struct yaffs_dev has a list of these.
  12431. + *
  12432. + * A seach context lives for the duration of a readdir.
  12433. + *
  12434. + * All these functions must be called while yaffs is locked.
  12435. + */
  12436. +
  12437. +struct yaffs_search_context {
  12438. + struct yaffs_dev *dev;
  12439. + struct yaffs_obj *dir_obj;
  12440. + struct yaffs_obj *next_return;
  12441. + struct list_head others;
  12442. +};
  12443. +
  12444. +/*
  12445. + * yaffs_new_search() creates a new search context, initialises it and
  12446. + * adds it to the device's search context list.
  12447. + *
  12448. + * Called at start of readdir.
  12449. + */
  12450. +static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
  12451. +{
  12452. + struct yaffs_dev *dev = dir->my_dev;
  12453. + struct yaffs_search_context *sc =
  12454. + kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
  12455. + if (sc) {
  12456. + sc->dir_obj = dir;
  12457. + sc->dev = dev;
  12458. + if (list_empty(&sc->dir_obj->variant.dir_variant.children))
  12459. + sc->next_return = NULL;
  12460. + else
  12461. + sc->next_return =
  12462. + list_entry(dir->variant.dir_variant.children.next,
  12463. + struct yaffs_obj, siblings);
  12464. + INIT_LIST_HEAD(&sc->others);
  12465. + list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
  12466. + }
  12467. + return sc;
  12468. +}
  12469. +
  12470. +/*
  12471. + * yaffs_search_end() disposes of a search context and cleans up.
  12472. + */
  12473. +static void yaffs_search_end(struct yaffs_search_context *sc)
  12474. +{
  12475. + if (sc) {
  12476. + list_del(&sc->others);
  12477. + kfree(sc);
  12478. + }
  12479. +}
  12480. +
  12481. +/*
  12482. + * yaffs_search_advance() moves a search context to the next object.
  12483. + * Called when the search iterates or when an object removal causes
  12484. + * the search context to be moved to the next object.
  12485. + */
  12486. +static void yaffs_search_advance(struct yaffs_search_context *sc)
  12487. +{
  12488. + if (!sc)
  12489. + return;
  12490. +
  12491. + if (sc->next_return == NULL ||
  12492. + list_empty(&sc->dir_obj->variant.dir_variant.children))
  12493. + sc->next_return = NULL;
  12494. + else {
  12495. + struct list_head *next = sc->next_return->siblings.next;
  12496. +
  12497. + if (next == &sc->dir_obj->variant.dir_variant.children)
  12498. + sc->next_return = NULL; /* end of list */
  12499. + else
  12500. + sc->next_return =
  12501. + list_entry(next, struct yaffs_obj, siblings);
  12502. + }
  12503. +}
  12504. +
  12505. +/*
  12506. + * yaffs_remove_obj_callback() is called when an object is unlinked.
  12507. + * We check open search contexts and advance any which are currently
  12508. + * on the object being iterated.
  12509. + */
  12510. +static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
  12511. +{
  12512. +
  12513. + struct list_head *i;
  12514. + struct yaffs_search_context *sc;
  12515. + struct list_head *search_contexts =
  12516. + &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
  12517. +
  12518. + /* Iterate through the directory search contexts.
  12519. + * If any are currently on the object being removed, then advance
  12520. + * the search context to the next object to prevent a hanging pointer.
  12521. + */
  12522. + list_for_each(i, search_contexts) {
  12523. + sc = list_entry(i, struct yaffs_search_context, others);
  12524. + if (sc->next_return == obj)
  12525. + yaffs_search_advance(sc);
  12526. + }
  12527. +
  12528. +}
  12529. +
  12530. +
  12531. +/*-----------------------------------------------------------------*/
  12532. +
  12533. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
  12534. +static int yaffs_readdir(struct file *file, struct dir_context *ctx)
  12535. +{
  12536. + struct yaffs_obj *obj;
  12537. + struct yaffs_dev *dev;
  12538. + struct yaffs_search_context *sc;
  12539. + struct inode *inode = file->f_dentry->d_inode;
  12540. + unsigned long offset, curoffs;
  12541. + struct yaffs_obj *l;
  12542. + int ret_val = 0;
  12543. +
  12544. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12545. +
  12546. + obj = yaffs_dentry_to_obj(file->f_dentry);
  12547. + dev = obj->my_dev;
  12548. +
  12549. + yaffs_gross_lock(dev);
  12550. +
  12551. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12552. +
  12553. + offset = ctx->pos;
  12554. +
  12555. + sc = yaffs_new_search(obj);
  12556. + if (!sc) {
  12557. + ret_val = -ENOMEM;
  12558. + goto out;
  12559. + }
  12560. +
  12561. + yaffs_trace(YAFFS_TRACE_OS,
  12562. + "yaffs_readdir: starting at %d", (int)offset);
  12563. +
  12564. + if (offset == 0) {
  12565. + yaffs_trace(YAFFS_TRACE_OS,
  12566. + "yaffs_readdir: entry . ino %d",
  12567. + (int)inode->i_ino);
  12568. + yaffs_gross_unlock(dev);
  12569. + if (!dir_emit_dot(file, ctx)) {
  12570. + yaffs_gross_lock(dev);
  12571. + goto out;
  12572. + }
  12573. + yaffs_gross_lock(dev);
  12574. + offset++;
  12575. + ctx->pos++;
  12576. + }
  12577. + if (offset == 1) {
  12578. + yaffs_trace(YAFFS_TRACE_OS,
  12579. + "yaffs_readdir: entry .. ino %d",
  12580. + (int)file->f_dentry->d_parent->d_inode->i_ino);
  12581. + yaffs_gross_unlock(dev);
  12582. + if (!dir_emit_dotdot(file, ctx)) {
  12583. + yaffs_gross_lock(dev);
  12584. + goto out;
  12585. + }
  12586. + yaffs_gross_lock(dev);
  12587. + offset++;
  12588. + ctx->pos++;
  12589. + }
  12590. +
  12591. + curoffs = 1;
  12592. +
  12593. + /* If the directory has changed since the open or last call to
  12594. + readdir, rewind to after the 2 canned entries. */
  12595. + if (file->f_version != inode->i_version) {
  12596. + offset = 2;
  12597. + ctx->pos = offset;
  12598. + file->f_version = inode->i_version;
  12599. + }
  12600. +
  12601. + while (sc->next_return) {
  12602. + curoffs++;
  12603. + l = sc->next_return;
  12604. + if (curoffs >= offset) {
  12605. + int this_inode = yaffs_get_obj_inode(l);
  12606. + int this_type = yaffs_get_obj_type(l);
  12607. +
  12608. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12609. + yaffs_trace(YAFFS_TRACE_OS,
  12610. + "yaffs_readdir: %s inode %d",
  12611. + name, yaffs_get_obj_inode(l));
  12612. +
  12613. + yaffs_gross_unlock(dev);
  12614. +
  12615. + if (!dir_emit(ctx, name, strlen(name),
  12616. + this_inode, this_type) < 0) {
  12617. + yaffs_gross_lock(dev);
  12618. + goto out;
  12619. + }
  12620. +
  12621. + yaffs_gross_lock(dev);
  12622. +
  12623. + offset++;
  12624. + ctx->pos++;
  12625. + }
  12626. + yaffs_search_advance(sc);
  12627. + }
  12628. +
  12629. +out:
  12630. + yaffs_search_end(sc);
  12631. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12632. + yaffs_gross_unlock(dev);
  12633. +
  12634. + return ret_val;
  12635. +}
  12636. +#else
  12637. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  12638. +{
  12639. + struct yaffs_obj *obj;
  12640. + struct yaffs_dev *dev;
  12641. + struct yaffs_search_context *sc;
  12642. + struct inode *inode = f->f_dentry->d_inode;
  12643. + unsigned long offset, curoffs;
  12644. + struct yaffs_obj *l;
  12645. + int ret_val = 0;
  12646. +
  12647. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12648. +
  12649. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12650. + dev = obj->my_dev;
  12651. +
  12652. + yaffs_gross_lock(dev);
  12653. +
  12654. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12655. +
  12656. + offset = f->f_pos;
  12657. +
  12658. + sc = yaffs_new_search(obj);
  12659. + if (!sc) {
  12660. + ret_val = -ENOMEM;
  12661. + goto out;
  12662. + }
  12663. +
  12664. + yaffs_trace(YAFFS_TRACE_OS,
  12665. + "yaffs_readdir: starting at %d", (int)offset);
  12666. +
  12667. + if (offset == 0) {
  12668. + yaffs_trace(YAFFS_TRACE_OS,
  12669. + "yaffs_readdir: entry . ino %d",
  12670. + (int)inode->i_ino);
  12671. + yaffs_gross_unlock(dev);
  12672. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
  12673. + yaffs_gross_lock(dev);
  12674. + goto out;
  12675. + }
  12676. + yaffs_gross_lock(dev);
  12677. + offset++;
  12678. + f->f_pos++;
  12679. + }
  12680. + if (offset == 1) {
  12681. + yaffs_trace(YAFFS_TRACE_OS,
  12682. + "yaffs_readdir: entry .. ino %d",
  12683. + (int)f->f_dentry->d_parent->d_inode->i_ino);
  12684. + yaffs_gross_unlock(dev);
  12685. + if (filldir(dirent, "..", 2, offset,
  12686. + f->f_dentry->d_parent->d_inode->i_ino,
  12687. + DT_DIR) < 0) {
  12688. + yaffs_gross_lock(dev);
  12689. + goto out;
  12690. + }
  12691. + yaffs_gross_lock(dev);
  12692. + offset++;
  12693. + f->f_pos++;
  12694. + }
  12695. +
  12696. + curoffs = 1;
  12697. +
  12698. + /* If the directory has changed since the open or last call to
  12699. + readdir, rewind to after the 2 canned entries. */
  12700. + if (f->f_version != inode->i_version) {
  12701. + offset = 2;
  12702. + f->f_pos = offset;
  12703. + f->f_version = inode->i_version;
  12704. + }
  12705. +
  12706. + while (sc->next_return) {
  12707. + curoffs++;
  12708. + l = sc->next_return;
  12709. + if (curoffs >= offset) {
  12710. + int this_inode = yaffs_get_obj_inode(l);
  12711. + int this_type = yaffs_get_obj_type(l);
  12712. +
  12713. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12714. + yaffs_trace(YAFFS_TRACE_OS,
  12715. + "yaffs_readdir: %s inode %d",
  12716. + name, yaffs_get_obj_inode(l));
  12717. +
  12718. + yaffs_gross_unlock(dev);
  12719. +
  12720. + if (filldir(dirent,
  12721. + name,
  12722. + strlen(name),
  12723. + offset, this_inode, this_type) < 0) {
  12724. + yaffs_gross_lock(dev);
  12725. + goto out;
  12726. + }
  12727. +
  12728. + yaffs_gross_lock(dev);
  12729. +
  12730. + offset++;
  12731. + f->f_pos++;
  12732. + }
  12733. + yaffs_search_advance(sc);
  12734. + }
  12735. +
  12736. +out:
  12737. + yaffs_search_end(sc);
  12738. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12739. + yaffs_gross_unlock(dev);
  12740. +
  12741. + return ret_val;
  12742. +}
  12743. +#endif
  12744. +
  12745. +static const struct file_operations yaffs_dir_operations = {
  12746. + .read = generic_read_dir,
  12747. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
  12748. + .iterate = yaffs_readdir,
  12749. +#else
  12750. + .readdir = yaffs_readdir,
  12751. +#endif
  12752. + .fsync = yaffs_sync_object,
  12753. + .llseek = generic_file_llseek,
  12754. +};
  12755. +
  12756. +static void yaffs_fill_inode_from_obj(struct inode *inode,
  12757. + struct yaffs_obj *obj)
  12758. +{
  12759. + if (inode && obj) {
  12760. +
  12761. + /* Check mode against the variant type and attempt to repair if broken. */
  12762. + u32 mode = obj->yst_mode;
  12763. + switch (obj->variant_type) {
  12764. + case YAFFS_OBJECT_TYPE_FILE:
  12765. + if (!S_ISREG(mode)) {
  12766. + obj->yst_mode &= ~S_IFMT;
  12767. + obj->yst_mode |= S_IFREG;
  12768. + }
  12769. +
  12770. + break;
  12771. + case YAFFS_OBJECT_TYPE_SYMLINK:
  12772. + if (!S_ISLNK(mode)) {
  12773. + obj->yst_mode &= ~S_IFMT;
  12774. + obj->yst_mode |= S_IFLNK;
  12775. + }
  12776. +
  12777. + break;
  12778. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  12779. + if (!S_ISDIR(mode)) {
  12780. + obj->yst_mode &= ~S_IFMT;
  12781. + obj->yst_mode |= S_IFDIR;
  12782. + }
  12783. +
  12784. + break;
  12785. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  12786. + case YAFFS_OBJECT_TYPE_HARDLINK:
  12787. + case YAFFS_OBJECT_TYPE_SPECIAL:
  12788. + default:
  12789. + /* TODO? */
  12790. + break;
  12791. + }
  12792. +
  12793. + inode->i_flags |= S_NOATIME;
  12794. +
  12795. + inode->i_ino = obj->obj_id;
  12796. + inode->i_mode = obj->yst_mode;
  12797. + i_uid_write(inode, obj->yst_uid);
  12798. + i_gid_write(inode, obj->yst_gid);
  12799. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  12800. + inode->i_blksize = inode->i_sb->s_blocksize;
  12801. +#endif
  12802. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12803. +
  12804. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  12805. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  12806. + inode->i_atime.tv_nsec = 0;
  12807. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  12808. + inode->i_mtime.tv_nsec = 0;
  12809. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  12810. + inode->i_ctime.tv_nsec = 0;
  12811. +#else
  12812. + inode->i_rdev = obj->yst_rdev;
  12813. + inode->i_atime = obj->yst_atime;
  12814. + inode->i_mtime = obj->yst_mtime;
  12815. + inode->i_ctime = obj->yst_ctime;
  12816. +#endif
  12817. + inode->i_size = yaffs_get_obj_length(obj);
  12818. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12819. +
  12820. + set_nlink(inode, yaffs_get_obj_link_count(obj));
  12821. +
  12822. + yaffs_trace(YAFFS_TRACE_OS,
  12823. + "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
  12824. + inode->i_mode, i_uid_read(inode), i_gid_read(inode),
  12825. + inode->i_size, atomic_read(&inode->i_count));
  12826. +
  12827. + switch (obj->yst_mode & S_IFMT) {
  12828. + default: /* fifo, device or socket */
  12829. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12830. + init_special_inode(inode, obj->yst_mode,
  12831. + old_decode_dev(obj->yst_rdev));
  12832. +#else
  12833. + init_special_inode(inode, obj->yst_mode,
  12834. + (dev_t) (obj->yst_rdev));
  12835. +#endif
  12836. + break;
  12837. + case S_IFREG: /* file */
  12838. + inode->i_op = &yaffs_file_inode_operations;
  12839. + inode->i_fop = &yaffs_file_operations;
  12840. + inode->i_mapping->a_ops =
  12841. + &yaffs_file_address_operations;
  12842. + break;
  12843. + case S_IFDIR: /* directory */
  12844. + inode->i_op = &yaffs_dir_inode_operations;
  12845. + inode->i_fop = &yaffs_dir_operations;
  12846. + break;
  12847. + case S_IFLNK: /* symlink */
  12848. + inode->i_op = &yaffs_symlink_inode_operations;
  12849. + break;
  12850. + }
  12851. +
  12852. + yaffs_inode_to_obj_lv(inode) = obj;
  12853. +
  12854. + obj->my_inode = inode;
  12855. +
  12856. + } else {
  12857. + yaffs_trace(YAFFS_TRACE_OS,
  12858. + "yaffs_fill_inode invalid parameters");
  12859. + }
  12860. +
  12861. +}
  12862. +
  12863. +
  12864. +
  12865. +/*
  12866. + * yaffs background thread functions .
  12867. + * yaffs_bg_thread_fn() the thread function
  12868. + * yaffs_bg_start() launches the background thread.
  12869. + * yaffs_bg_stop() cleans up the background thread.
  12870. + *
  12871. + * NB:
  12872. + * The thread should only run after the yaffs is initialised
  12873. + * The thread should be stopped before yaffs is unmounted.
  12874. + * The thread should not do any writing while the fs is in read only.
  12875. + */
  12876. +
  12877. +static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
  12878. +{
  12879. + unsigned erased_chunks =
  12880. + dev->n_erased_blocks * dev->param.chunks_per_block;
  12881. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12882. + unsigned scattered = 0; /* Free chunks not in an erased block */
  12883. +
  12884. + if (erased_chunks < dev->n_free_chunks)
  12885. + scattered = (dev->n_free_chunks - erased_chunks);
  12886. +
  12887. + if (!context->bg_running)
  12888. + return 0;
  12889. + else if (scattered < (dev->param.chunks_per_block * 2))
  12890. + return 0;
  12891. + else if (erased_chunks > dev->n_free_chunks / 2)
  12892. + return 0;
  12893. + else if (erased_chunks > dev->n_free_chunks / 4)
  12894. + return 1;
  12895. + else
  12896. + return 2;
  12897. +}
  12898. +
  12899. +#ifdef YAFFS_COMPILE_BACKGROUND
  12900. +
  12901. +void yaffs_background_waker(unsigned long data)
  12902. +{
  12903. + wake_up_process((struct task_struct *)data);
  12904. +}
  12905. +
  12906. +static int yaffs_bg_thread_fn(void *data)
  12907. +{
  12908. + struct yaffs_dev *dev = (struct yaffs_dev *)data;
  12909. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12910. + unsigned long now = jiffies;
  12911. + unsigned long next_dir_update = now;
  12912. + unsigned long next_gc = now;
  12913. + unsigned long expires;
  12914. + unsigned int urgency;
  12915. +
  12916. + int gc_result;
  12917. + struct timer_list timer;
  12918. +
  12919. + yaffs_trace(YAFFS_TRACE_BACKGROUND,
  12920. + "yaffs_background starting for dev %p", (void *)dev);
  12921. +
  12922. +#ifdef YAFFS_COMPILE_FREEZER
  12923. + set_freezable();
  12924. +#endif
  12925. + while (context->bg_running) {
  12926. + yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
  12927. +
  12928. + if (kthread_should_stop())
  12929. + break;
  12930. +
  12931. +#ifdef YAFFS_COMPILE_FREEZER
  12932. + if (try_to_freeze())
  12933. + continue;
  12934. +#endif
  12935. + yaffs_gross_lock(dev);
  12936. +
  12937. + now = jiffies;
  12938. +
  12939. + if (time_after(now, next_dir_update) && yaffs_bg_enable) {
  12940. + yaffs_update_dirty_dirs(dev);
  12941. + next_dir_update = now + HZ;
  12942. + }
  12943. +
  12944. + if (time_after(now, next_gc) && yaffs_bg_enable) {
  12945. + if (!dev->is_checkpointed) {
  12946. + urgency = yaffs_bg_gc_urgency(dev);
  12947. + gc_result = yaffs_bg_gc(dev, urgency);
  12948. + if (urgency > 1)
  12949. + next_gc = now + HZ / 20 + 1;
  12950. + else if (urgency > 0)
  12951. + next_gc = now + HZ / 10 + 1;
  12952. + else
  12953. + next_gc = now + HZ * 2;
  12954. + } else {
  12955. + /*
  12956. + * gc not running so set to next_dir_update
  12957. + * to cut down on wake ups
  12958. + */
  12959. + next_gc = next_dir_update;
  12960. + }
  12961. + }
  12962. + yaffs_gross_unlock(dev);
  12963. +#if 1
  12964. + expires = next_dir_update;
  12965. + if (time_before(next_gc, expires))
  12966. + expires = next_gc;
  12967. + if (time_before(expires, now))
  12968. + expires = now + HZ;
  12969. +
  12970. + Y_INIT_TIMER(&timer);
  12971. + timer.expires = expires + 1;
  12972. + timer.data = (unsigned long)current;
  12973. + timer.function = yaffs_background_waker;
  12974. +
  12975. + set_current_state(TASK_INTERRUPTIBLE);
  12976. + add_timer(&timer);
  12977. + schedule();
  12978. + del_timer_sync(&timer);
  12979. +#else
  12980. + msleep(10);
  12981. +#endif
  12982. + }
  12983. +
  12984. + return 0;
  12985. +}
  12986. +
  12987. +static int yaffs_bg_start(struct yaffs_dev *dev)
  12988. +{
  12989. + int retval = 0;
  12990. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12991. +
  12992. + if (dev->read_only)
  12993. + return -1;
  12994. +
  12995. + context->bg_running = 1;
  12996. +
  12997. + context->bg_thread = kthread_run(yaffs_bg_thread_fn,
  12998. + (void *)dev, "yaffs-bg-%d",
  12999. + context->mount_id);
  13000. +
  13001. + if (IS_ERR(context->bg_thread)) {
  13002. + retval = PTR_ERR(context->bg_thread);
  13003. + context->bg_thread = NULL;
  13004. + context->bg_running = 0;
  13005. + }
  13006. + return retval;
  13007. +}
  13008. +
  13009. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13010. +{
  13011. + struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
  13012. +
  13013. + ctxt->bg_running = 0;
  13014. +
  13015. + if (ctxt->bg_thread) {
  13016. + kthread_stop(ctxt->bg_thread);
  13017. + ctxt->bg_thread = NULL;
  13018. + }
  13019. +}
  13020. +#else
  13021. +static int yaffs_bg_thread_fn(void *data)
  13022. +{
  13023. + return 0;
  13024. +}
  13025. +
  13026. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13027. +{
  13028. + return 0;
  13029. +}
  13030. +
  13031. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13032. +{
  13033. +}
  13034. +#endif
  13035. +
  13036. +
  13037. +static void yaffs_flush_inodes(struct super_block *sb)
  13038. +{
  13039. + struct inode *iptr;
  13040. + struct yaffs_obj *obj;
  13041. +
  13042. + list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
  13043. + obj = yaffs_inode_to_obj(iptr);
  13044. + if (obj) {
  13045. + yaffs_trace(YAFFS_TRACE_OS,
  13046. + "flushing obj %d",
  13047. + obj->obj_id);
  13048. + yaffs_flush_file(obj, 1, 0);
  13049. + }
  13050. + }
  13051. +}
  13052. +
  13053. +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
  13054. +{
  13055. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13056. + if (!dev)
  13057. + return;
  13058. +
  13059. + yaffs_flush_inodes(sb);
  13060. + yaffs_update_dirty_dirs(dev);
  13061. + yaffs_flush_whole_cache(dev);
  13062. + if (do_checkpoint)
  13063. + yaffs_checkpoint_save(dev);
  13064. +}
  13065. +
  13066. +static LIST_HEAD(yaffs_context_list);
  13067. +struct mutex yaffs_context_lock;
  13068. +
  13069. +static void yaffs_put_super(struct super_block *sb)
  13070. +{
  13071. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13072. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  13073. +
  13074. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
  13075. + "yaffs_put_super");
  13076. +
  13077. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13078. + "Shutting down yaffs background thread");
  13079. + yaffs_bg_stop(dev);
  13080. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13081. + "yaffs background thread shut down");
  13082. +
  13083. + yaffs_gross_lock(dev);
  13084. +
  13085. + yaffs_flush_super(sb, 1);
  13086. +
  13087. + yaffs_deinitialise(dev);
  13088. +
  13089. + yaffs_gross_unlock(dev);
  13090. +
  13091. + mutex_lock(&yaffs_context_lock);
  13092. + list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
  13093. + mutex_unlock(&yaffs_context_lock);
  13094. +
  13095. + if (yaffs_dev_to_lc(dev)->spare_buffer) {
  13096. + kfree(yaffs_dev_to_lc(dev)->spare_buffer);
  13097. + yaffs_dev_to_lc(dev)->spare_buffer = NULL;
  13098. + }
  13099. +
  13100. + kfree(dev);
  13101. +
  13102. + yaffs_put_mtd_device(mtd);
  13103. +
  13104. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
  13105. + "yaffs_put_super done");
  13106. +}
  13107. +
  13108. +
  13109. +static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
  13110. +{
  13111. + return yaffs_gc_control;
  13112. +}
  13113. +
  13114. +
  13115. +#ifdef YAFFS_COMPILE_EXPORTFS
  13116. +
  13117. +static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  13118. + uint32_t generation)
  13119. +{
  13120. + return Y_IGET(sb, ino);
  13121. +}
  13122. +
  13123. +static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
  13124. + struct fid *fid, int fh_len,
  13125. + int fh_type)
  13126. +{
  13127. + return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  13128. + yaffs2_nfs_get_inode);
  13129. +}
  13130. +
  13131. +static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
  13132. + struct fid *fid, int fh_len,
  13133. + int fh_type)
  13134. +{
  13135. + return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  13136. + yaffs2_nfs_get_inode);
  13137. +}
  13138. +
  13139. +struct dentry *yaffs2_get_parent(struct dentry *dentry)
  13140. +{
  13141. +
  13142. + struct super_block *sb = dentry->d_inode->i_sb;
  13143. + struct dentry *parent = ERR_PTR(-ENOENT);
  13144. + struct inode *inode;
  13145. + unsigned long parent_ino;
  13146. + struct yaffs_obj *d_obj;
  13147. + struct yaffs_obj *parent_obj;
  13148. +
  13149. + d_obj = yaffs_inode_to_obj(dentry->d_inode);
  13150. +
  13151. + if (d_obj) {
  13152. + parent_obj = d_obj->parent;
  13153. + if (parent_obj) {
  13154. + parent_ino = yaffs_get_obj_inode(parent_obj);
  13155. + inode = Y_IGET(sb, parent_ino);
  13156. +
  13157. + if (IS_ERR(inode)) {
  13158. + parent = ERR_CAST(inode);
  13159. + } else {
  13160. + parent = d_obtain_alias(inode);
  13161. + if (!IS_ERR(parent)) {
  13162. + parent = ERR_PTR(-ENOMEM);
  13163. + iput(inode);
  13164. + }
  13165. + }
  13166. + }
  13167. + }
  13168. +
  13169. + return parent;
  13170. +}
  13171. +
  13172. +/* Just declare a zero structure as a NULL value implies
  13173. + * using the default functions of exportfs.
  13174. + */
  13175. +
  13176. +static struct export_operations yaffs_export_ops = {
  13177. + .fh_to_dentry = yaffs2_fh_to_dentry,
  13178. + .fh_to_parent = yaffs2_fh_to_parent,
  13179. + .get_parent = yaffs2_get_parent,
  13180. +};
  13181. +
  13182. +#endif
  13183. +
  13184. +static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
  13185. +{
  13186. + /* Clear the association between the inode and
  13187. + * the struct yaffs_obj.
  13188. + */
  13189. + obj->my_inode = NULL;
  13190. + yaffs_inode_to_obj_lv(inode) = NULL;
  13191. +
  13192. + /* If the object freeing was deferred, then the real
  13193. + * free happens now.
  13194. + * This should fix the inode inconsistency problem.
  13195. + */
  13196. + yaffs_handle_defered_free(obj);
  13197. +}
  13198. +
  13199. +#ifdef YAFFS_HAS_EVICT_INODE
  13200. +/* yaffs_evict_inode combines into one operation what was previously done in
  13201. + * yaffs_clear_inode() and yaffs_delete_inode()
  13202. + *
  13203. + */
  13204. +static void yaffs_evict_inode(struct inode *inode)
  13205. +{
  13206. + struct yaffs_obj *obj;
  13207. + struct yaffs_dev *dev;
  13208. + int deleteme = 0;
  13209. +
  13210. + obj = yaffs_inode_to_obj(inode);
  13211. +
  13212. + yaffs_trace(YAFFS_TRACE_OS,
  13213. + "yaffs_evict_inode: ino %d, count %d %s",
  13214. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13215. + obj ? "object exists" : "null object");
  13216. +
  13217. + if (!inode->i_nlink && !is_bad_inode(inode))
  13218. + deleteme = 1;
  13219. + truncate_inode_pages(&inode->i_data, 0);
  13220. + Y_CLEAR_INODE(inode);
  13221. +
  13222. + if (deleteme && obj) {
  13223. + dev = obj->my_dev;
  13224. + yaffs_gross_lock(dev);
  13225. + yaffs_del_obj(obj);
  13226. + yaffs_gross_unlock(dev);
  13227. + }
  13228. + if (obj) {
  13229. + dev = obj->my_dev;
  13230. + yaffs_gross_lock(dev);
  13231. + yaffs_unstitch_obj(inode, obj);
  13232. + yaffs_gross_unlock(dev);
  13233. + }
  13234. +}
  13235. +#else
  13236. +
  13237. +/* clear is called to tell the fs to release any per-inode data it holds.
  13238. + * The object might still exist on disk and is just being thrown out of the cache
  13239. + * or else the object has actually been deleted and we're being called via
  13240. + * the chain
  13241. + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
  13242. + */
  13243. +
  13244. +static void yaffs_clear_inode(struct inode *inode)
  13245. +{
  13246. + struct yaffs_obj *obj;
  13247. + struct yaffs_dev *dev;
  13248. +
  13249. + obj = yaffs_inode_to_obj(inode);
  13250. +
  13251. + yaffs_trace(YAFFS_TRACE_OS,
  13252. + "yaffs_clear_inode: ino %d, count %d %s",
  13253. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13254. + obj ? "object exists" : "null object");
  13255. +
  13256. + if (obj) {
  13257. + dev = obj->my_dev;
  13258. + yaffs_gross_lock(dev);
  13259. + yaffs_unstitch_obj(inode, obj);
  13260. + yaffs_gross_unlock(dev);
  13261. + }
  13262. +
  13263. +}
  13264. +
  13265. +/* delete is called when the link count is zero and the inode
  13266. + * is put (ie. nobody wants to know about it anymore, time to
  13267. + * delete the file).
  13268. + * NB Must call clear_inode()
  13269. + */
  13270. +static void yaffs_delete_inode(struct inode *inode)
  13271. +{
  13272. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  13273. + struct yaffs_dev *dev;
  13274. +
  13275. + yaffs_trace(YAFFS_TRACE_OS,
  13276. + "yaffs_delete_inode: ino %d, count %d %s",
  13277. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13278. + obj ? "object exists" : "null object");
  13279. +
  13280. + if (obj) {
  13281. + dev = obj->my_dev;
  13282. + yaffs_gross_lock(dev);
  13283. + yaffs_del_obj(obj);
  13284. + yaffs_gross_unlock(dev);
  13285. + }
  13286. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  13287. + truncate_inode_pages(&inode->i_data, 0);
  13288. +#endif
  13289. + clear_inode(inode);
  13290. +}
  13291. +#endif
  13292. +
  13293. +
  13294. +
  13295. +
  13296. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13297. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  13298. +{
  13299. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  13300. + struct super_block *sb = dentry->d_sb;
  13301. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13302. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  13303. +{
  13304. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13305. +#else
  13306. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  13307. +{
  13308. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13309. +#endif
  13310. +
  13311. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
  13312. +
  13313. + yaffs_gross_lock(dev);
  13314. +
  13315. + buf->f_type = YAFFS_MAGIC;
  13316. + buf->f_bsize = sb->s_blocksize;
  13317. + buf->f_namelen = 255;
  13318. +
  13319. + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
  13320. + /* Do this if chunk size is not a power of 2 */
  13321. +
  13322. + uint64_t bytes_in_dev;
  13323. + uint64_t bytes_free;
  13324. +
  13325. + bytes_in_dev =
  13326. + ((uint64_t)
  13327. + ((dev->param.end_block - dev->param.start_block +
  13328. + 1))) * ((uint64_t) (dev->param.chunks_per_block *
  13329. + dev->data_bytes_per_chunk));
  13330. +
  13331. + do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
  13332. + buf->f_blocks = bytes_in_dev;
  13333. +
  13334. + bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
  13335. + ((uint64_t) (dev->data_bytes_per_chunk));
  13336. +
  13337. + do_div(bytes_free, sb->s_blocksize);
  13338. +
  13339. + buf->f_bfree = bytes_free;
  13340. +
  13341. + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
  13342. +
  13343. + buf->f_blocks =
  13344. + (dev->param.end_block - dev->param.start_block + 1) *
  13345. + dev->param.chunks_per_block /
  13346. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13347. + buf->f_bfree =
  13348. + yaffs_get_n_free_chunks(dev) /
  13349. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13350. + } else {
  13351. + buf->f_blocks =
  13352. + (dev->param.end_block - dev->param.start_block + 1) *
  13353. + dev->param.chunks_per_block *
  13354. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13355. +
  13356. + buf->f_bfree =
  13357. + yaffs_get_n_free_chunks(dev) *
  13358. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13359. + }
  13360. +
  13361. + buf->f_files = 0;
  13362. + buf->f_ffree = 0;
  13363. + buf->f_bavail = buf->f_bfree;
  13364. +
  13365. + yaffs_gross_unlock(dev);
  13366. + return 0;
  13367. +}
  13368. +
  13369. +
  13370. +
  13371. +static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
  13372. +{
  13373. +
  13374. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13375. + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
  13376. + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
  13377. + int do_checkpoint;
  13378. + int dirty = yaffs_check_super_dirty(dev);
  13379. +
  13380. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13381. + "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
  13382. + gc_urgent,
  13383. + dirty ? "dirty" : "clean",
  13384. + request_checkpoint ? "checkpoint requested" : "no checkpoint",
  13385. + oneshot_checkpoint ? " one-shot" : "");
  13386. +
  13387. + yaffs_gross_lock(dev);
  13388. + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
  13389. + oneshot_checkpoint) && !dev->is_checkpointed;
  13390. +
  13391. + if (dirty || do_checkpoint) {
  13392. + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
  13393. + yaffs_clear_super_dirty(dev);
  13394. + if (oneshot_checkpoint)
  13395. + yaffs_auto_checkpoint &= ~4;
  13396. + }
  13397. + yaffs_gross_unlock(dev);
  13398. +
  13399. + return 0;
  13400. +}
  13401. +
  13402. +
  13403. +#ifdef YAFFS_HAS_WRITE_SUPER
  13404. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13405. +static void yaffs_write_super(struct super_block *sb)
  13406. +#else
  13407. +static int yaffs_write_super(struct super_block *sb)
  13408. +#endif
  13409. +{
  13410. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
  13411. +
  13412. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13413. + "yaffs_write_super %s",
  13414. + request_checkpoint ? " checkpt" : "");
  13415. +
  13416. + yaffs_do_sync_fs(sb, request_checkpoint);
  13417. +
  13418. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  13419. + return 0;
  13420. +#endif
  13421. +}
  13422. +#endif
  13423. +
  13424. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13425. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  13426. +#else
  13427. +static int yaffs_sync_fs(struct super_block *sb)
  13428. +#endif
  13429. +{
  13430. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
  13431. +
  13432. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  13433. + "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
  13434. +
  13435. + yaffs_do_sync_fs(sb, request_checkpoint);
  13436. +
  13437. + return 0;
  13438. +}
  13439. +
  13440. +
  13441. +
  13442. +static const struct super_operations yaffs_super_ops = {
  13443. + .statfs = yaffs_statfs,
  13444. +
  13445. +#ifndef YAFFS_USE_OWN_IGET
  13446. + .read_inode = yaffs_read_inode,
  13447. +#endif
  13448. +#ifdef YAFFS_HAS_PUT_INODE
  13449. + .put_inode = yaffs_put_inode,
  13450. +#endif
  13451. + .put_super = yaffs_put_super,
  13452. +#ifdef YAFFS_HAS_EVICT_INODE
  13453. + .evict_inode = yaffs_evict_inode,
  13454. +#else
  13455. + .delete_inode = yaffs_delete_inode,
  13456. + .clear_inode = yaffs_clear_inode,
  13457. +#endif
  13458. + .sync_fs = yaffs_sync_fs,
  13459. +#ifdef YAFFS_HAS_WRITE_SUPER
  13460. + .write_super = yaffs_write_super,
  13461. +#endif
  13462. +};
  13463. +
  13464. +struct yaffs_options {
  13465. + int inband_tags;
  13466. + int skip_checkpoint_read;
  13467. + int skip_checkpoint_write;
  13468. + int no_cache;
  13469. + int tags_ecc_on;
  13470. + int tags_ecc_overridden;
  13471. + int lazy_loading_enabled;
  13472. + int lazy_loading_overridden;
  13473. + int empty_lost_and_found;
  13474. + int empty_lost_and_found_overridden;
  13475. + int disable_summary;
  13476. +};
  13477. +
  13478. +#define MAX_OPT_LEN 30
  13479. +static int yaffs_parse_options(struct yaffs_options *options,
  13480. + const char *options_str)
  13481. +{
  13482. + char cur_opt[MAX_OPT_LEN + 1];
  13483. + int p;
  13484. + int error = 0;
  13485. +
  13486. + /* Parse through the options which is a comma seperated list */
  13487. +
  13488. + while (options_str && *options_str && !error) {
  13489. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  13490. + p = 0;
  13491. +
  13492. + while (*options_str == ',')
  13493. + options_str++;
  13494. +
  13495. + while (*options_str && *options_str != ',') {
  13496. + if (p < MAX_OPT_LEN) {
  13497. + cur_opt[p] = *options_str;
  13498. + p++;
  13499. + }
  13500. + options_str++;
  13501. + }
  13502. +
  13503. + if (!strcmp(cur_opt, "inband-tags")) {
  13504. + options->inband_tags = 1;
  13505. + } else if (!strcmp(cur_opt, "tags-ecc-off")) {
  13506. + options->tags_ecc_on = 0;
  13507. + options->tags_ecc_overridden = 1;
  13508. + } else if (!strcmp(cur_opt, "tags-ecc-on")) {
  13509. + options->tags_ecc_on = 1;
  13510. + options->tags_ecc_overridden = 1;
  13511. + } else if (!strcmp(cur_opt, "lazy-loading-off")) {
  13512. + options->lazy_loading_enabled = 0;
  13513. + options->lazy_loading_overridden = 1;
  13514. + } else if (!strcmp(cur_opt, "lazy-loading-on")) {
  13515. + options->lazy_loading_enabled = 1;
  13516. + options->lazy_loading_overridden = 1;
  13517. + } else if (!strcmp(cur_opt, "disable-summary")) {
  13518. + options->disable_summary = 1;
  13519. + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
  13520. + options->empty_lost_and_found = 0;
  13521. + options->empty_lost_and_found_overridden = 1;
  13522. + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
  13523. + options->empty_lost_and_found = 1;
  13524. + options->empty_lost_and_found_overridden = 1;
  13525. + } else if (!strcmp(cur_opt, "no-cache")) {
  13526. + options->no_cache = 1;
  13527. + } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
  13528. + options->skip_checkpoint_read = 1;
  13529. + } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
  13530. + options->skip_checkpoint_write = 1;
  13531. + } else if (!strcmp(cur_opt, "no-checkpoint")) {
  13532. + options->skip_checkpoint_read = 1;
  13533. + options->skip_checkpoint_write = 1;
  13534. + } else {
  13535. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  13536. + cur_opt);
  13537. + error = 1;
  13538. + }
  13539. + }
  13540. +
  13541. + return error;
  13542. +}
  13543. +
  13544. +
  13545. +static struct dentry *yaffs_make_root(struct inode *inode)
  13546. +{
  13547. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
  13548. + struct dentry *root = d_alloc_root(inode);
  13549. +
  13550. + if (!root)
  13551. + iput(inode);
  13552. +
  13553. + return root;
  13554. +#else
  13555. + return d_make_root(inode);
  13556. +#endif
  13557. +}
  13558. +
  13559. +
  13560. +
  13561. +
  13562. +static struct super_block *yaffs_internal_read_super(int yaffs_version,
  13563. + struct super_block *sb,
  13564. + void *data, int silent)
  13565. +{
  13566. + int n_blocks;
  13567. + struct inode *inode = NULL;
  13568. + struct dentry *root;
  13569. + struct yaffs_dev *dev = 0;
  13570. + char devname_buf[BDEVNAME_SIZE + 1];
  13571. + struct mtd_info *mtd;
  13572. + int err;
  13573. + char *data_str = (char *)data;
  13574. + struct yaffs_linux_context *context = NULL;
  13575. + struct yaffs_param *param;
  13576. +
  13577. + int read_only = 0;
  13578. + int inband_tags = 0;
  13579. +
  13580. + struct yaffs_options options;
  13581. +
  13582. + unsigned mount_id;
  13583. + int found;
  13584. + struct yaffs_linux_context *context_iterator;
  13585. + struct list_head *l;
  13586. +
  13587. + if (!sb) {
  13588. + printk(KERN_INFO "yaffs: sb is NULL\n");
  13589. + return NULL;
  13590. + }
  13591. +
  13592. + sb->s_magic = YAFFS_MAGIC;
  13593. + sb->s_op = &yaffs_super_ops;
  13594. + sb->s_flags |= MS_NOATIME;
  13595. +
  13596. + read_only = ((sb->s_flags & MS_RDONLY) != 0);
  13597. +
  13598. +#ifdef YAFFS_COMPILE_EXPORTFS
  13599. + sb->s_export_op = &yaffs_export_ops;
  13600. +#endif
  13601. +
  13602. + if (!sb->s_dev)
  13603. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  13604. + else if (!yaffs_devname(sb, devname_buf))
  13605. + printk(KERN_INFO "yaffs: devname is NULL\n");
  13606. + else
  13607. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
  13608. + sb->s_dev,
  13609. + yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
  13610. +
  13611. + if (!data_str)
  13612. + data_str = "";
  13613. +
  13614. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  13615. +
  13616. + memset(&options, 0, sizeof(options));
  13617. +
  13618. + if (yaffs_parse_options(&options, data_str)) {
  13619. + /* Option parsing failed */
  13620. + return NULL;
  13621. + }
  13622. +
  13623. + sb->s_blocksize = PAGE_CACHE_SIZE;
  13624. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  13625. +
  13626. + yaffs_trace(YAFFS_TRACE_OS,
  13627. + "yaffs_read_super: Using yaffs%d", yaffs_version);
  13628. + yaffs_trace(YAFFS_TRACE_OS,
  13629. + "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
  13630. +
  13631. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13632. + "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
  13633. + MAJOR(sb->s_dev), MINOR(sb->s_dev),
  13634. + yaffs_devname(sb, devname_buf));
  13635. +
  13636. + /* Get the device */
  13637. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  13638. + if (IS_ERR(mtd)) {
  13639. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13640. + "yaffs: MTD device %u either not valid or unavailable",
  13641. + MINOR(sb->s_dev));
  13642. + return NULL;
  13643. + }
  13644. +
  13645. + if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
  13646. + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
  13647. + yaffs_version = 2;
  13648. + }
  13649. +
  13650. + /* Added NCB 26/5/2006 for completeness */
  13651. + if (yaffs_version == 2 && !options.inband_tags
  13652. + && WRITE_SIZE(mtd) == 512) {
  13653. + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
  13654. + yaffs_version = 1;
  13655. + }
  13656. +
  13657. + if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
  13658. + options.inband_tags)
  13659. + inband_tags = 1;
  13660. +
  13661. + if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
  13662. + return NULL;
  13663. +
  13664. + /* OK, so if we got here, we have an MTD that's NAND and looks
  13665. + * like it has the right capabilities
  13666. + * Set the struct yaffs_dev up for mtd
  13667. + */
  13668. +
  13669. + if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
  13670. + read_only = 1;
  13671. + printk(KERN_INFO
  13672. + "yaffs: mtd is read only, setting superblock read only\n"
  13673. + );
  13674. + sb->s_flags |= MS_RDONLY;
  13675. + }
  13676. +
  13677. + dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
  13678. + context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
  13679. +
  13680. + if (!dev || !context) {
  13681. + kfree(dev);
  13682. + kfree(context);
  13683. + dev = NULL;
  13684. + context = NULL;
  13685. +
  13686. + /* Deep shit could not allocate device structure */
  13687. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13688. + "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
  13689. + );
  13690. + return NULL;
  13691. + }
  13692. + memset(dev, 0, sizeof(struct yaffs_dev));
  13693. + param = &(dev->param);
  13694. +
  13695. + memset(context, 0, sizeof(struct yaffs_linux_context));
  13696. + dev->os_context = context;
  13697. + INIT_LIST_HEAD(&(context->context_list));
  13698. + context->dev = dev;
  13699. + context->super = sb;
  13700. +
  13701. + dev->read_only = read_only;
  13702. +
  13703. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13704. + sb->s_fs_info = dev;
  13705. +#else
  13706. + sb->u.generic_sbp = dev;
  13707. +#endif
  13708. +
  13709. +
  13710. + dev->driver_context = mtd;
  13711. + param->name = mtd->name;
  13712. +
  13713. + /* Set up the memory size parameters.... */
  13714. +
  13715. +
  13716. + param->n_reserved_blocks = 5;
  13717. + param->n_caches = (options.no_cache) ? 0 : 10;
  13718. + param->inband_tags = inband_tags;
  13719. +
  13720. + param->enable_xattr = 1;
  13721. + if (options.lazy_loading_overridden)
  13722. + param->disable_lazy_load = !options.lazy_loading_enabled;
  13723. +
  13724. + param->defered_dir_update = 1;
  13725. +
  13726. + if (options.tags_ecc_overridden)
  13727. + param->no_tags_ecc = !options.tags_ecc_on;
  13728. +
  13729. + param->empty_lost_n_found = 1;
  13730. + param->refresh_period = 500;
  13731. + param->disable_summary = options.disable_summary;
  13732. +
  13733. +
  13734. +#ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
  13735. + param->disable_bad_block_marking = 1;
  13736. +#endif
  13737. + if (options.empty_lost_and_found_overridden)
  13738. + param->empty_lost_n_found = options.empty_lost_and_found;
  13739. +
  13740. + /* ... and the functions. */
  13741. + if (yaffs_version == 2) {
  13742. + param->is_yaffs2 = 1;
  13743. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13744. + param->total_bytes_per_chunk = mtd->writesize;
  13745. + param->chunks_per_block = mtd->erasesize / mtd->writesize;
  13746. +#else
  13747. + param->total_bytes_per_chunk = mtd->oobblock;
  13748. + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
  13749. +#endif
  13750. + n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  13751. +
  13752. + param->start_block = 0;
  13753. + param->end_block = n_blocks - 1;
  13754. + } else {
  13755. + param->is_yaffs2 = 0;
  13756. + n_blocks = YCALCBLOCKS(mtd->size,
  13757. + YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
  13758. +
  13759. + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
  13760. + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
  13761. + }
  13762. +
  13763. + param->start_block = 0;
  13764. + param->end_block = n_blocks - 1;
  13765. +
  13766. + yaffs_mtd_drv_install(dev);
  13767. +
  13768. + param->sb_dirty_fn = yaffs_set_super_dirty;
  13769. + param->gc_control_fn = yaffs_gc_control_callback;
  13770. +
  13771. + yaffs_dev_to_lc(dev)->super = sb;
  13772. +
  13773. + param->use_nand_ecc = 1;
  13774. +
  13775. + param->skip_checkpt_rd = options.skip_checkpoint_read;
  13776. + param->skip_checkpt_wr = options.skip_checkpoint_write;
  13777. +
  13778. + mutex_lock(&yaffs_context_lock);
  13779. + /* Get a mount id */
  13780. + found = 0;
  13781. + for (mount_id = 0; !found; mount_id++) {
  13782. + found = 1;
  13783. + list_for_each(l, &yaffs_context_list) {
  13784. + context_iterator =
  13785. + list_entry(l, struct yaffs_linux_context,
  13786. + context_list);
  13787. + if (context_iterator->mount_id == mount_id)
  13788. + found = 0;
  13789. + }
  13790. + }
  13791. + context->mount_id = mount_id;
  13792. +
  13793. + list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
  13794. + &yaffs_context_list);
  13795. + mutex_unlock(&yaffs_context_lock);
  13796. +
  13797. + /* Directory search handling... */
  13798. + INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
  13799. + param->remove_obj_fn = yaffs_remove_obj_callback;
  13800. +
  13801. + mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
  13802. +
  13803. + yaffs_gross_lock(dev);
  13804. +
  13805. + err = yaffs_guts_initialise(dev);
  13806. +
  13807. + yaffs_trace(YAFFS_TRACE_OS,
  13808. + "yaffs_read_super: guts initialised %s",
  13809. + (err == YAFFS_OK) ? "OK" : "FAILED");
  13810. +
  13811. + if (err == YAFFS_OK)
  13812. + yaffs_bg_start(dev);
  13813. +
  13814. + if (!context->bg_thread)
  13815. + param->defered_dir_update = 0;
  13816. +
  13817. + sb->s_maxbytes = yaffs_max_file_size(dev);
  13818. +
  13819. + /* Release lock before yaffs_get_inode() */
  13820. + yaffs_gross_unlock(dev);
  13821. +
  13822. + /* Create root inode */
  13823. + if (err == YAFFS_OK)
  13824. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
  13825. +
  13826. + if (!inode)
  13827. + return NULL;
  13828. +
  13829. + inode->i_op = &yaffs_dir_inode_operations;
  13830. + inode->i_fop = &yaffs_dir_operations;
  13831. +
  13832. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
  13833. +
  13834. + root = yaffs_make_root(inode);
  13835. +
  13836. + if (!root)
  13837. + return NULL;
  13838. +
  13839. + sb->s_root = root;
  13840. + if(!dev->is_checkpointed)
  13841. + yaffs_set_super_dirty(dev);
  13842. +
  13843. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13844. + "yaffs_read_super: is_checkpointed %d",
  13845. + dev->is_checkpointed);
  13846. +
  13847. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
  13848. + return sb;
  13849. +}
  13850. +
  13851. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13852. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  13853. + int silent)
  13854. +{
  13855. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  13856. +}
  13857. +
  13858. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13859. +static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
  13860. + const char *dev_name, void *data)
  13861. +{
  13862. + return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
  13863. +}
  13864. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13865. +static int yaffs_read_super(struct file_system_type *fs,
  13866. + int flags, const char *dev_name,
  13867. + void *data, struct vfsmount *mnt)
  13868. +{
  13869. +
  13870. + return get_sb_bdev(fs, flags, dev_name, data,
  13871. + yaffs_internal_read_super_mtd, mnt);
  13872. +}
  13873. +#else
  13874. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  13875. + int flags, const char *dev_name,
  13876. + void *data)
  13877. +{
  13878. +
  13879. + return get_sb_bdev(fs, flags, dev_name, data,
  13880. + yaffs_internal_read_super_mtd);
  13881. +}
  13882. +#endif
  13883. +
  13884. +static struct file_system_type yaffs_fs_type = {
  13885. + .owner = THIS_MODULE,
  13886. + .name = "yaffs",
  13887. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13888. + .mount = yaffs_mount,
  13889. +#else
  13890. + .get_sb = yaffs_read_super,
  13891. +#endif
  13892. + .kill_sb = kill_block_super,
  13893. + .fs_flags = FS_REQUIRES_DEV,
  13894. +};
  13895. +#else
  13896. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  13897. + int silent)
  13898. +{
  13899. + return yaffs_internal_read_super(1, sb, data, silent);
  13900. +}
  13901. +
  13902. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  13903. + FS_REQUIRES_DEV);
  13904. +#endif
  13905. +
  13906. +
  13907. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13908. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  13909. + int silent)
  13910. +{
  13911. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  13912. +}
  13913. +
  13914. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13915. +static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
  13916. + const char *dev_name, void *data)
  13917. +{
  13918. + return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
  13919. +}
  13920. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13921. +static int yaffs2_read_super(struct file_system_type *fs,
  13922. + int flags, const char *dev_name, void *data,
  13923. + struct vfsmount *mnt)
  13924. +{
  13925. + return get_sb_bdev(fs, flags, dev_name, data,
  13926. + yaffs2_internal_read_super_mtd, mnt);
  13927. +}
  13928. +#else
  13929. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  13930. + int flags, const char *dev_name,
  13931. + void *data)
  13932. +{
  13933. +
  13934. + return get_sb_bdev(fs, flags, dev_name, data,
  13935. + yaffs2_internal_read_super_mtd);
  13936. +}
  13937. +#endif
  13938. +
  13939. +static struct file_system_type yaffs2_fs_type = {
  13940. + .owner = THIS_MODULE,
  13941. + .name = "yaffs2",
  13942. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13943. + .mount = yaffs2_mount,
  13944. +#else
  13945. + .get_sb = yaffs2_read_super,
  13946. +#endif
  13947. + .kill_sb = kill_block_super,
  13948. + .fs_flags = FS_REQUIRES_DEV,
  13949. +};
  13950. +#else
  13951. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  13952. + void *data, int silent)
  13953. +{
  13954. + return yaffs_internal_read_super(2, sb, data, silent);
  13955. +}
  13956. +
  13957. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  13958. + FS_REQUIRES_DEV);
  13959. +#endif
  13960. +
  13961. +
  13962. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  13963. +static struct proc_dir_entry *my_proc_entry;
  13964. +
  13965. +static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
  13966. +{
  13967. + struct yaffs_param *param = &dev->param;
  13968. + int bs[10];
  13969. +
  13970. + yaffs_count_blocks_by_state(dev,bs);
  13971. +
  13972. + buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
  13973. + buf += sprintf(buf, "end_block............ %d\n", param->end_block);
  13974. + buf += sprintf(buf, "total_bytes_per_chunk %d\n",
  13975. + param->total_bytes_per_chunk);
  13976. + buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
  13977. + buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
  13978. + buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
  13979. + buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
  13980. + buf += sprintf(buf, "empty_lost_n_found... %d\n",
  13981. + param->empty_lost_n_found);
  13982. + buf += sprintf(buf, "disable_lazy_load.... %d\n",
  13983. + param->disable_lazy_load);
  13984. + buf += sprintf(buf, "disable_bad_block_mrk %d\n",
  13985. + param->disable_bad_block_marking);
  13986. + buf += sprintf(buf, "refresh_period....... %d\n",
  13987. + param->refresh_period);
  13988. + buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
  13989. + buf += sprintf(buf, "n_reserved_blocks.... %d\n",
  13990. + param->n_reserved_blocks);
  13991. + buf += sprintf(buf, "always_check_erased.. %d\n",
  13992. + param->always_check_erased);
  13993. + buf += sprintf(buf, "\n");
  13994. + buf += sprintf(buf, "block count by state\n");
  13995. + buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
  13996. + bs[0], bs[1], bs[2], bs[3], bs[4]);
  13997. + buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
  13998. + bs[5], bs[6], bs[7], bs[8], bs[9]);
  13999. +
  14000. + return buf;
  14001. +}
  14002. +
  14003. +static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
  14004. +{
  14005. + buf += sprintf(buf, "max file size....... %lld\n",
  14006. + (long long) yaffs_max_file_size(dev));
  14007. + buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
  14008. + dev->data_bytes_per_chunk);
  14009. + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
  14010. + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
  14011. + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
  14012. + buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
  14013. + dev->blocks_in_checkpt);
  14014. + buf += sprintf(buf, "\n");
  14015. + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
  14016. + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
  14017. + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
  14018. + buf += sprintf(buf, "\n");
  14019. + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
  14020. + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
  14021. + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
  14022. + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
  14023. + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
  14024. + buf += sprintf(buf, "passive_gc_count..... %u\n",
  14025. + dev->passive_gc_count);
  14026. + buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
  14027. + dev->oldest_dirty_gc_count);
  14028. + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
  14029. + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
  14030. + buf += sprintf(buf, "n_retried_writes..... %u\n",
  14031. + dev->n_retried_writes);
  14032. + buf += sprintf(buf, "n_retired_blocks..... %u\n",
  14033. + dev->n_retired_blocks);
  14034. + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
  14035. + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
  14036. + buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
  14037. + dev->n_tags_ecc_fixed);
  14038. + buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
  14039. + dev->n_tags_ecc_unfixed);
  14040. + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
  14041. + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
  14042. + buf += sprintf(buf, "n_unlinked_files..... %u\n",
  14043. + dev->n_unlinked_files);
  14044. + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
  14045. + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
  14046. + buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
  14047. + buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
  14048. +
  14049. + return buf;
  14050. +}
  14051. +
  14052. +static int yaffs_proc_read(char *page,
  14053. + char **start,
  14054. + off_t offset, int count, int *eof, void *data)
  14055. +{
  14056. + struct list_head *item;
  14057. + char *buf = page;
  14058. + int step = offset;
  14059. + int n = 0;
  14060. +
  14061. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  14062. + * We use 'offset' (*ppos) to indicate where we are in dev_list.
  14063. + * This also assumes the user has posted a read buffer large
  14064. + * enough to hold the complete output; but that's life in /proc.
  14065. + */
  14066. +
  14067. + *(int *)start = 1;
  14068. +
  14069. + /* Print header first */
  14070. + if (step == 0)
  14071. + buf +=
  14072. + sprintf(buf,
  14073. + "Multi-version YAFFS built:" __DATE__ " " __TIME__
  14074. + "\n");
  14075. + else if (step == 1)
  14076. + buf += sprintf(buf, "\n");
  14077. + else {
  14078. + step -= 2;
  14079. +
  14080. + mutex_lock(&yaffs_context_lock);
  14081. +
  14082. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14083. + list_for_each(item, &yaffs_context_list) {
  14084. + struct yaffs_linux_context *dc =
  14085. + list_entry(item, struct yaffs_linux_context,
  14086. + context_list);
  14087. + struct yaffs_dev *dev = dc->dev;
  14088. +
  14089. + if (n < (step & ~1)) {
  14090. + n += 2;
  14091. + continue;
  14092. + }
  14093. + if ((step & 1) == 0) {
  14094. + buf +=
  14095. + sprintf(buf, "\nDevice %d \"%s\"\n", n,
  14096. + dev->param.name);
  14097. + buf = yaffs_dump_dev_part0(buf, dev);
  14098. + } else {
  14099. + buf = yaffs_dump_dev_part1(buf, dev);
  14100. + }
  14101. +
  14102. + break;
  14103. + }
  14104. + mutex_unlock(&yaffs_context_lock);
  14105. + }
  14106. +
  14107. + return buf - page < count ? buf - page : count;
  14108. +}
  14109. +
  14110. +/**
  14111. + * Set the verbosity of the warnings and error messages.
  14112. + *
  14113. + * Note that the names can only be a..z or _ with the current code.
  14114. + */
  14115. +
  14116. +static struct {
  14117. + char *mask_name;
  14118. + unsigned mask_bitfield;
  14119. +} mask_flags[] = {
  14120. + {"allocate", YAFFS_TRACE_ALLOCATE},
  14121. + {"always", YAFFS_TRACE_ALWAYS},
  14122. + {"background", YAFFS_TRACE_BACKGROUND},
  14123. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  14124. + {"buffers", YAFFS_TRACE_BUFFERS},
  14125. + {"bug", YAFFS_TRACE_BUG},
  14126. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  14127. + {"deletion", YAFFS_TRACE_DELETION},
  14128. + {"erase", YAFFS_TRACE_ERASE},
  14129. + {"error", YAFFS_TRACE_ERROR},
  14130. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  14131. + {"gc", YAFFS_TRACE_GC},
  14132. + {"lock", YAFFS_TRACE_LOCK},
  14133. + {"mtd", YAFFS_TRACE_MTD},
  14134. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  14135. + {"os", YAFFS_TRACE_OS},
  14136. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  14137. + {"scan", YAFFS_TRACE_SCAN},
  14138. + {"mount", YAFFS_TRACE_MOUNT},
  14139. + {"tracing", YAFFS_TRACE_TRACING},
  14140. + {"sync", YAFFS_TRACE_SYNC},
  14141. + {"write", YAFFS_TRACE_WRITE},
  14142. + {"verify", YAFFS_TRACE_VERIFY},
  14143. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  14144. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  14145. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  14146. + {"all", 0xffffffff},
  14147. + {"none", 0},
  14148. + {NULL, 0},
  14149. +};
  14150. +
  14151. +#define MAX_MASK_NAME_LENGTH 40
  14152. +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
  14153. + unsigned long count, void *data)
  14154. +{
  14155. + unsigned rg = 0, mask_bitfield;
  14156. + char *end;
  14157. + char *mask_name;
  14158. + const char *x;
  14159. + char substring[MAX_MASK_NAME_LENGTH + 1];
  14160. + int i;
  14161. + int done = 0;
  14162. + int add, len = 0;
  14163. + int pos = 0;
  14164. +
  14165. + rg = yaffs_trace_mask;
  14166. +
  14167. + while (!done && (pos < count)) {
  14168. + done = 1;
  14169. + while ((pos < count) && isspace(buf[pos]))
  14170. + pos++;
  14171. +
  14172. + switch (buf[pos]) {
  14173. + case '+':
  14174. + case '-':
  14175. + case '=':
  14176. + add = buf[pos];
  14177. + pos++;
  14178. + break;
  14179. +
  14180. + default:
  14181. + add = ' ';
  14182. + break;
  14183. + }
  14184. + mask_name = NULL;
  14185. +
  14186. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  14187. +
  14188. + if (end > buf + pos) {
  14189. + mask_name = "numeral";
  14190. + len = end - (buf + pos);
  14191. + pos += len;
  14192. + done = 0;
  14193. + } else {
  14194. + for (x = buf + pos, i = 0;
  14195. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  14196. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  14197. + substring[i] = *x;
  14198. + substring[i] = '\0';
  14199. +
  14200. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14201. + if (strcmp(substring, mask_flags[i].mask_name)
  14202. + == 0) {
  14203. + mask_name = mask_flags[i].mask_name;
  14204. + mask_bitfield =
  14205. + mask_flags[i].mask_bitfield;
  14206. + done = 0;
  14207. + break;
  14208. + }
  14209. + }
  14210. + }
  14211. +
  14212. + if (mask_name != NULL) {
  14213. + done = 0;
  14214. + switch (add) {
  14215. + case '-':
  14216. + rg &= ~mask_bitfield;
  14217. + break;
  14218. + case '+':
  14219. + rg |= mask_bitfield;
  14220. + break;
  14221. + case '=':
  14222. + rg = mask_bitfield;
  14223. + break;
  14224. + default:
  14225. + rg |= mask_bitfield;
  14226. + break;
  14227. + }
  14228. + }
  14229. + }
  14230. +
  14231. + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
  14232. +
  14233. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
  14234. +
  14235. + if (rg & YAFFS_TRACE_ALWAYS) {
  14236. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14237. + char flag;
  14238. + flag = ((rg & mask_flags[i].mask_bitfield) ==
  14239. + mask_flags[i].mask_bitfield) ? '+' : '-';
  14240. + printk(KERN_DEBUG "%c%s\n", flag,
  14241. + mask_flags[i].mask_name);
  14242. + }
  14243. + }
  14244. +
  14245. + return count;
  14246. +}
  14247. +
  14248. +/* Debug strings are of the form:
  14249. + * .bnnn print info on block n
  14250. + * .cobjn,chunkn print nand chunk id for objn:chunkn
  14251. + */
  14252. +
  14253. +static int yaffs_proc_debug_write(struct file *file, const char *buf,
  14254. + unsigned long count, void *data)
  14255. +{
  14256. +
  14257. + char str[100];
  14258. + char *p0;
  14259. + char *p1;
  14260. + long p1_val;
  14261. + long p0_val;
  14262. + char cmd;
  14263. + struct list_head *item;
  14264. +
  14265. + memset(str, 0, sizeof(str));
  14266. + memcpy(str, buf, min(count, sizeof(str) -1));
  14267. +
  14268. + cmd = str[1];
  14269. +
  14270. + p0 = str + 2;
  14271. +
  14272. + p1 = p0;
  14273. +
  14274. + while (*p1 && *p1 != ',') {
  14275. + p1++;
  14276. + }
  14277. + *p1 = '\0';
  14278. + p1++;
  14279. +
  14280. + p0_val = simple_strtol(p0, NULL, 0);
  14281. + p1_val = simple_strtol(p1, NULL, 0);
  14282. +
  14283. +
  14284. + mutex_lock(&yaffs_context_lock);
  14285. +
  14286. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14287. + list_for_each(item, &yaffs_context_list) {
  14288. + struct yaffs_linux_context *dc =
  14289. + list_entry(item, struct yaffs_linux_context,
  14290. + context_list);
  14291. + struct yaffs_dev *dev = dc->dev;
  14292. +
  14293. + if (cmd == 'b') {
  14294. + struct yaffs_block_info *bi;
  14295. +
  14296. + bi = yaffs_get_block_info(dev,p0_val);
  14297. +
  14298. + if(bi) {
  14299. + printk("Block %d: state %d, retire %d, use %d, seq %d\n",
  14300. + (int)p0_val, bi->block_state,
  14301. + bi->needs_retiring, bi->pages_in_use,
  14302. + bi->seq_number);
  14303. + }
  14304. + } else if (cmd == 'c') {
  14305. + struct yaffs_obj *obj;
  14306. + int nand_chunk;
  14307. +
  14308. + obj = yaffs_find_by_number(dev, p0_val);
  14309. + if (!obj)
  14310. + printk("No obj %d\n", (int)p0_val);
  14311. + else {
  14312. + if(p1_val == 0)
  14313. + nand_chunk = obj->hdr_chunk;
  14314. + else
  14315. + nand_chunk =
  14316. + yaffs_find_chunk_in_file(obj,
  14317. + p1_val, NULL);
  14318. + printk("Nand chunk for %d:%d is %d\n",
  14319. + (int)p0_val, (int)p1_val, nand_chunk);
  14320. + }
  14321. + }
  14322. + }
  14323. +
  14324. + mutex_unlock(&yaffs_context_lock);
  14325. +
  14326. + return count;
  14327. +}
  14328. +
  14329. +static int yaffs_proc_write(struct file *file, const char *buf,
  14330. + unsigned long count, void *data)
  14331. +{
  14332. + if (buf[0] == '.')
  14333. + return yaffs_proc_debug_write(file, buf, count, data);
  14334. + return yaffs_proc_write_trace_options(file, buf, count, data);
  14335. +}
  14336. +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) */
  14337. +
  14338. +/* Stuff to handle installation of file systems */
  14339. +struct file_system_to_install {
  14340. + struct file_system_type *fst;
  14341. + int installed;
  14342. +};
  14343. +
  14344. +static struct file_system_to_install fs_to_install[] = {
  14345. + {&yaffs_fs_type, 0},
  14346. + {&yaffs2_fs_type, 0},
  14347. + {NULL, 0}
  14348. +};
  14349. +
  14350. +static int __init init_yaffs_fs(void)
  14351. +{
  14352. + int error = 0;
  14353. + struct file_system_to_install *fsinst;
  14354. +
  14355. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  14356. + "yaffs built " __DATE__ " " __TIME__ " Installing.");
  14357. +
  14358. + mutex_init(&yaffs_context_lock);
  14359. +
  14360. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  14361. + /* Install the proc_fs entries */
  14362. + my_proc_entry = create_proc_entry("yaffs",
  14363. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14364. +
  14365. + if (my_proc_entry) {
  14366. + my_proc_entry->write_proc = yaffs_proc_write;
  14367. + my_proc_entry->read_proc = yaffs_proc_read;
  14368. + my_proc_entry->data = NULL;
  14369. + } else {
  14370. + return -ENOMEM;
  14371. + }
  14372. +#endif
  14373. +
  14374. + /* Now add the file system entries */
  14375. +
  14376. + fsinst = fs_to_install;
  14377. +
  14378. + while (fsinst->fst && !error) {
  14379. + error = register_filesystem(fsinst->fst);
  14380. + if (!error)
  14381. + fsinst->installed = 1;
  14382. + fsinst++;
  14383. + }
  14384. +
  14385. + /* Any errors? uninstall */
  14386. + if (error) {
  14387. + fsinst = fs_to_install;
  14388. +
  14389. + while (fsinst->fst) {
  14390. + if (fsinst->installed) {
  14391. + unregister_filesystem(fsinst->fst);
  14392. + fsinst->installed = 0;
  14393. + }
  14394. + fsinst++;
  14395. + }
  14396. + }
  14397. +
  14398. + return error;
  14399. +}
  14400. +
  14401. +static void __exit exit_yaffs_fs(void)
  14402. +{
  14403. +
  14404. + struct file_system_to_install *fsinst;
  14405. +
  14406. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  14407. + "yaffs built " __DATE__ " " __TIME__ " removing.");
  14408. +
  14409. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  14410. + remove_proc_entry("yaffs", YPROC_ROOT);
  14411. +#endif
  14412. +
  14413. + fsinst = fs_to_install;
  14414. +
  14415. + while (fsinst->fst) {
  14416. + if (fsinst->installed) {
  14417. + unregister_filesystem(fsinst->fst);
  14418. + fsinst->installed = 0;
  14419. + }
  14420. + fsinst++;
  14421. + }
  14422. +}
  14423. +
  14424. +module_init(init_yaffs_fs)
  14425. + module_exit(exit_yaffs_fs)
  14426. +
  14427. + MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  14428. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
  14429. +MODULE_LICENSE("GPL");
  14430. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs1.c linux-3.15-rc5/fs/yaffs2/yaffs_yaffs1.c
  14431. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs1.c 1970-01-01 01:00:00.000000000 +0100
  14432. +++ linux-3.15-rc5/fs/yaffs2/yaffs_yaffs1.c 2014-05-17 01:53:27.000000000 +0200
  14433. @@ -0,0 +1,422 @@
  14434. +/*
  14435. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14436. + *
  14437. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14438. + * for Toby Churchill Ltd and Brightstar Engineering
  14439. + *
  14440. + * Created by Charles Manning <charles@aleph1.co.uk>
  14441. + *
  14442. + * This program is free software; you can redistribute it and/or modify
  14443. + * it under the terms of the GNU General Public License version 2 as
  14444. + * published by the Free Software Foundation.
  14445. + */
  14446. +
  14447. +#include "yaffs_yaffs1.h"
  14448. +#include "yportenv.h"
  14449. +#include "yaffs_trace.h"
  14450. +#include "yaffs_bitmap.h"
  14451. +#include "yaffs_getblockinfo.h"
  14452. +#include "yaffs_nand.h"
  14453. +#include "yaffs_attribs.h"
  14454. +
  14455. +int yaffs1_scan(struct yaffs_dev *dev)
  14456. +{
  14457. + struct yaffs_ext_tags tags;
  14458. + int blk;
  14459. + int result;
  14460. + int chunk;
  14461. + int c;
  14462. + int deleted;
  14463. + enum yaffs_block_state state;
  14464. + LIST_HEAD(hard_list);
  14465. + struct yaffs_block_info *bi;
  14466. + u32 seq_number;
  14467. + struct yaffs_obj_hdr *oh;
  14468. + struct yaffs_obj *in;
  14469. + struct yaffs_obj *parent;
  14470. + int alloc_failed = 0;
  14471. + struct yaffs_shadow_fixer *shadow_fixers = NULL;
  14472. + u8 *chunk_data;
  14473. +
  14474. + yaffs_trace(YAFFS_TRACE_SCAN,
  14475. + "yaffs1_scan starts intstartblk %d intendblk %d...",
  14476. + dev->internal_start_block, dev->internal_end_block);
  14477. +
  14478. + chunk_data = yaffs_get_temp_buffer(dev);
  14479. +
  14480. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  14481. +
  14482. + /* Scan all the blocks to determine their state */
  14483. + bi = dev->block_info;
  14484. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  14485. + blk++) {
  14486. + yaffs_clear_chunk_bits(dev, blk);
  14487. + bi->pages_in_use = 0;
  14488. + bi->soft_del_pages = 0;
  14489. +
  14490. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  14491. +
  14492. + bi->block_state = state;
  14493. + bi->seq_number = seq_number;
  14494. +
  14495. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  14496. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  14497. +
  14498. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  14499. + "Block scanning block %d state %d seq %d",
  14500. + blk, state, seq_number);
  14501. +
  14502. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  14503. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  14504. + "block %d is bad", blk);
  14505. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  14506. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  14507. + dev->n_erased_blocks++;
  14508. + dev->n_free_chunks += dev->param.chunks_per_block;
  14509. + }
  14510. + bi++;
  14511. + }
  14512. +
  14513. + /* For each block.... */
  14514. + for (blk = dev->internal_start_block;
  14515. + !alloc_failed && blk <= dev->internal_end_block; blk++) {
  14516. +
  14517. + cond_resched();
  14518. +
  14519. + bi = yaffs_get_block_info(dev, blk);
  14520. + state = bi->block_state;
  14521. +
  14522. + deleted = 0;
  14523. +
  14524. + /* For each chunk in each block that needs scanning.... */
  14525. + for (c = 0;
  14526. + !alloc_failed && c < dev->param.chunks_per_block &&
  14527. + state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
  14528. + /* Read the tags and decide what to do */
  14529. + chunk = blk * dev->param.chunks_per_block + c;
  14530. +
  14531. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  14532. + &tags);
  14533. +
  14534. + /* Let's have a good look at this chunk... */
  14535. +
  14536. + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
  14537. + tags.is_deleted) {
  14538. + /* YAFFS1 only...
  14539. + * A deleted chunk
  14540. + */
  14541. + deleted++;
  14542. + dev->n_free_chunks++;
  14543. + } else if (!tags.chunk_used) {
  14544. + /* An unassigned chunk in the block
  14545. + * This means that either the block is empty or
  14546. + * this is the one being allocated from
  14547. + */
  14548. +
  14549. + if (c == 0) {
  14550. + /* We're looking at the first chunk in
  14551. + *the block so the block is unused */
  14552. + state = YAFFS_BLOCK_STATE_EMPTY;
  14553. + dev->n_erased_blocks++;
  14554. + } else {
  14555. + /* this is the block being allocated */
  14556. + yaffs_trace(YAFFS_TRACE_SCAN,
  14557. + " Allocating from %d %d",
  14558. + blk, c);
  14559. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  14560. + dev->alloc_block = blk;
  14561. + dev->alloc_page = c;
  14562. + dev->alloc_block_finder = blk;
  14563. +
  14564. + }
  14565. +
  14566. + dev->n_free_chunks +=
  14567. + (dev->param.chunks_per_block - c);
  14568. + } else if (tags.chunk_id > 0) {
  14569. + /* chunk_id > 0 so it is a data chunk... */
  14570. + unsigned int endpos;
  14571. +
  14572. + yaffs_set_chunk_bit(dev, blk, c);
  14573. + bi->pages_in_use++;
  14574. +
  14575. + in = yaffs_find_or_create_by_number(dev,
  14576. + tags.obj_id,
  14577. + YAFFS_OBJECT_TYPE_FILE);
  14578. + /* PutChunkIntoFile checks for a clash
  14579. + * (two data chunks with the same chunk_id).
  14580. + */
  14581. +
  14582. + if (!in)
  14583. + alloc_failed = 1;
  14584. +
  14585. + if (in) {
  14586. + if (!yaffs_put_chunk_in_file
  14587. + (in, tags.chunk_id, chunk, 1))
  14588. + alloc_failed = 1;
  14589. + }
  14590. +
  14591. + endpos =
  14592. + (tags.chunk_id - 1) *
  14593. + dev->data_bytes_per_chunk +
  14594. + tags.n_bytes;
  14595. + if (in &&
  14596. + in->variant_type ==
  14597. + YAFFS_OBJECT_TYPE_FILE &&
  14598. + in->variant.file_variant.scanned_size <
  14599. + endpos) {
  14600. + in->variant.file_variant.scanned_size =
  14601. + endpos;
  14602. + if (!dev->param.use_header_file_size) {
  14603. + in->variant.
  14604. + file_variant.file_size =
  14605. + in->variant.
  14606. + file_variant.scanned_size;
  14607. + }
  14608. +
  14609. + }
  14610. + } else {
  14611. + /* chunk_id == 0, so it is an ObjectHeader.
  14612. + * Make the object
  14613. + */
  14614. + yaffs_set_chunk_bit(dev, blk, c);
  14615. + bi->pages_in_use++;
  14616. +
  14617. + result = yaffs_rd_chunk_tags_nand(dev, chunk,
  14618. + chunk_data,
  14619. + NULL);
  14620. +
  14621. + oh = (struct yaffs_obj_hdr *)chunk_data;
  14622. +
  14623. + in = yaffs_find_by_number(dev, tags.obj_id);
  14624. + if (in && in->variant_type != oh->type) {
  14625. + /* This should not happen, but somehow
  14626. + * Wev'e ended up with an obj_id that
  14627. + * has been reused but not yet deleted,
  14628. + * and worse still it has changed type.
  14629. + * Delete the old object.
  14630. + */
  14631. +
  14632. + yaffs_del_obj(in);
  14633. + in = NULL;
  14634. + }
  14635. +
  14636. + in = yaffs_find_or_create_by_number(dev,
  14637. + tags.obj_id,
  14638. + oh->type);
  14639. +
  14640. + if (!in)
  14641. + alloc_failed = 1;
  14642. +
  14643. + if (in && oh->shadows_obj > 0) {
  14644. +
  14645. + struct yaffs_shadow_fixer *fixer;
  14646. + fixer =
  14647. + kmalloc(sizeof
  14648. + (struct yaffs_shadow_fixer),
  14649. + GFP_NOFS);
  14650. + if (fixer) {
  14651. + fixer->next = shadow_fixers;
  14652. + shadow_fixers = fixer;
  14653. + fixer->obj_id = tags.obj_id;
  14654. + fixer->shadowed_id =
  14655. + oh->shadows_obj;
  14656. + yaffs_trace(YAFFS_TRACE_SCAN,
  14657. + " Shadow fixer: %d shadows %d",
  14658. + fixer->obj_id,
  14659. + fixer->shadowed_id);
  14660. +
  14661. + }
  14662. +
  14663. + }
  14664. +
  14665. + if (in && in->valid) {
  14666. + /* We have already filled this one.
  14667. + * We have a duplicate and need to
  14668. + * resolve it. */
  14669. +
  14670. + unsigned existing_serial = in->serial;
  14671. + unsigned new_serial =
  14672. + tags.serial_number;
  14673. +
  14674. + if (((existing_serial + 1) & 3) ==
  14675. + new_serial) {
  14676. + /* Use new one - destroy the
  14677. + * exisiting one */
  14678. + yaffs_chunk_del(dev,
  14679. + in->hdr_chunk,
  14680. + 1, __LINE__);
  14681. + in->valid = 0;
  14682. + } else {
  14683. + /* Use existing - destroy
  14684. + * this one. */
  14685. + yaffs_chunk_del(dev, chunk, 1,
  14686. + __LINE__);
  14687. + }
  14688. + }
  14689. +
  14690. + if (in && !in->valid &&
  14691. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  14692. + tags.obj_id ==
  14693. + YAFFS_OBJECTID_LOSTNFOUND)) {
  14694. + /* We only load some info, don't fiddle
  14695. + * with directory structure */
  14696. + in->valid = 1;
  14697. + in->variant_type = oh->type;
  14698. +
  14699. + in->yst_mode = oh->yst_mode;
  14700. + yaffs_load_attribs(in, oh);
  14701. + in->hdr_chunk = chunk;
  14702. + in->serial = tags.serial_number;
  14703. +
  14704. + } else if (in && !in->valid) {
  14705. + /* we need to load this info */
  14706. +
  14707. + in->valid = 1;
  14708. + in->variant_type = oh->type;
  14709. +
  14710. + in->yst_mode = oh->yst_mode;
  14711. + yaffs_load_attribs(in, oh);
  14712. + in->hdr_chunk = chunk;
  14713. + in->serial = tags.serial_number;
  14714. +
  14715. + yaffs_set_obj_name_from_oh(in, oh);
  14716. + in->dirty = 0;
  14717. +
  14718. + /* directory stuff...
  14719. + * hook up to parent
  14720. + */
  14721. +
  14722. + parent =
  14723. + yaffs_find_or_create_by_number
  14724. + (dev, oh->parent_obj_id,
  14725. + YAFFS_OBJECT_TYPE_DIRECTORY);
  14726. + if (!parent)
  14727. + alloc_failed = 1;
  14728. + if (parent && parent->variant_type ==
  14729. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  14730. + /* Set up as a directory */
  14731. + parent->variant_type =
  14732. + YAFFS_OBJECT_TYPE_DIRECTORY;
  14733. + INIT_LIST_HEAD(&parent->
  14734. + variant.dir_variant.
  14735. + children);
  14736. + } else if (!parent ||
  14737. + parent->variant_type !=
  14738. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  14739. + /* Hoosterman, a problem....
  14740. + * We're trying to use a
  14741. + * non-directory as a directory
  14742. + */
  14743. +
  14744. + yaffs_trace(YAFFS_TRACE_ERROR,
  14745. + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  14746. + );
  14747. + parent = dev->lost_n_found;
  14748. + }
  14749. +
  14750. + yaffs_add_obj_to_dir(parent, in);
  14751. +
  14752. + switch (in->variant_type) {
  14753. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  14754. + /* Todo got a problem */
  14755. + break;
  14756. + case YAFFS_OBJECT_TYPE_FILE:
  14757. + if (dev->param.
  14758. + use_header_file_size)
  14759. + in->variant.
  14760. + file_variant.file_size
  14761. + = yaffs_oh_to_size(oh);
  14762. + break;
  14763. + case YAFFS_OBJECT_TYPE_HARDLINK:
  14764. + in->variant.
  14765. + hardlink_variant.equiv_id =
  14766. + oh->equiv_id;
  14767. + list_add(&in->hard_links,
  14768. + &hard_list);
  14769. + break;
  14770. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  14771. + /* Do nothing */
  14772. + break;
  14773. + case YAFFS_OBJECT_TYPE_SPECIAL:
  14774. + /* Do nothing */
  14775. + break;
  14776. + case YAFFS_OBJECT_TYPE_SYMLINK:
  14777. + in->variant.symlink_variant.
  14778. + alias =
  14779. + yaffs_clone_str(oh->alias);
  14780. + if (!in->variant.
  14781. + symlink_variant.alias)
  14782. + alloc_failed = 1;
  14783. + break;
  14784. + }
  14785. + }
  14786. + }
  14787. + }
  14788. +
  14789. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  14790. + /* If we got this far while scanning,
  14791. + * then the block is fully allocated. */
  14792. + state = YAFFS_BLOCK_STATE_FULL;
  14793. + }
  14794. +
  14795. + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  14796. + /* If the block was partially allocated then
  14797. + * treat it as fully allocated. */
  14798. + state = YAFFS_BLOCK_STATE_FULL;
  14799. + dev->alloc_block = -1;
  14800. + }
  14801. +
  14802. + bi->block_state = state;
  14803. +
  14804. + /* Now let's see if it was dirty */
  14805. + if (bi->pages_in_use == 0 &&
  14806. + !bi->has_shrink_hdr &&
  14807. + bi->block_state == YAFFS_BLOCK_STATE_FULL)
  14808. + yaffs_block_became_dirty(dev, blk);
  14809. + }
  14810. +
  14811. + /* Ok, we've done all the scanning.
  14812. + * Fix up the hard link chains.
  14813. + * We should now have scanned all the objects, now it's time to add
  14814. + * these hardlinks.
  14815. + */
  14816. +
  14817. + yaffs_link_fixup(dev, &hard_list);
  14818. +
  14819. + /*
  14820. + * Fix up any shadowed objects.
  14821. + * There should not be more than one of these.
  14822. + */
  14823. + {
  14824. + struct yaffs_shadow_fixer *fixer;
  14825. + struct yaffs_obj *obj;
  14826. +
  14827. + while (shadow_fixers) {
  14828. + fixer = shadow_fixers;
  14829. + shadow_fixers = fixer->next;
  14830. + /* Complete the rename transaction by deleting the
  14831. + * shadowed object then setting the object header
  14832. + to unshadowed.
  14833. + */
  14834. + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  14835. + if (obj)
  14836. + yaffs_del_obj(obj);
  14837. +
  14838. + obj = yaffs_find_by_number(dev, fixer->obj_id);
  14839. +
  14840. + if (obj)
  14841. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  14842. +
  14843. + kfree(fixer);
  14844. + }
  14845. + }
  14846. +
  14847. + yaffs_release_temp_buffer(dev, chunk_data);
  14848. +
  14849. + if (alloc_failed)
  14850. + return YAFFS_FAIL;
  14851. +
  14852. + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  14853. +
  14854. + return YAFFS_OK;
  14855. +}
  14856. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs1.h linux-3.15-rc5/fs/yaffs2/yaffs_yaffs1.h
  14857. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs1.h 1970-01-01 01:00:00.000000000 +0100
  14858. +++ linux-3.15-rc5/fs/yaffs2/yaffs_yaffs1.h 2014-05-17 01:53:27.000000000 +0200
  14859. @@ -0,0 +1,22 @@
  14860. +/*
  14861. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14862. + *
  14863. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14864. + * for Toby Churchill Ltd and Brightstar Engineering
  14865. + *
  14866. + * Created by Charles Manning <charles@aleph1.co.uk>
  14867. + *
  14868. + * This program is free software; you can redistribute it and/or modify
  14869. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14870. + * published by the Free Software Foundation.
  14871. + *
  14872. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14873. + */
  14874. +
  14875. +#ifndef __YAFFS_YAFFS1_H__
  14876. +#define __YAFFS_YAFFS1_H__
  14877. +
  14878. +#include "yaffs_guts.h"
  14879. +int yaffs1_scan(struct yaffs_dev *dev);
  14880. +
  14881. +#endif
  14882. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs2.c linux-3.15-rc5/fs/yaffs2/yaffs_yaffs2.c
  14883. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs2.c 1970-01-01 01:00:00.000000000 +0100
  14884. +++ linux-3.15-rc5/fs/yaffs2/yaffs_yaffs2.c 2014-05-17 01:53:27.000000000 +0200
  14885. @@ -0,0 +1,1534 @@
  14886. +/*
  14887. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14888. + *
  14889. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14890. + * for Toby Churchill Ltd and Brightstar Engineering
  14891. + *
  14892. + * Created by Charles Manning <charles@aleph1.co.uk>
  14893. + *
  14894. + * This program is free software; you can redistribute it and/or modify
  14895. + * it under the terms of the GNU General Public License version 2 as
  14896. + * published by the Free Software Foundation.
  14897. + */
  14898. +
  14899. +#include "yaffs_guts.h"
  14900. +#include "yaffs_trace.h"
  14901. +#include "yaffs_yaffs2.h"
  14902. +#include "yaffs_checkptrw.h"
  14903. +#include "yaffs_bitmap.h"
  14904. +#include "yaffs_nand.h"
  14905. +#include "yaffs_getblockinfo.h"
  14906. +#include "yaffs_verify.h"
  14907. +#include "yaffs_attribs.h"
  14908. +#include "yaffs_summary.h"
  14909. +
  14910. +/*
  14911. + * Checkpoints are really no benefit on very small partitions.
  14912. + *
  14913. + * To save space on small partitions don't bother with checkpoints unless
  14914. + * the partition is at least this big.
  14915. + */
  14916. +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
  14917. +#define YAFFS_SMALL_HOLE_THRESHOLD 4
  14918. +
  14919. +/*
  14920. + * Oldest Dirty Sequence Number handling.
  14921. + */
  14922. +
  14923. +/* yaffs_calc_oldest_dirty_seq()
  14924. + * yaffs2_find_oldest_dirty_seq()
  14925. + * Calculate the oldest dirty sequence number if we don't know it.
  14926. + */
  14927. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
  14928. +{
  14929. + int i;
  14930. + unsigned seq;
  14931. + unsigned block_no = 0;
  14932. + struct yaffs_block_info *b;
  14933. +
  14934. + if (!dev->param.is_yaffs2)
  14935. + return;
  14936. +
  14937. + /* Find the oldest dirty sequence number. */
  14938. + seq = dev->seq_number + 1;
  14939. + b = dev->block_info;
  14940. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  14941. + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
  14942. + (b->pages_in_use - b->soft_del_pages) <
  14943. + dev->param.chunks_per_block &&
  14944. + b->seq_number < seq) {
  14945. + seq = b->seq_number;
  14946. + block_no = i;
  14947. + }
  14948. + b++;
  14949. + }
  14950. +
  14951. + if (block_no) {
  14952. + dev->oldest_dirty_seq = seq;
  14953. + dev->oldest_dirty_block = block_no;
  14954. + }
  14955. +}
  14956. +
  14957. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
  14958. +{
  14959. + if (!dev->param.is_yaffs2)
  14960. + return;
  14961. +
  14962. + if (!dev->oldest_dirty_seq)
  14963. + yaffs_calc_oldest_dirty_seq(dev);
  14964. +}
  14965. +
  14966. +/*
  14967. + * yaffs_clear_oldest_dirty_seq()
  14968. + * Called when a block is erased or marked bad. (ie. when its seq_number
  14969. + * becomes invalid). If the value matches the oldest then we clear
  14970. + * dev->oldest_dirty_seq to force its recomputation.
  14971. + */
  14972. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  14973. + struct yaffs_block_info *bi)
  14974. +{
  14975. +
  14976. + if (!dev->param.is_yaffs2)
  14977. + return;
  14978. +
  14979. + if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
  14980. + dev->oldest_dirty_seq = 0;
  14981. + dev->oldest_dirty_block = 0;
  14982. + }
  14983. +}
  14984. +
  14985. +/*
  14986. + * yaffs2_update_oldest_dirty_seq()
  14987. + * Update the oldest dirty sequence number whenever we dirty a block.
  14988. + * Only do this if the oldest_dirty_seq is actually being tracked.
  14989. + */
  14990. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  14991. + struct yaffs_block_info *bi)
  14992. +{
  14993. + if (!dev->param.is_yaffs2)
  14994. + return;
  14995. +
  14996. + if (dev->oldest_dirty_seq) {
  14997. + if (dev->oldest_dirty_seq > bi->seq_number) {
  14998. + dev->oldest_dirty_seq = bi->seq_number;
  14999. + dev->oldest_dirty_block = block_no;
  15000. + }
  15001. + }
  15002. +}
  15003. +
  15004. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
  15005. +{
  15006. +
  15007. + if (!dev->param.is_yaffs2)
  15008. + return 1; /* disqualification only applies to yaffs2. */
  15009. +
  15010. + if (!bi->has_shrink_hdr)
  15011. + return 1; /* can gc */
  15012. +
  15013. + yaffs2_find_oldest_dirty_seq(dev);
  15014. +
  15015. + /* Can't do gc of this block if there are any blocks older than this
  15016. + * one that have discarded pages.
  15017. + */
  15018. + return (bi->seq_number <= dev->oldest_dirty_seq);
  15019. +}
  15020. +
  15021. +/*
  15022. + * yaffs2_find_refresh_block()
  15023. + * periodically finds the oldest full block by sequence number for refreshing.
  15024. + * Only for yaffs2.
  15025. + */
  15026. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
  15027. +{
  15028. + u32 b;
  15029. + u32 oldest = 0;
  15030. + u32 oldest_seq = 0;
  15031. + struct yaffs_block_info *bi;
  15032. +
  15033. + if (!dev->param.is_yaffs2)
  15034. + return oldest;
  15035. +
  15036. + /*
  15037. + * If refresh period < 10 then refreshing is disabled.
  15038. + */
  15039. + if (dev->param.refresh_period < 10)
  15040. + return oldest;
  15041. +
  15042. + /*
  15043. + * Fix broken values.
  15044. + */
  15045. + if (dev->refresh_skip > dev->param.refresh_period)
  15046. + dev->refresh_skip = dev->param.refresh_period;
  15047. +
  15048. + if (dev->refresh_skip > 0)
  15049. + return oldest;
  15050. +
  15051. + /*
  15052. + * Refresh skip is now zero.
  15053. + * We'll do a refresh this time around....
  15054. + * Update the refresh skip and find the oldest block.
  15055. + */
  15056. + dev->refresh_skip = dev->param.refresh_period;
  15057. + dev->refresh_count++;
  15058. + bi = dev->block_info;
  15059. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  15060. +
  15061. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  15062. +
  15063. + if (oldest < 1 || bi->seq_number < oldest_seq) {
  15064. + oldest = b;
  15065. + oldest_seq = bi->seq_number;
  15066. + }
  15067. + }
  15068. + bi++;
  15069. + }
  15070. +
  15071. + if (oldest > 0) {
  15072. + yaffs_trace(YAFFS_TRACE_GC,
  15073. + "GC refresh count %d selected block %d with seq_number %d",
  15074. + dev->refresh_count, oldest, oldest_seq);
  15075. + }
  15076. +
  15077. + return oldest;
  15078. +}
  15079. +
  15080. +int yaffs2_checkpt_required(struct yaffs_dev *dev)
  15081. +{
  15082. + int nblocks;
  15083. +
  15084. + if (!dev->param.is_yaffs2)
  15085. + return 0;
  15086. +
  15087. + nblocks = dev->internal_end_block - dev->internal_start_block + 1;
  15088. +
  15089. + return !dev->param.skip_checkpt_wr &&
  15090. + !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
  15091. +}
  15092. +
  15093. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
  15094. +{
  15095. + int retval;
  15096. + int n_bytes = 0;
  15097. + int n_blocks;
  15098. + int dev_blocks;
  15099. +
  15100. + if (!dev->param.is_yaffs2)
  15101. + return 0;
  15102. +
  15103. + if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
  15104. + /* Not a valid value so recalculate */
  15105. + dev_blocks = dev->param.end_block - dev->param.start_block + 1;
  15106. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15107. + n_bytes += sizeof(struct yaffs_checkpt_dev);
  15108. + n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
  15109. + n_bytes += dev_blocks * dev->chunk_bit_stride;
  15110. + n_bytes +=
  15111. + (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
  15112. + dev->n_obj;
  15113. + n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
  15114. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15115. + n_bytes += sizeof(u32); /* checksum */
  15116. +
  15117. + /* Round up and add 2 blocks to allow for some bad blocks,
  15118. + * so add 3 */
  15119. +
  15120. + n_blocks =
  15121. + (n_bytes /
  15122. + (dev->data_bytes_per_chunk *
  15123. + dev->param.chunks_per_block)) + 3;
  15124. +
  15125. + dev->checkpoint_blocks_required = n_blocks;
  15126. + }
  15127. +
  15128. + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
  15129. + if (retval < 0)
  15130. + retval = 0;
  15131. + return retval;
  15132. +}
  15133. +
  15134. +/*--------------------- Checkpointing --------------------*/
  15135. +
  15136. +static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15137. +{
  15138. + struct yaffs_checkpt_validity cp;
  15139. +
  15140. + memset(&cp, 0, sizeof(cp));
  15141. +
  15142. + cp.struct_type = sizeof(cp);
  15143. + cp.magic = YAFFS_MAGIC;
  15144. + cp.version = YAFFS_CHECKPOINT_VERSION;
  15145. + cp.head = (head) ? 1 : 0;
  15146. +
  15147. + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
  15148. +}
  15149. +
  15150. +static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15151. +{
  15152. + struct yaffs_checkpt_validity cp;
  15153. + int ok;
  15154. +
  15155. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15156. +
  15157. + if (ok)
  15158. + ok = (cp.struct_type == sizeof(cp)) &&
  15159. + (cp.magic == YAFFS_MAGIC) &&
  15160. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  15161. + (cp.head == ((head) ? 1 : 0));
  15162. + return ok ? 1 : 0;
  15163. +}
  15164. +
  15165. +static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
  15166. + struct yaffs_dev *dev)
  15167. +{
  15168. + cp->n_erased_blocks = dev->n_erased_blocks;
  15169. + cp->alloc_block = dev->alloc_block;
  15170. + cp->alloc_page = dev->alloc_page;
  15171. + cp->n_free_chunks = dev->n_free_chunks;
  15172. +
  15173. + cp->n_deleted_files = dev->n_deleted_files;
  15174. + cp->n_unlinked_files = dev->n_unlinked_files;
  15175. + cp->n_bg_deletions = dev->n_bg_deletions;
  15176. + cp->seq_number = dev->seq_number;
  15177. +
  15178. +}
  15179. +
  15180. +static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
  15181. + struct yaffs_checkpt_dev *cp)
  15182. +{
  15183. + dev->n_erased_blocks = cp->n_erased_blocks;
  15184. + dev->alloc_block = cp->alloc_block;
  15185. + dev->alloc_page = cp->alloc_page;
  15186. + dev->n_free_chunks = cp->n_free_chunks;
  15187. +
  15188. + dev->n_deleted_files = cp->n_deleted_files;
  15189. + dev->n_unlinked_files = cp->n_unlinked_files;
  15190. + dev->n_bg_deletions = cp->n_bg_deletions;
  15191. + dev->seq_number = cp->seq_number;
  15192. +}
  15193. +
  15194. +static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
  15195. +{
  15196. + struct yaffs_checkpt_dev cp;
  15197. + u32 n_bytes;
  15198. + u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  15199. + int ok;
  15200. +
  15201. + /* Write device runtime values */
  15202. + yaffs2_dev_to_checkpt_dev(&cp, dev);
  15203. + cp.struct_type = sizeof(cp);
  15204. +
  15205. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15206. + if (!ok)
  15207. + return 0;
  15208. +
  15209. + /* Write block info */
  15210. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15211. + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
  15212. + if (!ok)
  15213. + return 0;
  15214. +
  15215. + /* Write chunk bits */
  15216. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15217. + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15218. +
  15219. + return ok ? 1 : 0;
  15220. +}
  15221. +
  15222. +static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
  15223. +{
  15224. + struct yaffs_checkpt_dev cp;
  15225. + u32 n_bytes;
  15226. + u32 n_blocks =
  15227. + (dev->internal_end_block - dev->internal_start_block + 1);
  15228. + int ok;
  15229. +
  15230. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15231. + if (!ok)
  15232. + return 0;
  15233. +
  15234. + if (cp.struct_type != sizeof(cp))
  15235. + return 0;
  15236. +
  15237. + yaffs_checkpt_dev_to_dev(dev, &cp);
  15238. +
  15239. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15240. +
  15241. + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
  15242. +
  15243. + if (!ok)
  15244. + return 0;
  15245. +
  15246. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15247. +
  15248. + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15249. +
  15250. + return ok ? 1 : 0;
  15251. +}
  15252. +
  15253. +static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
  15254. + struct yaffs_obj *obj)
  15255. +{
  15256. + cp->obj_id = obj->obj_id;
  15257. + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
  15258. + cp->hdr_chunk = obj->hdr_chunk;
  15259. + cp->variant_type = obj->variant_type;
  15260. + cp->deleted = obj->deleted;
  15261. + cp->soft_del = obj->soft_del;
  15262. + cp->unlinked = obj->unlinked;
  15263. + cp->fake = obj->fake;
  15264. + cp->rename_allowed = obj->rename_allowed;
  15265. + cp->unlink_allowed = obj->unlink_allowed;
  15266. + cp->serial = obj->serial;
  15267. + cp->n_data_chunks = obj->n_data_chunks;
  15268. +
  15269. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15270. + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
  15271. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15272. + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
  15273. +}
  15274. +
  15275. +static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
  15276. + struct yaffs_checkpt_obj *cp)
  15277. +{
  15278. + struct yaffs_obj *parent;
  15279. +
  15280. + if (obj->variant_type != cp->variant_type) {
  15281. + yaffs_trace(YAFFS_TRACE_ERROR,
  15282. + "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
  15283. + cp->obj_id, cp->variant_type, cp->hdr_chunk,
  15284. + obj->variant_type);
  15285. + return 0;
  15286. + }
  15287. +
  15288. + obj->obj_id = cp->obj_id;
  15289. +
  15290. + if (cp->parent_id)
  15291. + parent = yaffs_find_or_create_by_number(obj->my_dev,
  15292. + cp->parent_id,
  15293. + YAFFS_OBJECT_TYPE_DIRECTORY);
  15294. + else
  15295. + parent = NULL;
  15296. +
  15297. + if (parent) {
  15298. + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  15299. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  15300. + "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
  15301. + cp->obj_id, cp->parent_id,
  15302. + cp->variant_type, cp->hdr_chunk,
  15303. + parent->variant_type);
  15304. + return 0;
  15305. + }
  15306. + yaffs_add_obj_to_dir(parent, obj);
  15307. + }
  15308. +
  15309. + obj->hdr_chunk = cp->hdr_chunk;
  15310. + obj->variant_type = cp->variant_type;
  15311. + obj->deleted = cp->deleted;
  15312. + obj->soft_del = cp->soft_del;
  15313. + obj->unlinked = cp->unlinked;
  15314. + obj->fake = cp->fake;
  15315. + obj->rename_allowed = cp->rename_allowed;
  15316. + obj->unlink_allowed = cp->unlink_allowed;
  15317. + obj->serial = cp->serial;
  15318. + obj->n_data_chunks = cp->n_data_chunks;
  15319. +
  15320. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15321. + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
  15322. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15323. + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
  15324. +
  15325. + if (obj->hdr_chunk > 0)
  15326. + obj->lazy_loaded = 1;
  15327. + return 1;
  15328. +}
  15329. +
  15330. +static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
  15331. + struct yaffs_tnode *tn, u32 level,
  15332. + int chunk_offset)
  15333. +{
  15334. + int i;
  15335. + struct yaffs_dev *dev = in->my_dev;
  15336. + int ok = 1;
  15337. + u32 base_offset;
  15338. +
  15339. + if (!tn)
  15340. + return 1;
  15341. +
  15342. + if (level > 0) {
  15343. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  15344. + if (!tn->internal[i])
  15345. + continue;
  15346. + ok = yaffs2_checkpt_tnode_worker(in,
  15347. + tn->internal[i],
  15348. + level - 1,
  15349. + (chunk_offset <<
  15350. + YAFFS_TNODES_INTERNAL_BITS) + i);
  15351. + }
  15352. + return ok;
  15353. + }
  15354. +
  15355. + /* Level 0 tnode */
  15356. + base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
  15357. + ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
  15358. + sizeof(base_offset));
  15359. + if (ok)
  15360. + ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
  15361. + dev->tnode_size);
  15362. +
  15363. + return ok;
  15364. +}
  15365. +
  15366. +static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
  15367. +{
  15368. + u32 end_marker = ~0;
  15369. + int ok = 1;
  15370. +
  15371. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15372. + return ok;
  15373. +
  15374. + ok = yaffs2_checkpt_tnode_worker(obj,
  15375. + obj->variant.file_variant.top,
  15376. + obj->variant.file_variant.
  15377. + top_level, 0);
  15378. + if (ok)
  15379. + ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
  15380. + sizeof(end_marker)) == sizeof(end_marker));
  15381. +
  15382. + return ok ? 1 : 0;
  15383. +}
  15384. +
  15385. +static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
  15386. +{
  15387. + u32 base_chunk;
  15388. + int ok = 1;
  15389. + struct yaffs_dev *dev = obj->my_dev;
  15390. + struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
  15391. + struct yaffs_tnode *tn;
  15392. + int nread = 0;
  15393. +
  15394. + ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
  15395. + sizeof(base_chunk));
  15396. +
  15397. + while (ok && (~base_chunk)) {
  15398. + nread++;
  15399. + /* Read level 0 tnode */
  15400. +
  15401. + tn = yaffs_get_tnode(dev);
  15402. + if (tn)
  15403. + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
  15404. + dev->tnode_size);
  15405. + else
  15406. + ok = 0;
  15407. +
  15408. + if (tn && ok)
  15409. + ok = yaffs_add_find_tnode_0(dev,
  15410. + file_stuct_ptr,
  15411. + base_chunk, tn) ? 1 : 0;
  15412. +
  15413. + if (ok)
  15414. + ok = (yaffs2_checkpt_rd
  15415. + (dev, &base_chunk,
  15416. + sizeof(base_chunk)) == sizeof(base_chunk));
  15417. + }
  15418. +
  15419. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15420. + "Checkpoint read tnodes %d records, last %d. ok %d",
  15421. + nread, base_chunk, ok);
  15422. +
  15423. + return ok ? 1 : 0;
  15424. +}
  15425. +
  15426. +static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
  15427. +{
  15428. + struct yaffs_obj *obj;
  15429. + struct yaffs_checkpt_obj cp;
  15430. + int i;
  15431. + int ok = 1;
  15432. + struct list_head *lh;
  15433. +
  15434. + /* Iterate through the objects in each hash entry,
  15435. + * dumping them to the checkpointing stream.
  15436. + */
  15437. +
  15438. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  15439. + list_for_each(lh, &dev->obj_bucket[i].list) {
  15440. + obj = list_entry(lh, struct yaffs_obj, hash_link);
  15441. + if (!obj->defered_free) {
  15442. + yaffs2_obj_checkpt_obj(&cp, obj);
  15443. + cp.struct_type = sizeof(cp);
  15444. +
  15445. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15446. + "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
  15447. + cp.obj_id, cp.parent_id,
  15448. + cp.variant_type, cp.hdr_chunk, obj);
  15449. +
  15450. + ok = (yaffs2_checkpt_wr(dev, &cp,
  15451. + sizeof(cp)) == sizeof(cp));
  15452. +
  15453. + if (ok &&
  15454. + obj->variant_type ==
  15455. + YAFFS_OBJECT_TYPE_FILE)
  15456. + ok = yaffs2_wr_checkpt_tnodes(obj);
  15457. + }
  15458. + }
  15459. + }
  15460. +
  15461. + /* Dump end of list */
  15462. + memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
  15463. + cp.struct_type = sizeof(cp);
  15464. +
  15465. + if (ok)
  15466. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15467. +
  15468. + return ok ? 1 : 0;
  15469. +}
  15470. +
  15471. +static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
  15472. +{
  15473. + struct yaffs_obj *obj;
  15474. + struct yaffs_checkpt_obj cp;
  15475. + int ok = 1;
  15476. + int done = 0;
  15477. + LIST_HEAD(hard_list);
  15478. +
  15479. +
  15480. + while (ok && !done) {
  15481. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15482. + if (cp.struct_type != sizeof(cp)) {
  15483. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15484. + "struct size %d instead of %d ok %d",
  15485. + cp.struct_type, (int)sizeof(cp), ok);
  15486. + ok = 0;
  15487. + }
  15488. +
  15489. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15490. + "Checkpoint read object %d parent %d type %d chunk %d ",
  15491. + cp.obj_id, cp.parent_id, cp.variant_type,
  15492. + cp.hdr_chunk);
  15493. +
  15494. + if (ok && cp.obj_id == ~0) {
  15495. + done = 1;
  15496. + } else if (ok) {
  15497. + obj =
  15498. + yaffs_find_or_create_by_number(dev, cp.obj_id,
  15499. + cp.variant_type);
  15500. + if (obj) {
  15501. + ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
  15502. + if (!ok)
  15503. + break;
  15504. + if (obj->variant_type ==
  15505. + YAFFS_OBJECT_TYPE_FILE) {
  15506. + ok = yaffs2_rd_checkpt_tnodes(obj);
  15507. + } else if (obj->variant_type ==
  15508. + YAFFS_OBJECT_TYPE_HARDLINK) {
  15509. + list_add(&obj->hard_links, &hard_list);
  15510. + }
  15511. + } else {
  15512. + ok = 0;
  15513. + }
  15514. + }
  15515. + }
  15516. +
  15517. + if (ok)
  15518. + yaffs_link_fixup(dev, &hard_list);
  15519. +
  15520. + return ok ? 1 : 0;
  15521. +}
  15522. +
  15523. +static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
  15524. +{
  15525. + u32 checkpt_sum;
  15526. + int ok;
  15527. +
  15528. + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
  15529. +
  15530. + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
  15531. + sizeof(checkpt_sum));
  15532. +
  15533. + if (!ok)
  15534. + return 0;
  15535. +
  15536. + return 1;
  15537. +}
  15538. +
  15539. +static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
  15540. +{
  15541. + u32 checkpt_sum0;
  15542. + u32 checkpt_sum1;
  15543. + int ok;
  15544. +
  15545. + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
  15546. +
  15547. + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
  15548. + sizeof(checkpt_sum1));
  15549. +
  15550. + if (!ok)
  15551. + return 0;
  15552. +
  15553. + if (checkpt_sum0 != checkpt_sum1)
  15554. + return 0;
  15555. +
  15556. + return 1;
  15557. +}
  15558. +
  15559. +static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
  15560. +{
  15561. + int ok = 1;
  15562. +
  15563. + if (!yaffs2_checkpt_required(dev)) {
  15564. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15565. + "skipping checkpoint write");
  15566. + ok = 0;
  15567. + }
  15568. +
  15569. + if (ok)
  15570. + ok = yaffs2_checkpt_open(dev, 1);
  15571. +
  15572. + if (ok) {
  15573. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15574. + "write checkpoint validity");
  15575. + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
  15576. + }
  15577. + if (ok) {
  15578. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15579. + "write checkpoint device");
  15580. + ok = yaffs2_wr_checkpt_dev(dev);
  15581. + }
  15582. + if (ok) {
  15583. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15584. + "write checkpoint objects");
  15585. + ok = yaffs2_wr_checkpt_objs(dev);
  15586. + }
  15587. + if (ok) {
  15588. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15589. + "write checkpoint validity");
  15590. + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
  15591. + }
  15592. +
  15593. + if (ok)
  15594. + ok = yaffs2_wr_checkpt_sum(dev);
  15595. +
  15596. + if (!yaffs_checkpt_close(dev))
  15597. + ok = 0;
  15598. +
  15599. + if (ok)
  15600. + dev->is_checkpointed = 1;
  15601. + else
  15602. + dev->is_checkpointed = 0;
  15603. +
  15604. + return dev->is_checkpointed;
  15605. +}
  15606. +
  15607. +static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
  15608. +{
  15609. + int ok = 1;
  15610. +
  15611. + if (!dev->param.is_yaffs2)
  15612. + ok = 0;
  15613. +
  15614. + if (ok && dev->param.skip_checkpt_rd) {
  15615. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15616. + "skipping checkpoint read");
  15617. + ok = 0;
  15618. + }
  15619. +
  15620. + if (ok)
  15621. + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
  15622. +
  15623. + if (ok) {
  15624. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15625. + "read checkpoint validity");
  15626. + ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
  15627. + }
  15628. + if (ok) {
  15629. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15630. + "read checkpoint device");
  15631. + ok = yaffs2_rd_checkpt_dev(dev);
  15632. + }
  15633. + if (ok) {
  15634. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15635. + "read checkpoint objects");
  15636. + ok = yaffs2_rd_checkpt_objs(dev);
  15637. + }
  15638. + if (ok) {
  15639. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15640. + "read checkpoint validity");
  15641. + ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
  15642. + }
  15643. +
  15644. + if (ok) {
  15645. + ok = yaffs2_rd_checkpt_sum(dev);
  15646. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15647. + "read checkpoint checksum %d", ok);
  15648. + }
  15649. +
  15650. + if (!yaffs_checkpt_close(dev))
  15651. + ok = 0;
  15652. +
  15653. + if (ok)
  15654. + dev->is_checkpointed = 1;
  15655. + else
  15656. + dev->is_checkpointed = 0;
  15657. +
  15658. + return ok ? 1 : 0;
  15659. +}
  15660. +
  15661. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
  15662. +{
  15663. + if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
  15664. + dev->is_checkpointed = 0;
  15665. + yaffs2_checkpt_invalidate_stream(dev);
  15666. + }
  15667. + if (dev->param.sb_dirty_fn)
  15668. + dev->param.sb_dirty_fn(dev);
  15669. +}
  15670. +
  15671. +int yaffs_checkpoint_save(struct yaffs_dev *dev)
  15672. +{
  15673. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15674. + "save entry: is_checkpointed %d",
  15675. + dev->is_checkpointed);
  15676. +
  15677. + yaffs_verify_objects(dev);
  15678. + yaffs_verify_blocks(dev);
  15679. + yaffs_verify_free_chunks(dev);
  15680. +
  15681. + if (!dev->is_checkpointed) {
  15682. + yaffs2_checkpt_invalidate(dev);
  15683. + yaffs2_wr_checkpt_data(dev);
  15684. + }
  15685. +
  15686. + yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  15687. + "save exit: is_checkpointed %d",
  15688. + dev->is_checkpointed);
  15689. +
  15690. + return dev->is_checkpointed;
  15691. +}
  15692. +
  15693. +int yaffs2_checkpt_restore(struct yaffs_dev *dev)
  15694. +{
  15695. + int retval;
  15696. +
  15697. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15698. + "restore entry: is_checkpointed %d",
  15699. + dev->is_checkpointed);
  15700. +
  15701. + retval = yaffs2_rd_checkpt_data(dev);
  15702. +
  15703. + if (dev->is_checkpointed) {
  15704. + yaffs_verify_objects(dev);
  15705. + yaffs_verify_blocks(dev);
  15706. + yaffs_verify_free_chunks(dev);
  15707. + }
  15708. +
  15709. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15710. + "restore exit: is_checkpointed %d",
  15711. + dev->is_checkpointed);
  15712. +
  15713. + return retval;
  15714. +}
  15715. +
  15716. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
  15717. +{
  15718. + /* if new_size > old_file_size.
  15719. + * We're going to be writing a hole.
  15720. + * If the hole is small then write zeros otherwise write a start
  15721. + * of hole marker.
  15722. + */
  15723. + loff_t old_file_size;
  15724. + loff_t increase;
  15725. + int small_hole;
  15726. + int result = YAFFS_OK;
  15727. + struct yaffs_dev *dev = NULL;
  15728. + u8 *local_buffer = NULL;
  15729. + int small_increase_ok = 0;
  15730. +
  15731. + if (!obj)
  15732. + return YAFFS_FAIL;
  15733. +
  15734. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15735. + return YAFFS_FAIL;
  15736. +
  15737. + dev = obj->my_dev;
  15738. +
  15739. + /* Bail out if not yaffs2 mode */
  15740. + if (!dev->param.is_yaffs2)
  15741. + return YAFFS_OK;
  15742. +
  15743. + old_file_size = obj->variant.file_variant.file_size;
  15744. +
  15745. + if (new_size <= old_file_size)
  15746. + return YAFFS_OK;
  15747. +
  15748. + increase = new_size - old_file_size;
  15749. +
  15750. + if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
  15751. + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
  15752. + small_hole = 1;
  15753. + else
  15754. + small_hole = 0;
  15755. +
  15756. + if (small_hole)
  15757. + local_buffer = yaffs_get_temp_buffer(dev);
  15758. +
  15759. + if (local_buffer) {
  15760. + /* fill hole with zero bytes */
  15761. + loff_t pos = old_file_size;
  15762. + int this_write;
  15763. + int written;
  15764. + memset(local_buffer, 0, dev->data_bytes_per_chunk);
  15765. + small_increase_ok = 1;
  15766. +
  15767. + while (increase > 0 && small_increase_ok) {
  15768. + this_write = increase;
  15769. + if (this_write > dev->data_bytes_per_chunk)
  15770. + this_write = dev->data_bytes_per_chunk;
  15771. + written =
  15772. + yaffs_do_file_wr(obj, local_buffer, pos, this_write,
  15773. + 0);
  15774. + if (written == this_write) {
  15775. + pos += this_write;
  15776. + increase -= this_write;
  15777. + } else {
  15778. + small_increase_ok = 0;
  15779. + }
  15780. + }
  15781. +
  15782. + yaffs_release_temp_buffer(dev, local_buffer);
  15783. +
  15784. + /* If out of space then reverse any chunks we've added */
  15785. + if (!small_increase_ok)
  15786. + yaffs_resize_file_down(obj, old_file_size);
  15787. + }
  15788. +
  15789. + if (!small_increase_ok &&
  15790. + obj->parent &&
  15791. + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  15792. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
  15793. + /* Write a hole start header with the old file size */
  15794. + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
  15795. + }
  15796. +
  15797. + return result;
  15798. +}
  15799. +
  15800. +struct yaffs_block_index {
  15801. + int seq;
  15802. + int block;
  15803. +};
  15804. +
  15805. +static int yaffs2_ybicmp(const void *a, const void *b)
  15806. +{
  15807. + int aseq = ((struct yaffs_block_index *)a)->seq;
  15808. + int bseq = ((struct yaffs_block_index *)b)->seq;
  15809. + int ablock = ((struct yaffs_block_index *)a)->block;
  15810. + int bblock = ((struct yaffs_block_index *)b)->block;
  15811. +
  15812. + if (aseq == bseq)
  15813. + return ablock - bblock;
  15814. +
  15815. + return aseq - bseq;
  15816. +}
  15817. +
  15818. +static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
  15819. + struct yaffs_block_info *bi,
  15820. + int blk, int chunk_in_block,
  15821. + int *found_chunks,
  15822. + u8 *chunk_data,
  15823. + struct list_head *hard_list,
  15824. + int summary_available)
  15825. +{
  15826. + struct yaffs_obj_hdr *oh;
  15827. + struct yaffs_obj *in;
  15828. + struct yaffs_obj *parent;
  15829. + int equiv_id;
  15830. + loff_t file_size;
  15831. + int is_shrink;
  15832. + int is_unlinked;
  15833. + struct yaffs_ext_tags tags;
  15834. + int result;
  15835. + int alloc_failed = 0;
  15836. + int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
  15837. + struct yaffs_file_var *file_var;
  15838. + struct yaffs_hardlink_var *hl_var;
  15839. + struct yaffs_symlink_var *sl_var;
  15840. +
  15841. + if (summary_available) {
  15842. + result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
  15843. + tags.seq_number = bi->seq_number;
  15844. + }
  15845. +
  15846. + if (!summary_available || tags.obj_id == 0) {
  15847. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
  15848. + dev->tags_used++;
  15849. + } else {
  15850. + dev->summary_used++;
  15851. + }
  15852. +
  15853. + /* Let's have a good look at this chunk... */
  15854. +
  15855. + if (!tags.chunk_used) {
  15856. + /* An unassigned chunk in the block.
  15857. + * If there are used chunks after this one, then
  15858. + * it is a chunk that was skipped due to failing
  15859. + * the erased check. Just skip it so that it can
  15860. + * be deleted.
  15861. + * But, more typically, We get here when this is
  15862. + * an unallocated chunk and his means that
  15863. + * either the block is empty or this is the one
  15864. + * being allocated from
  15865. + */
  15866. +
  15867. + if (*found_chunks) {
  15868. + /* This is a chunk that was skipped due
  15869. + * to failing the erased check */
  15870. + } else if (chunk_in_block == 0) {
  15871. + /* We're looking at the first chunk in
  15872. + * the block so the block is unused */
  15873. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  15874. + dev->n_erased_blocks++;
  15875. + } else {
  15876. + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  15877. + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  15878. + if (dev->seq_number == bi->seq_number) {
  15879. + /* Allocating from this block*/
  15880. + yaffs_trace(YAFFS_TRACE_SCAN,
  15881. + " Allocating from %d %d",
  15882. + blk, chunk_in_block);
  15883. +
  15884. + bi->block_state =
  15885. + YAFFS_BLOCK_STATE_ALLOCATING;
  15886. + dev->alloc_block = blk;
  15887. + dev->alloc_page = chunk_in_block;
  15888. + dev->alloc_block_finder = blk;
  15889. + } else {
  15890. + /* This is a partially written block
  15891. + * that is not the current
  15892. + * allocation block.
  15893. + */
  15894. + yaffs_trace(YAFFS_TRACE_SCAN,
  15895. + "Partially written block %d detected. gc will fix this.",
  15896. + blk);
  15897. + }
  15898. + }
  15899. + }
  15900. +
  15901. + dev->n_free_chunks++;
  15902. +
  15903. + } else if (tags.ecc_result ==
  15904. + YAFFS_ECC_RESULT_UNFIXED) {
  15905. + yaffs_trace(YAFFS_TRACE_SCAN,
  15906. + " Unfixed ECC in chunk(%d:%d), chunk ignored",
  15907. + blk, chunk_in_block);
  15908. + dev->n_free_chunks++;
  15909. + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
  15910. + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
  15911. + tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
  15912. + (tags.chunk_id > 0 &&
  15913. + tags.n_bytes > dev->data_bytes_per_chunk) ||
  15914. + tags.seq_number != bi->seq_number) {
  15915. + yaffs_trace(YAFFS_TRACE_SCAN,
  15916. + "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
  15917. + blk, chunk_in_block, tags.obj_id,
  15918. + tags.chunk_id, tags.n_bytes);
  15919. + dev->n_free_chunks++;
  15920. + } else if (tags.chunk_id > 0) {
  15921. + /* chunk_id > 0 so it is a data chunk... */
  15922. + loff_t endpos;
  15923. + loff_t chunk_base = (tags.chunk_id - 1) *
  15924. + dev->data_bytes_per_chunk;
  15925. +
  15926. + *found_chunks = 1;
  15927. +
  15928. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  15929. + bi->pages_in_use++;
  15930. +
  15931. + in = yaffs_find_or_create_by_number(dev,
  15932. + tags.obj_id,
  15933. + YAFFS_OBJECT_TYPE_FILE);
  15934. + if (!in)
  15935. + /* Out of memory */
  15936. + alloc_failed = 1;
  15937. +
  15938. + if (in &&
  15939. + in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
  15940. + chunk_base < in->variant.file_variant.shrink_size) {
  15941. + /* This has not been invalidated by
  15942. + * a resize */
  15943. + if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
  15944. + chunk, -1))
  15945. + alloc_failed = 1;
  15946. +
  15947. + /* File size is calculated by looking at
  15948. + * the data chunks if we have not
  15949. + * seen an object header yet.
  15950. + * Stop this practice once we find an
  15951. + * object header.
  15952. + */
  15953. + endpos = chunk_base + tags.n_bytes;
  15954. +
  15955. + if (!in->valid &&
  15956. + in->variant.file_variant.scanned_size < endpos) {
  15957. + in->variant.file_variant.
  15958. + scanned_size = endpos;
  15959. + in->variant.file_variant.
  15960. + file_size = endpos;
  15961. + }
  15962. + } else if (in) {
  15963. + /* This chunk has been invalidated by a
  15964. + * resize, or a past file deletion
  15965. + * so delete the chunk*/
  15966. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  15967. + }
  15968. + } else {
  15969. + /* chunk_id == 0, so it is an ObjectHeader.
  15970. + * Thus, we read in the object header and make
  15971. + * the object
  15972. + */
  15973. + *found_chunks = 1;
  15974. +
  15975. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  15976. + bi->pages_in_use++;
  15977. +
  15978. + oh = NULL;
  15979. + in = NULL;
  15980. +
  15981. + if (tags.extra_available) {
  15982. + in = yaffs_find_or_create_by_number(dev,
  15983. + tags.obj_id,
  15984. + tags.extra_obj_type);
  15985. + if (!in)
  15986. + alloc_failed = 1;
  15987. + }
  15988. +
  15989. + if (!in ||
  15990. + (!in->valid && dev->param.disable_lazy_load) ||
  15991. + tags.extra_shadows ||
  15992. + (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  15993. + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
  15994. +
  15995. + /* If we don't have valid info then we
  15996. + * need to read the chunk
  15997. + * TODO In future we can probably defer
  15998. + * reading the chunk and living with
  15999. + * invalid data until needed.
  16000. + */
  16001. +
  16002. + result = yaffs_rd_chunk_tags_nand(dev,
  16003. + chunk,
  16004. + chunk_data,
  16005. + NULL);
  16006. +
  16007. + oh = (struct yaffs_obj_hdr *)chunk_data;
  16008. +
  16009. + if (dev->param.inband_tags) {
  16010. + /* Fix up the header if they got
  16011. + * corrupted by inband tags */
  16012. + oh->shadows_obj =
  16013. + oh->inband_shadowed_obj_id;
  16014. + oh->is_shrink =
  16015. + oh->inband_is_shrink;
  16016. + }
  16017. +
  16018. + if (!in) {
  16019. + in = yaffs_find_or_create_by_number(dev,
  16020. + tags.obj_id, oh->type);
  16021. + if (!in)
  16022. + alloc_failed = 1;
  16023. + }
  16024. + }
  16025. +
  16026. + if (!in) {
  16027. + /* TODO Hoosterman we have a problem! */
  16028. + yaffs_trace(YAFFS_TRACE_ERROR,
  16029. + "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
  16030. + tags.obj_id, chunk);
  16031. + return YAFFS_FAIL;
  16032. + }
  16033. +
  16034. + if (in->valid) {
  16035. + /* We have already filled this one.
  16036. + * We have a duplicate that will be
  16037. + * discarded, but we first have to suck
  16038. + * out resize info if it is a file.
  16039. + */
  16040. + if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
  16041. + ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
  16042. + (tags.extra_available &&
  16043. + tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  16044. + )) {
  16045. + loff_t this_size = (oh) ?
  16046. + yaffs_oh_to_size(oh) :
  16047. + tags.extra_file_size;
  16048. + u32 parent_obj_id = (oh) ?
  16049. + oh->parent_obj_id :
  16050. + tags.extra_parent_id;
  16051. +
  16052. + is_shrink = (oh) ?
  16053. + oh->is_shrink :
  16054. + tags.extra_is_shrink;
  16055. +
  16056. + /* If it is deleted (unlinked
  16057. + * at start also means deleted)
  16058. + * we treat the file size as
  16059. + * being zeroed at this point.
  16060. + */
  16061. + if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
  16062. + parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
  16063. + this_size = 0;
  16064. + is_shrink = 1;
  16065. + }
  16066. +
  16067. + if (is_shrink &&
  16068. + in->variant.file_variant.shrink_size >
  16069. + this_size)
  16070. + in->variant.file_variant.shrink_size =
  16071. + this_size;
  16072. +
  16073. + if (is_shrink)
  16074. + bi->has_shrink_hdr = 1;
  16075. + }
  16076. + /* Use existing - destroy this one. */
  16077. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  16078. + }
  16079. +
  16080. + if (!in->valid && in->variant_type !=
  16081. + (oh ? oh->type : tags.extra_obj_type)) {
  16082. + yaffs_trace(YAFFS_TRACE_ERROR,
  16083. + "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
  16084. + oh ? oh->type : tags.extra_obj_type,
  16085. + in->variant_type, tags.obj_id,
  16086. + chunk);
  16087. + in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
  16088. + }
  16089. +
  16090. + if (!in->valid &&
  16091. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  16092. + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
  16093. + /* We only load some info, don't fiddle
  16094. + * with directory structure */
  16095. + in->valid = 1;
  16096. +
  16097. + if (oh) {
  16098. + in->yst_mode = oh->yst_mode;
  16099. + yaffs_load_attribs(in, oh);
  16100. + in->lazy_loaded = 0;
  16101. + } else {
  16102. + in->lazy_loaded = 1;
  16103. + }
  16104. + in->hdr_chunk = chunk;
  16105. +
  16106. + } else if (!in->valid) {
  16107. + /* we need to load this info */
  16108. + in->valid = 1;
  16109. + in->hdr_chunk = chunk;
  16110. + if (oh) {
  16111. + in->variant_type = oh->type;
  16112. + in->yst_mode = oh->yst_mode;
  16113. + yaffs_load_attribs(in, oh);
  16114. +
  16115. + if (oh->shadows_obj > 0)
  16116. + yaffs_handle_shadowed_obj(dev,
  16117. + oh->shadows_obj, 1);
  16118. +
  16119. + yaffs_set_obj_name_from_oh(in, oh);
  16120. + parent = yaffs_find_or_create_by_number(dev,
  16121. + oh->parent_obj_id,
  16122. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16123. + file_size = yaffs_oh_to_size(oh);
  16124. + is_shrink = oh->is_shrink;
  16125. + equiv_id = oh->equiv_id;
  16126. + } else {
  16127. + in->variant_type = tags.extra_obj_type;
  16128. + parent = yaffs_find_or_create_by_number(dev,
  16129. + tags.extra_parent_id,
  16130. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16131. + file_size = tags.extra_file_size;
  16132. + is_shrink = tags.extra_is_shrink;
  16133. + equiv_id = tags.extra_equiv_id;
  16134. + in->lazy_loaded = 1;
  16135. + }
  16136. + in->dirty = 0;
  16137. +
  16138. + if (!parent)
  16139. + alloc_failed = 1;
  16140. +
  16141. + /* directory stuff...
  16142. + * hook up to parent
  16143. + */
  16144. +
  16145. + if (parent &&
  16146. + parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
  16147. + /* Set up as a directory */
  16148. + parent->variant_type =
  16149. + YAFFS_OBJECT_TYPE_DIRECTORY;
  16150. + INIT_LIST_HEAD(&parent->
  16151. + variant.dir_variant.children);
  16152. + } else if (!parent ||
  16153. + parent->variant_type !=
  16154. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  16155. + /* Hoosterman, another problem....
  16156. + * Trying to use a non-directory as a directory
  16157. + */
  16158. +
  16159. + yaffs_trace(YAFFS_TRACE_ERROR,
  16160. + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  16161. + );
  16162. + parent = dev->lost_n_found;
  16163. + }
  16164. + yaffs_add_obj_to_dir(parent, in);
  16165. +
  16166. + is_unlinked = (parent == dev->del_dir) ||
  16167. + (parent == dev->unlinked_dir);
  16168. +
  16169. + if (is_shrink)
  16170. + /* Mark the block */
  16171. + bi->has_shrink_hdr = 1;
  16172. +
  16173. + /* Note re hardlinks.
  16174. + * Since we might scan a hardlink before its equivalent
  16175. + * object is scanned we put them all in a list.
  16176. + * After scanning is complete, we should have all the
  16177. + * objects, so we run through this list and fix up all
  16178. + * the chains.
  16179. + */
  16180. +
  16181. + switch (in->variant_type) {
  16182. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  16183. + /* Todo got a problem */
  16184. + break;
  16185. + case YAFFS_OBJECT_TYPE_FILE:
  16186. + file_var = &in->variant.file_variant;
  16187. + if (file_var->scanned_size < file_size) {
  16188. + /* This covers the case where the file
  16189. + * size is greater than the data held.
  16190. + * This will happen if the file is
  16191. + * resized to be larger than its
  16192. + * current data extents.
  16193. + */
  16194. + file_var->file_size = file_size;
  16195. + file_var->scanned_size = file_size;
  16196. + }
  16197. +
  16198. + if (file_var->shrink_size > file_size)
  16199. + file_var->shrink_size = file_size;
  16200. +
  16201. + break;
  16202. + case YAFFS_OBJECT_TYPE_HARDLINK:
  16203. + hl_var = &in->variant.hardlink_variant;
  16204. + if (!is_unlinked) {
  16205. + hl_var->equiv_id = equiv_id;
  16206. + list_add(&in->hard_links, hard_list);
  16207. + }
  16208. + break;
  16209. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  16210. + /* Do nothing */
  16211. + break;
  16212. + case YAFFS_OBJECT_TYPE_SPECIAL:
  16213. + /* Do nothing */
  16214. + break;
  16215. + case YAFFS_OBJECT_TYPE_SYMLINK:
  16216. + sl_var = &in->variant.symlink_variant;
  16217. + if (oh) {
  16218. + sl_var->alias =
  16219. + yaffs_clone_str(oh->alias);
  16220. + if (!sl_var->alias)
  16221. + alloc_failed = 1;
  16222. + }
  16223. + break;
  16224. + }
  16225. + }
  16226. + }
  16227. + return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
  16228. +}
  16229. +
  16230. +int yaffs2_scan_backwards(struct yaffs_dev *dev)
  16231. +{
  16232. + int blk;
  16233. + int block_iter;
  16234. + int start_iter;
  16235. + int end_iter;
  16236. + int n_to_scan = 0;
  16237. + enum yaffs_block_state state;
  16238. + int c;
  16239. + int deleted;
  16240. + LIST_HEAD(hard_list);
  16241. + struct yaffs_block_info *bi;
  16242. + u32 seq_number;
  16243. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  16244. + u8 *chunk_data;
  16245. + int found_chunks;
  16246. + int alloc_failed = 0;
  16247. + struct yaffs_block_index *block_index = NULL;
  16248. + int alt_block_index = 0;
  16249. + int summary_available;
  16250. +
  16251. + yaffs_trace(YAFFS_TRACE_SCAN,
  16252. + "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
  16253. + dev->internal_start_block, dev->internal_end_block);
  16254. +
  16255. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  16256. +
  16257. + block_index =
  16258. + kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
  16259. +
  16260. + if (!block_index) {
  16261. + block_index =
  16262. + vmalloc(n_blocks * sizeof(struct yaffs_block_index));
  16263. + alt_block_index = 1;
  16264. + }
  16265. +
  16266. + if (!block_index) {
  16267. + yaffs_trace(YAFFS_TRACE_SCAN,
  16268. + "yaffs2_scan_backwards() could not allocate block index!"
  16269. + );
  16270. + return YAFFS_FAIL;
  16271. + }
  16272. +
  16273. + dev->blocks_in_checkpt = 0;
  16274. +
  16275. + chunk_data = yaffs_get_temp_buffer(dev);
  16276. +
  16277. + /* Scan all the blocks to determine their state */
  16278. + bi = dev->block_info;
  16279. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  16280. + blk++) {
  16281. + yaffs_clear_chunk_bits(dev, blk);
  16282. + bi->pages_in_use = 0;
  16283. + bi->soft_del_pages = 0;
  16284. +
  16285. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  16286. +
  16287. + bi->block_state = state;
  16288. + bi->seq_number = seq_number;
  16289. +
  16290. + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  16291. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  16292. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  16293. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  16294. +
  16295. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  16296. + "Block scanning block %d state %d seq %d",
  16297. + blk, bi->block_state, seq_number);
  16298. +
  16299. + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  16300. + dev->blocks_in_checkpt++;
  16301. +
  16302. + } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
  16303. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  16304. + "block %d is bad", blk);
  16305. + } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  16306. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  16307. + dev->n_erased_blocks++;
  16308. + dev->n_free_chunks += dev->param.chunks_per_block;
  16309. + } else if (bi->block_state ==
  16310. + YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  16311. + /* Determine the highest sequence number */
  16312. + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  16313. + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  16314. + block_index[n_to_scan].seq = seq_number;
  16315. + block_index[n_to_scan].block = blk;
  16316. + n_to_scan++;
  16317. + if (seq_number >= dev->seq_number)
  16318. + dev->seq_number = seq_number;
  16319. + } else {
  16320. + /* TODO: Nasty sequence number! */
  16321. + yaffs_trace(YAFFS_TRACE_SCAN,
  16322. + "Block scanning block %d has bad sequence number %d",
  16323. + blk, seq_number);
  16324. + }
  16325. + }
  16326. + bi++;
  16327. + }
  16328. +
  16329. + yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
  16330. +
  16331. + cond_resched();
  16332. +
  16333. + /* Sort the blocks by sequence number */
  16334. + sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  16335. + yaffs2_ybicmp, NULL);
  16336. +
  16337. + cond_resched();
  16338. +
  16339. + yaffs_trace(YAFFS_TRACE_SCAN, "...done");
  16340. +
  16341. + /* Now scan the blocks looking at the data. */
  16342. + start_iter = 0;
  16343. + end_iter = n_to_scan - 1;
  16344. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
  16345. +
  16346. + /* For each block.... backwards */
  16347. + for (block_iter = end_iter;
  16348. + !alloc_failed && block_iter >= start_iter;
  16349. + block_iter--) {
  16350. + /* Cooperative multitasking! This loop can run for so
  16351. + long that watchdog timers expire. */
  16352. + cond_resched();
  16353. +
  16354. + /* get the block to scan in the correct order */
  16355. + blk = block_index[block_iter].block;
  16356. + bi = yaffs_get_block_info(dev, blk);
  16357. + deleted = 0;
  16358. +
  16359. + summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
  16360. +
  16361. + /* For each chunk in each block that needs scanning.... */
  16362. + found_chunks = 0;
  16363. + if (summary_available)
  16364. + c = dev->chunks_per_summary - 1;
  16365. + else
  16366. + c = dev->param.chunks_per_block - 1;
  16367. +
  16368. + for (/* c is already initialised */;
  16369. + !alloc_failed && c >= 0 &&
  16370. + (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  16371. + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
  16372. + c--) {
  16373. + /* Scan backwards...
  16374. + * Read the tags and decide what to do
  16375. + */
  16376. + if (yaffs2_scan_chunk(dev, bi, blk, c,
  16377. + &found_chunks, chunk_data,
  16378. + &hard_list, summary_available) ==
  16379. + YAFFS_FAIL)
  16380. + alloc_failed = 1;
  16381. + }
  16382. +
  16383. + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  16384. + /* If we got this far while scanning, then the block
  16385. + * is fully allocated. */
  16386. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  16387. + }
  16388. +
  16389. + /* Now let's see if it was dirty */
  16390. + if (bi->pages_in_use == 0 &&
  16391. + !bi->has_shrink_hdr &&
  16392. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  16393. + yaffs_block_became_dirty(dev, blk);
  16394. + }
  16395. + }
  16396. +
  16397. + yaffs_skip_rest_of_block(dev);
  16398. +
  16399. + if (alt_block_index)
  16400. + vfree(block_index);
  16401. + else
  16402. + kfree(block_index);
  16403. +
  16404. + /* Ok, we've done all the scanning.
  16405. + * Fix up the hard link chains.
  16406. + * We have scanned all the objects, now it's time to add these
  16407. + * hardlinks.
  16408. + */
  16409. + yaffs_link_fixup(dev, &hard_list);
  16410. +
  16411. + yaffs_release_temp_buffer(dev, chunk_data);
  16412. +
  16413. + if (alloc_failed)
  16414. + return YAFFS_FAIL;
  16415. +
  16416. + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
  16417. +
  16418. + return YAFFS_OK;
  16419. +}
  16420. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs2.h linux-3.15-rc5/fs/yaffs2/yaffs_yaffs2.h
  16421. --- linux-3.15-rc5.orig/fs/yaffs2/yaffs_yaffs2.h 1970-01-01 01:00:00.000000000 +0100
  16422. +++ linux-3.15-rc5/fs/yaffs2/yaffs_yaffs2.h 2014-05-17 01:53:27.000000000 +0200
  16423. @@ -0,0 +1,39 @@
  16424. +/*
  16425. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16426. + *
  16427. + * Copyright (C) 2002-2011 Aleph One Ltd.
  16428. + * for Toby Churchill Ltd and Brightstar Engineering
  16429. + *
  16430. + * Created by Charles Manning <charles@aleph1.co.uk>
  16431. + *
  16432. + * This program is free software; you can redistribute it and/or modify
  16433. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16434. + * published by the Free Software Foundation.
  16435. + *
  16436. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16437. + */
  16438. +
  16439. +#ifndef __YAFFS_YAFFS2_H__
  16440. +#define __YAFFS_YAFFS2_H__
  16441. +
  16442. +#include "yaffs_guts.h"
  16443. +
  16444. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
  16445. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
  16446. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  16447. + struct yaffs_block_info *bi);
  16448. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  16449. + struct yaffs_block_info *bi);
  16450. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
  16451. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
  16452. +int yaffs2_checkpt_required(struct yaffs_dev *dev);
  16453. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
  16454. +
  16455. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
  16456. +int yaffs2_checkpt_save(struct yaffs_dev *dev);
  16457. +int yaffs2_checkpt_restore(struct yaffs_dev *dev);
  16458. +
  16459. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
  16460. +int yaffs2_scan_backwards(struct yaffs_dev *dev);
  16461. +
  16462. +#endif
  16463. diff -Nur linux-3.15-rc5.orig/fs/yaffs2/yportenv.h linux-3.15-rc5/fs/yaffs2/yportenv.h
  16464. --- linux-3.15-rc5.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  16465. +++ linux-3.15-rc5/fs/yaffs2/yportenv.h 2014-05-17 01:53:27.000000000 +0200
  16466. @@ -0,0 +1,85 @@
  16467. +/*
  16468. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16469. + *
  16470. + * Copyright (C) 2002-2011 Aleph One Ltd.
  16471. + * for Toby Churchill Ltd and Brightstar Engineering
  16472. + *
  16473. + * Created by Charles Manning <charles@aleph1.co.uk>
  16474. + *
  16475. + * This program is free software; you can redistribute it and/or modify
  16476. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16477. + * published by the Free Software Foundation.
  16478. + *
  16479. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16480. + */
  16481. +
  16482. +#ifndef __YPORTENV_H__
  16483. +#define __YPORTENV_H__
  16484. +
  16485. +/*
  16486. + * Define the MTD version in terms of Linux Kernel versions
  16487. + * This allows yaffs to be used independantly of the kernel
  16488. + * as well as with it.
  16489. + */
  16490. +
  16491. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  16492. +
  16493. +#ifdef YAFFS_OUT_OF_TREE
  16494. +#include "moduleconfig.h"
  16495. +#endif
  16496. +
  16497. +#include <linux/version.h>
  16498. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  16499. +
  16500. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  16501. +#include <linux/config.h>
  16502. +#endif
  16503. +#include <linux/version.h>
  16504. +#include <linux/kernel.h>
  16505. +#include <linux/mm.h>
  16506. +#include <linux/sched.h>
  16507. +#include <linux/string.h>
  16508. +#include <linux/slab.h>
  16509. +#include <linux/vmalloc.h>
  16510. +#include <linux/xattr.h>
  16511. +#include <linux/list.h>
  16512. +#include <linux/types.h>
  16513. +#include <linux/fs.h>
  16514. +#include <linux/stat.h>
  16515. +#include <linux/sort.h>
  16516. +#include <linux/bitops.h>
  16517. +
  16518. +/* These type wrappings are used to support Unicode names in WinCE. */
  16519. +#define YCHAR char
  16520. +#define YUCHAR unsigned char
  16521. +#define _Y(x) x
  16522. +
  16523. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16524. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16525. +
  16526. +
  16527. +#define YAFFS_ROOT_MODE 0755
  16528. +#define YAFFS_LOSTNFOUND_MODE 0700
  16529. +
  16530. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  16531. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  16532. +#define Y_TIME_CONVERT(x) (x).tv_sec
  16533. +#else
  16534. +#define Y_CURRENT_TIME CURRENT_TIME
  16535. +#define Y_TIME_CONVERT(x) (x)
  16536. +#endif
  16537. +
  16538. +#define compile_time_assertion(assertion) \
  16539. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  16540. +
  16541. +
  16542. +#define yaffs_printf(msk, fmt, ...) \
  16543. + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__)
  16544. +
  16545. +#define yaffs_trace(msk, fmt, ...) do { \
  16546. + if (yaffs_trace_mask & (msk)) \
  16547. + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \
  16548. +} while (0)
  16549. +
  16550. +
  16551. +#endif