patch-yaffs2 431 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547
  1. diff -Nur linux-3.14.4.orig/fs/Kconfig linux-3.14.4/fs/Kconfig
  2. --- linux-3.14.4.orig/fs/Kconfig 2014-05-13 13:33:14.000000000 +0200
  3. +++ linux-3.14.4/fs/Kconfig 2014-05-17 02:22:30.000000000 +0200
  4. @@ -189,6 +189,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.14.4.orig/fs/Makefile linux-3.14.4/fs/Makefile
  13. --- linux-3.14.4.orig/fs/Makefile 2014-05-13 13:33:14.000000000 +0200
  14. +++ linux-3.14.4/fs/Makefile 2014-05-17 02:22:30.000000000 +0200
  15. @@ -125,3 +125,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.14.4.orig/fs/yaffs2/Kconfig linux-3.14.4/fs/yaffs2/Kconfig
  21. --- linux-3.14.4.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
  22. +++ linux-3.14.4/fs/yaffs2/Kconfig 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/Makefile linux-3.14.4/fs/yaffs2/Makefile
  196. --- linux-3.14.4.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
  197. +++ linux-3.14.4/fs/yaffs2/Makefile 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_allocator.c linux-3.14.4/fs/yaffs2/yaffs_allocator.c
  218. --- linux-3.14.4.orig/fs/yaffs2/yaffs_allocator.c 1970-01-01 01:00:00.000000000 +0100
  219. +++ linux-3.14.4/fs/yaffs2/yaffs_allocator.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_allocator.h linux-3.14.4/fs/yaffs2/yaffs_allocator.h
  579. --- linux-3.14.4.orig/fs/yaffs2/yaffs_allocator.h 1970-01-01 01:00:00.000000000 +0100
  580. +++ linux-3.14.4/fs/yaffs2/yaffs_allocator.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_attribs.c linux-3.14.4/fs/yaffs2/yaffs_attribs.c
  613. --- linux-3.14.4.orig/fs/yaffs2/yaffs_attribs.c 1970-01-01 01:00:00.000000000 +0100
  614. +++ linux-3.14.4/fs/yaffs2/yaffs_attribs.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_attribs.h linux-3.14.4/fs/yaffs2/yaffs_attribs.h
  783. --- linux-3.14.4.orig/fs/yaffs2/yaffs_attribs.h 1970-01-01 01:00:00.000000000 +0100
  784. +++ linux-3.14.4/fs/yaffs2/yaffs_attribs.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_bitmap.c linux-3.14.4/fs/yaffs2/yaffs_bitmap.c
  815. --- linux-3.14.4.orig/fs/yaffs2/yaffs_bitmap.c 1970-01-01 01:00:00.000000000 +0100
  816. +++ linux-3.14.4/fs/yaffs2/yaffs_bitmap.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_bitmap.h linux-3.14.4/fs/yaffs2/yaffs_bitmap.h
  916. --- linux-3.14.4.orig/fs/yaffs2/yaffs_bitmap.h 1970-01-01 01:00:00.000000000 +0100
  917. +++ linux-3.14.4/fs/yaffs2/yaffs_bitmap.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_checkptrw.c linux-3.14.4/fs/yaffs2/yaffs_checkptrw.c
  953. --- linux-3.14.4.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
  954. +++ linux-3.14.4/fs/yaffs2/yaffs_checkptrw.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_checkptrw.h linux-3.14.4/fs/yaffs2/yaffs_checkptrw.h
  1431. --- linux-3.14.4.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
  1432. +++ linux-3.14.4/fs/yaffs2/yaffs_checkptrw.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_ecc.c linux-3.14.4/fs/yaffs2/yaffs_ecc.c
  1468. --- linux-3.14.4.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
  1469. +++ linux-3.14.4/fs/yaffs2/yaffs_ecc.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_ecc.h linux-3.14.4/fs/yaffs2/yaffs_ecc.h
  1753. --- linux-3.14.4.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
  1754. +++ linux-3.14.4/fs/yaffs2/yaffs_ecc.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_getblockinfo.h linux-3.14.4/fs/yaffs2/yaffs_getblockinfo.h
  1801. --- linux-3.14.4.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
  1802. +++ linux-3.14.4/fs/yaffs2/yaffs_getblockinfo.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_guts.c linux-3.14.4/fs/yaffs2/yaffs_guts.c
  1840. --- linux-3.14.4.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
  1841. +++ linux-3.14.4/fs/yaffs2/yaffs_guts.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_guts.h linux-3.14.4/fs/yaffs2/yaffs_guts.h
  6990. --- linux-3.14.4.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
  6991. +++ linux-3.14.4/fs/yaffs2/yaffs_guts.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_linux.h linux-3.14.4/fs/yaffs2/yaffs_linux.h
  8001. --- linux-3.14.4.orig/fs/yaffs2/yaffs_linux.h 1970-01-01 01:00:00.000000000 +0100
  8002. +++ linux-3.14.4/fs/yaffs2/yaffs_linux.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_mtdif.c linux-3.14.4/fs/yaffs2/yaffs_mtdif.c
  8053. --- linux-3.14.4.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
  8054. +++ linux-3.14.4/fs/yaffs2/yaffs_mtdif.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_mtdif.h linux-3.14.4/fs/yaffs2/yaffs_mtdif.h
  8366. --- linux-3.14.4.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
  8367. +++ linux-3.14.4/fs/yaffs2/yaffs_mtdif.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_nameval.c linux-3.14.4/fs/yaffs2/yaffs_nameval.c
  8395. --- linux-3.14.4.orig/fs/yaffs2/yaffs_nameval.c 1970-01-01 01:00:00.000000000 +0100
  8396. +++ linux-3.14.4/fs/yaffs2/yaffs_nameval.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_nameval.h linux-3.14.4/fs/yaffs2/yaffs_nameval.h
  8607. --- linux-3.14.4.orig/fs/yaffs2/yaffs_nameval.h 1970-01-01 01:00:00.000000000 +0100
  8608. +++ linux-3.14.4/fs/yaffs2/yaffs_nameval.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_nand.c linux-3.14.4/fs/yaffs2/yaffs_nand.c
  8639. --- linux-3.14.4.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
  8640. +++ linux-3.14.4/fs/yaffs2/yaffs_nand.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_nand.h linux-3.14.4/fs/yaffs2/yaffs_nand.h
  8765. --- linux-3.14.4.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
  8766. +++ linux-3.14.4/fs/yaffs2/yaffs_nand.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_packedtags1.c linux-3.14.4/fs/yaffs2/yaffs_packedtags1.c
  8808. --- linux-3.14.4.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
  8809. +++ linux-3.14.4/fs/yaffs2/yaffs_packedtags1.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_packedtags1.h linux-3.14.4/fs/yaffs2/yaffs_packedtags1.h
  8868. --- linux-3.14.4.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
  8869. +++ linux-3.14.4/fs/yaffs2/yaffs_packedtags1.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_packedtags2.c linux-3.14.4/fs/yaffs2/yaffs_packedtags2.c
  8911. --- linux-3.14.4.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
  8912. +++ linux-3.14.4/fs/yaffs2/yaffs_packedtags2.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_packedtags2.h linux-3.14.4/fs/yaffs2/yaffs_packedtags2.h
  9112. --- linux-3.14.4.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
  9113. +++ linux-3.14.4/fs/yaffs2/yaffs_packedtags2.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_summary.c linux-3.14.4/fs/yaffs2/yaffs_summary.c
  9163. --- linux-3.14.4.orig/fs/yaffs2/yaffs_summary.c 1970-01-01 01:00:00.000000000 +0100
  9164. +++ linux-3.14.4/fs/yaffs2/yaffs_summary.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_summary.h linux-3.14.4/fs/yaffs2/yaffs_summary.h
  9479. --- linux-3.14.4.orig/fs/yaffs2/yaffs_summary.h 1970-01-01 01:00:00.000000000 +0100
  9480. +++ linux-3.14.4/fs/yaffs2/yaffs_summary.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_tagscompat.c linux-3.14.4/fs/yaffs2/yaffs_tagscompat.c
  9520. --- linux-3.14.4.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
  9521. +++ linux-3.14.4/fs/yaffs2/yaffs_tagscompat.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_tagscompat.h linux-3.14.4/fs/yaffs2/yaffs_tagscompat.h
  9905. --- linux-3.14.4.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
  9906. +++ linux-3.14.4/fs/yaffs2/yaffs_tagscompat.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_tagsmarshall.c linux-3.14.4/fs/yaffs2/yaffs_tagsmarshall.c
  9953. --- linux-3.14.4.orig/fs/yaffs2/yaffs_tagsmarshall.c 1970-01-01 01:00:00.000000000 +0100
  9954. +++ linux-3.14.4/fs/yaffs2/yaffs_tagsmarshall.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_tagsmarshall.h linux-3.14.4/fs/yaffs2/yaffs_tagsmarshall.h
  10156. --- linux-3.14.4.orig/fs/yaffs2/yaffs_tagsmarshall.h 1970-01-01 01:00:00.000000000 +0100
  10157. +++ linux-3.14.4/fs/yaffs2/yaffs_tagsmarshall.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_trace.h linux-3.14.4/fs/yaffs2/yaffs_trace.h
  10182. --- linux-3.14.4.orig/fs/yaffs2/yaffs_trace.h 1970-01-01 01:00:00.000000000 +0100
  10183. +++ linux-3.14.4/fs/yaffs2/yaffs_trace.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_verify.c linux-3.14.4/fs/yaffs2/yaffs_verify.c
  10243. --- linux-3.14.4.orig/fs/yaffs2/yaffs_verify.c 1970-01-01 01:00:00.000000000 +0100
  10244. +++ linux-3.14.4/fs/yaffs2/yaffs_verify.c 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_verify.h linux-3.14.4/fs/yaffs2/yaffs_verify.h
  10776. --- linux-3.14.4.orig/fs/yaffs2/yaffs_verify.h 1970-01-01 01:00:00.000000000 +0100
  10777. +++ linux-3.14.4/fs/yaffs2/yaffs_verify.h 2014-05-17 02:22:30.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.14.4.orig/fs/yaffs2/yaffs_vfs.c linux-3.14.4/fs/yaffs2/yaffs_vfs.c
  10823. --- linux-3.14.4.orig/fs/yaffs2/yaffs_vfs.c 1970-01-01 01:00:00.000000000 +0100
  10824. +++ linux-3.14.4/fs/yaffs2/yaffs_vfs.c 2014-05-17 02:22:30.000000000 +0200
  10825. @@ -0,0 +1,3600 @@
  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. + ret = vfs_readlink(dentry, buffer, buflen, alias);
  11879. + kfree(alias);
  11880. + return ret;
  11881. +}
  11882. +
  11883. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11884. +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11885. +{
  11886. + void *ret;
  11887. +#else
  11888. +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
  11889. +{
  11890. + int ret
  11891. +#endif
  11892. + unsigned char *alias;
  11893. + int ret_int = 0;
  11894. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  11895. +
  11896. + yaffs_gross_lock(dev);
  11897. +
  11898. + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
  11899. + yaffs_gross_unlock(dev);
  11900. +
  11901. + if (!alias) {
  11902. + ret_int = -ENOMEM;
  11903. + goto out;
  11904. + }
  11905. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11906. + nd_set_link(nd, alias);
  11907. + ret = alias;
  11908. +out:
  11909. + if (ret_int)
  11910. + ret = ERR_PTR(ret_int);
  11911. + return ret;
  11912. +#else
  11913. + ret = vfs_follow_link(nd, alias);
  11914. + kfree(alias);
  11915. +out:
  11916. + if (ret_int)
  11917. + ret = ret_int;
  11918. + return ret;
  11919. +#endif
  11920. +}
  11921. +
  11922. +
  11923. +#ifdef YAFFS_HAS_PUT_INODE
  11924. +
  11925. +/* For now put inode is just for debugging
  11926. + * Put inode is called when the inode **structure** is put.
  11927. + */
  11928. +static void yaffs_put_inode(struct inode *inode)
  11929. +{
  11930. + yaffs_trace(YAFFS_TRACE_OS,
  11931. + "yaffs_put_inode: ino %d, count %d"),
  11932. + (int)inode->i_ino, atomic_read(&inode->i_count);
  11933. +
  11934. +}
  11935. +#endif
  11936. +
  11937. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11938. +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
  11939. +{
  11940. + kfree(alias);
  11941. +}
  11942. +#endif
  11943. +
  11944. +static const struct inode_operations yaffs_symlink_inode_operations = {
  11945. + .readlink = yaffs_readlink,
  11946. + .follow_link = yaffs_follow_link,
  11947. +#if (YAFFS_NEW_FOLLOW_LINK == 1)
  11948. + .put_link = yaffs_put_link,
  11949. +#endif
  11950. + .setattr = yaffs_setattr,
  11951. + .setxattr = yaffs_setxattr,
  11952. + .getxattr = yaffs_getxattr,
  11953. + .listxattr = yaffs_listxattr,
  11954. + .removexattr = yaffs_removexattr,
  11955. +};
  11956. +
  11957. +#ifdef YAFFS_USE_OWN_IGET
  11958. +
  11959. +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
  11960. +{
  11961. + struct inode *inode;
  11962. + struct yaffs_obj *obj;
  11963. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  11964. +
  11965. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
  11966. +
  11967. + inode = iget_locked(sb, ino);
  11968. + if (!inode)
  11969. + return ERR_PTR(-ENOMEM);
  11970. + if (!(inode->i_state & I_NEW))
  11971. + return inode;
  11972. +
  11973. + /* NB This is called as a side effect of other functions, but
  11974. + * we had to release the lock to prevent deadlocks, so
  11975. + * need to lock again.
  11976. + */
  11977. +
  11978. + yaffs_gross_lock(dev);
  11979. +
  11980. + obj = yaffs_find_by_number(dev, inode->i_ino);
  11981. +
  11982. + yaffs_fill_inode_from_obj(inode, obj);
  11983. +
  11984. + yaffs_gross_unlock(dev);
  11985. +
  11986. + unlock_new_inode(inode);
  11987. + return inode;
  11988. +}
  11989. +
  11990. +#else
  11991. +
  11992. +static void yaffs_read_inode(struct inode *inode)
  11993. +{
  11994. + /* NB This is called as a side effect of other functions, but
  11995. + * we had to release the lock to prevent deadlocks, so
  11996. + * need to lock again.
  11997. + */
  11998. +
  11999. + struct yaffs_obj *obj;
  12000. + struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
  12001. +
  12002. + yaffs_trace(YAFFS_TRACE_OS,
  12003. + "yaffs_read_inode for %d", (int)inode->i_ino);
  12004. +
  12005. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12006. + yaffs_gross_lock(dev);
  12007. +
  12008. + obj = yaffs_find_by_number(dev, inode->i_ino);
  12009. +
  12010. + yaffs_fill_inode_from_obj(inode, obj);
  12011. +
  12012. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12013. + yaffs_gross_unlock(dev);
  12014. +}
  12015. +
  12016. +#endif
  12017. +
  12018. +
  12019. +
  12020. +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
  12021. + struct yaffs_obj *obj)
  12022. +{
  12023. + struct inode *inode;
  12024. +
  12025. + if (!sb) {
  12026. + yaffs_trace(YAFFS_TRACE_OS,
  12027. + "yaffs_get_inode for NULL super_block!!");
  12028. + return NULL;
  12029. +
  12030. + }
  12031. +
  12032. + if (!obj) {
  12033. + yaffs_trace(YAFFS_TRACE_OS,
  12034. + "yaffs_get_inode for NULL object!!");
  12035. + return NULL;
  12036. +
  12037. + }
  12038. +
  12039. + yaffs_trace(YAFFS_TRACE_OS,
  12040. + "yaffs_get_inode for object %d", obj->obj_id);
  12041. +
  12042. + inode = Y_IGET(sb, obj->obj_id);
  12043. + if (IS_ERR(inode))
  12044. + return NULL;
  12045. +
  12046. + /* NB Side effect: iget calls back to yaffs_read_inode(). */
  12047. + /* iget also increments the inode's i_count */
  12048. + /* NB You can't be holding gross_lock or deadlock will happen! */
  12049. +
  12050. + return inode;
  12051. +}
  12052. +
  12053. +
  12054. +
  12055. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
  12056. +#define YCRED(x) x
  12057. +#else
  12058. +#define YCRED(x) (x->cred)
  12059. +#endif
  12060. +
  12061. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12062. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  12063. + dev_t rdev)
  12064. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12065. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12066. + dev_t rdev)
  12067. +#else
  12068. +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  12069. + int rdev)
  12070. +#endif
  12071. +{
  12072. + struct inode *inode;
  12073. +
  12074. + struct yaffs_obj *obj = NULL;
  12075. + struct yaffs_dev *dev;
  12076. +
  12077. + struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
  12078. +
  12079. + int error = -ENOSPC;
  12080. + uid_t uid = YCRED_FSUID();
  12081. + gid_t gid =
  12082. + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
  12083. +
  12084. + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
  12085. + mode |= S_ISGID;
  12086. +
  12087. + if (parent) {
  12088. + yaffs_trace(YAFFS_TRACE_OS,
  12089. + "yaffs_mknod: parent object %d type %d",
  12090. + parent->obj_id, parent->variant_type);
  12091. + } else {
  12092. + yaffs_trace(YAFFS_TRACE_OS,
  12093. + "yaffs_mknod: could not get parent object");
  12094. + return -EPERM;
  12095. + }
  12096. +
  12097. + yaffs_trace(YAFFS_TRACE_OS,
  12098. + "yaffs_mknod: making oject for %s, mode %x dev %x",
  12099. + dentry->d_name.name, mode, rdev);
  12100. +
  12101. + dev = parent->my_dev;
  12102. +
  12103. + yaffs_gross_lock(dev);
  12104. +
  12105. + switch (mode & S_IFMT) {
  12106. + default:
  12107. + /* Special (socket, fifo, device...) */
  12108. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
  12109. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12110. + obj =
  12111. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12112. + gid, old_encode_dev(rdev));
  12113. +#else
  12114. + obj =
  12115. + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
  12116. + gid, rdev);
  12117. +#endif
  12118. + break;
  12119. + case S_IFREG: /* file */
  12120. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
  12121. + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
  12122. + gid);
  12123. + break;
  12124. + case S_IFDIR: /* directory */
  12125. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
  12126. + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
  12127. + uid, gid);
  12128. + break;
  12129. + case S_IFLNK: /* symlink */
  12130. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
  12131. + obj = NULL; /* Do we ever get here? */
  12132. + break;
  12133. + }
  12134. +
  12135. + /* Can not call yaffs_get_inode() with gross lock held */
  12136. + yaffs_gross_unlock(dev);
  12137. +
  12138. + if (obj) {
  12139. + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
  12140. + d_instantiate(dentry, inode);
  12141. + update_dir_time(dir);
  12142. + yaffs_trace(YAFFS_TRACE_OS,
  12143. + "yaffs_mknod created object %d count = %d",
  12144. + obj->obj_id, atomic_read(&inode->i_count));
  12145. + error = 0;
  12146. + yaffs_fill_inode_from_obj(dir, parent);
  12147. + } else {
  12148. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
  12149. + error = -ENOMEM;
  12150. + }
  12151. +
  12152. + return error;
  12153. +}
  12154. +
  12155. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12156. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  12157. +#else
  12158. +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  12159. +#endif
  12160. +{
  12161. + int ret_val;
  12162. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
  12163. + ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
  12164. + return ret_val;
  12165. +}
  12166. +
  12167. +
  12168. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
  12169. +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  12170. + bool dummy)
  12171. +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
  12172. +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  12173. + struct nameidata *n)
  12174. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12175. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
  12176. + struct nameidata *n)
  12177. +#else
  12178. +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
  12179. +#endif
  12180. +{
  12181. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
  12182. + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
  12183. +}
  12184. +
  12185. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
  12186. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  12187. + unsigned int dummy)
  12188. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12189. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
  12190. + struct nameidata *n)
  12191. +#else
  12192. +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
  12193. +#endif
  12194. +{
  12195. + struct yaffs_obj *obj;
  12196. + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
  12197. +
  12198. + struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
  12199. +
  12200. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12201. + yaffs_gross_lock(dev);
  12202. +
  12203. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
  12204. + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
  12205. +
  12206. + obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
  12207. +
  12208. + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
  12209. +
  12210. + /* Can't hold gross lock when calling yaffs_get_inode() */
  12211. + if (current != yaffs_dev_to_lc(dev)->readdir_process)
  12212. + yaffs_gross_unlock(dev);
  12213. +
  12214. + if (obj) {
  12215. + yaffs_trace(YAFFS_TRACE_OS,
  12216. + "yaffs_lookup found %d", obj->obj_id);
  12217. +
  12218. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12219. + } else {
  12220. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
  12221. +
  12222. + }
  12223. +
  12224. +/* added NCB for 2.5/6 compatability - forces add even if inode is
  12225. + * NULL which creates dentry hash */
  12226. + d_add(dentry, inode);
  12227. +
  12228. + return NULL;
  12229. +}
  12230. +
  12231. +/*
  12232. + * Create a link...
  12233. + */
  12234. +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
  12235. + struct dentry *dentry)
  12236. +{
  12237. + struct inode *inode = old_dentry->d_inode;
  12238. + struct yaffs_obj *obj = NULL;
  12239. + struct yaffs_obj *link = NULL;
  12240. + struct yaffs_dev *dev;
  12241. +
  12242. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
  12243. +
  12244. + obj = yaffs_inode_to_obj(inode);
  12245. + dev = obj->my_dev;
  12246. +
  12247. + yaffs_gross_lock(dev);
  12248. +
  12249. + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
  12250. + link =
  12251. + yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12252. + obj);
  12253. +
  12254. + if (link) {
  12255. + set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
  12256. + d_instantiate(dentry, old_dentry->d_inode);
  12257. + atomic_inc(&old_dentry->d_inode->i_count);
  12258. + yaffs_trace(YAFFS_TRACE_OS,
  12259. + "yaffs_link link count %d i_count %d",
  12260. + old_dentry->d_inode->i_nlink,
  12261. + atomic_read(&old_dentry->d_inode->i_count));
  12262. + }
  12263. +
  12264. + yaffs_gross_unlock(dev);
  12265. +
  12266. + if (link) {
  12267. + update_dir_time(dir);
  12268. + return 0;
  12269. + }
  12270. +
  12271. + return -EPERM;
  12272. +}
  12273. +
  12274. +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
  12275. + const char *symname)
  12276. +{
  12277. + struct yaffs_obj *obj;
  12278. + struct yaffs_dev *dev;
  12279. + uid_t uid = YCRED_FSUID();
  12280. + gid_t gid =
  12281. + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
  12282. +
  12283. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
  12284. +
  12285. + if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
  12286. + YAFFS_MAX_NAME_LENGTH)
  12287. + return -ENAMETOOLONG;
  12288. +
  12289. + if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
  12290. + YAFFS_MAX_ALIAS_LENGTH)
  12291. + return -ENAMETOOLONG;
  12292. +
  12293. + dev = yaffs_inode_to_obj(dir)->my_dev;
  12294. + yaffs_gross_lock(dev);
  12295. + obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
  12296. + S_IFLNK | S_IRWXUGO, uid, gid, symname);
  12297. + yaffs_gross_unlock(dev);
  12298. +
  12299. + if (obj) {
  12300. + struct inode *inode;
  12301. +
  12302. + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
  12303. + d_instantiate(dentry, inode);
  12304. + update_dir_time(dir);
  12305. + yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
  12306. + return 0;
  12307. + } else {
  12308. + yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
  12309. + }
  12310. +
  12311. + return -ENOMEM;
  12312. +}
  12313. +
  12314. +/*
  12315. + * The VFS layer already does all the dentry stuff for rename.
  12316. + *
  12317. + * NB: POSIX says you can rename an object over an old object of the same name
  12318. + */
  12319. +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
  12320. + struct inode *new_dir, struct dentry *new_dentry)
  12321. +{
  12322. + struct yaffs_dev *dev;
  12323. + int ret_val = YAFFS_FAIL;
  12324. + struct yaffs_obj *target;
  12325. +
  12326. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
  12327. + dev = yaffs_inode_to_obj(old_dir)->my_dev;
  12328. +
  12329. + yaffs_gross_lock(dev);
  12330. +
  12331. + /* Check if the target is an existing directory that is not empty. */
  12332. + target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
  12333. + new_dentry->d_name.name);
  12334. +
  12335. + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
  12336. + !list_empty(&target->variant.dir_variant.children)) {
  12337. +
  12338. + yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
  12339. +
  12340. + ret_val = YAFFS_FAIL;
  12341. + } else {
  12342. + /* Now does unlinking internally using shadowing mechanism */
  12343. + yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
  12344. +
  12345. + ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
  12346. + old_dentry->d_name.name,
  12347. + yaffs_inode_to_obj(new_dir),
  12348. + new_dentry->d_name.name);
  12349. + }
  12350. + yaffs_gross_unlock(dev);
  12351. +
  12352. + if (ret_val == YAFFS_OK) {
  12353. + if (target)
  12354. + inode_dec_link_count(new_dentry->d_inode);
  12355. +
  12356. + update_dir_time(old_dir);
  12357. + if (old_dir != new_dir)
  12358. + update_dir_time(new_dir);
  12359. + return 0;
  12360. + } else {
  12361. + return -ENOTEMPTY;
  12362. + }
  12363. +}
  12364. +
  12365. +
  12366. +
  12367. +
  12368. +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
  12369. +{
  12370. + int ret_val;
  12371. +
  12372. + struct yaffs_dev *dev;
  12373. + struct yaffs_obj *obj;
  12374. +
  12375. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
  12376. + (int)(dir->i_ino), dentry->d_name.name);
  12377. + obj = yaffs_inode_to_obj(dir);
  12378. + dev = obj->my_dev;
  12379. +
  12380. + yaffs_gross_lock(dev);
  12381. +
  12382. + ret_val = yaffs_unlinker(obj, dentry->d_name.name);
  12383. +
  12384. + if (ret_val == YAFFS_OK) {
  12385. + inode_dec_link_count(dentry->d_inode);
  12386. + dir->i_version++;
  12387. + yaffs_gross_unlock(dev);
  12388. + update_dir_time(dir);
  12389. + return 0;
  12390. + }
  12391. + yaffs_gross_unlock(dev);
  12392. + return -ENOTEMPTY;
  12393. +}
  12394. +
  12395. +
  12396. +
  12397. +static const struct inode_operations yaffs_dir_inode_operations = {
  12398. + .create = yaffs_create,
  12399. + .lookup = yaffs_lookup,
  12400. + .link = yaffs_link,
  12401. + .unlink = yaffs_unlink,
  12402. + .symlink = yaffs_symlink,
  12403. + .mkdir = yaffs_mkdir,
  12404. + .rmdir = yaffs_unlink,
  12405. + .mknod = yaffs_mknod,
  12406. + .rename = yaffs_rename,
  12407. + .setattr = yaffs_setattr,
  12408. + .setxattr = yaffs_setxattr,
  12409. + .getxattr = yaffs_getxattr,
  12410. + .listxattr = yaffs_listxattr,
  12411. + .removexattr = yaffs_removexattr,
  12412. +};
  12413. +
  12414. +/*-----------------------------------------------------------------*/
  12415. +/* Directory search context allows us to unlock access to yaffs during
  12416. + * filldir without causing problems with the directory being modified.
  12417. + * This is similar to the tried and tested mechanism used in yaffs direct.
  12418. + *
  12419. + * A search context iterates along a doubly linked list of siblings in the
  12420. + * directory. If the iterating object is deleted then this would corrupt
  12421. + * the list iteration, likely causing a crash. The search context avoids
  12422. + * this by using the remove_obj_fn to move the search context to the
  12423. + * next object before the object is deleted.
  12424. + *
  12425. + * Many readdirs (and thus seach conexts) may be alive simulateously so
  12426. + * each struct yaffs_dev has a list of these.
  12427. + *
  12428. + * A seach context lives for the duration of a readdir.
  12429. + *
  12430. + * All these functions must be called while yaffs is locked.
  12431. + */
  12432. +
  12433. +struct yaffs_search_context {
  12434. + struct yaffs_dev *dev;
  12435. + struct yaffs_obj *dir_obj;
  12436. + struct yaffs_obj *next_return;
  12437. + struct list_head others;
  12438. +};
  12439. +
  12440. +/*
  12441. + * yaffs_new_search() creates a new search context, initialises it and
  12442. + * adds it to the device's search context list.
  12443. + *
  12444. + * Called at start of readdir.
  12445. + */
  12446. +static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
  12447. +{
  12448. + struct yaffs_dev *dev = dir->my_dev;
  12449. + struct yaffs_search_context *sc =
  12450. + kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
  12451. + if (sc) {
  12452. + sc->dir_obj = dir;
  12453. + sc->dev = dev;
  12454. + if (list_empty(&sc->dir_obj->variant.dir_variant.children))
  12455. + sc->next_return = NULL;
  12456. + else
  12457. + sc->next_return =
  12458. + list_entry(dir->variant.dir_variant.children.next,
  12459. + struct yaffs_obj, siblings);
  12460. + INIT_LIST_HEAD(&sc->others);
  12461. + list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
  12462. + }
  12463. + return sc;
  12464. +}
  12465. +
  12466. +/*
  12467. + * yaffs_search_end() disposes of a search context and cleans up.
  12468. + */
  12469. +static void yaffs_search_end(struct yaffs_search_context *sc)
  12470. +{
  12471. + if (sc) {
  12472. + list_del(&sc->others);
  12473. + kfree(sc);
  12474. + }
  12475. +}
  12476. +
  12477. +/*
  12478. + * yaffs_search_advance() moves a search context to the next object.
  12479. + * Called when the search iterates or when an object removal causes
  12480. + * the search context to be moved to the next object.
  12481. + */
  12482. +static void yaffs_search_advance(struct yaffs_search_context *sc)
  12483. +{
  12484. + if (!sc)
  12485. + return;
  12486. +
  12487. + if (sc->next_return == NULL ||
  12488. + list_empty(&sc->dir_obj->variant.dir_variant.children))
  12489. + sc->next_return = NULL;
  12490. + else {
  12491. + struct list_head *next = sc->next_return->siblings.next;
  12492. +
  12493. + if (next == &sc->dir_obj->variant.dir_variant.children)
  12494. + sc->next_return = NULL; /* end of list */
  12495. + else
  12496. + sc->next_return =
  12497. + list_entry(next, struct yaffs_obj, siblings);
  12498. + }
  12499. +}
  12500. +
  12501. +/*
  12502. + * yaffs_remove_obj_callback() is called when an object is unlinked.
  12503. + * We check open search contexts and advance any which are currently
  12504. + * on the object being iterated.
  12505. + */
  12506. +static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
  12507. +{
  12508. +
  12509. + struct list_head *i;
  12510. + struct yaffs_search_context *sc;
  12511. + struct list_head *search_contexts =
  12512. + &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
  12513. +
  12514. + /* Iterate through the directory search contexts.
  12515. + * If any are currently on the object being removed, then advance
  12516. + * the search context to the next object to prevent a hanging pointer.
  12517. + */
  12518. + list_for_each(i, search_contexts) {
  12519. + sc = list_entry(i, struct yaffs_search_context, others);
  12520. + if (sc->next_return == obj)
  12521. + yaffs_search_advance(sc);
  12522. + }
  12523. +
  12524. +}
  12525. +
  12526. +
  12527. +/*-----------------------------------------------------------------*/
  12528. +
  12529. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
  12530. +static int yaffs_readdir(struct file *file, struct dir_context *ctx)
  12531. +{
  12532. + struct yaffs_obj *obj;
  12533. + struct yaffs_dev *dev;
  12534. + struct yaffs_search_context *sc;
  12535. + struct inode *inode = file->f_dentry->d_inode;
  12536. + unsigned long offset, curoffs;
  12537. + struct yaffs_obj *l;
  12538. + int ret_val = 0;
  12539. +
  12540. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12541. +
  12542. + obj = yaffs_dentry_to_obj(file->f_dentry);
  12543. + dev = obj->my_dev;
  12544. +
  12545. + yaffs_gross_lock(dev);
  12546. +
  12547. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12548. +
  12549. + offset = ctx->pos;
  12550. +
  12551. + sc = yaffs_new_search(obj);
  12552. + if (!sc) {
  12553. + ret_val = -ENOMEM;
  12554. + goto out;
  12555. + }
  12556. +
  12557. + yaffs_trace(YAFFS_TRACE_OS,
  12558. + "yaffs_readdir: starting at %d", (int)offset);
  12559. +
  12560. + if (offset == 0) {
  12561. + yaffs_trace(YAFFS_TRACE_OS,
  12562. + "yaffs_readdir: entry . ino %d",
  12563. + (int)inode->i_ino);
  12564. + yaffs_gross_unlock(dev);
  12565. + if (!dir_emit_dot(file, ctx)) {
  12566. + yaffs_gross_lock(dev);
  12567. + goto out;
  12568. + }
  12569. + yaffs_gross_lock(dev);
  12570. + offset++;
  12571. + ctx->pos++;
  12572. + }
  12573. + if (offset == 1) {
  12574. + yaffs_trace(YAFFS_TRACE_OS,
  12575. + "yaffs_readdir: entry .. ino %d",
  12576. + (int)file->f_dentry->d_parent->d_inode->i_ino);
  12577. + yaffs_gross_unlock(dev);
  12578. + if (!dir_emit_dotdot(file, ctx)) {
  12579. + yaffs_gross_lock(dev);
  12580. + goto out;
  12581. + }
  12582. + yaffs_gross_lock(dev);
  12583. + offset++;
  12584. + ctx->pos++;
  12585. + }
  12586. +
  12587. + curoffs = 1;
  12588. +
  12589. + /* If the directory has changed since the open or last call to
  12590. + readdir, rewind to after the 2 canned entries. */
  12591. + if (file->f_version != inode->i_version) {
  12592. + offset = 2;
  12593. + ctx->pos = offset;
  12594. + file->f_version = inode->i_version;
  12595. + }
  12596. +
  12597. + while (sc->next_return) {
  12598. + curoffs++;
  12599. + l = sc->next_return;
  12600. + if (curoffs >= offset) {
  12601. + int this_inode = yaffs_get_obj_inode(l);
  12602. + int this_type = yaffs_get_obj_type(l);
  12603. +
  12604. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12605. + yaffs_trace(YAFFS_TRACE_OS,
  12606. + "yaffs_readdir: %s inode %d",
  12607. + name, yaffs_get_obj_inode(l));
  12608. +
  12609. + yaffs_gross_unlock(dev);
  12610. +
  12611. + if (!dir_emit(ctx, name, strlen(name),
  12612. + this_inode, this_type) < 0) {
  12613. + yaffs_gross_lock(dev);
  12614. + goto out;
  12615. + }
  12616. +
  12617. + yaffs_gross_lock(dev);
  12618. +
  12619. + offset++;
  12620. + ctx->pos++;
  12621. + }
  12622. + yaffs_search_advance(sc);
  12623. + }
  12624. +
  12625. +out:
  12626. + yaffs_search_end(sc);
  12627. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12628. + yaffs_gross_unlock(dev);
  12629. +
  12630. + return ret_val;
  12631. +}
  12632. +#else
  12633. +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
  12634. +{
  12635. + struct yaffs_obj *obj;
  12636. + struct yaffs_dev *dev;
  12637. + struct yaffs_search_context *sc;
  12638. + struct inode *inode = f->f_dentry->d_inode;
  12639. + unsigned long offset, curoffs;
  12640. + struct yaffs_obj *l;
  12641. + int ret_val = 0;
  12642. +
  12643. + char name[YAFFS_MAX_NAME_LENGTH + 1];
  12644. +
  12645. + obj = yaffs_dentry_to_obj(f->f_dentry);
  12646. + dev = obj->my_dev;
  12647. +
  12648. + yaffs_gross_lock(dev);
  12649. +
  12650. + yaffs_dev_to_lc(dev)->readdir_process = current;
  12651. +
  12652. + offset = f->f_pos;
  12653. +
  12654. + sc = yaffs_new_search(obj);
  12655. + if (!sc) {
  12656. + ret_val = -ENOMEM;
  12657. + goto out;
  12658. + }
  12659. +
  12660. + yaffs_trace(YAFFS_TRACE_OS,
  12661. + "yaffs_readdir: starting at %d", (int)offset);
  12662. +
  12663. + if (offset == 0) {
  12664. + yaffs_trace(YAFFS_TRACE_OS,
  12665. + "yaffs_readdir: entry . ino %d",
  12666. + (int)inode->i_ino);
  12667. + yaffs_gross_unlock(dev);
  12668. + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
  12669. + yaffs_gross_lock(dev);
  12670. + goto out;
  12671. + }
  12672. + yaffs_gross_lock(dev);
  12673. + offset++;
  12674. + f->f_pos++;
  12675. + }
  12676. + if (offset == 1) {
  12677. + yaffs_trace(YAFFS_TRACE_OS,
  12678. + "yaffs_readdir: entry .. ino %d",
  12679. + (int)f->f_dentry->d_parent->d_inode->i_ino);
  12680. + yaffs_gross_unlock(dev);
  12681. + if (filldir(dirent, "..", 2, offset,
  12682. + f->f_dentry->d_parent->d_inode->i_ino,
  12683. + DT_DIR) < 0) {
  12684. + yaffs_gross_lock(dev);
  12685. + goto out;
  12686. + }
  12687. + yaffs_gross_lock(dev);
  12688. + offset++;
  12689. + f->f_pos++;
  12690. + }
  12691. +
  12692. + curoffs = 1;
  12693. +
  12694. + /* If the directory has changed since the open or last call to
  12695. + readdir, rewind to after the 2 canned entries. */
  12696. + if (f->f_version != inode->i_version) {
  12697. + offset = 2;
  12698. + f->f_pos = offset;
  12699. + f->f_version = inode->i_version;
  12700. + }
  12701. +
  12702. + while (sc->next_return) {
  12703. + curoffs++;
  12704. + l = sc->next_return;
  12705. + if (curoffs >= offset) {
  12706. + int this_inode = yaffs_get_obj_inode(l);
  12707. + int this_type = yaffs_get_obj_type(l);
  12708. +
  12709. + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
  12710. + yaffs_trace(YAFFS_TRACE_OS,
  12711. + "yaffs_readdir: %s inode %d",
  12712. + name, yaffs_get_obj_inode(l));
  12713. +
  12714. + yaffs_gross_unlock(dev);
  12715. +
  12716. + if (filldir(dirent,
  12717. + name,
  12718. + strlen(name),
  12719. + offset, this_inode, this_type) < 0) {
  12720. + yaffs_gross_lock(dev);
  12721. + goto out;
  12722. + }
  12723. +
  12724. + yaffs_gross_lock(dev);
  12725. +
  12726. + offset++;
  12727. + f->f_pos++;
  12728. + }
  12729. + yaffs_search_advance(sc);
  12730. + }
  12731. +
  12732. +out:
  12733. + yaffs_search_end(sc);
  12734. + yaffs_dev_to_lc(dev)->readdir_process = NULL;
  12735. + yaffs_gross_unlock(dev);
  12736. +
  12737. + return ret_val;
  12738. +}
  12739. +#endif
  12740. +
  12741. +static const struct file_operations yaffs_dir_operations = {
  12742. + .read = generic_read_dir,
  12743. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
  12744. + .iterate = yaffs_readdir,
  12745. +#else
  12746. + .readdir = yaffs_readdir,
  12747. +#endif
  12748. + .fsync = yaffs_sync_object,
  12749. + .llseek = generic_file_llseek,
  12750. +};
  12751. +
  12752. +static void yaffs_fill_inode_from_obj(struct inode *inode,
  12753. + struct yaffs_obj *obj)
  12754. +{
  12755. + if (inode && obj) {
  12756. +
  12757. + /* Check mode against the variant type and attempt to repair if broken. */
  12758. + u32 mode = obj->yst_mode;
  12759. + switch (obj->variant_type) {
  12760. + case YAFFS_OBJECT_TYPE_FILE:
  12761. + if (!S_ISREG(mode)) {
  12762. + obj->yst_mode &= ~S_IFMT;
  12763. + obj->yst_mode |= S_IFREG;
  12764. + }
  12765. +
  12766. + break;
  12767. + case YAFFS_OBJECT_TYPE_SYMLINK:
  12768. + if (!S_ISLNK(mode)) {
  12769. + obj->yst_mode &= ~S_IFMT;
  12770. + obj->yst_mode |= S_IFLNK;
  12771. + }
  12772. +
  12773. + break;
  12774. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  12775. + if (!S_ISDIR(mode)) {
  12776. + obj->yst_mode &= ~S_IFMT;
  12777. + obj->yst_mode |= S_IFDIR;
  12778. + }
  12779. +
  12780. + break;
  12781. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  12782. + case YAFFS_OBJECT_TYPE_HARDLINK:
  12783. + case YAFFS_OBJECT_TYPE_SPECIAL:
  12784. + default:
  12785. + /* TODO? */
  12786. + break;
  12787. + }
  12788. +
  12789. + inode->i_flags |= S_NOATIME;
  12790. +
  12791. + inode->i_ino = obj->obj_id;
  12792. + inode->i_mode = obj->yst_mode;
  12793. + i_uid_write(inode, obj->yst_uid);
  12794. + i_gid_write(inode, obj->yst_gid);
  12795. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  12796. + inode->i_blksize = inode->i_sb->s_blocksize;
  12797. +#endif
  12798. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12799. +
  12800. + inode->i_rdev = old_decode_dev(obj->yst_rdev);
  12801. + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
  12802. + inode->i_atime.tv_nsec = 0;
  12803. + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
  12804. + inode->i_mtime.tv_nsec = 0;
  12805. + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
  12806. + inode->i_ctime.tv_nsec = 0;
  12807. +#else
  12808. + inode->i_rdev = obj->yst_rdev;
  12809. + inode->i_atime = obj->yst_atime;
  12810. + inode->i_mtime = obj->yst_mtime;
  12811. + inode->i_ctime = obj->yst_ctime;
  12812. +#endif
  12813. + inode->i_size = yaffs_get_obj_length(obj);
  12814. + inode->i_blocks = (inode->i_size + 511) >> 9;
  12815. +
  12816. + set_nlink(inode, yaffs_get_obj_link_count(obj));
  12817. +
  12818. + yaffs_trace(YAFFS_TRACE_OS,
  12819. + "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
  12820. + inode->i_mode, i_uid_read(inode), i_gid_read(inode),
  12821. + inode->i_size, atomic_read(&inode->i_count));
  12822. +
  12823. + switch (obj->yst_mode & S_IFMT) {
  12824. + default: /* fifo, device or socket */
  12825. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  12826. + init_special_inode(inode, obj->yst_mode,
  12827. + old_decode_dev(obj->yst_rdev));
  12828. +#else
  12829. + init_special_inode(inode, obj->yst_mode,
  12830. + (dev_t) (obj->yst_rdev));
  12831. +#endif
  12832. + break;
  12833. + case S_IFREG: /* file */
  12834. + inode->i_op = &yaffs_file_inode_operations;
  12835. + inode->i_fop = &yaffs_file_operations;
  12836. + inode->i_mapping->a_ops =
  12837. + &yaffs_file_address_operations;
  12838. + break;
  12839. + case S_IFDIR: /* directory */
  12840. + inode->i_op = &yaffs_dir_inode_operations;
  12841. + inode->i_fop = &yaffs_dir_operations;
  12842. + break;
  12843. + case S_IFLNK: /* symlink */
  12844. + inode->i_op = &yaffs_symlink_inode_operations;
  12845. + break;
  12846. + }
  12847. +
  12848. + yaffs_inode_to_obj_lv(inode) = obj;
  12849. +
  12850. + obj->my_inode = inode;
  12851. +
  12852. + } else {
  12853. + yaffs_trace(YAFFS_TRACE_OS,
  12854. + "yaffs_fill_inode invalid parameters");
  12855. + }
  12856. +
  12857. +}
  12858. +
  12859. +
  12860. +
  12861. +/*
  12862. + * yaffs background thread functions .
  12863. + * yaffs_bg_thread_fn() the thread function
  12864. + * yaffs_bg_start() launches the background thread.
  12865. + * yaffs_bg_stop() cleans up the background thread.
  12866. + *
  12867. + * NB:
  12868. + * The thread should only run after the yaffs is initialised
  12869. + * The thread should be stopped before yaffs is unmounted.
  12870. + * The thread should not do any writing while the fs is in read only.
  12871. + */
  12872. +
  12873. +static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
  12874. +{
  12875. + unsigned erased_chunks =
  12876. + dev->n_erased_blocks * dev->param.chunks_per_block;
  12877. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12878. + unsigned scattered = 0; /* Free chunks not in an erased block */
  12879. +
  12880. + if (erased_chunks < dev->n_free_chunks)
  12881. + scattered = (dev->n_free_chunks - erased_chunks);
  12882. +
  12883. + if (!context->bg_running)
  12884. + return 0;
  12885. + else if (scattered < (dev->param.chunks_per_block * 2))
  12886. + return 0;
  12887. + else if (erased_chunks > dev->n_free_chunks / 2)
  12888. + return 0;
  12889. + else if (erased_chunks > dev->n_free_chunks / 4)
  12890. + return 1;
  12891. + else
  12892. + return 2;
  12893. +}
  12894. +
  12895. +#ifdef YAFFS_COMPILE_BACKGROUND
  12896. +
  12897. +void yaffs_background_waker(unsigned long data)
  12898. +{
  12899. + wake_up_process((struct task_struct *)data);
  12900. +}
  12901. +
  12902. +static int yaffs_bg_thread_fn(void *data)
  12903. +{
  12904. + struct yaffs_dev *dev = (struct yaffs_dev *)data;
  12905. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12906. + unsigned long now = jiffies;
  12907. + unsigned long next_dir_update = now;
  12908. + unsigned long next_gc = now;
  12909. + unsigned long expires;
  12910. + unsigned int urgency;
  12911. +
  12912. + int gc_result;
  12913. + struct timer_list timer;
  12914. +
  12915. + yaffs_trace(YAFFS_TRACE_BACKGROUND,
  12916. + "yaffs_background starting for dev %p", (void *)dev);
  12917. +
  12918. +#ifdef YAFFS_COMPILE_FREEZER
  12919. + set_freezable();
  12920. +#endif
  12921. + while (context->bg_running) {
  12922. + yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
  12923. +
  12924. + if (kthread_should_stop())
  12925. + break;
  12926. +
  12927. +#ifdef YAFFS_COMPILE_FREEZER
  12928. + if (try_to_freeze())
  12929. + continue;
  12930. +#endif
  12931. + yaffs_gross_lock(dev);
  12932. +
  12933. + now = jiffies;
  12934. +
  12935. + if (time_after(now, next_dir_update) && yaffs_bg_enable) {
  12936. + yaffs_update_dirty_dirs(dev);
  12937. + next_dir_update = now + HZ;
  12938. + }
  12939. +
  12940. + if (time_after(now, next_gc) && yaffs_bg_enable) {
  12941. + if (!dev->is_checkpointed) {
  12942. + urgency = yaffs_bg_gc_urgency(dev);
  12943. + gc_result = yaffs_bg_gc(dev, urgency);
  12944. + if (urgency > 1)
  12945. + next_gc = now + HZ / 20 + 1;
  12946. + else if (urgency > 0)
  12947. + next_gc = now + HZ / 10 + 1;
  12948. + else
  12949. + next_gc = now + HZ * 2;
  12950. + } else {
  12951. + /*
  12952. + * gc not running so set to next_dir_update
  12953. + * to cut down on wake ups
  12954. + */
  12955. + next_gc = next_dir_update;
  12956. + }
  12957. + }
  12958. + yaffs_gross_unlock(dev);
  12959. +#if 1
  12960. + expires = next_dir_update;
  12961. + if (time_before(next_gc, expires))
  12962. + expires = next_gc;
  12963. + if (time_before(expires, now))
  12964. + expires = now + HZ;
  12965. +
  12966. + Y_INIT_TIMER(&timer);
  12967. + timer.expires = expires + 1;
  12968. + timer.data = (unsigned long)current;
  12969. + timer.function = yaffs_background_waker;
  12970. +
  12971. + set_current_state(TASK_INTERRUPTIBLE);
  12972. + add_timer(&timer);
  12973. + schedule();
  12974. + del_timer_sync(&timer);
  12975. +#else
  12976. + msleep(10);
  12977. +#endif
  12978. + }
  12979. +
  12980. + return 0;
  12981. +}
  12982. +
  12983. +static int yaffs_bg_start(struct yaffs_dev *dev)
  12984. +{
  12985. + int retval = 0;
  12986. + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
  12987. +
  12988. + if (dev->read_only)
  12989. + return -1;
  12990. +
  12991. + context->bg_running = 1;
  12992. +
  12993. + context->bg_thread = kthread_run(yaffs_bg_thread_fn,
  12994. + (void *)dev, "yaffs-bg-%d",
  12995. + context->mount_id);
  12996. +
  12997. + if (IS_ERR(context->bg_thread)) {
  12998. + retval = PTR_ERR(context->bg_thread);
  12999. + context->bg_thread = NULL;
  13000. + context->bg_running = 0;
  13001. + }
  13002. + return retval;
  13003. +}
  13004. +
  13005. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13006. +{
  13007. + struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
  13008. +
  13009. + ctxt->bg_running = 0;
  13010. +
  13011. + if (ctxt->bg_thread) {
  13012. + kthread_stop(ctxt->bg_thread);
  13013. + ctxt->bg_thread = NULL;
  13014. + }
  13015. +}
  13016. +#else
  13017. +static int yaffs_bg_thread_fn(void *data)
  13018. +{
  13019. + return 0;
  13020. +}
  13021. +
  13022. +static int yaffs_bg_start(struct yaffs_dev *dev)
  13023. +{
  13024. + return 0;
  13025. +}
  13026. +
  13027. +static void yaffs_bg_stop(struct yaffs_dev *dev)
  13028. +{
  13029. +}
  13030. +#endif
  13031. +
  13032. +
  13033. +static void yaffs_flush_inodes(struct super_block *sb)
  13034. +{
  13035. + struct inode *iptr;
  13036. + struct yaffs_obj *obj;
  13037. +
  13038. + list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
  13039. + obj = yaffs_inode_to_obj(iptr);
  13040. + if (obj) {
  13041. + yaffs_trace(YAFFS_TRACE_OS,
  13042. + "flushing obj %d",
  13043. + obj->obj_id);
  13044. + yaffs_flush_file(obj, 1, 0);
  13045. + }
  13046. + }
  13047. +}
  13048. +
  13049. +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
  13050. +{
  13051. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13052. + if (!dev)
  13053. + return;
  13054. +
  13055. + yaffs_flush_inodes(sb);
  13056. + yaffs_update_dirty_dirs(dev);
  13057. + yaffs_flush_whole_cache(dev);
  13058. + if (do_checkpoint)
  13059. + yaffs_checkpoint_save(dev);
  13060. +}
  13061. +
  13062. +static LIST_HEAD(yaffs_context_list);
  13063. +struct mutex yaffs_context_lock;
  13064. +
  13065. +static void yaffs_put_super(struct super_block *sb)
  13066. +{
  13067. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13068. + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  13069. +
  13070. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
  13071. + "yaffs_put_super");
  13072. +
  13073. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13074. + "Shutting down yaffs background thread");
  13075. + yaffs_bg_stop(dev);
  13076. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
  13077. + "yaffs background thread shut down");
  13078. +
  13079. + yaffs_gross_lock(dev);
  13080. +
  13081. + yaffs_flush_super(sb, 1);
  13082. +
  13083. + yaffs_deinitialise(dev);
  13084. +
  13085. + yaffs_gross_unlock(dev);
  13086. +
  13087. + mutex_lock(&yaffs_context_lock);
  13088. + list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
  13089. + mutex_unlock(&yaffs_context_lock);
  13090. +
  13091. + if (yaffs_dev_to_lc(dev)->spare_buffer) {
  13092. + kfree(yaffs_dev_to_lc(dev)->spare_buffer);
  13093. + yaffs_dev_to_lc(dev)->spare_buffer = NULL;
  13094. + }
  13095. +
  13096. + kfree(dev);
  13097. +
  13098. + yaffs_put_mtd_device(mtd);
  13099. +
  13100. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
  13101. + "yaffs_put_super done");
  13102. +}
  13103. +
  13104. +
  13105. +static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
  13106. +{
  13107. + return yaffs_gc_control;
  13108. +}
  13109. +
  13110. +
  13111. +#ifdef YAFFS_COMPILE_EXPORTFS
  13112. +
  13113. +static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  13114. + uint32_t generation)
  13115. +{
  13116. + return Y_IGET(sb, ino);
  13117. +}
  13118. +
  13119. +static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
  13120. + struct fid *fid, int fh_len,
  13121. + int fh_type)
  13122. +{
  13123. + return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  13124. + yaffs2_nfs_get_inode);
  13125. +}
  13126. +
  13127. +static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
  13128. + struct fid *fid, int fh_len,
  13129. + int fh_type)
  13130. +{
  13131. + return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  13132. + yaffs2_nfs_get_inode);
  13133. +}
  13134. +
  13135. +struct dentry *yaffs2_get_parent(struct dentry *dentry)
  13136. +{
  13137. +
  13138. + struct super_block *sb = dentry->d_inode->i_sb;
  13139. + struct dentry *parent = ERR_PTR(-ENOENT);
  13140. + struct inode *inode;
  13141. + unsigned long parent_ino;
  13142. + struct yaffs_obj *d_obj;
  13143. + struct yaffs_obj *parent_obj;
  13144. +
  13145. + d_obj = yaffs_inode_to_obj(dentry->d_inode);
  13146. +
  13147. + if (d_obj) {
  13148. + parent_obj = d_obj->parent;
  13149. + if (parent_obj) {
  13150. + parent_ino = yaffs_get_obj_inode(parent_obj);
  13151. + inode = Y_IGET(sb, parent_ino);
  13152. +
  13153. + if (IS_ERR(inode)) {
  13154. + parent = ERR_CAST(inode);
  13155. + } else {
  13156. + parent = d_obtain_alias(inode);
  13157. + if (!IS_ERR(parent)) {
  13158. + parent = ERR_PTR(-ENOMEM);
  13159. + iput(inode);
  13160. + }
  13161. + }
  13162. + }
  13163. + }
  13164. +
  13165. + return parent;
  13166. +}
  13167. +
  13168. +/* Just declare a zero structure as a NULL value implies
  13169. + * using the default functions of exportfs.
  13170. + */
  13171. +
  13172. +static struct export_operations yaffs_export_ops = {
  13173. + .fh_to_dentry = yaffs2_fh_to_dentry,
  13174. + .fh_to_parent = yaffs2_fh_to_parent,
  13175. + .get_parent = yaffs2_get_parent,
  13176. +};
  13177. +
  13178. +#endif
  13179. +
  13180. +static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
  13181. +{
  13182. + /* Clear the association between the inode and
  13183. + * the struct yaffs_obj.
  13184. + */
  13185. + obj->my_inode = NULL;
  13186. + yaffs_inode_to_obj_lv(inode) = NULL;
  13187. +
  13188. + /* If the object freeing was deferred, then the real
  13189. + * free happens now.
  13190. + * This should fix the inode inconsistency problem.
  13191. + */
  13192. + yaffs_handle_defered_free(obj);
  13193. +}
  13194. +
  13195. +#ifdef YAFFS_HAS_EVICT_INODE
  13196. +/* yaffs_evict_inode combines into one operation what was previously done in
  13197. + * yaffs_clear_inode() and yaffs_delete_inode()
  13198. + *
  13199. + */
  13200. +static void yaffs_evict_inode(struct inode *inode)
  13201. +{
  13202. + struct yaffs_obj *obj;
  13203. + struct yaffs_dev *dev;
  13204. + int deleteme = 0;
  13205. +
  13206. + obj = yaffs_inode_to_obj(inode);
  13207. +
  13208. + yaffs_trace(YAFFS_TRACE_OS,
  13209. + "yaffs_evict_inode: ino %d, count %d %s",
  13210. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13211. + obj ? "object exists" : "null object");
  13212. +
  13213. + if (!inode->i_nlink && !is_bad_inode(inode))
  13214. + deleteme = 1;
  13215. + truncate_inode_pages(&inode->i_data, 0);
  13216. + Y_CLEAR_INODE(inode);
  13217. +
  13218. + if (deleteme && obj) {
  13219. + dev = obj->my_dev;
  13220. + yaffs_gross_lock(dev);
  13221. + yaffs_del_obj(obj);
  13222. + yaffs_gross_unlock(dev);
  13223. + }
  13224. + if (obj) {
  13225. + dev = obj->my_dev;
  13226. + yaffs_gross_lock(dev);
  13227. + yaffs_unstitch_obj(inode, obj);
  13228. + yaffs_gross_unlock(dev);
  13229. + }
  13230. +}
  13231. +#else
  13232. +
  13233. +/* clear is called to tell the fs to release any per-inode data it holds.
  13234. + * The object might still exist on disk and is just being thrown out of the cache
  13235. + * or else the object has actually been deleted and we're being called via
  13236. + * the chain
  13237. + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
  13238. + */
  13239. +
  13240. +static void yaffs_clear_inode(struct inode *inode)
  13241. +{
  13242. + struct yaffs_obj *obj;
  13243. + struct yaffs_dev *dev;
  13244. +
  13245. + obj = yaffs_inode_to_obj(inode);
  13246. +
  13247. + yaffs_trace(YAFFS_TRACE_OS,
  13248. + "yaffs_clear_inode: ino %d, count %d %s",
  13249. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13250. + obj ? "object exists" : "null object");
  13251. +
  13252. + if (obj) {
  13253. + dev = obj->my_dev;
  13254. + yaffs_gross_lock(dev);
  13255. + yaffs_unstitch_obj(inode, obj);
  13256. + yaffs_gross_unlock(dev);
  13257. + }
  13258. +
  13259. +}
  13260. +
  13261. +/* delete is called when the link count is zero and the inode
  13262. + * is put (ie. nobody wants to know about it anymore, time to
  13263. + * delete the file).
  13264. + * NB Must call clear_inode()
  13265. + */
  13266. +static void yaffs_delete_inode(struct inode *inode)
  13267. +{
  13268. + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
  13269. + struct yaffs_dev *dev;
  13270. +
  13271. + yaffs_trace(YAFFS_TRACE_OS,
  13272. + "yaffs_delete_inode: ino %d, count %d %s",
  13273. + (int)inode->i_ino, atomic_read(&inode->i_count),
  13274. + obj ? "object exists" : "null object");
  13275. +
  13276. + if (obj) {
  13277. + dev = obj->my_dev;
  13278. + yaffs_gross_lock(dev);
  13279. + yaffs_del_obj(obj);
  13280. + yaffs_gross_unlock(dev);
  13281. + }
  13282. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
  13283. + truncate_inode_pages(&inode->i_data, 0);
  13284. +#endif
  13285. + clear_inode(inode);
  13286. +}
  13287. +#endif
  13288. +
  13289. +
  13290. +
  13291. +
  13292. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13293. +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
  13294. +{
  13295. + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
  13296. + struct super_block *sb = dentry->d_sb;
  13297. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13298. +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
  13299. +{
  13300. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13301. +#else
  13302. +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
  13303. +{
  13304. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13305. +#endif
  13306. +
  13307. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
  13308. +
  13309. + yaffs_gross_lock(dev);
  13310. +
  13311. + buf->f_type = YAFFS_MAGIC;
  13312. + buf->f_bsize = sb->s_blocksize;
  13313. + buf->f_namelen = 255;
  13314. +
  13315. + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
  13316. + /* Do this if chunk size is not a power of 2 */
  13317. +
  13318. + uint64_t bytes_in_dev;
  13319. + uint64_t bytes_free;
  13320. +
  13321. + bytes_in_dev =
  13322. + ((uint64_t)
  13323. + ((dev->param.end_block - dev->param.start_block +
  13324. + 1))) * ((uint64_t) (dev->param.chunks_per_block *
  13325. + dev->data_bytes_per_chunk));
  13326. +
  13327. + do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
  13328. + buf->f_blocks = bytes_in_dev;
  13329. +
  13330. + bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
  13331. + ((uint64_t) (dev->data_bytes_per_chunk));
  13332. +
  13333. + do_div(bytes_free, sb->s_blocksize);
  13334. +
  13335. + buf->f_bfree = bytes_free;
  13336. +
  13337. + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
  13338. +
  13339. + buf->f_blocks =
  13340. + (dev->param.end_block - dev->param.start_block + 1) *
  13341. + dev->param.chunks_per_block /
  13342. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13343. + buf->f_bfree =
  13344. + yaffs_get_n_free_chunks(dev) /
  13345. + (sb->s_blocksize / dev->data_bytes_per_chunk);
  13346. + } else {
  13347. + buf->f_blocks =
  13348. + (dev->param.end_block - dev->param.start_block + 1) *
  13349. + dev->param.chunks_per_block *
  13350. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13351. +
  13352. + buf->f_bfree =
  13353. + yaffs_get_n_free_chunks(dev) *
  13354. + (dev->data_bytes_per_chunk / sb->s_blocksize);
  13355. + }
  13356. +
  13357. + buf->f_files = 0;
  13358. + buf->f_ffree = 0;
  13359. + buf->f_bavail = buf->f_bfree;
  13360. +
  13361. + yaffs_gross_unlock(dev);
  13362. + return 0;
  13363. +}
  13364. +
  13365. +
  13366. +
  13367. +static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
  13368. +{
  13369. +
  13370. + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
  13371. + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
  13372. + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
  13373. + int do_checkpoint;
  13374. + int dirty = yaffs_check_super_dirty(dev);
  13375. +
  13376. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13377. + "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
  13378. + gc_urgent,
  13379. + dirty ? "dirty" : "clean",
  13380. + request_checkpoint ? "checkpoint requested" : "no checkpoint",
  13381. + oneshot_checkpoint ? " one-shot" : "");
  13382. +
  13383. + yaffs_gross_lock(dev);
  13384. + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
  13385. + oneshot_checkpoint) && !dev->is_checkpointed;
  13386. +
  13387. + if (dirty || do_checkpoint) {
  13388. + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
  13389. + yaffs_clear_super_dirty(dev);
  13390. + if (oneshot_checkpoint)
  13391. + yaffs_auto_checkpoint &= ~4;
  13392. + }
  13393. + yaffs_gross_unlock(dev);
  13394. +
  13395. + return 0;
  13396. +}
  13397. +
  13398. +
  13399. +#ifdef YAFFS_HAS_WRITE_SUPER
  13400. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13401. +static void yaffs_write_super(struct super_block *sb)
  13402. +#else
  13403. +static int yaffs_write_super(struct super_block *sb)
  13404. +#endif
  13405. +{
  13406. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
  13407. +
  13408. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
  13409. + "yaffs_write_super %s",
  13410. + request_checkpoint ? " checkpt" : "");
  13411. +
  13412. + yaffs_do_sync_fs(sb, request_checkpoint);
  13413. +
  13414. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
  13415. + return 0;
  13416. +#endif
  13417. +}
  13418. +#endif
  13419. +
  13420. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13421. +static int yaffs_sync_fs(struct super_block *sb, int wait)
  13422. +#else
  13423. +static int yaffs_sync_fs(struct super_block *sb)
  13424. +#endif
  13425. +{
  13426. + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
  13427. +
  13428. + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
  13429. + "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
  13430. +
  13431. + yaffs_do_sync_fs(sb, request_checkpoint);
  13432. +
  13433. + return 0;
  13434. +}
  13435. +
  13436. +
  13437. +
  13438. +static const struct super_operations yaffs_super_ops = {
  13439. + .statfs = yaffs_statfs,
  13440. +
  13441. +#ifndef YAFFS_USE_OWN_IGET
  13442. + .read_inode = yaffs_read_inode,
  13443. +#endif
  13444. +#ifdef YAFFS_HAS_PUT_INODE
  13445. + .put_inode = yaffs_put_inode,
  13446. +#endif
  13447. + .put_super = yaffs_put_super,
  13448. +#ifdef YAFFS_HAS_EVICT_INODE
  13449. + .evict_inode = yaffs_evict_inode,
  13450. +#else
  13451. + .delete_inode = yaffs_delete_inode,
  13452. + .clear_inode = yaffs_clear_inode,
  13453. +#endif
  13454. + .sync_fs = yaffs_sync_fs,
  13455. +#ifdef YAFFS_HAS_WRITE_SUPER
  13456. + .write_super = yaffs_write_super,
  13457. +#endif
  13458. +};
  13459. +
  13460. +struct yaffs_options {
  13461. + int inband_tags;
  13462. + int skip_checkpoint_read;
  13463. + int skip_checkpoint_write;
  13464. + int no_cache;
  13465. + int tags_ecc_on;
  13466. + int tags_ecc_overridden;
  13467. + int lazy_loading_enabled;
  13468. + int lazy_loading_overridden;
  13469. + int empty_lost_and_found;
  13470. + int empty_lost_and_found_overridden;
  13471. + int disable_summary;
  13472. +};
  13473. +
  13474. +#define MAX_OPT_LEN 30
  13475. +static int yaffs_parse_options(struct yaffs_options *options,
  13476. + const char *options_str)
  13477. +{
  13478. + char cur_opt[MAX_OPT_LEN + 1];
  13479. + int p;
  13480. + int error = 0;
  13481. +
  13482. + /* Parse through the options which is a comma seperated list */
  13483. +
  13484. + while (options_str && *options_str && !error) {
  13485. + memset(cur_opt, 0, MAX_OPT_LEN + 1);
  13486. + p = 0;
  13487. +
  13488. + while (*options_str == ',')
  13489. + options_str++;
  13490. +
  13491. + while (*options_str && *options_str != ',') {
  13492. + if (p < MAX_OPT_LEN) {
  13493. + cur_opt[p] = *options_str;
  13494. + p++;
  13495. + }
  13496. + options_str++;
  13497. + }
  13498. +
  13499. + if (!strcmp(cur_opt, "inband-tags")) {
  13500. + options->inband_tags = 1;
  13501. + } else if (!strcmp(cur_opt, "tags-ecc-off")) {
  13502. + options->tags_ecc_on = 0;
  13503. + options->tags_ecc_overridden = 1;
  13504. + } else if (!strcmp(cur_opt, "tags-ecc-on")) {
  13505. + options->tags_ecc_on = 1;
  13506. + options->tags_ecc_overridden = 1;
  13507. + } else if (!strcmp(cur_opt, "lazy-loading-off")) {
  13508. + options->lazy_loading_enabled = 0;
  13509. + options->lazy_loading_overridden = 1;
  13510. + } else if (!strcmp(cur_opt, "lazy-loading-on")) {
  13511. + options->lazy_loading_enabled = 1;
  13512. + options->lazy_loading_overridden = 1;
  13513. + } else if (!strcmp(cur_opt, "disable-summary")) {
  13514. + options->disable_summary = 1;
  13515. + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
  13516. + options->empty_lost_and_found = 0;
  13517. + options->empty_lost_and_found_overridden = 1;
  13518. + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
  13519. + options->empty_lost_and_found = 1;
  13520. + options->empty_lost_and_found_overridden = 1;
  13521. + } else if (!strcmp(cur_opt, "no-cache")) {
  13522. + options->no_cache = 1;
  13523. + } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
  13524. + options->skip_checkpoint_read = 1;
  13525. + } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
  13526. + options->skip_checkpoint_write = 1;
  13527. + } else if (!strcmp(cur_opt, "no-checkpoint")) {
  13528. + options->skip_checkpoint_read = 1;
  13529. + options->skip_checkpoint_write = 1;
  13530. + } else {
  13531. + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
  13532. + cur_opt);
  13533. + error = 1;
  13534. + }
  13535. + }
  13536. +
  13537. + return error;
  13538. +}
  13539. +
  13540. +
  13541. +static struct dentry *yaffs_make_root(struct inode *inode)
  13542. +{
  13543. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
  13544. + struct dentry *root = d_alloc_root(inode);
  13545. +
  13546. + if (!root)
  13547. + iput(inode);
  13548. +
  13549. + return root;
  13550. +#else
  13551. + return d_make_root(inode);
  13552. +#endif
  13553. +}
  13554. +
  13555. +
  13556. +
  13557. +
  13558. +static struct super_block *yaffs_internal_read_super(int yaffs_version,
  13559. + struct super_block *sb,
  13560. + void *data, int silent)
  13561. +{
  13562. + int n_blocks;
  13563. + struct inode *inode = NULL;
  13564. + struct dentry *root;
  13565. + struct yaffs_dev *dev = 0;
  13566. + char devname_buf[BDEVNAME_SIZE + 1];
  13567. + struct mtd_info *mtd;
  13568. + int err;
  13569. + char *data_str = (char *)data;
  13570. + struct yaffs_linux_context *context = NULL;
  13571. + struct yaffs_param *param;
  13572. +
  13573. + int read_only = 0;
  13574. + int inband_tags = 0;
  13575. +
  13576. + struct yaffs_options options;
  13577. +
  13578. + unsigned mount_id;
  13579. + int found;
  13580. + struct yaffs_linux_context *context_iterator;
  13581. + struct list_head *l;
  13582. +
  13583. + if (!sb) {
  13584. + printk(KERN_INFO "yaffs: sb is NULL\n");
  13585. + return NULL;
  13586. + }
  13587. +
  13588. + sb->s_magic = YAFFS_MAGIC;
  13589. + sb->s_op = &yaffs_super_ops;
  13590. + sb->s_flags |= MS_NOATIME;
  13591. +
  13592. + read_only = ((sb->s_flags & MS_RDONLY) != 0);
  13593. +
  13594. +#ifdef YAFFS_COMPILE_EXPORTFS
  13595. + sb->s_export_op = &yaffs_export_ops;
  13596. +#endif
  13597. +
  13598. + if (!sb->s_dev)
  13599. + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
  13600. + else if (!yaffs_devname(sb, devname_buf))
  13601. + printk(KERN_INFO "yaffs: devname is NULL\n");
  13602. + else
  13603. + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
  13604. + sb->s_dev,
  13605. + yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
  13606. +
  13607. + if (!data_str)
  13608. + data_str = "";
  13609. +
  13610. + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
  13611. +
  13612. + memset(&options, 0, sizeof(options));
  13613. +
  13614. + if (yaffs_parse_options(&options, data_str)) {
  13615. + /* Option parsing failed */
  13616. + return NULL;
  13617. + }
  13618. +
  13619. + sb->s_blocksize = PAGE_CACHE_SIZE;
  13620. + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  13621. +
  13622. + yaffs_trace(YAFFS_TRACE_OS,
  13623. + "yaffs_read_super: Using yaffs%d", yaffs_version);
  13624. + yaffs_trace(YAFFS_TRACE_OS,
  13625. + "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
  13626. +
  13627. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13628. + "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
  13629. + MAJOR(sb->s_dev), MINOR(sb->s_dev),
  13630. + yaffs_devname(sb, devname_buf));
  13631. +
  13632. + /* Get the device */
  13633. + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
  13634. + if (IS_ERR(mtd)) {
  13635. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13636. + "yaffs: MTD device %u either not valid or unavailable",
  13637. + MINOR(sb->s_dev));
  13638. + return NULL;
  13639. + }
  13640. +
  13641. + if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
  13642. + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
  13643. + yaffs_version = 2;
  13644. + }
  13645. +
  13646. + /* Added NCB 26/5/2006 for completeness */
  13647. + if (yaffs_version == 2 && !options.inband_tags
  13648. + && WRITE_SIZE(mtd) == 512) {
  13649. + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
  13650. + yaffs_version = 1;
  13651. + }
  13652. +
  13653. + if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
  13654. + options.inband_tags)
  13655. + inband_tags = 1;
  13656. +
  13657. + if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
  13658. + return NULL;
  13659. +
  13660. + /* OK, so if we got here, we have an MTD that's NAND and looks
  13661. + * like it has the right capabilities
  13662. + * Set the struct yaffs_dev up for mtd
  13663. + */
  13664. +
  13665. + if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
  13666. + read_only = 1;
  13667. + printk(KERN_INFO
  13668. + "yaffs: mtd is read only, setting superblock read only\n"
  13669. + );
  13670. + sb->s_flags |= MS_RDONLY;
  13671. + }
  13672. +
  13673. + dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
  13674. + context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
  13675. +
  13676. + if (!dev || !context) {
  13677. + kfree(dev);
  13678. + kfree(context);
  13679. + dev = NULL;
  13680. + context = NULL;
  13681. +
  13682. + /* Deep shit could not allocate device structure */
  13683. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13684. + "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
  13685. + );
  13686. + return NULL;
  13687. + }
  13688. + memset(dev, 0, sizeof(struct yaffs_dev));
  13689. + param = &(dev->param);
  13690. +
  13691. + memset(context, 0, sizeof(struct yaffs_linux_context));
  13692. + dev->os_context = context;
  13693. + INIT_LIST_HEAD(&(context->context_list));
  13694. + context->dev = dev;
  13695. + context->super = sb;
  13696. +
  13697. + dev->read_only = read_only;
  13698. +
  13699. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13700. + sb->s_fs_info = dev;
  13701. +#else
  13702. + sb->u.generic_sbp = dev;
  13703. +#endif
  13704. +
  13705. +
  13706. + dev->driver_context = mtd;
  13707. + param->name = mtd->name;
  13708. +
  13709. + /* Set up the memory size parameters.... */
  13710. +
  13711. +
  13712. + param->n_reserved_blocks = 5;
  13713. + param->n_caches = (options.no_cache) ? 0 : 10;
  13714. + param->inband_tags = inband_tags;
  13715. +
  13716. + param->enable_xattr = 1;
  13717. + if (options.lazy_loading_overridden)
  13718. + param->disable_lazy_load = !options.lazy_loading_enabled;
  13719. +
  13720. + param->defered_dir_update = 1;
  13721. +
  13722. + if (options.tags_ecc_overridden)
  13723. + param->no_tags_ecc = !options.tags_ecc_on;
  13724. +
  13725. + param->empty_lost_n_found = 1;
  13726. + param->refresh_period = 500;
  13727. + param->disable_summary = options.disable_summary;
  13728. +
  13729. +
  13730. +#ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
  13731. + param->disable_bad_block_marking = 1;
  13732. +#endif
  13733. + if (options.empty_lost_and_found_overridden)
  13734. + param->empty_lost_n_found = options.empty_lost_and_found;
  13735. +
  13736. + /* ... and the functions. */
  13737. + if (yaffs_version == 2) {
  13738. + param->is_yaffs2 = 1;
  13739. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13740. + param->total_bytes_per_chunk = mtd->writesize;
  13741. + param->chunks_per_block = mtd->erasesize / mtd->writesize;
  13742. +#else
  13743. + param->total_bytes_per_chunk = mtd->oobblock;
  13744. + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
  13745. +#endif
  13746. + n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
  13747. +
  13748. + param->start_block = 0;
  13749. + param->end_block = n_blocks - 1;
  13750. + } else {
  13751. + param->is_yaffs2 = 0;
  13752. + n_blocks = YCALCBLOCKS(mtd->size,
  13753. + YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
  13754. +
  13755. + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
  13756. + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
  13757. + }
  13758. +
  13759. + param->start_block = 0;
  13760. + param->end_block = n_blocks - 1;
  13761. +
  13762. + yaffs_mtd_drv_install(dev);
  13763. +
  13764. + param->sb_dirty_fn = yaffs_set_super_dirty;
  13765. + param->gc_control_fn = yaffs_gc_control_callback;
  13766. +
  13767. + yaffs_dev_to_lc(dev)->super = sb;
  13768. +
  13769. + param->use_nand_ecc = 1;
  13770. +
  13771. + param->skip_checkpt_rd = options.skip_checkpoint_read;
  13772. + param->skip_checkpt_wr = options.skip_checkpoint_write;
  13773. +
  13774. + mutex_lock(&yaffs_context_lock);
  13775. + /* Get a mount id */
  13776. + found = 0;
  13777. + for (mount_id = 0; !found; mount_id++) {
  13778. + found = 1;
  13779. + list_for_each(l, &yaffs_context_list) {
  13780. + context_iterator =
  13781. + list_entry(l, struct yaffs_linux_context,
  13782. + context_list);
  13783. + if (context_iterator->mount_id == mount_id)
  13784. + found = 0;
  13785. + }
  13786. + }
  13787. + context->mount_id = mount_id;
  13788. +
  13789. + list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
  13790. + &yaffs_context_list);
  13791. + mutex_unlock(&yaffs_context_lock);
  13792. +
  13793. + /* Directory search handling... */
  13794. + INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
  13795. + param->remove_obj_fn = yaffs_remove_obj_callback;
  13796. +
  13797. + mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
  13798. +
  13799. + yaffs_gross_lock(dev);
  13800. +
  13801. + err = yaffs_guts_initialise(dev);
  13802. +
  13803. + yaffs_trace(YAFFS_TRACE_OS,
  13804. + "yaffs_read_super: guts initialised %s",
  13805. + (err == YAFFS_OK) ? "OK" : "FAILED");
  13806. +
  13807. + if (err == YAFFS_OK)
  13808. + yaffs_bg_start(dev);
  13809. +
  13810. + if (!context->bg_thread)
  13811. + param->defered_dir_update = 0;
  13812. +
  13813. + sb->s_maxbytes = yaffs_max_file_size(dev);
  13814. +
  13815. + /* Release lock before yaffs_get_inode() */
  13816. + yaffs_gross_unlock(dev);
  13817. +
  13818. + /* Create root inode */
  13819. + if (err == YAFFS_OK)
  13820. + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
  13821. +
  13822. + if (!inode)
  13823. + return NULL;
  13824. +
  13825. + inode->i_op = &yaffs_dir_inode_operations;
  13826. + inode->i_fop = &yaffs_dir_operations;
  13827. +
  13828. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
  13829. +
  13830. + root = yaffs_make_root(inode);
  13831. +
  13832. + if (!root)
  13833. + return NULL;
  13834. +
  13835. + sb->s_root = root;
  13836. + if(!dev->is_checkpointed)
  13837. + yaffs_set_super_dirty(dev);
  13838. +
  13839. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  13840. + "yaffs_read_super: is_checkpointed %d",
  13841. + dev->is_checkpointed);
  13842. +
  13843. + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
  13844. + return sb;
  13845. +}
  13846. +
  13847. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13848. +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
  13849. + int silent)
  13850. +{
  13851. + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
  13852. +}
  13853. +
  13854. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13855. +static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
  13856. + const char *dev_name, void *data)
  13857. +{
  13858. + return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
  13859. +}
  13860. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13861. +static int yaffs_read_super(struct file_system_type *fs,
  13862. + int flags, const char *dev_name,
  13863. + void *data, struct vfsmount *mnt)
  13864. +{
  13865. +
  13866. + return get_sb_bdev(fs, flags, dev_name, data,
  13867. + yaffs_internal_read_super_mtd, mnt);
  13868. +}
  13869. +#else
  13870. +static struct super_block *yaffs_read_super(struct file_system_type *fs,
  13871. + int flags, const char *dev_name,
  13872. + void *data)
  13873. +{
  13874. +
  13875. + return get_sb_bdev(fs, flags, dev_name, data,
  13876. + yaffs_internal_read_super_mtd);
  13877. +}
  13878. +#endif
  13879. +
  13880. +static struct file_system_type yaffs_fs_type = {
  13881. + .owner = THIS_MODULE,
  13882. + .name = "yaffs",
  13883. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13884. + .mount = yaffs_mount,
  13885. +#else
  13886. + .get_sb = yaffs_read_super,
  13887. +#endif
  13888. + .kill_sb = kill_block_super,
  13889. + .fs_flags = FS_REQUIRES_DEV,
  13890. +};
  13891. +#else
  13892. +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
  13893. + int silent)
  13894. +{
  13895. + return yaffs_internal_read_super(1, sb, data, silent);
  13896. +}
  13897. +
  13898. +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
  13899. + FS_REQUIRES_DEV);
  13900. +#endif
  13901. +
  13902. +
  13903. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  13904. +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
  13905. + int silent)
  13906. +{
  13907. + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
  13908. +}
  13909. +
  13910. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13911. +static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
  13912. + const char *dev_name, void *data)
  13913. +{
  13914. + return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
  13915. +}
  13916. +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
  13917. +static int yaffs2_read_super(struct file_system_type *fs,
  13918. + int flags, const char *dev_name, void *data,
  13919. + struct vfsmount *mnt)
  13920. +{
  13921. + return get_sb_bdev(fs, flags, dev_name, data,
  13922. + yaffs2_internal_read_super_mtd, mnt);
  13923. +}
  13924. +#else
  13925. +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
  13926. + int flags, const char *dev_name,
  13927. + void *data)
  13928. +{
  13929. +
  13930. + return get_sb_bdev(fs, flags, dev_name, data,
  13931. + yaffs2_internal_read_super_mtd);
  13932. +}
  13933. +#endif
  13934. +
  13935. +static struct file_system_type yaffs2_fs_type = {
  13936. + .owner = THIS_MODULE,
  13937. + .name = "yaffs2",
  13938. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  13939. + .mount = yaffs2_mount,
  13940. +#else
  13941. + .get_sb = yaffs2_read_super,
  13942. +#endif
  13943. + .kill_sb = kill_block_super,
  13944. + .fs_flags = FS_REQUIRES_DEV,
  13945. +};
  13946. +#else
  13947. +static struct super_block *yaffs2_read_super(struct super_block *sb,
  13948. + void *data, int silent)
  13949. +{
  13950. + return yaffs_internal_read_super(2, sb, data, silent);
  13951. +}
  13952. +
  13953. +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
  13954. + FS_REQUIRES_DEV);
  13955. +#endif
  13956. +
  13957. +
  13958. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  13959. +static struct proc_dir_entry *my_proc_entry;
  13960. +
  13961. +static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
  13962. +{
  13963. + struct yaffs_param *param = &dev->param;
  13964. + int bs[10];
  13965. +
  13966. + yaffs_count_blocks_by_state(dev,bs);
  13967. +
  13968. + buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
  13969. + buf += sprintf(buf, "end_block............ %d\n", param->end_block);
  13970. + buf += sprintf(buf, "total_bytes_per_chunk %d\n",
  13971. + param->total_bytes_per_chunk);
  13972. + buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
  13973. + buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
  13974. + buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
  13975. + buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
  13976. + buf += sprintf(buf, "empty_lost_n_found... %d\n",
  13977. + param->empty_lost_n_found);
  13978. + buf += sprintf(buf, "disable_lazy_load.... %d\n",
  13979. + param->disable_lazy_load);
  13980. + buf += sprintf(buf, "disable_bad_block_mrk %d\n",
  13981. + param->disable_bad_block_marking);
  13982. + buf += sprintf(buf, "refresh_period....... %d\n",
  13983. + param->refresh_period);
  13984. + buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
  13985. + buf += sprintf(buf, "n_reserved_blocks.... %d\n",
  13986. + param->n_reserved_blocks);
  13987. + buf += sprintf(buf, "always_check_erased.. %d\n",
  13988. + param->always_check_erased);
  13989. + buf += sprintf(buf, "\n");
  13990. + buf += sprintf(buf, "block count by state\n");
  13991. + buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
  13992. + bs[0], bs[1], bs[2], bs[3], bs[4]);
  13993. + buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
  13994. + bs[5], bs[6], bs[7], bs[8], bs[9]);
  13995. +
  13996. + return buf;
  13997. +}
  13998. +
  13999. +static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
  14000. +{
  14001. + buf += sprintf(buf, "max file size....... %lld\n",
  14002. + (long long) yaffs_max_file_size(dev));
  14003. + buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
  14004. + dev->data_bytes_per_chunk);
  14005. + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
  14006. + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
  14007. + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
  14008. + buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
  14009. + dev->blocks_in_checkpt);
  14010. + buf += sprintf(buf, "\n");
  14011. + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
  14012. + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
  14013. + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
  14014. + buf += sprintf(buf, "\n");
  14015. + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
  14016. + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
  14017. + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
  14018. + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
  14019. + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
  14020. + buf += sprintf(buf, "passive_gc_count..... %u\n",
  14021. + dev->passive_gc_count);
  14022. + buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
  14023. + dev->oldest_dirty_gc_count);
  14024. + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
  14025. + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
  14026. + buf += sprintf(buf, "n_retried_writes..... %u\n",
  14027. + dev->n_retried_writes);
  14028. + buf += sprintf(buf, "n_retired_blocks..... %u\n",
  14029. + dev->n_retired_blocks);
  14030. + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
  14031. + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
  14032. + buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
  14033. + dev->n_tags_ecc_fixed);
  14034. + buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
  14035. + dev->n_tags_ecc_unfixed);
  14036. + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
  14037. + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
  14038. + buf += sprintf(buf, "n_unlinked_files..... %u\n",
  14039. + dev->n_unlinked_files);
  14040. + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
  14041. + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
  14042. + buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
  14043. + buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
  14044. +
  14045. + return buf;
  14046. +}
  14047. +
  14048. +static int yaffs_proc_read(char *page,
  14049. + char **start,
  14050. + off_t offset, int count, int *eof, void *data)
  14051. +{
  14052. + struct list_head *item;
  14053. + char *buf = page;
  14054. + int step = offset;
  14055. + int n = 0;
  14056. +
  14057. + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
  14058. + * We use 'offset' (*ppos) to indicate where we are in dev_list.
  14059. + * This also assumes the user has posted a read buffer large
  14060. + * enough to hold the complete output; but that's life in /proc.
  14061. + */
  14062. +
  14063. + *(int *)start = 1;
  14064. +
  14065. + /* Print header first */
  14066. + if (step == 0)
  14067. + buf +=
  14068. + sprintf(buf,
  14069. + "Multi-version YAFFS built:" __DATE__ " " __TIME__
  14070. + "\n");
  14071. + else if (step == 1)
  14072. + buf += sprintf(buf, "\n");
  14073. + else {
  14074. + step -= 2;
  14075. +
  14076. + mutex_lock(&yaffs_context_lock);
  14077. +
  14078. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14079. + list_for_each(item, &yaffs_context_list) {
  14080. + struct yaffs_linux_context *dc =
  14081. + list_entry(item, struct yaffs_linux_context,
  14082. + context_list);
  14083. + struct yaffs_dev *dev = dc->dev;
  14084. +
  14085. + if (n < (step & ~1)) {
  14086. + n += 2;
  14087. + continue;
  14088. + }
  14089. + if ((step & 1) == 0) {
  14090. + buf +=
  14091. + sprintf(buf, "\nDevice %d \"%s\"\n", n,
  14092. + dev->param.name);
  14093. + buf = yaffs_dump_dev_part0(buf, dev);
  14094. + } else {
  14095. + buf = yaffs_dump_dev_part1(buf, dev);
  14096. + }
  14097. +
  14098. + break;
  14099. + }
  14100. + mutex_unlock(&yaffs_context_lock);
  14101. + }
  14102. +
  14103. + return buf - page < count ? buf - page : count;
  14104. +}
  14105. +
  14106. +/**
  14107. + * Set the verbosity of the warnings and error messages.
  14108. + *
  14109. + * Note that the names can only be a..z or _ with the current code.
  14110. + */
  14111. +
  14112. +static struct {
  14113. + char *mask_name;
  14114. + unsigned mask_bitfield;
  14115. +} mask_flags[] = {
  14116. + {"allocate", YAFFS_TRACE_ALLOCATE},
  14117. + {"always", YAFFS_TRACE_ALWAYS},
  14118. + {"background", YAFFS_TRACE_BACKGROUND},
  14119. + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
  14120. + {"buffers", YAFFS_TRACE_BUFFERS},
  14121. + {"bug", YAFFS_TRACE_BUG},
  14122. + {"checkpt", YAFFS_TRACE_CHECKPOINT},
  14123. + {"deletion", YAFFS_TRACE_DELETION},
  14124. + {"erase", YAFFS_TRACE_ERASE},
  14125. + {"error", YAFFS_TRACE_ERROR},
  14126. + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
  14127. + {"gc", YAFFS_TRACE_GC},
  14128. + {"lock", YAFFS_TRACE_LOCK},
  14129. + {"mtd", YAFFS_TRACE_MTD},
  14130. + {"nandaccess", YAFFS_TRACE_NANDACCESS},
  14131. + {"os", YAFFS_TRACE_OS},
  14132. + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
  14133. + {"scan", YAFFS_TRACE_SCAN},
  14134. + {"mount", YAFFS_TRACE_MOUNT},
  14135. + {"tracing", YAFFS_TRACE_TRACING},
  14136. + {"sync", YAFFS_TRACE_SYNC},
  14137. + {"write", YAFFS_TRACE_WRITE},
  14138. + {"verify", YAFFS_TRACE_VERIFY},
  14139. + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
  14140. + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
  14141. + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
  14142. + {"all", 0xffffffff},
  14143. + {"none", 0},
  14144. + {NULL, 0},
  14145. +};
  14146. +
  14147. +#define MAX_MASK_NAME_LENGTH 40
  14148. +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
  14149. + unsigned long count, void *data)
  14150. +{
  14151. + unsigned rg = 0, mask_bitfield;
  14152. + char *end;
  14153. + char *mask_name;
  14154. + const char *x;
  14155. + char substring[MAX_MASK_NAME_LENGTH + 1];
  14156. + int i;
  14157. + int done = 0;
  14158. + int add, len = 0;
  14159. + int pos = 0;
  14160. +
  14161. + rg = yaffs_trace_mask;
  14162. +
  14163. + while (!done && (pos < count)) {
  14164. + done = 1;
  14165. + while ((pos < count) && isspace(buf[pos]))
  14166. + pos++;
  14167. +
  14168. + switch (buf[pos]) {
  14169. + case '+':
  14170. + case '-':
  14171. + case '=':
  14172. + add = buf[pos];
  14173. + pos++;
  14174. + break;
  14175. +
  14176. + default:
  14177. + add = ' ';
  14178. + break;
  14179. + }
  14180. + mask_name = NULL;
  14181. +
  14182. + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
  14183. +
  14184. + if (end > buf + pos) {
  14185. + mask_name = "numeral";
  14186. + len = end - (buf + pos);
  14187. + pos += len;
  14188. + done = 0;
  14189. + } else {
  14190. + for (x = buf + pos, i = 0;
  14191. + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
  14192. + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
  14193. + substring[i] = *x;
  14194. + substring[i] = '\0';
  14195. +
  14196. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14197. + if (strcmp(substring, mask_flags[i].mask_name)
  14198. + == 0) {
  14199. + mask_name = mask_flags[i].mask_name;
  14200. + mask_bitfield =
  14201. + mask_flags[i].mask_bitfield;
  14202. + done = 0;
  14203. + break;
  14204. + }
  14205. + }
  14206. + }
  14207. +
  14208. + if (mask_name != NULL) {
  14209. + done = 0;
  14210. + switch (add) {
  14211. + case '-':
  14212. + rg &= ~mask_bitfield;
  14213. + break;
  14214. + case '+':
  14215. + rg |= mask_bitfield;
  14216. + break;
  14217. + case '=':
  14218. + rg = mask_bitfield;
  14219. + break;
  14220. + default:
  14221. + rg |= mask_bitfield;
  14222. + break;
  14223. + }
  14224. + }
  14225. + }
  14226. +
  14227. + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
  14228. +
  14229. + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
  14230. +
  14231. + if (rg & YAFFS_TRACE_ALWAYS) {
  14232. + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
  14233. + char flag;
  14234. + flag = ((rg & mask_flags[i].mask_bitfield) ==
  14235. + mask_flags[i].mask_bitfield) ? '+' : '-';
  14236. + printk(KERN_DEBUG "%c%s\n", flag,
  14237. + mask_flags[i].mask_name);
  14238. + }
  14239. + }
  14240. +
  14241. + return count;
  14242. +}
  14243. +
  14244. +/* Debug strings are of the form:
  14245. + * .bnnn print info on block n
  14246. + * .cobjn,chunkn print nand chunk id for objn:chunkn
  14247. + */
  14248. +
  14249. +static int yaffs_proc_debug_write(struct file *file, const char *buf,
  14250. + unsigned long count, void *data)
  14251. +{
  14252. +
  14253. + char str[100];
  14254. + char *p0;
  14255. + char *p1;
  14256. + long p1_val;
  14257. + long p0_val;
  14258. + char cmd;
  14259. + struct list_head *item;
  14260. +
  14261. + memset(str, 0, sizeof(str));
  14262. + memcpy(str, buf, min(count, sizeof(str) -1));
  14263. +
  14264. + cmd = str[1];
  14265. +
  14266. + p0 = str + 2;
  14267. +
  14268. + p1 = p0;
  14269. +
  14270. + while (*p1 && *p1 != ',') {
  14271. + p1++;
  14272. + }
  14273. + *p1 = '\0';
  14274. + p1++;
  14275. +
  14276. + p0_val = simple_strtol(p0, NULL, 0);
  14277. + p1_val = simple_strtol(p1, NULL, 0);
  14278. +
  14279. +
  14280. + mutex_lock(&yaffs_context_lock);
  14281. +
  14282. + /* Locate and print the Nth entry. Order N-squared but N is small. */
  14283. + list_for_each(item, &yaffs_context_list) {
  14284. + struct yaffs_linux_context *dc =
  14285. + list_entry(item, struct yaffs_linux_context,
  14286. + context_list);
  14287. + struct yaffs_dev *dev = dc->dev;
  14288. +
  14289. + if (cmd == 'b') {
  14290. + struct yaffs_block_info *bi;
  14291. +
  14292. + bi = yaffs_get_block_info(dev,p0_val);
  14293. +
  14294. + if(bi) {
  14295. + printk("Block %d: state %d, retire %d, use %d, seq %d\n",
  14296. + (int)p0_val, bi->block_state,
  14297. + bi->needs_retiring, bi->pages_in_use,
  14298. + bi->seq_number);
  14299. + }
  14300. + } else if (cmd == 'c') {
  14301. + struct yaffs_obj *obj;
  14302. + int nand_chunk;
  14303. +
  14304. + obj = yaffs_find_by_number(dev, p0_val);
  14305. + if (!obj)
  14306. + printk("No obj %d\n", (int)p0_val);
  14307. + else {
  14308. + if(p1_val == 0)
  14309. + nand_chunk = obj->hdr_chunk;
  14310. + else
  14311. + nand_chunk =
  14312. + yaffs_find_chunk_in_file(obj,
  14313. + p1_val, NULL);
  14314. + printk("Nand chunk for %d:%d is %d\n",
  14315. + (int)p0_val, (int)p1_val, nand_chunk);
  14316. + }
  14317. + }
  14318. + }
  14319. +
  14320. + mutex_unlock(&yaffs_context_lock);
  14321. +
  14322. + return count;
  14323. +}
  14324. +
  14325. +static int yaffs_proc_write(struct file *file, const char *buf,
  14326. + unsigned long count, void *data)
  14327. +{
  14328. + if (buf[0] == '.')
  14329. + return yaffs_proc_debug_write(file, buf, count, data);
  14330. + return yaffs_proc_write_trace_options(file, buf, count, data);
  14331. +}
  14332. +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) */
  14333. +
  14334. +/* Stuff to handle installation of file systems */
  14335. +struct file_system_to_install {
  14336. + struct file_system_type *fst;
  14337. + int installed;
  14338. +};
  14339. +
  14340. +static struct file_system_to_install fs_to_install[] = {
  14341. + {&yaffs_fs_type, 0},
  14342. + {&yaffs2_fs_type, 0},
  14343. + {NULL, 0}
  14344. +};
  14345. +
  14346. +static int __init init_yaffs_fs(void)
  14347. +{
  14348. + int error = 0;
  14349. + struct file_system_to_install *fsinst;
  14350. +
  14351. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  14352. + "yaffs built " __DATE__ " " __TIME__ " Installing.");
  14353. +
  14354. + mutex_init(&yaffs_context_lock);
  14355. +
  14356. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  14357. + /* Install the proc_fs entries */
  14358. + my_proc_entry = create_proc_entry("yaffs",
  14359. + S_IRUGO | S_IFREG, YPROC_ROOT);
  14360. +
  14361. + if (my_proc_entry) {
  14362. + my_proc_entry->write_proc = yaffs_proc_write;
  14363. + my_proc_entry->read_proc = yaffs_proc_read;
  14364. + my_proc_entry->data = NULL;
  14365. + } else {
  14366. + return -ENOMEM;
  14367. + }
  14368. +#endif
  14369. +
  14370. + /* Now add the file system entries */
  14371. +
  14372. + fsinst = fs_to_install;
  14373. +
  14374. + while (fsinst->fst && !error) {
  14375. + error = register_filesystem(fsinst->fst);
  14376. + if (!error)
  14377. + fsinst->installed = 1;
  14378. + fsinst++;
  14379. + }
  14380. +
  14381. + /* Any errors? uninstall */
  14382. + if (error) {
  14383. + fsinst = fs_to_install;
  14384. +
  14385. + while (fsinst->fst) {
  14386. + if (fsinst->installed) {
  14387. + unregister_filesystem(fsinst->fst);
  14388. + fsinst->installed = 0;
  14389. + }
  14390. + fsinst++;
  14391. + }
  14392. + }
  14393. +
  14394. + return error;
  14395. +}
  14396. +
  14397. +static void __exit exit_yaffs_fs(void)
  14398. +{
  14399. +
  14400. + struct file_system_to_install *fsinst;
  14401. +
  14402. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  14403. + "yaffs built " __DATE__ " " __TIME__ " removing.");
  14404. +
  14405. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
  14406. + remove_proc_entry("yaffs", YPROC_ROOT);
  14407. +#endif
  14408. +
  14409. + fsinst = fs_to_install;
  14410. +
  14411. + while (fsinst->fst) {
  14412. + if (fsinst->installed) {
  14413. + unregister_filesystem(fsinst->fst);
  14414. + fsinst->installed = 0;
  14415. + }
  14416. + fsinst++;
  14417. + }
  14418. +}
  14419. +
  14420. +module_init(init_yaffs_fs)
  14421. + module_exit(exit_yaffs_fs)
  14422. +
  14423. + MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
  14424. +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
  14425. +MODULE_LICENSE("GPL");
  14426. diff -Nur linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs1.c linux-3.14.4/fs/yaffs2/yaffs_yaffs1.c
  14427. --- linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs1.c 1970-01-01 01:00:00.000000000 +0100
  14428. +++ linux-3.14.4/fs/yaffs2/yaffs_yaffs1.c 2014-05-17 02:22:30.000000000 +0200
  14429. @@ -0,0 +1,422 @@
  14430. +/*
  14431. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14432. + *
  14433. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14434. + * for Toby Churchill Ltd and Brightstar Engineering
  14435. + *
  14436. + * Created by Charles Manning <charles@aleph1.co.uk>
  14437. + *
  14438. + * This program is free software; you can redistribute it and/or modify
  14439. + * it under the terms of the GNU General Public License version 2 as
  14440. + * published by the Free Software Foundation.
  14441. + */
  14442. +
  14443. +#include "yaffs_yaffs1.h"
  14444. +#include "yportenv.h"
  14445. +#include "yaffs_trace.h"
  14446. +#include "yaffs_bitmap.h"
  14447. +#include "yaffs_getblockinfo.h"
  14448. +#include "yaffs_nand.h"
  14449. +#include "yaffs_attribs.h"
  14450. +
  14451. +int yaffs1_scan(struct yaffs_dev *dev)
  14452. +{
  14453. + struct yaffs_ext_tags tags;
  14454. + int blk;
  14455. + int result;
  14456. + int chunk;
  14457. + int c;
  14458. + int deleted;
  14459. + enum yaffs_block_state state;
  14460. + LIST_HEAD(hard_list);
  14461. + struct yaffs_block_info *bi;
  14462. + u32 seq_number;
  14463. + struct yaffs_obj_hdr *oh;
  14464. + struct yaffs_obj *in;
  14465. + struct yaffs_obj *parent;
  14466. + int alloc_failed = 0;
  14467. + struct yaffs_shadow_fixer *shadow_fixers = NULL;
  14468. + u8 *chunk_data;
  14469. +
  14470. + yaffs_trace(YAFFS_TRACE_SCAN,
  14471. + "yaffs1_scan starts intstartblk %d intendblk %d...",
  14472. + dev->internal_start_block, dev->internal_end_block);
  14473. +
  14474. + chunk_data = yaffs_get_temp_buffer(dev);
  14475. +
  14476. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  14477. +
  14478. + /* Scan all the blocks to determine their state */
  14479. + bi = dev->block_info;
  14480. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  14481. + blk++) {
  14482. + yaffs_clear_chunk_bits(dev, blk);
  14483. + bi->pages_in_use = 0;
  14484. + bi->soft_del_pages = 0;
  14485. +
  14486. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  14487. +
  14488. + bi->block_state = state;
  14489. + bi->seq_number = seq_number;
  14490. +
  14491. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  14492. + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
  14493. +
  14494. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  14495. + "Block scanning block %d state %d seq %d",
  14496. + blk, state, seq_number);
  14497. +
  14498. + if (state == YAFFS_BLOCK_STATE_DEAD) {
  14499. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  14500. + "block %d is bad", blk);
  14501. + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
  14502. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  14503. + dev->n_erased_blocks++;
  14504. + dev->n_free_chunks += dev->param.chunks_per_block;
  14505. + }
  14506. + bi++;
  14507. + }
  14508. +
  14509. + /* For each block.... */
  14510. + for (blk = dev->internal_start_block;
  14511. + !alloc_failed && blk <= dev->internal_end_block; blk++) {
  14512. +
  14513. + cond_resched();
  14514. +
  14515. + bi = yaffs_get_block_info(dev, blk);
  14516. + state = bi->block_state;
  14517. +
  14518. + deleted = 0;
  14519. +
  14520. + /* For each chunk in each block that needs scanning.... */
  14521. + for (c = 0;
  14522. + !alloc_failed && c < dev->param.chunks_per_block &&
  14523. + state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
  14524. + /* Read the tags and decide what to do */
  14525. + chunk = blk * dev->param.chunks_per_block + c;
  14526. +
  14527. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
  14528. + &tags);
  14529. +
  14530. + /* Let's have a good look at this chunk... */
  14531. +
  14532. + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
  14533. + tags.is_deleted) {
  14534. + /* YAFFS1 only...
  14535. + * A deleted chunk
  14536. + */
  14537. + deleted++;
  14538. + dev->n_free_chunks++;
  14539. + } else if (!tags.chunk_used) {
  14540. + /* An unassigned chunk in the block
  14541. + * This means that either the block is empty or
  14542. + * this is the one being allocated from
  14543. + */
  14544. +
  14545. + if (c == 0) {
  14546. + /* We're looking at the first chunk in
  14547. + *the block so the block is unused */
  14548. + state = YAFFS_BLOCK_STATE_EMPTY;
  14549. + dev->n_erased_blocks++;
  14550. + } else {
  14551. + /* this is the block being allocated */
  14552. + yaffs_trace(YAFFS_TRACE_SCAN,
  14553. + " Allocating from %d %d",
  14554. + blk, c);
  14555. + state = YAFFS_BLOCK_STATE_ALLOCATING;
  14556. + dev->alloc_block = blk;
  14557. + dev->alloc_page = c;
  14558. + dev->alloc_block_finder = blk;
  14559. +
  14560. + }
  14561. +
  14562. + dev->n_free_chunks +=
  14563. + (dev->param.chunks_per_block - c);
  14564. + } else if (tags.chunk_id > 0) {
  14565. + /* chunk_id > 0 so it is a data chunk... */
  14566. + unsigned int endpos;
  14567. +
  14568. + yaffs_set_chunk_bit(dev, blk, c);
  14569. + bi->pages_in_use++;
  14570. +
  14571. + in = yaffs_find_or_create_by_number(dev,
  14572. + tags.obj_id,
  14573. + YAFFS_OBJECT_TYPE_FILE);
  14574. + /* PutChunkIntoFile checks for a clash
  14575. + * (two data chunks with the same chunk_id).
  14576. + */
  14577. +
  14578. + if (!in)
  14579. + alloc_failed = 1;
  14580. +
  14581. + if (in) {
  14582. + if (!yaffs_put_chunk_in_file
  14583. + (in, tags.chunk_id, chunk, 1))
  14584. + alloc_failed = 1;
  14585. + }
  14586. +
  14587. + endpos =
  14588. + (tags.chunk_id - 1) *
  14589. + dev->data_bytes_per_chunk +
  14590. + tags.n_bytes;
  14591. + if (in &&
  14592. + in->variant_type ==
  14593. + YAFFS_OBJECT_TYPE_FILE &&
  14594. + in->variant.file_variant.scanned_size <
  14595. + endpos) {
  14596. + in->variant.file_variant.scanned_size =
  14597. + endpos;
  14598. + if (!dev->param.use_header_file_size) {
  14599. + in->variant.
  14600. + file_variant.file_size =
  14601. + in->variant.
  14602. + file_variant.scanned_size;
  14603. + }
  14604. +
  14605. + }
  14606. + } else {
  14607. + /* chunk_id == 0, so it is an ObjectHeader.
  14608. + * Make the object
  14609. + */
  14610. + yaffs_set_chunk_bit(dev, blk, c);
  14611. + bi->pages_in_use++;
  14612. +
  14613. + result = yaffs_rd_chunk_tags_nand(dev, chunk,
  14614. + chunk_data,
  14615. + NULL);
  14616. +
  14617. + oh = (struct yaffs_obj_hdr *)chunk_data;
  14618. +
  14619. + in = yaffs_find_by_number(dev, tags.obj_id);
  14620. + if (in && in->variant_type != oh->type) {
  14621. + /* This should not happen, but somehow
  14622. + * Wev'e ended up with an obj_id that
  14623. + * has been reused but not yet deleted,
  14624. + * and worse still it has changed type.
  14625. + * Delete the old object.
  14626. + */
  14627. +
  14628. + yaffs_del_obj(in);
  14629. + in = NULL;
  14630. + }
  14631. +
  14632. + in = yaffs_find_or_create_by_number(dev,
  14633. + tags.obj_id,
  14634. + oh->type);
  14635. +
  14636. + if (!in)
  14637. + alloc_failed = 1;
  14638. +
  14639. + if (in && oh->shadows_obj > 0) {
  14640. +
  14641. + struct yaffs_shadow_fixer *fixer;
  14642. + fixer =
  14643. + kmalloc(sizeof
  14644. + (struct yaffs_shadow_fixer),
  14645. + GFP_NOFS);
  14646. + if (fixer) {
  14647. + fixer->next = shadow_fixers;
  14648. + shadow_fixers = fixer;
  14649. + fixer->obj_id = tags.obj_id;
  14650. + fixer->shadowed_id =
  14651. + oh->shadows_obj;
  14652. + yaffs_trace(YAFFS_TRACE_SCAN,
  14653. + " Shadow fixer: %d shadows %d",
  14654. + fixer->obj_id,
  14655. + fixer->shadowed_id);
  14656. +
  14657. + }
  14658. +
  14659. + }
  14660. +
  14661. + if (in && in->valid) {
  14662. + /* We have already filled this one.
  14663. + * We have a duplicate and need to
  14664. + * resolve it. */
  14665. +
  14666. + unsigned existing_serial = in->serial;
  14667. + unsigned new_serial =
  14668. + tags.serial_number;
  14669. +
  14670. + if (((existing_serial + 1) & 3) ==
  14671. + new_serial) {
  14672. + /* Use new one - destroy the
  14673. + * exisiting one */
  14674. + yaffs_chunk_del(dev,
  14675. + in->hdr_chunk,
  14676. + 1, __LINE__);
  14677. + in->valid = 0;
  14678. + } else {
  14679. + /* Use existing - destroy
  14680. + * this one. */
  14681. + yaffs_chunk_del(dev, chunk, 1,
  14682. + __LINE__);
  14683. + }
  14684. + }
  14685. +
  14686. + if (in && !in->valid &&
  14687. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  14688. + tags.obj_id ==
  14689. + YAFFS_OBJECTID_LOSTNFOUND)) {
  14690. + /* We only load some info, don't fiddle
  14691. + * with directory structure */
  14692. + in->valid = 1;
  14693. + in->variant_type = oh->type;
  14694. +
  14695. + in->yst_mode = oh->yst_mode;
  14696. + yaffs_load_attribs(in, oh);
  14697. + in->hdr_chunk = chunk;
  14698. + in->serial = tags.serial_number;
  14699. +
  14700. + } else if (in && !in->valid) {
  14701. + /* we need to load this info */
  14702. +
  14703. + in->valid = 1;
  14704. + in->variant_type = oh->type;
  14705. +
  14706. + in->yst_mode = oh->yst_mode;
  14707. + yaffs_load_attribs(in, oh);
  14708. + in->hdr_chunk = chunk;
  14709. + in->serial = tags.serial_number;
  14710. +
  14711. + yaffs_set_obj_name_from_oh(in, oh);
  14712. + in->dirty = 0;
  14713. +
  14714. + /* directory stuff...
  14715. + * hook up to parent
  14716. + */
  14717. +
  14718. + parent =
  14719. + yaffs_find_or_create_by_number
  14720. + (dev, oh->parent_obj_id,
  14721. + YAFFS_OBJECT_TYPE_DIRECTORY);
  14722. + if (!parent)
  14723. + alloc_failed = 1;
  14724. + if (parent && parent->variant_type ==
  14725. + YAFFS_OBJECT_TYPE_UNKNOWN) {
  14726. + /* Set up as a directory */
  14727. + parent->variant_type =
  14728. + YAFFS_OBJECT_TYPE_DIRECTORY;
  14729. + INIT_LIST_HEAD(&parent->
  14730. + variant.dir_variant.
  14731. + children);
  14732. + } else if (!parent ||
  14733. + parent->variant_type !=
  14734. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  14735. + /* Hoosterman, a problem....
  14736. + * We're trying to use a
  14737. + * non-directory as a directory
  14738. + */
  14739. +
  14740. + yaffs_trace(YAFFS_TRACE_ERROR,
  14741. + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  14742. + );
  14743. + parent = dev->lost_n_found;
  14744. + }
  14745. +
  14746. + yaffs_add_obj_to_dir(parent, in);
  14747. +
  14748. + switch (in->variant_type) {
  14749. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  14750. + /* Todo got a problem */
  14751. + break;
  14752. + case YAFFS_OBJECT_TYPE_FILE:
  14753. + if (dev->param.
  14754. + use_header_file_size)
  14755. + in->variant.
  14756. + file_variant.file_size
  14757. + = yaffs_oh_to_size(oh);
  14758. + break;
  14759. + case YAFFS_OBJECT_TYPE_HARDLINK:
  14760. + in->variant.
  14761. + hardlink_variant.equiv_id =
  14762. + oh->equiv_id;
  14763. + list_add(&in->hard_links,
  14764. + &hard_list);
  14765. + break;
  14766. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  14767. + /* Do nothing */
  14768. + break;
  14769. + case YAFFS_OBJECT_TYPE_SPECIAL:
  14770. + /* Do nothing */
  14771. + break;
  14772. + case YAFFS_OBJECT_TYPE_SYMLINK:
  14773. + in->variant.symlink_variant.
  14774. + alias =
  14775. + yaffs_clone_str(oh->alias);
  14776. + if (!in->variant.
  14777. + symlink_variant.alias)
  14778. + alloc_failed = 1;
  14779. + break;
  14780. + }
  14781. + }
  14782. + }
  14783. + }
  14784. +
  14785. + if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  14786. + /* If we got this far while scanning,
  14787. + * then the block is fully allocated. */
  14788. + state = YAFFS_BLOCK_STATE_FULL;
  14789. + }
  14790. +
  14791. + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
  14792. + /* If the block was partially allocated then
  14793. + * treat it as fully allocated. */
  14794. + state = YAFFS_BLOCK_STATE_FULL;
  14795. + dev->alloc_block = -1;
  14796. + }
  14797. +
  14798. + bi->block_state = state;
  14799. +
  14800. + /* Now let's see if it was dirty */
  14801. + if (bi->pages_in_use == 0 &&
  14802. + !bi->has_shrink_hdr &&
  14803. + bi->block_state == YAFFS_BLOCK_STATE_FULL)
  14804. + yaffs_block_became_dirty(dev, blk);
  14805. + }
  14806. +
  14807. + /* Ok, we've done all the scanning.
  14808. + * Fix up the hard link chains.
  14809. + * We should now have scanned all the objects, now it's time to add
  14810. + * these hardlinks.
  14811. + */
  14812. +
  14813. + yaffs_link_fixup(dev, &hard_list);
  14814. +
  14815. + /*
  14816. + * Fix up any shadowed objects.
  14817. + * There should not be more than one of these.
  14818. + */
  14819. + {
  14820. + struct yaffs_shadow_fixer *fixer;
  14821. + struct yaffs_obj *obj;
  14822. +
  14823. + while (shadow_fixers) {
  14824. + fixer = shadow_fixers;
  14825. + shadow_fixers = fixer->next;
  14826. + /* Complete the rename transaction by deleting the
  14827. + * shadowed object then setting the object header
  14828. + to unshadowed.
  14829. + */
  14830. + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
  14831. + if (obj)
  14832. + yaffs_del_obj(obj);
  14833. +
  14834. + obj = yaffs_find_by_number(dev, fixer->obj_id);
  14835. +
  14836. + if (obj)
  14837. + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
  14838. +
  14839. + kfree(fixer);
  14840. + }
  14841. + }
  14842. +
  14843. + yaffs_release_temp_buffer(dev, chunk_data);
  14844. +
  14845. + if (alloc_failed)
  14846. + return YAFFS_FAIL;
  14847. +
  14848. + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
  14849. +
  14850. + return YAFFS_OK;
  14851. +}
  14852. diff -Nur linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs1.h linux-3.14.4/fs/yaffs2/yaffs_yaffs1.h
  14853. --- linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs1.h 1970-01-01 01:00:00.000000000 +0100
  14854. +++ linux-3.14.4/fs/yaffs2/yaffs_yaffs1.h 2014-05-17 02:22:30.000000000 +0200
  14855. @@ -0,0 +1,22 @@
  14856. +/*
  14857. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  14858. + *
  14859. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14860. + * for Toby Churchill Ltd and Brightstar Engineering
  14861. + *
  14862. + * Created by Charles Manning <charles@aleph1.co.uk>
  14863. + *
  14864. + * This program is free software; you can redistribute it and/or modify
  14865. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  14866. + * published by the Free Software Foundation.
  14867. + *
  14868. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14869. + */
  14870. +
  14871. +#ifndef __YAFFS_YAFFS1_H__
  14872. +#define __YAFFS_YAFFS1_H__
  14873. +
  14874. +#include "yaffs_guts.h"
  14875. +int yaffs1_scan(struct yaffs_dev *dev);
  14876. +
  14877. +#endif
  14878. diff -Nur linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs2.c linux-3.14.4/fs/yaffs2/yaffs_yaffs2.c
  14879. --- linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs2.c 1970-01-01 01:00:00.000000000 +0100
  14880. +++ linux-3.14.4/fs/yaffs2/yaffs_yaffs2.c 2014-05-17 02:22:30.000000000 +0200
  14881. @@ -0,0 +1,1534 @@
  14882. +/*
  14883. + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  14884. + *
  14885. + * Copyright (C) 2002-2011 Aleph One Ltd.
  14886. + * for Toby Churchill Ltd and Brightstar Engineering
  14887. + *
  14888. + * Created by Charles Manning <charles@aleph1.co.uk>
  14889. + *
  14890. + * This program is free software; you can redistribute it and/or modify
  14891. + * it under the terms of the GNU General Public License version 2 as
  14892. + * published by the Free Software Foundation.
  14893. + */
  14894. +
  14895. +#include "yaffs_guts.h"
  14896. +#include "yaffs_trace.h"
  14897. +#include "yaffs_yaffs2.h"
  14898. +#include "yaffs_checkptrw.h"
  14899. +#include "yaffs_bitmap.h"
  14900. +#include "yaffs_nand.h"
  14901. +#include "yaffs_getblockinfo.h"
  14902. +#include "yaffs_verify.h"
  14903. +#include "yaffs_attribs.h"
  14904. +#include "yaffs_summary.h"
  14905. +
  14906. +/*
  14907. + * Checkpoints are really no benefit on very small partitions.
  14908. + *
  14909. + * To save space on small partitions don't bother with checkpoints unless
  14910. + * the partition is at least this big.
  14911. + */
  14912. +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
  14913. +#define YAFFS_SMALL_HOLE_THRESHOLD 4
  14914. +
  14915. +/*
  14916. + * Oldest Dirty Sequence Number handling.
  14917. + */
  14918. +
  14919. +/* yaffs_calc_oldest_dirty_seq()
  14920. + * yaffs2_find_oldest_dirty_seq()
  14921. + * Calculate the oldest dirty sequence number if we don't know it.
  14922. + */
  14923. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
  14924. +{
  14925. + int i;
  14926. + unsigned seq;
  14927. + unsigned block_no = 0;
  14928. + struct yaffs_block_info *b;
  14929. +
  14930. + if (!dev->param.is_yaffs2)
  14931. + return;
  14932. +
  14933. + /* Find the oldest dirty sequence number. */
  14934. + seq = dev->seq_number + 1;
  14935. + b = dev->block_info;
  14936. + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  14937. + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
  14938. + (b->pages_in_use - b->soft_del_pages) <
  14939. + dev->param.chunks_per_block &&
  14940. + b->seq_number < seq) {
  14941. + seq = b->seq_number;
  14942. + block_no = i;
  14943. + }
  14944. + b++;
  14945. + }
  14946. +
  14947. + if (block_no) {
  14948. + dev->oldest_dirty_seq = seq;
  14949. + dev->oldest_dirty_block = block_no;
  14950. + }
  14951. +}
  14952. +
  14953. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
  14954. +{
  14955. + if (!dev->param.is_yaffs2)
  14956. + return;
  14957. +
  14958. + if (!dev->oldest_dirty_seq)
  14959. + yaffs_calc_oldest_dirty_seq(dev);
  14960. +}
  14961. +
  14962. +/*
  14963. + * yaffs_clear_oldest_dirty_seq()
  14964. + * Called when a block is erased or marked bad. (ie. when its seq_number
  14965. + * becomes invalid). If the value matches the oldest then we clear
  14966. + * dev->oldest_dirty_seq to force its recomputation.
  14967. + */
  14968. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  14969. + struct yaffs_block_info *bi)
  14970. +{
  14971. +
  14972. + if (!dev->param.is_yaffs2)
  14973. + return;
  14974. +
  14975. + if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
  14976. + dev->oldest_dirty_seq = 0;
  14977. + dev->oldest_dirty_block = 0;
  14978. + }
  14979. +}
  14980. +
  14981. +/*
  14982. + * yaffs2_update_oldest_dirty_seq()
  14983. + * Update the oldest dirty sequence number whenever we dirty a block.
  14984. + * Only do this if the oldest_dirty_seq is actually being tracked.
  14985. + */
  14986. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  14987. + struct yaffs_block_info *bi)
  14988. +{
  14989. + if (!dev->param.is_yaffs2)
  14990. + return;
  14991. +
  14992. + if (dev->oldest_dirty_seq) {
  14993. + if (dev->oldest_dirty_seq > bi->seq_number) {
  14994. + dev->oldest_dirty_seq = bi->seq_number;
  14995. + dev->oldest_dirty_block = block_no;
  14996. + }
  14997. + }
  14998. +}
  14999. +
  15000. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
  15001. +{
  15002. +
  15003. + if (!dev->param.is_yaffs2)
  15004. + return 1; /* disqualification only applies to yaffs2. */
  15005. +
  15006. + if (!bi->has_shrink_hdr)
  15007. + return 1; /* can gc */
  15008. +
  15009. + yaffs2_find_oldest_dirty_seq(dev);
  15010. +
  15011. + /* Can't do gc of this block if there are any blocks older than this
  15012. + * one that have discarded pages.
  15013. + */
  15014. + return (bi->seq_number <= dev->oldest_dirty_seq);
  15015. +}
  15016. +
  15017. +/*
  15018. + * yaffs2_find_refresh_block()
  15019. + * periodically finds the oldest full block by sequence number for refreshing.
  15020. + * Only for yaffs2.
  15021. + */
  15022. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
  15023. +{
  15024. + u32 b;
  15025. + u32 oldest = 0;
  15026. + u32 oldest_seq = 0;
  15027. + struct yaffs_block_info *bi;
  15028. +
  15029. + if (!dev->param.is_yaffs2)
  15030. + return oldest;
  15031. +
  15032. + /*
  15033. + * If refresh period < 10 then refreshing is disabled.
  15034. + */
  15035. + if (dev->param.refresh_period < 10)
  15036. + return oldest;
  15037. +
  15038. + /*
  15039. + * Fix broken values.
  15040. + */
  15041. + if (dev->refresh_skip > dev->param.refresh_period)
  15042. + dev->refresh_skip = dev->param.refresh_period;
  15043. +
  15044. + if (dev->refresh_skip > 0)
  15045. + return oldest;
  15046. +
  15047. + /*
  15048. + * Refresh skip is now zero.
  15049. + * We'll do a refresh this time around....
  15050. + * Update the refresh skip and find the oldest block.
  15051. + */
  15052. + dev->refresh_skip = dev->param.refresh_period;
  15053. + dev->refresh_count++;
  15054. + bi = dev->block_info;
  15055. + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  15056. +
  15057. + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  15058. +
  15059. + if (oldest < 1 || bi->seq_number < oldest_seq) {
  15060. + oldest = b;
  15061. + oldest_seq = bi->seq_number;
  15062. + }
  15063. + }
  15064. + bi++;
  15065. + }
  15066. +
  15067. + if (oldest > 0) {
  15068. + yaffs_trace(YAFFS_TRACE_GC,
  15069. + "GC refresh count %d selected block %d with seq_number %d",
  15070. + dev->refresh_count, oldest, oldest_seq);
  15071. + }
  15072. +
  15073. + return oldest;
  15074. +}
  15075. +
  15076. +int yaffs2_checkpt_required(struct yaffs_dev *dev)
  15077. +{
  15078. + int nblocks;
  15079. +
  15080. + if (!dev->param.is_yaffs2)
  15081. + return 0;
  15082. +
  15083. + nblocks = dev->internal_end_block - dev->internal_start_block + 1;
  15084. +
  15085. + return !dev->param.skip_checkpt_wr &&
  15086. + !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
  15087. +}
  15088. +
  15089. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
  15090. +{
  15091. + int retval;
  15092. + int n_bytes = 0;
  15093. + int n_blocks;
  15094. + int dev_blocks;
  15095. +
  15096. + if (!dev->param.is_yaffs2)
  15097. + return 0;
  15098. +
  15099. + if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
  15100. + /* Not a valid value so recalculate */
  15101. + dev_blocks = dev->param.end_block - dev->param.start_block + 1;
  15102. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15103. + n_bytes += sizeof(struct yaffs_checkpt_dev);
  15104. + n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
  15105. + n_bytes += dev_blocks * dev->chunk_bit_stride;
  15106. + n_bytes +=
  15107. + (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
  15108. + dev->n_obj;
  15109. + n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
  15110. + n_bytes += sizeof(struct yaffs_checkpt_validity);
  15111. + n_bytes += sizeof(u32); /* checksum */
  15112. +
  15113. + /* Round up and add 2 blocks to allow for some bad blocks,
  15114. + * so add 3 */
  15115. +
  15116. + n_blocks =
  15117. + (n_bytes /
  15118. + (dev->data_bytes_per_chunk *
  15119. + dev->param.chunks_per_block)) + 3;
  15120. +
  15121. + dev->checkpoint_blocks_required = n_blocks;
  15122. + }
  15123. +
  15124. + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
  15125. + if (retval < 0)
  15126. + retval = 0;
  15127. + return retval;
  15128. +}
  15129. +
  15130. +/*--------------------- Checkpointing --------------------*/
  15131. +
  15132. +static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15133. +{
  15134. + struct yaffs_checkpt_validity cp;
  15135. +
  15136. + memset(&cp, 0, sizeof(cp));
  15137. +
  15138. + cp.struct_type = sizeof(cp);
  15139. + cp.magic = YAFFS_MAGIC;
  15140. + cp.version = YAFFS_CHECKPOINT_VERSION;
  15141. + cp.head = (head) ? 1 : 0;
  15142. +
  15143. + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
  15144. +}
  15145. +
  15146. +static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  15147. +{
  15148. + struct yaffs_checkpt_validity cp;
  15149. + int ok;
  15150. +
  15151. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15152. +
  15153. + if (ok)
  15154. + ok = (cp.struct_type == sizeof(cp)) &&
  15155. + (cp.magic == YAFFS_MAGIC) &&
  15156. + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  15157. + (cp.head == ((head) ? 1 : 0));
  15158. + return ok ? 1 : 0;
  15159. +}
  15160. +
  15161. +static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
  15162. + struct yaffs_dev *dev)
  15163. +{
  15164. + cp->n_erased_blocks = dev->n_erased_blocks;
  15165. + cp->alloc_block = dev->alloc_block;
  15166. + cp->alloc_page = dev->alloc_page;
  15167. + cp->n_free_chunks = dev->n_free_chunks;
  15168. +
  15169. + cp->n_deleted_files = dev->n_deleted_files;
  15170. + cp->n_unlinked_files = dev->n_unlinked_files;
  15171. + cp->n_bg_deletions = dev->n_bg_deletions;
  15172. + cp->seq_number = dev->seq_number;
  15173. +
  15174. +}
  15175. +
  15176. +static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
  15177. + struct yaffs_checkpt_dev *cp)
  15178. +{
  15179. + dev->n_erased_blocks = cp->n_erased_blocks;
  15180. + dev->alloc_block = cp->alloc_block;
  15181. + dev->alloc_page = cp->alloc_page;
  15182. + dev->n_free_chunks = cp->n_free_chunks;
  15183. +
  15184. + dev->n_deleted_files = cp->n_deleted_files;
  15185. + dev->n_unlinked_files = cp->n_unlinked_files;
  15186. + dev->n_bg_deletions = cp->n_bg_deletions;
  15187. + dev->seq_number = cp->seq_number;
  15188. +}
  15189. +
  15190. +static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
  15191. +{
  15192. + struct yaffs_checkpt_dev cp;
  15193. + u32 n_bytes;
  15194. + u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  15195. + int ok;
  15196. +
  15197. + /* Write device runtime values */
  15198. + yaffs2_dev_to_checkpt_dev(&cp, dev);
  15199. + cp.struct_type = sizeof(cp);
  15200. +
  15201. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15202. + if (!ok)
  15203. + return 0;
  15204. +
  15205. + /* Write block info */
  15206. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15207. + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
  15208. + if (!ok)
  15209. + return 0;
  15210. +
  15211. + /* Write chunk bits */
  15212. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15213. + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15214. +
  15215. + return ok ? 1 : 0;
  15216. +}
  15217. +
  15218. +static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
  15219. +{
  15220. + struct yaffs_checkpt_dev cp;
  15221. + u32 n_bytes;
  15222. + u32 n_blocks =
  15223. + (dev->internal_end_block - dev->internal_start_block + 1);
  15224. + int ok;
  15225. +
  15226. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15227. + if (!ok)
  15228. + return 0;
  15229. +
  15230. + if (cp.struct_type != sizeof(cp))
  15231. + return 0;
  15232. +
  15233. + yaffs_checkpt_dev_to_dev(dev, &cp);
  15234. +
  15235. + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  15236. +
  15237. + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
  15238. +
  15239. + if (!ok)
  15240. + return 0;
  15241. +
  15242. + n_bytes = n_blocks * dev->chunk_bit_stride;
  15243. +
  15244. + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
  15245. +
  15246. + return ok ? 1 : 0;
  15247. +}
  15248. +
  15249. +static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
  15250. + struct yaffs_obj *obj)
  15251. +{
  15252. + cp->obj_id = obj->obj_id;
  15253. + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
  15254. + cp->hdr_chunk = obj->hdr_chunk;
  15255. + cp->variant_type = obj->variant_type;
  15256. + cp->deleted = obj->deleted;
  15257. + cp->soft_del = obj->soft_del;
  15258. + cp->unlinked = obj->unlinked;
  15259. + cp->fake = obj->fake;
  15260. + cp->rename_allowed = obj->rename_allowed;
  15261. + cp->unlink_allowed = obj->unlink_allowed;
  15262. + cp->serial = obj->serial;
  15263. + cp->n_data_chunks = obj->n_data_chunks;
  15264. +
  15265. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15266. + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
  15267. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15268. + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
  15269. +}
  15270. +
  15271. +static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
  15272. + struct yaffs_checkpt_obj *cp)
  15273. +{
  15274. + struct yaffs_obj *parent;
  15275. +
  15276. + if (obj->variant_type != cp->variant_type) {
  15277. + yaffs_trace(YAFFS_TRACE_ERROR,
  15278. + "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
  15279. + cp->obj_id, cp->variant_type, cp->hdr_chunk,
  15280. + obj->variant_type);
  15281. + return 0;
  15282. + }
  15283. +
  15284. + obj->obj_id = cp->obj_id;
  15285. +
  15286. + if (cp->parent_id)
  15287. + parent = yaffs_find_or_create_by_number(obj->my_dev,
  15288. + cp->parent_id,
  15289. + YAFFS_OBJECT_TYPE_DIRECTORY);
  15290. + else
  15291. + parent = NULL;
  15292. +
  15293. + if (parent) {
  15294. + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  15295. + yaffs_trace(YAFFS_TRACE_ALWAYS,
  15296. + "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
  15297. + cp->obj_id, cp->parent_id,
  15298. + cp->variant_type, cp->hdr_chunk,
  15299. + parent->variant_type);
  15300. + return 0;
  15301. + }
  15302. + yaffs_add_obj_to_dir(parent, obj);
  15303. + }
  15304. +
  15305. + obj->hdr_chunk = cp->hdr_chunk;
  15306. + obj->variant_type = cp->variant_type;
  15307. + obj->deleted = cp->deleted;
  15308. + obj->soft_del = cp->soft_del;
  15309. + obj->unlinked = cp->unlinked;
  15310. + obj->fake = cp->fake;
  15311. + obj->rename_allowed = cp->rename_allowed;
  15312. + obj->unlink_allowed = cp->unlink_allowed;
  15313. + obj->serial = cp->serial;
  15314. + obj->n_data_chunks = cp->n_data_chunks;
  15315. +
  15316. + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  15317. + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
  15318. + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  15319. + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
  15320. +
  15321. + if (obj->hdr_chunk > 0)
  15322. + obj->lazy_loaded = 1;
  15323. + return 1;
  15324. +}
  15325. +
  15326. +static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
  15327. + struct yaffs_tnode *tn, u32 level,
  15328. + int chunk_offset)
  15329. +{
  15330. + int i;
  15331. + struct yaffs_dev *dev = in->my_dev;
  15332. + int ok = 1;
  15333. + u32 base_offset;
  15334. +
  15335. + if (!tn)
  15336. + return 1;
  15337. +
  15338. + if (level > 0) {
  15339. + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  15340. + if (!tn->internal[i])
  15341. + continue;
  15342. + ok = yaffs2_checkpt_tnode_worker(in,
  15343. + tn->internal[i],
  15344. + level - 1,
  15345. + (chunk_offset <<
  15346. + YAFFS_TNODES_INTERNAL_BITS) + i);
  15347. + }
  15348. + return ok;
  15349. + }
  15350. +
  15351. + /* Level 0 tnode */
  15352. + base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
  15353. + ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
  15354. + sizeof(base_offset));
  15355. + if (ok)
  15356. + ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
  15357. + dev->tnode_size);
  15358. +
  15359. + return ok;
  15360. +}
  15361. +
  15362. +static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
  15363. +{
  15364. + u32 end_marker = ~0;
  15365. + int ok = 1;
  15366. +
  15367. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15368. + return ok;
  15369. +
  15370. + ok = yaffs2_checkpt_tnode_worker(obj,
  15371. + obj->variant.file_variant.top,
  15372. + obj->variant.file_variant.
  15373. + top_level, 0);
  15374. + if (ok)
  15375. + ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
  15376. + sizeof(end_marker)) == sizeof(end_marker));
  15377. +
  15378. + return ok ? 1 : 0;
  15379. +}
  15380. +
  15381. +static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
  15382. +{
  15383. + u32 base_chunk;
  15384. + int ok = 1;
  15385. + struct yaffs_dev *dev = obj->my_dev;
  15386. + struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
  15387. + struct yaffs_tnode *tn;
  15388. + int nread = 0;
  15389. +
  15390. + ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
  15391. + sizeof(base_chunk));
  15392. +
  15393. + while (ok && (~base_chunk)) {
  15394. + nread++;
  15395. + /* Read level 0 tnode */
  15396. +
  15397. + tn = yaffs_get_tnode(dev);
  15398. + if (tn)
  15399. + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
  15400. + dev->tnode_size);
  15401. + else
  15402. + ok = 0;
  15403. +
  15404. + if (tn && ok)
  15405. + ok = yaffs_add_find_tnode_0(dev,
  15406. + file_stuct_ptr,
  15407. + base_chunk, tn) ? 1 : 0;
  15408. +
  15409. + if (ok)
  15410. + ok = (yaffs2_checkpt_rd
  15411. + (dev, &base_chunk,
  15412. + sizeof(base_chunk)) == sizeof(base_chunk));
  15413. + }
  15414. +
  15415. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15416. + "Checkpoint read tnodes %d records, last %d. ok %d",
  15417. + nread, base_chunk, ok);
  15418. +
  15419. + return ok ? 1 : 0;
  15420. +}
  15421. +
  15422. +static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
  15423. +{
  15424. + struct yaffs_obj *obj;
  15425. + struct yaffs_checkpt_obj cp;
  15426. + int i;
  15427. + int ok = 1;
  15428. + struct list_head *lh;
  15429. +
  15430. + /* Iterate through the objects in each hash entry,
  15431. + * dumping them to the checkpointing stream.
  15432. + */
  15433. +
  15434. + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  15435. + list_for_each(lh, &dev->obj_bucket[i].list) {
  15436. + obj = list_entry(lh, struct yaffs_obj, hash_link);
  15437. + if (!obj->defered_free) {
  15438. + yaffs2_obj_checkpt_obj(&cp, obj);
  15439. + cp.struct_type = sizeof(cp);
  15440. +
  15441. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15442. + "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
  15443. + cp.obj_id, cp.parent_id,
  15444. + cp.variant_type, cp.hdr_chunk, obj);
  15445. +
  15446. + ok = (yaffs2_checkpt_wr(dev, &cp,
  15447. + sizeof(cp)) == sizeof(cp));
  15448. +
  15449. + if (ok &&
  15450. + obj->variant_type ==
  15451. + YAFFS_OBJECT_TYPE_FILE)
  15452. + ok = yaffs2_wr_checkpt_tnodes(obj);
  15453. + }
  15454. + }
  15455. + }
  15456. +
  15457. + /* Dump end of list */
  15458. + memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
  15459. + cp.struct_type = sizeof(cp);
  15460. +
  15461. + if (ok)
  15462. + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  15463. +
  15464. + return ok ? 1 : 0;
  15465. +}
  15466. +
  15467. +static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
  15468. +{
  15469. + struct yaffs_obj *obj;
  15470. + struct yaffs_checkpt_obj cp;
  15471. + int ok = 1;
  15472. + int done = 0;
  15473. + LIST_HEAD(hard_list);
  15474. +
  15475. +
  15476. + while (ok && !done) {
  15477. + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  15478. + if (cp.struct_type != sizeof(cp)) {
  15479. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15480. + "struct size %d instead of %d ok %d",
  15481. + cp.struct_type, (int)sizeof(cp), ok);
  15482. + ok = 0;
  15483. + }
  15484. +
  15485. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15486. + "Checkpoint read object %d parent %d type %d chunk %d ",
  15487. + cp.obj_id, cp.parent_id, cp.variant_type,
  15488. + cp.hdr_chunk);
  15489. +
  15490. + if (ok && cp.obj_id == ~0) {
  15491. + done = 1;
  15492. + } else if (ok) {
  15493. + obj =
  15494. + yaffs_find_or_create_by_number(dev, cp.obj_id,
  15495. + cp.variant_type);
  15496. + if (obj) {
  15497. + ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
  15498. + if (!ok)
  15499. + break;
  15500. + if (obj->variant_type ==
  15501. + YAFFS_OBJECT_TYPE_FILE) {
  15502. + ok = yaffs2_rd_checkpt_tnodes(obj);
  15503. + } else if (obj->variant_type ==
  15504. + YAFFS_OBJECT_TYPE_HARDLINK) {
  15505. + list_add(&obj->hard_links, &hard_list);
  15506. + }
  15507. + } else {
  15508. + ok = 0;
  15509. + }
  15510. + }
  15511. + }
  15512. +
  15513. + if (ok)
  15514. + yaffs_link_fixup(dev, &hard_list);
  15515. +
  15516. + return ok ? 1 : 0;
  15517. +}
  15518. +
  15519. +static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
  15520. +{
  15521. + u32 checkpt_sum;
  15522. + int ok;
  15523. +
  15524. + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
  15525. +
  15526. + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
  15527. + sizeof(checkpt_sum));
  15528. +
  15529. + if (!ok)
  15530. + return 0;
  15531. +
  15532. + return 1;
  15533. +}
  15534. +
  15535. +static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
  15536. +{
  15537. + u32 checkpt_sum0;
  15538. + u32 checkpt_sum1;
  15539. + int ok;
  15540. +
  15541. + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
  15542. +
  15543. + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
  15544. + sizeof(checkpt_sum1));
  15545. +
  15546. + if (!ok)
  15547. + return 0;
  15548. +
  15549. + if (checkpt_sum0 != checkpt_sum1)
  15550. + return 0;
  15551. +
  15552. + return 1;
  15553. +}
  15554. +
  15555. +static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
  15556. +{
  15557. + int ok = 1;
  15558. +
  15559. + if (!yaffs2_checkpt_required(dev)) {
  15560. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15561. + "skipping checkpoint write");
  15562. + ok = 0;
  15563. + }
  15564. +
  15565. + if (ok)
  15566. + ok = yaffs2_checkpt_open(dev, 1);
  15567. +
  15568. + if (ok) {
  15569. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15570. + "write checkpoint validity");
  15571. + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
  15572. + }
  15573. + if (ok) {
  15574. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15575. + "write checkpoint device");
  15576. + ok = yaffs2_wr_checkpt_dev(dev);
  15577. + }
  15578. + if (ok) {
  15579. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15580. + "write checkpoint objects");
  15581. + ok = yaffs2_wr_checkpt_objs(dev);
  15582. + }
  15583. + if (ok) {
  15584. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15585. + "write checkpoint validity");
  15586. + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
  15587. + }
  15588. +
  15589. + if (ok)
  15590. + ok = yaffs2_wr_checkpt_sum(dev);
  15591. +
  15592. + if (!yaffs_checkpt_close(dev))
  15593. + ok = 0;
  15594. +
  15595. + if (ok)
  15596. + dev->is_checkpointed = 1;
  15597. + else
  15598. + dev->is_checkpointed = 0;
  15599. +
  15600. + return dev->is_checkpointed;
  15601. +}
  15602. +
  15603. +static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
  15604. +{
  15605. + int ok = 1;
  15606. +
  15607. + if (!dev->param.is_yaffs2)
  15608. + ok = 0;
  15609. +
  15610. + if (ok && dev->param.skip_checkpt_rd) {
  15611. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15612. + "skipping checkpoint read");
  15613. + ok = 0;
  15614. + }
  15615. +
  15616. + if (ok)
  15617. + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
  15618. +
  15619. + if (ok) {
  15620. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15621. + "read checkpoint validity");
  15622. + ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
  15623. + }
  15624. + if (ok) {
  15625. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15626. + "read checkpoint device");
  15627. + ok = yaffs2_rd_checkpt_dev(dev);
  15628. + }
  15629. + if (ok) {
  15630. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15631. + "read checkpoint objects");
  15632. + ok = yaffs2_rd_checkpt_objs(dev);
  15633. + }
  15634. + if (ok) {
  15635. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15636. + "read checkpoint validity");
  15637. + ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
  15638. + }
  15639. +
  15640. + if (ok) {
  15641. + ok = yaffs2_rd_checkpt_sum(dev);
  15642. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15643. + "read checkpoint checksum %d", ok);
  15644. + }
  15645. +
  15646. + if (!yaffs_checkpt_close(dev))
  15647. + ok = 0;
  15648. +
  15649. + if (ok)
  15650. + dev->is_checkpointed = 1;
  15651. + else
  15652. + dev->is_checkpointed = 0;
  15653. +
  15654. + return ok ? 1 : 0;
  15655. +}
  15656. +
  15657. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
  15658. +{
  15659. + if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
  15660. + dev->is_checkpointed = 0;
  15661. + yaffs2_checkpt_invalidate_stream(dev);
  15662. + }
  15663. + if (dev->param.sb_dirty_fn)
  15664. + dev->param.sb_dirty_fn(dev);
  15665. +}
  15666. +
  15667. +int yaffs_checkpoint_save(struct yaffs_dev *dev)
  15668. +{
  15669. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15670. + "save entry: is_checkpointed %d",
  15671. + dev->is_checkpointed);
  15672. +
  15673. + yaffs_verify_objects(dev);
  15674. + yaffs_verify_blocks(dev);
  15675. + yaffs_verify_free_chunks(dev);
  15676. +
  15677. + if (!dev->is_checkpointed) {
  15678. + yaffs2_checkpt_invalidate(dev);
  15679. + yaffs2_wr_checkpt_data(dev);
  15680. + }
  15681. +
  15682. + yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  15683. + "save exit: is_checkpointed %d",
  15684. + dev->is_checkpointed);
  15685. +
  15686. + return dev->is_checkpointed;
  15687. +}
  15688. +
  15689. +int yaffs2_checkpt_restore(struct yaffs_dev *dev)
  15690. +{
  15691. + int retval;
  15692. +
  15693. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15694. + "restore entry: is_checkpointed %d",
  15695. + dev->is_checkpointed);
  15696. +
  15697. + retval = yaffs2_rd_checkpt_data(dev);
  15698. +
  15699. + if (dev->is_checkpointed) {
  15700. + yaffs_verify_objects(dev);
  15701. + yaffs_verify_blocks(dev);
  15702. + yaffs_verify_free_chunks(dev);
  15703. + }
  15704. +
  15705. + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  15706. + "restore exit: is_checkpointed %d",
  15707. + dev->is_checkpointed);
  15708. +
  15709. + return retval;
  15710. +}
  15711. +
  15712. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
  15713. +{
  15714. + /* if new_size > old_file_size.
  15715. + * We're going to be writing a hole.
  15716. + * If the hole is small then write zeros otherwise write a start
  15717. + * of hole marker.
  15718. + */
  15719. + loff_t old_file_size;
  15720. + loff_t increase;
  15721. + int small_hole;
  15722. + int result = YAFFS_OK;
  15723. + struct yaffs_dev *dev = NULL;
  15724. + u8 *local_buffer = NULL;
  15725. + int small_increase_ok = 0;
  15726. +
  15727. + if (!obj)
  15728. + return YAFFS_FAIL;
  15729. +
  15730. + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  15731. + return YAFFS_FAIL;
  15732. +
  15733. + dev = obj->my_dev;
  15734. +
  15735. + /* Bail out if not yaffs2 mode */
  15736. + if (!dev->param.is_yaffs2)
  15737. + return YAFFS_OK;
  15738. +
  15739. + old_file_size = obj->variant.file_variant.file_size;
  15740. +
  15741. + if (new_size <= old_file_size)
  15742. + return YAFFS_OK;
  15743. +
  15744. + increase = new_size - old_file_size;
  15745. +
  15746. + if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
  15747. + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
  15748. + small_hole = 1;
  15749. + else
  15750. + small_hole = 0;
  15751. +
  15752. + if (small_hole)
  15753. + local_buffer = yaffs_get_temp_buffer(dev);
  15754. +
  15755. + if (local_buffer) {
  15756. + /* fill hole with zero bytes */
  15757. + loff_t pos = old_file_size;
  15758. + int this_write;
  15759. + int written;
  15760. + memset(local_buffer, 0, dev->data_bytes_per_chunk);
  15761. + small_increase_ok = 1;
  15762. +
  15763. + while (increase > 0 && small_increase_ok) {
  15764. + this_write = increase;
  15765. + if (this_write > dev->data_bytes_per_chunk)
  15766. + this_write = dev->data_bytes_per_chunk;
  15767. + written =
  15768. + yaffs_do_file_wr(obj, local_buffer, pos, this_write,
  15769. + 0);
  15770. + if (written == this_write) {
  15771. + pos += this_write;
  15772. + increase -= this_write;
  15773. + } else {
  15774. + small_increase_ok = 0;
  15775. + }
  15776. + }
  15777. +
  15778. + yaffs_release_temp_buffer(dev, local_buffer);
  15779. +
  15780. + /* If out of space then reverse any chunks we've added */
  15781. + if (!small_increase_ok)
  15782. + yaffs_resize_file_down(obj, old_file_size);
  15783. + }
  15784. +
  15785. + if (!small_increase_ok &&
  15786. + obj->parent &&
  15787. + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  15788. + obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
  15789. + /* Write a hole start header with the old file size */
  15790. + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
  15791. + }
  15792. +
  15793. + return result;
  15794. +}
  15795. +
  15796. +struct yaffs_block_index {
  15797. + int seq;
  15798. + int block;
  15799. +};
  15800. +
  15801. +static int yaffs2_ybicmp(const void *a, const void *b)
  15802. +{
  15803. + int aseq = ((struct yaffs_block_index *)a)->seq;
  15804. + int bseq = ((struct yaffs_block_index *)b)->seq;
  15805. + int ablock = ((struct yaffs_block_index *)a)->block;
  15806. + int bblock = ((struct yaffs_block_index *)b)->block;
  15807. +
  15808. + if (aseq == bseq)
  15809. + return ablock - bblock;
  15810. +
  15811. + return aseq - bseq;
  15812. +}
  15813. +
  15814. +static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
  15815. + struct yaffs_block_info *bi,
  15816. + int blk, int chunk_in_block,
  15817. + int *found_chunks,
  15818. + u8 *chunk_data,
  15819. + struct list_head *hard_list,
  15820. + int summary_available)
  15821. +{
  15822. + struct yaffs_obj_hdr *oh;
  15823. + struct yaffs_obj *in;
  15824. + struct yaffs_obj *parent;
  15825. + int equiv_id;
  15826. + loff_t file_size;
  15827. + int is_shrink;
  15828. + int is_unlinked;
  15829. + struct yaffs_ext_tags tags;
  15830. + int result;
  15831. + int alloc_failed = 0;
  15832. + int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
  15833. + struct yaffs_file_var *file_var;
  15834. + struct yaffs_hardlink_var *hl_var;
  15835. + struct yaffs_symlink_var *sl_var;
  15836. +
  15837. + if (summary_available) {
  15838. + result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
  15839. + tags.seq_number = bi->seq_number;
  15840. + }
  15841. +
  15842. + if (!summary_available || tags.obj_id == 0) {
  15843. + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
  15844. + dev->tags_used++;
  15845. + } else {
  15846. + dev->summary_used++;
  15847. + }
  15848. +
  15849. + /* Let's have a good look at this chunk... */
  15850. +
  15851. + if (!tags.chunk_used) {
  15852. + /* An unassigned chunk in the block.
  15853. + * If there are used chunks after this one, then
  15854. + * it is a chunk that was skipped due to failing
  15855. + * the erased check. Just skip it so that it can
  15856. + * be deleted.
  15857. + * But, more typically, We get here when this is
  15858. + * an unallocated chunk and his means that
  15859. + * either the block is empty or this is the one
  15860. + * being allocated from
  15861. + */
  15862. +
  15863. + if (*found_chunks) {
  15864. + /* This is a chunk that was skipped due
  15865. + * to failing the erased check */
  15866. + } else if (chunk_in_block == 0) {
  15867. + /* We're looking at the first chunk in
  15868. + * the block so the block is unused */
  15869. + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  15870. + dev->n_erased_blocks++;
  15871. + } else {
  15872. + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  15873. + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  15874. + if (dev->seq_number == bi->seq_number) {
  15875. + /* Allocating from this block*/
  15876. + yaffs_trace(YAFFS_TRACE_SCAN,
  15877. + " Allocating from %d %d",
  15878. + blk, chunk_in_block);
  15879. +
  15880. + bi->block_state =
  15881. + YAFFS_BLOCK_STATE_ALLOCATING;
  15882. + dev->alloc_block = blk;
  15883. + dev->alloc_page = chunk_in_block;
  15884. + dev->alloc_block_finder = blk;
  15885. + } else {
  15886. + /* This is a partially written block
  15887. + * that is not the current
  15888. + * allocation block.
  15889. + */
  15890. + yaffs_trace(YAFFS_TRACE_SCAN,
  15891. + "Partially written block %d detected. gc will fix this.",
  15892. + blk);
  15893. + }
  15894. + }
  15895. + }
  15896. +
  15897. + dev->n_free_chunks++;
  15898. +
  15899. + } else if (tags.ecc_result ==
  15900. + YAFFS_ECC_RESULT_UNFIXED) {
  15901. + yaffs_trace(YAFFS_TRACE_SCAN,
  15902. + " Unfixed ECC in chunk(%d:%d), chunk ignored",
  15903. + blk, chunk_in_block);
  15904. + dev->n_free_chunks++;
  15905. + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
  15906. + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
  15907. + tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
  15908. + (tags.chunk_id > 0 &&
  15909. + tags.n_bytes > dev->data_bytes_per_chunk) ||
  15910. + tags.seq_number != bi->seq_number) {
  15911. + yaffs_trace(YAFFS_TRACE_SCAN,
  15912. + "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
  15913. + blk, chunk_in_block, tags.obj_id,
  15914. + tags.chunk_id, tags.n_bytes);
  15915. + dev->n_free_chunks++;
  15916. + } else if (tags.chunk_id > 0) {
  15917. + /* chunk_id > 0 so it is a data chunk... */
  15918. + loff_t endpos;
  15919. + loff_t chunk_base = (tags.chunk_id - 1) *
  15920. + dev->data_bytes_per_chunk;
  15921. +
  15922. + *found_chunks = 1;
  15923. +
  15924. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  15925. + bi->pages_in_use++;
  15926. +
  15927. + in = yaffs_find_or_create_by_number(dev,
  15928. + tags.obj_id,
  15929. + YAFFS_OBJECT_TYPE_FILE);
  15930. + if (!in)
  15931. + /* Out of memory */
  15932. + alloc_failed = 1;
  15933. +
  15934. + if (in &&
  15935. + in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
  15936. + chunk_base < in->variant.file_variant.shrink_size) {
  15937. + /* This has not been invalidated by
  15938. + * a resize */
  15939. + if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
  15940. + chunk, -1))
  15941. + alloc_failed = 1;
  15942. +
  15943. + /* File size is calculated by looking at
  15944. + * the data chunks if we have not
  15945. + * seen an object header yet.
  15946. + * Stop this practice once we find an
  15947. + * object header.
  15948. + */
  15949. + endpos = chunk_base + tags.n_bytes;
  15950. +
  15951. + if (!in->valid &&
  15952. + in->variant.file_variant.scanned_size < endpos) {
  15953. + in->variant.file_variant.
  15954. + scanned_size = endpos;
  15955. + in->variant.file_variant.
  15956. + file_size = endpos;
  15957. + }
  15958. + } else if (in) {
  15959. + /* This chunk has been invalidated by a
  15960. + * resize, or a past file deletion
  15961. + * so delete the chunk*/
  15962. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  15963. + }
  15964. + } else {
  15965. + /* chunk_id == 0, so it is an ObjectHeader.
  15966. + * Thus, we read in the object header and make
  15967. + * the object
  15968. + */
  15969. + *found_chunks = 1;
  15970. +
  15971. + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  15972. + bi->pages_in_use++;
  15973. +
  15974. + oh = NULL;
  15975. + in = NULL;
  15976. +
  15977. + if (tags.extra_available) {
  15978. + in = yaffs_find_or_create_by_number(dev,
  15979. + tags.obj_id,
  15980. + tags.extra_obj_type);
  15981. + if (!in)
  15982. + alloc_failed = 1;
  15983. + }
  15984. +
  15985. + if (!in ||
  15986. + (!in->valid && dev->param.disable_lazy_load) ||
  15987. + tags.extra_shadows ||
  15988. + (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  15989. + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
  15990. +
  15991. + /* If we don't have valid info then we
  15992. + * need to read the chunk
  15993. + * TODO In future we can probably defer
  15994. + * reading the chunk and living with
  15995. + * invalid data until needed.
  15996. + */
  15997. +
  15998. + result = yaffs_rd_chunk_tags_nand(dev,
  15999. + chunk,
  16000. + chunk_data,
  16001. + NULL);
  16002. +
  16003. + oh = (struct yaffs_obj_hdr *)chunk_data;
  16004. +
  16005. + if (dev->param.inband_tags) {
  16006. + /* Fix up the header if they got
  16007. + * corrupted by inband tags */
  16008. + oh->shadows_obj =
  16009. + oh->inband_shadowed_obj_id;
  16010. + oh->is_shrink =
  16011. + oh->inband_is_shrink;
  16012. + }
  16013. +
  16014. + if (!in) {
  16015. + in = yaffs_find_or_create_by_number(dev,
  16016. + tags.obj_id, oh->type);
  16017. + if (!in)
  16018. + alloc_failed = 1;
  16019. + }
  16020. + }
  16021. +
  16022. + if (!in) {
  16023. + /* TODO Hoosterman we have a problem! */
  16024. + yaffs_trace(YAFFS_TRACE_ERROR,
  16025. + "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
  16026. + tags.obj_id, chunk);
  16027. + return YAFFS_FAIL;
  16028. + }
  16029. +
  16030. + if (in->valid) {
  16031. + /* We have already filled this one.
  16032. + * We have a duplicate that will be
  16033. + * discarded, but we first have to suck
  16034. + * out resize info if it is a file.
  16035. + */
  16036. + if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
  16037. + ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
  16038. + (tags.extra_available &&
  16039. + tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  16040. + )) {
  16041. + loff_t this_size = (oh) ?
  16042. + yaffs_oh_to_size(oh) :
  16043. + tags.extra_file_size;
  16044. + u32 parent_obj_id = (oh) ?
  16045. + oh->parent_obj_id :
  16046. + tags.extra_parent_id;
  16047. +
  16048. + is_shrink = (oh) ?
  16049. + oh->is_shrink :
  16050. + tags.extra_is_shrink;
  16051. +
  16052. + /* If it is deleted (unlinked
  16053. + * at start also means deleted)
  16054. + * we treat the file size as
  16055. + * being zeroed at this point.
  16056. + */
  16057. + if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
  16058. + parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
  16059. + this_size = 0;
  16060. + is_shrink = 1;
  16061. + }
  16062. +
  16063. + if (is_shrink &&
  16064. + in->variant.file_variant.shrink_size >
  16065. + this_size)
  16066. + in->variant.file_variant.shrink_size =
  16067. + this_size;
  16068. +
  16069. + if (is_shrink)
  16070. + bi->has_shrink_hdr = 1;
  16071. + }
  16072. + /* Use existing - destroy this one. */
  16073. + yaffs_chunk_del(dev, chunk, 1, __LINE__);
  16074. + }
  16075. +
  16076. + if (!in->valid && in->variant_type !=
  16077. + (oh ? oh->type : tags.extra_obj_type)) {
  16078. + yaffs_trace(YAFFS_TRACE_ERROR,
  16079. + "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
  16080. + oh ? oh->type : tags.extra_obj_type,
  16081. + in->variant_type, tags.obj_id,
  16082. + chunk);
  16083. + in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
  16084. + }
  16085. +
  16086. + if (!in->valid &&
  16087. + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  16088. + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
  16089. + /* We only load some info, don't fiddle
  16090. + * with directory structure */
  16091. + in->valid = 1;
  16092. +
  16093. + if (oh) {
  16094. + in->yst_mode = oh->yst_mode;
  16095. + yaffs_load_attribs(in, oh);
  16096. + in->lazy_loaded = 0;
  16097. + } else {
  16098. + in->lazy_loaded = 1;
  16099. + }
  16100. + in->hdr_chunk = chunk;
  16101. +
  16102. + } else if (!in->valid) {
  16103. + /* we need to load this info */
  16104. + in->valid = 1;
  16105. + in->hdr_chunk = chunk;
  16106. + if (oh) {
  16107. + in->variant_type = oh->type;
  16108. + in->yst_mode = oh->yst_mode;
  16109. + yaffs_load_attribs(in, oh);
  16110. +
  16111. + if (oh->shadows_obj > 0)
  16112. + yaffs_handle_shadowed_obj(dev,
  16113. + oh->shadows_obj, 1);
  16114. +
  16115. + yaffs_set_obj_name_from_oh(in, oh);
  16116. + parent = yaffs_find_or_create_by_number(dev,
  16117. + oh->parent_obj_id,
  16118. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16119. + file_size = yaffs_oh_to_size(oh);
  16120. + is_shrink = oh->is_shrink;
  16121. + equiv_id = oh->equiv_id;
  16122. + } else {
  16123. + in->variant_type = tags.extra_obj_type;
  16124. + parent = yaffs_find_or_create_by_number(dev,
  16125. + tags.extra_parent_id,
  16126. + YAFFS_OBJECT_TYPE_DIRECTORY);
  16127. + file_size = tags.extra_file_size;
  16128. + is_shrink = tags.extra_is_shrink;
  16129. + equiv_id = tags.extra_equiv_id;
  16130. + in->lazy_loaded = 1;
  16131. + }
  16132. + in->dirty = 0;
  16133. +
  16134. + if (!parent)
  16135. + alloc_failed = 1;
  16136. +
  16137. + /* directory stuff...
  16138. + * hook up to parent
  16139. + */
  16140. +
  16141. + if (parent &&
  16142. + parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
  16143. + /* Set up as a directory */
  16144. + parent->variant_type =
  16145. + YAFFS_OBJECT_TYPE_DIRECTORY;
  16146. + INIT_LIST_HEAD(&parent->
  16147. + variant.dir_variant.children);
  16148. + } else if (!parent ||
  16149. + parent->variant_type !=
  16150. + YAFFS_OBJECT_TYPE_DIRECTORY) {
  16151. + /* Hoosterman, another problem....
  16152. + * Trying to use a non-directory as a directory
  16153. + */
  16154. +
  16155. + yaffs_trace(YAFFS_TRACE_ERROR,
  16156. + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  16157. + );
  16158. + parent = dev->lost_n_found;
  16159. + }
  16160. + yaffs_add_obj_to_dir(parent, in);
  16161. +
  16162. + is_unlinked = (parent == dev->del_dir) ||
  16163. + (parent == dev->unlinked_dir);
  16164. +
  16165. + if (is_shrink)
  16166. + /* Mark the block */
  16167. + bi->has_shrink_hdr = 1;
  16168. +
  16169. + /* Note re hardlinks.
  16170. + * Since we might scan a hardlink before its equivalent
  16171. + * object is scanned we put them all in a list.
  16172. + * After scanning is complete, we should have all the
  16173. + * objects, so we run through this list and fix up all
  16174. + * the chains.
  16175. + */
  16176. +
  16177. + switch (in->variant_type) {
  16178. + case YAFFS_OBJECT_TYPE_UNKNOWN:
  16179. + /* Todo got a problem */
  16180. + break;
  16181. + case YAFFS_OBJECT_TYPE_FILE:
  16182. + file_var = &in->variant.file_variant;
  16183. + if (file_var->scanned_size < file_size) {
  16184. + /* This covers the case where the file
  16185. + * size is greater than the data held.
  16186. + * This will happen if the file is
  16187. + * resized to be larger than its
  16188. + * current data extents.
  16189. + */
  16190. + file_var->file_size = file_size;
  16191. + file_var->scanned_size = file_size;
  16192. + }
  16193. +
  16194. + if (file_var->shrink_size > file_size)
  16195. + file_var->shrink_size = file_size;
  16196. +
  16197. + break;
  16198. + case YAFFS_OBJECT_TYPE_HARDLINK:
  16199. + hl_var = &in->variant.hardlink_variant;
  16200. + if (!is_unlinked) {
  16201. + hl_var->equiv_id = equiv_id;
  16202. + list_add(&in->hard_links, hard_list);
  16203. + }
  16204. + break;
  16205. + case YAFFS_OBJECT_TYPE_DIRECTORY:
  16206. + /* Do nothing */
  16207. + break;
  16208. + case YAFFS_OBJECT_TYPE_SPECIAL:
  16209. + /* Do nothing */
  16210. + break;
  16211. + case YAFFS_OBJECT_TYPE_SYMLINK:
  16212. + sl_var = &in->variant.symlink_variant;
  16213. + if (oh) {
  16214. + sl_var->alias =
  16215. + yaffs_clone_str(oh->alias);
  16216. + if (!sl_var->alias)
  16217. + alloc_failed = 1;
  16218. + }
  16219. + break;
  16220. + }
  16221. + }
  16222. + }
  16223. + return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
  16224. +}
  16225. +
  16226. +int yaffs2_scan_backwards(struct yaffs_dev *dev)
  16227. +{
  16228. + int blk;
  16229. + int block_iter;
  16230. + int start_iter;
  16231. + int end_iter;
  16232. + int n_to_scan = 0;
  16233. + enum yaffs_block_state state;
  16234. + int c;
  16235. + int deleted;
  16236. + LIST_HEAD(hard_list);
  16237. + struct yaffs_block_info *bi;
  16238. + u32 seq_number;
  16239. + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  16240. + u8 *chunk_data;
  16241. + int found_chunks;
  16242. + int alloc_failed = 0;
  16243. + struct yaffs_block_index *block_index = NULL;
  16244. + int alt_block_index = 0;
  16245. + int summary_available;
  16246. +
  16247. + yaffs_trace(YAFFS_TRACE_SCAN,
  16248. + "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
  16249. + dev->internal_start_block, dev->internal_end_block);
  16250. +
  16251. + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  16252. +
  16253. + block_index =
  16254. + kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
  16255. +
  16256. + if (!block_index) {
  16257. + block_index =
  16258. + vmalloc(n_blocks * sizeof(struct yaffs_block_index));
  16259. + alt_block_index = 1;
  16260. + }
  16261. +
  16262. + if (!block_index) {
  16263. + yaffs_trace(YAFFS_TRACE_SCAN,
  16264. + "yaffs2_scan_backwards() could not allocate block index!"
  16265. + );
  16266. + return YAFFS_FAIL;
  16267. + }
  16268. +
  16269. + dev->blocks_in_checkpt = 0;
  16270. +
  16271. + chunk_data = yaffs_get_temp_buffer(dev);
  16272. +
  16273. + /* Scan all the blocks to determine their state */
  16274. + bi = dev->block_info;
  16275. + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  16276. + blk++) {
  16277. + yaffs_clear_chunk_bits(dev, blk);
  16278. + bi->pages_in_use = 0;
  16279. + bi->soft_del_pages = 0;
  16280. +
  16281. + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  16282. +
  16283. + bi->block_state = state;
  16284. + bi->seq_number = seq_number;
  16285. +
  16286. + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  16287. + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  16288. + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  16289. + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  16290. +
  16291. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  16292. + "Block scanning block %d state %d seq %d",
  16293. + blk, bi->block_state, seq_number);
  16294. +
  16295. + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  16296. + dev->blocks_in_checkpt++;
  16297. +
  16298. + } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
  16299. + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  16300. + "block %d is bad", blk);
  16301. + } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  16302. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  16303. + dev->n_erased_blocks++;
  16304. + dev->n_free_chunks += dev->param.chunks_per_block;
  16305. + } else if (bi->block_state ==
  16306. + YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  16307. + /* Determine the highest sequence number */
  16308. + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  16309. + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  16310. + block_index[n_to_scan].seq = seq_number;
  16311. + block_index[n_to_scan].block = blk;
  16312. + n_to_scan++;
  16313. + if (seq_number >= dev->seq_number)
  16314. + dev->seq_number = seq_number;
  16315. + } else {
  16316. + /* TODO: Nasty sequence number! */
  16317. + yaffs_trace(YAFFS_TRACE_SCAN,
  16318. + "Block scanning block %d has bad sequence number %d",
  16319. + blk, seq_number);
  16320. + }
  16321. + }
  16322. + bi++;
  16323. + }
  16324. +
  16325. + yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
  16326. +
  16327. + cond_resched();
  16328. +
  16329. + /* Sort the blocks by sequence number */
  16330. + sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  16331. + yaffs2_ybicmp, NULL);
  16332. +
  16333. + cond_resched();
  16334. +
  16335. + yaffs_trace(YAFFS_TRACE_SCAN, "...done");
  16336. +
  16337. + /* Now scan the blocks looking at the data. */
  16338. + start_iter = 0;
  16339. + end_iter = n_to_scan - 1;
  16340. + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
  16341. +
  16342. + /* For each block.... backwards */
  16343. + for (block_iter = end_iter;
  16344. + !alloc_failed && block_iter >= start_iter;
  16345. + block_iter--) {
  16346. + /* Cooperative multitasking! This loop can run for so
  16347. + long that watchdog timers expire. */
  16348. + cond_resched();
  16349. +
  16350. + /* get the block to scan in the correct order */
  16351. + blk = block_index[block_iter].block;
  16352. + bi = yaffs_get_block_info(dev, blk);
  16353. + deleted = 0;
  16354. +
  16355. + summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
  16356. +
  16357. + /* For each chunk in each block that needs scanning.... */
  16358. + found_chunks = 0;
  16359. + if (summary_available)
  16360. + c = dev->chunks_per_summary - 1;
  16361. + else
  16362. + c = dev->param.chunks_per_block - 1;
  16363. +
  16364. + for (/* c is already initialised */;
  16365. + !alloc_failed && c >= 0 &&
  16366. + (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  16367. + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
  16368. + c--) {
  16369. + /* Scan backwards...
  16370. + * Read the tags and decide what to do
  16371. + */
  16372. + if (yaffs2_scan_chunk(dev, bi, blk, c,
  16373. + &found_chunks, chunk_data,
  16374. + &hard_list, summary_available) ==
  16375. + YAFFS_FAIL)
  16376. + alloc_failed = 1;
  16377. + }
  16378. +
  16379. + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  16380. + /* If we got this far while scanning, then the block
  16381. + * is fully allocated. */
  16382. + bi->block_state = YAFFS_BLOCK_STATE_FULL;
  16383. + }
  16384. +
  16385. + /* Now let's see if it was dirty */
  16386. + if (bi->pages_in_use == 0 &&
  16387. + !bi->has_shrink_hdr &&
  16388. + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  16389. + yaffs_block_became_dirty(dev, blk);
  16390. + }
  16391. + }
  16392. +
  16393. + yaffs_skip_rest_of_block(dev);
  16394. +
  16395. + if (alt_block_index)
  16396. + vfree(block_index);
  16397. + else
  16398. + kfree(block_index);
  16399. +
  16400. + /* Ok, we've done all the scanning.
  16401. + * Fix up the hard link chains.
  16402. + * We have scanned all the objects, now it's time to add these
  16403. + * hardlinks.
  16404. + */
  16405. + yaffs_link_fixup(dev, &hard_list);
  16406. +
  16407. + yaffs_release_temp_buffer(dev, chunk_data);
  16408. +
  16409. + if (alloc_failed)
  16410. + return YAFFS_FAIL;
  16411. +
  16412. + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
  16413. +
  16414. + return YAFFS_OK;
  16415. +}
  16416. diff -Nur linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs2.h linux-3.14.4/fs/yaffs2/yaffs_yaffs2.h
  16417. --- linux-3.14.4.orig/fs/yaffs2/yaffs_yaffs2.h 1970-01-01 01:00:00.000000000 +0100
  16418. +++ linux-3.14.4/fs/yaffs2/yaffs_yaffs2.h 2014-05-17 02:22:30.000000000 +0200
  16419. @@ -0,0 +1,39 @@
  16420. +/*
  16421. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16422. + *
  16423. + * Copyright (C) 2002-2011 Aleph One Ltd.
  16424. + * for Toby Churchill Ltd and Brightstar Engineering
  16425. + *
  16426. + * Created by Charles Manning <charles@aleph1.co.uk>
  16427. + *
  16428. + * This program is free software; you can redistribute it and/or modify
  16429. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16430. + * published by the Free Software Foundation.
  16431. + *
  16432. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16433. + */
  16434. +
  16435. +#ifndef __YAFFS_YAFFS2_H__
  16436. +#define __YAFFS_YAFFS2_H__
  16437. +
  16438. +#include "yaffs_guts.h"
  16439. +
  16440. +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
  16441. +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
  16442. +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  16443. + struct yaffs_block_info *bi);
  16444. +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  16445. + struct yaffs_block_info *bi);
  16446. +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
  16447. +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
  16448. +int yaffs2_checkpt_required(struct yaffs_dev *dev);
  16449. +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
  16450. +
  16451. +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
  16452. +int yaffs2_checkpt_save(struct yaffs_dev *dev);
  16453. +int yaffs2_checkpt_restore(struct yaffs_dev *dev);
  16454. +
  16455. +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
  16456. +int yaffs2_scan_backwards(struct yaffs_dev *dev);
  16457. +
  16458. +#endif
  16459. diff -Nur linux-3.14.4.orig/fs/yaffs2/yportenv.h linux-3.14.4/fs/yaffs2/yportenv.h
  16460. --- linux-3.14.4.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
  16461. +++ linux-3.14.4/fs/yaffs2/yportenv.h 2014-05-17 02:22:30.000000000 +0200
  16462. @@ -0,0 +1,85 @@
  16463. +/*
  16464. + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  16465. + *
  16466. + * Copyright (C) 2002-2011 Aleph One Ltd.
  16467. + * for Toby Churchill Ltd and Brightstar Engineering
  16468. + *
  16469. + * Created by Charles Manning <charles@aleph1.co.uk>
  16470. + *
  16471. + * This program is free software; you can redistribute it and/or modify
  16472. + * it under the terms of the GNU Lesser General Public License version 2.1 as
  16473. + * published by the Free Software Foundation.
  16474. + *
  16475. + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  16476. + */
  16477. +
  16478. +#ifndef __YPORTENV_H__
  16479. +#define __YPORTENV_H__
  16480. +
  16481. +/*
  16482. + * Define the MTD version in terms of Linux Kernel versions
  16483. + * This allows yaffs to be used independantly of the kernel
  16484. + * as well as with it.
  16485. + */
  16486. +
  16487. +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
  16488. +
  16489. +#ifdef YAFFS_OUT_OF_TREE
  16490. +#include "moduleconfig.h"
  16491. +#endif
  16492. +
  16493. +#include <linux/version.h>
  16494. +#define MTD_VERSION_CODE LINUX_VERSION_CODE
  16495. +
  16496. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
  16497. +#include <linux/config.h>
  16498. +#endif
  16499. +#include <linux/version.h>
  16500. +#include <linux/kernel.h>
  16501. +#include <linux/mm.h>
  16502. +#include <linux/sched.h>
  16503. +#include <linux/string.h>
  16504. +#include <linux/slab.h>
  16505. +#include <linux/vmalloc.h>
  16506. +#include <linux/xattr.h>
  16507. +#include <linux/list.h>
  16508. +#include <linux/types.h>
  16509. +#include <linux/fs.h>
  16510. +#include <linux/stat.h>
  16511. +#include <linux/sort.h>
  16512. +#include <linux/bitops.h>
  16513. +
  16514. +/* These type wrappings are used to support Unicode names in WinCE. */
  16515. +#define YCHAR char
  16516. +#define YUCHAR unsigned char
  16517. +#define _Y(x) x
  16518. +
  16519. +#define YAFFS_LOSTNFOUND_NAME "lost+found"
  16520. +#define YAFFS_LOSTNFOUND_PREFIX "obj"
  16521. +
  16522. +
  16523. +#define YAFFS_ROOT_MODE 0755
  16524. +#define YAFFS_LOSTNFOUND_MODE 0700
  16525. +
  16526. +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
  16527. +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
  16528. +#define Y_TIME_CONVERT(x) (x).tv_sec
  16529. +#else
  16530. +#define Y_CURRENT_TIME CURRENT_TIME
  16531. +#define Y_TIME_CONVERT(x) (x)
  16532. +#endif
  16533. +
  16534. +#define compile_time_assertion(assertion) \
  16535. + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
  16536. +
  16537. +
  16538. +#define yaffs_printf(msk, fmt, ...) \
  16539. + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__)
  16540. +
  16541. +#define yaffs_trace(msk, fmt, ...) do { \
  16542. + if (yaffs_trace_mask & (msk)) \
  16543. + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \
  16544. +} while (0)
  16545. +
  16546. +
  16547. +#endif