yaffs_guts.c 120 KB

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