yaffs_guts.c 121 KB

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