yaffs_guts.c 120 KB

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