alloc.c 154 KB

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