alloc.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * alloc.c
  5. *
  6. * Extent allocs and frees
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/swap.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "aops.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "localalloc.h"
  40. #include "suballoc.h"
  41. #include "sysfile.h"
  42. #include "file.h"
  43. #include "super.h"
  44. #include "uptodate.h"
  45. #include "buffer_head_io.h"
  46. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  47. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  48. struct ocfs2_extent_block *eb);
  49. /*
  50. * Structures which describe a path through a btree, and functions to
  51. * manipulate them.
  52. *
  53. * The idea here is to be as generic as possible with the tree
  54. * manipulation code.
  55. */
  56. struct ocfs2_path_item {
  57. struct buffer_head *bh;
  58. struct ocfs2_extent_list *el;
  59. };
  60. #define OCFS2_MAX_PATH_DEPTH 5
  61. struct ocfs2_path {
  62. int p_tree_depth;
  63. struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
  64. };
  65. #define path_root_bh(_path) ((_path)->p_node[0].bh)
  66. #define path_root_el(_path) ((_path)->p_node[0].el)
  67. #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
  68. #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
  69. #define path_num_items(_path) ((_path)->p_tree_depth + 1)
  70. /*
  71. * Reset the actual path elements so that we can re-use the structure
  72. * to build another path. Generally, this involves freeing the buffer
  73. * heads.
  74. */
  75. static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  76. {
  77. int i, start = 0, depth = 0;
  78. struct ocfs2_path_item *node;
  79. if (keep_root)
  80. start = 1;
  81. for(i = start; i < path_num_items(path); i++) {
  82. node = &path->p_node[i];
  83. brelse(node->bh);
  84. node->bh = NULL;
  85. node->el = NULL;
  86. }
  87. /*
  88. * Tree depth may change during truncate, or insert. If we're
  89. * keeping the root extent list, then make sure that our path
  90. * structure reflects the proper depth.
  91. */
  92. if (keep_root)
  93. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  94. path->p_tree_depth = depth;
  95. }
  96. static void ocfs2_free_path(struct ocfs2_path *path)
  97. {
  98. if (path) {
  99. ocfs2_reinit_path(path, 0);
  100. kfree(path);
  101. }
  102. }
  103. /*
  104. * All the elements of src into dest. After this call, src could be freed
  105. * without affecting dest.
  106. *
  107. * Both paths should have the same root. Any non-root elements of dest
  108. * will be freed.
  109. */
  110. static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  111. {
  112. int i;
  113. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  114. BUG_ON(path_root_el(dest) != path_root_el(src));
  115. ocfs2_reinit_path(dest, 1);
  116. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  117. dest->p_node[i].bh = src->p_node[i].bh;
  118. dest->p_node[i].el = src->p_node[i].el;
  119. if (dest->p_node[i].bh)
  120. get_bh(dest->p_node[i].bh);
  121. }
  122. }
  123. /*
  124. * Make the *dest path the same as src and re-initialize src path to
  125. * have a root only.
  126. */
  127. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  128. {
  129. int i;
  130. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  131. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  132. brelse(dest->p_node[i].bh);
  133. dest->p_node[i].bh = src->p_node[i].bh;
  134. dest->p_node[i].el = src->p_node[i].el;
  135. src->p_node[i].bh = NULL;
  136. src->p_node[i].el = NULL;
  137. }
  138. }
  139. /*
  140. * Insert an extent block at given index.
  141. *
  142. * This will not take an additional reference on eb_bh.
  143. */
  144. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  145. struct buffer_head *eb_bh)
  146. {
  147. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  148. /*
  149. * Right now, no root bh is an extent block, so this helps
  150. * catch code errors with dinode trees. The assertion can be
  151. * safely removed if we ever need to insert extent block
  152. * structures at the root.
  153. */
  154. BUG_ON(index == 0);
  155. path->p_node[index].bh = eb_bh;
  156. path->p_node[index].el = &eb->h_list;
  157. }
  158. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  159. struct ocfs2_extent_list *root_el)
  160. {
  161. struct ocfs2_path *path;
  162. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  163. path = kzalloc(sizeof(*path), GFP_NOFS);
  164. if (path) {
  165. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  166. get_bh(root_bh);
  167. path_root_bh(path) = root_bh;
  168. path_root_el(path) = root_el;
  169. }
  170. return path;
  171. }
  172. /*
  173. * Allocate and initialize a new path based on a disk inode tree.
  174. */
  175. static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
  176. {
  177. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  178. struct ocfs2_extent_list *el = &di->id2.i_list;
  179. return ocfs2_new_path(di_bh, el);
  180. }
  181. /*
  182. * Convenience function to journal all components in a path.
  183. */
  184. static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
  185. struct ocfs2_path *path)
  186. {
  187. int i, ret = 0;
  188. if (!path)
  189. goto out;
  190. for(i = 0; i < path_num_items(path); i++) {
  191. ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
  192. OCFS2_JOURNAL_ACCESS_WRITE);
  193. if (ret < 0) {
  194. mlog_errno(ret);
  195. goto out;
  196. }
  197. }
  198. out:
  199. return ret;
  200. }
  201. /*
  202. * Return the index of the extent record which contains cluster #v_cluster.
  203. * -1 is returned if it was not found.
  204. *
  205. * Should work fine on interior and exterior nodes.
  206. */
  207. int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
  208. {
  209. int ret = -1;
  210. int i;
  211. struct ocfs2_extent_rec *rec;
  212. u32 rec_end, rec_start, clusters;
  213. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  214. rec = &el->l_recs[i];
  215. rec_start = le32_to_cpu(rec->e_cpos);
  216. clusters = ocfs2_rec_clusters(el, rec);
  217. rec_end = rec_start + clusters;
  218. if (v_cluster >= rec_start && v_cluster < rec_end) {
  219. ret = i;
  220. break;
  221. }
  222. }
  223. return ret;
  224. }
  225. enum ocfs2_contig_type {
  226. CONTIG_NONE = 0,
  227. CONTIG_LEFT,
  228. CONTIG_RIGHT,
  229. CONTIG_LEFTRIGHT,
  230. };
  231. /*
  232. * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
  233. * ocfs2_extent_contig only work properly against leaf nodes!
  234. */
  235. static int ocfs2_block_extent_contig(struct super_block *sb,
  236. struct ocfs2_extent_rec *ext,
  237. u64 blkno)
  238. {
  239. u64 blk_end = le64_to_cpu(ext->e_blkno);
  240. blk_end += ocfs2_clusters_to_blocks(sb,
  241. le16_to_cpu(ext->e_leaf_clusters));
  242. return blkno == blk_end;
  243. }
  244. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  245. struct ocfs2_extent_rec *right)
  246. {
  247. u32 left_range;
  248. left_range = le32_to_cpu(left->e_cpos) +
  249. le16_to_cpu(left->e_leaf_clusters);
  250. return (left_range == le32_to_cpu(right->e_cpos));
  251. }
  252. static enum ocfs2_contig_type
  253. ocfs2_extent_contig(struct inode *inode,
  254. struct ocfs2_extent_rec *ext,
  255. struct ocfs2_extent_rec *insert_rec)
  256. {
  257. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  258. /*
  259. * Refuse to coalesce extent records with different flag
  260. * fields - we don't want to mix unwritten extents with user
  261. * data.
  262. */
  263. if (ext->e_flags != insert_rec->e_flags)
  264. return CONTIG_NONE;
  265. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  266. ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
  267. return CONTIG_RIGHT;
  268. blkno = le64_to_cpu(ext->e_blkno);
  269. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  270. ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
  271. return CONTIG_LEFT;
  272. return CONTIG_NONE;
  273. }
  274. /*
  275. * NOTE: We can have pretty much any combination of contiguousness and
  276. * appending.
  277. *
  278. * The usefulness of APPEND_TAIL is more in that it lets us know that
  279. * we'll have to update the path to that leaf.
  280. */
  281. enum ocfs2_append_type {
  282. APPEND_NONE = 0,
  283. APPEND_TAIL,
  284. };
  285. enum ocfs2_split_type {
  286. SPLIT_NONE = 0,
  287. SPLIT_LEFT,
  288. SPLIT_RIGHT,
  289. };
  290. struct ocfs2_insert_type {
  291. enum ocfs2_split_type ins_split;
  292. enum ocfs2_append_type ins_appending;
  293. enum ocfs2_contig_type ins_contig;
  294. int ins_contig_index;
  295. int ins_tree_depth;
  296. };
  297. struct ocfs2_merge_ctxt {
  298. enum ocfs2_contig_type c_contig_type;
  299. int c_has_empty_extent;
  300. int c_split_covers_rec;
  301. int c_used_tail_recs;
  302. };
  303. /*
  304. * How many free extents have we got before we need more meta data?
  305. */
  306. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  307. struct inode *inode,
  308. struct ocfs2_dinode *fe)
  309. {
  310. int retval;
  311. struct ocfs2_extent_list *el;
  312. struct ocfs2_extent_block *eb;
  313. struct buffer_head *eb_bh = NULL;
  314. mlog_entry_void();
  315. if (!OCFS2_IS_VALID_DINODE(fe)) {
  316. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  317. retval = -EIO;
  318. goto bail;
  319. }
  320. if (fe->i_last_eb_blk) {
  321. retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  322. &eb_bh, OCFS2_BH_CACHED, inode);
  323. if (retval < 0) {
  324. mlog_errno(retval);
  325. goto bail;
  326. }
  327. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  328. el = &eb->h_list;
  329. } else
  330. el = &fe->id2.i_list;
  331. BUG_ON(el->l_tree_depth != 0);
  332. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  333. bail:
  334. if (eb_bh)
  335. brelse(eb_bh);
  336. mlog_exit(retval);
  337. return retval;
  338. }
  339. /* expects array to already be allocated
  340. *
  341. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  342. * l_count for you
  343. */
  344. static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
  345. handle_t *handle,
  346. struct inode *inode,
  347. int wanted,
  348. struct ocfs2_alloc_context *meta_ac,
  349. struct buffer_head *bhs[])
  350. {
  351. int count, status, i;
  352. u16 suballoc_bit_start;
  353. u32 num_got;
  354. u64 first_blkno;
  355. struct ocfs2_extent_block *eb;
  356. mlog_entry_void();
  357. count = 0;
  358. while (count < wanted) {
  359. status = ocfs2_claim_metadata(osb,
  360. handle,
  361. meta_ac,
  362. wanted - count,
  363. &suballoc_bit_start,
  364. &num_got,
  365. &first_blkno);
  366. if (status < 0) {
  367. mlog_errno(status);
  368. goto bail;
  369. }
  370. for(i = count; i < (num_got + count); i++) {
  371. bhs[i] = sb_getblk(osb->sb, first_blkno);
  372. if (bhs[i] == NULL) {
  373. status = -EIO;
  374. mlog_errno(status);
  375. goto bail;
  376. }
  377. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  378. status = ocfs2_journal_access(handle, inode, bhs[i],
  379. OCFS2_JOURNAL_ACCESS_CREATE);
  380. if (status < 0) {
  381. mlog_errno(status);
  382. goto bail;
  383. }
  384. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  385. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  386. /* Ok, setup the minimal stuff here. */
  387. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  388. eb->h_blkno = cpu_to_le64(first_blkno);
  389. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  390. eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
  391. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  392. eb->h_list.l_count =
  393. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  394. suballoc_bit_start++;
  395. first_blkno++;
  396. /* We'll also be dirtied by the caller, so
  397. * this isn't absolutely necessary. */
  398. status = ocfs2_journal_dirty(handle, bhs[i]);
  399. if (status < 0) {
  400. mlog_errno(status);
  401. goto bail;
  402. }
  403. }
  404. count += num_got;
  405. }
  406. status = 0;
  407. bail:
  408. if (status < 0) {
  409. for(i = 0; i < wanted; i++) {
  410. if (bhs[i])
  411. brelse(bhs[i]);
  412. bhs[i] = NULL;
  413. }
  414. }
  415. mlog_exit(status);
  416. return status;
  417. }
  418. /*
  419. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  420. *
  421. * Returns the sum of the rightmost extent rec logical offset and
  422. * cluster count.
  423. *
  424. * ocfs2_add_branch() uses this to determine what logical cluster
  425. * value should be populated into the leftmost new branch records.
  426. *
  427. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  428. * value for the new topmost tree record.
  429. */
  430. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  431. {
  432. int i;
  433. i = le16_to_cpu(el->l_next_free_rec) - 1;
  434. return le32_to_cpu(el->l_recs[i].e_cpos) +
  435. ocfs2_rec_clusters(el, &el->l_recs[i]);
  436. }
  437. /*
  438. * Add an entire tree branch to our inode. eb_bh is the extent block
  439. * to start at, if we don't want to start the branch at the dinode
  440. * structure.
  441. *
  442. * last_eb_bh is required as we have to update it's next_leaf pointer
  443. * for the new last extent block.
  444. *
  445. * the new branch will be 'empty' in the sense that every block will
  446. * contain a single record with cluster count == 0.
  447. */
  448. static int ocfs2_add_branch(struct ocfs2_super *osb,
  449. handle_t *handle,
  450. struct inode *inode,
  451. struct buffer_head *fe_bh,
  452. struct buffer_head *eb_bh,
  453. struct buffer_head **last_eb_bh,
  454. struct ocfs2_alloc_context *meta_ac)
  455. {
  456. int status, new_blocks, i;
  457. u64 next_blkno, new_last_eb_blk;
  458. struct buffer_head *bh;
  459. struct buffer_head **new_eb_bhs = NULL;
  460. struct ocfs2_dinode *fe;
  461. struct ocfs2_extent_block *eb;
  462. struct ocfs2_extent_list *eb_el;
  463. struct ocfs2_extent_list *el;
  464. u32 new_cpos;
  465. mlog_entry_void();
  466. BUG_ON(!last_eb_bh || !*last_eb_bh);
  467. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  468. if (eb_bh) {
  469. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  470. el = &eb->h_list;
  471. } else
  472. el = &fe->id2.i_list;
  473. /* we never add a branch to a leaf. */
  474. BUG_ON(!el->l_tree_depth);
  475. new_blocks = le16_to_cpu(el->l_tree_depth);
  476. /* allocate the number of new eb blocks we need */
  477. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  478. GFP_KERNEL);
  479. if (!new_eb_bhs) {
  480. status = -ENOMEM;
  481. mlog_errno(status);
  482. goto bail;
  483. }
  484. status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
  485. meta_ac, new_eb_bhs);
  486. if (status < 0) {
  487. mlog_errno(status);
  488. goto bail;
  489. }
  490. eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
  491. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  492. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  493. * linked with the rest of the tree.
  494. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  495. *
  496. * when we leave the loop, new_last_eb_blk will point to the
  497. * newest leaf, and next_blkno will point to the topmost extent
  498. * block. */
  499. next_blkno = new_last_eb_blk = 0;
  500. for(i = 0; i < new_blocks; i++) {
  501. bh = new_eb_bhs[i];
  502. eb = (struct ocfs2_extent_block *) bh->b_data;
  503. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  504. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  505. status = -EIO;
  506. goto bail;
  507. }
  508. eb_el = &eb->h_list;
  509. status = ocfs2_journal_access(handle, inode, bh,
  510. OCFS2_JOURNAL_ACCESS_CREATE);
  511. if (status < 0) {
  512. mlog_errno(status);
  513. goto bail;
  514. }
  515. eb->h_next_leaf_blk = 0;
  516. eb_el->l_tree_depth = cpu_to_le16(i);
  517. eb_el->l_next_free_rec = cpu_to_le16(1);
  518. /*
  519. * This actually counts as an empty extent as
  520. * c_clusters == 0
  521. */
  522. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  523. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  524. /*
  525. * eb_el isn't always an interior node, but even leaf
  526. * nodes want a zero'd flags and reserved field so
  527. * this gets the whole 32 bits regardless of use.
  528. */
  529. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  530. if (!eb_el->l_tree_depth)
  531. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  532. status = ocfs2_journal_dirty(handle, bh);
  533. if (status < 0) {
  534. mlog_errno(status);
  535. goto bail;
  536. }
  537. next_blkno = le64_to_cpu(eb->h_blkno);
  538. }
  539. /* This is a bit hairy. We want to update up to three blocks
  540. * here without leaving any of them in an inconsistent state
  541. * in case of error. We don't have to worry about
  542. * journal_dirty erroring as it won't unless we've aborted the
  543. * handle (in which case we would never be here) so reserving
  544. * the write with journal_access is all we need to do. */
  545. status = ocfs2_journal_access(handle, inode, *last_eb_bh,
  546. OCFS2_JOURNAL_ACCESS_WRITE);
  547. if (status < 0) {
  548. mlog_errno(status);
  549. goto bail;
  550. }
  551. status = ocfs2_journal_access(handle, inode, fe_bh,
  552. OCFS2_JOURNAL_ACCESS_WRITE);
  553. if (status < 0) {
  554. mlog_errno(status);
  555. goto bail;
  556. }
  557. if (eb_bh) {
  558. status = ocfs2_journal_access(handle, inode, eb_bh,
  559. OCFS2_JOURNAL_ACCESS_WRITE);
  560. if (status < 0) {
  561. mlog_errno(status);
  562. goto bail;
  563. }
  564. }
  565. /* Link the new branch into the rest of the tree (el will
  566. * either be on the fe, or the extent block passed in. */
  567. i = le16_to_cpu(el->l_next_free_rec);
  568. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  569. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  570. el->l_recs[i].e_int_clusters = 0;
  571. le16_add_cpu(&el->l_next_free_rec, 1);
  572. /* fe needs a new last extent block pointer, as does the
  573. * next_leaf on the previously last-extent-block. */
  574. fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
  575. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  576. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  577. status = ocfs2_journal_dirty(handle, *last_eb_bh);
  578. if (status < 0)
  579. mlog_errno(status);
  580. status = ocfs2_journal_dirty(handle, fe_bh);
  581. if (status < 0)
  582. mlog_errno(status);
  583. if (eb_bh) {
  584. status = ocfs2_journal_dirty(handle, eb_bh);
  585. if (status < 0)
  586. mlog_errno(status);
  587. }
  588. /*
  589. * Some callers want to track the rightmost leaf so pass it
  590. * back here.
  591. */
  592. brelse(*last_eb_bh);
  593. get_bh(new_eb_bhs[0]);
  594. *last_eb_bh = new_eb_bhs[0];
  595. status = 0;
  596. bail:
  597. if (new_eb_bhs) {
  598. for (i = 0; i < new_blocks; i++)
  599. if (new_eb_bhs[i])
  600. brelse(new_eb_bhs[i]);
  601. kfree(new_eb_bhs);
  602. }
  603. mlog_exit(status);
  604. return status;
  605. }
  606. /*
  607. * adds another level to the allocation tree.
  608. * returns back the new extent block so you can add a branch to it
  609. * after this call.
  610. */
  611. static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
  612. handle_t *handle,
  613. struct inode *inode,
  614. struct buffer_head *fe_bh,
  615. struct ocfs2_alloc_context *meta_ac,
  616. struct buffer_head **ret_new_eb_bh)
  617. {
  618. int status, i;
  619. u32 new_clusters;
  620. struct buffer_head *new_eb_bh = NULL;
  621. struct ocfs2_dinode *fe;
  622. struct ocfs2_extent_block *eb;
  623. struct ocfs2_extent_list *fe_el;
  624. struct ocfs2_extent_list *eb_el;
  625. mlog_entry_void();
  626. status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
  627. &new_eb_bh);
  628. if (status < 0) {
  629. mlog_errno(status);
  630. goto bail;
  631. }
  632. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  633. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  634. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  635. status = -EIO;
  636. goto bail;
  637. }
  638. eb_el = &eb->h_list;
  639. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  640. fe_el = &fe->id2.i_list;
  641. status = ocfs2_journal_access(handle, inode, new_eb_bh,
  642. OCFS2_JOURNAL_ACCESS_CREATE);
  643. if (status < 0) {
  644. mlog_errno(status);
  645. goto bail;
  646. }
  647. /* copy the fe data into the new extent block */
  648. eb_el->l_tree_depth = fe_el->l_tree_depth;
  649. eb_el->l_next_free_rec = fe_el->l_next_free_rec;
  650. for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  651. eb_el->l_recs[i] = fe_el->l_recs[i];
  652. status = ocfs2_journal_dirty(handle, new_eb_bh);
  653. if (status < 0) {
  654. mlog_errno(status);
  655. goto bail;
  656. }
  657. status = ocfs2_journal_access(handle, inode, fe_bh,
  658. OCFS2_JOURNAL_ACCESS_WRITE);
  659. if (status < 0) {
  660. mlog_errno(status);
  661. goto bail;
  662. }
  663. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  664. /* update fe now */
  665. le16_add_cpu(&fe_el->l_tree_depth, 1);
  666. fe_el->l_recs[0].e_cpos = 0;
  667. fe_el->l_recs[0].e_blkno = eb->h_blkno;
  668. fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  669. for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++)
  670. memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  671. fe_el->l_next_free_rec = cpu_to_le16(1);
  672. /* If this is our 1st tree depth shift, then last_eb_blk
  673. * becomes the allocated extent block */
  674. if (fe_el->l_tree_depth == cpu_to_le16(1))
  675. fe->i_last_eb_blk = eb->h_blkno;
  676. status = ocfs2_journal_dirty(handle, fe_bh);
  677. if (status < 0) {
  678. mlog_errno(status);
  679. goto bail;
  680. }
  681. *ret_new_eb_bh = new_eb_bh;
  682. new_eb_bh = NULL;
  683. status = 0;
  684. bail:
  685. if (new_eb_bh)
  686. brelse(new_eb_bh);
  687. mlog_exit(status);
  688. return status;
  689. }
  690. /*
  691. * Should only be called when there is no space left in any of the
  692. * leaf nodes. What we want to do is find the lowest tree depth
  693. * non-leaf extent block with room for new records. There are three
  694. * valid results of this search:
  695. *
  696. * 1) a lowest extent block is found, then we pass it back in
  697. * *lowest_eb_bh and return '0'
  698. *
  699. * 2) the search fails to find anything, but the dinode has room. We
  700. * pass NULL back in *lowest_eb_bh, but still return '0'
  701. *
  702. * 3) the search fails to find anything AND the dinode is full, in
  703. * which case we return > 0
  704. *
  705. * return status < 0 indicates an error.
  706. */
  707. static int ocfs2_find_branch_target(struct ocfs2_super *osb,
  708. struct inode *inode,
  709. struct buffer_head *fe_bh,
  710. struct buffer_head **target_bh)
  711. {
  712. int status = 0, i;
  713. u64 blkno;
  714. struct ocfs2_dinode *fe;
  715. struct ocfs2_extent_block *eb;
  716. struct ocfs2_extent_list *el;
  717. struct buffer_head *bh = NULL;
  718. struct buffer_head *lowest_bh = NULL;
  719. mlog_entry_void();
  720. *target_bh = NULL;
  721. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  722. el = &fe->id2.i_list;
  723. while(le16_to_cpu(el->l_tree_depth) > 1) {
  724. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  725. ocfs2_error(inode->i_sb, "Dinode %llu has empty "
  726. "extent list (next_free_rec == 0)",
  727. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  728. status = -EIO;
  729. goto bail;
  730. }
  731. i = le16_to_cpu(el->l_next_free_rec) - 1;
  732. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  733. if (!blkno) {
  734. ocfs2_error(inode->i_sb, "Dinode %llu has extent "
  735. "list where extent # %d has no physical "
  736. "block start",
  737. (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
  738. status = -EIO;
  739. goto bail;
  740. }
  741. if (bh) {
  742. brelse(bh);
  743. bh = NULL;
  744. }
  745. status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
  746. inode);
  747. if (status < 0) {
  748. mlog_errno(status);
  749. goto bail;
  750. }
  751. eb = (struct ocfs2_extent_block *) bh->b_data;
  752. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  753. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  754. status = -EIO;
  755. goto bail;
  756. }
  757. el = &eb->h_list;
  758. if (le16_to_cpu(el->l_next_free_rec) <
  759. le16_to_cpu(el->l_count)) {
  760. if (lowest_bh)
  761. brelse(lowest_bh);
  762. lowest_bh = bh;
  763. get_bh(lowest_bh);
  764. }
  765. }
  766. /* If we didn't find one and the fe doesn't have any room,
  767. * then return '1' */
  768. if (!lowest_bh
  769. && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
  770. status = 1;
  771. *target_bh = lowest_bh;
  772. bail:
  773. if (bh)
  774. brelse(bh);
  775. mlog_exit(status);
  776. return status;
  777. }
  778. /*
  779. * Grow a b-tree so that it has more records.
  780. *
  781. * We might shift the tree depth in which case existing paths should
  782. * be considered invalid.
  783. *
  784. * Tree depth after the grow is returned via *final_depth.
  785. *
  786. * *last_eb_bh will be updated by ocfs2_add_branch().
  787. */
  788. static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
  789. struct buffer_head *di_bh, int *final_depth,
  790. struct buffer_head **last_eb_bh,
  791. struct ocfs2_alloc_context *meta_ac)
  792. {
  793. int ret, shift;
  794. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  795. int depth = le16_to_cpu(di->id2.i_list.l_tree_depth);
  796. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  797. struct buffer_head *bh = NULL;
  798. BUG_ON(meta_ac == NULL);
  799. shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh);
  800. if (shift < 0) {
  801. ret = shift;
  802. mlog_errno(ret);
  803. goto out;
  804. }
  805. /* We traveled all the way to the bottom of the allocation tree
  806. * and didn't find room for any more extents - we need to add
  807. * another tree level */
  808. if (shift) {
  809. BUG_ON(bh);
  810. mlog(0, "need to shift tree depth (current = %d)\n", depth);
  811. /* ocfs2_shift_tree_depth will return us a buffer with
  812. * the new extent block (so we can pass that to
  813. * ocfs2_add_branch). */
  814. ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh,
  815. meta_ac, &bh);
  816. if (ret < 0) {
  817. mlog_errno(ret);
  818. goto out;
  819. }
  820. depth++;
  821. if (depth == 1) {
  822. /*
  823. * Special case: we have room now if we shifted from
  824. * tree_depth 0, so no more work needs to be done.
  825. *
  826. * We won't be calling add_branch, so pass
  827. * back *last_eb_bh as the new leaf. At depth
  828. * zero, it should always be null so there's
  829. * no reason to brelse.
  830. */
  831. BUG_ON(*last_eb_bh);
  832. get_bh(bh);
  833. *last_eb_bh = bh;
  834. goto out;
  835. }
  836. }
  837. /* call ocfs2_add_branch to add the final part of the tree with
  838. * the new data. */
  839. mlog(0, "add branch. bh = %p\n", bh);
  840. ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh,
  841. meta_ac);
  842. if (ret < 0) {
  843. mlog_errno(ret);
  844. goto out;
  845. }
  846. out:
  847. if (final_depth)
  848. *final_depth = depth;
  849. brelse(bh);
  850. return ret;
  851. }
  852. /*
  853. * This is only valid for leaf nodes, which are the only ones that can
  854. * have empty extents anyway.
  855. */
  856. static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
  857. {
  858. return !rec->e_leaf_clusters;
  859. }
  860. /*
  861. * This function will discard the rightmost extent record.
  862. */
  863. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  864. {
  865. int next_free = le16_to_cpu(el->l_next_free_rec);
  866. int count = le16_to_cpu(el->l_count);
  867. unsigned int num_bytes;
  868. BUG_ON(!next_free);
  869. /* This will cause us to go off the end of our extent list. */
  870. BUG_ON(next_free >= count);
  871. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  872. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  873. }
  874. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  875. struct ocfs2_extent_rec *insert_rec)
  876. {
  877. int i, insert_index, next_free, has_empty, num_bytes;
  878. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  879. struct ocfs2_extent_rec *rec;
  880. next_free = le16_to_cpu(el->l_next_free_rec);
  881. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  882. BUG_ON(!next_free);
  883. /* The tree code before us didn't allow enough room in the leaf. */
  884. if (el->l_next_free_rec == el->l_count && !has_empty)
  885. BUG();
  886. /*
  887. * The easiest way to approach this is to just remove the
  888. * empty extent and temporarily decrement next_free.
  889. */
  890. if (has_empty) {
  891. /*
  892. * If next_free was 1 (only an empty extent), this
  893. * loop won't execute, which is fine. We still want
  894. * the decrement above to happen.
  895. */
  896. for(i = 0; i < (next_free - 1); i++)
  897. el->l_recs[i] = el->l_recs[i+1];
  898. next_free--;
  899. }
  900. /*
  901. * Figure out what the new record index should be.
  902. */
  903. for(i = 0; i < next_free; i++) {
  904. rec = &el->l_recs[i];
  905. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  906. break;
  907. }
  908. insert_index = i;
  909. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  910. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  911. BUG_ON(insert_index < 0);
  912. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  913. BUG_ON(insert_index > next_free);
  914. /*
  915. * No need to memmove if we're just adding to the tail.
  916. */
  917. if (insert_index != next_free) {
  918. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  919. num_bytes = next_free - insert_index;
  920. num_bytes *= sizeof(struct ocfs2_extent_rec);
  921. memmove(&el->l_recs[insert_index + 1],
  922. &el->l_recs[insert_index],
  923. num_bytes);
  924. }
  925. /*
  926. * Either we had an empty extent, and need to re-increment or
  927. * there was no empty extent on a non full rightmost leaf node,
  928. * in which case we still need to increment.
  929. */
  930. next_free++;
  931. el->l_next_free_rec = cpu_to_le16(next_free);
  932. /*
  933. * Make sure none of the math above just messed up our tree.
  934. */
  935. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  936. el->l_recs[insert_index] = *insert_rec;
  937. }
  938. static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
  939. {
  940. int size, num_recs = le16_to_cpu(el->l_next_free_rec);
  941. BUG_ON(num_recs == 0);
  942. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  943. num_recs--;
  944. size = num_recs * sizeof(struct ocfs2_extent_rec);
  945. memmove(&el->l_recs[0], &el->l_recs[1], size);
  946. memset(&el->l_recs[num_recs], 0,
  947. sizeof(struct ocfs2_extent_rec));
  948. el->l_next_free_rec = cpu_to_le16(num_recs);
  949. }
  950. }
  951. /*
  952. * Create an empty extent record .
  953. *
  954. * l_next_free_rec may be updated.
  955. *
  956. * If an empty extent already exists do nothing.
  957. */
  958. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  959. {
  960. int next_free = le16_to_cpu(el->l_next_free_rec);
  961. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  962. if (next_free == 0)
  963. goto set_and_inc;
  964. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  965. return;
  966. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  967. "Asked to create an empty extent in a full list:\n"
  968. "count = %u, tree depth = %u",
  969. le16_to_cpu(el->l_count),
  970. le16_to_cpu(el->l_tree_depth));
  971. ocfs2_shift_records_right(el);
  972. set_and_inc:
  973. le16_add_cpu(&el->l_next_free_rec, 1);
  974. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  975. }
  976. /*
  977. * For a rotation which involves two leaf nodes, the "root node" is
  978. * the lowest level tree node which contains a path to both leafs. This
  979. * resulting set of information can be used to form a complete "subtree"
  980. *
  981. * This function is passed two full paths from the dinode down to a
  982. * pair of adjacent leaves. It's task is to figure out which path
  983. * index contains the subtree root - this can be the root index itself
  984. * in a worst-case rotation.
  985. *
  986. * The array index of the subtree root is passed back.
  987. */
  988. static int ocfs2_find_subtree_root(struct inode *inode,
  989. struct ocfs2_path *left,
  990. struct ocfs2_path *right)
  991. {
  992. int i = 0;
  993. /*
  994. * Check that the caller passed in two paths from the same tree.
  995. */
  996. BUG_ON(path_root_bh(left) != path_root_bh(right));
  997. do {
  998. i++;
  999. /*
  1000. * The caller didn't pass two adjacent paths.
  1001. */
  1002. mlog_bug_on_msg(i > left->p_tree_depth,
  1003. "Inode %lu, left depth %u, right depth %u\n"
  1004. "left leaf blk %llu, right leaf blk %llu\n",
  1005. inode->i_ino, left->p_tree_depth,
  1006. right->p_tree_depth,
  1007. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  1008. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  1009. } while (left->p_node[i].bh->b_blocknr ==
  1010. right->p_node[i].bh->b_blocknr);
  1011. return i - 1;
  1012. }
  1013. typedef void (path_insert_t)(void *, struct buffer_head *);
  1014. /*
  1015. * Traverse a btree path in search of cpos, starting at root_el.
  1016. *
  1017. * This code can be called with a cpos larger than the tree, in which
  1018. * case it will return the rightmost path.
  1019. */
  1020. static int __ocfs2_find_path(struct inode *inode,
  1021. struct ocfs2_extent_list *root_el, u32 cpos,
  1022. path_insert_t *func, void *data)
  1023. {
  1024. int i, ret = 0;
  1025. u32 range;
  1026. u64 blkno;
  1027. struct buffer_head *bh = NULL;
  1028. struct ocfs2_extent_block *eb;
  1029. struct ocfs2_extent_list *el;
  1030. struct ocfs2_extent_rec *rec;
  1031. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1032. el = root_el;
  1033. while (el->l_tree_depth) {
  1034. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1035. ocfs2_error(inode->i_sb,
  1036. "Inode %llu has empty extent list at "
  1037. "depth %u\n",
  1038. (unsigned long long)oi->ip_blkno,
  1039. le16_to_cpu(el->l_tree_depth));
  1040. ret = -EROFS;
  1041. goto out;
  1042. }
  1043. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  1044. rec = &el->l_recs[i];
  1045. /*
  1046. * In the case that cpos is off the allocation
  1047. * tree, this should just wind up returning the
  1048. * rightmost record.
  1049. */
  1050. range = le32_to_cpu(rec->e_cpos) +
  1051. ocfs2_rec_clusters(el, rec);
  1052. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1053. break;
  1054. }
  1055. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1056. if (blkno == 0) {
  1057. ocfs2_error(inode->i_sb,
  1058. "Inode %llu has bad blkno in extent list "
  1059. "at depth %u (index %d)\n",
  1060. (unsigned long long)oi->ip_blkno,
  1061. le16_to_cpu(el->l_tree_depth), i);
  1062. ret = -EROFS;
  1063. goto out;
  1064. }
  1065. brelse(bh);
  1066. bh = NULL;
  1067. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  1068. &bh, OCFS2_BH_CACHED, inode);
  1069. if (ret) {
  1070. mlog_errno(ret);
  1071. goto out;
  1072. }
  1073. eb = (struct ocfs2_extent_block *) bh->b_data;
  1074. el = &eb->h_list;
  1075. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  1076. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  1077. ret = -EIO;
  1078. goto out;
  1079. }
  1080. if (le16_to_cpu(el->l_next_free_rec) >
  1081. le16_to_cpu(el->l_count)) {
  1082. ocfs2_error(inode->i_sb,
  1083. "Inode %llu has bad count in extent list "
  1084. "at block %llu (next free=%u, count=%u)\n",
  1085. (unsigned long long)oi->ip_blkno,
  1086. (unsigned long long)bh->b_blocknr,
  1087. le16_to_cpu(el->l_next_free_rec),
  1088. le16_to_cpu(el->l_count));
  1089. ret = -EROFS;
  1090. goto out;
  1091. }
  1092. if (func)
  1093. func(data, bh);
  1094. }
  1095. out:
  1096. /*
  1097. * Catch any trailing bh that the loop didn't handle.
  1098. */
  1099. brelse(bh);
  1100. return ret;
  1101. }
  1102. /*
  1103. * Given an initialized path (that is, it has a valid root extent
  1104. * list), this function will traverse the btree in search of the path
  1105. * which would contain cpos.
  1106. *
  1107. * The path traveled is recorded in the path structure.
  1108. *
  1109. * Note that this will not do any comparisons on leaf node extent
  1110. * records, so it will work fine in the case that we just added a tree
  1111. * branch.
  1112. */
  1113. struct find_path_data {
  1114. int index;
  1115. struct ocfs2_path *path;
  1116. };
  1117. static void find_path_ins(void *data, struct buffer_head *bh)
  1118. {
  1119. struct find_path_data *fp = data;
  1120. get_bh(bh);
  1121. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  1122. fp->index++;
  1123. }
  1124. static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
  1125. u32 cpos)
  1126. {
  1127. struct find_path_data data;
  1128. data.index = 1;
  1129. data.path = path;
  1130. return __ocfs2_find_path(inode, path_root_el(path), cpos,
  1131. find_path_ins, &data);
  1132. }
  1133. static void find_leaf_ins(void *data, struct buffer_head *bh)
  1134. {
  1135. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  1136. struct ocfs2_extent_list *el = &eb->h_list;
  1137. struct buffer_head **ret = data;
  1138. /* We want to retain only the leaf block. */
  1139. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1140. get_bh(bh);
  1141. *ret = bh;
  1142. }
  1143. }
  1144. /*
  1145. * Find the leaf block in the tree which would contain cpos. No
  1146. * checking of the actual leaf is done.
  1147. *
  1148. * Some paths want to call this instead of allocating a path structure
  1149. * and calling ocfs2_find_path().
  1150. *
  1151. * This function doesn't handle non btree extent lists.
  1152. */
  1153. int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
  1154. u32 cpos, struct buffer_head **leaf_bh)
  1155. {
  1156. int ret;
  1157. struct buffer_head *bh = NULL;
  1158. ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
  1159. if (ret) {
  1160. mlog_errno(ret);
  1161. goto out;
  1162. }
  1163. *leaf_bh = bh;
  1164. out:
  1165. return ret;
  1166. }
  1167. /*
  1168. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1169. *
  1170. * Basically, we've moved stuff around at the bottom of the tree and
  1171. * we need to fix up the extent records above the changes to reflect
  1172. * the new changes.
  1173. *
  1174. * left_rec: the record on the left.
  1175. * left_child_el: is the child list pointed to by left_rec
  1176. * right_rec: the record to the right of left_rec
  1177. * right_child_el: is the child list pointed to by right_rec
  1178. *
  1179. * By definition, this only works on interior nodes.
  1180. */
  1181. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1182. struct ocfs2_extent_list *left_child_el,
  1183. struct ocfs2_extent_rec *right_rec,
  1184. struct ocfs2_extent_list *right_child_el)
  1185. {
  1186. u32 left_clusters, right_end;
  1187. /*
  1188. * Interior nodes never have holes. Their cpos is the cpos of
  1189. * the leftmost record in their child list. Their cluster
  1190. * count covers the full theoretical range of their child list
  1191. * - the range between their cpos and the cpos of the record
  1192. * immediately to their right.
  1193. */
  1194. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1195. if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
  1196. BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
  1197. left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
  1198. }
  1199. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1200. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1201. /*
  1202. * Calculate the rightmost cluster count boundary before
  1203. * moving cpos - we will need to adjust clusters after
  1204. * updating e_cpos to keep the same highest cluster count.
  1205. */
  1206. right_end = le32_to_cpu(right_rec->e_cpos);
  1207. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1208. right_rec->e_cpos = left_rec->e_cpos;
  1209. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1210. right_end -= le32_to_cpu(right_rec->e_cpos);
  1211. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1212. }
  1213. /*
  1214. * Adjust the adjacent root node records involved in a
  1215. * rotation. left_el_blkno is passed in as a key so that we can easily
  1216. * find it's index in the root list.
  1217. */
  1218. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1219. struct ocfs2_extent_list *left_el,
  1220. struct ocfs2_extent_list *right_el,
  1221. u64 left_el_blkno)
  1222. {
  1223. int i;
  1224. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1225. le16_to_cpu(left_el->l_tree_depth));
  1226. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1227. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1228. break;
  1229. }
  1230. /*
  1231. * The path walking code should have never returned a root and
  1232. * two paths which are not adjacent.
  1233. */
  1234. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1235. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1236. &root_el->l_recs[i + 1], right_el);
  1237. }
  1238. /*
  1239. * We've changed a leaf block (in right_path) and need to reflect that
  1240. * change back up the subtree.
  1241. *
  1242. * This happens in multiple places:
  1243. * - When we've moved an extent record from the left path leaf to the right
  1244. * path leaf to make room for an empty extent in the left path leaf.
  1245. * - When our insert into the right path leaf is at the leftmost edge
  1246. * and requires an update of the path immediately to it's left. This
  1247. * can occur at the end of some types of rotation and appending inserts.
  1248. */
  1249. static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
  1250. struct ocfs2_path *left_path,
  1251. struct ocfs2_path *right_path,
  1252. int subtree_index)
  1253. {
  1254. int ret, i, idx;
  1255. struct ocfs2_extent_list *el, *left_el, *right_el;
  1256. struct ocfs2_extent_rec *left_rec, *right_rec;
  1257. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1258. /*
  1259. * Update the counts and position values within all the
  1260. * interior nodes to reflect the leaf rotation we just did.
  1261. *
  1262. * The root node is handled below the loop.
  1263. *
  1264. * We begin the loop with right_el and left_el pointing to the
  1265. * leaf lists and work our way up.
  1266. *
  1267. * NOTE: within this loop, left_el and right_el always refer
  1268. * to the *child* lists.
  1269. */
  1270. left_el = path_leaf_el(left_path);
  1271. right_el = path_leaf_el(right_path);
  1272. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1273. mlog(0, "Adjust records at index %u\n", i);
  1274. /*
  1275. * One nice property of knowing that all of these
  1276. * nodes are below the root is that we only deal with
  1277. * the leftmost right node record and the rightmost
  1278. * left node record.
  1279. */
  1280. el = left_path->p_node[i].el;
  1281. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1282. left_rec = &el->l_recs[idx];
  1283. el = right_path->p_node[i].el;
  1284. right_rec = &el->l_recs[0];
  1285. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1286. right_el);
  1287. ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1288. if (ret)
  1289. mlog_errno(ret);
  1290. ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1291. if (ret)
  1292. mlog_errno(ret);
  1293. /*
  1294. * Setup our list pointers now so that the current
  1295. * parents become children in the next iteration.
  1296. */
  1297. left_el = left_path->p_node[i].el;
  1298. right_el = right_path->p_node[i].el;
  1299. }
  1300. /*
  1301. * At the root node, adjust the two adjacent records which
  1302. * begin our path to the leaves.
  1303. */
  1304. el = left_path->p_node[subtree_index].el;
  1305. left_el = left_path->p_node[subtree_index + 1].el;
  1306. right_el = right_path->p_node[subtree_index + 1].el;
  1307. ocfs2_adjust_root_records(el, left_el, right_el,
  1308. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1309. root_bh = left_path->p_node[subtree_index].bh;
  1310. ret = ocfs2_journal_dirty(handle, root_bh);
  1311. if (ret)
  1312. mlog_errno(ret);
  1313. }
  1314. static int ocfs2_rotate_subtree_right(struct inode *inode,
  1315. handle_t *handle,
  1316. struct ocfs2_path *left_path,
  1317. struct ocfs2_path *right_path,
  1318. int subtree_index)
  1319. {
  1320. int ret, i;
  1321. struct buffer_head *right_leaf_bh;
  1322. struct buffer_head *left_leaf_bh = NULL;
  1323. struct buffer_head *root_bh;
  1324. struct ocfs2_extent_list *right_el, *left_el;
  1325. struct ocfs2_extent_rec move_rec;
  1326. left_leaf_bh = path_leaf_bh(left_path);
  1327. left_el = path_leaf_el(left_path);
  1328. if (left_el->l_next_free_rec != left_el->l_count) {
  1329. ocfs2_error(inode->i_sb,
  1330. "Inode %llu has non-full interior leaf node %llu"
  1331. "(next free = %u)",
  1332. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1333. (unsigned long long)left_leaf_bh->b_blocknr,
  1334. le16_to_cpu(left_el->l_next_free_rec));
  1335. return -EROFS;
  1336. }
  1337. /*
  1338. * This extent block may already have an empty record, so we
  1339. * return early if so.
  1340. */
  1341. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1342. return 0;
  1343. root_bh = left_path->p_node[subtree_index].bh;
  1344. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1345. ret = ocfs2_journal_access(handle, inode, root_bh,
  1346. OCFS2_JOURNAL_ACCESS_WRITE);
  1347. if (ret) {
  1348. mlog_errno(ret);
  1349. goto out;
  1350. }
  1351. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1352. ret = ocfs2_journal_access(handle, inode,
  1353. right_path->p_node[i].bh,
  1354. OCFS2_JOURNAL_ACCESS_WRITE);
  1355. if (ret) {
  1356. mlog_errno(ret);
  1357. goto out;
  1358. }
  1359. ret = ocfs2_journal_access(handle, inode,
  1360. left_path->p_node[i].bh,
  1361. OCFS2_JOURNAL_ACCESS_WRITE);
  1362. if (ret) {
  1363. mlog_errno(ret);
  1364. goto out;
  1365. }
  1366. }
  1367. right_leaf_bh = path_leaf_bh(right_path);
  1368. right_el = path_leaf_el(right_path);
  1369. /* This is a code error, not a disk corruption. */
  1370. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1371. "because rightmost leaf block %llu is empty\n",
  1372. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1373. (unsigned long long)right_leaf_bh->b_blocknr);
  1374. ocfs2_create_empty_extent(right_el);
  1375. ret = ocfs2_journal_dirty(handle, right_leaf_bh);
  1376. if (ret) {
  1377. mlog_errno(ret);
  1378. goto out;
  1379. }
  1380. /* Do the copy now. */
  1381. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1382. move_rec = left_el->l_recs[i];
  1383. right_el->l_recs[0] = move_rec;
  1384. /*
  1385. * Clear out the record we just copied and shift everything
  1386. * over, leaving an empty extent in the left leaf.
  1387. *
  1388. * We temporarily subtract from next_free_rec so that the
  1389. * shift will lose the tail record (which is now defunct).
  1390. */
  1391. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1392. ocfs2_shift_records_right(left_el);
  1393. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1394. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1395. ret = ocfs2_journal_dirty(handle, left_leaf_bh);
  1396. if (ret) {
  1397. mlog_errno(ret);
  1398. goto out;
  1399. }
  1400. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1401. subtree_index);
  1402. out:
  1403. return ret;
  1404. }
  1405. /*
  1406. * Given a full path, determine what cpos value would return us a path
  1407. * containing the leaf immediately to the left of the current one.
  1408. *
  1409. * Will return zero if the path passed in is already the leftmost path.
  1410. */
  1411. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1412. struct ocfs2_path *path, u32 *cpos)
  1413. {
  1414. int i, j, ret = 0;
  1415. u64 blkno;
  1416. struct ocfs2_extent_list *el;
  1417. BUG_ON(path->p_tree_depth == 0);
  1418. *cpos = 0;
  1419. blkno = path_leaf_bh(path)->b_blocknr;
  1420. /* Start at the tree node just above the leaf and work our way up. */
  1421. i = path->p_tree_depth - 1;
  1422. while (i >= 0) {
  1423. el = path->p_node[i].el;
  1424. /*
  1425. * Find the extent record just before the one in our
  1426. * path.
  1427. */
  1428. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1429. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1430. if (j == 0) {
  1431. if (i == 0) {
  1432. /*
  1433. * We've determined that the
  1434. * path specified is already
  1435. * the leftmost one - return a
  1436. * cpos of zero.
  1437. */
  1438. goto out;
  1439. }
  1440. /*
  1441. * The leftmost record points to our
  1442. * leaf - we need to travel up the
  1443. * tree one level.
  1444. */
  1445. goto next_node;
  1446. }
  1447. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1448. *cpos = *cpos + ocfs2_rec_clusters(el,
  1449. &el->l_recs[j - 1]);
  1450. *cpos = *cpos - 1;
  1451. goto out;
  1452. }
  1453. }
  1454. /*
  1455. * If we got here, we never found a valid node where
  1456. * the tree indicated one should be.
  1457. */
  1458. ocfs2_error(sb,
  1459. "Invalid extent tree at extent block %llu\n",
  1460. (unsigned long long)blkno);
  1461. ret = -EROFS;
  1462. goto out;
  1463. next_node:
  1464. blkno = path->p_node[i].bh->b_blocknr;
  1465. i--;
  1466. }
  1467. out:
  1468. return ret;
  1469. }
  1470. /*
  1471. * Extend the transaction by enough credits to complete the rotation,
  1472. * and still leave at least the original number of credits allocated
  1473. * to this transaction.
  1474. */
  1475. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1476. int op_credits,
  1477. struct ocfs2_path *path)
  1478. {
  1479. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
  1480. if (handle->h_buffer_credits < credits)
  1481. return ocfs2_extend_trans(handle, credits);
  1482. return 0;
  1483. }
  1484. /*
  1485. * Trap the case where we're inserting into the theoretical range past
  1486. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1487. * whose cpos is less than ours into the right leaf.
  1488. *
  1489. * It's only necessary to look at the rightmost record of the left
  1490. * leaf because the logic that calls us should ensure that the
  1491. * theoretical ranges in the path components above the leaves are
  1492. * correct.
  1493. */
  1494. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1495. u32 insert_cpos)
  1496. {
  1497. struct ocfs2_extent_list *left_el;
  1498. struct ocfs2_extent_rec *rec;
  1499. int next_free;
  1500. left_el = path_leaf_el(left_path);
  1501. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1502. rec = &left_el->l_recs[next_free - 1];
  1503. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1504. return 1;
  1505. return 0;
  1506. }
  1507. static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  1508. {
  1509. int next_free = le16_to_cpu(el->l_next_free_rec);
  1510. unsigned int range;
  1511. struct ocfs2_extent_rec *rec;
  1512. if (next_free == 0)
  1513. return 0;
  1514. rec = &el->l_recs[0];
  1515. if (ocfs2_is_empty_extent(rec)) {
  1516. /* Empty list. */
  1517. if (next_free == 1)
  1518. return 0;
  1519. rec = &el->l_recs[1];
  1520. }
  1521. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1522. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1523. return 1;
  1524. return 0;
  1525. }
  1526. /*
  1527. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1528. *
  1529. * The path to the rightmost leaf should be passed in.
  1530. *
  1531. * The array is assumed to be large enough to hold an entire path (tree depth).
  1532. *
  1533. * Upon succesful return from this function:
  1534. *
  1535. * - The 'right_path' array will contain a path to the leaf block
  1536. * whose range contains e_cpos.
  1537. * - That leaf block will have a single empty extent in list index 0.
  1538. * - In the case that the rotation requires a post-insert update,
  1539. * *ret_left_path will contain a valid path which can be passed to
  1540. * ocfs2_insert_path().
  1541. */
  1542. static int ocfs2_rotate_tree_right(struct inode *inode,
  1543. handle_t *handle,
  1544. enum ocfs2_split_type split,
  1545. u32 insert_cpos,
  1546. struct ocfs2_path *right_path,
  1547. struct ocfs2_path **ret_left_path)
  1548. {
  1549. int ret, start, orig_credits = handle->h_buffer_credits;
  1550. u32 cpos;
  1551. struct ocfs2_path *left_path = NULL;
  1552. *ret_left_path = NULL;
  1553. left_path = ocfs2_new_path(path_root_bh(right_path),
  1554. path_root_el(right_path));
  1555. if (!left_path) {
  1556. ret = -ENOMEM;
  1557. mlog_errno(ret);
  1558. goto out;
  1559. }
  1560. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
  1561. if (ret) {
  1562. mlog_errno(ret);
  1563. goto out;
  1564. }
  1565. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  1566. /*
  1567. * What we want to do here is:
  1568. *
  1569. * 1) Start with the rightmost path.
  1570. *
  1571. * 2) Determine a path to the leaf block directly to the left
  1572. * of that leaf.
  1573. *
  1574. * 3) Determine the 'subtree root' - the lowest level tree node
  1575. * which contains a path to both leaves.
  1576. *
  1577. * 4) Rotate the subtree.
  1578. *
  1579. * 5) Find the next subtree by considering the left path to be
  1580. * the new right path.
  1581. *
  1582. * The check at the top of this while loop also accepts
  1583. * insert_cpos == cpos because cpos is only a _theoretical_
  1584. * value to get us the left path - insert_cpos might very well
  1585. * be filling that hole.
  1586. *
  1587. * Stop at a cpos of '0' because we either started at the
  1588. * leftmost branch (i.e., a tree with one branch and a
  1589. * rotation inside of it), or we've gone as far as we can in
  1590. * rotating subtrees.
  1591. */
  1592. while (cpos && insert_cpos <= cpos) {
  1593. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  1594. insert_cpos, cpos);
  1595. ret = ocfs2_find_path(inode, left_path, cpos);
  1596. if (ret) {
  1597. mlog_errno(ret);
  1598. goto out;
  1599. }
  1600. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  1601. path_leaf_bh(right_path),
  1602. "Inode %lu: error during insert of %u "
  1603. "(left path cpos %u) results in two identical "
  1604. "paths ending at %llu\n",
  1605. inode->i_ino, insert_cpos, cpos,
  1606. (unsigned long long)
  1607. path_leaf_bh(left_path)->b_blocknr);
  1608. if (split == SPLIT_NONE &&
  1609. ocfs2_rotate_requires_path_adjustment(left_path,
  1610. insert_cpos)) {
  1611. /*
  1612. * We've rotated the tree as much as we
  1613. * should. The rest is up to
  1614. * ocfs2_insert_path() to complete, after the
  1615. * record insertion. We indicate this
  1616. * situation by returning the left path.
  1617. *
  1618. * The reason we don't adjust the records here
  1619. * before the record insert is that an error
  1620. * later might break the rule where a parent
  1621. * record e_cpos will reflect the actual
  1622. * e_cpos of the 1st nonempty record of the
  1623. * child list.
  1624. */
  1625. *ret_left_path = left_path;
  1626. goto out_ret_path;
  1627. }
  1628. start = ocfs2_find_subtree_root(inode, left_path, right_path);
  1629. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  1630. start,
  1631. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  1632. right_path->p_tree_depth);
  1633. ret = ocfs2_extend_rotate_transaction(handle, start,
  1634. orig_credits, right_path);
  1635. if (ret) {
  1636. mlog_errno(ret);
  1637. goto out;
  1638. }
  1639. ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
  1640. right_path, start);
  1641. if (ret) {
  1642. mlog_errno(ret);
  1643. goto out;
  1644. }
  1645. if (split != SPLIT_NONE &&
  1646. ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
  1647. insert_cpos)) {
  1648. /*
  1649. * A rotate moves the rightmost left leaf
  1650. * record over to the leftmost right leaf
  1651. * slot. If we're doing an extent split
  1652. * instead of a real insert, then we have to
  1653. * check that the extent to be split wasn't
  1654. * just moved over. If it was, then we can
  1655. * exit here, passing left_path back -
  1656. * ocfs2_split_extent() is smart enough to
  1657. * search both leaves.
  1658. */
  1659. *ret_left_path = left_path;
  1660. goto out_ret_path;
  1661. }
  1662. /*
  1663. * There is no need to re-read the next right path
  1664. * as we know that it'll be our current left
  1665. * path. Optimize by copying values instead.
  1666. */
  1667. ocfs2_mv_path(right_path, left_path);
  1668. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1669. &cpos);
  1670. if (ret) {
  1671. mlog_errno(ret);
  1672. goto out;
  1673. }
  1674. }
  1675. out:
  1676. ocfs2_free_path(left_path);
  1677. out_ret_path:
  1678. return ret;
  1679. }
  1680. static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
  1681. struct ocfs2_path *path)
  1682. {
  1683. int i, idx;
  1684. struct ocfs2_extent_rec *rec;
  1685. struct ocfs2_extent_list *el;
  1686. struct ocfs2_extent_block *eb;
  1687. u32 range;
  1688. /* Path should always be rightmost. */
  1689. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  1690. BUG_ON(eb->h_next_leaf_blk != 0ULL);
  1691. el = &eb->h_list;
  1692. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  1693. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  1694. rec = &el->l_recs[idx];
  1695. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1696. for (i = 0; i < path->p_tree_depth; i++) {
  1697. el = path->p_node[i].el;
  1698. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  1699. rec = &el->l_recs[idx];
  1700. rec->e_int_clusters = cpu_to_le32(range);
  1701. le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
  1702. ocfs2_journal_dirty(handle, path->p_node[i].bh);
  1703. }
  1704. }
  1705. static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
  1706. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1707. struct ocfs2_path *path, int unlink_start)
  1708. {
  1709. int ret, i;
  1710. struct ocfs2_extent_block *eb;
  1711. struct ocfs2_extent_list *el;
  1712. struct buffer_head *bh;
  1713. for(i = unlink_start; i < path_num_items(path); i++) {
  1714. bh = path->p_node[i].bh;
  1715. eb = (struct ocfs2_extent_block *)bh->b_data;
  1716. /*
  1717. * Not all nodes might have had their final count
  1718. * decremented by the caller - handle this here.
  1719. */
  1720. el = &eb->h_list;
  1721. if (le16_to_cpu(el->l_next_free_rec) > 1) {
  1722. mlog(ML_ERROR,
  1723. "Inode %llu, attempted to remove extent block "
  1724. "%llu with %u records\n",
  1725. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1726. (unsigned long long)le64_to_cpu(eb->h_blkno),
  1727. le16_to_cpu(el->l_next_free_rec));
  1728. ocfs2_journal_dirty(handle, bh);
  1729. ocfs2_remove_from_cache(inode, bh);
  1730. continue;
  1731. }
  1732. el->l_next_free_rec = 0;
  1733. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1734. ocfs2_journal_dirty(handle, bh);
  1735. ret = ocfs2_cache_extent_block_free(dealloc, eb);
  1736. if (ret)
  1737. mlog_errno(ret);
  1738. ocfs2_remove_from_cache(inode, bh);
  1739. }
  1740. }
  1741. static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
  1742. struct ocfs2_path *left_path,
  1743. struct ocfs2_path *right_path,
  1744. int subtree_index,
  1745. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1746. {
  1747. int i;
  1748. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1749. struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
  1750. struct ocfs2_extent_list *el;
  1751. struct ocfs2_extent_block *eb;
  1752. el = path_leaf_el(left_path);
  1753. eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
  1754. for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1755. if (root_el->l_recs[i].e_blkno == eb->h_blkno)
  1756. break;
  1757. BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
  1758. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  1759. le16_add_cpu(&root_el->l_next_free_rec, -1);
  1760. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  1761. eb->h_next_leaf_blk = 0;
  1762. ocfs2_journal_dirty(handle, root_bh);
  1763. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  1764. ocfs2_unlink_path(inode, handle, dealloc, right_path,
  1765. subtree_index + 1);
  1766. }
  1767. static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
  1768. struct ocfs2_path *left_path,
  1769. struct ocfs2_path *right_path,
  1770. int subtree_index,
  1771. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1772. int *deleted)
  1773. {
  1774. int ret, i, del_right_subtree = 0, right_has_empty = 0;
  1775. struct buffer_head *root_bh, *di_bh = path_root_bh(right_path);
  1776. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1777. struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
  1778. struct ocfs2_extent_block *eb;
  1779. *deleted = 0;
  1780. right_leaf_el = path_leaf_el(right_path);
  1781. left_leaf_el = path_leaf_el(left_path);
  1782. root_bh = left_path->p_node[subtree_index].bh;
  1783. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1784. if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
  1785. return 0;
  1786. eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
  1787. if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
  1788. /*
  1789. * It's legal for us to proceed if the right leaf is
  1790. * the rightmost one and it has an empty extent. There
  1791. * are two cases to handle - whether the leaf will be
  1792. * empty after removal or not. If the leaf isn't empty
  1793. * then just remove the empty extent up front. The
  1794. * next block will handle empty leaves by flagging
  1795. * them for unlink.
  1796. *
  1797. * Non rightmost leaves will throw -EAGAIN and the
  1798. * caller can manually move the subtree and retry.
  1799. */
  1800. if (eb->h_next_leaf_blk != 0ULL)
  1801. return -EAGAIN;
  1802. if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
  1803. ret = ocfs2_journal_access(handle, inode,
  1804. path_leaf_bh(right_path),
  1805. OCFS2_JOURNAL_ACCESS_WRITE);
  1806. if (ret) {
  1807. mlog_errno(ret);
  1808. goto out;
  1809. }
  1810. ocfs2_remove_empty_extent(right_leaf_el);
  1811. } else
  1812. right_has_empty = 1;
  1813. }
  1814. if (eb->h_next_leaf_blk == 0ULL &&
  1815. le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
  1816. /*
  1817. * We have to update i_last_eb_blk during the meta
  1818. * data delete.
  1819. */
  1820. ret = ocfs2_journal_access(handle, inode, di_bh,
  1821. OCFS2_JOURNAL_ACCESS_WRITE);
  1822. if (ret) {
  1823. mlog_errno(ret);
  1824. goto out;
  1825. }
  1826. del_right_subtree = 1;
  1827. }
  1828. /*
  1829. * Getting here with an empty extent in the right path implies
  1830. * that it's the rightmost path and will be deleted.
  1831. */
  1832. BUG_ON(right_has_empty && !del_right_subtree);
  1833. ret = ocfs2_journal_access(handle, inode, root_bh,
  1834. OCFS2_JOURNAL_ACCESS_WRITE);
  1835. if (ret) {
  1836. mlog_errno(ret);
  1837. goto out;
  1838. }
  1839. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1840. ret = ocfs2_journal_access(handle, inode,
  1841. right_path->p_node[i].bh,
  1842. OCFS2_JOURNAL_ACCESS_WRITE);
  1843. if (ret) {
  1844. mlog_errno(ret);
  1845. goto out;
  1846. }
  1847. ret = ocfs2_journal_access(handle, inode,
  1848. left_path->p_node[i].bh,
  1849. OCFS2_JOURNAL_ACCESS_WRITE);
  1850. if (ret) {
  1851. mlog_errno(ret);
  1852. goto out;
  1853. }
  1854. }
  1855. if (!right_has_empty) {
  1856. /*
  1857. * Only do this if we're moving a real
  1858. * record. Otherwise, the action is delayed until
  1859. * after removal of the right path in which case we
  1860. * can do a simple shift to remove the empty extent.
  1861. */
  1862. ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
  1863. memset(&right_leaf_el->l_recs[0], 0,
  1864. sizeof(struct ocfs2_extent_rec));
  1865. }
  1866. if (eb->h_next_leaf_blk == 0ULL) {
  1867. /*
  1868. * Move recs over to get rid of empty extent, decrease
  1869. * next_free. This is allowed to remove the last
  1870. * extent in our leaf (setting l_next_free_rec to
  1871. * zero) - the delete code below won't care.
  1872. */
  1873. ocfs2_remove_empty_extent(right_leaf_el);
  1874. }
  1875. ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  1876. if (ret)
  1877. mlog_errno(ret);
  1878. ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  1879. if (ret)
  1880. mlog_errno(ret);
  1881. if (del_right_subtree) {
  1882. ocfs2_unlink_subtree(inode, handle, left_path, right_path,
  1883. subtree_index, dealloc);
  1884. ocfs2_update_edge_lengths(inode, handle, left_path);
  1885. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  1886. di->i_last_eb_blk = eb->h_blkno;
  1887. /*
  1888. * Removal of the extent in the left leaf was skipped
  1889. * above so we could delete the right path
  1890. * 1st.
  1891. */
  1892. if (right_has_empty)
  1893. ocfs2_remove_empty_extent(left_leaf_el);
  1894. ret = ocfs2_journal_dirty(handle, di_bh);
  1895. if (ret)
  1896. mlog_errno(ret);
  1897. *deleted = 1;
  1898. } else
  1899. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1900. subtree_index);
  1901. out:
  1902. return ret;
  1903. }
  1904. /*
  1905. * Given a full path, determine what cpos value would return us a path
  1906. * containing the leaf immediately to the right of the current one.
  1907. *
  1908. * Will return zero if the path passed in is already the rightmost path.
  1909. *
  1910. * This looks similar, but is subtly different to
  1911. * ocfs2_find_cpos_for_left_leaf().
  1912. */
  1913. static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  1914. struct ocfs2_path *path, u32 *cpos)
  1915. {
  1916. int i, j, ret = 0;
  1917. u64 blkno;
  1918. struct ocfs2_extent_list *el;
  1919. *cpos = 0;
  1920. if (path->p_tree_depth == 0)
  1921. return 0;
  1922. blkno = path_leaf_bh(path)->b_blocknr;
  1923. /* Start at the tree node just above the leaf and work our way up. */
  1924. i = path->p_tree_depth - 1;
  1925. while (i >= 0) {
  1926. int next_free;
  1927. el = path->p_node[i].el;
  1928. /*
  1929. * Find the extent record just after the one in our
  1930. * path.
  1931. */
  1932. next_free = le16_to_cpu(el->l_next_free_rec);
  1933. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1934. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1935. if (j == (next_free - 1)) {
  1936. if (i == 0) {
  1937. /*
  1938. * We've determined that the
  1939. * path specified is already
  1940. * the rightmost one - return a
  1941. * cpos of zero.
  1942. */
  1943. goto out;
  1944. }
  1945. /*
  1946. * The rightmost record points to our
  1947. * leaf - we need to travel up the
  1948. * tree one level.
  1949. */
  1950. goto next_node;
  1951. }
  1952. *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
  1953. goto out;
  1954. }
  1955. }
  1956. /*
  1957. * If we got here, we never found a valid node where
  1958. * the tree indicated one should be.
  1959. */
  1960. ocfs2_error(sb,
  1961. "Invalid extent tree at extent block %llu\n",
  1962. (unsigned long long)blkno);
  1963. ret = -EROFS;
  1964. goto out;
  1965. next_node:
  1966. blkno = path->p_node[i].bh->b_blocknr;
  1967. i--;
  1968. }
  1969. out:
  1970. return ret;
  1971. }
  1972. static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
  1973. handle_t *handle,
  1974. struct buffer_head *bh,
  1975. struct ocfs2_extent_list *el)
  1976. {
  1977. int ret;
  1978. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  1979. return 0;
  1980. ret = ocfs2_journal_access(handle, inode, bh,
  1981. OCFS2_JOURNAL_ACCESS_WRITE);
  1982. if (ret) {
  1983. mlog_errno(ret);
  1984. goto out;
  1985. }
  1986. ocfs2_remove_empty_extent(el);
  1987. ret = ocfs2_journal_dirty(handle, bh);
  1988. if (ret)
  1989. mlog_errno(ret);
  1990. out:
  1991. return ret;
  1992. }
  1993. static int __ocfs2_rotate_tree_left(struct inode *inode,
  1994. handle_t *handle, int orig_credits,
  1995. struct ocfs2_path *path,
  1996. struct ocfs2_cached_dealloc_ctxt *dealloc,
  1997. struct ocfs2_path **empty_extent_path)
  1998. {
  1999. int ret, subtree_root, deleted;
  2000. u32 right_cpos;
  2001. struct ocfs2_path *left_path = NULL;
  2002. struct ocfs2_path *right_path = NULL;
  2003. BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
  2004. *empty_extent_path = NULL;
  2005. ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
  2006. &right_cpos);
  2007. if (ret) {
  2008. mlog_errno(ret);
  2009. goto out;
  2010. }
  2011. left_path = ocfs2_new_path(path_root_bh(path),
  2012. path_root_el(path));
  2013. if (!left_path) {
  2014. ret = -ENOMEM;
  2015. mlog_errno(ret);
  2016. goto out;
  2017. }
  2018. ocfs2_cp_path(left_path, path);
  2019. right_path = ocfs2_new_path(path_root_bh(path),
  2020. path_root_el(path));
  2021. if (!right_path) {
  2022. ret = -ENOMEM;
  2023. mlog_errno(ret);
  2024. goto out;
  2025. }
  2026. while (right_cpos) {
  2027. ret = ocfs2_find_path(inode, right_path, right_cpos);
  2028. if (ret) {
  2029. mlog_errno(ret);
  2030. goto out;
  2031. }
  2032. subtree_root = ocfs2_find_subtree_root(inode, left_path,
  2033. right_path);
  2034. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2035. subtree_root,
  2036. (unsigned long long)
  2037. right_path->p_node[subtree_root].bh->b_blocknr,
  2038. right_path->p_tree_depth);
  2039. ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
  2040. orig_credits, left_path);
  2041. if (ret) {
  2042. mlog_errno(ret);
  2043. goto out;
  2044. }
  2045. ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
  2046. right_path, subtree_root,
  2047. dealloc, &deleted);
  2048. if (ret == -EAGAIN) {
  2049. /*
  2050. * The rotation has to temporarily stop due to
  2051. * the right subtree having an empty
  2052. * extent. Pass it back to the caller for a
  2053. * fixup.
  2054. */
  2055. *empty_extent_path = right_path;
  2056. right_path = NULL;
  2057. goto out;
  2058. }
  2059. if (ret) {
  2060. mlog_errno(ret);
  2061. goto out;
  2062. }
  2063. /*
  2064. * The subtree rotate might have removed records on
  2065. * the rightmost edge. If so, then rotation is
  2066. * complete.
  2067. */
  2068. if (deleted)
  2069. break;
  2070. ocfs2_mv_path(left_path, right_path);
  2071. ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
  2072. &right_cpos);
  2073. if (ret) {
  2074. mlog_errno(ret);
  2075. goto out;
  2076. }
  2077. }
  2078. out:
  2079. ocfs2_free_path(right_path);
  2080. ocfs2_free_path(left_path);
  2081. return ret;
  2082. }
  2083. static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
  2084. struct ocfs2_path *path,
  2085. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2086. {
  2087. int ret, subtree_index;
  2088. u32 cpos;
  2089. struct ocfs2_path *left_path = NULL;
  2090. struct ocfs2_dinode *di;
  2091. struct ocfs2_extent_block *eb;
  2092. struct ocfs2_extent_list *el;
  2093. /*
  2094. * XXX: This code assumes that the root is an inode, which is
  2095. * true for now but may change as tree code gets generic.
  2096. */
  2097. di = (struct ocfs2_dinode *)path_root_bh(path)->b_data;
  2098. if (!OCFS2_IS_VALID_DINODE(di)) {
  2099. ret = -EIO;
  2100. ocfs2_error(inode->i_sb,
  2101. "Inode %llu has invalid path root",
  2102. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2103. goto out;
  2104. }
  2105. /*
  2106. * There's two ways we handle this depending on
  2107. * whether path is the only existing one.
  2108. */
  2109. ret = ocfs2_extend_rotate_transaction(handle, 0,
  2110. handle->h_buffer_credits,
  2111. path);
  2112. if (ret) {
  2113. mlog_errno(ret);
  2114. goto out;
  2115. }
  2116. ret = ocfs2_journal_access_path(inode, handle, path);
  2117. if (ret) {
  2118. mlog_errno(ret);
  2119. goto out;
  2120. }
  2121. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  2122. if (ret) {
  2123. mlog_errno(ret);
  2124. goto out;
  2125. }
  2126. if (cpos) {
  2127. /*
  2128. * We have a path to the left of this one - it needs
  2129. * an update too.
  2130. */
  2131. left_path = ocfs2_new_path(path_root_bh(path),
  2132. path_root_el(path));
  2133. if (!left_path) {
  2134. ret = -ENOMEM;
  2135. mlog_errno(ret);
  2136. goto out;
  2137. }
  2138. ret = ocfs2_find_path(inode, left_path, cpos);
  2139. if (ret) {
  2140. mlog_errno(ret);
  2141. goto out;
  2142. }
  2143. ret = ocfs2_journal_access_path(inode, handle, left_path);
  2144. if (ret) {
  2145. mlog_errno(ret);
  2146. goto out;
  2147. }
  2148. subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
  2149. ocfs2_unlink_subtree(inode, handle, left_path, path,
  2150. subtree_index, dealloc);
  2151. ocfs2_update_edge_lengths(inode, handle, left_path);
  2152. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2153. di->i_last_eb_blk = eb->h_blkno;
  2154. } else {
  2155. /*
  2156. * 'path' is also the leftmost path which
  2157. * means it must be the only one. This gets
  2158. * handled differently because we want to
  2159. * revert the inode back to having extents
  2160. * in-line.
  2161. */
  2162. ocfs2_unlink_path(inode, handle, dealloc, path, 1);
  2163. el = &di->id2.i_list;
  2164. el->l_tree_depth = 0;
  2165. el->l_next_free_rec = 0;
  2166. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2167. di->i_last_eb_blk = 0;
  2168. }
  2169. ocfs2_journal_dirty(handle, path_root_bh(path));
  2170. out:
  2171. ocfs2_free_path(left_path);
  2172. return ret;
  2173. }
  2174. /*
  2175. * Left rotation of btree records.
  2176. *
  2177. * In many ways, this is (unsurprisingly) the opposite of right
  2178. * rotation. We start at some non-rightmost path containing an empty
  2179. * extent in the leaf block. The code works its way to the rightmost
  2180. * path by rotating records to the left in every subtree.
  2181. *
  2182. * This is used by any code which reduces the number of extent records
  2183. * in a leaf. After removal, an empty record should be placed in the
  2184. * leftmost list position.
  2185. *
  2186. * This won't handle a length update of the rightmost path records if
  2187. * the rightmost tree leaf record is removed so the caller is
  2188. * responsible for detecting and correcting that.
  2189. */
  2190. static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
  2191. struct ocfs2_path *path,
  2192. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2193. {
  2194. int ret, orig_credits = handle->h_buffer_credits;
  2195. struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
  2196. struct ocfs2_extent_block *eb;
  2197. struct ocfs2_extent_list *el;
  2198. el = path_leaf_el(path);
  2199. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2200. return 0;
  2201. if (path->p_tree_depth == 0) {
  2202. rightmost_no_delete:
  2203. /*
  2204. * In-inode extents. This is trivially handled, so do
  2205. * it up front.
  2206. */
  2207. ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
  2208. path_leaf_bh(path),
  2209. path_leaf_el(path));
  2210. if (ret)
  2211. mlog_errno(ret);
  2212. goto out;
  2213. }
  2214. /*
  2215. * Handle rightmost branch now. There's several cases:
  2216. * 1) simple rotation leaving records in there. That's trivial.
  2217. * 2) rotation requiring a branch delete - there's no more
  2218. * records left. Two cases of this:
  2219. * a) There are branches to the left.
  2220. * b) This is also the leftmost (the only) branch.
  2221. *
  2222. * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
  2223. * 2a) we need the left branch so that we can update it with the unlink
  2224. * 2b) we need to bring the inode back to inline extents.
  2225. */
  2226. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2227. el = &eb->h_list;
  2228. if (eb->h_next_leaf_blk == 0) {
  2229. /*
  2230. * This gets a bit tricky if we're going to delete the
  2231. * rightmost path. Get the other cases out of the way
  2232. * 1st.
  2233. */
  2234. if (le16_to_cpu(el->l_next_free_rec) > 1)
  2235. goto rightmost_no_delete;
  2236. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  2237. ret = -EIO;
  2238. ocfs2_error(inode->i_sb,
  2239. "Inode %llu has empty extent block at %llu",
  2240. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2241. (unsigned long long)le64_to_cpu(eb->h_blkno));
  2242. goto out;
  2243. }
  2244. /*
  2245. * XXX: The caller can not trust "path" any more after
  2246. * this as it will have been deleted. What do we do?
  2247. *
  2248. * In theory the rotate-for-merge code will never get
  2249. * here because it'll always ask for a rotate in a
  2250. * nonempty list.
  2251. */
  2252. ret = ocfs2_remove_rightmost_path(inode, handle, path,
  2253. dealloc);
  2254. if (ret)
  2255. mlog_errno(ret);
  2256. goto out;
  2257. }
  2258. /*
  2259. * Now we can loop, remembering the path we get from -EAGAIN
  2260. * and restarting from there.
  2261. */
  2262. try_rotate:
  2263. ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
  2264. dealloc, &restart_path);
  2265. if (ret && ret != -EAGAIN) {
  2266. mlog_errno(ret);
  2267. goto out;
  2268. }
  2269. while (ret == -EAGAIN) {
  2270. tmp_path = restart_path;
  2271. restart_path = NULL;
  2272. ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
  2273. tmp_path, dealloc,
  2274. &restart_path);
  2275. if (ret && ret != -EAGAIN) {
  2276. mlog_errno(ret);
  2277. goto out;
  2278. }
  2279. ocfs2_free_path(tmp_path);
  2280. tmp_path = NULL;
  2281. if (ret == 0)
  2282. goto try_rotate;
  2283. }
  2284. out:
  2285. ocfs2_free_path(tmp_path);
  2286. ocfs2_free_path(restart_path);
  2287. return ret;
  2288. }
  2289. static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
  2290. int index)
  2291. {
  2292. struct ocfs2_extent_rec *rec = &el->l_recs[index];
  2293. unsigned int size;
  2294. if (rec->e_leaf_clusters == 0) {
  2295. /*
  2296. * We consumed all of the merged-from record. An empty
  2297. * extent cannot exist anywhere but the 1st array
  2298. * position, so move things over if the merged-from
  2299. * record doesn't occupy that position.
  2300. *
  2301. * This creates a new empty extent so the caller
  2302. * should be smart enough to have removed any existing
  2303. * ones.
  2304. */
  2305. if (index > 0) {
  2306. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2307. size = index * sizeof(struct ocfs2_extent_rec);
  2308. memmove(&el->l_recs[1], &el->l_recs[0], size);
  2309. }
  2310. /*
  2311. * Always memset - the caller doesn't check whether it
  2312. * created an empty extent, so there could be junk in
  2313. * the other fields.
  2314. */
  2315. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2316. }
  2317. }
  2318. /*
  2319. * Remove split_rec clusters from the record at index and merge them
  2320. * onto the beginning of the record at index + 1.
  2321. */
  2322. static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh,
  2323. handle_t *handle,
  2324. struct ocfs2_extent_rec *split_rec,
  2325. struct ocfs2_extent_list *el, int index)
  2326. {
  2327. int ret;
  2328. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2329. struct ocfs2_extent_rec *left_rec;
  2330. struct ocfs2_extent_rec *right_rec;
  2331. BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
  2332. left_rec = &el->l_recs[index];
  2333. right_rec = &el->l_recs[index + 1];
  2334. ret = ocfs2_journal_access(handle, inode, bh,
  2335. OCFS2_JOURNAL_ACCESS_WRITE);
  2336. if (ret) {
  2337. mlog_errno(ret);
  2338. goto out;
  2339. }
  2340. le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
  2341. le32_add_cpu(&right_rec->e_cpos, -split_clusters);
  2342. le64_add_cpu(&right_rec->e_blkno,
  2343. -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
  2344. le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
  2345. ocfs2_cleanup_merge(el, index);
  2346. ret = ocfs2_journal_dirty(handle, bh);
  2347. if (ret)
  2348. mlog_errno(ret);
  2349. out:
  2350. return ret;
  2351. }
  2352. /*
  2353. * Remove split_rec clusters from the record at index and merge them
  2354. * onto the tail of the record at index - 1.
  2355. */
  2356. static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh,
  2357. handle_t *handle,
  2358. struct ocfs2_extent_rec *split_rec,
  2359. struct ocfs2_extent_list *el, int index)
  2360. {
  2361. int ret, has_empty_extent = 0;
  2362. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2363. struct ocfs2_extent_rec *left_rec;
  2364. struct ocfs2_extent_rec *right_rec;
  2365. BUG_ON(index <= 0);
  2366. left_rec = &el->l_recs[index - 1];
  2367. right_rec = &el->l_recs[index];
  2368. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  2369. has_empty_extent = 1;
  2370. ret = ocfs2_journal_access(handle, inode, bh,
  2371. OCFS2_JOURNAL_ACCESS_WRITE);
  2372. if (ret) {
  2373. mlog_errno(ret);
  2374. goto out;
  2375. }
  2376. if (has_empty_extent && index == 1) {
  2377. /*
  2378. * The easy case - we can just plop the record right in.
  2379. */
  2380. *left_rec = *split_rec;
  2381. has_empty_extent = 0;
  2382. } else {
  2383. le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
  2384. }
  2385. le32_add_cpu(&right_rec->e_cpos, split_clusters);
  2386. le64_add_cpu(&right_rec->e_blkno,
  2387. ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
  2388. le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
  2389. ocfs2_cleanup_merge(el, index);
  2390. ret = ocfs2_journal_dirty(handle, bh);
  2391. if (ret)
  2392. mlog_errno(ret);
  2393. out:
  2394. return ret;
  2395. }
  2396. static int ocfs2_try_to_merge_extent(struct inode *inode,
  2397. handle_t *handle,
  2398. struct ocfs2_path *left_path,
  2399. int split_index,
  2400. struct ocfs2_extent_rec *split_rec,
  2401. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2402. struct ocfs2_merge_ctxt *ctxt)
  2403. {
  2404. int ret = 0;
  2405. struct ocfs2_extent_list *el = path_leaf_el(left_path);
  2406. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  2407. BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
  2408. if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
  2409. /*
  2410. * The merge code will need to create an empty
  2411. * extent to take the place of the newly
  2412. * emptied slot. Remove any pre-existing empty
  2413. * extents - having more than one in a leaf is
  2414. * illegal.
  2415. */
  2416. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2417. dealloc);
  2418. if (ret) {
  2419. mlog_errno(ret);
  2420. goto out;
  2421. }
  2422. split_index--;
  2423. rec = &el->l_recs[split_index];
  2424. }
  2425. if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
  2426. /*
  2427. * Left-right contig implies this.
  2428. */
  2429. BUG_ON(!ctxt->c_split_covers_rec);
  2430. BUG_ON(split_index == 0);
  2431. /*
  2432. * Since the leftright insert always covers the entire
  2433. * extent, this call will delete the insert record
  2434. * entirely, resulting in an empty extent record added to
  2435. * the extent block.
  2436. *
  2437. * Since the adding of an empty extent shifts
  2438. * everything back to the right, there's no need to
  2439. * update split_index here.
  2440. */
  2441. ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path),
  2442. handle, split_rec, el, split_index);
  2443. if (ret) {
  2444. mlog_errno(ret);
  2445. goto out;
  2446. }
  2447. /*
  2448. * We can only get this from logic error above.
  2449. */
  2450. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  2451. /*
  2452. * The left merge left us with an empty extent, remove
  2453. * it.
  2454. */
  2455. ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc);
  2456. if (ret) {
  2457. mlog_errno(ret);
  2458. goto out;
  2459. }
  2460. split_index--;
  2461. rec = &el->l_recs[split_index];
  2462. /*
  2463. * Note that we don't pass split_rec here on purpose -
  2464. * we've merged it into the left side.
  2465. */
  2466. ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path),
  2467. handle, rec, el, split_index);
  2468. if (ret) {
  2469. mlog_errno(ret);
  2470. goto out;
  2471. }
  2472. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  2473. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2474. dealloc);
  2475. /*
  2476. * Error from this last rotate is not critical, so
  2477. * print but don't bubble it up.
  2478. */
  2479. if (ret)
  2480. mlog_errno(ret);
  2481. ret = 0;
  2482. } else {
  2483. /*
  2484. * Merge a record to the left or right.
  2485. *
  2486. * 'contig_type' is relative to the existing record,
  2487. * so for example, if we're "right contig", it's to
  2488. * the record on the left (hence the left merge).
  2489. */
  2490. if (ctxt->c_contig_type == CONTIG_RIGHT) {
  2491. ret = ocfs2_merge_rec_left(inode,
  2492. path_leaf_bh(left_path),
  2493. handle, split_rec, el,
  2494. split_index);
  2495. if (ret) {
  2496. mlog_errno(ret);
  2497. goto out;
  2498. }
  2499. } else {
  2500. ret = ocfs2_merge_rec_right(inode,
  2501. path_leaf_bh(left_path),
  2502. handle, split_rec, el,
  2503. split_index);
  2504. if (ret) {
  2505. mlog_errno(ret);
  2506. goto out;
  2507. }
  2508. }
  2509. if (ctxt->c_split_covers_rec) {
  2510. /*
  2511. * The merge may have left an empty extent in
  2512. * our leaf. Try to rotate it away.
  2513. */
  2514. ret = ocfs2_rotate_tree_left(inode, handle, left_path,
  2515. dealloc);
  2516. if (ret)
  2517. mlog_errno(ret);
  2518. ret = 0;
  2519. }
  2520. }
  2521. out:
  2522. return ret;
  2523. }
  2524. static void ocfs2_subtract_from_rec(struct super_block *sb,
  2525. enum ocfs2_split_type split,
  2526. struct ocfs2_extent_rec *rec,
  2527. struct ocfs2_extent_rec *split_rec)
  2528. {
  2529. u64 len_blocks;
  2530. len_blocks = ocfs2_clusters_to_blocks(sb,
  2531. le16_to_cpu(split_rec->e_leaf_clusters));
  2532. if (split == SPLIT_LEFT) {
  2533. /*
  2534. * Region is on the left edge of the existing
  2535. * record.
  2536. */
  2537. le32_add_cpu(&rec->e_cpos,
  2538. le16_to_cpu(split_rec->e_leaf_clusters));
  2539. le64_add_cpu(&rec->e_blkno, len_blocks);
  2540. le16_add_cpu(&rec->e_leaf_clusters,
  2541. -le16_to_cpu(split_rec->e_leaf_clusters));
  2542. } else {
  2543. /*
  2544. * Region is on the right edge of the existing
  2545. * record.
  2546. */
  2547. le16_add_cpu(&rec->e_leaf_clusters,
  2548. -le16_to_cpu(split_rec->e_leaf_clusters));
  2549. }
  2550. }
  2551. /*
  2552. * Do the final bits of extent record insertion at the target leaf
  2553. * list. If this leaf is part of an allocation tree, it is assumed
  2554. * that the tree above has been prepared.
  2555. */
  2556. static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
  2557. struct ocfs2_extent_list *el,
  2558. struct ocfs2_insert_type *insert,
  2559. struct inode *inode)
  2560. {
  2561. int i = insert->ins_contig_index;
  2562. unsigned int range;
  2563. struct ocfs2_extent_rec *rec;
  2564. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  2565. if (insert->ins_split != SPLIT_NONE) {
  2566. i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
  2567. BUG_ON(i == -1);
  2568. rec = &el->l_recs[i];
  2569. ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
  2570. insert_rec);
  2571. goto rotate;
  2572. }
  2573. /*
  2574. * Contiguous insert - either left or right.
  2575. */
  2576. if (insert->ins_contig != CONTIG_NONE) {
  2577. rec = &el->l_recs[i];
  2578. if (insert->ins_contig == CONTIG_LEFT) {
  2579. rec->e_blkno = insert_rec->e_blkno;
  2580. rec->e_cpos = insert_rec->e_cpos;
  2581. }
  2582. le16_add_cpu(&rec->e_leaf_clusters,
  2583. le16_to_cpu(insert_rec->e_leaf_clusters));
  2584. return;
  2585. }
  2586. /*
  2587. * Handle insert into an empty leaf.
  2588. */
  2589. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  2590. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  2591. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  2592. el->l_recs[0] = *insert_rec;
  2593. el->l_next_free_rec = cpu_to_le16(1);
  2594. return;
  2595. }
  2596. /*
  2597. * Appending insert.
  2598. */
  2599. if (insert->ins_appending == APPEND_TAIL) {
  2600. i = le16_to_cpu(el->l_next_free_rec) - 1;
  2601. rec = &el->l_recs[i];
  2602. range = le32_to_cpu(rec->e_cpos)
  2603. + le16_to_cpu(rec->e_leaf_clusters);
  2604. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  2605. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  2606. le16_to_cpu(el->l_count),
  2607. "inode %lu, depth %u, count %u, next free %u, "
  2608. "rec.cpos %u, rec.clusters %u, "
  2609. "insert.cpos %u, insert.clusters %u\n",
  2610. inode->i_ino,
  2611. le16_to_cpu(el->l_tree_depth),
  2612. le16_to_cpu(el->l_count),
  2613. le16_to_cpu(el->l_next_free_rec),
  2614. le32_to_cpu(el->l_recs[i].e_cpos),
  2615. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  2616. le32_to_cpu(insert_rec->e_cpos),
  2617. le16_to_cpu(insert_rec->e_leaf_clusters));
  2618. i++;
  2619. el->l_recs[i] = *insert_rec;
  2620. le16_add_cpu(&el->l_next_free_rec, 1);
  2621. return;
  2622. }
  2623. rotate:
  2624. /*
  2625. * Ok, we have to rotate.
  2626. *
  2627. * At this point, it is safe to assume that inserting into an
  2628. * empty leaf and appending to a leaf have both been handled
  2629. * above.
  2630. *
  2631. * This leaf needs to have space, either by the empty 1st
  2632. * extent record, or by virtue of an l_next_rec < l_count.
  2633. */
  2634. ocfs2_rotate_leaf(el, insert_rec);
  2635. }
  2636. static inline void ocfs2_update_dinode_clusters(struct inode *inode,
  2637. struct ocfs2_dinode *di,
  2638. u32 clusters)
  2639. {
  2640. le32_add_cpu(&di->i_clusters, clusters);
  2641. spin_lock(&OCFS2_I(inode)->ip_lock);
  2642. OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
  2643. spin_unlock(&OCFS2_I(inode)->ip_lock);
  2644. }
  2645. static void ocfs2_adjust_rightmost_records(struct inode *inode,
  2646. handle_t *handle,
  2647. struct ocfs2_path *path,
  2648. struct ocfs2_extent_rec *insert_rec)
  2649. {
  2650. int ret, i, next_free;
  2651. struct buffer_head *bh;
  2652. struct ocfs2_extent_list *el;
  2653. struct ocfs2_extent_rec *rec;
  2654. /*
  2655. * Update everything except the leaf block.
  2656. */
  2657. for (i = 0; i < path->p_tree_depth; i++) {
  2658. bh = path->p_node[i].bh;
  2659. el = path->p_node[i].el;
  2660. next_free = le16_to_cpu(el->l_next_free_rec);
  2661. if (next_free == 0) {
  2662. ocfs2_error(inode->i_sb,
  2663. "Dinode %llu has a bad extent list",
  2664. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2665. ret = -EIO;
  2666. return;
  2667. }
  2668. rec = &el->l_recs[next_free - 1];
  2669. rec->e_int_clusters = insert_rec->e_cpos;
  2670. le32_add_cpu(&rec->e_int_clusters,
  2671. le16_to_cpu(insert_rec->e_leaf_clusters));
  2672. le32_add_cpu(&rec->e_int_clusters,
  2673. -le32_to_cpu(rec->e_cpos));
  2674. ret = ocfs2_journal_dirty(handle, bh);
  2675. if (ret)
  2676. mlog_errno(ret);
  2677. }
  2678. }
  2679. static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
  2680. struct ocfs2_extent_rec *insert_rec,
  2681. struct ocfs2_path *right_path,
  2682. struct ocfs2_path **ret_left_path)
  2683. {
  2684. int ret, next_free;
  2685. struct ocfs2_extent_list *el;
  2686. struct ocfs2_path *left_path = NULL;
  2687. *ret_left_path = NULL;
  2688. /*
  2689. * This shouldn't happen for non-trees. The extent rec cluster
  2690. * count manipulation below only works for interior nodes.
  2691. */
  2692. BUG_ON(right_path->p_tree_depth == 0);
  2693. /*
  2694. * If our appending insert is at the leftmost edge of a leaf,
  2695. * then we might need to update the rightmost records of the
  2696. * neighboring path.
  2697. */
  2698. el = path_leaf_el(right_path);
  2699. next_free = le16_to_cpu(el->l_next_free_rec);
  2700. if (next_free == 0 ||
  2701. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  2702. u32 left_cpos;
  2703. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  2704. &left_cpos);
  2705. if (ret) {
  2706. mlog_errno(ret);
  2707. goto out;
  2708. }
  2709. mlog(0, "Append may need a left path update. cpos: %u, "
  2710. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  2711. left_cpos);
  2712. /*
  2713. * No need to worry if the append is already in the
  2714. * leftmost leaf.
  2715. */
  2716. if (left_cpos) {
  2717. left_path = ocfs2_new_path(path_root_bh(right_path),
  2718. path_root_el(right_path));
  2719. if (!left_path) {
  2720. ret = -ENOMEM;
  2721. mlog_errno(ret);
  2722. goto out;
  2723. }
  2724. ret = ocfs2_find_path(inode, left_path, left_cpos);
  2725. if (ret) {
  2726. mlog_errno(ret);
  2727. goto out;
  2728. }
  2729. /*
  2730. * ocfs2_insert_path() will pass the left_path to the
  2731. * journal for us.
  2732. */
  2733. }
  2734. }
  2735. ret = ocfs2_journal_access_path(inode, handle, right_path);
  2736. if (ret) {
  2737. mlog_errno(ret);
  2738. goto out;
  2739. }
  2740. ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
  2741. *ret_left_path = left_path;
  2742. ret = 0;
  2743. out:
  2744. if (ret != 0)
  2745. ocfs2_free_path(left_path);
  2746. return ret;
  2747. }
  2748. static void ocfs2_split_record(struct inode *inode,
  2749. struct ocfs2_path *left_path,
  2750. struct ocfs2_path *right_path,
  2751. struct ocfs2_extent_rec *split_rec,
  2752. enum ocfs2_split_type split)
  2753. {
  2754. int index;
  2755. u32 cpos = le32_to_cpu(split_rec->e_cpos);
  2756. struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
  2757. struct ocfs2_extent_rec *rec, *tmprec;
  2758. right_el = path_leaf_el(right_path);;
  2759. if (left_path)
  2760. left_el = path_leaf_el(left_path);
  2761. el = right_el;
  2762. insert_el = right_el;
  2763. index = ocfs2_search_extent_list(el, cpos);
  2764. if (index != -1) {
  2765. if (index == 0 && left_path) {
  2766. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2767. /*
  2768. * This typically means that the record
  2769. * started in the left path but moved to the
  2770. * right as a result of rotation. We either
  2771. * move the existing record to the left, or we
  2772. * do the later insert there.
  2773. *
  2774. * In this case, the left path should always
  2775. * exist as the rotate code will have passed
  2776. * it back for a post-insert update.
  2777. */
  2778. if (split == SPLIT_LEFT) {
  2779. /*
  2780. * It's a left split. Since we know
  2781. * that the rotate code gave us an
  2782. * empty extent in the left path, we
  2783. * can just do the insert there.
  2784. */
  2785. insert_el = left_el;
  2786. } else {
  2787. /*
  2788. * Right split - we have to move the
  2789. * existing record over to the left
  2790. * leaf. The insert will be into the
  2791. * newly created empty extent in the
  2792. * right leaf.
  2793. */
  2794. tmprec = &right_el->l_recs[index];
  2795. ocfs2_rotate_leaf(left_el, tmprec);
  2796. el = left_el;
  2797. memset(tmprec, 0, sizeof(*tmprec));
  2798. index = ocfs2_search_extent_list(left_el, cpos);
  2799. BUG_ON(index == -1);
  2800. }
  2801. }
  2802. } else {
  2803. BUG_ON(!left_path);
  2804. BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
  2805. /*
  2806. * Left path is easy - we can just allow the insert to
  2807. * happen.
  2808. */
  2809. el = left_el;
  2810. insert_el = left_el;
  2811. index = ocfs2_search_extent_list(el, cpos);
  2812. BUG_ON(index == -1);
  2813. }
  2814. rec = &el->l_recs[index];
  2815. ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
  2816. ocfs2_rotate_leaf(insert_el, split_rec);
  2817. }
  2818. /*
  2819. * This function only does inserts on an allocation b-tree. For dinode
  2820. * lists, ocfs2_insert_at_leaf() is called directly.
  2821. *
  2822. * right_path is the path we want to do the actual insert
  2823. * in. left_path should only be passed in if we need to update that
  2824. * portion of the tree after an edge insert.
  2825. */
  2826. static int ocfs2_insert_path(struct inode *inode,
  2827. handle_t *handle,
  2828. struct ocfs2_path *left_path,
  2829. struct ocfs2_path *right_path,
  2830. struct ocfs2_extent_rec *insert_rec,
  2831. struct ocfs2_insert_type *insert)
  2832. {
  2833. int ret, subtree_index;
  2834. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  2835. /*
  2836. * Pass both paths to the journal. The majority of inserts
  2837. * will be touching all components anyway.
  2838. */
  2839. ret = ocfs2_journal_access_path(inode, handle, right_path);
  2840. if (ret < 0) {
  2841. mlog_errno(ret);
  2842. goto out;
  2843. }
  2844. if (left_path) {
  2845. int credits = handle->h_buffer_credits;
  2846. /*
  2847. * There's a chance that left_path got passed back to
  2848. * us without being accounted for in the
  2849. * journal. Extend our transaction here to be sure we
  2850. * can change those blocks.
  2851. */
  2852. credits += left_path->p_tree_depth;
  2853. ret = ocfs2_extend_trans(handle, credits);
  2854. if (ret < 0) {
  2855. mlog_errno(ret);
  2856. goto out;
  2857. }
  2858. ret = ocfs2_journal_access_path(inode, handle, left_path);
  2859. if (ret < 0) {
  2860. mlog_errno(ret);
  2861. goto out;
  2862. }
  2863. }
  2864. if (insert->ins_split != SPLIT_NONE) {
  2865. /*
  2866. * We could call ocfs2_insert_at_leaf() for some types
  2867. * of splits, but it's easier to just let one seperate
  2868. * function sort it all out.
  2869. */
  2870. ocfs2_split_record(inode, left_path, right_path,
  2871. insert_rec, insert->ins_split);
  2872. } else
  2873. ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
  2874. insert, inode);
  2875. ret = ocfs2_journal_dirty(handle, leaf_bh);
  2876. if (ret)
  2877. mlog_errno(ret);
  2878. if (left_path) {
  2879. /*
  2880. * The rotate code has indicated that we need to fix
  2881. * up portions of the tree after the insert.
  2882. *
  2883. * XXX: Should we extend the transaction here?
  2884. */
  2885. subtree_index = ocfs2_find_subtree_root(inode, left_path,
  2886. right_path);
  2887. ocfs2_complete_edge_insert(inode, handle, left_path,
  2888. right_path, subtree_index);
  2889. }
  2890. ret = 0;
  2891. out:
  2892. return ret;
  2893. }
  2894. static int ocfs2_do_insert_extent(struct inode *inode,
  2895. handle_t *handle,
  2896. struct buffer_head *di_bh,
  2897. struct ocfs2_extent_rec *insert_rec,
  2898. struct ocfs2_insert_type *type)
  2899. {
  2900. int ret, rotate = 0;
  2901. u32 cpos;
  2902. struct ocfs2_path *right_path = NULL;
  2903. struct ocfs2_path *left_path = NULL;
  2904. struct ocfs2_dinode *di;
  2905. struct ocfs2_extent_list *el;
  2906. di = (struct ocfs2_dinode *) di_bh->b_data;
  2907. el = &di->id2.i_list;
  2908. ret = ocfs2_journal_access(handle, inode, di_bh,
  2909. OCFS2_JOURNAL_ACCESS_WRITE);
  2910. if (ret) {
  2911. mlog_errno(ret);
  2912. goto out;
  2913. }
  2914. if (le16_to_cpu(el->l_tree_depth) == 0) {
  2915. ocfs2_insert_at_leaf(insert_rec, el, type, inode);
  2916. goto out_update_clusters;
  2917. }
  2918. right_path = ocfs2_new_inode_path(di_bh);
  2919. if (!right_path) {
  2920. ret = -ENOMEM;
  2921. mlog_errno(ret);
  2922. goto out;
  2923. }
  2924. /*
  2925. * Determine the path to start with. Rotations need the
  2926. * rightmost path, everything else can go directly to the
  2927. * target leaf.
  2928. */
  2929. cpos = le32_to_cpu(insert_rec->e_cpos);
  2930. if (type->ins_appending == APPEND_NONE &&
  2931. type->ins_contig == CONTIG_NONE) {
  2932. rotate = 1;
  2933. cpos = UINT_MAX;
  2934. }
  2935. ret = ocfs2_find_path(inode, right_path, cpos);
  2936. if (ret) {
  2937. mlog_errno(ret);
  2938. goto out;
  2939. }
  2940. /*
  2941. * Rotations and appends need special treatment - they modify
  2942. * parts of the tree's above them.
  2943. *
  2944. * Both might pass back a path immediate to the left of the
  2945. * one being inserted to. This will be cause
  2946. * ocfs2_insert_path() to modify the rightmost records of
  2947. * left_path to account for an edge insert.
  2948. *
  2949. * XXX: When modifying this code, keep in mind that an insert
  2950. * can wind up skipping both of these two special cases...
  2951. */
  2952. if (rotate) {
  2953. ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
  2954. le32_to_cpu(insert_rec->e_cpos),
  2955. right_path, &left_path);
  2956. if (ret) {
  2957. mlog_errno(ret);
  2958. goto out;
  2959. }
  2960. } else if (type->ins_appending == APPEND_TAIL
  2961. && type->ins_contig != CONTIG_LEFT) {
  2962. ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
  2963. right_path, &left_path);
  2964. if (ret) {
  2965. mlog_errno(ret);
  2966. goto out;
  2967. }
  2968. }
  2969. ret = ocfs2_insert_path(inode, handle, left_path, right_path,
  2970. insert_rec, type);
  2971. if (ret) {
  2972. mlog_errno(ret);
  2973. goto out;
  2974. }
  2975. out_update_clusters:
  2976. if (type->ins_split == SPLIT_NONE)
  2977. ocfs2_update_dinode_clusters(inode, di,
  2978. le16_to_cpu(insert_rec->e_leaf_clusters));
  2979. ret = ocfs2_journal_dirty(handle, di_bh);
  2980. if (ret)
  2981. mlog_errno(ret);
  2982. out:
  2983. ocfs2_free_path(left_path);
  2984. ocfs2_free_path(right_path);
  2985. return ret;
  2986. }
  2987. static enum ocfs2_contig_type
  2988. ocfs2_figure_merge_contig_type(struct inode *inode,
  2989. struct ocfs2_extent_list *el, int index,
  2990. struct ocfs2_extent_rec *split_rec)
  2991. {
  2992. struct ocfs2_extent_rec *rec;
  2993. enum ocfs2_contig_type ret = CONTIG_NONE;
  2994. /*
  2995. * We're careful to check for an empty extent record here -
  2996. * the merge code will know what to do if it sees one.
  2997. */
  2998. if (index > 0) {
  2999. rec = &el->l_recs[index - 1];
  3000. if (index == 1 && ocfs2_is_empty_extent(rec)) {
  3001. if (split_rec->e_cpos == el->l_recs[index].e_cpos)
  3002. ret = CONTIG_RIGHT;
  3003. } else {
  3004. ret = ocfs2_extent_contig(inode, rec, split_rec);
  3005. }
  3006. }
  3007. if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) {
  3008. enum ocfs2_contig_type contig_type;
  3009. rec = &el->l_recs[index + 1];
  3010. contig_type = ocfs2_extent_contig(inode, rec, split_rec);
  3011. if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
  3012. ret = CONTIG_LEFTRIGHT;
  3013. else if (ret == CONTIG_NONE)
  3014. ret = contig_type;
  3015. }
  3016. return ret;
  3017. }
  3018. static void ocfs2_figure_contig_type(struct inode *inode,
  3019. struct ocfs2_insert_type *insert,
  3020. struct ocfs2_extent_list *el,
  3021. struct ocfs2_extent_rec *insert_rec)
  3022. {
  3023. int i;
  3024. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  3025. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3026. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  3027. contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
  3028. insert_rec);
  3029. if (contig_type != CONTIG_NONE) {
  3030. insert->ins_contig_index = i;
  3031. break;
  3032. }
  3033. }
  3034. insert->ins_contig = contig_type;
  3035. }
  3036. /*
  3037. * This should only be called against the righmost leaf extent list.
  3038. *
  3039. * ocfs2_figure_appending_type() will figure out whether we'll have to
  3040. * insert at the tail of the rightmost leaf.
  3041. *
  3042. * This should also work against the dinode list for tree's with 0
  3043. * depth. If we consider the dinode list to be the rightmost leaf node
  3044. * then the logic here makes sense.
  3045. */
  3046. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  3047. struct ocfs2_extent_list *el,
  3048. struct ocfs2_extent_rec *insert_rec)
  3049. {
  3050. int i;
  3051. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  3052. struct ocfs2_extent_rec *rec;
  3053. insert->ins_appending = APPEND_NONE;
  3054. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3055. if (!el->l_next_free_rec)
  3056. goto set_tail_append;
  3057. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  3058. /* Were all records empty? */
  3059. if (le16_to_cpu(el->l_next_free_rec) == 1)
  3060. goto set_tail_append;
  3061. }
  3062. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3063. rec = &el->l_recs[i];
  3064. if (cpos >=
  3065. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  3066. goto set_tail_append;
  3067. return;
  3068. set_tail_append:
  3069. insert->ins_appending = APPEND_TAIL;
  3070. }
  3071. /*
  3072. * Helper function called at the begining of an insert.
  3073. *
  3074. * This computes a few things that are commonly used in the process of
  3075. * inserting into the btree:
  3076. * - Whether the new extent is contiguous with an existing one.
  3077. * - The current tree depth.
  3078. * - Whether the insert is an appending one.
  3079. * - The total # of free records in the tree.
  3080. *
  3081. * All of the information is stored on the ocfs2_insert_type
  3082. * structure.
  3083. */
  3084. static int ocfs2_figure_insert_type(struct inode *inode,
  3085. struct buffer_head *di_bh,
  3086. struct buffer_head **last_eb_bh,
  3087. struct ocfs2_extent_rec *insert_rec,
  3088. int *free_records,
  3089. struct ocfs2_insert_type *insert)
  3090. {
  3091. int ret;
  3092. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3093. struct ocfs2_extent_block *eb;
  3094. struct ocfs2_extent_list *el;
  3095. struct ocfs2_path *path = NULL;
  3096. struct buffer_head *bh = NULL;
  3097. insert->ins_split = SPLIT_NONE;
  3098. el = &di->id2.i_list;
  3099. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  3100. if (el->l_tree_depth) {
  3101. /*
  3102. * If we have tree depth, we read in the
  3103. * rightmost extent block ahead of time as
  3104. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  3105. * may want it later.
  3106. */
  3107. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3108. le64_to_cpu(di->i_last_eb_blk), &bh,
  3109. OCFS2_BH_CACHED, inode);
  3110. if (ret) {
  3111. mlog_exit(ret);
  3112. goto out;
  3113. }
  3114. eb = (struct ocfs2_extent_block *) bh->b_data;
  3115. el = &eb->h_list;
  3116. }
  3117. /*
  3118. * Unless we have a contiguous insert, we'll need to know if
  3119. * there is room left in our allocation tree for another
  3120. * extent record.
  3121. *
  3122. * XXX: This test is simplistic, we can search for empty
  3123. * extent records too.
  3124. */
  3125. *free_records = le16_to_cpu(el->l_count) -
  3126. le16_to_cpu(el->l_next_free_rec);
  3127. if (!insert->ins_tree_depth) {
  3128. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  3129. ocfs2_figure_appending_type(insert, el, insert_rec);
  3130. return 0;
  3131. }
  3132. path = ocfs2_new_inode_path(di_bh);
  3133. if (!path) {
  3134. ret = -ENOMEM;
  3135. mlog_errno(ret);
  3136. goto out;
  3137. }
  3138. /*
  3139. * In the case that we're inserting past what the tree
  3140. * currently accounts for, ocfs2_find_path() will return for
  3141. * us the rightmost tree path. This is accounted for below in
  3142. * the appending code.
  3143. */
  3144. ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
  3145. if (ret) {
  3146. mlog_errno(ret);
  3147. goto out;
  3148. }
  3149. el = path_leaf_el(path);
  3150. /*
  3151. * Now that we have the path, there's two things we want to determine:
  3152. * 1) Contiguousness (also set contig_index if this is so)
  3153. *
  3154. * 2) Are we doing an append? We can trivially break this up
  3155. * into two types of appends: simple record append, or a
  3156. * rotate inside the tail leaf.
  3157. */
  3158. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  3159. /*
  3160. * The insert code isn't quite ready to deal with all cases of
  3161. * left contiguousness. Specifically, if it's an insert into
  3162. * the 1st record in a leaf, it will require the adjustment of
  3163. * cluster count on the last record of the path directly to it's
  3164. * left. For now, just catch that case and fool the layers
  3165. * above us. This works just fine for tree_depth == 0, which
  3166. * is why we allow that above.
  3167. */
  3168. if (insert->ins_contig == CONTIG_LEFT &&
  3169. insert->ins_contig_index == 0)
  3170. insert->ins_contig = CONTIG_NONE;
  3171. /*
  3172. * Ok, so we can simply compare against last_eb to figure out
  3173. * whether the path doesn't exist. This will only happen in
  3174. * the case that we're doing a tail append, so maybe we can
  3175. * take advantage of that information somehow.
  3176. */
  3177. if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
  3178. /*
  3179. * Ok, ocfs2_find_path() returned us the rightmost
  3180. * tree path. This might be an appending insert. There are
  3181. * two cases:
  3182. * 1) We're doing a true append at the tail:
  3183. * -This might even be off the end of the leaf
  3184. * 2) We're "appending" by rotating in the tail
  3185. */
  3186. ocfs2_figure_appending_type(insert, el, insert_rec);
  3187. }
  3188. out:
  3189. ocfs2_free_path(path);
  3190. if (ret == 0)
  3191. *last_eb_bh = bh;
  3192. else
  3193. brelse(bh);
  3194. return ret;
  3195. }
  3196. /*
  3197. * Insert an extent into an inode btree.
  3198. *
  3199. * The caller needs to update fe->i_clusters
  3200. */
  3201. int ocfs2_insert_extent(struct ocfs2_super *osb,
  3202. handle_t *handle,
  3203. struct inode *inode,
  3204. struct buffer_head *fe_bh,
  3205. u32 cpos,
  3206. u64 start_blk,
  3207. u32 new_clusters,
  3208. u8 flags,
  3209. struct ocfs2_alloc_context *meta_ac)
  3210. {
  3211. int status;
  3212. int uninitialized_var(free_records);
  3213. struct buffer_head *last_eb_bh = NULL;
  3214. struct ocfs2_insert_type insert = {0, };
  3215. struct ocfs2_extent_rec rec;
  3216. mlog(0, "add %u clusters at position %u to inode %llu\n",
  3217. new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3218. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  3219. (OCFS2_I(inode)->ip_clusters != cpos),
  3220. "Device %s, asking for sparse allocation: inode %llu, "
  3221. "cpos %u, clusters %u\n",
  3222. osb->dev_str,
  3223. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
  3224. OCFS2_I(inode)->ip_clusters);
  3225. memset(&rec, 0, sizeof(rec));
  3226. rec.e_cpos = cpu_to_le32(cpos);
  3227. rec.e_blkno = cpu_to_le64(start_blk);
  3228. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  3229. rec.e_flags = flags;
  3230. status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
  3231. &free_records, &insert);
  3232. if (status < 0) {
  3233. mlog_errno(status);
  3234. goto bail;
  3235. }
  3236. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  3237. "Insert.contig_index: %d, Insert.free_records: %d, "
  3238. "Insert.tree_depth: %d\n",
  3239. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  3240. free_records, insert.ins_tree_depth);
  3241. if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
  3242. status = ocfs2_grow_tree(inode, handle, fe_bh,
  3243. &insert.ins_tree_depth, &last_eb_bh,
  3244. meta_ac);
  3245. if (status) {
  3246. mlog_errno(status);
  3247. goto bail;
  3248. }
  3249. }
  3250. /* Finally, we can add clusters. This might rotate the tree for us. */
  3251. status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
  3252. if (status < 0)
  3253. mlog_errno(status);
  3254. else
  3255. ocfs2_extent_map_insert_rec(inode, &rec);
  3256. bail:
  3257. if (last_eb_bh)
  3258. brelse(last_eb_bh);
  3259. mlog_exit(status);
  3260. return status;
  3261. }
  3262. static void ocfs2_make_right_split_rec(struct super_block *sb,
  3263. struct ocfs2_extent_rec *split_rec,
  3264. u32 cpos,
  3265. struct ocfs2_extent_rec *rec)
  3266. {
  3267. u32 rec_cpos = le32_to_cpu(rec->e_cpos);
  3268. u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
  3269. memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
  3270. split_rec->e_cpos = cpu_to_le32(cpos);
  3271. split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
  3272. split_rec->e_blkno = rec->e_blkno;
  3273. le64_add_cpu(&split_rec->e_blkno,
  3274. ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
  3275. split_rec->e_flags = rec->e_flags;
  3276. }
  3277. static int ocfs2_split_and_insert(struct inode *inode,
  3278. handle_t *handle,
  3279. struct ocfs2_path *path,
  3280. struct buffer_head *di_bh,
  3281. struct buffer_head **last_eb_bh,
  3282. int split_index,
  3283. struct ocfs2_extent_rec *orig_split_rec,
  3284. struct ocfs2_alloc_context *meta_ac)
  3285. {
  3286. int ret = 0, depth;
  3287. unsigned int insert_range, rec_range, do_leftright = 0;
  3288. struct ocfs2_extent_rec tmprec;
  3289. struct ocfs2_extent_list *rightmost_el;
  3290. struct ocfs2_extent_rec rec;
  3291. struct ocfs2_extent_rec split_rec = *orig_split_rec;
  3292. struct ocfs2_insert_type insert;
  3293. struct ocfs2_extent_block *eb;
  3294. struct ocfs2_dinode *di;
  3295. leftright:
  3296. /*
  3297. * Store a copy of the record on the stack - it might move
  3298. * around as the tree is manipulated below.
  3299. */
  3300. rec = path_leaf_el(path)->l_recs[split_index];
  3301. di = (struct ocfs2_dinode *)di_bh->b_data;
  3302. rightmost_el = &di->id2.i_list;
  3303. depth = le16_to_cpu(rightmost_el->l_tree_depth);
  3304. if (depth) {
  3305. BUG_ON(!(*last_eb_bh));
  3306. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  3307. rightmost_el = &eb->h_list;
  3308. }
  3309. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  3310. le16_to_cpu(rightmost_el->l_count)) {
  3311. ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh,
  3312. meta_ac);
  3313. if (ret) {
  3314. mlog_errno(ret);
  3315. goto out;
  3316. }
  3317. }
  3318. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  3319. insert.ins_appending = APPEND_NONE;
  3320. insert.ins_contig = CONTIG_NONE;
  3321. insert.ins_tree_depth = depth;
  3322. insert_range = le32_to_cpu(split_rec.e_cpos) +
  3323. le16_to_cpu(split_rec.e_leaf_clusters);
  3324. rec_range = le32_to_cpu(rec.e_cpos) +
  3325. le16_to_cpu(rec.e_leaf_clusters);
  3326. if (split_rec.e_cpos == rec.e_cpos) {
  3327. insert.ins_split = SPLIT_LEFT;
  3328. } else if (insert_range == rec_range) {
  3329. insert.ins_split = SPLIT_RIGHT;
  3330. } else {
  3331. /*
  3332. * Left/right split. We fake this as a right split
  3333. * first and then make a second pass as a left split.
  3334. */
  3335. insert.ins_split = SPLIT_RIGHT;
  3336. ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
  3337. &rec);
  3338. split_rec = tmprec;
  3339. BUG_ON(do_leftright);
  3340. do_leftright = 1;
  3341. }
  3342. ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec,
  3343. &insert);
  3344. if (ret) {
  3345. mlog_errno(ret);
  3346. goto out;
  3347. }
  3348. if (do_leftright == 1) {
  3349. u32 cpos;
  3350. struct ocfs2_extent_list *el;
  3351. do_leftright++;
  3352. split_rec = *orig_split_rec;
  3353. ocfs2_reinit_path(path, 1);
  3354. cpos = le32_to_cpu(split_rec.e_cpos);
  3355. ret = ocfs2_find_path(inode, path, cpos);
  3356. if (ret) {
  3357. mlog_errno(ret);
  3358. goto out;
  3359. }
  3360. el = path_leaf_el(path);
  3361. split_index = ocfs2_search_extent_list(el, cpos);
  3362. goto leftright;
  3363. }
  3364. out:
  3365. return ret;
  3366. }
  3367. /*
  3368. * Mark part or all of the extent record at split_index in the leaf
  3369. * pointed to by path as written. This removes the unwritten
  3370. * extent flag.
  3371. *
  3372. * Care is taken to handle contiguousness so as to not grow the tree.
  3373. *
  3374. * meta_ac is not strictly necessary - we only truly need it if growth
  3375. * of the tree is required. All other cases will degrade into a less
  3376. * optimal tree layout.
  3377. *
  3378. * last_eb_bh should be the rightmost leaf block for any inode with a
  3379. * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call.
  3380. *
  3381. * This code is optimized for readability - several passes might be
  3382. * made over certain portions of the tree. All of those blocks will
  3383. * have been brought into cache (and pinned via the journal), so the
  3384. * extra overhead is not expressed in terms of disk reads.
  3385. */
  3386. static int __ocfs2_mark_extent_written(struct inode *inode,
  3387. struct buffer_head *di_bh,
  3388. handle_t *handle,
  3389. struct ocfs2_path *path,
  3390. int split_index,
  3391. struct ocfs2_extent_rec *split_rec,
  3392. struct ocfs2_alloc_context *meta_ac,
  3393. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3394. {
  3395. int ret = 0;
  3396. struct ocfs2_extent_list *el = path_leaf_el(path);
  3397. struct buffer_head *eb_bh, *last_eb_bh = NULL;
  3398. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  3399. struct ocfs2_merge_ctxt ctxt;
  3400. struct ocfs2_extent_list *rightmost_el;
  3401. if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) {
  3402. ret = -EIO;
  3403. mlog_errno(ret);
  3404. goto out;
  3405. }
  3406. if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
  3407. ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
  3408. (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
  3409. ret = -EIO;
  3410. mlog_errno(ret);
  3411. goto out;
  3412. }
  3413. eb_bh = path_leaf_bh(path);
  3414. ret = ocfs2_journal_access(handle, inode, eb_bh,
  3415. OCFS2_JOURNAL_ACCESS_WRITE);
  3416. if (ret) {
  3417. mlog_errno(ret);
  3418. goto out;
  3419. }
  3420. ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el,
  3421. split_index,
  3422. split_rec);
  3423. /*
  3424. * The core merge / split code wants to know how much room is
  3425. * left in this inodes allocation tree, so we pass the
  3426. * rightmost extent list.
  3427. */
  3428. if (path->p_tree_depth) {
  3429. struct ocfs2_extent_block *eb;
  3430. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3431. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3432. le64_to_cpu(di->i_last_eb_blk),
  3433. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3434. if (ret) {
  3435. mlog_exit(ret);
  3436. goto out;
  3437. }
  3438. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3439. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  3440. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  3441. ret = -EROFS;
  3442. goto out;
  3443. }
  3444. rightmost_el = &eb->h_list;
  3445. } else
  3446. rightmost_el = path_root_el(path);
  3447. ctxt.c_used_tail_recs = le16_to_cpu(rightmost_el->l_next_free_rec);
  3448. if (ctxt.c_used_tail_recs > 0 &&
  3449. ocfs2_is_empty_extent(&rightmost_el->l_recs[0]))
  3450. ctxt.c_used_tail_recs--;
  3451. if (rec->e_cpos == split_rec->e_cpos &&
  3452. rec->e_leaf_clusters == split_rec->e_leaf_clusters)
  3453. ctxt.c_split_covers_rec = 1;
  3454. else
  3455. ctxt.c_split_covers_rec = 0;
  3456. ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
  3457. mlog(0, "index: %d, contig: %u, used_tail_recs: %u, "
  3458. "has_empty: %u, split_covers: %u\n", split_index,
  3459. ctxt.c_contig_type, ctxt.c_used_tail_recs,
  3460. ctxt.c_has_empty_extent, ctxt.c_split_covers_rec);
  3461. if (ctxt.c_contig_type == CONTIG_NONE) {
  3462. if (ctxt.c_split_covers_rec)
  3463. el->l_recs[split_index] = *split_rec;
  3464. else
  3465. ret = ocfs2_split_and_insert(inode, handle, path, di_bh,
  3466. &last_eb_bh, split_index,
  3467. split_rec, meta_ac);
  3468. if (ret)
  3469. mlog_errno(ret);
  3470. } else {
  3471. ret = ocfs2_try_to_merge_extent(inode, handle, path,
  3472. split_index, split_rec,
  3473. dealloc, &ctxt);
  3474. if (ret)
  3475. mlog_errno(ret);
  3476. }
  3477. ocfs2_journal_dirty(handle, eb_bh);
  3478. out:
  3479. brelse(last_eb_bh);
  3480. return ret;
  3481. }
  3482. /*
  3483. * Mark the already-existing extent at cpos as written for len clusters.
  3484. *
  3485. * If the existing extent is larger than the request, initiate a
  3486. * split. An attempt will be made at merging with adjacent extents.
  3487. *
  3488. * The caller is responsible for passing down meta_ac if we'll need it.
  3489. */
  3490. int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh,
  3491. handle_t *handle, u32 cpos, u32 len, u32 phys,
  3492. struct ocfs2_alloc_context *meta_ac,
  3493. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3494. {
  3495. int ret, index;
  3496. u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
  3497. struct ocfs2_extent_rec split_rec;
  3498. struct ocfs2_path *left_path = NULL;
  3499. struct ocfs2_extent_list *el;
  3500. mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
  3501. inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
  3502. if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
  3503. ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
  3504. "that are being written to, but the feature bit "
  3505. "is not set in the super block.",
  3506. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3507. ret = -EROFS;
  3508. goto out;
  3509. }
  3510. /*
  3511. * XXX: This should be fixed up so that we just re-insert the
  3512. * next extent records.
  3513. */
  3514. ocfs2_extent_map_trunc(inode, 0);
  3515. left_path = ocfs2_new_inode_path(di_bh);
  3516. if (!left_path) {
  3517. ret = -ENOMEM;
  3518. mlog_errno(ret);
  3519. goto out;
  3520. }
  3521. ret = ocfs2_find_path(inode, left_path, cpos);
  3522. if (ret) {
  3523. mlog_errno(ret);
  3524. goto out;
  3525. }
  3526. el = path_leaf_el(left_path);
  3527. index = ocfs2_search_extent_list(el, cpos);
  3528. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3529. ocfs2_error(inode->i_sb,
  3530. "Inode %llu has an extent at cpos %u which can no "
  3531. "longer be found.\n",
  3532. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
  3533. ret = -EROFS;
  3534. goto out;
  3535. }
  3536. memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
  3537. split_rec.e_cpos = cpu_to_le32(cpos);
  3538. split_rec.e_leaf_clusters = cpu_to_le16(len);
  3539. split_rec.e_blkno = cpu_to_le64(start_blkno);
  3540. split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
  3541. split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
  3542. ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path,
  3543. index, &split_rec, meta_ac, dealloc);
  3544. if (ret)
  3545. mlog_errno(ret);
  3546. out:
  3547. ocfs2_free_path(left_path);
  3548. return ret;
  3549. }
  3550. static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh,
  3551. handle_t *handle, struct ocfs2_path *path,
  3552. int index, u32 new_range,
  3553. struct ocfs2_alloc_context *meta_ac)
  3554. {
  3555. int ret, depth, credits = handle->h_buffer_credits;
  3556. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  3557. struct buffer_head *last_eb_bh = NULL;
  3558. struct ocfs2_extent_block *eb;
  3559. struct ocfs2_extent_list *rightmost_el, *el;
  3560. struct ocfs2_extent_rec split_rec;
  3561. struct ocfs2_extent_rec *rec;
  3562. struct ocfs2_insert_type insert;
  3563. /*
  3564. * Setup the record to split before we grow the tree.
  3565. */
  3566. el = path_leaf_el(path);
  3567. rec = &el->l_recs[index];
  3568. ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
  3569. depth = path->p_tree_depth;
  3570. if (depth > 0) {
  3571. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  3572. le64_to_cpu(di->i_last_eb_blk),
  3573. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3574. if (ret < 0) {
  3575. mlog_errno(ret);
  3576. goto out;
  3577. }
  3578. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3579. rightmost_el = &eb->h_list;
  3580. } else
  3581. rightmost_el = path_leaf_el(path);
  3582. credits += path->p_tree_depth + ocfs2_extend_meta_needed(di);
  3583. ret = ocfs2_extend_trans(handle, credits);
  3584. if (ret) {
  3585. mlog_errno(ret);
  3586. goto out;
  3587. }
  3588. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  3589. le16_to_cpu(rightmost_el->l_count)) {
  3590. ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh,
  3591. meta_ac);
  3592. if (ret) {
  3593. mlog_errno(ret);
  3594. goto out;
  3595. }
  3596. }
  3597. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  3598. insert.ins_appending = APPEND_NONE;
  3599. insert.ins_contig = CONTIG_NONE;
  3600. insert.ins_split = SPLIT_RIGHT;
  3601. insert.ins_tree_depth = depth;
  3602. ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert);
  3603. if (ret)
  3604. mlog_errno(ret);
  3605. out:
  3606. brelse(last_eb_bh);
  3607. return ret;
  3608. }
  3609. static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
  3610. struct ocfs2_path *path, int index,
  3611. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3612. u32 cpos, u32 len)
  3613. {
  3614. int ret;
  3615. u32 left_cpos, rec_range, trunc_range;
  3616. int wants_rotate = 0, is_rightmost_tree_rec = 0;
  3617. struct super_block *sb = inode->i_sb;
  3618. struct ocfs2_path *left_path = NULL;
  3619. struct ocfs2_extent_list *el = path_leaf_el(path);
  3620. struct ocfs2_extent_rec *rec;
  3621. struct ocfs2_extent_block *eb;
  3622. if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
  3623. ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
  3624. if (ret) {
  3625. mlog_errno(ret);
  3626. goto out;
  3627. }
  3628. index--;
  3629. }
  3630. if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
  3631. path->p_tree_depth) {
  3632. /*
  3633. * Check whether this is the rightmost tree record. If
  3634. * we remove all of this record or part of its right
  3635. * edge then an update of the record lengths above it
  3636. * will be required.
  3637. */
  3638. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  3639. if (eb->h_next_leaf_blk == 0)
  3640. is_rightmost_tree_rec = 1;
  3641. }
  3642. rec = &el->l_recs[index];
  3643. if (index == 0 && path->p_tree_depth &&
  3644. le32_to_cpu(rec->e_cpos) == cpos) {
  3645. /*
  3646. * Changing the leftmost offset (via partial or whole
  3647. * record truncate) of an interior (or rightmost) path
  3648. * means we have to update the subtree that is formed
  3649. * by this leaf and the one to it's left.
  3650. *
  3651. * There are two cases we can skip:
  3652. * 1) Path is the leftmost one in our inode tree.
  3653. * 2) The leaf is rightmost and will be empty after
  3654. * we remove the extent record - the rotate code
  3655. * knows how to update the newly formed edge.
  3656. */
  3657. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
  3658. &left_cpos);
  3659. if (ret) {
  3660. mlog_errno(ret);
  3661. goto out;
  3662. }
  3663. if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
  3664. left_path = ocfs2_new_path(path_root_bh(path),
  3665. path_root_el(path));
  3666. if (!left_path) {
  3667. ret = -ENOMEM;
  3668. mlog_errno(ret);
  3669. goto out;
  3670. }
  3671. ret = ocfs2_find_path(inode, left_path, left_cpos);
  3672. if (ret) {
  3673. mlog_errno(ret);
  3674. goto out;
  3675. }
  3676. }
  3677. }
  3678. ret = ocfs2_extend_rotate_transaction(handle, 0,
  3679. handle->h_buffer_credits,
  3680. path);
  3681. if (ret) {
  3682. mlog_errno(ret);
  3683. goto out;
  3684. }
  3685. ret = ocfs2_journal_access_path(inode, handle, path);
  3686. if (ret) {
  3687. mlog_errno(ret);
  3688. goto out;
  3689. }
  3690. ret = ocfs2_journal_access_path(inode, handle, left_path);
  3691. if (ret) {
  3692. mlog_errno(ret);
  3693. goto out;
  3694. }
  3695. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  3696. trunc_range = cpos + len;
  3697. if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
  3698. int next_free;
  3699. memset(rec, 0, sizeof(*rec));
  3700. ocfs2_cleanup_merge(el, index);
  3701. wants_rotate = 1;
  3702. next_free = le16_to_cpu(el->l_next_free_rec);
  3703. if (is_rightmost_tree_rec && next_free > 1) {
  3704. /*
  3705. * We skip the edge update if this path will
  3706. * be deleted by the rotate code.
  3707. */
  3708. rec = &el->l_recs[next_free - 1];
  3709. ocfs2_adjust_rightmost_records(inode, handle, path,
  3710. rec);
  3711. }
  3712. } else if (le32_to_cpu(rec->e_cpos) == cpos) {
  3713. /* Remove leftmost portion of the record. */
  3714. le32_add_cpu(&rec->e_cpos, len);
  3715. le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
  3716. le16_add_cpu(&rec->e_leaf_clusters, -len);
  3717. } else if (rec_range == trunc_range) {
  3718. /* Remove rightmost portion of the record */
  3719. le16_add_cpu(&rec->e_leaf_clusters, -len);
  3720. if (is_rightmost_tree_rec)
  3721. ocfs2_adjust_rightmost_records(inode, handle, path, rec);
  3722. } else {
  3723. /* Caller should have trapped this. */
  3724. mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
  3725. "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3726. le32_to_cpu(rec->e_cpos),
  3727. le16_to_cpu(rec->e_leaf_clusters), cpos, len);
  3728. BUG();
  3729. }
  3730. if (left_path) {
  3731. int subtree_index;
  3732. subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
  3733. ocfs2_complete_edge_insert(inode, handle, left_path, path,
  3734. subtree_index);
  3735. }
  3736. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  3737. ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc);
  3738. if (ret) {
  3739. mlog_errno(ret);
  3740. goto out;
  3741. }
  3742. out:
  3743. ocfs2_free_path(left_path);
  3744. return ret;
  3745. }
  3746. int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh,
  3747. u32 cpos, u32 len, handle_t *handle,
  3748. struct ocfs2_alloc_context *meta_ac,
  3749. struct ocfs2_cached_dealloc_ctxt *dealloc)
  3750. {
  3751. int ret, index;
  3752. u32 rec_range, trunc_range;
  3753. struct ocfs2_extent_rec *rec;
  3754. struct ocfs2_extent_list *el;
  3755. struct ocfs2_path *path;
  3756. ocfs2_extent_map_trunc(inode, 0);
  3757. path = ocfs2_new_inode_path(di_bh);
  3758. if (!path) {
  3759. ret = -ENOMEM;
  3760. mlog_errno(ret);
  3761. goto out;
  3762. }
  3763. ret = ocfs2_find_path(inode, path, cpos);
  3764. if (ret) {
  3765. mlog_errno(ret);
  3766. goto out;
  3767. }
  3768. el = path_leaf_el(path);
  3769. index = ocfs2_search_extent_list(el, cpos);
  3770. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3771. ocfs2_error(inode->i_sb,
  3772. "Inode %llu has an extent at cpos %u which can no "
  3773. "longer be found.\n",
  3774. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
  3775. ret = -EROFS;
  3776. goto out;
  3777. }
  3778. /*
  3779. * We have 3 cases of extent removal:
  3780. * 1) Range covers the entire extent rec
  3781. * 2) Range begins or ends on one edge of the extent rec
  3782. * 3) Range is in the middle of the extent rec (no shared edges)
  3783. *
  3784. * For case 1 we remove the extent rec and left rotate to
  3785. * fill the hole.
  3786. *
  3787. * For case 2 we just shrink the existing extent rec, with a
  3788. * tree update if the shrinking edge is also the edge of an
  3789. * extent block.
  3790. *
  3791. * For case 3 we do a right split to turn the extent rec into
  3792. * something case 2 can handle.
  3793. */
  3794. rec = &el->l_recs[index];
  3795. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  3796. trunc_range = cpos + len;
  3797. BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
  3798. mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
  3799. "(cpos %u, len %u)\n",
  3800. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
  3801. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
  3802. if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
  3803. ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
  3804. cpos, len);
  3805. if (ret) {
  3806. mlog_errno(ret);
  3807. goto out;
  3808. }
  3809. } else {
  3810. ret = ocfs2_split_tree(inode, di_bh, handle, path, index,
  3811. trunc_range, meta_ac);
  3812. if (ret) {
  3813. mlog_errno(ret);
  3814. goto out;
  3815. }
  3816. /*
  3817. * The split could have manipulated the tree enough to
  3818. * move the record location, so we have to look for it again.
  3819. */
  3820. ocfs2_reinit_path(path, 1);
  3821. ret = ocfs2_find_path(inode, path, cpos);
  3822. if (ret) {
  3823. mlog_errno(ret);
  3824. goto out;
  3825. }
  3826. el = path_leaf_el(path);
  3827. index = ocfs2_search_extent_list(el, cpos);
  3828. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  3829. ocfs2_error(inode->i_sb,
  3830. "Inode %llu: split at cpos %u lost record.",
  3831. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3832. cpos);
  3833. ret = -EROFS;
  3834. goto out;
  3835. }
  3836. /*
  3837. * Double check our values here. If anything is fishy,
  3838. * it's easier to catch it at the top level.
  3839. */
  3840. rec = &el->l_recs[index];
  3841. rec_range = le32_to_cpu(rec->e_cpos) +
  3842. ocfs2_rec_clusters(el, rec);
  3843. if (rec_range != trunc_range) {
  3844. ocfs2_error(inode->i_sb,
  3845. "Inode %llu: error after split at cpos %u"
  3846. "trunc len %u, existing record is (%u,%u)",
  3847. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3848. cpos, len, le32_to_cpu(rec->e_cpos),
  3849. ocfs2_rec_clusters(el, rec));
  3850. ret = -EROFS;
  3851. goto out;
  3852. }
  3853. ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
  3854. cpos, len);
  3855. if (ret) {
  3856. mlog_errno(ret);
  3857. goto out;
  3858. }
  3859. }
  3860. out:
  3861. ocfs2_free_path(path);
  3862. return ret;
  3863. }
  3864. int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  3865. {
  3866. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3867. struct ocfs2_dinode *di;
  3868. struct ocfs2_truncate_log *tl;
  3869. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3870. tl = &di->id2.i_dealloc;
  3871. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  3872. "slot %d, invalid truncate log parameters: used = "
  3873. "%u, count = %u\n", osb->slot_num,
  3874. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  3875. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  3876. }
  3877. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  3878. unsigned int new_start)
  3879. {
  3880. unsigned int tail_index;
  3881. unsigned int current_tail;
  3882. /* No records, nothing to coalesce */
  3883. if (!le16_to_cpu(tl->tl_used))
  3884. return 0;
  3885. tail_index = le16_to_cpu(tl->tl_used) - 1;
  3886. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  3887. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  3888. return current_tail == new_start;
  3889. }
  3890. int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  3891. handle_t *handle,
  3892. u64 start_blk,
  3893. unsigned int num_clusters)
  3894. {
  3895. int status, index;
  3896. unsigned int start_cluster, tl_count;
  3897. struct inode *tl_inode = osb->osb_tl_inode;
  3898. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3899. struct ocfs2_dinode *di;
  3900. struct ocfs2_truncate_log *tl;
  3901. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  3902. (unsigned long long)start_blk, num_clusters);
  3903. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  3904. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  3905. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3906. tl = &di->id2.i_dealloc;
  3907. if (!OCFS2_IS_VALID_DINODE(di)) {
  3908. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  3909. status = -EIO;
  3910. goto bail;
  3911. }
  3912. tl_count = le16_to_cpu(tl->tl_count);
  3913. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  3914. tl_count == 0,
  3915. "Truncate record count on #%llu invalid "
  3916. "wanted %u, actual %u\n",
  3917. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  3918. ocfs2_truncate_recs_per_inode(osb->sb),
  3919. le16_to_cpu(tl->tl_count));
  3920. /* Caller should have known to flush before calling us. */
  3921. index = le16_to_cpu(tl->tl_used);
  3922. if (index >= tl_count) {
  3923. status = -ENOSPC;
  3924. mlog_errno(status);
  3925. goto bail;
  3926. }
  3927. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  3928. OCFS2_JOURNAL_ACCESS_WRITE);
  3929. if (status < 0) {
  3930. mlog_errno(status);
  3931. goto bail;
  3932. }
  3933. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  3934. "%llu (index = %d)\n", num_clusters, start_cluster,
  3935. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  3936. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  3937. /*
  3938. * Move index back to the record we are coalescing with.
  3939. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  3940. */
  3941. index--;
  3942. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  3943. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  3944. index, le32_to_cpu(tl->tl_recs[index].t_start),
  3945. num_clusters);
  3946. } else {
  3947. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  3948. tl->tl_used = cpu_to_le16(index + 1);
  3949. }
  3950. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  3951. status = ocfs2_journal_dirty(handle, tl_bh);
  3952. if (status < 0) {
  3953. mlog_errno(status);
  3954. goto bail;
  3955. }
  3956. bail:
  3957. mlog_exit(status);
  3958. return status;
  3959. }
  3960. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  3961. handle_t *handle,
  3962. struct inode *data_alloc_inode,
  3963. struct buffer_head *data_alloc_bh)
  3964. {
  3965. int status = 0;
  3966. int i;
  3967. unsigned int num_clusters;
  3968. u64 start_blk;
  3969. struct ocfs2_truncate_rec rec;
  3970. struct ocfs2_dinode *di;
  3971. struct ocfs2_truncate_log *tl;
  3972. struct inode *tl_inode = osb->osb_tl_inode;
  3973. struct buffer_head *tl_bh = osb->osb_tl_bh;
  3974. mlog_entry_void();
  3975. di = (struct ocfs2_dinode *) tl_bh->b_data;
  3976. tl = &di->id2.i_dealloc;
  3977. i = le16_to_cpu(tl->tl_used) - 1;
  3978. while (i >= 0) {
  3979. /* Caller has given us at least enough credits to
  3980. * update the truncate log dinode */
  3981. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  3982. OCFS2_JOURNAL_ACCESS_WRITE);
  3983. if (status < 0) {
  3984. mlog_errno(status);
  3985. goto bail;
  3986. }
  3987. tl->tl_used = cpu_to_le16(i);
  3988. status = ocfs2_journal_dirty(handle, tl_bh);
  3989. if (status < 0) {
  3990. mlog_errno(status);
  3991. goto bail;
  3992. }
  3993. /* TODO: Perhaps we can calculate the bulk of the
  3994. * credits up front rather than extending like
  3995. * this. */
  3996. status = ocfs2_extend_trans(handle,
  3997. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  3998. if (status < 0) {
  3999. mlog_errno(status);
  4000. goto bail;
  4001. }
  4002. rec = tl->tl_recs[i];
  4003. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  4004. le32_to_cpu(rec.t_start));
  4005. num_clusters = le32_to_cpu(rec.t_clusters);
  4006. /* if start_blk is not set, we ignore the record as
  4007. * invalid. */
  4008. if (start_blk) {
  4009. mlog(0, "free record %d, start = %u, clusters = %u\n",
  4010. i, le32_to_cpu(rec.t_start), num_clusters);
  4011. status = ocfs2_free_clusters(handle, data_alloc_inode,
  4012. data_alloc_bh, start_blk,
  4013. num_clusters);
  4014. if (status < 0) {
  4015. mlog_errno(status);
  4016. goto bail;
  4017. }
  4018. }
  4019. i--;
  4020. }
  4021. bail:
  4022. mlog_exit(status);
  4023. return status;
  4024. }
  4025. /* Expects you to already be holding tl_inode->i_mutex */
  4026. int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  4027. {
  4028. int status;
  4029. unsigned int num_to_flush;
  4030. handle_t *handle;
  4031. struct inode *tl_inode = osb->osb_tl_inode;
  4032. struct inode *data_alloc_inode = NULL;
  4033. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4034. struct buffer_head *data_alloc_bh = NULL;
  4035. struct ocfs2_dinode *di;
  4036. struct ocfs2_truncate_log *tl;
  4037. mlog_entry_void();
  4038. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  4039. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4040. tl = &di->id2.i_dealloc;
  4041. if (!OCFS2_IS_VALID_DINODE(di)) {
  4042. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  4043. status = -EIO;
  4044. goto out;
  4045. }
  4046. num_to_flush = le16_to_cpu(tl->tl_used);
  4047. mlog(0, "Flush %u records from truncate log #%llu\n",
  4048. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  4049. if (!num_to_flush) {
  4050. status = 0;
  4051. goto out;
  4052. }
  4053. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  4054. GLOBAL_BITMAP_SYSTEM_INODE,
  4055. OCFS2_INVALID_SLOT);
  4056. if (!data_alloc_inode) {
  4057. status = -EINVAL;
  4058. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  4059. goto out;
  4060. }
  4061. mutex_lock(&data_alloc_inode->i_mutex);
  4062. status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
  4063. if (status < 0) {
  4064. mlog_errno(status);
  4065. goto out_mutex;
  4066. }
  4067. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  4068. if (IS_ERR(handle)) {
  4069. status = PTR_ERR(handle);
  4070. mlog_errno(status);
  4071. goto out_unlock;
  4072. }
  4073. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  4074. data_alloc_bh);
  4075. if (status < 0)
  4076. mlog_errno(status);
  4077. ocfs2_commit_trans(osb, handle);
  4078. out_unlock:
  4079. brelse(data_alloc_bh);
  4080. ocfs2_meta_unlock(data_alloc_inode, 1);
  4081. out_mutex:
  4082. mutex_unlock(&data_alloc_inode->i_mutex);
  4083. iput(data_alloc_inode);
  4084. out:
  4085. mlog_exit(status);
  4086. return status;
  4087. }
  4088. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  4089. {
  4090. int status;
  4091. struct inode *tl_inode = osb->osb_tl_inode;
  4092. mutex_lock(&tl_inode->i_mutex);
  4093. status = __ocfs2_flush_truncate_log(osb);
  4094. mutex_unlock(&tl_inode->i_mutex);
  4095. return status;
  4096. }
  4097. static void ocfs2_truncate_log_worker(struct work_struct *work)
  4098. {
  4099. int status;
  4100. struct ocfs2_super *osb =
  4101. container_of(work, struct ocfs2_super,
  4102. osb_truncate_log_wq.work);
  4103. mlog_entry_void();
  4104. status = ocfs2_flush_truncate_log(osb);
  4105. if (status < 0)
  4106. mlog_errno(status);
  4107. mlog_exit(status);
  4108. }
  4109. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  4110. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  4111. int cancel)
  4112. {
  4113. if (osb->osb_tl_inode) {
  4114. /* We want to push off log flushes while truncates are
  4115. * still running. */
  4116. if (cancel)
  4117. cancel_delayed_work(&osb->osb_truncate_log_wq);
  4118. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  4119. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  4120. }
  4121. }
  4122. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  4123. int slot_num,
  4124. struct inode **tl_inode,
  4125. struct buffer_head **tl_bh)
  4126. {
  4127. int status;
  4128. struct inode *inode = NULL;
  4129. struct buffer_head *bh = NULL;
  4130. inode = ocfs2_get_system_file_inode(osb,
  4131. TRUNCATE_LOG_SYSTEM_INODE,
  4132. slot_num);
  4133. if (!inode) {
  4134. status = -EINVAL;
  4135. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  4136. goto bail;
  4137. }
  4138. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  4139. OCFS2_BH_CACHED, inode);
  4140. if (status < 0) {
  4141. iput(inode);
  4142. mlog_errno(status);
  4143. goto bail;
  4144. }
  4145. *tl_inode = inode;
  4146. *tl_bh = bh;
  4147. bail:
  4148. mlog_exit(status);
  4149. return status;
  4150. }
  4151. /* called during the 1st stage of node recovery. we stamp a clean
  4152. * truncate log and pass back a copy for processing later. if the
  4153. * truncate log does not require processing, a *tl_copy is set to
  4154. * NULL. */
  4155. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  4156. int slot_num,
  4157. struct ocfs2_dinode **tl_copy)
  4158. {
  4159. int status;
  4160. struct inode *tl_inode = NULL;
  4161. struct buffer_head *tl_bh = NULL;
  4162. struct ocfs2_dinode *di;
  4163. struct ocfs2_truncate_log *tl;
  4164. *tl_copy = NULL;
  4165. mlog(0, "recover truncate log from slot %d\n", slot_num);
  4166. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  4167. if (status < 0) {
  4168. mlog_errno(status);
  4169. goto bail;
  4170. }
  4171. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4172. tl = &di->id2.i_dealloc;
  4173. if (!OCFS2_IS_VALID_DINODE(di)) {
  4174. OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
  4175. status = -EIO;
  4176. goto bail;
  4177. }
  4178. if (le16_to_cpu(tl->tl_used)) {
  4179. mlog(0, "We'll have %u logs to recover\n",
  4180. le16_to_cpu(tl->tl_used));
  4181. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  4182. if (!(*tl_copy)) {
  4183. status = -ENOMEM;
  4184. mlog_errno(status);
  4185. goto bail;
  4186. }
  4187. /* Assuming the write-out below goes well, this copy
  4188. * will be passed back to recovery for processing. */
  4189. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  4190. /* All we need to do to clear the truncate log is set
  4191. * tl_used. */
  4192. tl->tl_used = 0;
  4193. status = ocfs2_write_block(osb, tl_bh, tl_inode);
  4194. if (status < 0) {
  4195. mlog_errno(status);
  4196. goto bail;
  4197. }
  4198. }
  4199. bail:
  4200. if (tl_inode)
  4201. iput(tl_inode);
  4202. if (tl_bh)
  4203. brelse(tl_bh);
  4204. if (status < 0 && (*tl_copy)) {
  4205. kfree(*tl_copy);
  4206. *tl_copy = NULL;
  4207. }
  4208. mlog_exit(status);
  4209. return status;
  4210. }
  4211. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  4212. struct ocfs2_dinode *tl_copy)
  4213. {
  4214. int status = 0;
  4215. int i;
  4216. unsigned int clusters, num_recs, start_cluster;
  4217. u64 start_blk;
  4218. handle_t *handle;
  4219. struct inode *tl_inode = osb->osb_tl_inode;
  4220. struct ocfs2_truncate_log *tl;
  4221. mlog_entry_void();
  4222. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  4223. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  4224. return -EINVAL;
  4225. }
  4226. tl = &tl_copy->id2.i_dealloc;
  4227. num_recs = le16_to_cpu(tl->tl_used);
  4228. mlog(0, "cleanup %u records from %llu\n", num_recs,
  4229. (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
  4230. mutex_lock(&tl_inode->i_mutex);
  4231. for(i = 0; i < num_recs; i++) {
  4232. if (ocfs2_truncate_log_needs_flush(osb)) {
  4233. status = __ocfs2_flush_truncate_log(osb);
  4234. if (status < 0) {
  4235. mlog_errno(status);
  4236. goto bail_up;
  4237. }
  4238. }
  4239. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  4240. if (IS_ERR(handle)) {
  4241. status = PTR_ERR(handle);
  4242. mlog_errno(status);
  4243. goto bail_up;
  4244. }
  4245. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  4246. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  4247. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  4248. status = ocfs2_truncate_log_append(osb, handle,
  4249. start_blk, clusters);
  4250. ocfs2_commit_trans(osb, handle);
  4251. if (status < 0) {
  4252. mlog_errno(status);
  4253. goto bail_up;
  4254. }
  4255. }
  4256. bail_up:
  4257. mutex_unlock(&tl_inode->i_mutex);
  4258. mlog_exit(status);
  4259. return status;
  4260. }
  4261. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  4262. {
  4263. int status;
  4264. struct inode *tl_inode = osb->osb_tl_inode;
  4265. mlog_entry_void();
  4266. if (tl_inode) {
  4267. cancel_delayed_work(&osb->osb_truncate_log_wq);
  4268. flush_workqueue(ocfs2_wq);
  4269. status = ocfs2_flush_truncate_log(osb);
  4270. if (status < 0)
  4271. mlog_errno(status);
  4272. brelse(osb->osb_tl_bh);
  4273. iput(osb->osb_tl_inode);
  4274. }
  4275. mlog_exit_void();
  4276. }
  4277. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  4278. {
  4279. int status;
  4280. struct inode *tl_inode = NULL;
  4281. struct buffer_head *tl_bh = NULL;
  4282. mlog_entry_void();
  4283. status = ocfs2_get_truncate_log_info(osb,
  4284. osb->slot_num,
  4285. &tl_inode,
  4286. &tl_bh);
  4287. if (status < 0)
  4288. mlog_errno(status);
  4289. /* ocfs2_truncate_log_shutdown keys on the existence of
  4290. * osb->osb_tl_inode so we don't set any of the osb variables
  4291. * until we're sure all is well. */
  4292. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  4293. ocfs2_truncate_log_worker);
  4294. osb->osb_tl_bh = tl_bh;
  4295. osb->osb_tl_inode = tl_inode;
  4296. mlog_exit(status);
  4297. return status;
  4298. }
  4299. /*
  4300. * Delayed de-allocation of suballocator blocks.
  4301. *
  4302. * Some sets of block de-allocations might involve multiple suballocator inodes.
  4303. *
  4304. * The locking for this can get extremely complicated, especially when
  4305. * the suballocator inodes to delete from aren't known until deep
  4306. * within an unrelated codepath.
  4307. *
  4308. * ocfs2_extent_block structures are a good example of this - an inode
  4309. * btree could have been grown by any number of nodes each allocating
  4310. * out of their own suballoc inode.
  4311. *
  4312. * These structures allow the delay of block de-allocation until a
  4313. * later time, when locking of multiple cluster inodes won't cause
  4314. * deadlock.
  4315. */
  4316. /*
  4317. * Describes a single block free from a suballocator
  4318. */
  4319. struct ocfs2_cached_block_free {
  4320. struct ocfs2_cached_block_free *free_next;
  4321. u64 free_blk;
  4322. unsigned int free_bit;
  4323. };
  4324. struct ocfs2_per_slot_free_list {
  4325. struct ocfs2_per_slot_free_list *f_next_suballocator;
  4326. int f_inode_type;
  4327. int f_slot;
  4328. struct ocfs2_cached_block_free *f_first;
  4329. };
  4330. static int ocfs2_free_cached_items(struct ocfs2_super *osb,
  4331. int sysfile_type,
  4332. int slot,
  4333. struct ocfs2_cached_block_free *head)
  4334. {
  4335. int ret;
  4336. u64 bg_blkno;
  4337. handle_t *handle;
  4338. struct inode *inode;
  4339. struct buffer_head *di_bh = NULL;
  4340. struct ocfs2_cached_block_free *tmp;
  4341. inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
  4342. if (!inode) {
  4343. ret = -EINVAL;
  4344. mlog_errno(ret);
  4345. goto out;
  4346. }
  4347. mutex_lock(&inode->i_mutex);
  4348. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  4349. if (ret) {
  4350. mlog_errno(ret);
  4351. goto out_mutex;
  4352. }
  4353. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  4354. if (IS_ERR(handle)) {
  4355. ret = PTR_ERR(handle);
  4356. mlog_errno(ret);
  4357. goto out_unlock;
  4358. }
  4359. while (head) {
  4360. bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
  4361. head->free_bit);
  4362. mlog(0, "Free bit: (bit %u, blkno %llu)\n",
  4363. head->free_bit, (unsigned long long)head->free_blk);
  4364. ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
  4365. head->free_bit, bg_blkno, 1);
  4366. if (ret) {
  4367. mlog_errno(ret);
  4368. goto out_journal;
  4369. }
  4370. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  4371. if (ret) {
  4372. mlog_errno(ret);
  4373. goto out_journal;
  4374. }
  4375. tmp = head;
  4376. head = head->free_next;
  4377. kfree(tmp);
  4378. }
  4379. out_journal:
  4380. ocfs2_commit_trans(osb, handle);
  4381. out_unlock:
  4382. ocfs2_meta_unlock(inode, 1);
  4383. brelse(di_bh);
  4384. out_mutex:
  4385. mutex_unlock(&inode->i_mutex);
  4386. iput(inode);
  4387. out:
  4388. while(head) {
  4389. /* Premature exit may have left some dangling items. */
  4390. tmp = head;
  4391. head = head->free_next;
  4392. kfree(tmp);
  4393. }
  4394. return ret;
  4395. }
  4396. int ocfs2_run_deallocs(struct ocfs2_super *osb,
  4397. struct ocfs2_cached_dealloc_ctxt *ctxt)
  4398. {
  4399. int ret = 0, ret2;
  4400. struct ocfs2_per_slot_free_list *fl;
  4401. if (!ctxt)
  4402. return 0;
  4403. while (ctxt->c_first_suballocator) {
  4404. fl = ctxt->c_first_suballocator;
  4405. if (fl->f_first) {
  4406. mlog(0, "Free items: (type %u, slot %d)\n",
  4407. fl->f_inode_type, fl->f_slot);
  4408. ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
  4409. fl->f_slot, fl->f_first);
  4410. if (ret2)
  4411. mlog_errno(ret2);
  4412. if (!ret)
  4413. ret = ret2;
  4414. }
  4415. ctxt->c_first_suballocator = fl->f_next_suballocator;
  4416. kfree(fl);
  4417. }
  4418. return ret;
  4419. }
  4420. static struct ocfs2_per_slot_free_list *
  4421. ocfs2_find_per_slot_free_list(int type,
  4422. int slot,
  4423. struct ocfs2_cached_dealloc_ctxt *ctxt)
  4424. {
  4425. struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
  4426. while (fl) {
  4427. if (fl->f_inode_type == type && fl->f_slot == slot)
  4428. return fl;
  4429. fl = fl->f_next_suballocator;
  4430. }
  4431. fl = kmalloc(sizeof(*fl), GFP_NOFS);
  4432. if (fl) {
  4433. fl->f_inode_type = type;
  4434. fl->f_slot = slot;
  4435. fl->f_first = NULL;
  4436. fl->f_next_suballocator = ctxt->c_first_suballocator;
  4437. ctxt->c_first_suballocator = fl;
  4438. }
  4439. return fl;
  4440. }
  4441. static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  4442. int type, int slot, u64 blkno,
  4443. unsigned int bit)
  4444. {
  4445. int ret;
  4446. struct ocfs2_per_slot_free_list *fl;
  4447. struct ocfs2_cached_block_free *item;
  4448. fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
  4449. if (fl == NULL) {
  4450. ret = -ENOMEM;
  4451. mlog_errno(ret);
  4452. goto out;
  4453. }
  4454. item = kmalloc(sizeof(*item), GFP_NOFS);
  4455. if (item == NULL) {
  4456. ret = -ENOMEM;
  4457. mlog_errno(ret);
  4458. goto out;
  4459. }
  4460. mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
  4461. type, slot, bit, (unsigned long long)blkno);
  4462. item->free_blk = blkno;
  4463. item->free_bit = bit;
  4464. item->free_next = fl->f_first;
  4465. fl->f_first = item;
  4466. ret = 0;
  4467. out:
  4468. return ret;
  4469. }
  4470. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  4471. struct ocfs2_extent_block *eb)
  4472. {
  4473. return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
  4474. le16_to_cpu(eb->h_suballoc_slot),
  4475. le64_to_cpu(eb->h_blkno),
  4476. le16_to_cpu(eb->h_suballoc_bit));
  4477. }
  4478. /* This function will figure out whether the currently last extent
  4479. * block will be deleted, and if it will, what the new last extent
  4480. * block will be so we can update his h_next_leaf_blk field, as well
  4481. * as the dinodes i_last_eb_blk */
  4482. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  4483. unsigned int clusters_to_del,
  4484. struct ocfs2_path *path,
  4485. struct buffer_head **new_last_eb)
  4486. {
  4487. int next_free, ret = 0;
  4488. u32 cpos;
  4489. struct ocfs2_extent_rec *rec;
  4490. struct ocfs2_extent_block *eb;
  4491. struct ocfs2_extent_list *el;
  4492. struct buffer_head *bh = NULL;
  4493. *new_last_eb = NULL;
  4494. /* we have no tree, so of course, no last_eb. */
  4495. if (!path->p_tree_depth)
  4496. goto out;
  4497. /* trunc to zero special case - this makes tree_depth = 0
  4498. * regardless of what it is. */
  4499. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  4500. goto out;
  4501. el = path_leaf_el(path);
  4502. BUG_ON(!el->l_next_free_rec);
  4503. /*
  4504. * Make sure that this extent list will actually be empty
  4505. * after we clear away the data. We can shortcut out if
  4506. * there's more than one non-empty extent in the
  4507. * list. Otherwise, a check of the remaining extent is
  4508. * necessary.
  4509. */
  4510. next_free = le16_to_cpu(el->l_next_free_rec);
  4511. rec = NULL;
  4512. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  4513. if (next_free > 2)
  4514. goto out;
  4515. /* We may have a valid extent in index 1, check it. */
  4516. if (next_free == 2)
  4517. rec = &el->l_recs[1];
  4518. /*
  4519. * Fall through - no more nonempty extents, so we want
  4520. * to delete this leaf.
  4521. */
  4522. } else {
  4523. if (next_free > 1)
  4524. goto out;
  4525. rec = &el->l_recs[0];
  4526. }
  4527. if (rec) {
  4528. /*
  4529. * Check it we'll only be trimming off the end of this
  4530. * cluster.
  4531. */
  4532. if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
  4533. goto out;
  4534. }
  4535. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  4536. if (ret) {
  4537. mlog_errno(ret);
  4538. goto out;
  4539. }
  4540. ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
  4541. if (ret) {
  4542. mlog_errno(ret);
  4543. goto out;
  4544. }
  4545. eb = (struct ocfs2_extent_block *) bh->b_data;
  4546. el = &eb->h_list;
  4547. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  4548. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  4549. ret = -EROFS;
  4550. goto out;
  4551. }
  4552. *new_last_eb = bh;
  4553. get_bh(*new_last_eb);
  4554. mlog(0, "returning block %llu, (cpos: %u)\n",
  4555. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  4556. out:
  4557. brelse(bh);
  4558. return ret;
  4559. }
  4560. /*
  4561. * Trim some clusters off the rightmost edge of a tree. Only called
  4562. * during truncate.
  4563. *
  4564. * The caller needs to:
  4565. * - start journaling of each path component.
  4566. * - compute and fully set up any new last ext block
  4567. */
  4568. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  4569. handle_t *handle, struct ocfs2_truncate_context *tc,
  4570. u32 clusters_to_del, u64 *delete_start)
  4571. {
  4572. int ret, i, index = path->p_tree_depth;
  4573. u32 new_edge = 0;
  4574. u64 deleted_eb = 0;
  4575. struct buffer_head *bh;
  4576. struct ocfs2_extent_list *el;
  4577. struct ocfs2_extent_rec *rec;
  4578. *delete_start = 0;
  4579. while (index >= 0) {
  4580. bh = path->p_node[index].bh;
  4581. el = path->p_node[index].el;
  4582. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  4583. index, (unsigned long long)bh->b_blocknr);
  4584. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  4585. if (index !=
  4586. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  4587. ocfs2_error(inode->i_sb,
  4588. "Inode %lu has invalid ext. block %llu",
  4589. inode->i_ino,
  4590. (unsigned long long)bh->b_blocknr);
  4591. ret = -EROFS;
  4592. goto out;
  4593. }
  4594. find_tail_record:
  4595. i = le16_to_cpu(el->l_next_free_rec) - 1;
  4596. rec = &el->l_recs[i];
  4597. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  4598. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  4599. ocfs2_rec_clusters(el, rec),
  4600. (unsigned long long)le64_to_cpu(rec->e_blkno),
  4601. le16_to_cpu(el->l_next_free_rec));
  4602. BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
  4603. if (le16_to_cpu(el->l_tree_depth) == 0) {
  4604. /*
  4605. * If the leaf block contains a single empty
  4606. * extent and no records, we can just remove
  4607. * the block.
  4608. */
  4609. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  4610. memset(rec, 0,
  4611. sizeof(struct ocfs2_extent_rec));
  4612. el->l_next_free_rec = cpu_to_le16(0);
  4613. goto delete;
  4614. }
  4615. /*
  4616. * Remove any empty extents by shifting things
  4617. * left. That should make life much easier on
  4618. * the code below. This condition is rare
  4619. * enough that we shouldn't see a performance
  4620. * hit.
  4621. */
  4622. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  4623. le16_add_cpu(&el->l_next_free_rec, -1);
  4624. for(i = 0;
  4625. i < le16_to_cpu(el->l_next_free_rec); i++)
  4626. el->l_recs[i] = el->l_recs[i + 1];
  4627. memset(&el->l_recs[i], 0,
  4628. sizeof(struct ocfs2_extent_rec));
  4629. /*
  4630. * We've modified our extent list. The
  4631. * simplest way to handle this change
  4632. * is to being the search from the
  4633. * start again.
  4634. */
  4635. goto find_tail_record;
  4636. }
  4637. le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
  4638. /*
  4639. * We'll use "new_edge" on our way back up the
  4640. * tree to know what our rightmost cpos is.
  4641. */
  4642. new_edge = le16_to_cpu(rec->e_leaf_clusters);
  4643. new_edge += le32_to_cpu(rec->e_cpos);
  4644. /*
  4645. * The caller will use this to delete data blocks.
  4646. */
  4647. *delete_start = le64_to_cpu(rec->e_blkno)
  4648. + ocfs2_clusters_to_blocks(inode->i_sb,
  4649. le16_to_cpu(rec->e_leaf_clusters));
  4650. /*
  4651. * If it's now empty, remove this record.
  4652. */
  4653. if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
  4654. memset(rec, 0,
  4655. sizeof(struct ocfs2_extent_rec));
  4656. le16_add_cpu(&el->l_next_free_rec, -1);
  4657. }
  4658. } else {
  4659. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  4660. memset(rec, 0,
  4661. sizeof(struct ocfs2_extent_rec));
  4662. le16_add_cpu(&el->l_next_free_rec, -1);
  4663. goto delete;
  4664. }
  4665. /* Can this actually happen? */
  4666. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4667. goto delete;
  4668. /*
  4669. * We never actually deleted any clusters
  4670. * because our leaf was empty. There's no
  4671. * reason to adjust the rightmost edge then.
  4672. */
  4673. if (new_edge == 0)
  4674. goto delete;
  4675. rec->e_int_clusters = cpu_to_le32(new_edge);
  4676. le32_add_cpu(&rec->e_int_clusters,
  4677. -le32_to_cpu(rec->e_cpos));
  4678. /*
  4679. * A deleted child record should have been
  4680. * caught above.
  4681. */
  4682. BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
  4683. }
  4684. delete:
  4685. ret = ocfs2_journal_dirty(handle, bh);
  4686. if (ret) {
  4687. mlog_errno(ret);
  4688. goto out;
  4689. }
  4690. mlog(0, "extent list container %llu, after: record %d: "
  4691. "(%u, %u, %llu), next = %u.\n",
  4692. (unsigned long long)bh->b_blocknr, i,
  4693. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
  4694. (unsigned long long)le64_to_cpu(rec->e_blkno),
  4695. le16_to_cpu(el->l_next_free_rec));
  4696. /*
  4697. * We must be careful to only attempt delete of an
  4698. * extent block (and not the root inode block).
  4699. */
  4700. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  4701. struct ocfs2_extent_block *eb =
  4702. (struct ocfs2_extent_block *)bh->b_data;
  4703. /*
  4704. * Save this for use when processing the
  4705. * parent block.
  4706. */
  4707. deleted_eb = le64_to_cpu(eb->h_blkno);
  4708. mlog(0, "deleting this extent block.\n");
  4709. ocfs2_remove_from_cache(inode, bh);
  4710. BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
  4711. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  4712. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  4713. ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
  4714. /* An error here is not fatal. */
  4715. if (ret < 0)
  4716. mlog_errno(ret);
  4717. } else {
  4718. deleted_eb = 0;
  4719. }
  4720. index--;
  4721. }
  4722. ret = 0;
  4723. out:
  4724. return ret;
  4725. }
  4726. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  4727. unsigned int clusters_to_del,
  4728. struct inode *inode,
  4729. struct buffer_head *fe_bh,
  4730. handle_t *handle,
  4731. struct ocfs2_truncate_context *tc,
  4732. struct ocfs2_path *path)
  4733. {
  4734. int status;
  4735. struct ocfs2_dinode *fe;
  4736. struct ocfs2_extent_block *last_eb = NULL;
  4737. struct ocfs2_extent_list *el;
  4738. struct buffer_head *last_eb_bh = NULL;
  4739. u64 delete_blk = 0;
  4740. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  4741. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  4742. path, &last_eb_bh);
  4743. if (status < 0) {
  4744. mlog_errno(status);
  4745. goto bail;
  4746. }
  4747. /*
  4748. * Each component will be touched, so we might as well journal
  4749. * here to avoid having to handle errors later.
  4750. */
  4751. status = ocfs2_journal_access_path(inode, handle, path);
  4752. if (status < 0) {
  4753. mlog_errno(status);
  4754. goto bail;
  4755. }
  4756. if (last_eb_bh) {
  4757. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  4758. OCFS2_JOURNAL_ACCESS_WRITE);
  4759. if (status < 0) {
  4760. mlog_errno(status);
  4761. goto bail;
  4762. }
  4763. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4764. }
  4765. el = &(fe->id2.i_list);
  4766. /*
  4767. * Lower levels depend on this never happening, but it's best
  4768. * to check it up here before changing the tree.
  4769. */
  4770. if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
  4771. ocfs2_error(inode->i_sb,
  4772. "Inode %lu has an empty extent record, depth %u\n",
  4773. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  4774. status = -EROFS;
  4775. goto bail;
  4776. }
  4777. spin_lock(&OCFS2_I(inode)->ip_lock);
  4778. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  4779. clusters_to_del;
  4780. spin_unlock(&OCFS2_I(inode)->ip_lock);
  4781. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  4782. inode->i_blocks = ocfs2_inode_sector_count(inode);
  4783. status = ocfs2_trim_tree(inode, path, handle, tc,
  4784. clusters_to_del, &delete_blk);
  4785. if (status) {
  4786. mlog_errno(status);
  4787. goto bail;
  4788. }
  4789. if (le32_to_cpu(fe->i_clusters) == 0) {
  4790. /* trunc to zero is a special case. */
  4791. el->l_tree_depth = 0;
  4792. fe->i_last_eb_blk = 0;
  4793. } else if (last_eb)
  4794. fe->i_last_eb_blk = last_eb->h_blkno;
  4795. status = ocfs2_journal_dirty(handle, fe_bh);
  4796. if (status < 0) {
  4797. mlog_errno(status);
  4798. goto bail;
  4799. }
  4800. if (last_eb) {
  4801. /* If there will be a new last extent block, then by
  4802. * definition, there cannot be any leaves to the right of
  4803. * him. */
  4804. last_eb->h_next_leaf_blk = 0;
  4805. status = ocfs2_journal_dirty(handle, last_eb_bh);
  4806. if (status < 0) {
  4807. mlog_errno(status);
  4808. goto bail;
  4809. }
  4810. }
  4811. if (delete_blk) {
  4812. status = ocfs2_truncate_log_append(osb, handle, delete_blk,
  4813. clusters_to_del);
  4814. if (status < 0) {
  4815. mlog_errno(status);
  4816. goto bail;
  4817. }
  4818. }
  4819. status = 0;
  4820. bail:
  4821. mlog_exit(status);
  4822. return status;
  4823. }
  4824. static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
  4825. {
  4826. set_buffer_uptodate(bh);
  4827. mark_buffer_dirty(bh);
  4828. return 0;
  4829. }
  4830. static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
  4831. {
  4832. set_buffer_uptodate(bh);
  4833. mark_buffer_dirty(bh);
  4834. return ocfs2_journal_dirty_data(handle, bh);
  4835. }
  4836. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
  4837. loff_t end, struct page **pages,
  4838. int numpages, u64 phys, handle_t *handle)
  4839. {
  4840. int i, ret, partial = 0;
  4841. void *kaddr;
  4842. struct page *page;
  4843. unsigned int from, to = PAGE_CACHE_SIZE;
  4844. struct super_block *sb = inode->i_sb;
  4845. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  4846. if (numpages == 0)
  4847. goto out;
  4848. to = PAGE_CACHE_SIZE;
  4849. for(i = 0; i < numpages; i++) {
  4850. page = pages[i];
  4851. from = start & (PAGE_CACHE_SIZE - 1);
  4852. if ((end >> PAGE_CACHE_SHIFT) == page->index)
  4853. to = end & (PAGE_CACHE_SIZE - 1);
  4854. BUG_ON(from > PAGE_CACHE_SIZE);
  4855. BUG_ON(to > PAGE_CACHE_SIZE);
  4856. ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
  4857. if (ret)
  4858. mlog_errno(ret);
  4859. kaddr = kmap_atomic(page, KM_USER0);
  4860. memset(kaddr + from, 0, to - from);
  4861. kunmap_atomic(kaddr, KM_USER0);
  4862. /*
  4863. * Need to set the buffers we zero'd into uptodate
  4864. * here if they aren't - ocfs2_map_page_blocks()
  4865. * might've skipped some
  4866. */
  4867. if (ocfs2_should_order_data(inode)) {
  4868. ret = walk_page_buffers(handle,
  4869. page_buffers(page),
  4870. from, to, &partial,
  4871. ocfs2_ordered_zero_func);
  4872. if (ret < 0)
  4873. mlog_errno(ret);
  4874. } else {
  4875. ret = walk_page_buffers(handle, page_buffers(page),
  4876. from, to, &partial,
  4877. ocfs2_writeback_zero_func);
  4878. if (ret < 0)
  4879. mlog_errno(ret);
  4880. }
  4881. if (!partial)
  4882. SetPageUptodate(page);
  4883. flush_dcache_page(page);
  4884. start = (page->index + 1) << PAGE_CACHE_SHIFT;
  4885. }
  4886. out:
  4887. if (pages) {
  4888. for (i = 0; i < numpages; i++) {
  4889. page = pages[i];
  4890. unlock_page(page);
  4891. mark_page_accessed(page);
  4892. page_cache_release(page);
  4893. }
  4894. }
  4895. }
  4896. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
  4897. struct page **pages, int *num, u64 *phys)
  4898. {
  4899. int i, numpages = 0, ret = 0;
  4900. unsigned int ext_flags;
  4901. struct super_block *sb = inode->i_sb;
  4902. struct address_space *mapping = inode->i_mapping;
  4903. unsigned long index;
  4904. loff_t last_page_bytes;
  4905. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  4906. BUG_ON(start > end);
  4907. if (start == end)
  4908. goto out;
  4909. BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
  4910. (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
  4911. ret = ocfs2_extent_map_get_blocks(inode, start >> sb->s_blocksize_bits,
  4912. phys, NULL, &ext_flags);
  4913. if (ret) {
  4914. mlog_errno(ret);
  4915. goto out;
  4916. }
  4917. /* Tail is a hole. */
  4918. if (*phys == 0)
  4919. goto out;
  4920. /* Tail is marked as unwritten, we can count on write to zero
  4921. * in that case. */
  4922. if (ext_flags & OCFS2_EXT_UNWRITTEN)
  4923. goto out;
  4924. last_page_bytes = PAGE_ALIGN(end);
  4925. index = start >> PAGE_CACHE_SHIFT;
  4926. do {
  4927. pages[numpages] = grab_cache_page(mapping, index);
  4928. if (!pages[numpages]) {
  4929. ret = -ENOMEM;
  4930. mlog_errno(ret);
  4931. goto out;
  4932. }
  4933. numpages++;
  4934. index++;
  4935. } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
  4936. out:
  4937. if (ret != 0) {
  4938. if (pages) {
  4939. for (i = 0; i < numpages; i++) {
  4940. if (pages[i]) {
  4941. unlock_page(pages[i]);
  4942. page_cache_release(pages[i]);
  4943. }
  4944. }
  4945. }
  4946. numpages = 0;
  4947. }
  4948. *num = numpages;
  4949. return ret;
  4950. }
  4951. /*
  4952. * Zero the area past i_size but still within an allocated
  4953. * cluster. This avoids exposing nonzero data on subsequent file
  4954. * extends.
  4955. *
  4956. * We need to call this before i_size is updated on the inode because
  4957. * otherwise block_write_full_page() will skip writeout of pages past
  4958. * i_size. The new_i_size parameter is passed for this reason.
  4959. */
  4960. int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
  4961. u64 range_start, u64 range_end)
  4962. {
  4963. int ret, numpages;
  4964. struct page **pages = NULL;
  4965. u64 phys;
  4966. /*
  4967. * File systems which don't support sparse files zero on every
  4968. * extend.
  4969. */
  4970. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  4971. return 0;
  4972. pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
  4973. sizeof(struct page *), GFP_NOFS);
  4974. if (pages == NULL) {
  4975. ret = -ENOMEM;
  4976. mlog_errno(ret);
  4977. goto out;
  4978. }
  4979. ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
  4980. &numpages, &phys);
  4981. if (ret) {
  4982. mlog_errno(ret);
  4983. goto out;
  4984. }
  4985. if (numpages == 0)
  4986. goto out;
  4987. ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
  4988. numpages, phys, handle);
  4989. /*
  4990. * Initiate writeout of the pages we zero'd here. We don't
  4991. * wait on them - the truncate_inode_pages() call later will
  4992. * do that for us.
  4993. */
  4994. ret = do_sync_mapping_range(inode->i_mapping, range_start,
  4995. range_end - 1, SYNC_FILE_RANGE_WRITE);
  4996. if (ret)
  4997. mlog_errno(ret);
  4998. out:
  4999. if (pages)
  5000. kfree(pages);
  5001. return ret;
  5002. }
  5003. /*
  5004. * It is expected, that by the time you call this function,
  5005. * inode->i_size and fe->i_size have been adjusted.
  5006. *
  5007. * WARNING: This will kfree the truncate context
  5008. */
  5009. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  5010. struct inode *inode,
  5011. struct buffer_head *fe_bh,
  5012. struct ocfs2_truncate_context *tc)
  5013. {
  5014. int status, i, credits, tl_sem = 0;
  5015. u32 clusters_to_del, new_highest_cpos, range;
  5016. struct ocfs2_extent_list *el;
  5017. handle_t *handle = NULL;
  5018. struct inode *tl_inode = osb->osb_tl_inode;
  5019. struct ocfs2_path *path = NULL;
  5020. mlog_entry_void();
  5021. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  5022. i_size_read(inode));
  5023. path = ocfs2_new_inode_path(fe_bh);
  5024. if (!path) {
  5025. status = -ENOMEM;
  5026. mlog_errno(status);
  5027. goto bail;
  5028. }
  5029. ocfs2_extent_map_trunc(inode, new_highest_cpos);
  5030. start:
  5031. /*
  5032. * Check that we still have allocation to delete.
  5033. */
  5034. if (OCFS2_I(inode)->ip_clusters == 0) {
  5035. status = 0;
  5036. goto bail;
  5037. }
  5038. /*
  5039. * Truncate always works against the rightmost tree branch.
  5040. */
  5041. status = ocfs2_find_path(inode, path, UINT_MAX);
  5042. if (status) {
  5043. mlog_errno(status);
  5044. goto bail;
  5045. }
  5046. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  5047. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  5048. /*
  5049. * By now, el will point to the extent list on the bottom most
  5050. * portion of this tree. Only the tail record is considered in
  5051. * each pass.
  5052. *
  5053. * We handle the following cases, in order:
  5054. * - empty extent: delete the remaining branch
  5055. * - remove the entire record
  5056. * - remove a partial record
  5057. * - no record needs to be removed (truncate has completed)
  5058. */
  5059. el = path_leaf_el(path);
  5060. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  5061. ocfs2_error(inode->i_sb,
  5062. "Inode %llu has empty extent block at %llu\n",
  5063. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  5064. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  5065. status = -EROFS;
  5066. goto bail;
  5067. }
  5068. i = le16_to_cpu(el->l_next_free_rec) - 1;
  5069. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  5070. ocfs2_rec_clusters(el, &el->l_recs[i]);
  5071. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  5072. clusters_to_del = 0;
  5073. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  5074. clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
  5075. } else if (range > new_highest_cpos) {
  5076. clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
  5077. le32_to_cpu(el->l_recs[i].e_cpos)) -
  5078. new_highest_cpos;
  5079. } else {
  5080. status = 0;
  5081. goto bail;
  5082. }
  5083. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  5084. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  5085. BUG_ON(clusters_to_del == 0);
  5086. mutex_lock(&tl_inode->i_mutex);
  5087. tl_sem = 1;
  5088. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  5089. * record is free for use. If there isn't any, we flush to get
  5090. * an empty truncate log. */
  5091. if (ocfs2_truncate_log_needs_flush(osb)) {
  5092. status = __ocfs2_flush_truncate_log(osb);
  5093. if (status < 0) {
  5094. mlog_errno(status);
  5095. goto bail;
  5096. }
  5097. }
  5098. credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  5099. (struct ocfs2_dinode *)fe_bh->b_data,
  5100. el);
  5101. handle = ocfs2_start_trans(osb, credits);
  5102. if (IS_ERR(handle)) {
  5103. status = PTR_ERR(handle);
  5104. handle = NULL;
  5105. mlog_errno(status);
  5106. goto bail;
  5107. }
  5108. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  5109. tc, path);
  5110. if (status < 0) {
  5111. mlog_errno(status);
  5112. goto bail;
  5113. }
  5114. mutex_unlock(&tl_inode->i_mutex);
  5115. tl_sem = 0;
  5116. ocfs2_commit_trans(osb, handle);
  5117. handle = NULL;
  5118. ocfs2_reinit_path(path, 1);
  5119. /*
  5120. * The check above will catch the case where we've truncated
  5121. * away all allocation.
  5122. */
  5123. goto start;
  5124. bail:
  5125. ocfs2_schedule_truncate_log_flush(osb, 1);
  5126. if (tl_sem)
  5127. mutex_unlock(&tl_inode->i_mutex);
  5128. if (handle)
  5129. ocfs2_commit_trans(osb, handle);
  5130. ocfs2_run_deallocs(osb, &tc->tc_dealloc);
  5131. ocfs2_free_path(path);
  5132. /* This will drop the ext_alloc cluster lock for us */
  5133. ocfs2_free_truncate_context(tc);
  5134. mlog_exit(status);
  5135. return status;
  5136. }
  5137. /*
  5138. * Expects the inode to already be locked.
  5139. */
  5140. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  5141. struct inode *inode,
  5142. struct buffer_head *fe_bh,
  5143. struct ocfs2_truncate_context **tc)
  5144. {
  5145. int status;
  5146. unsigned int new_i_clusters;
  5147. struct ocfs2_dinode *fe;
  5148. struct ocfs2_extent_block *eb;
  5149. struct buffer_head *last_eb_bh = NULL;
  5150. mlog_entry_void();
  5151. *tc = NULL;
  5152. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  5153. i_size_read(inode));
  5154. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  5155. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  5156. "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
  5157. (unsigned long long)le64_to_cpu(fe->i_size));
  5158. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  5159. if (!(*tc)) {
  5160. status = -ENOMEM;
  5161. mlog_errno(status);
  5162. goto bail;
  5163. }
  5164. ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
  5165. if (fe->id2.i_list.l_tree_depth) {
  5166. status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  5167. &last_eb_bh, OCFS2_BH_CACHED, inode);
  5168. if (status < 0) {
  5169. mlog_errno(status);
  5170. goto bail;
  5171. }
  5172. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  5173. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  5174. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  5175. brelse(last_eb_bh);
  5176. status = -EIO;
  5177. goto bail;
  5178. }
  5179. }
  5180. (*tc)->tc_last_eb_bh = last_eb_bh;
  5181. status = 0;
  5182. bail:
  5183. if (status < 0) {
  5184. if (*tc)
  5185. ocfs2_free_truncate_context(*tc);
  5186. *tc = NULL;
  5187. }
  5188. mlog_exit_void();
  5189. return status;
  5190. }
  5191. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  5192. {
  5193. /*
  5194. * The caller is responsible for completing deallocation
  5195. * before freeing the context.
  5196. */
  5197. if (tc->tc_dealloc.c_first_suballocator != NULL)
  5198. mlog(ML_NOTICE,
  5199. "Truncate completion has non-empty dealloc context\n");
  5200. if (tc->tc_last_eb_bh)
  5201. brelse(tc->tc_last_eb_bh);
  5202. kfree(tc);
  5203. }