yaffs_guts.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yportenv.h"
  14. #include "yaffs_trace.h"
  15. #include "yaffs_guts.h"
  16. #include "yaffs_tagsvalidity.h"
  17. #include "yaffs_getblockinfo.h"
  18. #include "yaffs_tagscompat.h"
  19. #include "yaffs_nand.h"
  20. #include "yaffs_yaffs1.h"
  21. #include "yaffs_yaffs2.h"
  22. #include "yaffs_bitmap.h"
  23. #include "yaffs_verify.h"
  24. #include "yaffs_nand.h"
  25. #include "yaffs_packedtags2.h"
  26. #include "yaffs_nameval.h"
  27. #include "yaffs_allocator.h"
  28. #include "yaffs_attribs.h"
  29. /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
  30. #define YAFFS_GC_GOOD_ENOUGH 2
  31. #define YAFFS_GC_PASSIVE_THRESHOLD 4
  32. #include "yaffs_ecc.h"
  33. /* Forward declarations */
  34. static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  35. const u8 * buffer, int n_bytes, int use_reserve);
  36. /* Function to calculate chunk and offset */
  37. static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  38. int *chunk_out, u32 * offset_out)
  39. {
  40. int chunk;
  41. u32 offset;
  42. chunk = (u32) (addr >> dev->chunk_shift);
  43. if (dev->chunk_div == 1) {
  44. /* easy power of 2 case */
  45. offset = (u32) (addr & dev->chunk_mask);
  46. } else {
  47. /* Non power-of-2 case */
  48. loff_t chunk_base;
  49. chunk /= dev->chunk_div;
  50. chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  51. offset = (u32) (addr - chunk_base);
  52. }
  53. *chunk_out = chunk;
  54. *offset_out = offset;
  55. }
  56. /* Function to return the number of shifts for a power of 2 greater than or
  57. * equal to the given number
  58. * Note we don't try to cater for all possible numbers and this does not have to
  59. * be hellishly efficient.
  60. */
  61. static u32 calc_shifts_ceiling(u32 x)
  62. {
  63. int extra_bits;
  64. int shifts;
  65. shifts = extra_bits = 0;
  66. while (x > 1) {
  67. if (x & 1)
  68. extra_bits++;
  69. x >>= 1;
  70. shifts++;
  71. }
  72. if (extra_bits)
  73. shifts++;
  74. return shifts;
  75. }
  76. /* Function to return the number of shifts to get a 1 in bit 0
  77. */
  78. static u32 calc_shifts(u32 x)
  79. {
  80. u32 shifts;
  81. shifts = 0;
  82. if (!x)
  83. return 0;
  84. while (!(x & 1)) {
  85. x >>= 1;
  86. shifts++;
  87. }
  88. return shifts;
  89. }
  90. /*
  91. * Temporary buffer manipulations.
  92. */
  93. static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  94. {
  95. int i;
  96. u8 *buf = (u8 *) 1;
  97. memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  98. for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  99. dev->temp_buffer[i].line = 0; /* not in use */
  100. dev->temp_buffer[i].buffer = buf =
  101. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  102. }
  103. return buf ? YAFFS_OK : YAFFS_FAIL;
  104. }
  105. u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no)
  106. {
  107. int i, j;
  108. dev->temp_in_use++;
  109. if (dev->temp_in_use > dev->max_temp)
  110. dev->max_temp = dev->temp_in_use;
  111. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  112. if (dev->temp_buffer[i].line == 0) {
  113. dev->temp_buffer[i].line = line_no;
  114. if ((i + 1) > dev->max_temp) {
  115. dev->max_temp = i + 1;
  116. for (j = 0; j <= i; j++)
  117. dev->temp_buffer[j].max_line =
  118. dev->temp_buffer[j].line;
  119. }
  120. return dev->temp_buffer[i].buffer;
  121. }
  122. }
  123. yaffs_trace(YAFFS_TRACE_BUFFERS,
  124. "Out of temp buffers at line %d, other held by lines:",
  125. line_no);
  126. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  127. yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line);
  128. /*
  129. * If we got here then we have to allocate an unmanaged one
  130. * This is not good.
  131. */
  132. dev->unmanaged_buffer_allocs++;
  133. return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
  134. }
  135. void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no)
  136. {
  137. int i;
  138. dev->temp_in_use--;
  139. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  140. if (dev->temp_buffer[i].buffer == buffer) {
  141. dev->temp_buffer[i].line = 0;
  142. return;
  143. }
  144. }
  145. if (buffer) {
  146. /* assume it is an unmanaged one. */
  147. yaffs_trace(YAFFS_TRACE_BUFFERS,
  148. "Releasing unmanaged temp buffer in line %d",
  149. line_no);
  150. kfree(buffer);
  151. dev->unmanaged_buffer_deallocs++;
  152. }
  153. }
  154. /*
  155. * Determine if we have a managed buffer.
  156. */
  157. int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer)
  158. {
  159. int i;
  160. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  161. if (dev->temp_buffer[i].buffer == buffer)
  162. return 1;
  163. }
  164. for (i = 0; i < dev->param.n_caches; i++) {
  165. if (dev->cache[i].data == buffer)
  166. return 1;
  167. }
  168. if (buffer == dev->checkpt_buffer)
  169. return 1;
  170. yaffs_trace(YAFFS_TRACE_ALWAYS,
  171. "yaffs: unmaged buffer detected.");
  172. return 0;
  173. }
  174. /*
  175. * Functions for robustisizing TODO
  176. *
  177. */
  178. static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  179. const u8 * data,
  180. const struct yaffs_ext_tags *tags)
  181. {
  182. dev = dev;
  183. nand_chunk = nand_chunk;
  184. data = data;
  185. tags = tags;
  186. }
  187. static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  188. const struct yaffs_ext_tags *tags)
  189. {
  190. dev = dev;
  191. nand_chunk = nand_chunk;
  192. tags = tags;
  193. }
  194. void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  195. struct yaffs_block_info *bi)
  196. {
  197. if (!bi->gc_prioritise) {
  198. bi->gc_prioritise = 1;
  199. dev->has_pending_prioritised_gc = 1;
  200. bi->chunk_error_strikes++;
  201. if (bi->chunk_error_strikes > 3) {
  202. bi->needs_retiring = 1; /* Too many stikes, so retire this */
  203. yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out");
  204. }
  205. }
  206. }
  207. static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  208. int erased_ok)
  209. {
  210. int flash_block = nand_chunk / dev->param.chunks_per_block;
  211. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  212. yaffs_handle_chunk_error(dev, bi);
  213. if (erased_ok) {
  214. /* Was an actual write failure, so mark the block for retirement */
  215. bi->needs_retiring = 1;
  216. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  217. "**>> Block %d needs retiring", flash_block);
  218. }
  219. /* Delete the chunk */
  220. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  221. yaffs_skip_rest_of_block(dev);
  222. }
  223. /*
  224. * Verification code
  225. */
  226. /*
  227. * Simple hash function. Needs to have a reasonable spread
  228. */
  229. static inline int yaffs_hash_fn(int n)
  230. {
  231. n = abs(n);
  232. return n % YAFFS_NOBJECT_BUCKETS;
  233. }
  234. /*
  235. * Access functions to useful fake objects.
  236. * Note that root might have a presence in NAND if permissions are set.
  237. */
  238. struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  239. {
  240. return dev->root_dir;
  241. }
  242. struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  243. {
  244. return dev->lost_n_found;
  245. }
  246. /*
  247. * Erased NAND checking functions
  248. */
  249. int yaffs_check_ff(u8 * buffer, int n_bytes)
  250. {
  251. /* Horrible, slow implementation */
  252. while (n_bytes--) {
  253. if (*buffer != 0xFF)
  254. return 0;
  255. buffer++;
  256. }
  257. return 1;
  258. }
  259. static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  260. {
  261. int retval = YAFFS_OK;
  262. u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
  263. struct yaffs_ext_tags tags;
  264. int result;
  265. result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  266. if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  267. retval = YAFFS_FAIL;
  268. if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
  269. tags.chunk_used) {
  270. yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk);
  271. retval = YAFFS_FAIL;
  272. }
  273. yaffs_release_temp_buffer(dev, data, __LINE__);
  274. return retval;
  275. }
  276. static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  277. int nand_chunk,
  278. const u8 * data,
  279. struct yaffs_ext_tags *tags)
  280. {
  281. int retval = YAFFS_OK;
  282. struct yaffs_ext_tags temp_tags;
  283. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  284. int result;
  285. result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  286. if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  287. temp_tags.obj_id != tags->obj_id ||
  288. temp_tags.chunk_id != tags->chunk_id ||
  289. temp_tags.n_bytes != tags->n_bytes)
  290. retval = YAFFS_FAIL;
  291. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  292. return retval;
  293. }
  294. int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  295. {
  296. int reserved_chunks;
  297. int reserved_blocks = dev->param.n_reserved_blocks;
  298. int checkpt_blocks;
  299. checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  300. reserved_chunks =
  301. ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block);
  302. return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  303. }
  304. static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  305. {
  306. int i;
  307. struct yaffs_block_info *bi;
  308. if (dev->n_erased_blocks < 1) {
  309. /* Hoosterman we've got a problem.
  310. * Can't get space to gc
  311. */
  312. yaffs_trace(YAFFS_TRACE_ERROR,
  313. "yaffs tragedy: no more erased blocks" );
  314. return -1;
  315. }
  316. /* Find an empty block. */
  317. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  318. dev->alloc_block_finder++;
  319. if (dev->alloc_block_finder < dev->internal_start_block
  320. || dev->alloc_block_finder > dev->internal_end_block) {
  321. dev->alloc_block_finder = dev->internal_start_block;
  322. }
  323. bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  324. if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  325. bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  326. dev->seq_number++;
  327. bi->seq_number = dev->seq_number;
  328. dev->n_erased_blocks--;
  329. yaffs_trace(YAFFS_TRACE_ALLOCATE,
  330. "Allocated block %d, seq %d, %d left" ,
  331. dev->alloc_block_finder, dev->seq_number,
  332. dev->n_erased_blocks);
  333. return dev->alloc_block_finder;
  334. }
  335. }
  336. yaffs_trace(YAFFS_TRACE_ALWAYS,
  337. "yaffs tragedy: no more erased blocks, but there should have been %d",
  338. dev->n_erased_blocks);
  339. return -1;
  340. }
  341. static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  342. struct yaffs_block_info **block_ptr)
  343. {
  344. int ret_val;
  345. struct yaffs_block_info *bi;
  346. if (dev->alloc_block < 0) {
  347. /* Get next block to allocate off */
  348. dev->alloc_block = yaffs_find_alloc_block(dev);
  349. dev->alloc_page = 0;
  350. }
  351. if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  352. /* Not enough space to allocate unless we're allowed to use the reserve. */
  353. return -1;
  354. }
  355. if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  356. && dev->alloc_page == 0)
  357. yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
  358. /* Next page please.... */
  359. if (dev->alloc_block >= 0) {
  360. bi = yaffs_get_block_info(dev, dev->alloc_block);
  361. ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  362. dev->alloc_page;
  363. bi->pages_in_use++;
  364. yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  365. dev->alloc_page++;
  366. dev->n_free_chunks--;
  367. /* If the block is full set the state to full */
  368. if (dev->alloc_page >= dev->param.chunks_per_block) {
  369. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  370. dev->alloc_block = -1;
  371. }
  372. if (block_ptr)
  373. *block_ptr = bi;
  374. return ret_val;
  375. }
  376. yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" );
  377. return -1;
  378. }
  379. static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  380. {
  381. int n;
  382. n = dev->n_erased_blocks * dev->param.chunks_per_block;
  383. if (dev->alloc_block > 0)
  384. n += (dev->param.chunks_per_block - dev->alloc_page);
  385. return n;
  386. }
  387. /*
  388. * yaffs_skip_rest_of_block() skips over the rest of the allocation block
  389. * if we don't want to write to it.
  390. */
  391. void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  392. {
  393. if (dev->alloc_block > 0) {
  394. struct yaffs_block_info *bi =
  395. yaffs_get_block_info(dev, dev->alloc_block);
  396. if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  397. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  398. dev->alloc_block = -1;
  399. }
  400. }
  401. }
  402. static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  403. const u8 * data,
  404. struct yaffs_ext_tags *tags, int use_reserver)
  405. {
  406. int attempts = 0;
  407. int write_ok = 0;
  408. int chunk;
  409. yaffs2_checkpt_invalidate(dev);
  410. do {
  411. struct yaffs_block_info *bi = 0;
  412. int erased_ok = 0;
  413. chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  414. if (chunk < 0) {
  415. /* no space */
  416. break;
  417. }
  418. /* First check this chunk is erased, if it needs
  419. * checking. The checking policy (unless forced
  420. * always on) is as follows:
  421. *
  422. * Check the first page we try to write in a block.
  423. * If the check passes then we don't need to check any
  424. * more. If the check fails, we check again...
  425. * If the block has been erased, we don't need to check.
  426. *
  427. * However, if the block has been prioritised for gc,
  428. * then we think there might be something odd about
  429. * this block and stop using it.
  430. *
  431. * Rationale: We should only ever see chunks that have
  432. * not been erased if there was a partially written
  433. * chunk due to power loss. This checking policy should
  434. * catch that case with very few checks and thus save a
  435. * lot of checks that are most likely not needed.
  436. *
  437. * Mods to the above
  438. * If an erase check fails or the write fails we skip the
  439. * rest of the block.
  440. */
  441. /* let's give it a try */
  442. attempts++;
  443. if (dev->param.always_check_erased)
  444. bi->skip_erased_check = 0;
  445. if (!bi->skip_erased_check) {
  446. erased_ok = yaffs_check_chunk_erased(dev, chunk);
  447. if (erased_ok != YAFFS_OK) {
  448. yaffs_trace(YAFFS_TRACE_ERROR,
  449. "**>> yaffs chunk %d was not erased",
  450. chunk);
  451. /* If not erased, delete this one,
  452. * skip rest of block and
  453. * try another chunk */
  454. yaffs_chunk_del(dev, chunk, 1, __LINE__);
  455. yaffs_skip_rest_of_block(dev);
  456. continue;
  457. }
  458. }
  459. write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  460. if (!bi->skip_erased_check)
  461. write_ok =
  462. yaffs_verify_chunk_written(dev, chunk, data, tags);
  463. if (write_ok != YAFFS_OK) {
  464. /* Clean up aborted write, skip to next block and
  465. * try another chunk */
  466. yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  467. continue;
  468. }
  469. bi->skip_erased_check = 1;
  470. /* Copy the data into the robustification buffer */
  471. yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  472. } while (write_ok != YAFFS_OK &&
  473. (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  474. if (!write_ok)
  475. chunk = -1;
  476. if (attempts > 1) {
  477. yaffs_trace(YAFFS_TRACE_ERROR,
  478. "**>> yaffs write required %d attempts",
  479. attempts);
  480. dev->n_retired_writes += (attempts - 1);
  481. }
  482. return chunk;
  483. }
  484. /*
  485. * Block retiring for handling a broken block.
  486. */
  487. static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  488. {
  489. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  490. yaffs2_checkpt_invalidate(dev);
  491. yaffs2_clear_oldest_dirty_seq(dev, bi);
  492. if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  493. if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  494. yaffs_trace(YAFFS_TRACE_ALWAYS,
  495. "yaffs: Failed to mark bad and erase block %d",
  496. flash_block);
  497. } else {
  498. struct yaffs_ext_tags tags;
  499. int chunk_id =
  500. flash_block * dev->param.chunks_per_block;
  501. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  502. memset(buffer, 0xff, dev->data_bytes_per_chunk);
  503. yaffs_init_tags(&tags);
  504. tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  505. if (dev->param.write_chunk_tags_fn(dev, chunk_id -
  506. dev->chunk_offset,
  507. buffer,
  508. &tags) != YAFFS_OK)
  509. yaffs_trace(YAFFS_TRACE_ALWAYS,
  510. "yaffs: Failed to write bad block marker to block %d",
  511. flash_block);
  512. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  513. }
  514. }
  515. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  516. bi->gc_prioritise = 0;
  517. bi->needs_retiring = 0;
  518. dev->n_retired_blocks++;
  519. }
  520. /*---------------- Name handling functions ------------*/
  521. static u16 yaffs_calc_name_sum(const YCHAR * name)
  522. {
  523. u16 sum = 0;
  524. u16 i = 1;
  525. const YUCHAR *bname = (const YUCHAR *)name;
  526. if (bname) {
  527. while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) {
  528. /* 0x1f mask is case insensitive */
  529. sum += ((*bname) & 0x1f) * i;
  530. i++;
  531. bname++;
  532. }
  533. }
  534. return sum;
  535. }
  536. void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  537. {
  538. #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  539. memset(obj->short_name, 0, sizeof(obj->short_name));
  540. if (name &&
  541. strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  542. YAFFS_SHORT_NAME_LENGTH)
  543. strcpy(obj->short_name, name);
  544. else
  545. obj->short_name[0] = _Y('\0');
  546. #endif
  547. obj->sum = yaffs_calc_name_sum(name);
  548. }
  549. void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  550. const struct yaffs_obj_hdr *oh)
  551. {
  552. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  553. YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  554. memset(tmp_name, 0, sizeof(tmp_name));
  555. yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  556. YAFFS_MAX_NAME_LENGTH + 1);
  557. yaffs_set_obj_name(obj, tmp_name);
  558. #else
  559. yaffs_set_obj_name(obj, oh->name);
  560. #endif
  561. }
  562. /*-------------------- TNODES -------------------
  563. * List of spare tnodes
  564. * The list is hooked together using the first pointer
  565. * in the tnode.
  566. */
  567. struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  568. {
  569. struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  570. if (tn) {
  571. memset(tn, 0, dev->tnode_size);
  572. dev->n_tnodes++;
  573. }
  574. dev->checkpoint_blocks_required = 0; /* force recalculation */
  575. return tn;
  576. }
  577. /* FreeTnode frees up a tnode and puts it back on the free list */
  578. static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  579. {
  580. yaffs_free_raw_tnode(dev, tn);
  581. dev->n_tnodes--;
  582. dev->checkpoint_blocks_required = 0; /* force recalculation */
  583. }
  584. static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  585. {
  586. yaffs_deinit_raw_tnodes_and_objs(dev);
  587. dev->n_obj = 0;
  588. dev->n_tnodes = 0;
  589. }
  590. void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  591. unsigned pos, unsigned val)
  592. {
  593. u32 *map = (u32 *) tn;
  594. u32 bit_in_map;
  595. u32 bit_in_word;
  596. u32 word_in_map;
  597. u32 mask;
  598. pos &= YAFFS_TNODES_LEVEL0_MASK;
  599. val >>= dev->chunk_grp_bits;
  600. bit_in_map = pos * dev->tnode_width;
  601. word_in_map = bit_in_map / 32;
  602. bit_in_word = bit_in_map & (32 - 1);
  603. mask = dev->tnode_mask << bit_in_word;
  604. map[word_in_map] &= ~mask;
  605. map[word_in_map] |= (mask & (val << bit_in_word));
  606. if (dev->tnode_width > (32 - bit_in_word)) {
  607. bit_in_word = (32 - bit_in_word);
  608. word_in_map++;;
  609. mask =
  610. dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word);
  611. map[word_in_map] &= ~mask;
  612. map[word_in_map] |= (mask & (val >> bit_in_word));
  613. }
  614. }
  615. u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  616. unsigned pos)
  617. {
  618. u32 *map = (u32 *) tn;
  619. u32 bit_in_map;
  620. u32 bit_in_word;
  621. u32 word_in_map;
  622. u32 val;
  623. pos &= YAFFS_TNODES_LEVEL0_MASK;
  624. bit_in_map = pos * dev->tnode_width;
  625. word_in_map = bit_in_map / 32;
  626. bit_in_word = bit_in_map & (32 - 1);
  627. val = map[word_in_map] >> bit_in_word;
  628. if (dev->tnode_width > (32 - bit_in_word)) {
  629. bit_in_word = (32 - bit_in_word);
  630. word_in_map++;;
  631. val |= (map[word_in_map] << bit_in_word);
  632. }
  633. val &= dev->tnode_mask;
  634. val <<= dev->chunk_grp_bits;
  635. return val;
  636. }
  637. /* ------------------- End of individual tnode manipulation -----------------*/
  638. /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  639. * The look up tree is represented by the top tnode and the number of top_level
  640. * in the tree. 0 means only the level 0 tnode is in the tree.
  641. */
  642. /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  643. struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  644. struct yaffs_file_var *file_struct,
  645. u32 chunk_id)
  646. {
  647. struct yaffs_tnode *tn = file_struct->top;
  648. u32 i;
  649. int required_depth;
  650. int level = file_struct->top_level;
  651. dev = dev;
  652. /* Check sane level and chunk Id */
  653. if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  654. return NULL;
  655. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  656. return NULL;
  657. /* First check we're tall enough (ie enough top_level) */
  658. i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  659. required_depth = 0;
  660. while (i) {
  661. i >>= YAFFS_TNODES_INTERNAL_BITS;
  662. required_depth++;
  663. }
  664. if (required_depth > file_struct->top_level)
  665. return NULL; /* Not tall enough, so we can't find it */
  666. /* Traverse down to level 0 */
  667. while (level > 0 && tn) {
  668. tn = tn->internal[(chunk_id >>
  669. (YAFFS_TNODES_LEVEL0_BITS +
  670. (level - 1) *
  671. YAFFS_TNODES_INTERNAL_BITS)) &
  672. YAFFS_TNODES_INTERNAL_MASK];
  673. level--;
  674. }
  675. return tn;
  676. }
  677. /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
  678. * This happens in two steps:
  679. * 1. If the tree isn't tall enough, then make it taller.
  680. * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  681. *
  682. * Used when modifying the tree.
  683. *
  684. * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
  685. * be plugged into the ttree.
  686. */
  687. struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  688. struct yaffs_file_var *file_struct,
  689. u32 chunk_id,
  690. struct yaffs_tnode *passed_tn)
  691. {
  692. int required_depth;
  693. int i;
  694. int l;
  695. struct yaffs_tnode *tn;
  696. u32 x;
  697. /* Check sane level and page Id */
  698. if (file_struct->top_level < 0
  699. || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  700. return NULL;
  701. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  702. return NULL;
  703. /* First check we're tall enough (ie enough top_level) */
  704. x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  705. required_depth = 0;
  706. while (x) {
  707. x >>= YAFFS_TNODES_INTERNAL_BITS;
  708. required_depth++;
  709. }
  710. if (required_depth > file_struct->top_level) {
  711. /* Not tall enough, gotta make the tree taller */
  712. for (i = file_struct->top_level; i < required_depth; i++) {
  713. tn = yaffs_get_tnode(dev);
  714. if (tn) {
  715. tn->internal[0] = file_struct->top;
  716. file_struct->top = tn;
  717. file_struct->top_level++;
  718. } else {
  719. yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes");
  720. return NULL;
  721. }
  722. }
  723. }
  724. /* Traverse down to level 0, adding anything we need */
  725. l = file_struct->top_level;
  726. tn = file_struct->top;
  727. if (l > 0) {
  728. while (l > 0 && tn) {
  729. x = (chunk_id >>
  730. (YAFFS_TNODES_LEVEL0_BITS +
  731. (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  732. YAFFS_TNODES_INTERNAL_MASK;
  733. if ((l > 1) && !tn->internal[x]) {
  734. /* Add missing non-level-zero tnode */
  735. tn->internal[x] = yaffs_get_tnode(dev);
  736. if (!tn->internal[x])
  737. return NULL;
  738. } else if (l == 1) {
  739. /* Looking from level 1 at level 0 */
  740. if (passed_tn) {
  741. /* If we already have one, then release it. */
  742. if (tn->internal[x])
  743. yaffs_free_tnode(dev,
  744. tn->
  745. internal[x]);
  746. tn->internal[x] = passed_tn;
  747. } else if (!tn->internal[x]) {
  748. /* Don't have one, none passed in */
  749. tn->internal[x] = yaffs_get_tnode(dev);
  750. if (!tn->internal[x])
  751. return NULL;
  752. }
  753. }
  754. tn = tn->internal[x];
  755. l--;
  756. }
  757. } else {
  758. /* top is level 0 */
  759. if (passed_tn) {
  760. memcpy(tn, passed_tn,
  761. (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  762. yaffs_free_tnode(dev, passed_tn);
  763. }
  764. }
  765. return tn;
  766. }
  767. static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  768. int chunk_obj)
  769. {
  770. return (tags->chunk_id == chunk_obj &&
  771. tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
  772. }
  773. static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  774. struct yaffs_ext_tags *tags, int obj_id,
  775. int inode_chunk)
  776. {
  777. int j;
  778. for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  779. if (yaffs_check_chunk_bit
  780. (dev, the_chunk / dev->param.chunks_per_block,
  781. the_chunk % dev->param.chunks_per_block)) {
  782. if (dev->chunk_grp_size == 1)
  783. return the_chunk;
  784. else {
  785. yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  786. tags);
  787. if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
  788. /* found it; */
  789. return the_chunk;
  790. }
  791. }
  792. }
  793. the_chunk++;
  794. }
  795. return -1;
  796. }
  797. static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  798. struct yaffs_ext_tags *tags)
  799. {
  800. /*Get the Tnode, then get the level 0 offset chunk offset */
  801. struct yaffs_tnode *tn;
  802. int the_chunk = -1;
  803. struct yaffs_ext_tags local_tags;
  804. int ret_val = -1;
  805. struct yaffs_dev *dev = in->my_dev;
  806. if (!tags) {
  807. /* Passed a NULL, so use our own tags space */
  808. tags = &local_tags;
  809. }
  810. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  811. if (tn) {
  812. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  813. ret_val =
  814. yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  815. inode_chunk);
  816. }
  817. return ret_val;
  818. }
  819. static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  820. struct yaffs_ext_tags *tags)
  821. {
  822. /* Get the Tnode, then get the level 0 offset chunk offset */
  823. struct yaffs_tnode *tn;
  824. int the_chunk = -1;
  825. struct yaffs_ext_tags local_tags;
  826. struct yaffs_dev *dev = in->my_dev;
  827. int ret_val = -1;
  828. if (!tags) {
  829. /* Passed a NULL, so use our own tags space */
  830. tags = &local_tags;
  831. }
  832. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  833. if (tn) {
  834. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  835. ret_val =
  836. yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  837. inode_chunk);
  838. /* Delete the entry in the filestructure (if found) */
  839. if (ret_val != -1)
  840. yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  841. }
  842. return ret_val;
  843. }
  844. int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  845. int nand_chunk, int in_scan)
  846. {
  847. /* NB in_scan is zero unless scanning.
  848. * For forward scanning, in_scan is > 0;
  849. * for backward scanning in_scan is < 0
  850. *
  851. * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
  852. */
  853. struct yaffs_tnode *tn;
  854. struct yaffs_dev *dev = in->my_dev;
  855. int existing_cunk;
  856. struct yaffs_ext_tags existing_tags;
  857. struct yaffs_ext_tags new_tags;
  858. unsigned existing_serial, new_serial;
  859. if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  860. /* Just ignore an attempt at putting a chunk into a non-file during scanning
  861. * If it is not during Scanning then something went wrong!
  862. */
  863. if (!in_scan) {
  864. yaffs_trace(YAFFS_TRACE_ERROR,
  865. "yaffs tragedy:attempt to put data chunk into a non-file"
  866. );
  867. YBUG();
  868. }
  869. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  870. return YAFFS_OK;
  871. }
  872. tn = yaffs_add_find_tnode_0(dev,
  873. &in->variant.file_variant,
  874. inode_chunk, NULL);
  875. if (!tn)
  876. return YAFFS_FAIL;
  877. if (!nand_chunk)
  878. /* Dummy insert, bail now */
  879. return YAFFS_OK;
  880. existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  881. if (in_scan != 0) {
  882. /* If we're scanning then we need to test for duplicates
  883. * NB This does not need to be efficient since it should only ever
  884. * happen when the power fails during a write, then only one
  885. * chunk should ever be affected.
  886. *
  887. * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
  888. * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
  889. */
  890. if (existing_cunk > 0) {
  891. /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
  892. * thus we have to do a FindChunkInFile to get the real chunk id.
  893. *
  894. * We have a duplicate now we need to decide which one to use:
  895. *
  896. * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
  897. * Forward scanning YAFFS2: The new one is what we use, dump the old one.
  898. * YAFFS1: Get both sets of tags and compare serial numbers.
  899. */
  900. if (in_scan > 0) {
  901. /* Only do this for forward scanning */
  902. yaffs_rd_chunk_tags_nand(dev,
  903. nand_chunk,
  904. NULL, &new_tags);
  905. /* Do a proper find */
  906. existing_cunk =
  907. yaffs_find_chunk_in_file(in, inode_chunk,
  908. &existing_tags);
  909. }
  910. if (existing_cunk <= 0) {
  911. /*Hoosterman - how did this happen? */
  912. yaffs_trace(YAFFS_TRACE_ERROR,
  913. "yaffs tragedy: existing chunk < 0 in scan"
  914. );
  915. }
  916. /* NB The deleted flags should be false, otherwise the chunks will
  917. * not be loaded during a scan
  918. */
  919. if (in_scan > 0) {
  920. new_serial = new_tags.serial_number;
  921. existing_serial = existing_tags.serial_number;
  922. }
  923. if ((in_scan > 0) &&
  924. (existing_cunk <= 0 ||
  925. ((existing_serial + 1) & 3) == new_serial)) {
  926. /* Forward scanning.
  927. * Use new
  928. * Delete the old one and drop through to update the tnode
  929. */
  930. yaffs_chunk_del(dev, existing_cunk, 1,
  931. __LINE__);
  932. } else {
  933. /* Backward scanning or we want to use the existing one
  934. * Use existing.
  935. * Delete the new one and return early so that the tnode isn't changed
  936. */
  937. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  938. return YAFFS_OK;
  939. }
  940. }
  941. }
  942. if (existing_cunk == 0)
  943. in->n_data_chunks++;
  944. yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  945. return YAFFS_OK;
  946. }
  947. static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  948. {
  949. struct yaffs_block_info *the_block;
  950. unsigned block_no;
  951. yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
  952. block_no = chunk / dev->param.chunks_per_block;
  953. the_block = yaffs_get_block_info(dev, block_no);
  954. if (the_block) {
  955. the_block->soft_del_pages++;
  956. dev->n_free_chunks++;
  957. yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  958. }
  959. }
  960. /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
  961. * All soft deleting does is increment the block's softdelete count and pulls the chunk out
  962. * of the tnode.
  963. * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
  964. */
  965. static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  966. u32 level, int chunk_offset)
  967. {
  968. int i;
  969. int the_chunk;
  970. int all_done = 1;
  971. struct yaffs_dev *dev = in->my_dev;
  972. if (tn) {
  973. if (level > 0) {
  974. for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0;
  975. i--) {
  976. if (tn->internal[i]) {
  977. all_done =
  978. yaffs_soft_del_worker(in,
  979. tn->internal
  980. [i],
  981. level - 1,
  982. (chunk_offset
  983. <<
  984. YAFFS_TNODES_INTERNAL_BITS)
  985. + i);
  986. if (all_done) {
  987. yaffs_free_tnode(dev,
  988. tn->internal
  989. [i]);
  990. tn->internal[i] = NULL;
  991. } else {
  992. /* Hoosterman... how could this happen? */
  993. }
  994. }
  995. }
  996. return (all_done) ? 1 : 0;
  997. } else if (level == 0) {
  998. for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  999. the_chunk = yaffs_get_group_base(dev, tn, i);
  1000. if (the_chunk) {
  1001. /* Note this does not find the real chunk, only the chunk group.
  1002. * We make an assumption that a chunk group is not larger than
  1003. * a block.
  1004. */
  1005. yaffs_soft_del_chunk(dev, the_chunk);
  1006. yaffs_load_tnode_0(dev, tn, i, 0);
  1007. }
  1008. }
  1009. return 1;
  1010. }
  1011. }
  1012. return 1;
  1013. }
  1014. static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  1015. {
  1016. struct yaffs_dev *dev = obj->my_dev;
  1017. struct yaffs_obj *parent;
  1018. yaffs_verify_obj_in_dir(obj);
  1019. parent = obj->parent;
  1020. yaffs_verify_dir(parent);
  1021. if (dev && dev->param.remove_obj_fn)
  1022. dev->param.remove_obj_fn(obj);
  1023. list_del_init(&obj->siblings);
  1024. obj->parent = NULL;
  1025. yaffs_verify_dir(parent);
  1026. }
  1027. void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  1028. {
  1029. if (!directory) {
  1030. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1031. "tragedy: Trying to add an object to a null pointer directory"
  1032. );
  1033. YBUG();
  1034. return;
  1035. }
  1036. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  1037. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1038. "tragedy: Trying to add an object to a non-directory"
  1039. );
  1040. YBUG();
  1041. }
  1042. if (obj->siblings.prev == NULL) {
  1043. /* Not initialised */
  1044. YBUG();
  1045. }
  1046. yaffs_verify_dir(directory);
  1047. yaffs_remove_obj_from_dir(obj);
  1048. /* Now add it */
  1049. list_add(&obj->siblings, &directory->variant.dir_variant.children);
  1050. obj->parent = directory;
  1051. if (directory == obj->my_dev->unlinked_dir
  1052. || directory == obj->my_dev->del_dir) {
  1053. obj->unlinked = 1;
  1054. obj->my_dev->n_unlinked_files++;
  1055. obj->rename_allowed = 0;
  1056. }
  1057. yaffs_verify_dir(directory);
  1058. yaffs_verify_obj_in_dir(obj);
  1059. }
  1060. static int yaffs_change_obj_name(struct yaffs_obj *obj,
  1061. struct yaffs_obj *new_dir,
  1062. const YCHAR * new_name, int force, int shadows)
  1063. {
  1064. int unlink_op;
  1065. int del_op;
  1066. struct yaffs_obj *existing_target;
  1067. if (new_dir == NULL)
  1068. new_dir = obj->parent; /* use the old directory */
  1069. if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  1070. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1071. "tragedy: yaffs_change_obj_name: new_dir is not a directory"
  1072. );
  1073. YBUG();
  1074. }
  1075. /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
  1076. if (obj->my_dev->param.is_yaffs2)
  1077. unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  1078. else
  1079. unlink_op = (new_dir == obj->my_dev->unlinked_dir
  1080. && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
  1081. del_op = (new_dir == obj->my_dev->del_dir);
  1082. existing_target = yaffs_find_by_name(new_dir, new_name);
  1083. /* If the object is a file going into the unlinked directory,
  1084. * then it is OK to just stuff it in since duplicate names are allowed.
  1085. * else only proceed if the new name does not exist and if we're putting
  1086. * it into a directory.
  1087. */
  1088. if ((unlink_op ||
  1089. del_op ||
  1090. force ||
  1091. (shadows > 0) ||
  1092. !existing_target) &&
  1093. new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  1094. yaffs_set_obj_name(obj, new_name);
  1095. obj->dirty = 1;
  1096. yaffs_add_obj_to_dir(new_dir, obj);
  1097. if (unlink_op)
  1098. obj->unlinked = 1;
  1099. /* If it is a deletion then we mark it as a shrink for gc purposes. */
  1100. if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >=
  1101. 0)
  1102. return YAFFS_OK;
  1103. }
  1104. return YAFFS_FAIL;
  1105. }
  1106. /*------------------------ Short Operations Cache ----------------------------------------
  1107. * In many situations where there is no high level buffering a lot of
  1108. * reads might be short sequential reads, and a lot of writes may be short
  1109. * sequential writes. eg. scanning/writing a jpeg file.
  1110. * In these cases, a short read/write cache can provide a huge perfomance
  1111. * benefit with dumb-as-a-rock code.
  1112. * In Linux, the page cache provides read buffering and the short op cache
  1113. * provides write buffering.
  1114. *
  1115. * There are a limited number (~10) of cache chunks per device so that we don't
  1116. * need a very intelligent search.
  1117. */
  1118. static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  1119. {
  1120. struct yaffs_dev *dev = obj->my_dev;
  1121. int i;
  1122. struct yaffs_cache *cache;
  1123. int n_caches = obj->my_dev->param.n_caches;
  1124. for (i = 0; i < n_caches; i++) {
  1125. cache = &dev->cache[i];
  1126. if (cache->object == obj && cache->dirty)
  1127. return 1;
  1128. }
  1129. return 0;
  1130. }
  1131. static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  1132. {
  1133. struct yaffs_dev *dev = obj->my_dev;
  1134. int lowest = -99; /* Stop compiler whining. */
  1135. int i;
  1136. struct yaffs_cache *cache;
  1137. int chunk_written = 0;
  1138. int n_caches = obj->my_dev->param.n_caches;
  1139. if (n_caches > 0) {
  1140. do {
  1141. cache = NULL;
  1142. /* Find the dirty cache for this object with the lowest chunk id. */
  1143. for (i = 0; i < n_caches; i++) {
  1144. if (dev->cache[i].object == obj &&
  1145. dev->cache[i].dirty) {
  1146. if (!cache
  1147. || dev->cache[i].chunk_id <
  1148. lowest) {
  1149. cache = &dev->cache[i];
  1150. lowest = cache->chunk_id;
  1151. }
  1152. }
  1153. }
  1154. if (cache && !cache->locked) {
  1155. /* Write it out and free it up */
  1156. chunk_written =
  1157. yaffs_wr_data_obj(cache->object,
  1158. cache->chunk_id,
  1159. cache->data,
  1160. cache->n_bytes, 1);
  1161. cache->dirty = 0;
  1162. cache->object = NULL;
  1163. }
  1164. } while (cache && chunk_written > 0);
  1165. if (cache)
  1166. /* Hoosterman, disk full while writing cache out. */
  1167. yaffs_trace(YAFFS_TRACE_ERROR,
  1168. "yaffs tragedy: no space during cache write");
  1169. }
  1170. }
  1171. /*yaffs_flush_whole_cache(dev)
  1172. *
  1173. *
  1174. */
  1175. void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  1176. {
  1177. struct yaffs_obj *obj;
  1178. int n_caches = dev->param.n_caches;
  1179. int i;
  1180. /* Find a dirty object in the cache and flush it...
  1181. * until there are no further dirty objects.
  1182. */
  1183. do {
  1184. obj = NULL;
  1185. for (i = 0; i < n_caches && !obj; i++) {
  1186. if (dev->cache[i].object && dev->cache[i].dirty)
  1187. obj = dev->cache[i].object;
  1188. }
  1189. if (obj)
  1190. yaffs_flush_file_cache(obj);
  1191. } while (obj);
  1192. }
  1193. /* Grab us a cache chunk for use.
  1194. * First look for an empty one.
  1195. * Then look for the least recently used non-dirty one.
  1196. * Then look for the least recently used dirty one...., flush and look again.
  1197. */
  1198. static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  1199. {
  1200. int i;
  1201. if (dev->param.n_caches > 0) {
  1202. for (i = 0; i < dev->param.n_caches; i++) {
  1203. if (!dev->cache[i].object)
  1204. return &dev->cache[i];
  1205. }
  1206. }
  1207. return NULL;
  1208. }
  1209. static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  1210. {
  1211. struct yaffs_cache *cache;
  1212. struct yaffs_obj *the_obj;
  1213. int usage;
  1214. int i;
  1215. int pushout;
  1216. if (dev->param.n_caches > 0) {
  1217. /* Try find a non-dirty one... */
  1218. cache = yaffs_grab_chunk_worker(dev);
  1219. if (!cache) {
  1220. /* They were all dirty, find the last recently used object and flush
  1221. * its cache, then find again.
  1222. * NB what's here is not very accurate, we actually flush the object
  1223. * the last recently used page.
  1224. */
  1225. /* With locking we can't assume we can use entry zero */
  1226. the_obj = NULL;
  1227. usage = -1;
  1228. cache = NULL;
  1229. pushout = -1;
  1230. for (i = 0; i < dev->param.n_caches; i++) {
  1231. if (dev->cache[i].object &&
  1232. !dev->cache[i].locked &&
  1233. (dev->cache[i].last_use < usage
  1234. || !cache)) {
  1235. usage = dev->cache[i].last_use;
  1236. the_obj = dev->cache[i].object;
  1237. cache = &dev->cache[i];
  1238. pushout = i;
  1239. }
  1240. }
  1241. if (!cache || cache->dirty) {
  1242. /* Flush and try again */
  1243. yaffs_flush_file_cache(the_obj);
  1244. cache = yaffs_grab_chunk_worker(dev);
  1245. }
  1246. }
  1247. return cache;
  1248. } else {
  1249. return NULL;
  1250. }
  1251. }
  1252. /* Find a cached chunk */
  1253. static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  1254. int chunk_id)
  1255. {
  1256. struct yaffs_dev *dev = obj->my_dev;
  1257. int i;
  1258. if (dev->param.n_caches > 0) {
  1259. for (i = 0; i < dev->param.n_caches; i++) {
  1260. if (dev->cache[i].object == obj &&
  1261. dev->cache[i].chunk_id == chunk_id) {
  1262. dev->cache_hits++;
  1263. return &dev->cache[i];
  1264. }
  1265. }
  1266. }
  1267. return NULL;
  1268. }
  1269. /* Mark the chunk for the least recently used algorithym */
  1270. static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  1271. int is_write)
  1272. {
  1273. if (dev->param.n_caches > 0) {
  1274. if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
  1275. /* Reset the cache usages */
  1276. int i;
  1277. for (i = 1; i < dev->param.n_caches; i++)
  1278. dev->cache[i].last_use = 0;
  1279. dev->cache_last_use = 0;
  1280. }
  1281. dev->cache_last_use++;
  1282. cache->last_use = dev->cache_last_use;
  1283. if (is_write)
  1284. cache->dirty = 1;
  1285. }
  1286. }
  1287. /* Invalidate a single cache page.
  1288. * Do this when a whole page gets written,
  1289. * ie the short cache for this page is no longer valid.
  1290. */
  1291. static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  1292. {
  1293. if (object->my_dev->param.n_caches > 0) {
  1294. struct yaffs_cache *cache =
  1295. yaffs_find_chunk_cache(object, chunk_id);
  1296. if (cache)
  1297. cache->object = NULL;
  1298. }
  1299. }
  1300. /* Invalidate all the cache pages associated with this object
  1301. * Do this whenever ther file is deleted or resized.
  1302. */
  1303. static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  1304. {
  1305. int i;
  1306. struct yaffs_dev *dev = in->my_dev;
  1307. if (dev->param.n_caches > 0) {
  1308. /* Invalidate it. */
  1309. for (i = 0; i < dev->param.n_caches; i++) {
  1310. if (dev->cache[i].object == in)
  1311. dev->cache[i].object = NULL;
  1312. }
  1313. }
  1314. }
  1315. static void yaffs_unhash_obj(struct yaffs_obj *obj)
  1316. {
  1317. int bucket;
  1318. struct yaffs_dev *dev = obj->my_dev;
  1319. /* If it is still linked into the bucket list, free from the list */
  1320. if (!list_empty(&obj->hash_link)) {
  1321. list_del_init(&obj->hash_link);
  1322. bucket = yaffs_hash_fn(obj->obj_id);
  1323. dev->obj_bucket[bucket].count--;
  1324. }
  1325. }
  1326. /* FreeObject frees up a Object and puts it back on the free list */
  1327. static void yaffs_free_obj(struct yaffs_obj *obj)
  1328. {
  1329. struct yaffs_dev *dev = obj->my_dev;
  1330. yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
  1331. obj, obj->my_inode);
  1332. if (!obj)
  1333. YBUG();
  1334. if (obj->parent)
  1335. YBUG();
  1336. if (!list_empty(&obj->siblings))
  1337. YBUG();
  1338. if (obj->my_inode) {
  1339. /* We're still hooked up to a cached inode.
  1340. * Don't delete now, but mark for later deletion
  1341. */
  1342. obj->defered_free = 1;
  1343. return;
  1344. }
  1345. yaffs_unhash_obj(obj);
  1346. yaffs_free_raw_obj(dev, obj);
  1347. dev->n_obj--;
  1348. dev->checkpoint_blocks_required = 0; /* force recalculation */
  1349. }
  1350. void yaffs_handle_defered_free(struct yaffs_obj *obj)
  1351. {
  1352. if (obj->defered_free)
  1353. yaffs_free_obj(obj);
  1354. }
  1355. static int yaffs_generic_obj_del(struct yaffs_obj *in)
  1356. {
  1357. /* First off, invalidate the file's data in the cache, without flushing. */
  1358. yaffs_invalidate_whole_cache(in);
  1359. if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
  1360. /* Move to the unlinked directory so we have a record that it was deleted. */
  1361. yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  1362. 0);
  1363. }
  1364. yaffs_remove_obj_from_dir(in);
  1365. yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  1366. in->hdr_chunk = 0;
  1367. yaffs_free_obj(in);
  1368. return YAFFS_OK;
  1369. }
  1370. static void yaffs_soft_del_file(struct yaffs_obj *obj)
  1371. {
  1372. if (obj->deleted &&
  1373. obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
  1374. if (obj->n_data_chunks <= 0) {
  1375. /* Empty file with no duplicate object headers,
  1376. * just delete it immediately */
  1377. yaffs_free_tnode(obj->my_dev,
  1378. obj->variant.file_variant.top);
  1379. obj->variant.file_variant.top = NULL;
  1380. yaffs_trace(YAFFS_TRACE_TRACING,
  1381. "yaffs: Deleting empty file %d",
  1382. obj->obj_id);
  1383. yaffs_generic_obj_del(obj);
  1384. } else {
  1385. yaffs_soft_del_worker(obj,
  1386. obj->variant.file_variant.top,
  1387. obj->variant.
  1388. file_variant.top_level, 0);
  1389. obj->soft_del = 1;
  1390. }
  1391. }
  1392. }
  1393. /* Pruning removes any part of the file structure tree that is beyond the
  1394. * bounds of the file (ie that does not point to chunks).
  1395. *
  1396. * A file should only get pruned when its size is reduced.
  1397. *
  1398. * Before pruning, the chunks must be pulled from the tree and the
  1399. * level 0 tnode entries must be zeroed out.
  1400. * Could also use this for file deletion, but that's probably better handled
  1401. * by a special case.
  1402. *
  1403. * This function is recursive. For levels > 0 the function is called again on
  1404. * any sub-tree. For level == 0 we just check if the sub-tree has data.
  1405. * If there is no data in a subtree then it is pruned.
  1406. */
  1407. static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  1408. struct yaffs_tnode *tn, u32 level,
  1409. int del0)
  1410. {
  1411. int i;
  1412. int has_data;
  1413. if (tn) {
  1414. has_data = 0;
  1415. if (level > 0) {
  1416. for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  1417. if (tn->internal[i]) {
  1418. tn->internal[i] =
  1419. yaffs_prune_worker(dev,
  1420. tn->internal[i],
  1421. level - 1,
  1422. (i ==
  1423. 0) ? del0 : 1);
  1424. }
  1425. if (tn->internal[i])
  1426. has_data++;
  1427. }
  1428. } else {
  1429. int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  1430. u32 *map = (u32 *) tn;
  1431. for (i = 0; !has_data && i < tnode_size_u32; i++) {
  1432. if (map[i])
  1433. has_data++;
  1434. }
  1435. }
  1436. if (has_data == 0 && del0) {
  1437. /* Free and return NULL */
  1438. yaffs_free_tnode(dev, tn);
  1439. tn = NULL;
  1440. }
  1441. }
  1442. return tn;
  1443. }
  1444. static int yaffs_prune_tree(struct yaffs_dev *dev,
  1445. struct yaffs_file_var *file_struct)
  1446. {
  1447. int i;
  1448. int has_data;
  1449. int done = 0;
  1450. struct yaffs_tnode *tn;
  1451. if (file_struct->top_level > 0) {
  1452. file_struct->top =
  1453. yaffs_prune_worker(dev, file_struct->top,
  1454. file_struct->top_level, 0);
  1455. /* Now we have a tree with all the non-zero branches NULL but the height
  1456. * is the same as it was.
  1457. * Let's see if we can trim internal tnodes to shorten the tree.
  1458. * We can do this if only the 0th element in the tnode is in use
  1459. * (ie all the non-zero are NULL)
  1460. */
  1461. while (file_struct->top_level && !done) {
  1462. tn = file_struct->top;
  1463. has_data = 0;
  1464. for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  1465. if (tn->internal[i])
  1466. has_data++;
  1467. }
  1468. if (!has_data) {
  1469. file_struct->top = tn->internal[0];
  1470. file_struct->top_level--;
  1471. yaffs_free_tnode(dev, tn);
  1472. } else {
  1473. done = 1;
  1474. }
  1475. }
  1476. }
  1477. return YAFFS_OK;
  1478. }
  1479. /*-------------------- End of File Structure functions.-------------------*/
  1480. /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
  1481. static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  1482. {
  1483. struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  1484. if (obj) {
  1485. dev->n_obj++;
  1486. /* Now sweeten it up... */
  1487. memset(obj, 0, sizeof(struct yaffs_obj));
  1488. obj->being_created = 1;
  1489. obj->my_dev = dev;
  1490. obj->hdr_chunk = 0;
  1491. obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  1492. INIT_LIST_HEAD(&(obj->hard_links));
  1493. INIT_LIST_HEAD(&(obj->hash_link));
  1494. INIT_LIST_HEAD(&obj->siblings);
  1495. /* Now make the directory sane */
  1496. if (dev->root_dir) {
  1497. obj->parent = dev->root_dir;
  1498. list_add(&(obj->siblings),
  1499. &dev->root_dir->variant.dir_variant.children);
  1500. }
  1501. /* Add it to the lost and found directory.
  1502. * NB Can't put root or lost-n-found in lost-n-found so
  1503. * check if lost-n-found exists first
  1504. */
  1505. if (dev->lost_n_found)
  1506. yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  1507. obj->being_created = 0;
  1508. }
  1509. dev->checkpoint_blocks_required = 0; /* force recalculation */
  1510. return obj;
  1511. }
  1512. static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  1513. {
  1514. int i;
  1515. int l = 999;
  1516. int lowest = 999999;
  1517. /* Search for the shortest list or one that
  1518. * isn't too long.
  1519. */
  1520. for (i = 0; i < 10 && lowest > 4; i++) {
  1521. dev->bucket_finder++;
  1522. dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  1523. if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  1524. lowest = dev->obj_bucket[dev->bucket_finder].count;
  1525. l = dev->bucket_finder;
  1526. }
  1527. }
  1528. return l;
  1529. }
  1530. static int yaffs_new_obj_id(struct yaffs_dev *dev)
  1531. {
  1532. int bucket = yaffs_find_nice_bucket(dev);
  1533. /* Now find an object value that has not already been taken
  1534. * by scanning the list.
  1535. */
  1536. int found = 0;
  1537. struct list_head *i;
  1538. u32 n = (u32) bucket;
  1539. /* yaffs_check_obj_hash_sane(); */
  1540. while (!found) {
  1541. found = 1;
  1542. n += YAFFS_NOBJECT_BUCKETS;
  1543. if (1 || dev->obj_bucket[bucket].count > 0) {
  1544. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1545. /* If there is already one in the list */
  1546. if (i && list_entry(i, struct yaffs_obj,
  1547. hash_link)->obj_id == n) {
  1548. found = 0;
  1549. }
  1550. }
  1551. }
  1552. }
  1553. return n;
  1554. }
  1555. static void yaffs_hash_obj(struct yaffs_obj *in)
  1556. {
  1557. int bucket = yaffs_hash_fn(in->obj_id);
  1558. struct yaffs_dev *dev = in->my_dev;
  1559. list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  1560. dev->obj_bucket[bucket].count++;
  1561. }
  1562. struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  1563. {
  1564. int bucket = yaffs_hash_fn(number);
  1565. struct list_head *i;
  1566. struct yaffs_obj *in;
  1567. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1568. /* Look if it is in the list */
  1569. if (i) {
  1570. in = list_entry(i, struct yaffs_obj, hash_link);
  1571. if (in->obj_id == number) {
  1572. /* Don't tell the VFS about this one if it is defered free */
  1573. if (in->defered_free)
  1574. return NULL;
  1575. return in;
  1576. }
  1577. }
  1578. }
  1579. return NULL;
  1580. }
  1581. struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  1582. enum yaffs_obj_type type)
  1583. {
  1584. struct yaffs_obj *the_obj = NULL;
  1585. struct yaffs_tnode *tn = NULL;
  1586. if (number < 0)
  1587. number = yaffs_new_obj_id(dev);
  1588. if (type == YAFFS_OBJECT_TYPE_FILE) {
  1589. tn = yaffs_get_tnode(dev);
  1590. if (!tn)
  1591. return NULL;
  1592. }
  1593. the_obj = yaffs_alloc_empty_obj(dev);
  1594. if (!the_obj) {
  1595. if (tn)
  1596. yaffs_free_tnode(dev, tn);
  1597. return NULL;
  1598. }
  1599. if (the_obj) {
  1600. the_obj->fake = 0;
  1601. the_obj->rename_allowed = 1;
  1602. the_obj->unlink_allowed = 1;
  1603. the_obj->obj_id = number;
  1604. yaffs_hash_obj(the_obj);
  1605. the_obj->variant_type = type;
  1606. yaffs_load_current_time(the_obj, 1, 1);
  1607. switch (type) {
  1608. case YAFFS_OBJECT_TYPE_FILE:
  1609. the_obj->variant.file_variant.file_size = 0;
  1610. the_obj->variant.file_variant.scanned_size = 0;
  1611. the_obj->variant.file_variant.shrink_size = ~0; /* max */
  1612. the_obj->variant.file_variant.top_level = 0;
  1613. the_obj->variant.file_variant.top = tn;
  1614. break;
  1615. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1616. INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  1617. INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  1618. break;
  1619. case YAFFS_OBJECT_TYPE_SYMLINK:
  1620. case YAFFS_OBJECT_TYPE_HARDLINK:
  1621. case YAFFS_OBJECT_TYPE_SPECIAL:
  1622. /* No action required */
  1623. break;
  1624. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1625. /* todo this should not happen */
  1626. break;
  1627. }
  1628. }
  1629. return the_obj;
  1630. }
  1631. static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  1632. int number, u32 mode)
  1633. {
  1634. struct yaffs_obj *obj =
  1635. yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  1636. if (obj) {
  1637. obj->fake = 1; /* it is fake so it might have no NAND presence... */
  1638. obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
  1639. obj->unlink_allowed = 0; /* ... or unlink it */
  1640. obj->deleted = 0;
  1641. obj->unlinked = 0;
  1642. obj->yst_mode = mode;
  1643. obj->my_dev = dev;
  1644. obj->hdr_chunk = 0; /* Not a valid chunk. */
  1645. }
  1646. return obj;
  1647. }
  1648. static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  1649. {
  1650. int i;
  1651. dev->n_obj = 0;
  1652. dev->n_tnodes = 0;
  1653. yaffs_init_raw_tnodes_and_objs(dev);
  1654. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  1655. INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  1656. dev->obj_bucket[i].count = 0;
  1657. }
  1658. }
  1659. struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  1660. int number,
  1661. enum yaffs_obj_type type)
  1662. {
  1663. struct yaffs_obj *the_obj = NULL;
  1664. if (number > 0)
  1665. the_obj = yaffs_find_by_number(dev, number);
  1666. if (!the_obj)
  1667. the_obj = yaffs_new_obj(dev, number, type);
  1668. return the_obj;
  1669. }
  1670. YCHAR *yaffs_clone_str(const YCHAR * str)
  1671. {
  1672. YCHAR *new_str = NULL;
  1673. int len;
  1674. if (!str)
  1675. str = _Y("");
  1676. len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  1677. new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
  1678. if (new_str) {
  1679. strncpy(new_str, str, len);
  1680. new_str[len] = 0;
  1681. }
  1682. return new_str;
  1683. }
  1684. /*
  1685. *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
  1686. * link (ie. name) is created or deleted in the directory.
  1687. *
  1688. * ie.
  1689. * create dir/a : update dir's mtime/ctime
  1690. * rm dir/a: update dir's mtime/ctime
  1691. * modify dir/a: don't update dir's mtimme/ctime
  1692. *
  1693. * This can be handled immediately or defered. Defering helps reduce the number
  1694. * of updates when many files in a directory are changed within a brief period.
  1695. *
  1696. * If the directory updating is defered then yaffs_update_dirty_dirs must be
  1697. * called periodically.
  1698. */
  1699. static void yaffs_update_parent(struct yaffs_obj *obj)
  1700. {
  1701. struct yaffs_dev *dev;
  1702. if (!obj)
  1703. return;
  1704. dev = obj->my_dev;
  1705. obj->dirty = 1;
  1706. yaffs_load_current_time(obj, 0, 1);
  1707. if (dev->param.defered_dir_update) {
  1708. struct list_head *link = &obj->variant.dir_variant.dirty;
  1709. if (list_empty(link)) {
  1710. list_add(link, &dev->dirty_dirs);
  1711. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  1712. "Added object %d to dirty directories",
  1713. obj->obj_id);
  1714. }
  1715. } else {
  1716. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1717. }
  1718. }
  1719. void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  1720. {
  1721. struct list_head *link;
  1722. struct yaffs_obj *obj;
  1723. struct yaffs_dir_var *d_s;
  1724. union yaffs_obj_var *o_v;
  1725. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
  1726. while (!list_empty(&dev->dirty_dirs)) {
  1727. link = dev->dirty_dirs.next;
  1728. list_del_init(link);
  1729. d_s = list_entry(link, struct yaffs_dir_var, dirty);
  1730. o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  1731. obj = list_entry(o_v, struct yaffs_obj, variant);
  1732. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
  1733. obj->obj_id);
  1734. if (obj->dirty)
  1735. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1736. }
  1737. }
  1738. /*
  1739. * Mknod (create) a new object.
  1740. * equiv_obj only has meaning for a hard link;
  1741. * alias_str only has meaning for a symlink.
  1742. * rdev only has meaning for devices (a subset of special objects)
  1743. */
  1744. static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  1745. struct yaffs_obj *parent,
  1746. const YCHAR * name,
  1747. u32 mode,
  1748. u32 uid,
  1749. u32 gid,
  1750. struct yaffs_obj *equiv_obj,
  1751. const YCHAR * alias_str, u32 rdev)
  1752. {
  1753. struct yaffs_obj *in;
  1754. YCHAR *str = NULL;
  1755. struct yaffs_dev *dev = parent->my_dev;
  1756. /* Check if the entry exists. If it does then fail the call since we don't want a dup. */
  1757. if (yaffs_find_by_name(parent, name))
  1758. return NULL;
  1759. if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  1760. str = yaffs_clone_str(alias_str);
  1761. if (!str)
  1762. return NULL;
  1763. }
  1764. in = yaffs_new_obj(dev, -1, type);
  1765. if (!in) {
  1766. if (str)
  1767. kfree(str);
  1768. return NULL;
  1769. }
  1770. if (in) {
  1771. in->hdr_chunk = 0;
  1772. in->valid = 1;
  1773. in->variant_type = type;
  1774. in->yst_mode = mode;
  1775. yaffs_attribs_init(in, gid, uid, rdev);
  1776. in->n_data_chunks = 0;
  1777. yaffs_set_obj_name(in, name);
  1778. in->dirty = 1;
  1779. yaffs_add_obj_to_dir(parent, in);
  1780. in->my_dev = parent->my_dev;
  1781. switch (type) {
  1782. case YAFFS_OBJECT_TYPE_SYMLINK:
  1783. in->variant.symlink_variant.alias = str;
  1784. break;
  1785. case YAFFS_OBJECT_TYPE_HARDLINK:
  1786. in->variant.hardlink_variant.equiv_obj = equiv_obj;
  1787. in->variant.hardlink_variant.equiv_id =
  1788. equiv_obj->obj_id;
  1789. list_add(&in->hard_links, &equiv_obj->hard_links);
  1790. break;
  1791. case YAFFS_OBJECT_TYPE_FILE:
  1792. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1793. case YAFFS_OBJECT_TYPE_SPECIAL:
  1794. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1795. /* do nothing */
  1796. break;
  1797. }
  1798. if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  1799. /* Could not create the object header, fail the creation */
  1800. yaffs_del_obj(in);
  1801. in = NULL;
  1802. }
  1803. yaffs_update_parent(parent);
  1804. }
  1805. return in;
  1806. }
  1807. struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  1808. const YCHAR * name, u32 mode, u32 uid,
  1809. u32 gid)
  1810. {
  1811. return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  1812. uid, gid, NULL, NULL, 0);
  1813. }
  1814. struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name,
  1815. u32 mode, u32 uid, u32 gid)
  1816. {
  1817. return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  1818. mode, uid, gid, NULL, NULL, 0);
  1819. }
  1820. struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  1821. const YCHAR * name, u32 mode, u32 uid,
  1822. u32 gid, u32 rdev)
  1823. {
  1824. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  1825. uid, gid, NULL, NULL, rdev);
  1826. }
  1827. struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  1828. const YCHAR * name, u32 mode, u32 uid,
  1829. u32 gid, const YCHAR * alias)
  1830. {
  1831. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  1832. uid, gid, NULL, alias, 0);
  1833. }
  1834. /* yaffs_link_obj returns the object id of the equivalent object.*/
  1835. struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  1836. struct yaffs_obj *equiv_obj)
  1837. {
  1838. /* Get the real object in case we were fed a hard link as an equivalent object */
  1839. equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  1840. if (yaffs_create_obj
  1841. (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
  1842. equiv_obj, NULL, 0)) {
  1843. return equiv_obj;
  1844. } else {
  1845. return NULL;
  1846. }
  1847. }
  1848. /*------------------------- Block Management and Page Allocation ----------------*/
  1849. static int yaffs_init_blocks(struct yaffs_dev *dev)
  1850. {
  1851. int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  1852. dev->block_info = NULL;
  1853. dev->chunk_bits = NULL;
  1854. dev->alloc_block = -1; /* force it to get a new one */
  1855. /* If the first allocation strategy fails, thry the alternate one */
  1856. dev->block_info =
  1857. kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
  1858. if (!dev->block_info) {
  1859. dev->block_info =
  1860. vmalloc(n_blocks * sizeof(struct yaffs_block_info));
  1861. dev->block_info_alt = 1;
  1862. } else {
  1863. dev->block_info_alt = 0;
  1864. }
  1865. if (dev->block_info) {
  1866. /* Set up dynamic blockinfo stuff. Round up bytes. */
  1867. dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
  1868. dev->chunk_bits =
  1869. kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
  1870. if (!dev->chunk_bits) {
  1871. dev->chunk_bits =
  1872. vmalloc(dev->chunk_bit_stride * n_blocks);
  1873. dev->chunk_bits_alt = 1;
  1874. } else {
  1875. dev->chunk_bits_alt = 0;
  1876. }
  1877. }
  1878. if (dev->block_info && dev->chunk_bits) {
  1879. memset(dev->block_info, 0,
  1880. n_blocks * sizeof(struct yaffs_block_info));
  1881. memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  1882. return YAFFS_OK;
  1883. }
  1884. return YAFFS_FAIL;
  1885. }
  1886. static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  1887. {
  1888. if (dev->block_info_alt && dev->block_info)
  1889. vfree(dev->block_info);
  1890. else if (dev->block_info)
  1891. kfree(dev->block_info);
  1892. dev->block_info_alt = 0;
  1893. dev->block_info = NULL;
  1894. if (dev->chunk_bits_alt && dev->chunk_bits)
  1895. vfree(dev->chunk_bits);
  1896. else if (dev->chunk_bits)
  1897. kfree(dev->chunk_bits);
  1898. dev->chunk_bits_alt = 0;
  1899. dev->chunk_bits = NULL;
  1900. }
  1901. void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  1902. {
  1903. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  1904. int erased_ok = 0;
  1905. /* If the block is still healthy erase it and mark as clean.
  1906. * If the block has had a data failure, then retire it.
  1907. */
  1908. yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  1909. "yaffs_block_became_dirty block %d state %d %s",
  1910. block_no, bi->block_state,
  1911. (bi->needs_retiring) ? "needs retiring" : "");
  1912. yaffs2_clear_oldest_dirty_seq(dev, bi);
  1913. bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  1914. /* If this is the block being garbage collected then stop gc'ing this block */
  1915. if (block_no == dev->gc_block)
  1916. dev->gc_block = 0;
  1917. /* If this block is currently the best candidate for gc then drop as a candidate */
  1918. if (block_no == dev->gc_dirtiest) {
  1919. dev->gc_dirtiest = 0;
  1920. dev->gc_pages_in_use = 0;
  1921. }
  1922. if (!bi->needs_retiring) {
  1923. yaffs2_checkpt_invalidate(dev);
  1924. erased_ok = yaffs_erase_block(dev, block_no);
  1925. if (!erased_ok) {
  1926. dev->n_erase_failures++;
  1927. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1928. "**>> Erasure failed %d", block_no);
  1929. }
  1930. }
  1931. if (erased_ok &&
  1932. ((yaffs_trace_mask & YAFFS_TRACE_ERASE)
  1933. || !yaffs_skip_verification(dev))) {
  1934. int i;
  1935. for (i = 0; i < dev->param.chunks_per_block; i++) {
  1936. if (!yaffs_check_chunk_erased
  1937. (dev, block_no * dev->param.chunks_per_block + i)) {
  1938. yaffs_trace(YAFFS_TRACE_ERROR,
  1939. ">>Block %d erasure supposedly OK, but chunk %d not erased",
  1940. block_no, i);
  1941. }
  1942. }
  1943. }
  1944. if (erased_ok) {
  1945. /* Clean it up... */
  1946. bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1947. bi->seq_number = 0;
  1948. dev->n_erased_blocks++;
  1949. bi->pages_in_use = 0;
  1950. bi->soft_del_pages = 0;
  1951. bi->has_shrink_hdr = 0;
  1952. bi->skip_erased_check = 1; /* Clean, so no need to check */
  1953. bi->gc_prioritise = 0;
  1954. yaffs_clear_chunk_bits(dev, block_no);
  1955. yaffs_trace(YAFFS_TRACE_ERASE,
  1956. "Erased block %d", block_no);
  1957. } else {
  1958. /* We lost a block of free space */
  1959. dev->n_free_chunks -= dev->param.chunks_per_block;
  1960. yaffs_retire_block(dev, block_no);
  1961. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1962. "**>> Block %d retired", block_no);
  1963. }
  1964. }
  1965. static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  1966. {
  1967. int old_chunk;
  1968. int new_chunk;
  1969. int mark_flash;
  1970. int ret_val = YAFFS_OK;
  1971. int i;
  1972. int is_checkpt_block;
  1973. int matching_chunk;
  1974. int max_copies;
  1975. int chunks_before = yaffs_get_erased_chunks(dev);
  1976. int chunks_after;
  1977. struct yaffs_ext_tags tags;
  1978. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  1979. struct yaffs_obj *object;
  1980. is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  1981. yaffs_trace(YAFFS_TRACE_TRACING,
  1982. "Collecting block %d, in use %d, shrink %d, whole_block %d",
  1983. block, bi->pages_in_use, bi->has_shrink_hdr,
  1984. whole_block);
  1985. /*yaffs_verify_free_chunks(dev); */
  1986. if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  1987. bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  1988. bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
  1989. dev->gc_disable = 1;
  1990. if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  1991. yaffs_trace(YAFFS_TRACE_TRACING,
  1992. "Collecting block %d that has no chunks in use",
  1993. block);
  1994. yaffs_block_became_dirty(dev, block);
  1995. } else {
  1996. u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
  1997. yaffs_verify_blk(dev, bi, block);
  1998. max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  1999. old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  2000. for ( /* init already done */ ;
  2001. ret_val == YAFFS_OK &&
  2002. dev->gc_chunk < dev->param.chunks_per_block &&
  2003. (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  2004. max_copies > 0; dev->gc_chunk++, old_chunk++) {
  2005. if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  2006. /* This page is in use and might need to be copied off */
  2007. max_copies--;
  2008. mark_flash = 1;
  2009. yaffs_init_tags(&tags);
  2010. yaffs_rd_chunk_tags_nand(dev, old_chunk,
  2011. buffer, &tags);
  2012. object = yaffs_find_by_number(dev, tags.obj_id);
  2013. yaffs_trace(YAFFS_TRACE_GC_DETAIL,
  2014. "Collecting chunk in block %d, %d %d %d ",
  2015. dev->gc_chunk, tags.obj_id,
  2016. tags.chunk_id, tags.n_bytes);
  2017. if (object && !yaffs_skip_verification(dev)) {
  2018. if (tags.chunk_id == 0)
  2019. matching_chunk =
  2020. object->hdr_chunk;
  2021. else if (object->soft_del)
  2022. matching_chunk = old_chunk; /* Defeat the test */
  2023. else
  2024. matching_chunk =
  2025. yaffs_find_chunk_in_file
  2026. (object, tags.chunk_id,
  2027. NULL);
  2028. if (old_chunk != matching_chunk)
  2029. yaffs_trace(YAFFS_TRACE_ERROR,
  2030. "gc: page in gc mismatch: %d %d %d %d",
  2031. old_chunk,
  2032. matching_chunk,
  2033. tags.obj_id,
  2034. tags.chunk_id);
  2035. }
  2036. if (!object) {
  2037. yaffs_trace(YAFFS_TRACE_ERROR,
  2038. "page %d in gc has no object: %d %d %d ",
  2039. old_chunk,
  2040. tags.obj_id, tags.chunk_id,
  2041. tags.n_bytes);
  2042. }
  2043. if (object &&
  2044. object->deleted &&
  2045. object->soft_del && tags.chunk_id != 0) {
  2046. /* Data chunk in a soft deleted file, throw it away
  2047. * It's a soft deleted data chunk,
  2048. * No need to copy this, just forget about it and
  2049. * fix up the object.
  2050. */
  2051. /* Free chunks already includes softdeleted chunks.
  2052. * How ever this chunk is going to soon be really deleted
  2053. * which will increment free chunks.
  2054. * We have to decrement free chunks so this works out properly.
  2055. */
  2056. dev->n_free_chunks--;
  2057. bi->soft_del_pages--;
  2058. object->n_data_chunks--;
  2059. if (object->n_data_chunks <= 0) {
  2060. /* remeber to clean up the object */
  2061. dev->gc_cleanup_list[dev->
  2062. n_clean_ups]
  2063. = tags.obj_id;
  2064. dev->n_clean_ups++;
  2065. }
  2066. mark_flash = 0;
  2067. } else if (0) {
  2068. /* Todo object && object->deleted && object->n_data_chunks == 0 */
  2069. /* Deleted object header with no data chunks.
  2070. * Can be discarded and the file deleted.
  2071. */
  2072. object->hdr_chunk = 0;
  2073. yaffs_free_tnode(object->my_dev,
  2074. object->
  2075. variant.file_variant.
  2076. top);
  2077. object->variant.file_variant.top = NULL;
  2078. yaffs_generic_obj_del(object);
  2079. } else if (object) {
  2080. /* It's either a data chunk in a live file or
  2081. * an ObjectHeader, so we're interested in it.
  2082. * NB Need to keep the ObjectHeaders of deleted files
  2083. * until the whole file has been deleted off
  2084. */
  2085. tags.serial_number++;
  2086. dev->n_gc_copies++;
  2087. if (tags.chunk_id == 0) {
  2088. /* It is an object Id,
  2089. * We need to nuke the shrinkheader flags first
  2090. * Also need to clean up shadowing.
  2091. * We no longer want the shrink_header flag since its work is done
  2092. * and if it is left in place it will mess up scanning.
  2093. */
  2094. struct yaffs_obj_hdr *oh;
  2095. oh = (struct yaffs_obj_hdr *)
  2096. buffer;
  2097. oh->is_shrink = 0;
  2098. tags.extra_is_shrink = 0;
  2099. oh->shadows_obj = 0;
  2100. oh->inband_shadowed_obj_id = 0;
  2101. tags.extra_shadows = 0;
  2102. /* Update file size */
  2103. if (object->variant_type ==
  2104. YAFFS_OBJECT_TYPE_FILE) {
  2105. oh->file_size =
  2106. object->variant.
  2107. file_variant.
  2108. file_size;
  2109. tags.extra_length =
  2110. oh->file_size;
  2111. }
  2112. yaffs_verify_oh(object, oh,
  2113. &tags, 1);
  2114. new_chunk =
  2115. yaffs_write_new_chunk(dev,
  2116. (u8 *)
  2117. oh,
  2118. &tags,
  2119. 1);
  2120. } else {
  2121. new_chunk =
  2122. yaffs_write_new_chunk(dev,
  2123. buffer,
  2124. &tags,
  2125. 1);
  2126. }
  2127. if (new_chunk < 0) {
  2128. ret_val = YAFFS_FAIL;
  2129. } else {
  2130. /* Ok, now fix up the Tnodes etc. */
  2131. if (tags.chunk_id == 0) {
  2132. /* It's a header */
  2133. object->hdr_chunk =
  2134. new_chunk;
  2135. object->serial =
  2136. tags.serial_number;
  2137. } else {
  2138. /* It's a data chunk */
  2139. int ok;
  2140. ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0);
  2141. }
  2142. }
  2143. }
  2144. if (ret_val == YAFFS_OK)
  2145. yaffs_chunk_del(dev, old_chunk,
  2146. mark_flash, __LINE__);
  2147. }
  2148. }
  2149. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2150. }
  2151. yaffs_verify_collected_blk(dev, bi, block);
  2152. if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  2153. /*
  2154. * The gc did not complete. Set block state back to FULL
  2155. * because checkpointing does not restore gc.
  2156. */
  2157. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  2158. } else {
  2159. /* The gc completed. */
  2160. /* Do any required cleanups */
  2161. for (i = 0; i < dev->n_clean_ups; i++) {
  2162. /* Time to delete the file too */
  2163. object =
  2164. yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
  2165. if (object) {
  2166. yaffs_free_tnode(dev,
  2167. object->variant.
  2168. file_variant.top);
  2169. object->variant.file_variant.top = NULL;
  2170. yaffs_trace(YAFFS_TRACE_GC,
  2171. "yaffs: About to finally delete object %d",
  2172. object->obj_id);
  2173. yaffs_generic_obj_del(object);
  2174. object->my_dev->n_deleted_files--;
  2175. }
  2176. }
  2177. chunks_after = yaffs_get_erased_chunks(dev);
  2178. if (chunks_before >= chunks_after)
  2179. yaffs_trace(YAFFS_TRACE_GC,
  2180. "gc did not increase free chunks before %d after %d",
  2181. chunks_before, chunks_after);
  2182. dev->gc_block = 0;
  2183. dev->gc_chunk = 0;
  2184. dev->n_clean_ups = 0;
  2185. }
  2186. dev->gc_disable = 0;
  2187. return ret_val;
  2188. }
  2189. /*
  2190. * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
  2191. * for garbage collection.
  2192. */
  2193. static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
  2194. int aggressive, int background)
  2195. {
  2196. int i;
  2197. int iterations;
  2198. unsigned selected = 0;
  2199. int prioritised = 0;
  2200. int prioritised_exist = 0;
  2201. struct yaffs_block_info *bi;
  2202. int threshold;
  2203. /* First let's see if we need to grab a prioritised block */
  2204. if (dev->has_pending_prioritised_gc && !aggressive) {
  2205. dev->gc_dirtiest = 0;
  2206. bi = dev->block_info;
  2207. for (i = dev->internal_start_block;
  2208. i <= dev->internal_end_block && !selected; i++) {
  2209. if (bi->gc_prioritise) {
  2210. prioritised_exist = 1;
  2211. if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  2212. yaffs_block_ok_for_gc(dev, bi)) {
  2213. selected = i;
  2214. prioritised = 1;
  2215. }
  2216. }
  2217. bi++;
  2218. }
  2219. /*
  2220. * If there is a prioritised block and none was selected then
  2221. * this happened because there is at least one old dirty block gumming
  2222. * up the works. Let's gc the oldest dirty block.
  2223. */
  2224. if (prioritised_exist &&
  2225. !selected && dev->oldest_dirty_block > 0)
  2226. selected = dev->oldest_dirty_block;
  2227. if (!prioritised_exist) /* None found, so we can clear this */
  2228. dev->has_pending_prioritised_gc = 0;
  2229. }
  2230. /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
  2231. * search harder.
  2232. * else (we're doing a leasurely gc), then we only bother to do this if the
  2233. * block has only a few pages in use.
  2234. */
  2235. if (!selected) {
  2236. int pages_used;
  2237. int n_blocks =
  2238. dev->internal_end_block - dev->internal_start_block + 1;
  2239. if (aggressive) {
  2240. threshold = dev->param.chunks_per_block;
  2241. iterations = n_blocks;
  2242. } else {
  2243. int max_threshold;
  2244. if (background)
  2245. max_threshold = dev->param.chunks_per_block / 2;
  2246. else
  2247. max_threshold = dev->param.chunks_per_block / 8;
  2248. if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  2249. max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  2250. threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
  2251. if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  2252. threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  2253. if (threshold > max_threshold)
  2254. threshold = max_threshold;
  2255. iterations = n_blocks / 16 + 1;
  2256. if (iterations > 100)
  2257. iterations = 100;
  2258. }
  2259. for (i = 0;
  2260. i < iterations &&
  2261. (dev->gc_dirtiest < 1 ||
  2262. dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) {
  2263. dev->gc_block_finder++;
  2264. if (dev->gc_block_finder < dev->internal_start_block ||
  2265. dev->gc_block_finder > dev->internal_end_block)
  2266. dev->gc_block_finder =
  2267. dev->internal_start_block;
  2268. bi = yaffs_get_block_info(dev, dev->gc_block_finder);
  2269. pages_used = bi->pages_in_use - bi->soft_del_pages;
  2270. if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  2271. pages_used < dev->param.chunks_per_block &&
  2272. (dev->gc_dirtiest < 1
  2273. || pages_used < dev->gc_pages_in_use)
  2274. && yaffs_block_ok_for_gc(dev, bi)) {
  2275. dev->gc_dirtiest = dev->gc_block_finder;
  2276. dev->gc_pages_in_use = pages_used;
  2277. }
  2278. }
  2279. if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
  2280. selected = dev->gc_dirtiest;
  2281. }
  2282. /*
  2283. * If nothing has been selected for a while, try selecting the oldest dirty
  2284. * because that's gumming up the works.
  2285. */
  2286. if (!selected && dev->param.is_yaffs2 &&
  2287. dev->gc_not_done >= (background ? 10 : 20)) {
  2288. yaffs2_find_oldest_dirty_seq(dev);
  2289. if (dev->oldest_dirty_block > 0) {
  2290. selected = dev->oldest_dirty_block;
  2291. dev->gc_dirtiest = selected;
  2292. dev->oldest_dirty_gc_count++;
  2293. bi = yaffs_get_block_info(dev, selected);
  2294. dev->gc_pages_in_use =
  2295. bi->pages_in_use - bi->soft_del_pages;
  2296. } else {
  2297. dev->gc_not_done = 0;
  2298. }
  2299. }
  2300. if (selected) {
  2301. yaffs_trace(YAFFS_TRACE_GC,
  2302. "GC Selected block %d with %d free, prioritised:%d",
  2303. selected,
  2304. dev->param.chunks_per_block - dev->gc_pages_in_use,
  2305. prioritised);
  2306. dev->n_gc_blocks++;
  2307. if (background)
  2308. dev->bg_gcs++;
  2309. dev->gc_dirtiest = 0;
  2310. dev->gc_pages_in_use = 0;
  2311. dev->gc_not_done = 0;
  2312. if (dev->refresh_skip > 0)
  2313. dev->refresh_skip--;
  2314. } else {
  2315. dev->gc_not_done++;
  2316. yaffs_trace(YAFFS_TRACE_GC,
  2317. "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
  2318. dev->gc_block_finder, dev->gc_not_done, threshold,
  2319. dev->gc_dirtiest, dev->gc_pages_in_use,
  2320. dev->oldest_dirty_block, background ? " bg" : "");
  2321. }
  2322. return selected;
  2323. }
  2324. /* New garbage collector
  2325. * If we're very low on erased blocks then we do aggressive garbage collection
  2326. * otherwise we do "leasurely" garbage collection.
  2327. * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  2328. * Passive gc only inspects smaller areas and will only accept more dirty blocks.
  2329. *
  2330. * The idea is to help clear out space in a more spread-out manner.
  2331. * Dunno if it really does anything useful.
  2332. */
  2333. static int yaffs_check_gc(struct yaffs_dev *dev, int background)
  2334. {
  2335. int aggressive = 0;
  2336. int gc_ok = YAFFS_OK;
  2337. int max_tries = 0;
  2338. int min_erased;
  2339. int erased_chunks;
  2340. int checkpt_block_adjust;
  2341. if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
  2342. return YAFFS_OK;
  2343. if (dev->gc_disable) {
  2344. /* Bail out so we don't get recursive gc */
  2345. return YAFFS_OK;
  2346. }
  2347. /* This loop should pass the first time.
  2348. * We'll only see looping here if the collection does not increase space.
  2349. */
  2350. do {
  2351. max_tries++;
  2352. checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
  2353. min_erased =
  2354. dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
  2355. erased_chunks =
  2356. dev->n_erased_blocks * dev->param.chunks_per_block;
  2357. /* If we need a block soon then do aggressive gc. */
  2358. if (dev->n_erased_blocks < min_erased)
  2359. aggressive = 1;
  2360. else {
  2361. if (!background
  2362. && erased_chunks > (dev->n_free_chunks / 4))
  2363. break;
  2364. if (dev->gc_skip > 20)
  2365. dev->gc_skip = 20;
  2366. if (erased_chunks < dev->n_free_chunks / 2 ||
  2367. dev->gc_skip < 1 || background)
  2368. aggressive = 0;
  2369. else {
  2370. dev->gc_skip--;
  2371. break;
  2372. }
  2373. }
  2374. dev->gc_skip = 5;
  2375. /* If we don't already have a block being gc'd then see if we should start another */
  2376. if (dev->gc_block < 1 && !aggressive) {
  2377. dev->gc_block = yaffs2_find_refresh_block(dev);
  2378. dev->gc_chunk = 0;
  2379. dev->n_clean_ups = 0;
  2380. }
  2381. if (dev->gc_block < 1) {
  2382. dev->gc_block =
  2383. yaffs_find_gc_block(dev, aggressive, background);
  2384. dev->gc_chunk = 0;
  2385. dev->n_clean_ups = 0;
  2386. }
  2387. if (dev->gc_block > 0) {
  2388. dev->all_gcs++;
  2389. if (!aggressive)
  2390. dev->passive_gc_count++;
  2391. yaffs_trace(YAFFS_TRACE_GC,
  2392. "yaffs: GC n_erased_blocks %d aggressive %d",
  2393. dev->n_erased_blocks, aggressive);
  2394. gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
  2395. }
  2396. if (dev->n_erased_blocks < (dev->param.n_reserved_blocks)
  2397. && dev->gc_block > 0) {
  2398. yaffs_trace(YAFFS_TRACE_GC,
  2399. "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
  2400. dev->n_erased_blocks, max_tries,
  2401. dev->gc_block);
  2402. }
  2403. } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
  2404. (dev->gc_block > 0) && (max_tries < 2));
  2405. return aggressive ? gc_ok : YAFFS_OK;
  2406. }
  2407. /*
  2408. * yaffs_bg_gc()
  2409. * Garbage collects. Intended to be called from a background thread.
  2410. * Returns non-zero if at least half the free chunks are erased.
  2411. */
  2412. int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
  2413. {
  2414. int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
  2415. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
  2416. yaffs_check_gc(dev, 1);
  2417. return erased_chunks > dev->n_free_chunks / 2;
  2418. }
  2419. /*-------------------- Data file manipulation -----------------*/
  2420. static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
  2421. {
  2422. int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
  2423. if (nand_chunk >= 0)
  2424. return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
  2425. buffer, NULL);
  2426. else {
  2427. yaffs_trace(YAFFS_TRACE_NANDACCESS,
  2428. "Chunk %d not found zero instead",
  2429. nand_chunk);
  2430. /* get sane (zero) data if you read a hole */
  2431. memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
  2432. return 0;
  2433. }
  2434. }
  2435. void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  2436. int lyn)
  2437. {
  2438. int block;
  2439. int page;
  2440. struct yaffs_ext_tags tags;
  2441. struct yaffs_block_info *bi;
  2442. if (chunk_id <= 0)
  2443. return;
  2444. dev->n_deletions++;
  2445. block = chunk_id / dev->param.chunks_per_block;
  2446. page = chunk_id % dev->param.chunks_per_block;
  2447. if (!yaffs_check_chunk_bit(dev, block, page))
  2448. yaffs_trace(YAFFS_TRACE_VERIFY,
  2449. "Deleting invalid chunk %d", chunk_id);
  2450. bi = yaffs_get_block_info(dev, block);
  2451. yaffs2_update_oldest_dirty_seq(dev, block, bi);
  2452. yaffs_trace(YAFFS_TRACE_DELETION,
  2453. "line %d delete of chunk %d",
  2454. lyn, chunk_id);
  2455. if (!dev->param.is_yaffs2 && mark_flash &&
  2456. bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
  2457. yaffs_init_tags(&tags);
  2458. tags.is_deleted = 1;
  2459. yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
  2460. yaffs_handle_chunk_update(dev, chunk_id, &tags);
  2461. } else {
  2462. dev->n_unmarked_deletions++;
  2463. }
  2464. /* Pull out of the management area.
  2465. * If the whole block became dirty, this will kick off an erasure.
  2466. */
  2467. if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
  2468. bi->block_state == YAFFS_BLOCK_STATE_FULL ||
  2469. bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
  2470. bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  2471. dev->n_free_chunks++;
  2472. yaffs_clear_chunk_bit(dev, block, page);
  2473. bi->pages_in_use--;
  2474. if (bi->pages_in_use == 0 &&
  2475. !bi->has_shrink_hdr &&
  2476. bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
  2477. bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
  2478. yaffs_block_became_dirty(dev, block);
  2479. }
  2480. }
  2481. }
  2482. static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  2483. const u8 * buffer, int n_bytes, int use_reserve)
  2484. {
  2485. /* Find old chunk Need to do this to get serial number
  2486. * Write new one and patch into tree.
  2487. * Invalidate old tags.
  2488. */
  2489. int prev_chunk_id;
  2490. struct yaffs_ext_tags prev_tags;
  2491. int new_chunk_id;
  2492. struct yaffs_ext_tags new_tags;
  2493. struct yaffs_dev *dev = in->my_dev;
  2494. yaffs_check_gc(dev, 0);
  2495. /* Get the previous chunk at this location in the file if it exists.
  2496. * If it does not exist then put a zero into the tree. This creates
  2497. * the tnode now, rather than later when it is harder to clean up.
  2498. */
  2499. prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
  2500. if (prev_chunk_id < 1 &&
  2501. !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
  2502. return 0;
  2503. /* Set up new tags */
  2504. yaffs_init_tags(&new_tags);
  2505. new_tags.chunk_id = inode_chunk;
  2506. new_tags.obj_id = in->obj_id;
  2507. new_tags.serial_number =
  2508. (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
  2509. new_tags.n_bytes = n_bytes;
  2510. if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
  2511. yaffs_trace(YAFFS_TRACE_ERROR,
  2512. "Writing %d bytes to chunk!!!!!!!!!",
  2513. n_bytes);
  2514. YBUG();
  2515. }
  2516. new_chunk_id =
  2517. yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
  2518. if (new_chunk_id > 0) {
  2519. yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
  2520. if (prev_chunk_id > 0)
  2521. yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  2522. yaffs_verify_file_sane(in);
  2523. }
  2524. return new_chunk_id;
  2525. }
  2526. static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
  2527. const YCHAR * name, const void *value, int size,
  2528. int flags)
  2529. {
  2530. struct yaffs_xattr_mod xmod;
  2531. int result;
  2532. xmod.set = set;
  2533. xmod.name = name;
  2534. xmod.data = value;
  2535. xmod.size = size;
  2536. xmod.flags = flags;
  2537. xmod.result = -ENOSPC;
  2538. result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
  2539. if (result > 0)
  2540. return xmod.result;
  2541. else
  2542. return -ENOSPC;
  2543. }
  2544. static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  2545. struct yaffs_xattr_mod *xmod)
  2546. {
  2547. int retval = 0;
  2548. int x_offs = sizeof(struct yaffs_obj_hdr);
  2549. struct yaffs_dev *dev = obj->my_dev;
  2550. int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  2551. char *x_buffer = buffer + x_offs;
  2552. if (xmod->set)
  2553. retval =
  2554. nval_set(x_buffer, x_size, xmod->name, xmod->data,
  2555. xmod->size, xmod->flags);
  2556. else
  2557. retval = nval_del(x_buffer, x_size, xmod->name);
  2558. obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  2559. obj->xattr_known = 1;
  2560. xmod->result = retval;
  2561. return retval;
  2562. }
  2563. static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name,
  2564. void *value, int size)
  2565. {
  2566. char *buffer = NULL;
  2567. int result;
  2568. struct yaffs_ext_tags tags;
  2569. struct yaffs_dev *dev = obj->my_dev;
  2570. int x_offs = sizeof(struct yaffs_obj_hdr);
  2571. int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  2572. char *x_buffer;
  2573. int retval = 0;
  2574. if (obj->hdr_chunk < 1)
  2575. return -ENODATA;
  2576. /* If we know that the object has no xattribs then don't do all the
  2577. * reading and parsing.
  2578. */
  2579. if (obj->xattr_known && !obj->has_xattr) {
  2580. if (name)
  2581. return -ENODATA;
  2582. else
  2583. return 0;
  2584. }
  2585. buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__);
  2586. if (!buffer)
  2587. return -ENOMEM;
  2588. result =
  2589. yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
  2590. if (result != YAFFS_OK)
  2591. retval = -ENOENT;
  2592. else {
  2593. x_buffer = buffer + x_offs;
  2594. if (!obj->xattr_known) {
  2595. obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  2596. obj->xattr_known = 1;
  2597. }
  2598. if (name)
  2599. retval = nval_get(x_buffer, x_size, name, value, size);
  2600. else
  2601. retval = nval_list(x_buffer, x_size, value, size);
  2602. }
  2603. yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__);
  2604. return retval;
  2605. }
  2606. int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  2607. const void *value, int size, int flags)
  2608. {
  2609. return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
  2610. }
  2611. int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
  2612. {
  2613. return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
  2614. }
  2615. int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  2616. int size)
  2617. {
  2618. return yaffs_do_xattrib_fetch(obj, name, value, size);
  2619. }
  2620. int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
  2621. {
  2622. return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
  2623. }
  2624. static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
  2625. {
  2626. u8 *chunk_data;
  2627. struct yaffs_obj_hdr *oh;
  2628. struct yaffs_dev *dev;
  2629. struct yaffs_ext_tags tags;
  2630. int result;
  2631. int alloc_failed = 0;
  2632. if (!in)
  2633. return;
  2634. dev = in->my_dev;
  2635. if (in->lazy_loaded && in->hdr_chunk > 0) {
  2636. in->lazy_loaded = 0;
  2637. chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
  2638. result =
  2639. yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data,
  2640. &tags);
  2641. oh = (struct yaffs_obj_hdr *)chunk_data;
  2642. in->yst_mode = oh->yst_mode;
  2643. yaffs_load_attribs(in, oh);
  2644. yaffs_set_obj_name_from_oh(in, oh);
  2645. if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  2646. in->variant.symlink_variant.alias =
  2647. yaffs_clone_str(oh->alias);
  2648. if (!in->variant.symlink_variant.alias)
  2649. alloc_failed = 1; /* Not returned to caller */
  2650. }
  2651. yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
  2652. }
  2653. }
  2654. static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name,
  2655. const YCHAR * oh_name, int buff_size)
  2656. {
  2657. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  2658. if (dev->param.auto_unicode) {
  2659. if (*oh_name) {
  2660. /* It is an ASCII name, do an ASCII to
  2661. * unicode conversion */
  2662. const char *ascii_oh_name = (const char *)oh_name;
  2663. int n = buff_size - 1;
  2664. while (n > 0 && *ascii_oh_name) {
  2665. *name = *ascii_oh_name;
  2666. name++;
  2667. ascii_oh_name++;
  2668. n--;
  2669. }
  2670. } else {
  2671. strncpy(name, oh_name + 1, buff_size - 1);
  2672. }
  2673. } else {
  2674. #else
  2675. {
  2676. #endif
  2677. strncpy(name, oh_name, buff_size - 1);
  2678. }
  2679. }
  2680. static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name,
  2681. const YCHAR * name)
  2682. {
  2683. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  2684. int is_ascii;
  2685. YCHAR *w;
  2686. if (dev->param.auto_unicode) {
  2687. is_ascii = 1;
  2688. w = name;
  2689. /* Figure out if the name will fit in ascii character set */
  2690. while (is_ascii && *w) {
  2691. if ((*w) & 0xff00)
  2692. is_ascii = 0;
  2693. w++;
  2694. }
  2695. if (is_ascii) {
  2696. /* It is an ASCII name, so do a unicode to ascii conversion */
  2697. char *ascii_oh_name = (char *)oh_name;
  2698. int n = YAFFS_MAX_NAME_LENGTH - 1;
  2699. while (n > 0 && *name) {
  2700. *ascii_oh_name = *name;
  2701. name++;
  2702. ascii_oh_name++;
  2703. n--;
  2704. }
  2705. } else {
  2706. /* It is a unicode name, so save starting at the second YCHAR */
  2707. *oh_name = 0;
  2708. strncpy(oh_name + 1, name,
  2709. YAFFS_MAX_NAME_LENGTH - 2);
  2710. }
  2711. } else {
  2712. #else
  2713. {
  2714. #endif
  2715. strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
  2716. }
  2717. }
  2718. /* UpdateObjectHeader updates the header on NAND for an object.
  2719. * If name is not NULL, then that new name is used.
  2720. */
  2721. int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force,
  2722. int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
  2723. {
  2724. struct yaffs_block_info *bi;
  2725. struct yaffs_dev *dev = in->my_dev;
  2726. int prev_chunk_id;
  2727. int ret_val = 0;
  2728. int result = 0;
  2729. int new_chunk_id;
  2730. struct yaffs_ext_tags new_tags;
  2731. struct yaffs_ext_tags old_tags;
  2732. const YCHAR *alias = NULL;
  2733. u8 *buffer = NULL;
  2734. YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
  2735. struct yaffs_obj_hdr *oh = NULL;
  2736. strcpy(old_name, _Y("silly old name"));
  2737. if (!in->fake || in == dev->root_dir ||
  2738. force || xmod) {
  2739. yaffs_check_gc(dev, 0);
  2740. yaffs_check_obj_details_loaded(in);
  2741. buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
  2742. oh = (struct yaffs_obj_hdr *)buffer;
  2743. prev_chunk_id = in->hdr_chunk;
  2744. if (prev_chunk_id > 0) {
  2745. result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
  2746. buffer, &old_tags);
  2747. yaffs_verify_oh(in, oh, &old_tags, 0);
  2748. memcpy(old_name, oh->name, sizeof(oh->name));
  2749. memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr));
  2750. } else {
  2751. memset(buffer, 0xFF, dev->data_bytes_per_chunk);
  2752. }
  2753. oh->type = in->variant_type;
  2754. oh->yst_mode = in->yst_mode;
  2755. oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
  2756. yaffs_load_attribs_oh(oh, in);
  2757. if (in->parent)
  2758. oh->parent_obj_id = in->parent->obj_id;
  2759. else
  2760. oh->parent_obj_id = 0;
  2761. if (name && *name) {
  2762. memset(oh->name, 0, sizeof(oh->name));
  2763. yaffs_load_oh_from_name(dev, oh->name, name);
  2764. } else if (prev_chunk_id > 0) {
  2765. memcpy(oh->name, old_name, sizeof(oh->name));
  2766. } else {
  2767. memset(oh->name, 0, sizeof(oh->name));
  2768. }
  2769. oh->is_shrink = is_shrink;
  2770. switch (in->variant_type) {
  2771. case YAFFS_OBJECT_TYPE_UNKNOWN:
  2772. /* Should not happen */
  2773. break;
  2774. case YAFFS_OBJECT_TYPE_FILE:
  2775. oh->file_size =
  2776. (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
  2777. || oh->parent_obj_id ==
  2778. YAFFS_OBJECTID_UNLINKED) ? 0 : in->
  2779. variant.file_variant.file_size;
  2780. break;
  2781. case YAFFS_OBJECT_TYPE_HARDLINK:
  2782. oh->equiv_id = in->variant.hardlink_variant.equiv_id;
  2783. break;
  2784. case YAFFS_OBJECT_TYPE_SPECIAL:
  2785. /* Do nothing */
  2786. break;
  2787. case YAFFS_OBJECT_TYPE_DIRECTORY:
  2788. /* Do nothing */
  2789. break;
  2790. case YAFFS_OBJECT_TYPE_SYMLINK:
  2791. alias = in->variant.symlink_variant.alias;
  2792. if (!alias)
  2793. alias = _Y("no alias");
  2794. strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
  2795. oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  2796. break;
  2797. }
  2798. /* process any xattrib modifications */
  2799. if (xmod)
  2800. yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
  2801. /* Tags */
  2802. yaffs_init_tags(&new_tags);
  2803. in->serial++;
  2804. new_tags.chunk_id = 0;
  2805. new_tags.obj_id = in->obj_id;
  2806. new_tags.serial_number = in->serial;
  2807. /* Add extra info for file header */
  2808. new_tags.extra_available = 1;
  2809. new_tags.extra_parent_id = oh->parent_obj_id;
  2810. new_tags.extra_length = oh->file_size;
  2811. new_tags.extra_is_shrink = oh->is_shrink;
  2812. new_tags.extra_equiv_id = oh->equiv_id;
  2813. new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
  2814. new_tags.extra_obj_type = in->variant_type;
  2815. yaffs_verify_oh(in, oh, &new_tags, 1);
  2816. /* Create new chunk in NAND */
  2817. new_chunk_id =
  2818. yaffs_write_new_chunk(dev, buffer, &new_tags,
  2819. (prev_chunk_id > 0) ? 1 : 0);
  2820. if (new_chunk_id >= 0) {
  2821. in->hdr_chunk = new_chunk_id;
  2822. if (prev_chunk_id > 0) {
  2823. yaffs_chunk_del(dev, prev_chunk_id, 1,
  2824. __LINE__);
  2825. }
  2826. if (!yaffs_obj_cache_dirty(in))
  2827. in->dirty = 0;
  2828. /* If this was a shrink, then mark the block that the chunk lives on */
  2829. if (is_shrink) {
  2830. bi = yaffs_get_block_info(in->my_dev,
  2831. new_chunk_id /
  2832. in->my_dev->param.
  2833. chunks_per_block);
  2834. bi->has_shrink_hdr = 1;
  2835. }
  2836. }
  2837. ret_val = new_chunk_id;
  2838. }
  2839. if (buffer)
  2840. yaffs_release_temp_buffer(dev, buffer, __LINE__);
  2841. return ret_val;
  2842. }
  2843. /*--------------------- File read/write ------------------------
  2844. * Read and write have very similar structures.
  2845. * In general the read/write has three parts to it
  2846. * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  2847. * Some complete chunks
  2848. * An incomplete chunk to end off with
  2849. *
  2850. * Curve-balls: the first chunk might also be the last chunk.
  2851. */
  2852. int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
  2853. {
  2854. int chunk;
  2855. u32 start;
  2856. int n_copy;
  2857. int n = n_bytes;
  2858. int n_done = 0;
  2859. struct yaffs_cache *cache;
  2860. struct yaffs_dev *dev;
  2861. dev = in->my_dev;
  2862. while (n > 0) {
  2863. /* chunk = offset / dev->data_bytes_per_chunk + 1; */
  2864. /* start = offset % dev->data_bytes_per_chunk; */
  2865. yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  2866. chunk++;
  2867. /* OK now check for the curveball where the start and end are in
  2868. * the same chunk.
  2869. */
  2870. if ((start + n) < dev->data_bytes_per_chunk)
  2871. n_copy = n;
  2872. else
  2873. n_copy = dev->data_bytes_per_chunk - start;
  2874. cache = yaffs_find_chunk_cache(in, chunk);
  2875. /* If the chunk is already in the cache or it is less than a whole chunk
  2876. * or we're using inband tags then use the cache (if there is caching)
  2877. * else bypass the cache.
  2878. */
  2879. if (cache || n_copy != dev->data_bytes_per_chunk
  2880. || dev->param.inband_tags) {
  2881. if (dev->param.n_caches > 0) {
  2882. /* If we can't find the data in the cache, then load it up. */
  2883. if (!cache) {
  2884. cache =
  2885. yaffs_grab_chunk_cache(in->my_dev);
  2886. cache->object = in;
  2887. cache->chunk_id = chunk;
  2888. cache->dirty = 0;
  2889. cache->locked = 0;
  2890. yaffs_rd_data_obj(in, chunk,
  2891. cache->data);
  2892. cache->n_bytes = 0;
  2893. }
  2894. yaffs_use_cache(dev, cache, 0);
  2895. cache->locked = 1;
  2896. memcpy(buffer, &cache->data[start], n_copy);
  2897. cache->locked = 0;
  2898. } else {
  2899. /* Read into the local buffer then copy.. */
  2900. u8 *local_buffer =
  2901. yaffs_get_temp_buffer(dev, __LINE__);
  2902. yaffs_rd_data_obj(in, chunk, local_buffer);
  2903. memcpy(buffer, &local_buffer[start], n_copy);
  2904. yaffs_release_temp_buffer(dev, local_buffer,
  2905. __LINE__);
  2906. }
  2907. } else {
  2908. /* A full chunk. Read directly into the supplied buffer. */
  2909. yaffs_rd_data_obj(in, chunk, buffer);
  2910. }
  2911. n -= n_copy;
  2912. offset += n_copy;
  2913. buffer += n_copy;
  2914. n_done += n_copy;
  2915. }
  2916. return n_done;
  2917. }
  2918. int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  2919. int n_bytes, int write_trhrough)
  2920. {
  2921. int chunk;
  2922. u32 start;
  2923. int n_copy;
  2924. int n = n_bytes;
  2925. int n_done = 0;
  2926. int n_writeback;
  2927. int start_write = offset;
  2928. int chunk_written = 0;
  2929. u32 n_bytes_read;
  2930. u32 chunk_start;
  2931. struct yaffs_dev *dev;
  2932. dev = in->my_dev;
  2933. while (n > 0 && chunk_written >= 0) {
  2934. yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  2935. if (chunk * dev->data_bytes_per_chunk + start != offset ||
  2936. start >= dev->data_bytes_per_chunk) {
  2937. yaffs_trace(YAFFS_TRACE_ERROR,
  2938. "AddrToChunk of offset %d gives chunk %d start %d",
  2939. (int)offset, chunk, start);
  2940. }
  2941. chunk++; /* File pos to chunk in file offset */
  2942. /* OK now check for the curveball where the start and end are in
  2943. * the same chunk.
  2944. */
  2945. if ((start + n) < dev->data_bytes_per_chunk) {
  2946. n_copy = n;
  2947. /* Now folks, to calculate how many bytes to write back....
  2948. * If we're overwriting and not writing to then end of file then
  2949. * we need to write back as much as was there before.
  2950. */
  2951. chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk);
  2952. if (chunk_start > in->variant.file_variant.file_size)
  2953. n_bytes_read = 0; /* Past end of file */
  2954. else
  2955. n_bytes_read =
  2956. in->variant.file_variant.file_size -
  2957. chunk_start;
  2958. if (n_bytes_read > dev->data_bytes_per_chunk)
  2959. n_bytes_read = dev->data_bytes_per_chunk;
  2960. n_writeback =
  2961. (n_bytes_read >
  2962. (start + n)) ? n_bytes_read : (start + n);
  2963. if (n_writeback < 0
  2964. || n_writeback > dev->data_bytes_per_chunk)
  2965. YBUG();
  2966. } else {
  2967. n_copy = dev->data_bytes_per_chunk - start;
  2968. n_writeback = dev->data_bytes_per_chunk;
  2969. }
  2970. if (n_copy != dev->data_bytes_per_chunk
  2971. || dev->param.inband_tags) {
  2972. /* An incomplete start or end chunk (or maybe both start and end chunk),
  2973. * or we're using inband tags, so we want to use the cache buffers.
  2974. */
  2975. if (dev->param.n_caches > 0) {
  2976. struct yaffs_cache *cache;
  2977. /* If we can't find the data in the cache, then load the cache */
  2978. cache = yaffs_find_chunk_cache(in, chunk);
  2979. if (!cache
  2980. && yaffs_check_alloc_available(dev, 1)) {
  2981. cache = yaffs_grab_chunk_cache(dev);
  2982. cache->object = in;
  2983. cache->chunk_id = chunk;
  2984. cache->dirty = 0;
  2985. cache->locked = 0;
  2986. yaffs_rd_data_obj(in, chunk,
  2987. cache->data);
  2988. } else if (cache &&
  2989. !cache->dirty &&
  2990. !yaffs_check_alloc_available(dev,
  2991. 1)) {
  2992. /* Drop the cache if it was a read cache item and
  2993. * no space check has been made for it.
  2994. */
  2995. cache = NULL;
  2996. }
  2997. if (cache) {
  2998. yaffs_use_cache(dev, cache, 1);
  2999. cache->locked = 1;
  3000. memcpy(&cache->data[start], buffer,
  3001. n_copy);
  3002. cache->locked = 0;
  3003. cache->n_bytes = n_writeback;
  3004. if (write_trhrough) {
  3005. chunk_written =
  3006. yaffs_wr_data_obj
  3007. (cache->object,
  3008. cache->chunk_id,
  3009. cache->data,
  3010. cache->n_bytes, 1);
  3011. cache->dirty = 0;
  3012. }
  3013. } else {
  3014. chunk_written = -1; /* fail the write */
  3015. }
  3016. } else {
  3017. /* An incomplete start or end chunk (or maybe both start and end chunk)
  3018. * Read into the local buffer then copy, then copy over and write back.
  3019. */
  3020. u8 *local_buffer =
  3021. yaffs_get_temp_buffer(dev, __LINE__);
  3022. yaffs_rd_data_obj(in, chunk, local_buffer);
  3023. memcpy(&local_buffer[start], buffer, n_copy);
  3024. chunk_written =
  3025. yaffs_wr_data_obj(in, chunk,
  3026. local_buffer,
  3027. n_writeback, 0);
  3028. yaffs_release_temp_buffer(dev, local_buffer,
  3029. __LINE__);
  3030. }
  3031. } else {
  3032. /* A full chunk. Write directly from the supplied buffer. */
  3033. chunk_written =
  3034. yaffs_wr_data_obj(in, chunk, buffer,
  3035. dev->data_bytes_per_chunk, 0);
  3036. /* Since we've overwritten the cached data, we better invalidate it. */
  3037. yaffs_invalidate_chunk_cache(in, chunk);
  3038. }
  3039. if (chunk_written >= 0) {
  3040. n -= n_copy;
  3041. offset += n_copy;
  3042. buffer += n_copy;
  3043. n_done += n_copy;
  3044. }
  3045. }
  3046. /* Update file object */
  3047. if ((start_write + n_done) > in->variant.file_variant.file_size)
  3048. in->variant.file_variant.file_size = (start_write + n_done);
  3049. in->dirty = 1;
  3050. return n_done;
  3051. }
  3052. int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset,
  3053. int n_bytes, int write_trhrough)
  3054. {
  3055. yaffs2_handle_hole(in, offset);
  3056. return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough);
  3057. }
  3058. /* ---------------------- File resizing stuff ------------------ */
  3059. static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
  3060. {
  3061. struct yaffs_dev *dev = in->my_dev;
  3062. int old_size = in->variant.file_variant.file_size;
  3063. int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk;
  3064. int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
  3065. dev->data_bytes_per_chunk;
  3066. int i;
  3067. int chunk_id;
  3068. /* Delete backwards so that we don't end up with holes if
  3069. * power is lost part-way through the operation.
  3070. */
  3071. for (i = last_del; i >= start_del; i--) {
  3072. /* NB this could be optimised somewhat,
  3073. * eg. could retrieve the tags and write them without
  3074. * using yaffs_chunk_del
  3075. */
  3076. chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
  3077. if (chunk_id > 0) {
  3078. if (chunk_id <
  3079. (dev->internal_start_block *
  3080. dev->param.chunks_per_block)
  3081. || chunk_id >=
  3082. ((dev->internal_end_block +
  3083. 1) * dev->param.chunks_per_block)) {
  3084. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3085. "Found daft chunk_id %d for %d",
  3086. chunk_id, i);
  3087. } else {
  3088. in->n_data_chunks--;
  3089. yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
  3090. }
  3091. }
  3092. }
  3093. }
  3094. void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
  3095. {
  3096. int new_full;
  3097. u32 new_partial;
  3098. struct yaffs_dev *dev = obj->my_dev;
  3099. yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
  3100. yaffs_prune_chunks(obj, new_size);
  3101. if (new_partial != 0) {
  3102. int last_chunk = 1 + new_full;
  3103. u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
  3104. /* Rewrite the last chunk with its new size and zero pad */
  3105. yaffs_rd_data_obj(obj, last_chunk, local_buffer);
  3106. memset(local_buffer + new_partial, 0,
  3107. dev->data_bytes_per_chunk - new_partial);
  3108. yaffs_wr_data_obj(obj, last_chunk, local_buffer,
  3109. new_partial, 1);
  3110. yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
  3111. }
  3112. obj->variant.file_variant.file_size = new_size;
  3113. yaffs_prune_tree(dev, &obj->variant.file_variant);
  3114. }
  3115. int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
  3116. {
  3117. struct yaffs_dev *dev = in->my_dev;
  3118. int old_size = in->variant.file_variant.file_size;
  3119. yaffs_flush_file_cache(in);
  3120. yaffs_invalidate_whole_cache(in);
  3121. yaffs_check_gc(dev, 0);
  3122. if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
  3123. return YAFFS_FAIL;
  3124. if (new_size == old_size)
  3125. return YAFFS_OK;
  3126. if (new_size > old_size) {
  3127. yaffs2_handle_hole(in, new_size);
  3128. in->variant.file_variant.file_size = new_size;
  3129. } else {
  3130. /* new_size < old_size */
  3131. yaffs_resize_file_down(in, new_size);
  3132. }
  3133. /* Write a new object header to reflect the resize.
  3134. * show we've shrunk the file, if need be
  3135. * Do this only if the file is not in the deleted directories
  3136. * and is not shadowed.
  3137. */
  3138. if (in->parent &&
  3139. !in->is_shadowed &&
  3140. in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  3141. in->parent->obj_id != YAFFS_OBJECTID_DELETED)
  3142. yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
  3143. return YAFFS_OK;
  3144. }
  3145. int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
  3146. {
  3147. int ret_val;
  3148. if (in->dirty) {
  3149. yaffs_flush_file_cache(in);
  3150. if (data_sync) /* Only sync data */
  3151. ret_val = YAFFS_OK;
  3152. else {
  3153. if (update_time)
  3154. yaffs_load_current_time(in, 0, 0);
  3155. ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
  3156. 0) ? YAFFS_OK : YAFFS_FAIL;
  3157. }
  3158. } else {
  3159. ret_val = YAFFS_OK;
  3160. }
  3161. return ret_val;
  3162. }
  3163. /* yaffs_del_file deletes the whole file data
  3164. * and the inode associated with the file.
  3165. * It does not delete the links associated with the file.
  3166. */
  3167. static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
  3168. {
  3169. int ret_val;
  3170. int del_now = 0;
  3171. struct yaffs_dev *dev = in->my_dev;
  3172. if (!in->my_inode)
  3173. del_now = 1;
  3174. if (del_now) {
  3175. ret_val =
  3176. yaffs_change_obj_name(in, in->my_dev->del_dir,
  3177. _Y("deleted"), 0, 0);
  3178. yaffs_trace(YAFFS_TRACE_TRACING,
  3179. "yaffs: immediate deletion of file %d",
  3180. in->obj_id);
  3181. in->deleted = 1;
  3182. in->my_dev->n_deleted_files++;
  3183. if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  3184. yaffs_resize_file(in, 0);
  3185. yaffs_soft_del_file(in);
  3186. } else {
  3187. ret_val =
  3188. yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
  3189. _Y("unlinked"), 0, 0);
  3190. }
  3191. return ret_val;
  3192. }
  3193. int yaffs_del_file(struct yaffs_obj *in)
  3194. {
  3195. int ret_val = YAFFS_OK;
  3196. int deleted; /* Need to cache value on stack if in is freed */
  3197. struct yaffs_dev *dev = in->my_dev;
  3198. if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  3199. yaffs_resize_file(in, 0);
  3200. if (in->n_data_chunks > 0) {
  3201. /* Use soft deletion if there is data in the file.
  3202. * That won't be the case if it has been resized to zero.
  3203. */
  3204. if (!in->unlinked)
  3205. ret_val = yaffs_unlink_file_if_needed(in);
  3206. deleted = in->deleted;
  3207. if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
  3208. in->deleted = 1;
  3209. deleted = 1;
  3210. in->my_dev->n_deleted_files++;
  3211. yaffs_soft_del_file(in);
  3212. }
  3213. return deleted ? YAFFS_OK : YAFFS_FAIL;
  3214. } else {
  3215. /* The file has no data chunks so we toss it immediately */
  3216. yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
  3217. in->variant.file_variant.top = NULL;
  3218. yaffs_generic_obj_del(in);
  3219. return YAFFS_OK;
  3220. }
  3221. }
  3222. int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
  3223. {
  3224. return (obj &&
  3225. obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
  3226. !(list_empty(&obj->variant.dir_variant.children));
  3227. }
  3228. static int yaffs_del_dir(struct yaffs_obj *obj)
  3229. {
  3230. /* First check that the directory is empty. */
  3231. if (yaffs_is_non_empty_dir(obj))
  3232. return YAFFS_FAIL;
  3233. return yaffs_generic_obj_del(obj);
  3234. }
  3235. static int yaffs_del_symlink(struct yaffs_obj *in)
  3236. {
  3237. if (in->variant.symlink_variant.alias)
  3238. kfree(in->variant.symlink_variant.alias);
  3239. in->variant.symlink_variant.alias = NULL;
  3240. return yaffs_generic_obj_del(in);
  3241. }
  3242. static int yaffs_del_link(struct yaffs_obj *in)
  3243. {
  3244. /* remove this hardlink from the list assocaited with the equivalent
  3245. * object
  3246. */
  3247. list_del_init(&in->hard_links);
  3248. return yaffs_generic_obj_del(in);
  3249. }
  3250. int yaffs_del_obj(struct yaffs_obj *obj)
  3251. {
  3252. int ret_val = -1;
  3253. switch (obj->variant_type) {
  3254. case YAFFS_OBJECT_TYPE_FILE:
  3255. ret_val = yaffs_del_file(obj);
  3256. break;
  3257. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3258. if (!list_empty(&obj->variant.dir_variant.dirty)) {
  3259. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  3260. "Remove object %d from dirty directories",
  3261. obj->obj_id);
  3262. list_del_init(&obj->variant.dir_variant.dirty);
  3263. }
  3264. return yaffs_del_dir(obj);
  3265. break;
  3266. case YAFFS_OBJECT_TYPE_SYMLINK:
  3267. ret_val = yaffs_del_symlink(obj);
  3268. break;
  3269. case YAFFS_OBJECT_TYPE_HARDLINK:
  3270. ret_val = yaffs_del_link(obj);
  3271. break;
  3272. case YAFFS_OBJECT_TYPE_SPECIAL:
  3273. ret_val = yaffs_generic_obj_del(obj);
  3274. break;
  3275. case YAFFS_OBJECT_TYPE_UNKNOWN:
  3276. ret_val = 0;
  3277. break; /* should not happen. */
  3278. }
  3279. return ret_val;
  3280. }
  3281. static int yaffs_unlink_worker(struct yaffs_obj *obj)
  3282. {
  3283. int del_now = 0;
  3284. if (!obj->my_inode)
  3285. del_now = 1;
  3286. if (obj)
  3287. yaffs_update_parent(obj->parent);
  3288. if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  3289. return yaffs_del_link(obj);
  3290. } else if (!list_empty(&obj->hard_links)) {
  3291. /* Curve ball: We're unlinking an object that has a hardlink.
  3292. *
  3293. * This problem arises because we are not strictly following
  3294. * The Linux link/inode model.
  3295. *
  3296. * We can't really delete the object.
  3297. * Instead, we do the following:
  3298. * - Select a hardlink.
  3299. * - Unhook it from the hard links
  3300. * - Move it from its parent directory (so that the rename can work)
  3301. * - Rename the object to the hardlink's name.
  3302. * - Delete the hardlink
  3303. */
  3304. struct yaffs_obj *hl;
  3305. struct yaffs_obj *parent;
  3306. int ret_val;
  3307. YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  3308. hl = list_entry(obj->hard_links.next, struct yaffs_obj,
  3309. hard_links);
  3310. yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  3311. parent = hl->parent;
  3312. list_del_init(&hl->hard_links);
  3313. yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
  3314. ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
  3315. if (ret_val == YAFFS_OK)
  3316. ret_val = yaffs_generic_obj_del(hl);
  3317. return ret_val;
  3318. } else if (del_now) {
  3319. switch (obj->variant_type) {
  3320. case YAFFS_OBJECT_TYPE_FILE:
  3321. return yaffs_del_file(obj);
  3322. break;
  3323. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3324. list_del_init(&obj->variant.dir_variant.dirty);
  3325. return yaffs_del_dir(obj);
  3326. break;
  3327. case YAFFS_OBJECT_TYPE_SYMLINK:
  3328. return yaffs_del_symlink(obj);
  3329. break;
  3330. case YAFFS_OBJECT_TYPE_SPECIAL:
  3331. return yaffs_generic_obj_del(obj);
  3332. break;
  3333. case YAFFS_OBJECT_TYPE_HARDLINK:
  3334. case YAFFS_OBJECT_TYPE_UNKNOWN:
  3335. default:
  3336. return YAFFS_FAIL;
  3337. }
  3338. } else if (yaffs_is_non_empty_dir(obj)) {
  3339. return YAFFS_FAIL;
  3340. } else {
  3341. return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
  3342. _Y("unlinked"), 0, 0);
  3343. }
  3344. }
  3345. static int yaffs_unlink_obj(struct yaffs_obj *obj)
  3346. {
  3347. if (obj && obj->unlink_allowed)
  3348. return yaffs_unlink_worker(obj);
  3349. return YAFFS_FAIL;
  3350. }
  3351. int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name)
  3352. {
  3353. struct yaffs_obj *obj;
  3354. obj = yaffs_find_by_name(dir, name);
  3355. return yaffs_unlink_obj(obj);
  3356. }
  3357. /* Note:
  3358. * If old_name is NULL then we take old_dir as the object to be renamed.
  3359. */
  3360. int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  3361. struct yaffs_obj *new_dir, const YCHAR * new_name)
  3362. {
  3363. struct yaffs_obj *obj = NULL;
  3364. struct yaffs_obj *existing_target = NULL;
  3365. int force = 0;
  3366. int result;
  3367. struct yaffs_dev *dev;
  3368. if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3369. YBUG();
  3370. if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3371. YBUG();
  3372. dev = old_dir->my_dev;
  3373. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  3374. /* Special case for case insemsitive systems.
  3375. * While look-up is case insensitive, the name isn't.
  3376. * Therefore we might want to change x.txt to X.txt
  3377. */
  3378. if (old_dir == new_dir &&
  3379. old_name && new_name &&
  3380. strcmp(old_name, new_name) == 0)
  3381. force = 1;
  3382. #endif
  3383. if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
  3384. YAFFS_MAX_NAME_LENGTH)
  3385. /* ENAMETOOLONG */
  3386. return YAFFS_FAIL;
  3387. if(old_name)
  3388. obj = yaffs_find_by_name(old_dir, old_name);
  3389. else{
  3390. obj = old_dir;
  3391. old_dir = obj->parent;
  3392. }
  3393. if (obj && obj->rename_allowed) {
  3394. /* Now do the handling for an existing target, if there is one */
  3395. existing_target = yaffs_find_by_name(new_dir, new_name);
  3396. if (yaffs_is_non_empty_dir(existing_target)){
  3397. return YAFFS_FAIL; /* ENOTEMPTY */
  3398. } else if (existing_target && existing_target != obj) {
  3399. /* Nuke the target first, using shadowing,
  3400. * but only if it isn't the same object.
  3401. *
  3402. * Note we must disable gc otherwise it can mess up the shadowing.
  3403. *
  3404. */
  3405. dev->gc_disable = 1;
  3406. yaffs_change_obj_name(obj, new_dir, new_name, force,
  3407. existing_target->obj_id);
  3408. existing_target->is_shadowed = 1;
  3409. yaffs_unlink_obj(existing_target);
  3410. dev->gc_disable = 0;
  3411. }
  3412. result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
  3413. yaffs_update_parent(old_dir);
  3414. if (new_dir != old_dir)
  3415. yaffs_update_parent(new_dir);
  3416. return result;
  3417. }
  3418. return YAFFS_FAIL;
  3419. }
  3420. /*----------------------- Initialisation Scanning ---------------------- */
  3421. void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  3422. int backward_scanning)
  3423. {
  3424. struct yaffs_obj *obj;
  3425. if (!backward_scanning) {
  3426. /* Handle YAFFS1 forward scanning case
  3427. * For YAFFS1 we always do the deletion
  3428. */
  3429. } else {
  3430. /* Handle YAFFS2 case (backward scanning)
  3431. * If the shadowed object exists then ignore.
  3432. */
  3433. obj = yaffs_find_by_number(dev, obj_id);
  3434. if (obj)
  3435. return;
  3436. }
  3437. /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
  3438. * We put it in unlinked dir to be cleaned up after the scanning
  3439. */
  3440. obj =
  3441. yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
  3442. if (!obj)
  3443. return;
  3444. obj->is_shadowed = 1;
  3445. yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
  3446. obj->variant.file_variant.shrink_size = 0;
  3447. obj->valid = 1; /* So that we don't read any other info for this file */
  3448. }
  3449. void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list)
  3450. {
  3451. struct yaffs_obj *hl;
  3452. struct yaffs_obj *in;
  3453. while (hard_list) {
  3454. hl = hard_list;
  3455. hard_list = (struct yaffs_obj *)(hard_list->hard_links.next);
  3456. in = yaffs_find_by_number(dev,
  3457. hl->variant.
  3458. hardlink_variant.equiv_id);
  3459. if (in) {
  3460. /* Add the hardlink pointers */
  3461. hl->variant.hardlink_variant.equiv_obj = in;
  3462. list_add(&hl->hard_links, &in->hard_links);
  3463. } else {
  3464. /* Todo Need to report/handle this better.
  3465. * Got a problem... hardlink to a non-existant object
  3466. */
  3467. hl->variant.hardlink_variant.equiv_obj = NULL;
  3468. INIT_LIST_HEAD(&hl->hard_links);
  3469. }
  3470. }
  3471. }
  3472. static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
  3473. {
  3474. /*
  3475. * Sort out state of unlinked and deleted objects after scanning.
  3476. */
  3477. struct list_head *i;
  3478. struct list_head *n;
  3479. struct yaffs_obj *l;
  3480. if (dev->read_only)
  3481. return;
  3482. /* Soft delete all the unlinked files */
  3483. list_for_each_safe(i, n,
  3484. &dev->unlinked_dir->variant.dir_variant.children) {
  3485. if (i) {
  3486. l = list_entry(i, struct yaffs_obj, siblings);
  3487. yaffs_del_obj(l);
  3488. }
  3489. }
  3490. list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
  3491. if (i) {
  3492. l = list_entry(i, struct yaffs_obj, siblings);
  3493. yaffs_del_obj(l);
  3494. }
  3495. }
  3496. }
  3497. /*
  3498. * This code iterates through all the objects making sure that they are rooted.
  3499. * Any unrooted objects are re-rooted in lost+found.
  3500. * An object needs to be in one of:
  3501. * - Directly under deleted, unlinked
  3502. * - Directly or indirectly under root.
  3503. *
  3504. * Note:
  3505. * This code assumes that we don't ever change the current relationships between
  3506. * directories:
  3507. * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
  3508. * lost-n-found->parent == root_dir
  3509. *
  3510. * This fixes the problem where directories might have inadvertently been deleted
  3511. * leaving the object "hanging" without being rooted in the directory tree.
  3512. */
  3513. static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
  3514. {
  3515. return (obj == dev->del_dir ||
  3516. obj == dev->unlinked_dir || obj == dev->root_dir);
  3517. }
  3518. static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
  3519. {
  3520. struct yaffs_obj *obj;
  3521. struct yaffs_obj *parent;
  3522. int i;
  3523. struct list_head *lh;
  3524. struct list_head *n;
  3525. int depth_limit;
  3526. int hanging;
  3527. if (dev->read_only)
  3528. return;
  3529. /* Iterate through the objects in each hash entry,
  3530. * looking at each object.
  3531. * Make sure it is rooted.
  3532. */
  3533. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  3534. list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
  3535. if (lh) {
  3536. obj =
  3537. list_entry(lh, struct yaffs_obj, hash_link);
  3538. parent = obj->parent;
  3539. if (yaffs_has_null_parent(dev, obj)) {
  3540. /* These directories are not hanging */
  3541. hanging = 0;
  3542. } else if (!parent
  3543. || parent->variant_type !=
  3544. YAFFS_OBJECT_TYPE_DIRECTORY) {
  3545. hanging = 1;
  3546. } else if (yaffs_has_null_parent(dev, parent)) {
  3547. hanging = 0;
  3548. } else {
  3549. /*
  3550. * Need to follow the parent chain to see if it is hanging.
  3551. */
  3552. hanging = 0;
  3553. depth_limit = 100;
  3554. while (parent != dev->root_dir &&
  3555. parent->parent &&
  3556. parent->parent->variant_type ==
  3557. YAFFS_OBJECT_TYPE_DIRECTORY
  3558. && depth_limit > 0) {
  3559. parent = parent->parent;
  3560. depth_limit--;
  3561. }
  3562. if (parent != dev->root_dir)
  3563. hanging = 1;
  3564. }
  3565. if (hanging) {
  3566. yaffs_trace(YAFFS_TRACE_SCAN,
  3567. "Hanging object %d moved to lost and found",
  3568. obj->obj_id);
  3569. yaffs_add_obj_to_dir(dev->lost_n_found,
  3570. obj);
  3571. }
  3572. }
  3573. }
  3574. }
  3575. }
  3576. /*
  3577. * Delete directory contents for cleaning up lost and found.
  3578. */
  3579. static void yaffs_del_dir_contents(struct yaffs_obj *dir)
  3580. {
  3581. struct yaffs_obj *obj;
  3582. struct list_head *lh;
  3583. struct list_head *n;
  3584. if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3585. YBUG();
  3586. list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
  3587. if (lh) {
  3588. obj = list_entry(lh, struct yaffs_obj, siblings);
  3589. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  3590. yaffs_del_dir_contents(obj);
  3591. yaffs_trace(YAFFS_TRACE_SCAN,
  3592. "Deleting lost_found object %d",
  3593. obj->obj_id);
  3594. /* Need to use UnlinkObject since Delete would not handle
  3595. * hardlinked objects correctly.
  3596. */
  3597. yaffs_unlink_obj(obj);
  3598. }
  3599. }
  3600. }
  3601. static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
  3602. {
  3603. yaffs_del_dir_contents(dev->lost_n_found);
  3604. }
  3605. struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
  3606. const YCHAR * name)
  3607. {
  3608. int sum;
  3609. struct list_head *i;
  3610. YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  3611. struct yaffs_obj *l;
  3612. if (!name)
  3613. return NULL;
  3614. if (!directory) {
  3615. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3616. "tragedy: yaffs_find_by_name: null pointer directory"
  3617. );
  3618. YBUG();
  3619. return NULL;
  3620. }
  3621. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3622. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3623. "tragedy: yaffs_find_by_name: non-directory"
  3624. );
  3625. YBUG();
  3626. }
  3627. sum = yaffs_calc_name_sum(name);
  3628. list_for_each(i, &directory->variant.dir_variant.children) {
  3629. if (i) {
  3630. l = list_entry(i, struct yaffs_obj, siblings);
  3631. if (l->parent != directory)
  3632. YBUG();
  3633. yaffs_check_obj_details_loaded(l);
  3634. /* Special case for lost-n-found */
  3635. if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  3636. if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
  3637. return l;
  3638. } else if (l->sum == sum
  3639. || l->hdr_chunk <= 0) {
  3640. /* LostnFound chunk called Objxxx
  3641. * Do a real check
  3642. */
  3643. yaffs_get_obj_name(l, buffer,
  3644. YAFFS_MAX_NAME_LENGTH + 1);
  3645. if (strncmp
  3646. (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
  3647. return l;
  3648. }
  3649. }
  3650. }
  3651. return NULL;
  3652. }
  3653. /* GetEquivalentObject dereferences any hard links to get to the
  3654. * actual object.
  3655. */
  3656. struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
  3657. {
  3658. if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  3659. /* We want the object id of the equivalent object, not this one */
  3660. obj = obj->variant.hardlink_variant.equiv_obj;
  3661. yaffs_check_obj_details_loaded(obj);
  3662. }
  3663. return obj;
  3664. }
  3665. /*
  3666. * A note or two on object names.
  3667. * * If the object name is missing, we then make one up in the form objnnn
  3668. *
  3669. * * ASCII names are stored in the object header's name field from byte zero
  3670. * * Unicode names are historically stored starting from byte zero.
  3671. *
  3672. * Then there are automatic Unicode names...
  3673. * The purpose of these is to save names in a way that can be read as
  3674. * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
  3675. * system to share files.
  3676. *
  3677. * These automatic unicode are stored slightly differently...
  3678. * - If the name can fit in the ASCII character space then they are saved as
  3679. * ascii names as per above.
  3680. * - If the name needs Unicode then the name is saved in Unicode
  3681. * starting at oh->name[1].
  3682. */
  3683. static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name,
  3684. int buffer_size)
  3685. {
  3686. /* Create an object name if we could not find one. */
  3687. if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
  3688. YCHAR local_name[20];
  3689. YCHAR num_string[20];
  3690. YCHAR *x = &num_string[19];
  3691. unsigned v = obj->obj_id;
  3692. num_string[19] = 0;
  3693. while (v > 0) {
  3694. x--;
  3695. *x = '0' + (v % 10);
  3696. v /= 10;
  3697. }
  3698. /* make up a name */
  3699. strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
  3700. strcat(local_name, x);
  3701. strncpy(name, local_name, buffer_size - 1);
  3702. }
  3703. }
  3704. int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size)
  3705. {
  3706. memset(name, 0, buffer_size * sizeof(YCHAR));
  3707. yaffs_check_obj_details_loaded(obj);
  3708. if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  3709. strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
  3710. }
  3711. #ifndef CONFIG_YAFFS_NO_SHORT_NAMES
  3712. else if (obj->short_name[0]) {
  3713. strcpy(name, obj->short_name);
  3714. }
  3715. #endif
  3716. else if (obj->hdr_chunk > 0) {
  3717. int result;
  3718. u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
  3719. struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
  3720. memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
  3721. if (obj->hdr_chunk > 0) {
  3722. result = yaffs_rd_chunk_tags_nand(obj->my_dev,
  3723. obj->hdr_chunk,
  3724. buffer, NULL);
  3725. }
  3726. yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
  3727. buffer_size);
  3728. yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
  3729. }
  3730. yaffs_fix_null_name(obj, name, buffer_size);
  3731. return strnlen(name, YAFFS_MAX_NAME_LENGTH);
  3732. }
  3733. int yaffs_get_obj_length(struct yaffs_obj *obj)
  3734. {
  3735. /* Dereference any hard linking */
  3736. obj = yaffs_get_equivalent_obj(obj);
  3737. if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  3738. return obj->variant.file_variant.file_size;
  3739. if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  3740. if (!obj->variant.symlink_variant.alias)
  3741. return 0;
  3742. return strnlen(obj->variant.symlink_variant.alias,
  3743. YAFFS_MAX_ALIAS_LENGTH);
  3744. } else {
  3745. /* Only a directory should drop through to here */
  3746. return obj->my_dev->data_bytes_per_chunk;
  3747. }
  3748. }
  3749. int yaffs_get_obj_link_count(struct yaffs_obj *obj)
  3750. {
  3751. int count = 0;
  3752. struct list_head *i;
  3753. if (!obj->unlinked)
  3754. count++; /* the object itself */
  3755. list_for_each(i, &obj->hard_links)
  3756. count++; /* add the hard links; */
  3757. return count;
  3758. }
  3759. int yaffs_get_obj_inode(struct yaffs_obj *obj)
  3760. {
  3761. obj = yaffs_get_equivalent_obj(obj);
  3762. return obj->obj_id;
  3763. }
  3764. unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
  3765. {
  3766. obj = yaffs_get_equivalent_obj(obj);
  3767. switch (obj->variant_type) {
  3768. case YAFFS_OBJECT_TYPE_FILE:
  3769. return DT_REG;
  3770. break;
  3771. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3772. return DT_DIR;
  3773. break;
  3774. case YAFFS_OBJECT_TYPE_SYMLINK:
  3775. return DT_LNK;
  3776. break;
  3777. case YAFFS_OBJECT_TYPE_HARDLINK:
  3778. return DT_REG;
  3779. break;
  3780. case YAFFS_OBJECT_TYPE_SPECIAL:
  3781. if (S_ISFIFO(obj->yst_mode))
  3782. return DT_FIFO;
  3783. if (S_ISCHR(obj->yst_mode))
  3784. return DT_CHR;
  3785. if (S_ISBLK(obj->yst_mode))
  3786. return DT_BLK;
  3787. if (S_ISSOCK(obj->yst_mode))
  3788. return DT_SOCK;
  3789. default:
  3790. return DT_REG;
  3791. break;
  3792. }
  3793. }
  3794. YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
  3795. {
  3796. obj = yaffs_get_equivalent_obj(obj);
  3797. if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  3798. return yaffs_clone_str(obj->variant.symlink_variant.alias);
  3799. else
  3800. return yaffs_clone_str(_Y(""));
  3801. }
  3802. /*--------------------------- Initialisation code -------------------------- */
  3803. static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
  3804. {
  3805. /* Common functions, gotta have */
  3806. if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
  3807. return 0;
  3808. #ifdef CONFIG_YAFFS_YAFFS2
  3809. /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  3810. if (dev->param.write_chunk_tags_fn &&
  3811. dev->param.read_chunk_tags_fn &&
  3812. !dev->param.write_chunk_fn &&
  3813. !dev->param.read_chunk_fn &&
  3814. dev->param.bad_block_fn && dev->param.query_block_fn)
  3815. return 1;
  3816. #endif
  3817. /* Can use the "spare" style interface for yaffs1 */
  3818. if (!dev->param.is_yaffs2 &&
  3819. !dev->param.write_chunk_tags_fn &&
  3820. !dev->param.read_chunk_tags_fn &&
  3821. dev->param.write_chunk_fn &&
  3822. dev->param.read_chunk_fn &&
  3823. !dev->param.bad_block_fn && !dev->param.query_block_fn)
  3824. return 1;
  3825. return 0; /* bad */
  3826. }
  3827. static int yaffs_create_initial_dir(struct yaffs_dev *dev)
  3828. {
  3829. /* Initialise the unlinked, deleted, root and lost and found directories */
  3830. dev->lost_n_found = dev->root_dir = NULL;
  3831. dev->unlinked_dir = dev->del_dir = NULL;
  3832. dev->unlinked_dir =
  3833. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  3834. dev->del_dir =
  3835. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  3836. dev->root_dir =
  3837. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
  3838. YAFFS_ROOT_MODE | S_IFDIR);
  3839. dev->lost_n_found =
  3840. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
  3841. YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  3842. if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
  3843. && dev->del_dir) {
  3844. yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
  3845. return YAFFS_OK;
  3846. }
  3847. return YAFFS_FAIL;
  3848. }
  3849. int yaffs_guts_initialise(struct yaffs_dev *dev)
  3850. {
  3851. int init_failed = 0;
  3852. unsigned x;
  3853. int bits;
  3854. yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" );
  3855. /* Check stuff that must be set */
  3856. if (!dev) {
  3857. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3858. "yaffs: Need a device"
  3859. );
  3860. return YAFFS_FAIL;
  3861. }
  3862. dev->internal_start_block = dev->param.start_block;
  3863. dev->internal_end_block = dev->param.end_block;
  3864. dev->block_offset = 0;
  3865. dev->chunk_offset = 0;
  3866. dev->n_free_chunks = 0;
  3867. dev->gc_block = 0;
  3868. if (dev->param.start_block == 0) {
  3869. dev->internal_start_block = dev->param.start_block + 1;
  3870. dev->internal_end_block = dev->param.end_block + 1;
  3871. dev->block_offset = 1;
  3872. dev->chunk_offset = dev->param.chunks_per_block;
  3873. }
  3874. /* Check geometry parameters. */
  3875. if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
  3876. dev->param.total_bytes_per_chunk < 1024) ||
  3877. (!dev->param.is_yaffs2 &&
  3878. dev->param.total_bytes_per_chunk < 512) ||
  3879. (dev->param.inband_tags && !dev->param.is_yaffs2) ||
  3880. dev->param.chunks_per_block < 2 ||
  3881. dev->param.n_reserved_blocks < 2 ||
  3882. dev->internal_start_block <= 0 ||
  3883. dev->internal_end_block <= 0 ||
  3884. dev->internal_end_block <=
  3885. (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
  3886. ) {
  3887. /* otherwise it is too small */
  3888. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3889. "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
  3890. dev->param.total_bytes_per_chunk,
  3891. dev->param.is_yaffs2 ? "2" : "",
  3892. dev->param.inband_tags);
  3893. return YAFFS_FAIL;
  3894. }
  3895. if (yaffs_init_nand(dev) != YAFFS_OK) {
  3896. yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
  3897. return YAFFS_FAIL;
  3898. }
  3899. /* Sort out space for inband tags, if required */
  3900. if (dev->param.inband_tags)
  3901. dev->data_bytes_per_chunk =
  3902. dev->param.total_bytes_per_chunk -
  3903. sizeof(struct yaffs_packed_tags2_tags_only);
  3904. else
  3905. dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
  3906. /* Got the right mix of functions? */
  3907. if (!yaffs_check_dev_fns(dev)) {
  3908. /* Function missing */
  3909. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3910. "device function(s) missing or wrong");
  3911. return YAFFS_FAIL;
  3912. }
  3913. if (dev->is_mounted) {
  3914. yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
  3915. return YAFFS_FAIL;
  3916. }
  3917. /* Finished with most checks. One or two more checks happen later on too. */
  3918. dev->is_mounted = 1;
  3919. /* OK now calculate a few things for the device */
  3920. /*
  3921. * Calculate all the chunk size manipulation numbers:
  3922. */
  3923. x = dev->data_bytes_per_chunk;
  3924. /* We always use dev->chunk_shift and dev->chunk_div */
  3925. dev->chunk_shift = calc_shifts(x);
  3926. x >>= dev->chunk_shift;
  3927. dev->chunk_div = x;
  3928. /* We only use chunk mask if chunk_div is 1 */
  3929. dev->chunk_mask = (1 << dev->chunk_shift) - 1;
  3930. /*
  3931. * Calculate chunk_grp_bits.
  3932. * We need to find the next power of 2 > than internal_end_block
  3933. */
  3934. x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
  3935. bits = calc_shifts_ceiling(x);
  3936. /* Set up tnode width if wide tnodes are enabled. */
  3937. if (!dev->param.wide_tnodes_disabled) {
  3938. /* bits must be even so that we end up with 32-bit words */
  3939. if (bits & 1)
  3940. bits++;
  3941. if (bits < 16)
  3942. dev->tnode_width = 16;
  3943. else
  3944. dev->tnode_width = bits;
  3945. } else {
  3946. dev->tnode_width = 16;
  3947. }
  3948. dev->tnode_mask = (1 << dev->tnode_width) - 1;
  3949. /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  3950. * so if the bitwidth of the
  3951. * chunk range we're using is greater than 16 we need
  3952. * to figure out chunk shift and chunk_grp_size
  3953. */
  3954. if (bits <= dev->tnode_width)
  3955. dev->chunk_grp_bits = 0;
  3956. else
  3957. dev->chunk_grp_bits = bits - dev->tnode_width;
  3958. dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
  3959. if (dev->tnode_size < sizeof(struct yaffs_tnode))
  3960. dev->tnode_size = sizeof(struct yaffs_tnode);
  3961. dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
  3962. if (dev->param.chunks_per_block < dev->chunk_grp_size) {
  3963. /* We have a problem because the soft delete won't work if
  3964. * the chunk group size > chunks per block.
  3965. * This can be remedied by using larger "virtual blocks".
  3966. */
  3967. yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
  3968. return YAFFS_FAIL;
  3969. }
  3970. /* OK, we've finished verifying the device, lets continue with initialisation */
  3971. /* More device initialisation */
  3972. dev->all_gcs = 0;
  3973. dev->passive_gc_count = 0;
  3974. dev->oldest_dirty_gc_count = 0;
  3975. dev->bg_gcs = 0;
  3976. dev->gc_block_finder = 0;
  3977. dev->buffered_block = -1;
  3978. dev->doing_buffered_block_rewrite = 0;
  3979. dev->n_deleted_files = 0;
  3980. dev->n_bg_deletions = 0;
  3981. dev->n_unlinked_files = 0;
  3982. dev->n_ecc_fixed = 0;
  3983. dev->n_ecc_unfixed = 0;
  3984. dev->n_tags_ecc_fixed = 0;
  3985. dev->n_tags_ecc_unfixed = 0;
  3986. dev->n_erase_failures = 0;
  3987. dev->n_erased_blocks = 0;
  3988. dev->gc_disable = 0;
  3989. dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
  3990. INIT_LIST_HEAD(&dev->dirty_dirs);
  3991. dev->oldest_dirty_seq = 0;
  3992. dev->oldest_dirty_block = 0;
  3993. /* Initialise temporary buffers and caches. */
  3994. if (!yaffs_init_tmp_buffers(dev))
  3995. init_failed = 1;
  3996. dev->cache = NULL;
  3997. dev->gc_cleanup_list = NULL;
  3998. if (!init_failed && dev->param.n_caches > 0) {
  3999. int i;
  4000. void *buf;
  4001. int cache_bytes =
  4002. dev->param.n_caches * sizeof(struct yaffs_cache);
  4003. if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
  4004. dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
  4005. dev->cache = kmalloc(cache_bytes, GFP_NOFS);
  4006. buf = (u8 *) dev->cache;
  4007. if (dev->cache)
  4008. memset(dev->cache, 0, cache_bytes);
  4009. for (i = 0; i < dev->param.n_caches && buf; i++) {
  4010. dev->cache[i].object = NULL;
  4011. dev->cache[i].last_use = 0;
  4012. dev->cache[i].dirty = 0;
  4013. dev->cache[i].data = buf =
  4014. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  4015. }
  4016. if (!buf)
  4017. init_failed = 1;
  4018. dev->cache_last_use = 0;
  4019. }
  4020. dev->cache_hits = 0;
  4021. if (!init_failed) {
  4022. dev->gc_cleanup_list =
  4023. kmalloc(dev->param.chunks_per_block * sizeof(u32),
  4024. GFP_NOFS);
  4025. if (!dev->gc_cleanup_list)
  4026. init_failed = 1;
  4027. }
  4028. if (dev->param.is_yaffs2)
  4029. dev->param.use_header_file_size = 1;
  4030. if (!init_failed && !yaffs_init_blocks(dev))
  4031. init_failed = 1;
  4032. yaffs_init_tnodes_and_objs(dev);
  4033. if (!init_failed && !yaffs_create_initial_dir(dev))
  4034. init_failed = 1;
  4035. if (!init_failed) {
  4036. /* Now scan the flash. */
  4037. if (dev->param.is_yaffs2) {
  4038. if (yaffs2_checkpt_restore(dev)) {
  4039. yaffs_check_obj_details_loaded(dev->root_dir);
  4040. yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  4041. "yaffs: restored from checkpoint"
  4042. );
  4043. } else {
  4044. /* Clean up the mess caused by an aborted checkpoint load
  4045. * and scan backwards.
  4046. */
  4047. yaffs_deinit_blocks(dev);
  4048. yaffs_deinit_tnodes_and_objs(dev);
  4049. dev->n_erased_blocks = 0;
  4050. dev->n_free_chunks = 0;
  4051. dev->alloc_block = -1;
  4052. dev->alloc_page = -1;
  4053. dev->n_deleted_files = 0;
  4054. dev->n_unlinked_files = 0;
  4055. dev->n_bg_deletions = 0;
  4056. if (!init_failed && !yaffs_init_blocks(dev))
  4057. init_failed = 1;
  4058. yaffs_init_tnodes_and_objs(dev);
  4059. if (!init_failed
  4060. && !yaffs_create_initial_dir(dev))
  4061. init_failed = 1;
  4062. if (!init_failed && !yaffs2_scan_backwards(dev))
  4063. init_failed = 1;
  4064. }
  4065. } else if (!yaffs1_scan(dev)) {
  4066. init_failed = 1;
  4067. }
  4068. yaffs_strip_deleted_objs(dev);
  4069. yaffs_fix_hanging_objs(dev);
  4070. if (dev->param.empty_lost_n_found)
  4071. yaffs_empty_l_n_f(dev);
  4072. }
  4073. if (init_failed) {
  4074. /* Clean up the mess */
  4075. yaffs_trace(YAFFS_TRACE_TRACING,
  4076. "yaffs: yaffs_guts_initialise() aborted.");
  4077. yaffs_deinitialise(dev);
  4078. return YAFFS_FAIL;
  4079. }
  4080. /* Zero out stats */
  4081. dev->n_page_reads = 0;
  4082. dev->n_page_writes = 0;
  4083. dev->n_erasures = 0;
  4084. dev->n_gc_copies = 0;
  4085. dev->n_retired_writes = 0;
  4086. dev->n_retired_blocks = 0;
  4087. yaffs_verify_free_chunks(dev);
  4088. yaffs_verify_blocks(dev);
  4089. /* Clean up any aborted checkpoint data */
  4090. if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
  4091. yaffs2_checkpt_invalidate(dev);
  4092. yaffs_trace(YAFFS_TRACE_TRACING,
  4093. "yaffs: yaffs_guts_initialise() done.");
  4094. return YAFFS_OK;
  4095. }
  4096. void yaffs_deinitialise(struct yaffs_dev *dev)
  4097. {
  4098. if (dev->is_mounted) {
  4099. int i;
  4100. yaffs_deinit_blocks(dev);
  4101. yaffs_deinit_tnodes_and_objs(dev);
  4102. if (dev->param.n_caches > 0 && dev->cache) {
  4103. for (i = 0; i < dev->param.n_caches; i++) {
  4104. if (dev->cache[i].data)
  4105. kfree(dev->cache[i].data);
  4106. dev->cache[i].data = NULL;
  4107. }
  4108. kfree(dev->cache);
  4109. dev->cache = NULL;
  4110. }
  4111. kfree(dev->gc_cleanup_list);
  4112. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  4113. kfree(dev->temp_buffer[i].buffer);
  4114. dev->is_mounted = 0;
  4115. if (dev->param.deinitialise_flash_fn)
  4116. dev->param.deinitialise_flash_fn(dev);
  4117. }
  4118. }
  4119. int yaffs_count_free_chunks(struct yaffs_dev *dev)
  4120. {
  4121. int n_free = 0;
  4122. int b;
  4123. struct yaffs_block_info *blk;
  4124. blk = dev->block_info;
  4125. for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  4126. switch (blk->block_state) {
  4127. case YAFFS_BLOCK_STATE_EMPTY:
  4128. case YAFFS_BLOCK_STATE_ALLOCATING:
  4129. case YAFFS_BLOCK_STATE_COLLECTING:
  4130. case YAFFS_BLOCK_STATE_FULL:
  4131. n_free +=
  4132. (dev->param.chunks_per_block - blk->pages_in_use +
  4133. blk->soft_del_pages);
  4134. break;
  4135. default:
  4136. break;
  4137. }
  4138. blk++;
  4139. }
  4140. return n_free;
  4141. }
  4142. int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
  4143. {
  4144. /* This is what we report to the outside world */
  4145. int n_free;
  4146. int n_dirty_caches;
  4147. int blocks_for_checkpt;
  4148. int i;
  4149. n_free = dev->n_free_chunks;
  4150. n_free += dev->n_deleted_files;
  4151. /* Now count the number of dirty chunks in the cache and subtract those */
  4152. for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
  4153. if (dev->cache[i].dirty)
  4154. n_dirty_caches++;
  4155. }
  4156. n_free -= n_dirty_caches;
  4157. n_free -=
  4158. ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
  4159. /* Now we figure out how much to reserve for the checkpoint and report that... */
  4160. blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
  4161. n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
  4162. if (n_free < 0)
  4163. n_free = 0;
  4164. return n_free;
  4165. }