yaffs_guts.c 123 KB

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