alloc.c 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663
  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. #include <linux/quotaops.h>
  31. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  32. #include <cluster/masklog.h>
  33. #include "ocfs2.h"
  34. #include "alloc.h"
  35. #include "aops.h"
  36. #include "blockcheck.h"
  37. #include "dlmglue.h"
  38. #include "extent_map.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "localalloc.h"
  42. #include "suballoc.h"
  43. #include "sysfile.h"
  44. #include "file.h"
  45. #include "super.h"
  46. #include "uptodate.h"
  47. #include "xattr.h"
  48. #include "refcounttree.h"
  49. #include "buffer_head_io.h"
  50. enum ocfs2_contig_type {
  51. CONTIG_NONE = 0,
  52. CONTIG_LEFT,
  53. CONTIG_RIGHT,
  54. CONTIG_LEFTRIGHT,
  55. };
  56. static enum ocfs2_contig_type
  57. ocfs2_extent_rec_contig(struct super_block *sb,
  58. struct ocfs2_extent_rec *ext,
  59. struct ocfs2_extent_rec *insert_rec);
  60. /*
  61. * Operations for a specific extent tree type.
  62. *
  63. * To implement an on-disk btree (extent tree) type in ocfs2, add
  64. * an ocfs2_extent_tree_operations structure and the matching
  65. * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
  66. * for the allocation portion of the extent tree.
  67. */
  68. struct ocfs2_extent_tree_operations {
  69. /*
  70. * last_eb_blk is the block number of the right most leaf extent
  71. * block. Most on-disk structures containing an extent tree store
  72. * this value for fast access. The ->eo_set_last_eb_blk() and
  73. * ->eo_get_last_eb_blk() operations access this value. They are
  74. * both required.
  75. */
  76. void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
  77. u64 blkno);
  78. u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
  79. /*
  80. * The on-disk structure usually keeps track of how many total
  81. * clusters are stored in this extent tree. This function updates
  82. * that value. new_clusters is the delta, and must be
  83. * added to the total. Required.
  84. */
  85. void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
  86. u32 new_clusters);
  87. /*
  88. * If this extent tree is supported by an extent map, insert
  89. * a record into the map.
  90. */
  91. void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
  92. struct ocfs2_extent_rec *rec);
  93. /*
  94. * If this extent tree is supported by an extent map, truncate the
  95. * map to clusters,
  96. */
  97. void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
  98. u32 clusters);
  99. /*
  100. * If ->eo_insert_check() exists, it is called before rec is
  101. * inserted into the extent tree. It is optional.
  102. */
  103. int (*eo_insert_check)(struct ocfs2_extent_tree *et,
  104. struct ocfs2_extent_rec *rec);
  105. int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
  106. /*
  107. * --------------------------------------------------------------
  108. * The remaining are internal to ocfs2_extent_tree and don't have
  109. * accessor functions
  110. */
  111. /*
  112. * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
  113. * It is required.
  114. */
  115. void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
  116. /*
  117. * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
  118. * it exists. If it does not, et->et_max_leaf_clusters is set
  119. * to 0 (unlimited). Optional.
  120. */
  121. void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
  122. /*
  123. * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
  124. * are contiguous or not. Optional. Don't need to set it if use
  125. * ocfs2_extent_rec as the tree leaf.
  126. */
  127. enum ocfs2_contig_type
  128. (*eo_extent_contig)(struct ocfs2_extent_tree *et,
  129. struct ocfs2_extent_rec *ext,
  130. struct ocfs2_extent_rec *insert_rec);
  131. };
  132. /*
  133. * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
  134. * in the methods.
  135. */
  136. static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
  137. static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
  138. u64 blkno);
  139. static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
  140. u32 clusters);
  141. static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
  142. struct ocfs2_extent_rec *rec);
  143. static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
  144. u32 clusters);
  145. static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
  146. struct ocfs2_extent_rec *rec);
  147. static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
  148. static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
  149. static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
  150. .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
  151. .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
  152. .eo_update_clusters = ocfs2_dinode_update_clusters,
  153. .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
  154. .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
  155. .eo_insert_check = ocfs2_dinode_insert_check,
  156. .eo_sanity_check = ocfs2_dinode_sanity_check,
  157. .eo_fill_root_el = ocfs2_dinode_fill_root_el,
  158. };
  159. static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
  160. u64 blkno)
  161. {
  162. struct ocfs2_dinode *di = et->et_object;
  163. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  164. di->i_last_eb_blk = cpu_to_le64(blkno);
  165. }
  166. static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
  167. {
  168. struct ocfs2_dinode *di = et->et_object;
  169. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  170. return le64_to_cpu(di->i_last_eb_blk);
  171. }
  172. static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
  173. u32 clusters)
  174. {
  175. struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
  176. struct ocfs2_dinode *di = et->et_object;
  177. le32_add_cpu(&di->i_clusters, clusters);
  178. spin_lock(&oi->ip_lock);
  179. oi->ip_clusters = le32_to_cpu(di->i_clusters);
  180. spin_unlock(&oi->ip_lock);
  181. }
  182. static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
  183. struct ocfs2_extent_rec *rec)
  184. {
  185. struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
  186. ocfs2_extent_map_insert_rec(inode, rec);
  187. }
  188. static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
  189. u32 clusters)
  190. {
  191. struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
  192. ocfs2_extent_map_trunc(inode, clusters);
  193. }
  194. static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
  195. struct ocfs2_extent_rec *rec)
  196. {
  197. struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
  198. struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
  199. BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
  200. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  201. (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
  202. "Device %s, asking for sparse allocation: inode %llu, "
  203. "cpos %u, clusters %u\n",
  204. osb->dev_str,
  205. (unsigned long long)oi->ip_blkno,
  206. rec->e_cpos, oi->ip_clusters);
  207. return 0;
  208. }
  209. static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
  210. {
  211. struct ocfs2_dinode *di = et->et_object;
  212. BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
  213. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  214. return 0;
  215. }
  216. static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
  217. {
  218. struct ocfs2_dinode *di = et->et_object;
  219. et->et_root_el = &di->id2.i_list;
  220. }
  221. static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
  222. {
  223. struct ocfs2_xattr_value_buf *vb = et->et_object;
  224. et->et_root_el = &vb->vb_xv->xr_list;
  225. }
  226. static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
  227. u64 blkno)
  228. {
  229. struct ocfs2_xattr_value_buf *vb = et->et_object;
  230. vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
  231. }
  232. static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
  233. {
  234. struct ocfs2_xattr_value_buf *vb = et->et_object;
  235. return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
  236. }
  237. static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
  238. u32 clusters)
  239. {
  240. struct ocfs2_xattr_value_buf *vb = et->et_object;
  241. le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
  242. }
  243. static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
  244. .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
  245. .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
  246. .eo_update_clusters = ocfs2_xattr_value_update_clusters,
  247. .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
  248. };
  249. static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
  250. {
  251. struct ocfs2_xattr_block *xb = et->et_object;
  252. et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
  253. }
  254. static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
  255. {
  256. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  257. et->et_max_leaf_clusters =
  258. ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
  259. }
  260. static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
  261. u64 blkno)
  262. {
  263. struct ocfs2_xattr_block *xb = et->et_object;
  264. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  265. xt->xt_last_eb_blk = cpu_to_le64(blkno);
  266. }
  267. static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
  268. {
  269. struct ocfs2_xattr_block *xb = et->et_object;
  270. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  271. return le64_to_cpu(xt->xt_last_eb_blk);
  272. }
  273. static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
  274. u32 clusters)
  275. {
  276. struct ocfs2_xattr_block *xb = et->et_object;
  277. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
  278. }
  279. static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
  280. .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
  281. .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
  282. .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
  283. .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
  284. .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
  285. };
  286. static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
  287. u64 blkno)
  288. {
  289. struct ocfs2_dx_root_block *dx_root = et->et_object;
  290. dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
  291. }
  292. static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
  293. {
  294. struct ocfs2_dx_root_block *dx_root = et->et_object;
  295. return le64_to_cpu(dx_root->dr_last_eb_blk);
  296. }
  297. static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
  298. u32 clusters)
  299. {
  300. struct ocfs2_dx_root_block *dx_root = et->et_object;
  301. le32_add_cpu(&dx_root->dr_clusters, clusters);
  302. }
  303. static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
  304. {
  305. struct ocfs2_dx_root_block *dx_root = et->et_object;
  306. BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
  307. return 0;
  308. }
  309. static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
  310. {
  311. struct ocfs2_dx_root_block *dx_root = et->et_object;
  312. et->et_root_el = &dx_root->dr_list;
  313. }
  314. static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
  315. .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
  316. .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
  317. .eo_update_clusters = ocfs2_dx_root_update_clusters,
  318. .eo_sanity_check = ocfs2_dx_root_sanity_check,
  319. .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
  320. };
  321. static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
  322. {
  323. struct ocfs2_refcount_block *rb = et->et_object;
  324. et->et_root_el = &rb->rf_list;
  325. }
  326. static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
  327. u64 blkno)
  328. {
  329. struct ocfs2_refcount_block *rb = et->et_object;
  330. rb->rf_last_eb_blk = cpu_to_le64(blkno);
  331. }
  332. static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
  333. {
  334. struct ocfs2_refcount_block *rb = et->et_object;
  335. return le64_to_cpu(rb->rf_last_eb_blk);
  336. }
  337. static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
  338. u32 clusters)
  339. {
  340. struct ocfs2_refcount_block *rb = et->et_object;
  341. le32_add_cpu(&rb->rf_clusters, clusters);
  342. }
  343. static enum ocfs2_contig_type
  344. ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
  345. struct ocfs2_extent_rec *ext,
  346. struct ocfs2_extent_rec *insert_rec)
  347. {
  348. return CONTIG_NONE;
  349. }
  350. static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
  351. .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
  352. .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
  353. .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
  354. .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
  355. .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
  356. };
  357. static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
  358. struct ocfs2_caching_info *ci,
  359. struct buffer_head *bh,
  360. ocfs2_journal_access_func access,
  361. void *obj,
  362. struct ocfs2_extent_tree_operations *ops)
  363. {
  364. et->et_ops = ops;
  365. et->et_root_bh = bh;
  366. et->et_ci = ci;
  367. et->et_root_journal_access = access;
  368. if (!obj)
  369. obj = (void *)bh->b_data;
  370. et->et_object = obj;
  371. et->et_ops->eo_fill_root_el(et);
  372. if (!et->et_ops->eo_fill_max_leaf_clusters)
  373. et->et_max_leaf_clusters = 0;
  374. else
  375. et->et_ops->eo_fill_max_leaf_clusters(et);
  376. }
  377. void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
  378. struct ocfs2_caching_info *ci,
  379. struct buffer_head *bh)
  380. {
  381. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
  382. NULL, &ocfs2_dinode_et_ops);
  383. }
  384. void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
  385. struct ocfs2_caching_info *ci,
  386. struct buffer_head *bh)
  387. {
  388. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
  389. NULL, &ocfs2_xattr_tree_et_ops);
  390. }
  391. void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
  392. struct ocfs2_caching_info *ci,
  393. struct ocfs2_xattr_value_buf *vb)
  394. {
  395. __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
  396. &ocfs2_xattr_value_et_ops);
  397. }
  398. void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
  399. struct ocfs2_caching_info *ci,
  400. struct buffer_head *bh)
  401. {
  402. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
  403. NULL, &ocfs2_dx_root_et_ops);
  404. }
  405. void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
  406. struct ocfs2_caching_info *ci,
  407. struct buffer_head *bh)
  408. {
  409. __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
  410. NULL, &ocfs2_refcount_tree_et_ops);
  411. }
  412. static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
  413. u64 new_last_eb_blk)
  414. {
  415. et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
  416. }
  417. static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
  418. {
  419. return et->et_ops->eo_get_last_eb_blk(et);
  420. }
  421. static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
  422. u32 clusters)
  423. {
  424. et->et_ops->eo_update_clusters(et, clusters);
  425. }
  426. static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
  427. struct ocfs2_extent_rec *rec)
  428. {
  429. if (et->et_ops->eo_extent_map_insert)
  430. et->et_ops->eo_extent_map_insert(et, rec);
  431. }
  432. static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
  433. u32 clusters)
  434. {
  435. if (et->et_ops->eo_extent_map_truncate)
  436. et->et_ops->eo_extent_map_truncate(et, clusters);
  437. }
  438. static inline int ocfs2_et_root_journal_access(handle_t *handle,
  439. struct ocfs2_extent_tree *et,
  440. int type)
  441. {
  442. return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
  443. type);
  444. }
  445. static inline enum ocfs2_contig_type
  446. ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
  447. struct ocfs2_extent_rec *rec,
  448. struct ocfs2_extent_rec *insert_rec)
  449. {
  450. if (et->et_ops->eo_extent_contig)
  451. return et->et_ops->eo_extent_contig(et, rec, insert_rec);
  452. return ocfs2_extent_rec_contig(
  453. ocfs2_metadata_cache_get_super(et->et_ci),
  454. rec, insert_rec);
  455. }
  456. static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
  457. struct ocfs2_extent_rec *rec)
  458. {
  459. int ret = 0;
  460. if (et->et_ops->eo_insert_check)
  461. ret = et->et_ops->eo_insert_check(et, rec);
  462. return ret;
  463. }
  464. static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
  465. {
  466. int ret = 0;
  467. if (et->et_ops->eo_sanity_check)
  468. ret = et->et_ops->eo_sanity_check(et);
  469. return ret;
  470. }
  471. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  472. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  473. struct ocfs2_extent_block *eb);
  474. static void ocfs2_adjust_rightmost_records(handle_t *handle,
  475. struct ocfs2_extent_tree *et,
  476. struct ocfs2_path *path,
  477. struct ocfs2_extent_rec *insert_rec);
  478. /*
  479. * Reset the actual path elements so that we can re-use the structure
  480. * to build another path. Generally, this involves freeing the buffer
  481. * heads.
  482. */
  483. void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  484. {
  485. int i, start = 0, depth = 0;
  486. struct ocfs2_path_item *node;
  487. if (keep_root)
  488. start = 1;
  489. for(i = start; i < path_num_items(path); i++) {
  490. node = &path->p_node[i];
  491. brelse(node->bh);
  492. node->bh = NULL;
  493. node->el = NULL;
  494. }
  495. /*
  496. * Tree depth may change during truncate, or insert. If we're
  497. * keeping the root extent list, then make sure that our path
  498. * structure reflects the proper depth.
  499. */
  500. if (keep_root)
  501. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  502. else
  503. path_root_access(path) = NULL;
  504. path->p_tree_depth = depth;
  505. }
  506. void ocfs2_free_path(struct ocfs2_path *path)
  507. {
  508. if (path) {
  509. ocfs2_reinit_path(path, 0);
  510. kfree(path);
  511. }
  512. }
  513. /*
  514. * All the elements of src into dest. After this call, src could be freed
  515. * without affecting dest.
  516. *
  517. * Both paths should have the same root. Any non-root elements of dest
  518. * will be freed.
  519. */
  520. static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  521. {
  522. int i;
  523. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  524. BUG_ON(path_root_el(dest) != path_root_el(src));
  525. BUG_ON(path_root_access(dest) != path_root_access(src));
  526. ocfs2_reinit_path(dest, 1);
  527. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  528. dest->p_node[i].bh = src->p_node[i].bh;
  529. dest->p_node[i].el = src->p_node[i].el;
  530. if (dest->p_node[i].bh)
  531. get_bh(dest->p_node[i].bh);
  532. }
  533. }
  534. /*
  535. * Make the *dest path the same as src and re-initialize src path to
  536. * have a root only.
  537. */
  538. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  539. {
  540. int i;
  541. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  542. BUG_ON(path_root_access(dest) != path_root_access(src));
  543. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  544. brelse(dest->p_node[i].bh);
  545. dest->p_node[i].bh = src->p_node[i].bh;
  546. dest->p_node[i].el = src->p_node[i].el;
  547. src->p_node[i].bh = NULL;
  548. src->p_node[i].el = NULL;
  549. }
  550. }
  551. /*
  552. * Insert an extent block at given index.
  553. *
  554. * This will not take an additional reference on eb_bh.
  555. */
  556. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  557. struct buffer_head *eb_bh)
  558. {
  559. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  560. /*
  561. * Right now, no root bh is an extent block, so this helps
  562. * catch code errors with dinode trees. The assertion can be
  563. * safely removed if we ever need to insert extent block
  564. * structures at the root.
  565. */
  566. BUG_ON(index == 0);
  567. path->p_node[index].bh = eb_bh;
  568. path->p_node[index].el = &eb->h_list;
  569. }
  570. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  571. struct ocfs2_extent_list *root_el,
  572. ocfs2_journal_access_func access)
  573. {
  574. struct ocfs2_path *path;
  575. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  576. path = kzalloc(sizeof(*path), GFP_NOFS);
  577. if (path) {
  578. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  579. get_bh(root_bh);
  580. path_root_bh(path) = root_bh;
  581. path_root_el(path) = root_el;
  582. path_root_access(path) = access;
  583. }
  584. return path;
  585. }
  586. struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
  587. {
  588. return ocfs2_new_path(path_root_bh(path), path_root_el(path),
  589. path_root_access(path));
  590. }
  591. struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
  592. {
  593. return ocfs2_new_path(et->et_root_bh, et->et_root_el,
  594. et->et_root_journal_access);
  595. }
  596. /*
  597. * Journal the buffer at depth idx. All idx>0 are extent_blocks,
  598. * otherwise it's the root_access function.
  599. *
  600. * I don't like the way this function's name looks next to
  601. * ocfs2_journal_access_path(), but I don't have a better one.
  602. */
  603. int ocfs2_path_bh_journal_access(handle_t *handle,
  604. struct ocfs2_caching_info *ci,
  605. struct ocfs2_path *path,
  606. int idx)
  607. {
  608. ocfs2_journal_access_func access = path_root_access(path);
  609. if (!access)
  610. access = ocfs2_journal_access;
  611. if (idx)
  612. access = ocfs2_journal_access_eb;
  613. return access(handle, ci, path->p_node[idx].bh,
  614. OCFS2_JOURNAL_ACCESS_WRITE);
  615. }
  616. /*
  617. * Convenience function to journal all components in a path.
  618. */
  619. int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
  620. handle_t *handle,
  621. struct ocfs2_path *path)
  622. {
  623. int i, ret = 0;
  624. if (!path)
  625. goto out;
  626. for(i = 0; i < path_num_items(path); i++) {
  627. ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
  628. if (ret < 0) {
  629. mlog_errno(ret);
  630. goto out;
  631. }
  632. }
  633. out:
  634. return ret;
  635. }
  636. /*
  637. * Return the index of the extent record which contains cluster #v_cluster.
  638. * -1 is returned if it was not found.
  639. *
  640. * Should work fine on interior and exterior nodes.
  641. */
  642. int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
  643. {
  644. int ret = -1;
  645. int i;
  646. struct ocfs2_extent_rec *rec;
  647. u32 rec_end, rec_start, clusters;
  648. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  649. rec = &el->l_recs[i];
  650. rec_start = le32_to_cpu(rec->e_cpos);
  651. clusters = ocfs2_rec_clusters(el, rec);
  652. rec_end = rec_start + clusters;
  653. if (v_cluster >= rec_start && v_cluster < rec_end) {
  654. ret = i;
  655. break;
  656. }
  657. }
  658. return ret;
  659. }
  660. /*
  661. * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
  662. * ocfs2_extent_rec_contig only work properly against leaf nodes!
  663. */
  664. static int ocfs2_block_extent_contig(struct super_block *sb,
  665. struct ocfs2_extent_rec *ext,
  666. u64 blkno)
  667. {
  668. u64 blk_end = le64_to_cpu(ext->e_blkno);
  669. blk_end += ocfs2_clusters_to_blocks(sb,
  670. le16_to_cpu(ext->e_leaf_clusters));
  671. return blkno == blk_end;
  672. }
  673. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  674. struct ocfs2_extent_rec *right)
  675. {
  676. u32 left_range;
  677. left_range = le32_to_cpu(left->e_cpos) +
  678. le16_to_cpu(left->e_leaf_clusters);
  679. return (left_range == le32_to_cpu(right->e_cpos));
  680. }
  681. static enum ocfs2_contig_type
  682. ocfs2_extent_rec_contig(struct super_block *sb,
  683. struct ocfs2_extent_rec *ext,
  684. struct ocfs2_extent_rec *insert_rec)
  685. {
  686. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  687. /*
  688. * Refuse to coalesce extent records with different flag
  689. * fields - we don't want to mix unwritten extents with user
  690. * data.
  691. */
  692. if (ext->e_flags != insert_rec->e_flags)
  693. return CONTIG_NONE;
  694. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  695. ocfs2_block_extent_contig(sb, ext, blkno))
  696. return CONTIG_RIGHT;
  697. blkno = le64_to_cpu(ext->e_blkno);
  698. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  699. ocfs2_block_extent_contig(sb, insert_rec, blkno))
  700. return CONTIG_LEFT;
  701. return CONTIG_NONE;
  702. }
  703. /*
  704. * NOTE: We can have pretty much any combination of contiguousness and
  705. * appending.
  706. *
  707. * The usefulness of APPEND_TAIL is more in that it lets us know that
  708. * we'll have to update the path to that leaf.
  709. */
  710. enum ocfs2_append_type {
  711. APPEND_NONE = 0,
  712. APPEND_TAIL,
  713. };
  714. enum ocfs2_split_type {
  715. SPLIT_NONE = 0,
  716. SPLIT_LEFT,
  717. SPLIT_RIGHT,
  718. };
  719. struct ocfs2_insert_type {
  720. enum ocfs2_split_type ins_split;
  721. enum ocfs2_append_type ins_appending;
  722. enum ocfs2_contig_type ins_contig;
  723. int ins_contig_index;
  724. int ins_tree_depth;
  725. };
  726. struct ocfs2_merge_ctxt {
  727. enum ocfs2_contig_type c_contig_type;
  728. int c_has_empty_extent;
  729. int c_split_covers_rec;
  730. };
  731. static int ocfs2_validate_extent_block(struct super_block *sb,
  732. struct buffer_head *bh)
  733. {
  734. int rc;
  735. struct ocfs2_extent_block *eb =
  736. (struct ocfs2_extent_block *)bh->b_data;
  737. mlog(0, "Validating extent block %llu\n",
  738. (unsigned long long)bh->b_blocknr);
  739. BUG_ON(!buffer_uptodate(bh));
  740. /*
  741. * If the ecc fails, we return the error but otherwise
  742. * leave the filesystem running. We know any error is
  743. * local to this block.
  744. */
  745. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
  746. if (rc) {
  747. mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
  748. (unsigned long long)bh->b_blocknr);
  749. return rc;
  750. }
  751. /*
  752. * Errors after here are fatal.
  753. */
  754. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  755. ocfs2_error(sb,
  756. "Extent block #%llu has bad signature %.*s",
  757. (unsigned long long)bh->b_blocknr, 7,
  758. eb->h_signature);
  759. return -EINVAL;
  760. }
  761. if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
  762. ocfs2_error(sb,
  763. "Extent block #%llu has an invalid h_blkno "
  764. "of %llu",
  765. (unsigned long long)bh->b_blocknr,
  766. (unsigned long long)le64_to_cpu(eb->h_blkno));
  767. return -EINVAL;
  768. }
  769. if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  770. ocfs2_error(sb,
  771. "Extent block #%llu has an invalid "
  772. "h_fs_generation of #%u",
  773. (unsigned long long)bh->b_blocknr,
  774. le32_to_cpu(eb->h_fs_generation));
  775. return -EINVAL;
  776. }
  777. return 0;
  778. }
  779. int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
  780. struct buffer_head **bh)
  781. {
  782. int rc;
  783. struct buffer_head *tmp = *bh;
  784. rc = ocfs2_read_block(ci, eb_blkno, &tmp,
  785. ocfs2_validate_extent_block);
  786. /* If ocfs2_read_block() got us a new bh, pass it up. */
  787. if (!rc && !*bh)
  788. *bh = tmp;
  789. return rc;
  790. }
  791. /*
  792. * How many free extents have we got before we need more meta data?
  793. */
  794. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  795. struct ocfs2_extent_tree *et)
  796. {
  797. int retval;
  798. struct ocfs2_extent_list *el = NULL;
  799. struct ocfs2_extent_block *eb;
  800. struct buffer_head *eb_bh = NULL;
  801. u64 last_eb_blk = 0;
  802. mlog_entry_void();
  803. el = et->et_root_el;
  804. last_eb_blk = ocfs2_et_get_last_eb_blk(et);
  805. if (last_eb_blk) {
  806. retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
  807. &eb_bh);
  808. if (retval < 0) {
  809. mlog_errno(retval);
  810. goto bail;
  811. }
  812. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  813. el = &eb->h_list;
  814. }
  815. BUG_ON(el->l_tree_depth != 0);
  816. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  817. bail:
  818. brelse(eb_bh);
  819. mlog_exit(retval);
  820. return retval;
  821. }
  822. /* expects array to already be allocated
  823. *
  824. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  825. * l_count for you
  826. */
  827. static int ocfs2_create_new_meta_bhs(handle_t *handle,
  828. struct ocfs2_extent_tree *et,
  829. int wanted,
  830. struct ocfs2_alloc_context *meta_ac,
  831. struct buffer_head *bhs[])
  832. {
  833. int count, status, i;
  834. u16 suballoc_bit_start;
  835. u32 num_got;
  836. u64 first_blkno;
  837. struct ocfs2_super *osb =
  838. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  839. struct ocfs2_extent_block *eb;
  840. mlog_entry_void();
  841. count = 0;
  842. while (count < wanted) {
  843. status = ocfs2_claim_metadata(osb,
  844. handle,
  845. meta_ac,
  846. wanted - count,
  847. &suballoc_bit_start,
  848. &num_got,
  849. &first_blkno);
  850. if (status < 0) {
  851. mlog_errno(status);
  852. goto bail;
  853. }
  854. for(i = count; i < (num_got + count); i++) {
  855. bhs[i] = sb_getblk(osb->sb, first_blkno);
  856. if (bhs[i] == NULL) {
  857. status = -EIO;
  858. mlog_errno(status);
  859. goto bail;
  860. }
  861. ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
  862. status = ocfs2_journal_access_eb(handle, et->et_ci,
  863. bhs[i],
  864. OCFS2_JOURNAL_ACCESS_CREATE);
  865. if (status < 0) {
  866. mlog_errno(status);
  867. goto bail;
  868. }
  869. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  870. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  871. /* Ok, setup the minimal stuff here. */
  872. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  873. eb->h_blkno = cpu_to_le64(first_blkno);
  874. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  875. eb->h_suballoc_slot =
  876. cpu_to_le16(meta_ac->ac_alloc_slot);
  877. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  878. eb->h_list.l_count =
  879. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  880. suballoc_bit_start++;
  881. first_blkno++;
  882. /* We'll also be dirtied by the caller, so
  883. * this isn't absolutely necessary. */
  884. ocfs2_journal_dirty(handle, bhs[i]);
  885. }
  886. count += num_got;
  887. }
  888. status = 0;
  889. bail:
  890. if (status < 0) {
  891. for(i = 0; i < wanted; i++) {
  892. brelse(bhs[i]);
  893. bhs[i] = NULL;
  894. }
  895. }
  896. mlog_exit(status);
  897. return status;
  898. }
  899. /*
  900. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  901. *
  902. * Returns the sum of the rightmost extent rec logical offset and
  903. * cluster count.
  904. *
  905. * ocfs2_add_branch() uses this to determine what logical cluster
  906. * value should be populated into the leftmost new branch records.
  907. *
  908. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  909. * value for the new topmost tree record.
  910. */
  911. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  912. {
  913. int i;
  914. i = le16_to_cpu(el->l_next_free_rec) - 1;
  915. return le32_to_cpu(el->l_recs[i].e_cpos) +
  916. ocfs2_rec_clusters(el, &el->l_recs[i]);
  917. }
  918. /*
  919. * Change range of the branches in the right most path according to the leaf
  920. * extent block's rightmost record.
  921. */
  922. static int ocfs2_adjust_rightmost_branch(handle_t *handle,
  923. struct ocfs2_extent_tree *et)
  924. {
  925. int status;
  926. struct ocfs2_path *path = NULL;
  927. struct ocfs2_extent_list *el;
  928. struct ocfs2_extent_rec *rec;
  929. path = ocfs2_new_path_from_et(et);
  930. if (!path) {
  931. status = -ENOMEM;
  932. return status;
  933. }
  934. status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
  935. if (status < 0) {
  936. mlog_errno(status);
  937. goto out;
  938. }
  939. status = ocfs2_extend_trans(handle, path_num_items(path) +
  940. handle->h_buffer_credits);
  941. if (status < 0) {
  942. mlog_errno(status);
  943. goto out;
  944. }
  945. status = ocfs2_journal_access_path(et->et_ci, handle, path);
  946. if (status < 0) {
  947. mlog_errno(status);
  948. goto out;
  949. }
  950. el = path_leaf_el(path);
  951. rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
  952. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  953. out:
  954. ocfs2_free_path(path);
  955. return status;
  956. }
  957. /*
  958. * Add an entire tree branch to our inode. eb_bh is the extent block
  959. * to start at, if we don't want to start the branch at the root
  960. * structure.
  961. *
  962. * last_eb_bh is required as we have to update it's next_leaf pointer
  963. * for the new last extent block.
  964. *
  965. * the new branch will be 'empty' in the sense that every block will
  966. * contain a single record with cluster count == 0.
  967. */
  968. static int ocfs2_add_branch(handle_t *handle,
  969. struct ocfs2_extent_tree *et,
  970. struct buffer_head *eb_bh,
  971. struct buffer_head **last_eb_bh,
  972. struct ocfs2_alloc_context *meta_ac)
  973. {
  974. int status, new_blocks, i;
  975. u64 next_blkno, new_last_eb_blk;
  976. struct buffer_head *bh;
  977. struct buffer_head **new_eb_bhs = NULL;
  978. struct ocfs2_extent_block *eb;
  979. struct ocfs2_extent_list *eb_el;
  980. struct ocfs2_extent_list *el;
  981. u32 new_cpos, root_end;
  982. mlog_entry_void();
  983. BUG_ON(!last_eb_bh || !*last_eb_bh);
  984. if (eb_bh) {
  985. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  986. el = &eb->h_list;
  987. } else
  988. el = et->et_root_el;
  989. /* we never add a branch to a leaf. */
  990. BUG_ON(!el->l_tree_depth);
  991. new_blocks = le16_to_cpu(el->l_tree_depth);
  992. eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
  993. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  994. root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
  995. /*
  996. * If there is a gap before the root end and the real end
  997. * of the righmost leaf block, we need to remove the gap
  998. * between new_cpos and root_end first so that the tree
  999. * is consistent after we add a new branch(it will start
  1000. * from new_cpos).
  1001. */
  1002. if (root_end > new_cpos) {
  1003. mlog(0, "adjust the cluster end from %u to %u\n",
  1004. root_end, new_cpos);
  1005. status = ocfs2_adjust_rightmost_branch(handle, et);
  1006. if (status) {
  1007. mlog_errno(status);
  1008. goto bail;
  1009. }
  1010. }
  1011. /* allocate the number of new eb blocks we need */
  1012. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  1013. GFP_KERNEL);
  1014. if (!new_eb_bhs) {
  1015. status = -ENOMEM;
  1016. mlog_errno(status);
  1017. goto bail;
  1018. }
  1019. status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
  1020. meta_ac, new_eb_bhs);
  1021. if (status < 0) {
  1022. mlog_errno(status);
  1023. goto bail;
  1024. }
  1025. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  1026. * linked with the rest of the tree.
  1027. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  1028. *
  1029. * when we leave the loop, new_last_eb_blk will point to the
  1030. * newest leaf, and next_blkno will point to the topmost extent
  1031. * block. */
  1032. next_blkno = new_last_eb_blk = 0;
  1033. for(i = 0; i < new_blocks; i++) {
  1034. bh = new_eb_bhs[i];
  1035. eb = (struct ocfs2_extent_block *) bh->b_data;
  1036. /* ocfs2_create_new_meta_bhs() should create it right! */
  1037. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1038. eb_el = &eb->h_list;
  1039. status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
  1040. OCFS2_JOURNAL_ACCESS_CREATE);
  1041. if (status < 0) {
  1042. mlog_errno(status);
  1043. goto bail;
  1044. }
  1045. eb->h_next_leaf_blk = 0;
  1046. eb_el->l_tree_depth = cpu_to_le16(i);
  1047. eb_el->l_next_free_rec = cpu_to_le16(1);
  1048. /*
  1049. * This actually counts as an empty extent as
  1050. * c_clusters == 0
  1051. */
  1052. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  1053. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  1054. /*
  1055. * eb_el isn't always an interior node, but even leaf
  1056. * nodes want a zero'd flags and reserved field so
  1057. * this gets the whole 32 bits regardless of use.
  1058. */
  1059. eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
  1060. if (!eb_el->l_tree_depth)
  1061. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  1062. ocfs2_journal_dirty(handle, bh);
  1063. next_blkno = le64_to_cpu(eb->h_blkno);
  1064. }
  1065. /* This is a bit hairy. We want to update up to three blocks
  1066. * here without leaving any of them in an inconsistent state
  1067. * in case of error. We don't have to worry about
  1068. * journal_dirty erroring as it won't unless we've aborted the
  1069. * handle (in which case we would never be here) so reserving
  1070. * the write with journal_access is all we need to do. */
  1071. status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
  1072. OCFS2_JOURNAL_ACCESS_WRITE);
  1073. if (status < 0) {
  1074. mlog_errno(status);
  1075. goto bail;
  1076. }
  1077. status = ocfs2_et_root_journal_access(handle, et,
  1078. OCFS2_JOURNAL_ACCESS_WRITE);
  1079. if (status < 0) {
  1080. mlog_errno(status);
  1081. goto bail;
  1082. }
  1083. if (eb_bh) {
  1084. status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
  1085. OCFS2_JOURNAL_ACCESS_WRITE);
  1086. if (status < 0) {
  1087. mlog_errno(status);
  1088. goto bail;
  1089. }
  1090. }
  1091. /* Link the new branch into the rest of the tree (el will
  1092. * either be on the root_bh, or the extent block passed in. */
  1093. i = le16_to_cpu(el->l_next_free_rec);
  1094. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  1095. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  1096. el->l_recs[i].e_int_clusters = 0;
  1097. le16_add_cpu(&el->l_next_free_rec, 1);
  1098. /* fe needs a new last extent block pointer, as does the
  1099. * next_leaf on the previously last-extent-block. */
  1100. ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
  1101. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  1102. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  1103. ocfs2_journal_dirty(handle, *last_eb_bh);
  1104. ocfs2_journal_dirty(handle, et->et_root_bh);
  1105. if (eb_bh)
  1106. ocfs2_journal_dirty(handle, eb_bh);
  1107. /*
  1108. * Some callers want to track the rightmost leaf so pass it
  1109. * back here.
  1110. */
  1111. brelse(*last_eb_bh);
  1112. get_bh(new_eb_bhs[0]);
  1113. *last_eb_bh = new_eb_bhs[0];
  1114. status = 0;
  1115. bail:
  1116. if (new_eb_bhs) {
  1117. for (i = 0; i < new_blocks; i++)
  1118. brelse(new_eb_bhs[i]);
  1119. kfree(new_eb_bhs);
  1120. }
  1121. mlog_exit(status);
  1122. return status;
  1123. }
  1124. /*
  1125. * adds another level to the allocation tree.
  1126. * returns back the new extent block so you can add a branch to it
  1127. * after this call.
  1128. */
  1129. static int ocfs2_shift_tree_depth(handle_t *handle,
  1130. struct ocfs2_extent_tree *et,
  1131. struct ocfs2_alloc_context *meta_ac,
  1132. struct buffer_head **ret_new_eb_bh)
  1133. {
  1134. int status, i;
  1135. u32 new_clusters;
  1136. struct buffer_head *new_eb_bh = NULL;
  1137. struct ocfs2_extent_block *eb;
  1138. struct ocfs2_extent_list *root_el;
  1139. struct ocfs2_extent_list *eb_el;
  1140. mlog_entry_void();
  1141. status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
  1142. &new_eb_bh);
  1143. if (status < 0) {
  1144. mlog_errno(status);
  1145. goto bail;
  1146. }
  1147. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  1148. /* ocfs2_create_new_meta_bhs() should create it right! */
  1149. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  1150. eb_el = &eb->h_list;
  1151. root_el = et->et_root_el;
  1152. status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
  1153. OCFS2_JOURNAL_ACCESS_CREATE);
  1154. if (status < 0) {
  1155. mlog_errno(status);
  1156. goto bail;
  1157. }
  1158. /* copy the root extent list data into the new extent block */
  1159. eb_el->l_tree_depth = root_el->l_tree_depth;
  1160. eb_el->l_next_free_rec = root_el->l_next_free_rec;
  1161. for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1162. eb_el->l_recs[i] = root_el->l_recs[i];
  1163. ocfs2_journal_dirty(handle, new_eb_bh);
  1164. status = ocfs2_et_root_journal_access(handle, et,
  1165. OCFS2_JOURNAL_ACCESS_WRITE);
  1166. if (status < 0) {
  1167. mlog_errno(status);
  1168. goto bail;
  1169. }
  1170. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  1171. /* update root_bh now */
  1172. le16_add_cpu(&root_el->l_tree_depth, 1);
  1173. root_el->l_recs[0].e_cpos = 0;
  1174. root_el->l_recs[0].e_blkno = eb->h_blkno;
  1175. root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
  1176. for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  1177. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  1178. root_el->l_next_free_rec = cpu_to_le16(1);
  1179. /* If this is our 1st tree depth shift, then last_eb_blk
  1180. * becomes the allocated extent block */
  1181. if (root_el->l_tree_depth == cpu_to_le16(1))
  1182. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  1183. ocfs2_journal_dirty(handle, et->et_root_bh);
  1184. *ret_new_eb_bh = new_eb_bh;
  1185. new_eb_bh = NULL;
  1186. status = 0;
  1187. bail:
  1188. brelse(new_eb_bh);
  1189. mlog_exit(status);
  1190. return status;
  1191. }
  1192. /*
  1193. * Should only be called when there is no space left in any of the
  1194. * leaf nodes. What we want to do is find the lowest tree depth
  1195. * non-leaf extent block with room for new records. There are three
  1196. * valid results of this search:
  1197. *
  1198. * 1) a lowest extent block is found, then we pass it back in
  1199. * *lowest_eb_bh and return '0'
  1200. *
  1201. * 2) the search fails to find anything, but the root_el has room. We
  1202. * pass NULL back in *lowest_eb_bh, but still return '0'
  1203. *
  1204. * 3) the search fails to find anything AND the root_el is full, in
  1205. * which case we return > 0
  1206. *
  1207. * return status < 0 indicates an error.
  1208. */
  1209. static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
  1210. struct buffer_head **target_bh)
  1211. {
  1212. int status = 0, i;
  1213. u64 blkno;
  1214. struct ocfs2_extent_block *eb;
  1215. struct ocfs2_extent_list *el;
  1216. struct buffer_head *bh = NULL;
  1217. struct buffer_head *lowest_bh = NULL;
  1218. mlog_entry_void();
  1219. *target_bh = NULL;
  1220. el = et->et_root_el;
  1221. while(le16_to_cpu(el->l_tree_depth) > 1) {
  1222. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1223. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1224. "Owner %llu has empty "
  1225. "extent list (next_free_rec == 0)",
  1226. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  1227. status = -EIO;
  1228. goto bail;
  1229. }
  1230. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1231. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1232. if (!blkno) {
  1233. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1234. "Owner %llu has extent "
  1235. "list where extent # %d has no physical "
  1236. "block start",
  1237. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
  1238. status = -EIO;
  1239. goto bail;
  1240. }
  1241. brelse(bh);
  1242. bh = NULL;
  1243. status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
  1244. if (status < 0) {
  1245. mlog_errno(status);
  1246. goto bail;
  1247. }
  1248. eb = (struct ocfs2_extent_block *) bh->b_data;
  1249. el = &eb->h_list;
  1250. if (le16_to_cpu(el->l_next_free_rec) <
  1251. le16_to_cpu(el->l_count)) {
  1252. brelse(lowest_bh);
  1253. lowest_bh = bh;
  1254. get_bh(lowest_bh);
  1255. }
  1256. }
  1257. /* If we didn't find one and the fe doesn't have any room,
  1258. * then return '1' */
  1259. el = et->et_root_el;
  1260. if (!lowest_bh && (el->l_next_free_rec == el->l_count))
  1261. status = 1;
  1262. *target_bh = lowest_bh;
  1263. bail:
  1264. brelse(bh);
  1265. mlog_exit(status);
  1266. return status;
  1267. }
  1268. /*
  1269. * Grow a b-tree so that it has more records.
  1270. *
  1271. * We might shift the tree depth in which case existing paths should
  1272. * be considered invalid.
  1273. *
  1274. * Tree depth after the grow is returned via *final_depth.
  1275. *
  1276. * *last_eb_bh will be updated by ocfs2_add_branch().
  1277. */
  1278. static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  1279. int *final_depth, struct buffer_head **last_eb_bh,
  1280. struct ocfs2_alloc_context *meta_ac)
  1281. {
  1282. int ret, shift;
  1283. struct ocfs2_extent_list *el = et->et_root_el;
  1284. int depth = le16_to_cpu(el->l_tree_depth);
  1285. struct buffer_head *bh = NULL;
  1286. BUG_ON(meta_ac == NULL);
  1287. shift = ocfs2_find_branch_target(et, &bh);
  1288. if (shift < 0) {
  1289. ret = shift;
  1290. mlog_errno(ret);
  1291. goto out;
  1292. }
  1293. /* We traveled all the way to the bottom of the allocation tree
  1294. * and didn't find room for any more extents - we need to add
  1295. * another tree level */
  1296. if (shift) {
  1297. BUG_ON(bh);
  1298. mlog(0, "need to shift tree depth (current = %d)\n", depth);
  1299. /* ocfs2_shift_tree_depth will return us a buffer with
  1300. * the new extent block (so we can pass that to
  1301. * ocfs2_add_branch). */
  1302. ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
  1303. if (ret < 0) {
  1304. mlog_errno(ret);
  1305. goto out;
  1306. }
  1307. depth++;
  1308. if (depth == 1) {
  1309. /*
  1310. * Special case: we have room now if we shifted from
  1311. * tree_depth 0, so no more work needs to be done.
  1312. *
  1313. * We won't be calling add_branch, so pass
  1314. * back *last_eb_bh as the new leaf. At depth
  1315. * zero, it should always be null so there's
  1316. * no reason to brelse.
  1317. */
  1318. BUG_ON(*last_eb_bh);
  1319. get_bh(bh);
  1320. *last_eb_bh = bh;
  1321. goto out;
  1322. }
  1323. }
  1324. /* call ocfs2_add_branch to add the final part of the tree with
  1325. * the new data. */
  1326. mlog(0, "add branch. bh = %p\n", bh);
  1327. ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
  1328. meta_ac);
  1329. if (ret < 0) {
  1330. mlog_errno(ret);
  1331. goto out;
  1332. }
  1333. out:
  1334. if (final_depth)
  1335. *final_depth = depth;
  1336. brelse(bh);
  1337. return ret;
  1338. }
  1339. /*
  1340. * This function will discard the rightmost extent record.
  1341. */
  1342. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  1343. {
  1344. int next_free = le16_to_cpu(el->l_next_free_rec);
  1345. int count = le16_to_cpu(el->l_count);
  1346. unsigned int num_bytes;
  1347. BUG_ON(!next_free);
  1348. /* This will cause us to go off the end of our extent list. */
  1349. BUG_ON(next_free >= count);
  1350. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  1351. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  1352. }
  1353. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  1354. struct ocfs2_extent_rec *insert_rec)
  1355. {
  1356. int i, insert_index, next_free, has_empty, num_bytes;
  1357. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  1358. struct ocfs2_extent_rec *rec;
  1359. next_free = le16_to_cpu(el->l_next_free_rec);
  1360. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  1361. BUG_ON(!next_free);
  1362. /* The tree code before us didn't allow enough room in the leaf. */
  1363. BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
  1364. /*
  1365. * The easiest way to approach this is to just remove the
  1366. * empty extent and temporarily decrement next_free.
  1367. */
  1368. if (has_empty) {
  1369. /*
  1370. * If next_free was 1 (only an empty extent), this
  1371. * loop won't execute, which is fine. We still want
  1372. * the decrement above to happen.
  1373. */
  1374. for(i = 0; i < (next_free - 1); i++)
  1375. el->l_recs[i] = el->l_recs[i+1];
  1376. next_free--;
  1377. }
  1378. /*
  1379. * Figure out what the new record index should be.
  1380. */
  1381. for(i = 0; i < next_free; i++) {
  1382. rec = &el->l_recs[i];
  1383. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  1384. break;
  1385. }
  1386. insert_index = i;
  1387. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  1388. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  1389. BUG_ON(insert_index < 0);
  1390. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  1391. BUG_ON(insert_index > next_free);
  1392. /*
  1393. * No need to memmove if we're just adding to the tail.
  1394. */
  1395. if (insert_index != next_free) {
  1396. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  1397. num_bytes = next_free - insert_index;
  1398. num_bytes *= sizeof(struct ocfs2_extent_rec);
  1399. memmove(&el->l_recs[insert_index + 1],
  1400. &el->l_recs[insert_index],
  1401. num_bytes);
  1402. }
  1403. /*
  1404. * Either we had an empty extent, and need to re-increment or
  1405. * there was no empty extent on a non full rightmost leaf node,
  1406. * in which case we still need to increment.
  1407. */
  1408. next_free++;
  1409. el->l_next_free_rec = cpu_to_le16(next_free);
  1410. /*
  1411. * Make sure none of the math above just messed up our tree.
  1412. */
  1413. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  1414. el->l_recs[insert_index] = *insert_rec;
  1415. }
  1416. static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
  1417. {
  1418. int size, num_recs = le16_to_cpu(el->l_next_free_rec);
  1419. BUG_ON(num_recs == 0);
  1420. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  1421. num_recs--;
  1422. size = num_recs * sizeof(struct ocfs2_extent_rec);
  1423. memmove(&el->l_recs[0], &el->l_recs[1], size);
  1424. memset(&el->l_recs[num_recs], 0,
  1425. sizeof(struct ocfs2_extent_rec));
  1426. el->l_next_free_rec = cpu_to_le16(num_recs);
  1427. }
  1428. }
  1429. /*
  1430. * Create an empty extent record .
  1431. *
  1432. * l_next_free_rec may be updated.
  1433. *
  1434. * If an empty extent already exists do nothing.
  1435. */
  1436. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  1437. {
  1438. int next_free = le16_to_cpu(el->l_next_free_rec);
  1439. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  1440. if (next_free == 0)
  1441. goto set_and_inc;
  1442. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  1443. return;
  1444. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  1445. "Asked to create an empty extent in a full list:\n"
  1446. "count = %u, tree depth = %u",
  1447. le16_to_cpu(el->l_count),
  1448. le16_to_cpu(el->l_tree_depth));
  1449. ocfs2_shift_records_right(el);
  1450. set_and_inc:
  1451. le16_add_cpu(&el->l_next_free_rec, 1);
  1452. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1453. }
  1454. /*
  1455. * For a rotation which involves two leaf nodes, the "root node" is
  1456. * the lowest level tree node which contains a path to both leafs. This
  1457. * resulting set of information can be used to form a complete "subtree"
  1458. *
  1459. * This function is passed two full paths from the dinode down to a
  1460. * pair of adjacent leaves. It's task is to figure out which path
  1461. * index contains the subtree root - this can be the root index itself
  1462. * in a worst-case rotation.
  1463. *
  1464. * The array index of the subtree root is passed back.
  1465. */
  1466. int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
  1467. struct ocfs2_path *left,
  1468. struct ocfs2_path *right)
  1469. {
  1470. int i = 0;
  1471. /*
  1472. * Check that the caller passed in two paths from the same tree.
  1473. */
  1474. BUG_ON(path_root_bh(left) != path_root_bh(right));
  1475. do {
  1476. i++;
  1477. /*
  1478. * The caller didn't pass two adjacent paths.
  1479. */
  1480. mlog_bug_on_msg(i > left->p_tree_depth,
  1481. "Owner %llu, left depth %u, right depth %u\n"
  1482. "left leaf blk %llu, right leaf blk %llu\n",
  1483. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1484. left->p_tree_depth, right->p_tree_depth,
  1485. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  1486. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  1487. } while (left->p_node[i].bh->b_blocknr ==
  1488. right->p_node[i].bh->b_blocknr);
  1489. return i - 1;
  1490. }
  1491. typedef void (path_insert_t)(void *, struct buffer_head *);
  1492. /*
  1493. * Traverse a btree path in search of cpos, starting at root_el.
  1494. *
  1495. * This code can be called with a cpos larger than the tree, in which
  1496. * case it will return the rightmost path.
  1497. */
  1498. static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
  1499. struct ocfs2_extent_list *root_el, u32 cpos,
  1500. path_insert_t *func, void *data)
  1501. {
  1502. int i, ret = 0;
  1503. u32 range;
  1504. u64 blkno;
  1505. struct buffer_head *bh = NULL;
  1506. struct ocfs2_extent_block *eb;
  1507. struct ocfs2_extent_list *el;
  1508. struct ocfs2_extent_rec *rec;
  1509. el = root_el;
  1510. while (el->l_tree_depth) {
  1511. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  1512. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1513. "Owner %llu has empty extent list at "
  1514. "depth %u\n",
  1515. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1516. le16_to_cpu(el->l_tree_depth));
  1517. ret = -EROFS;
  1518. goto out;
  1519. }
  1520. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  1521. rec = &el->l_recs[i];
  1522. /*
  1523. * In the case that cpos is off the allocation
  1524. * tree, this should just wind up returning the
  1525. * rightmost record.
  1526. */
  1527. range = le32_to_cpu(rec->e_cpos) +
  1528. ocfs2_rec_clusters(el, rec);
  1529. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1530. break;
  1531. }
  1532. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  1533. if (blkno == 0) {
  1534. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1535. "Owner %llu has bad blkno in extent list "
  1536. "at depth %u (index %d)\n",
  1537. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1538. le16_to_cpu(el->l_tree_depth), i);
  1539. ret = -EROFS;
  1540. goto out;
  1541. }
  1542. brelse(bh);
  1543. bh = NULL;
  1544. ret = ocfs2_read_extent_block(ci, blkno, &bh);
  1545. if (ret) {
  1546. mlog_errno(ret);
  1547. goto out;
  1548. }
  1549. eb = (struct ocfs2_extent_block *) bh->b_data;
  1550. el = &eb->h_list;
  1551. if (le16_to_cpu(el->l_next_free_rec) >
  1552. le16_to_cpu(el->l_count)) {
  1553. ocfs2_error(ocfs2_metadata_cache_get_super(ci),
  1554. "Owner %llu has bad count in extent list "
  1555. "at block %llu (next free=%u, count=%u)\n",
  1556. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  1557. (unsigned long long)bh->b_blocknr,
  1558. le16_to_cpu(el->l_next_free_rec),
  1559. le16_to_cpu(el->l_count));
  1560. ret = -EROFS;
  1561. goto out;
  1562. }
  1563. if (func)
  1564. func(data, bh);
  1565. }
  1566. out:
  1567. /*
  1568. * Catch any trailing bh that the loop didn't handle.
  1569. */
  1570. brelse(bh);
  1571. return ret;
  1572. }
  1573. /*
  1574. * Given an initialized path (that is, it has a valid root extent
  1575. * list), this function will traverse the btree in search of the path
  1576. * which would contain cpos.
  1577. *
  1578. * The path traveled is recorded in the path structure.
  1579. *
  1580. * Note that this will not do any comparisons on leaf node extent
  1581. * records, so it will work fine in the case that we just added a tree
  1582. * branch.
  1583. */
  1584. struct find_path_data {
  1585. int index;
  1586. struct ocfs2_path *path;
  1587. };
  1588. static void find_path_ins(void *data, struct buffer_head *bh)
  1589. {
  1590. struct find_path_data *fp = data;
  1591. get_bh(bh);
  1592. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  1593. fp->index++;
  1594. }
  1595. int ocfs2_find_path(struct ocfs2_caching_info *ci,
  1596. struct ocfs2_path *path, u32 cpos)
  1597. {
  1598. struct find_path_data data;
  1599. data.index = 1;
  1600. data.path = path;
  1601. return __ocfs2_find_path(ci, path_root_el(path), cpos,
  1602. find_path_ins, &data);
  1603. }
  1604. static void find_leaf_ins(void *data, struct buffer_head *bh)
  1605. {
  1606. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  1607. struct ocfs2_extent_list *el = &eb->h_list;
  1608. struct buffer_head **ret = data;
  1609. /* We want to retain only the leaf block. */
  1610. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1611. get_bh(bh);
  1612. *ret = bh;
  1613. }
  1614. }
  1615. /*
  1616. * Find the leaf block in the tree which would contain cpos. No
  1617. * checking of the actual leaf is done.
  1618. *
  1619. * Some paths want to call this instead of allocating a path structure
  1620. * and calling ocfs2_find_path().
  1621. *
  1622. * This function doesn't handle non btree extent lists.
  1623. */
  1624. int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
  1625. struct ocfs2_extent_list *root_el, u32 cpos,
  1626. struct buffer_head **leaf_bh)
  1627. {
  1628. int ret;
  1629. struct buffer_head *bh = NULL;
  1630. ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
  1631. if (ret) {
  1632. mlog_errno(ret);
  1633. goto out;
  1634. }
  1635. *leaf_bh = bh;
  1636. out:
  1637. return ret;
  1638. }
  1639. /*
  1640. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1641. *
  1642. * Basically, we've moved stuff around at the bottom of the tree and
  1643. * we need to fix up the extent records above the changes to reflect
  1644. * the new changes.
  1645. *
  1646. * left_rec: the record on the left.
  1647. * left_child_el: is the child list pointed to by left_rec
  1648. * right_rec: the record to the right of left_rec
  1649. * right_child_el: is the child list pointed to by right_rec
  1650. *
  1651. * By definition, this only works on interior nodes.
  1652. */
  1653. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1654. struct ocfs2_extent_list *left_child_el,
  1655. struct ocfs2_extent_rec *right_rec,
  1656. struct ocfs2_extent_list *right_child_el)
  1657. {
  1658. u32 left_clusters, right_end;
  1659. /*
  1660. * Interior nodes never have holes. Their cpos is the cpos of
  1661. * the leftmost record in their child list. Their cluster
  1662. * count covers the full theoretical range of their child list
  1663. * - the range between their cpos and the cpos of the record
  1664. * immediately to their right.
  1665. */
  1666. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1667. if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
  1668. BUG_ON(right_child_el->l_tree_depth);
  1669. BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
  1670. left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
  1671. }
  1672. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1673. left_rec->e_int_clusters = cpu_to_le32(left_clusters);
  1674. /*
  1675. * Calculate the rightmost cluster count boundary before
  1676. * moving cpos - we will need to adjust clusters after
  1677. * updating e_cpos to keep the same highest cluster count.
  1678. */
  1679. right_end = le32_to_cpu(right_rec->e_cpos);
  1680. right_end += le32_to_cpu(right_rec->e_int_clusters);
  1681. right_rec->e_cpos = left_rec->e_cpos;
  1682. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1683. right_end -= le32_to_cpu(right_rec->e_cpos);
  1684. right_rec->e_int_clusters = cpu_to_le32(right_end);
  1685. }
  1686. /*
  1687. * Adjust the adjacent root node records involved in a
  1688. * rotation. left_el_blkno is passed in as a key so that we can easily
  1689. * find it's index in the root list.
  1690. */
  1691. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1692. struct ocfs2_extent_list *left_el,
  1693. struct ocfs2_extent_list *right_el,
  1694. u64 left_el_blkno)
  1695. {
  1696. int i;
  1697. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1698. le16_to_cpu(left_el->l_tree_depth));
  1699. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1700. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1701. break;
  1702. }
  1703. /*
  1704. * The path walking code should have never returned a root and
  1705. * two paths which are not adjacent.
  1706. */
  1707. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1708. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1709. &root_el->l_recs[i + 1], right_el);
  1710. }
  1711. /*
  1712. * We've changed a leaf block (in right_path) and need to reflect that
  1713. * change back up the subtree.
  1714. *
  1715. * This happens in multiple places:
  1716. * - When we've moved an extent record from the left path leaf to the right
  1717. * path leaf to make room for an empty extent in the left path leaf.
  1718. * - When our insert into the right path leaf is at the leftmost edge
  1719. * and requires an update of the path immediately to it's left. This
  1720. * can occur at the end of some types of rotation and appending inserts.
  1721. * - When we've adjusted the last extent record in the left path leaf and the
  1722. * 1st extent record in the right path leaf during cross extent block merge.
  1723. */
  1724. static void ocfs2_complete_edge_insert(handle_t *handle,
  1725. struct ocfs2_path *left_path,
  1726. struct ocfs2_path *right_path,
  1727. int subtree_index)
  1728. {
  1729. int i, idx;
  1730. struct ocfs2_extent_list *el, *left_el, *right_el;
  1731. struct ocfs2_extent_rec *left_rec, *right_rec;
  1732. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1733. /*
  1734. * Update the counts and position values within all the
  1735. * interior nodes to reflect the leaf rotation we just did.
  1736. *
  1737. * The root node is handled below the loop.
  1738. *
  1739. * We begin the loop with right_el and left_el pointing to the
  1740. * leaf lists and work our way up.
  1741. *
  1742. * NOTE: within this loop, left_el and right_el always refer
  1743. * to the *child* lists.
  1744. */
  1745. left_el = path_leaf_el(left_path);
  1746. right_el = path_leaf_el(right_path);
  1747. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1748. mlog(0, "Adjust records at index %u\n", i);
  1749. /*
  1750. * One nice property of knowing that all of these
  1751. * nodes are below the root is that we only deal with
  1752. * the leftmost right node record and the rightmost
  1753. * left node record.
  1754. */
  1755. el = left_path->p_node[i].el;
  1756. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1757. left_rec = &el->l_recs[idx];
  1758. el = right_path->p_node[i].el;
  1759. right_rec = &el->l_recs[0];
  1760. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1761. right_el);
  1762. ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1763. ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1764. /*
  1765. * Setup our list pointers now so that the current
  1766. * parents become children in the next iteration.
  1767. */
  1768. left_el = left_path->p_node[i].el;
  1769. right_el = right_path->p_node[i].el;
  1770. }
  1771. /*
  1772. * At the root node, adjust the two adjacent records which
  1773. * begin our path to the leaves.
  1774. */
  1775. el = left_path->p_node[subtree_index].el;
  1776. left_el = left_path->p_node[subtree_index + 1].el;
  1777. right_el = right_path->p_node[subtree_index + 1].el;
  1778. ocfs2_adjust_root_records(el, left_el, right_el,
  1779. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1780. root_bh = left_path->p_node[subtree_index].bh;
  1781. ocfs2_journal_dirty(handle, root_bh);
  1782. }
  1783. static int ocfs2_rotate_subtree_right(handle_t *handle,
  1784. struct ocfs2_extent_tree *et,
  1785. struct ocfs2_path *left_path,
  1786. struct ocfs2_path *right_path,
  1787. int subtree_index)
  1788. {
  1789. int ret, i;
  1790. struct buffer_head *right_leaf_bh;
  1791. struct buffer_head *left_leaf_bh = NULL;
  1792. struct buffer_head *root_bh;
  1793. struct ocfs2_extent_list *right_el, *left_el;
  1794. struct ocfs2_extent_rec move_rec;
  1795. left_leaf_bh = path_leaf_bh(left_path);
  1796. left_el = path_leaf_el(left_path);
  1797. if (left_el->l_next_free_rec != left_el->l_count) {
  1798. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  1799. "Inode %llu has non-full interior leaf node %llu"
  1800. "(next free = %u)",
  1801. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1802. (unsigned long long)left_leaf_bh->b_blocknr,
  1803. le16_to_cpu(left_el->l_next_free_rec));
  1804. return -EROFS;
  1805. }
  1806. /*
  1807. * This extent block may already have an empty record, so we
  1808. * return early if so.
  1809. */
  1810. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1811. return 0;
  1812. root_bh = left_path->p_node[subtree_index].bh;
  1813. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1814. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  1815. subtree_index);
  1816. if (ret) {
  1817. mlog_errno(ret);
  1818. goto out;
  1819. }
  1820. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1821. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1822. right_path, i);
  1823. if (ret) {
  1824. mlog_errno(ret);
  1825. goto out;
  1826. }
  1827. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  1828. left_path, i);
  1829. if (ret) {
  1830. mlog_errno(ret);
  1831. goto out;
  1832. }
  1833. }
  1834. right_leaf_bh = path_leaf_bh(right_path);
  1835. right_el = path_leaf_el(right_path);
  1836. /* This is a code error, not a disk corruption. */
  1837. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1838. "because rightmost leaf block %llu is empty\n",
  1839. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  1840. (unsigned long long)right_leaf_bh->b_blocknr);
  1841. ocfs2_create_empty_extent(right_el);
  1842. ocfs2_journal_dirty(handle, right_leaf_bh);
  1843. /* Do the copy now. */
  1844. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1845. move_rec = left_el->l_recs[i];
  1846. right_el->l_recs[0] = move_rec;
  1847. /*
  1848. * Clear out the record we just copied and shift everything
  1849. * over, leaving an empty extent in the left leaf.
  1850. *
  1851. * We temporarily subtract from next_free_rec so that the
  1852. * shift will lose the tail record (which is now defunct).
  1853. */
  1854. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1855. ocfs2_shift_records_right(left_el);
  1856. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1857. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1858. ocfs2_journal_dirty(handle, left_leaf_bh);
  1859. ocfs2_complete_edge_insert(handle, left_path, right_path,
  1860. subtree_index);
  1861. out:
  1862. return ret;
  1863. }
  1864. /*
  1865. * Given a full path, determine what cpos value would return us a path
  1866. * containing the leaf immediately to the left of the current one.
  1867. *
  1868. * Will return zero if the path passed in is already the leftmost path.
  1869. */
  1870. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1871. struct ocfs2_path *path, u32 *cpos)
  1872. {
  1873. int i, j, ret = 0;
  1874. u64 blkno;
  1875. struct ocfs2_extent_list *el;
  1876. BUG_ON(path->p_tree_depth == 0);
  1877. *cpos = 0;
  1878. blkno = path_leaf_bh(path)->b_blocknr;
  1879. /* Start at the tree node just above the leaf and work our way up. */
  1880. i = path->p_tree_depth - 1;
  1881. while (i >= 0) {
  1882. el = path->p_node[i].el;
  1883. /*
  1884. * Find the extent record just before the one in our
  1885. * path.
  1886. */
  1887. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1888. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1889. if (j == 0) {
  1890. if (i == 0) {
  1891. /*
  1892. * We've determined that the
  1893. * path specified is already
  1894. * the leftmost one - return a
  1895. * cpos of zero.
  1896. */
  1897. goto out;
  1898. }
  1899. /*
  1900. * The leftmost record points to our
  1901. * leaf - we need to travel up the
  1902. * tree one level.
  1903. */
  1904. goto next_node;
  1905. }
  1906. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1907. *cpos = *cpos + ocfs2_rec_clusters(el,
  1908. &el->l_recs[j - 1]);
  1909. *cpos = *cpos - 1;
  1910. goto out;
  1911. }
  1912. }
  1913. /*
  1914. * If we got here, we never found a valid node where
  1915. * the tree indicated one should be.
  1916. */
  1917. ocfs2_error(sb,
  1918. "Invalid extent tree at extent block %llu\n",
  1919. (unsigned long long)blkno);
  1920. ret = -EROFS;
  1921. goto out;
  1922. next_node:
  1923. blkno = path->p_node[i].bh->b_blocknr;
  1924. i--;
  1925. }
  1926. out:
  1927. return ret;
  1928. }
  1929. /*
  1930. * Extend the transaction by enough credits to complete the rotation,
  1931. * and still leave at least the original number of credits allocated
  1932. * to this transaction.
  1933. */
  1934. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1935. int op_credits,
  1936. struct ocfs2_path *path)
  1937. {
  1938. int ret;
  1939. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
  1940. if (handle->h_buffer_credits < credits) {
  1941. ret = ocfs2_extend_trans(handle,
  1942. credits - handle->h_buffer_credits);
  1943. if (ret)
  1944. return ret;
  1945. if (unlikely(handle->h_buffer_credits < credits))
  1946. return ocfs2_extend_trans(handle, credits);
  1947. }
  1948. return 0;
  1949. }
  1950. /*
  1951. * Trap the case where we're inserting into the theoretical range past
  1952. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1953. * whose cpos is less than ours into the right leaf.
  1954. *
  1955. * It's only necessary to look at the rightmost record of the left
  1956. * leaf because the logic that calls us should ensure that the
  1957. * theoretical ranges in the path components above the leaves are
  1958. * correct.
  1959. */
  1960. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1961. u32 insert_cpos)
  1962. {
  1963. struct ocfs2_extent_list *left_el;
  1964. struct ocfs2_extent_rec *rec;
  1965. int next_free;
  1966. left_el = path_leaf_el(left_path);
  1967. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1968. rec = &left_el->l_recs[next_free - 1];
  1969. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1970. return 1;
  1971. return 0;
  1972. }
  1973. static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
  1974. {
  1975. int next_free = le16_to_cpu(el->l_next_free_rec);
  1976. unsigned int range;
  1977. struct ocfs2_extent_rec *rec;
  1978. if (next_free == 0)
  1979. return 0;
  1980. rec = &el->l_recs[0];
  1981. if (ocfs2_is_empty_extent(rec)) {
  1982. /* Empty list. */
  1983. if (next_free == 1)
  1984. return 0;
  1985. rec = &el->l_recs[1];
  1986. }
  1987. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1988. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  1989. return 1;
  1990. return 0;
  1991. }
  1992. /*
  1993. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1994. *
  1995. * The path to the rightmost leaf should be passed in.
  1996. *
  1997. * The array is assumed to be large enough to hold an entire path (tree depth).
  1998. *
  1999. * Upon successful return from this function:
  2000. *
  2001. * - The 'right_path' array will contain a path to the leaf block
  2002. * whose range contains e_cpos.
  2003. * - That leaf block will have a single empty extent in list index 0.
  2004. * - In the case that the rotation requires a post-insert update,
  2005. * *ret_left_path will contain a valid path which can be passed to
  2006. * ocfs2_insert_path().
  2007. */
  2008. static int ocfs2_rotate_tree_right(handle_t *handle,
  2009. struct ocfs2_extent_tree *et,
  2010. enum ocfs2_split_type split,
  2011. u32 insert_cpos,
  2012. struct ocfs2_path *right_path,
  2013. struct ocfs2_path **ret_left_path)
  2014. {
  2015. int ret, start, orig_credits = handle->h_buffer_credits;
  2016. u32 cpos;
  2017. struct ocfs2_path *left_path = NULL;
  2018. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2019. *ret_left_path = NULL;
  2020. left_path = ocfs2_new_path_from_path(right_path);
  2021. if (!left_path) {
  2022. ret = -ENOMEM;
  2023. mlog_errno(ret);
  2024. goto out;
  2025. }
  2026. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2027. if (ret) {
  2028. mlog_errno(ret);
  2029. goto out;
  2030. }
  2031. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  2032. /*
  2033. * What we want to do here is:
  2034. *
  2035. * 1) Start with the rightmost path.
  2036. *
  2037. * 2) Determine a path to the leaf block directly to the left
  2038. * of that leaf.
  2039. *
  2040. * 3) Determine the 'subtree root' - the lowest level tree node
  2041. * which contains a path to both leaves.
  2042. *
  2043. * 4) Rotate the subtree.
  2044. *
  2045. * 5) Find the next subtree by considering the left path to be
  2046. * the new right path.
  2047. *
  2048. * The check at the top of this while loop also accepts
  2049. * insert_cpos == cpos because cpos is only a _theoretical_
  2050. * value to get us the left path - insert_cpos might very well
  2051. * be filling that hole.
  2052. *
  2053. * Stop at a cpos of '0' because we either started at the
  2054. * leftmost branch (i.e., a tree with one branch and a
  2055. * rotation inside of it), or we've gone as far as we can in
  2056. * rotating subtrees.
  2057. */
  2058. while (cpos && insert_cpos <= cpos) {
  2059. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  2060. insert_cpos, cpos);
  2061. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2062. if (ret) {
  2063. mlog_errno(ret);
  2064. goto out;
  2065. }
  2066. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  2067. path_leaf_bh(right_path),
  2068. "Owner %llu: error during insert of %u "
  2069. "(left path cpos %u) results in two identical "
  2070. "paths ending at %llu\n",
  2071. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2072. insert_cpos, cpos,
  2073. (unsigned long long)
  2074. path_leaf_bh(left_path)->b_blocknr);
  2075. if (split == SPLIT_NONE &&
  2076. ocfs2_rotate_requires_path_adjustment(left_path,
  2077. insert_cpos)) {
  2078. /*
  2079. * We've rotated the tree as much as we
  2080. * should. The rest is up to
  2081. * ocfs2_insert_path() to complete, after the
  2082. * record insertion. We indicate this
  2083. * situation by returning the left path.
  2084. *
  2085. * The reason we don't adjust the records here
  2086. * before the record insert is that an error
  2087. * later might break the rule where a parent
  2088. * record e_cpos will reflect the actual
  2089. * e_cpos of the 1st nonempty record of the
  2090. * child list.
  2091. */
  2092. *ret_left_path = left_path;
  2093. goto out_ret_path;
  2094. }
  2095. start = ocfs2_find_subtree_root(et, left_path, right_path);
  2096. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2097. start,
  2098. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  2099. right_path->p_tree_depth);
  2100. ret = ocfs2_extend_rotate_transaction(handle, start,
  2101. orig_credits, right_path);
  2102. if (ret) {
  2103. mlog_errno(ret);
  2104. goto out;
  2105. }
  2106. ret = ocfs2_rotate_subtree_right(handle, et, left_path,
  2107. right_path, start);
  2108. if (ret) {
  2109. mlog_errno(ret);
  2110. goto out;
  2111. }
  2112. if (split != SPLIT_NONE &&
  2113. ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
  2114. insert_cpos)) {
  2115. /*
  2116. * A rotate moves the rightmost left leaf
  2117. * record over to the leftmost right leaf
  2118. * slot. If we're doing an extent split
  2119. * instead of a real insert, then we have to
  2120. * check that the extent to be split wasn't
  2121. * just moved over. If it was, then we can
  2122. * exit here, passing left_path back -
  2123. * ocfs2_split_extent() is smart enough to
  2124. * search both leaves.
  2125. */
  2126. *ret_left_path = left_path;
  2127. goto out_ret_path;
  2128. }
  2129. /*
  2130. * There is no need to re-read the next right path
  2131. * as we know that it'll be our current left
  2132. * path. Optimize by copying values instead.
  2133. */
  2134. ocfs2_mv_path(right_path, left_path);
  2135. ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
  2136. if (ret) {
  2137. mlog_errno(ret);
  2138. goto out;
  2139. }
  2140. }
  2141. out:
  2142. ocfs2_free_path(left_path);
  2143. out_ret_path:
  2144. return ret;
  2145. }
  2146. static int ocfs2_update_edge_lengths(handle_t *handle,
  2147. struct ocfs2_extent_tree *et,
  2148. int subtree_index, struct ocfs2_path *path)
  2149. {
  2150. int i, idx, ret;
  2151. struct ocfs2_extent_rec *rec;
  2152. struct ocfs2_extent_list *el;
  2153. struct ocfs2_extent_block *eb;
  2154. u32 range;
  2155. /*
  2156. * In normal tree rotation process, we will never touch the
  2157. * tree branch above subtree_index and ocfs2_extend_rotate_transaction
  2158. * doesn't reserve the credits for them either.
  2159. *
  2160. * But we do have a special case here which will update the rightmost
  2161. * records for all the bh in the path.
  2162. * So we have to allocate extra credits and access them.
  2163. */
  2164. ret = ocfs2_extend_trans(handle,
  2165. handle->h_buffer_credits + subtree_index);
  2166. if (ret) {
  2167. mlog_errno(ret);
  2168. goto out;
  2169. }
  2170. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2171. if (ret) {
  2172. mlog_errno(ret);
  2173. goto out;
  2174. }
  2175. /* Path should always be rightmost. */
  2176. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2177. BUG_ON(eb->h_next_leaf_blk != 0ULL);
  2178. el = &eb->h_list;
  2179. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  2180. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2181. rec = &el->l_recs[idx];
  2182. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  2183. for (i = 0; i < path->p_tree_depth; i++) {
  2184. el = path->p_node[i].el;
  2185. idx = le16_to_cpu(el->l_next_free_rec) - 1;
  2186. rec = &el->l_recs[idx];
  2187. rec->e_int_clusters = cpu_to_le32(range);
  2188. le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
  2189. ocfs2_journal_dirty(handle, path->p_node[i].bh);
  2190. }
  2191. out:
  2192. return ret;
  2193. }
  2194. static void ocfs2_unlink_path(handle_t *handle,
  2195. struct ocfs2_extent_tree *et,
  2196. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2197. struct ocfs2_path *path, int unlink_start)
  2198. {
  2199. int ret, i;
  2200. struct ocfs2_extent_block *eb;
  2201. struct ocfs2_extent_list *el;
  2202. struct buffer_head *bh;
  2203. for(i = unlink_start; i < path_num_items(path); i++) {
  2204. bh = path->p_node[i].bh;
  2205. eb = (struct ocfs2_extent_block *)bh->b_data;
  2206. /*
  2207. * Not all nodes might have had their final count
  2208. * decremented by the caller - handle this here.
  2209. */
  2210. el = &eb->h_list;
  2211. if (le16_to_cpu(el->l_next_free_rec) > 1) {
  2212. mlog(ML_ERROR,
  2213. "Inode %llu, attempted to remove extent block "
  2214. "%llu with %u records\n",
  2215. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2216. (unsigned long long)le64_to_cpu(eb->h_blkno),
  2217. le16_to_cpu(el->l_next_free_rec));
  2218. ocfs2_journal_dirty(handle, bh);
  2219. ocfs2_remove_from_cache(et->et_ci, bh);
  2220. continue;
  2221. }
  2222. el->l_next_free_rec = 0;
  2223. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2224. ocfs2_journal_dirty(handle, bh);
  2225. ret = ocfs2_cache_extent_block_free(dealloc, eb);
  2226. if (ret)
  2227. mlog_errno(ret);
  2228. ocfs2_remove_from_cache(et->et_ci, bh);
  2229. }
  2230. }
  2231. static void ocfs2_unlink_subtree(handle_t *handle,
  2232. struct ocfs2_extent_tree *et,
  2233. struct ocfs2_path *left_path,
  2234. struct ocfs2_path *right_path,
  2235. int subtree_index,
  2236. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2237. {
  2238. int i;
  2239. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  2240. struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
  2241. struct ocfs2_extent_list *el;
  2242. struct ocfs2_extent_block *eb;
  2243. el = path_leaf_el(left_path);
  2244. eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
  2245. for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
  2246. if (root_el->l_recs[i].e_blkno == eb->h_blkno)
  2247. break;
  2248. BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
  2249. memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
  2250. le16_add_cpu(&root_el->l_next_free_rec, -1);
  2251. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2252. eb->h_next_leaf_blk = 0;
  2253. ocfs2_journal_dirty(handle, root_bh);
  2254. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2255. ocfs2_unlink_path(handle, et, dealloc, right_path,
  2256. subtree_index + 1);
  2257. }
  2258. static int ocfs2_rotate_subtree_left(handle_t *handle,
  2259. struct ocfs2_extent_tree *et,
  2260. struct ocfs2_path *left_path,
  2261. struct ocfs2_path *right_path,
  2262. int subtree_index,
  2263. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2264. int *deleted)
  2265. {
  2266. int ret, i, del_right_subtree = 0, right_has_empty = 0;
  2267. struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
  2268. struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
  2269. struct ocfs2_extent_block *eb;
  2270. *deleted = 0;
  2271. right_leaf_el = path_leaf_el(right_path);
  2272. left_leaf_el = path_leaf_el(left_path);
  2273. root_bh = left_path->p_node[subtree_index].bh;
  2274. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2275. if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
  2276. return 0;
  2277. eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
  2278. if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
  2279. /*
  2280. * It's legal for us to proceed if the right leaf is
  2281. * the rightmost one and it has an empty extent. There
  2282. * are two cases to handle - whether the leaf will be
  2283. * empty after removal or not. If the leaf isn't empty
  2284. * then just remove the empty extent up front. The
  2285. * next block will handle empty leaves by flagging
  2286. * them for unlink.
  2287. *
  2288. * Non rightmost leaves will throw -EAGAIN and the
  2289. * caller can manually move the subtree and retry.
  2290. */
  2291. if (eb->h_next_leaf_blk != 0ULL)
  2292. return -EAGAIN;
  2293. if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
  2294. ret = ocfs2_journal_access_eb(handle, et->et_ci,
  2295. path_leaf_bh(right_path),
  2296. OCFS2_JOURNAL_ACCESS_WRITE);
  2297. if (ret) {
  2298. mlog_errno(ret);
  2299. goto out;
  2300. }
  2301. ocfs2_remove_empty_extent(right_leaf_el);
  2302. } else
  2303. right_has_empty = 1;
  2304. }
  2305. if (eb->h_next_leaf_blk == 0ULL &&
  2306. le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
  2307. /*
  2308. * We have to update i_last_eb_blk during the meta
  2309. * data delete.
  2310. */
  2311. ret = ocfs2_et_root_journal_access(handle, et,
  2312. OCFS2_JOURNAL_ACCESS_WRITE);
  2313. if (ret) {
  2314. mlog_errno(ret);
  2315. goto out;
  2316. }
  2317. del_right_subtree = 1;
  2318. }
  2319. /*
  2320. * Getting here with an empty extent in the right path implies
  2321. * that it's the rightmost path and will be deleted.
  2322. */
  2323. BUG_ON(right_has_empty && !del_right_subtree);
  2324. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2325. subtree_index);
  2326. if (ret) {
  2327. mlog_errno(ret);
  2328. goto out;
  2329. }
  2330. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  2331. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2332. right_path, i);
  2333. if (ret) {
  2334. mlog_errno(ret);
  2335. goto out;
  2336. }
  2337. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2338. left_path, i);
  2339. if (ret) {
  2340. mlog_errno(ret);
  2341. goto out;
  2342. }
  2343. }
  2344. if (!right_has_empty) {
  2345. /*
  2346. * Only do this if we're moving a real
  2347. * record. Otherwise, the action is delayed until
  2348. * after removal of the right path in which case we
  2349. * can do a simple shift to remove the empty extent.
  2350. */
  2351. ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
  2352. memset(&right_leaf_el->l_recs[0], 0,
  2353. sizeof(struct ocfs2_extent_rec));
  2354. }
  2355. if (eb->h_next_leaf_blk == 0ULL) {
  2356. /*
  2357. * Move recs over to get rid of empty extent, decrease
  2358. * next_free. This is allowed to remove the last
  2359. * extent in our leaf (setting l_next_free_rec to
  2360. * zero) - the delete code below won't care.
  2361. */
  2362. ocfs2_remove_empty_extent(right_leaf_el);
  2363. }
  2364. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  2365. ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2366. if (del_right_subtree) {
  2367. ocfs2_unlink_subtree(handle, et, left_path, right_path,
  2368. subtree_index, dealloc);
  2369. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2370. left_path);
  2371. if (ret) {
  2372. mlog_errno(ret);
  2373. goto out;
  2374. }
  2375. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2376. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2377. /*
  2378. * Removal of the extent in the left leaf was skipped
  2379. * above so we could delete the right path
  2380. * 1st.
  2381. */
  2382. if (right_has_empty)
  2383. ocfs2_remove_empty_extent(left_leaf_el);
  2384. ocfs2_journal_dirty(handle, et_root_bh);
  2385. *deleted = 1;
  2386. } else
  2387. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2388. subtree_index);
  2389. out:
  2390. return ret;
  2391. }
  2392. /*
  2393. * Given a full path, determine what cpos value would return us a path
  2394. * containing the leaf immediately to the right of the current one.
  2395. *
  2396. * Will return zero if the path passed in is already the rightmost path.
  2397. *
  2398. * This looks similar, but is subtly different to
  2399. * ocfs2_find_cpos_for_left_leaf().
  2400. */
  2401. int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
  2402. struct ocfs2_path *path, u32 *cpos)
  2403. {
  2404. int i, j, ret = 0;
  2405. u64 blkno;
  2406. struct ocfs2_extent_list *el;
  2407. *cpos = 0;
  2408. if (path->p_tree_depth == 0)
  2409. return 0;
  2410. blkno = path_leaf_bh(path)->b_blocknr;
  2411. /* Start at the tree node just above the leaf and work our way up. */
  2412. i = path->p_tree_depth - 1;
  2413. while (i >= 0) {
  2414. int next_free;
  2415. el = path->p_node[i].el;
  2416. /*
  2417. * Find the extent record just after the one in our
  2418. * path.
  2419. */
  2420. next_free = le16_to_cpu(el->l_next_free_rec);
  2421. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  2422. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  2423. if (j == (next_free - 1)) {
  2424. if (i == 0) {
  2425. /*
  2426. * We've determined that the
  2427. * path specified is already
  2428. * the rightmost one - return a
  2429. * cpos of zero.
  2430. */
  2431. goto out;
  2432. }
  2433. /*
  2434. * The rightmost record points to our
  2435. * leaf - we need to travel up the
  2436. * tree one level.
  2437. */
  2438. goto next_node;
  2439. }
  2440. *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
  2441. goto out;
  2442. }
  2443. }
  2444. /*
  2445. * If we got here, we never found a valid node where
  2446. * the tree indicated one should be.
  2447. */
  2448. ocfs2_error(sb,
  2449. "Invalid extent tree at extent block %llu\n",
  2450. (unsigned long long)blkno);
  2451. ret = -EROFS;
  2452. goto out;
  2453. next_node:
  2454. blkno = path->p_node[i].bh->b_blocknr;
  2455. i--;
  2456. }
  2457. out:
  2458. return ret;
  2459. }
  2460. static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
  2461. struct ocfs2_extent_tree *et,
  2462. struct ocfs2_path *path)
  2463. {
  2464. int ret;
  2465. struct buffer_head *bh = path_leaf_bh(path);
  2466. struct ocfs2_extent_list *el = path_leaf_el(path);
  2467. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2468. return 0;
  2469. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  2470. path_num_items(path) - 1);
  2471. if (ret) {
  2472. mlog_errno(ret);
  2473. goto out;
  2474. }
  2475. ocfs2_remove_empty_extent(el);
  2476. ocfs2_journal_dirty(handle, bh);
  2477. out:
  2478. return ret;
  2479. }
  2480. static int __ocfs2_rotate_tree_left(handle_t *handle,
  2481. struct ocfs2_extent_tree *et,
  2482. int orig_credits,
  2483. struct ocfs2_path *path,
  2484. struct ocfs2_cached_dealloc_ctxt *dealloc,
  2485. struct ocfs2_path **empty_extent_path)
  2486. {
  2487. int ret, subtree_root, deleted;
  2488. u32 right_cpos;
  2489. struct ocfs2_path *left_path = NULL;
  2490. struct ocfs2_path *right_path = NULL;
  2491. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  2492. BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
  2493. *empty_extent_path = NULL;
  2494. ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  2495. if (ret) {
  2496. mlog_errno(ret);
  2497. goto out;
  2498. }
  2499. left_path = ocfs2_new_path_from_path(path);
  2500. if (!left_path) {
  2501. ret = -ENOMEM;
  2502. mlog_errno(ret);
  2503. goto out;
  2504. }
  2505. ocfs2_cp_path(left_path, path);
  2506. right_path = ocfs2_new_path_from_path(path);
  2507. if (!right_path) {
  2508. ret = -ENOMEM;
  2509. mlog_errno(ret);
  2510. goto out;
  2511. }
  2512. while (right_cpos) {
  2513. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2514. if (ret) {
  2515. mlog_errno(ret);
  2516. goto out;
  2517. }
  2518. subtree_root = ocfs2_find_subtree_root(et, left_path,
  2519. right_path);
  2520. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  2521. subtree_root,
  2522. (unsigned long long)
  2523. right_path->p_node[subtree_root].bh->b_blocknr,
  2524. right_path->p_tree_depth);
  2525. ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
  2526. orig_credits, left_path);
  2527. if (ret) {
  2528. mlog_errno(ret);
  2529. goto out;
  2530. }
  2531. /*
  2532. * Caller might still want to make changes to the
  2533. * tree root, so re-add it to the journal here.
  2534. */
  2535. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2536. left_path, 0);
  2537. if (ret) {
  2538. mlog_errno(ret);
  2539. goto out;
  2540. }
  2541. ret = ocfs2_rotate_subtree_left(handle, et, left_path,
  2542. right_path, subtree_root,
  2543. dealloc, &deleted);
  2544. if (ret == -EAGAIN) {
  2545. /*
  2546. * The rotation has to temporarily stop due to
  2547. * the right subtree having an empty
  2548. * extent. Pass it back to the caller for a
  2549. * fixup.
  2550. */
  2551. *empty_extent_path = right_path;
  2552. right_path = NULL;
  2553. goto out;
  2554. }
  2555. if (ret) {
  2556. mlog_errno(ret);
  2557. goto out;
  2558. }
  2559. /*
  2560. * The subtree rotate might have removed records on
  2561. * the rightmost edge. If so, then rotation is
  2562. * complete.
  2563. */
  2564. if (deleted)
  2565. break;
  2566. ocfs2_mv_path(left_path, right_path);
  2567. ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
  2568. &right_cpos);
  2569. if (ret) {
  2570. mlog_errno(ret);
  2571. goto out;
  2572. }
  2573. }
  2574. out:
  2575. ocfs2_free_path(right_path);
  2576. ocfs2_free_path(left_path);
  2577. return ret;
  2578. }
  2579. static int ocfs2_remove_rightmost_path(handle_t *handle,
  2580. struct ocfs2_extent_tree *et,
  2581. struct ocfs2_path *path,
  2582. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2583. {
  2584. int ret, subtree_index;
  2585. u32 cpos;
  2586. struct ocfs2_path *left_path = NULL;
  2587. struct ocfs2_extent_block *eb;
  2588. struct ocfs2_extent_list *el;
  2589. ret = ocfs2_et_sanity_check(et);
  2590. if (ret)
  2591. goto out;
  2592. /*
  2593. * There's two ways we handle this depending on
  2594. * whether path is the only existing one.
  2595. */
  2596. ret = ocfs2_extend_rotate_transaction(handle, 0,
  2597. handle->h_buffer_credits,
  2598. path);
  2599. if (ret) {
  2600. mlog_errno(ret);
  2601. goto out;
  2602. }
  2603. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  2604. if (ret) {
  2605. mlog_errno(ret);
  2606. goto out;
  2607. }
  2608. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2609. path, &cpos);
  2610. if (ret) {
  2611. mlog_errno(ret);
  2612. goto out;
  2613. }
  2614. if (cpos) {
  2615. /*
  2616. * We have a path to the left of this one - it needs
  2617. * an update too.
  2618. */
  2619. left_path = ocfs2_new_path_from_path(path);
  2620. if (!left_path) {
  2621. ret = -ENOMEM;
  2622. mlog_errno(ret);
  2623. goto out;
  2624. }
  2625. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  2626. if (ret) {
  2627. mlog_errno(ret);
  2628. goto out;
  2629. }
  2630. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  2631. if (ret) {
  2632. mlog_errno(ret);
  2633. goto out;
  2634. }
  2635. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  2636. ocfs2_unlink_subtree(handle, et, left_path, path,
  2637. subtree_index, dealloc);
  2638. ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
  2639. left_path);
  2640. if (ret) {
  2641. mlog_errno(ret);
  2642. goto out;
  2643. }
  2644. eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
  2645. ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
  2646. } else {
  2647. /*
  2648. * 'path' is also the leftmost path which
  2649. * means it must be the only one. This gets
  2650. * handled differently because we want to
  2651. * revert the root back to having extents
  2652. * in-line.
  2653. */
  2654. ocfs2_unlink_path(handle, et, dealloc, path, 1);
  2655. el = et->et_root_el;
  2656. el->l_tree_depth = 0;
  2657. el->l_next_free_rec = 0;
  2658. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2659. ocfs2_et_set_last_eb_blk(et, 0);
  2660. }
  2661. ocfs2_journal_dirty(handle, path_root_bh(path));
  2662. out:
  2663. ocfs2_free_path(left_path);
  2664. return ret;
  2665. }
  2666. /*
  2667. * Left rotation of btree records.
  2668. *
  2669. * In many ways, this is (unsurprisingly) the opposite of right
  2670. * rotation. We start at some non-rightmost path containing an empty
  2671. * extent in the leaf block. The code works its way to the rightmost
  2672. * path by rotating records to the left in every subtree.
  2673. *
  2674. * This is used by any code which reduces the number of extent records
  2675. * in a leaf. After removal, an empty record should be placed in the
  2676. * leftmost list position.
  2677. *
  2678. * This won't handle a length update of the rightmost path records if
  2679. * the rightmost tree leaf record is removed so the caller is
  2680. * responsible for detecting and correcting that.
  2681. */
  2682. static int ocfs2_rotate_tree_left(handle_t *handle,
  2683. struct ocfs2_extent_tree *et,
  2684. struct ocfs2_path *path,
  2685. struct ocfs2_cached_dealloc_ctxt *dealloc)
  2686. {
  2687. int ret, orig_credits = handle->h_buffer_credits;
  2688. struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
  2689. struct ocfs2_extent_block *eb;
  2690. struct ocfs2_extent_list *el;
  2691. el = path_leaf_el(path);
  2692. if (!ocfs2_is_empty_extent(&el->l_recs[0]))
  2693. return 0;
  2694. if (path->p_tree_depth == 0) {
  2695. rightmost_no_delete:
  2696. /*
  2697. * Inline extents. This is trivially handled, so do
  2698. * it up front.
  2699. */
  2700. ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
  2701. if (ret)
  2702. mlog_errno(ret);
  2703. goto out;
  2704. }
  2705. /*
  2706. * Handle rightmost branch now. There's several cases:
  2707. * 1) simple rotation leaving records in there. That's trivial.
  2708. * 2) rotation requiring a branch delete - there's no more
  2709. * records left. Two cases of this:
  2710. * a) There are branches to the left.
  2711. * b) This is also the leftmost (the only) branch.
  2712. *
  2713. * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
  2714. * 2a) we need the left branch so that we can update it with the unlink
  2715. * 2b) we need to bring the root back to inline extents.
  2716. */
  2717. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  2718. el = &eb->h_list;
  2719. if (eb->h_next_leaf_blk == 0) {
  2720. /*
  2721. * This gets a bit tricky if we're going to delete the
  2722. * rightmost path. Get the other cases out of the way
  2723. * 1st.
  2724. */
  2725. if (le16_to_cpu(el->l_next_free_rec) > 1)
  2726. goto rightmost_no_delete;
  2727. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  2728. ret = -EIO;
  2729. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  2730. "Owner %llu has empty extent block at %llu",
  2731. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  2732. (unsigned long long)le64_to_cpu(eb->h_blkno));
  2733. goto out;
  2734. }
  2735. /*
  2736. * XXX: The caller can not trust "path" any more after
  2737. * this as it will have been deleted. What do we do?
  2738. *
  2739. * In theory the rotate-for-merge code will never get
  2740. * here because it'll always ask for a rotate in a
  2741. * nonempty list.
  2742. */
  2743. ret = ocfs2_remove_rightmost_path(handle, et, path,
  2744. dealloc);
  2745. if (ret)
  2746. mlog_errno(ret);
  2747. goto out;
  2748. }
  2749. /*
  2750. * Now we can loop, remembering the path we get from -EAGAIN
  2751. * and restarting from there.
  2752. */
  2753. try_rotate:
  2754. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
  2755. dealloc, &restart_path);
  2756. if (ret && ret != -EAGAIN) {
  2757. mlog_errno(ret);
  2758. goto out;
  2759. }
  2760. while (ret == -EAGAIN) {
  2761. tmp_path = restart_path;
  2762. restart_path = NULL;
  2763. ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
  2764. tmp_path, dealloc,
  2765. &restart_path);
  2766. if (ret && ret != -EAGAIN) {
  2767. mlog_errno(ret);
  2768. goto out;
  2769. }
  2770. ocfs2_free_path(tmp_path);
  2771. tmp_path = NULL;
  2772. if (ret == 0)
  2773. goto try_rotate;
  2774. }
  2775. out:
  2776. ocfs2_free_path(tmp_path);
  2777. ocfs2_free_path(restart_path);
  2778. return ret;
  2779. }
  2780. static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
  2781. int index)
  2782. {
  2783. struct ocfs2_extent_rec *rec = &el->l_recs[index];
  2784. unsigned int size;
  2785. if (rec->e_leaf_clusters == 0) {
  2786. /*
  2787. * We consumed all of the merged-from record. An empty
  2788. * extent cannot exist anywhere but the 1st array
  2789. * position, so move things over if the merged-from
  2790. * record doesn't occupy that position.
  2791. *
  2792. * This creates a new empty extent so the caller
  2793. * should be smart enough to have removed any existing
  2794. * ones.
  2795. */
  2796. if (index > 0) {
  2797. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  2798. size = index * sizeof(struct ocfs2_extent_rec);
  2799. memmove(&el->l_recs[1], &el->l_recs[0], size);
  2800. }
  2801. /*
  2802. * Always memset - the caller doesn't check whether it
  2803. * created an empty extent, so there could be junk in
  2804. * the other fields.
  2805. */
  2806. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  2807. }
  2808. }
  2809. static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
  2810. struct ocfs2_path *left_path,
  2811. struct ocfs2_path **ret_right_path)
  2812. {
  2813. int ret;
  2814. u32 right_cpos;
  2815. struct ocfs2_path *right_path = NULL;
  2816. struct ocfs2_extent_list *left_el;
  2817. *ret_right_path = NULL;
  2818. /* This function shouldn't be called for non-trees. */
  2819. BUG_ON(left_path->p_tree_depth == 0);
  2820. left_el = path_leaf_el(left_path);
  2821. BUG_ON(left_el->l_next_free_rec != left_el->l_count);
  2822. ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2823. left_path, &right_cpos);
  2824. if (ret) {
  2825. mlog_errno(ret);
  2826. goto out;
  2827. }
  2828. /* This function shouldn't be called for the rightmost leaf. */
  2829. BUG_ON(right_cpos == 0);
  2830. right_path = ocfs2_new_path_from_path(left_path);
  2831. if (!right_path) {
  2832. ret = -ENOMEM;
  2833. mlog_errno(ret);
  2834. goto out;
  2835. }
  2836. ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  2837. if (ret) {
  2838. mlog_errno(ret);
  2839. goto out;
  2840. }
  2841. *ret_right_path = right_path;
  2842. out:
  2843. if (ret)
  2844. ocfs2_free_path(right_path);
  2845. return ret;
  2846. }
  2847. /*
  2848. * Remove split_rec clusters from the record at index and merge them
  2849. * onto the beginning of the record "next" to it.
  2850. * For index < l_count - 1, the next means the extent rec at index + 1.
  2851. * For index == l_count - 1, the "next" means the 1st extent rec of the
  2852. * next extent block.
  2853. */
  2854. static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
  2855. handle_t *handle,
  2856. struct ocfs2_extent_tree *et,
  2857. struct ocfs2_extent_rec *split_rec,
  2858. int index)
  2859. {
  2860. int ret, next_free, i;
  2861. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  2862. struct ocfs2_extent_rec *left_rec;
  2863. struct ocfs2_extent_rec *right_rec;
  2864. struct ocfs2_extent_list *right_el;
  2865. struct ocfs2_path *right_path = NULL;
  2866. int subtree_index = 0;
  2867. struct ocfs2_extent_list *el = path_leaf_el(left_path);
  2868. struct buffer_head *bh = path_leaf_bh(left_path);
  2869. struct buffer_head *root_bh = NULL;
  2870. BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
  2871. left_rec = &el->l_recs[index];
  2872. if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
  2873. le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
  2874. /* we meet with a cross extent block merge. */
  2875. ret = ocfs2_get_right_path(et, left_path, &right_path);
  2876. if (ret) {
  2877. mlog_errno(ret);
  2878. goto out;
  2879. }
  2880. right_el = path_leaf_el(right_path);
  2881. next_free = le16_to_cpu(right_el->l_next_free_rec);
  2882. BUG_ON(next_free <= 0);
  2883. right_rec = &right_el->l_recs[0];
  2884. if (ocfs2_is_empty_extent(right_rec)) {
  2885. BUG_ON(next_free <= 1);
  2886. right_rec = &right_el->l_recs[1];
  2887. }
  2888. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  2889. le16_to_cpu(left_rec->e_leaf_clusters) !=
  2890. le32_to_cpu(right_rec->e_cpos));
  2891. subtree_index = ocfs2_find_subtree_root(et, left_path,
  2892. right_path);
  2893. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  2894. handle->h_buffer_credits,
  2895. right_path);
  2896. if (ret) {
  2897. mlog_errno(ret);
  2898. goto out;
  2899. }
  2900. root_bh = left_path->p_node[subtree_index].bh;
  2901. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  2902. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  2903. subtree_index);
  2904. if (ret) {
  2905. mlog_errno(ret);
  2906. goto out;
  2907. }
  2908. for (i = subtree_index + 1;
  2909. i < path_num_items(right_path); i++) {
  2910. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2911. right_path, i);
  2912. if (ret) {
  2913. mlog_errno(ret);
  2914. goto out;
  2915. }
  2916. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  2917. left_path, i);
  2918. if (ret) {
  2919. mlog_errno(ret);
  2920. goto out;
  2921. }
  2922. }
  2923. } else {
  2924. BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
  2925. right_rec = &el->l_recs[index + 1];
  2926. }
  2927. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
  2928. path_num_items(left_path) - 1);
  2929. if (ret) {
  2930. mlog_errno(ret);
  2931. goto out;
  2932. }
  2933. le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
  2934. le32_add_cpu(&right_rec->e_cpos, -split_clusters);
  2935. le64_add_cpu(&right_rec->e_blkno,
  2936. -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  2937. split_clusters));
  2938. le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
  2939. ocfs2_cleanup_merge(el, index);
  2940. ocfs2_journal_dirty(handle, bh);
  2941. if (right_path) {
  2942. ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
  2943. ocfs2_complete_edge_insert(handle, left_path, right_path,
  2944. subtree_index);
  2945. }
  2946. out:
  2947. if (right_path)
  2948. ocfs2_free_path(right_path);
  2949. return ret;
  2950. }
  2951. static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
  2952. struct ocfs2_path *right_path,
  2953. struct ocfs2_path **ret_left_path)
  2954. {
  2955. int ret;
  2956. u32 left_cpos;
  2957. struct ocfs2_path *left_path = NULL;
  2958. *ret_left_path = NULL;
  2959. /* This function shouldn't be called for non-trees. */
  2960. BUG_ON(right_path->p_tree_depth == 0);
  2961. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  2962. right_path, &left_cpos);
  2963. if (ret) {
  2964. mlog_errno(ret);
  2965. goto out;
  2966. }
  2967. /* This function shouldn't be called for the leftmost leaf. */
  2968. BUG_ON(left_cpos == 0);
  2969. left_path = ocfs2_new_path_from_path(right_path);
  2970. if (!left_path) {
  2971. ret = -ENOMEM;
  2972. mlog_errno(ret);
  2973. goto out;
  2974. }
  2975. ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
  2976. if (ret) {
  2977. mlog_errno(ret);
  2978. goto out;
  2979. }
  2980. *ret_left_path = left_path;
  2981. out:
  2982. if (ret)
  2983. ocfs2_free_path(left_path);
  2984. return ret;
  2985. }
  2986. /*
  2987. * Remove split_rec clusters from the record at index and merge them
  2988. * onto the tail of the record "before" it.
  2989. * For index > 0, the "before" means the extent rec at index - 1.
  2990. *
  2991. * For index == 0, the "before" means the last record of the previous
  2992. * extent block. And there is also a situation that we may need to
  2993. * remove the rightmost leaf extent block in the right_path and change
  2994. * the right path to indicate the new rightmost path.
  2995. */
  2996. static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
  2997. handle_t *handle,
  2998. struct ocfs2_extent_tree *et,
  2999. struct ocfs2_extent_rec *split_rec,
  3000. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3001. int index)
  3002. {
  3003. int ret, i, subtree_index = 0, has_empty_extent = 0;
  3004. unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
  3005. struct ocfs2_extent_rec *left_rec;
  3006. struct ocfs2_extent_rec *right_rec;
  3007. struct ocfs2_extent_list *el = path_leaf_el(right_path);
  3008. struct buffer_head *bh = path_leaf_bh(right_path);
  3009. struct buffer_head *root_bh = NULL;
  3010. struct ocfs2_path *left_path = NULL;
  3011. struct ocfs2_extent_list *left_el;
  3012. BUG_ON(index < 0);
  3013. right_rec = &el->l_recs[index];
  3014. if (index == 0) {
  3015. /* we meet with a cross extent block merge. */
  3016. ret = ocfs2_get_left_path(et, right_path, &left_path);
  3017. if (ret) {
  3018. mlog_errno(ret);
  3019. goto out;
  3020. }
  3021. left_el = path_leaf_el(left_path);
  3022. BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
  3023. le16_to_cpu(left_el->l_count));
  3024. left_rec = &left_el->l_recs[
  3025. le16_to_cpu(left_el->l_next_free_rec) - 1];
  3026. BUG_ON(le32_to_cpu(left_rec->e_cpos) +
  3027. le16_to_cpu(left_rec->e_leaf_clusters) !=
  3028. le32_to_cpu(split_rec->e_cpos));
  3029. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3030. right_path);
  3031. ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
  3032. handle->h_buffer_credits,
  3033. left_path);
  3034. if (ret) {
  3035. mlog_errno(ret);
  3036. goto out;
  3037. }
  3038. root_bh = left_path->p_node[subtree_index].bh;
  3039. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  3040. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3041. subtree_index);
  3042. if (ret) {
  3043. mlog_errno(ret);
  3044. goto out;
  3045. }
  3046. for (i = subtree_index + 1;
  3047. i < path_num_items(right_path); i++) {
  3048. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3049. right_path, i);
  3050. if (ret) {
  3051. mlog_errno(ret);
  3052. goto out;
  3053. }
  3054. ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
  3055. left_path, i);
  3056. if (ret) {
  3057. mlog_errno(ret);
  3058. goto out;
  3059. }
  3060. }
  3061. } else {
  3062. left_rec = &el->l_recs[index - 1];
  3063. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  3064. has_empty_extent = 1;
  3065. }
  3066. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
  3067. path_num_items(right_path) - 1);
  3068. if (ret) {
  3069. mlog_errno(ret);
  3070. goto out;
  3071. }
  3072. if (has_empty_extent && index == 1) {
  3073. /*
  3074. * The easy case - we can just plop the record right in.
  3075. */
  3076. *left_rec = *split_rec;
  3077. has_empty_extent = 0;
  3078. } else
  3079. le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
  3080. le32_add_cpu(&right_rec->e_cpos, split_clusters);
  3081. le64_add_cpu(&right_rec->e_blkno,
  3082. ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
  3083. split_clusters));
  3084. le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
  3085. ocfs2_cleanup_merge(el, index);
  3086. ocfs2_journal_dirty(handle, bh);
  3087. if (left_path) {
  3088. ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
  3089. /*
  3090. * In the situation that the right_rec is empty and the extent
  3091. * block is empty also, ocfs2_complete_edge_insert can't handle
  3092. * it and we need to delete the right extent block.
  3093. */
  3094. if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
  3095. le16_to_cpu(el->l_next_free_rec) == 1) {
  3096. ret = ocfs2_remove_rightmost_path(handle, et,
  3097. right_path,
  3098. dealloc);
  3099. if (ret) {
  3100. mlog_errno(ret);
  3101. goto out;
  3102. }
  3103. /* Now the rightmost extent block has been deleted.
  3104. * So we use the new rightmost path.
  3105. */
  3106. ocfs2_mv_path(right_path, left_path);
  3107. left_path = NULL;
  3108. } else
  3109. ocfs2_complete_edge_insert(handle, left_path,
  3110. right_path, subtree_index);
  3111. }
  3112. out:
  3113. if (left_path)
  3114. ocfs2_free_path(left_path);
  3115. return ret;
  3116. }
  3117. static int ocfs2_try_to_merge_extent(handle_t *handle,
  3118. struct ocfs2_extent_tree *et,
  3119. struct ocfs2_path *path,
  3120. int split_index,
  3121. struct ocfs2_extent_rec *split_rec,
  3122. struct ocfs2_cached_dealloc_ctxt *dealloc,
  3123. struct ocfs2_merge_ctxt *ctxt)
  3124. {
  3125. int ret = 0;
  3126. struct ocfs2_extent_list *el = path_leaf_el(path);
  3127. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  3128. BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
  3129. if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
  3130. /*
  3131. * The merge code will need to create an empty
  3132. * extent to take the place of the newly
  3133. * emptied slot. Remove any pre-existing empty
  3134. * extents - having more than one in a leaf is
  3135. * illegal.
  3136. */
  3137. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3138. if (ret) {
  3139. mlog_errno(ret);
  3140. goto out;
  3141. }
  3142. split_index--;
  3143. rec = &el->l_recs[split_index];
  3144. }
  3145. if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
  3146. /*
  3147. * Left-right contig implies this.
  3148. */
  3149. BUG_ON(!ctxt->c_split_covers_rec);
  3150. /*
  3151. * Since the leftright insert always covers the entire
  3152. * extent, this call will delete the insert record
  3153. * entirely, resulting in an empty extent record added to
  3154. * the extent block.
  3155. *
  3156. * Since the adding of an empty extent shifts
  3157. * everything back to the right, there's no need to
  3158. * update split_index here.
  3159. *
  3160. * When the split_index is zero, we need to merge it to the
  3161. * prevoius extent block. It is more efficient and easier
  3162. * if we do merge_right first and merge_left later.
  3163. */
  3164. ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
  3165. split_index);
  3166. if (ret) {
  3167. mlog_errno(ret);
  3168. goto out;
  3169. }
  3170. /*
  3171. * We can only get this from logic error above.
  3172. */
  3173. BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
  3174. /* The merge left us with an empty extent, remove it. */
  3175. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3176. if (ret) {
  3177. mlog_errno(ret);
  3178. goto out;
  3179. }
  3180. rec = &el->l_recs[split_index];
  3181. /*
  3182. * Note that we don't pass split_rec here on purpose -
  3183. * we've merged it into the rec already.
  3184. */
  3185. ret = ocfs2_merge_rec_left(path, handle, et, rec,
  3186. dealloc, split_index);
  3187. if (ret) {
  3188. mlog_errno(ret);
  3189. goto out;
  3190. }
  3191. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  3192. /*
  3193. * Error from this last rotate is not critical, so
  3194. * print but don't bubble it up.
  3195. */
  3196. if (ret)
  3197. mlog_errno(ret);
  3198. ret = 0;
  3199. } else {
  3200. /*
  3201. * Merge a record to the left or right.
  3202. *
  3203. * 'contig_type' is relative to the existing record,
  3204. * so for example, if we're "right contig", it's to
  3205. * the record on the left (hence the left merge).
  3206. */
  3207. if (ctxt->c_contig_type == CONTIG_RIGHT) {
  3208. ret = ocfs2_merge_rec_left(path, handle, et,
  3209. split_rec, dealloc,
  3210. split_index);
  3211. if (ret) {
  3212. mlog_errno(ret);
  3213. goto out;
  3214. }
  3215. } else {
  3216. ret = ocfs2_merge_rec_right(path, handle,
  3217. et, split_rec,
  3218. split_index);
  3219. if (ret) {
  3220. mlog_errno(ret);
  3221. goto out;
  3222. }
  3223. }
  3224. if (ctxt->c_split_covers_rec) {
  3225. /*
  3226. * The merge may have left an empty extent in
  3227. * our leaf. Try to rotate it away.
  3228. */
  3229. ret = ocfs2_rotate_tree_left(handle, et, path,
  3230. dealloc);
  3231. if (ret)
  3232. mlog_errno(ret);
  3233. ret = 0;
  3234. }
  3235. }
  3236. out:
  3237. return ret;
  3238. }
  3239. static void ocfs2_subtract_from_rec(struct super_block *sb,
  3240. enum ocfs2_split_type split,
  3241. struct ocfs2_extent_rec *rec,
  3242. struct ocfs2_extent_rec *split_rec)
  3243. {
  3244. u64 len_blocks;
  3245. len_blocks = ocfs2_clusters_to_blocks(sb,
  3246. le16_to_cpu(split_rec->e_leaf_clusters));
  3247. if (split == SPLIT_LEFT) {
  3248. /*
  3249. * Region is on the left edge of the existing
  3250. * record.
  3251. */
  3252. le32_add_cpu(&rec->e_cpos,
  3253. le16_to_cpu(split_rec->e_leaf_clusters));
  3254. le64_add_cpu(&rec->e_blkno, len_blocks);
  3255. le16_add_cpu(&rec->e_leaf_clusters,
  3256. -le16_to_cpu(split_rec->e_leaf_clusters));
  3257. } else {
  3258. /*
  3259. * Region is on the right edge of the existing
  3260. * record.
  3261. */
  3262. le16_add_cpu(&rec->e_leaf_clusters,
  3263. -le16_to_cpu(split_rec->e_leaf_clusters));
  3264. }
  3265. }
  3266. /*
  3267. * Do the final bits of extent record insertion at the target leaf
  3268. * list. If this leaf is part of an allocation tree, it is assumed
  3269. * that the tree above has been prepared.
  3270. */
  3271. static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
  3272. struct ocfs2_extent_rec *insert_rec,
  3273. struct ocfs2_extent_list *el,
  3274. struct ocfs2_insert_type *insert)
  3275. {
  3276. int i = insert->ins_contig_index;
  3277. unsigned int range;
  3278. struct ocfs2_extent_rec *rec;
  3279. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3280. if (insert->ins_split != SPLIT_NONE) {
  3281. i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
  3282. BUG_ON(i == -1);
  3283. rec = &el->l_recs[i];
  3284. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3285. insert->ins_split, rec,
  3286. insert_rec);
  3287. goto rotate;
  3288. }
  3289. /*
  3290. * Contiguous insert - either left or right.
  3291. */
  3292. if (insert->ins_contig != CONTIG_NONE) {
  3293. rec = &el->l_recs[i];
  3294. if (insert->ins_contig == CONTIG_LEFT) {
  3295. rec->e_blkno = insert_rec->e_blkno;
  3296. rec->e_cpos = insert_rec->e_cpos;
  3297. }
  3298. le16_add_cpu(&rec->e_leaf_clusters,
  3299. le16_to_cpu(insert_rec->e_leaf_clusters));
  3300. return;
  3301. }
  3302. /*
  3303. * Handle insert into an empty leaf.
  3304. */
  3305. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  3306. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  3307. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3308. el->l_recs[0] = *insert_rec;
  3309. el->l_next_free_rec = cpu_to_le16(1);
  3310. return;
  3311. }
  3312. /*
  3313. * Appending insert.
  3314. */
  3315. if (insert->ins_appending == APPEND_TAIL) {
  3316. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3317. rec = &el->l_recs[i];
  3318. range = le32_to_cpu(rec->e_cpos)
  3319. + le16_to_cpu(rec->e_leaf_clusters);
  3320. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  3321. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  3322. le16_to_cpu(el->l_count),
  3323. "owner %llu, depth %u, count %u, next free %u, "
  3324. "rec.cpos %u, rec.clusters %u, "
  3325. "insert.cpos %u, insert.clusters %u\n",
  3326. ocfs2_metadata_cache_owner(et->et_ci),
  3327. le16_to_cpu(el->l_tree_depth),
  3328. le16_to_cpu(el->l_count),
  3329. le16_to_cpu(el->l_next_free_rec),
  3330. le32_to_cpu(el->l_recs[i].e_cpos),
  3331. le16_to_cpu(el->l_recs[i].e_leaf_clusters),
  3332. le32_to_cpu(insert_rec->e_cpos),
  3333. le16_to_cpu(insert_rec->e_leaf_clusters));
  3334. i++;
  3335. el->l_recs[i] = *insert_rec;
  3336. le16_add_cpu(&el->l_next_free_rec, 1);
  3337. return;
  3338. }
  3339. rotate:
  3340. /*
  3341. * Ok, we have to rotate.
  3342. *
  3343. * At this point, it is safe to assume that inserting into an
  3344. * empty leaf and appending to a leaf have both been handled
  3345. * above.
  3346. *
  3347. * This leaf needs to have space, either by the empty 1st
  3348. * extent record, or by virtue of an l_next_rec < l_count.
  3349. */
  3350. ocfs2_rotate_leaf(el, insert_rec);
  3351. }
  3352. static void ocfs2_adjust_rightmost_records(handle_t *handle,
  3353. struct ocfs2_extent_tree *et,
  3354. struct ocfs2_path *path,
  3355. struct ocfs2_extent_rec *insert_rec)
  3356. {
  3357. int ret, i, next_free;
  3358. struct buffer_head *bh;
  3359. struct ocfs2_extent_list *el;
  3360. struct ocfs2_extent_rec *rec;
  3361. /*
  3362. * Update everything except the leaf block.
  3363. */
  3364. for (i = 0; i < path->p_tree_depth; i++) {
  3365. bh = path->p_node[i].bh;
  3366. el = path->p_node[i].el;
  3367. next_free = le16_to_cpu(el->l_next_free_rec);
  3368. if (next_free == 0) {
  3369. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  3370. "Owner %llu has a bad extent list",
  3371. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  3372. ret = -EIO;
  3373. return;
  3374. }
  3375. rec = &el->l_recs[next_free - 1];
  3376. rec->e_int_clusters = insert_rec->e_cpos;
  3377. le32_add_cpu(&rec->e_int_clusters,
  3378. le16_to_cpu(insert_rec->e_leaf_clusters));
  3379. le32_add_cpu(&rec->e_int_clusters,
  3380. -le32_to_cpu(rec->e_cpos));
  3381. ocfs2_journal_dirty(handle, bh);
  3382. }
  3383. }
  3384. static int ocfs2_append_rec_to_path(handle_t *handle,
  3385. struct ocfs2_extent_tree *et,
  3386. struct ocfs2_extent_rec *insert_rec,
  3387. struct ocfs2_path *right_path,
  3388. struct ocfs2_path **ret_left_path)
  3389. {
  3390. int ret, next_free;
  3391. struct ocfs2_extent_list *el;
  3392. struct ocfs2_path *left_path = NULL;
  3393. *ret_left_path = NULL;
  3394. /*
  3395. * This shouldn't happen for non-trees. The extent rec cluster
  3396. * count manipulation below only works for interior nodes.
  3397. */
  3398. BUG_ON(right_path->p_tree_depth == 0);
  3399. /*
  3400. * If our appending insert is at the leftmost edge of a leaf,
  3401. * then we might need to update the rightmost records of the
  3402. * neighboring path.
  3403. */
  3404. el = path_leaf_el(right_path);
  3405. next_free = le16_to_cpu(el->l_next_free_rec);
  3406. if (next_free == 0 ||
  3407. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  3408. u32 left_cpos;
  3409. ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
  3410. right_path, &left_cpos);
  3411. if (ret) {
  3412. mlog_errno(ret);
  3413. goto out;
  3414. }
  3415. mlog(0, "Append may need a left path update. cpos: %u, "
  3416. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  3417. left_cpos);
  3418. /*
  3419. * No need to worry if the append is already in the
  3420. * leftmost leaf.
  3421. */
  3422. if (left_cpos) {
  3423. left_path = ocfs2_new_path_from_path(right_path);
  3424. if (!left_path) {
  3425. ret = -ENOMEM;
  3426. mlog_errno(ret);
  3427. goto out;
  3428. }
  3429. ret = ocfs2_find_path(et->et_ci, left_path,
  3430. left_cpos);
  3431. if (ret) {
  3432. mlog_errno(ret);
  3433. goto out;
  3434. }
  3435. /*
  3436. * ocfs2_insert_path() will pass the left_path to the
  3437. * journal for us.
  3438. */
  3439. }
  3440. }
  3441. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3442. if (ret) {
  3443. mlog_errno(ret);
  3444. goto out;
  3445. }
  3446. ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
  3447. *ret_left_path = left_path;
  3448. ret = 0;
  3449. out:
  3450. if (ret != 0)
  3451. ocfs2_free_path(left_path);
  3452. return ret;
  3453. }
  3454. static void ocfs2_split_record(struct ocfs2_extent_tree *et,
  3455. struct ocfs2_path *left_path,
  3456. struct ocfs2_path *right_path,
  3457. struct ocfs2_extent_rec *split_rec,
  3458. enum ocfs2_split_type split)
  3459. {
  3460. int index;
  3461. u32 cpos = le32_to_cpu(split_rec->e_cpos);
  3462. struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
  3463. struct ocfs2_extent_rec *rec, *tmprec;
  3464. right_el = path_leaf_el(right_path);
  3465. if (left_path)
  3466. left_el = path_leaf_el(left_path);
  3467. el = right_el;
  3468. insert_el = right_el;
  3469. index = ocfs2_search_extent_list(el, cpos);
  3470. if (index != -1) {
  3471. if (index == 0 && left_path) {
  3472. BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
  3473. /*
  3474. * This typically means that the record
  3475. * started in the left path but moved to the
  3476. * right as a result of rotation. We either
  3477. * move the existing record to the left, or we
  3478. * do the later insert there.
  3479. *
  3480. * In this case, the left path should always
  3481. * exist as the rotate code will have passed
  3482. * it back for a post-insert update.
  3483. */
  3484. if (split == SPLIT_LEFT) {
  3485. /*
  3486. * It's a left split. Since we know
  3487. * that the rotate code gave us an
  3488. * empty extent in the left path, we
  3489. * can just do the insert there.
  3490. */
  3491. insert_el = left_el;
  3492. } else {
  3493. /*
  3494. * Right split - we have to move the
  3495. * existing record over to the left
  3496. * leaf. The insert will be into the
  3497. * newly created empty extent in the
  3498. * right leaf.
  3499. */
  3500. tmprec = &right_el->l_recs[index];
  3501. ocfs2_rotate_leaf(left_el, tmprec);
  3502. el = left_el;
  3503. memset(tmprec, 0, sizeof(*tmprec));
  3504. index = ocfs2_search_extent_list(left_el, cpos);
  3505. BUG_ON(index == -1);
  3506. }
  3507. }
  3508. } else {
  3509. BUG_ON(!left_path);
  3510. BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
  3511. /*
  3512. * Left path is easy - we can just allow the insert to
  3513. * happen.
  3514. */
  3515. el = left_el;
  3516. insert_el = left_el;
  3517. index = ocfs2_search_extent_list(el, cpos);
  3518. BUG_ON(index == -1);
  3519. }
  3520. rec = &el->l_recs[index];
  3521. ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  3522. split, rec, split_rec);
  3523. ocfs2_rotate_leaf(insert_el, split_rec);
  3524. }
  3525. /*
  3526. * This function only does inserts on an allocation b-tree. For tree
  3527. * depth = 0, ocfs2_insert_at_leaf() is called directly.
  3528. *
  3529. * right_path is the path we want to do the actual insert
  3530. * in. left_path should only be passed in if we need to update that
  3531. * portion of the tree after an edge insert.
  3532. */
  3533. static int ocfs2_insert_path(handle_t *handle,
  3534. struct ocfs2_extent_tree *et,
  3535. struct ocfs2_path *left_path,
  3536. struct ocfs2_path *right_path,
  3537. struct ocfs2_extent_rec *insert_rec,
  3538. struct ocfs2_insert_type *insert)
  3539. {
  3540. int ret, subtree_index;
  3541. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  3542. if (left_path) {
  3543. int credits = handle->h_buffer_credits;
  3544. /*
  3545. * There's a chance that left_path got passed back to
  3546. * us without being accounted for in the
  3547. * journal. Extend our transaction here to be sure we
  3548. * can change those blocks.
  3549. */
  3550. credits += left_path->p_tree_depth;
  3551. ret = ocfs2_extend_trans(handle, credits);
  3552. if (ret < 0) {
  3553. mlog_errno(ret);
  3554. goto out;
  3555. }
  3556. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  3557. if (ret < 0) {
  3558. mlog_errno(ret);
  3559. goto out;
  3560. }
  3561. }
  3562. /*
  3563. * Pass both paths to the journal. The majority of inserts
  3564. * will be touching all components anyway.
  3565. */
  3566. ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
  3567. if (ret < 0) {
  3568. mlog_errno(ret);
  3569. goto out;
  3570. }
  3571. if (insert->ins_split != SPLIT_NONE) {
  3572. /*
  3573. * We could call ocfs2_insert_at_leaf() for some types
  3574. * of splits, but it's easier to just let one separate
  3575. * function sort it all out.
  3576. */
  3577. ocfs2_split_record(et, left_path, right_path,
  3578. insert_rec, insert->ins_split);
  3579. /*
  3580. * Split might have modified either leaf and we don't
  3581. * have a guarantee that the later edge insert will
  3582. * dirty this for us.
  3583. */
  3584. if (left_path)
  3585. ocfs2_journal_dirty(handle,
  3586. path_leaf_bh(left_path));
  3587. } else
  3588. ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
  3589. insert);
  3590. ocfs2_journal_dirty(handle, leaf_bh);
  3591. if (left_path) {
  3592. /*
  3593. * The rotate code has indicated that we need to fix
  3594. * up portions of the tree after the insert.
  3595. *
  3596. * XXX: Should we extend the transaction here?
  3597. */
  3598. subtree_index = ocfs2_find_subtree_root(et, left_path,
  3599. right_path);
  3600. ocfs2_complete_edge_insert(handle, left_path, right_path,
  3601. subtree_index);
  3602. }
  3603. ret = 0;
  3604. out:
  3605. return ret;
  3606. }
  3607. static int ocfs2_do_insert_extent(handle_t *handle,
  3608. struct ocfs2_extent_tree *et,
  3609. struct ocfs2_extent_rec *insert_rec,
  3610. struct ocfs2_insert_type *type)
  3611. {
  3612. int ret, rotate = 0;
  3613. u32 cpos;
  3614. struct ocfs2_path *right_path = NULL;
  3615. struct ocfs2_path *left_path = NULL;
  3616. struct ocfs2_extent_list *el;
  3617. el = et->et_root_el;
  3618. ret = ocfs2_et_root_journal_access(handle, et,
  3619. OCFS2_JOURNAL_ACCESS_WRITE);
  3620. if (ret) {
  3621. mlog_errno(ret);
  3622. goto out;
  3623. }
  3624. if (le16_to_cpu(el->l_tree_depth) == 0) {
  3625. ocfs2_insert_at_leaf(et, insert_rec, el, type);
  3626. goto out_update_clusters;
  3627. }
  3628. right_path = ocfs2_new_path_from_et(et);
  3629. if (!right_path) {
  3630. ret = -ENOMEM;
  3631. mlog_errno(ret);
  3632. goto out;
  3633. }
  3634. /*
  3635. * Determine the path to start with. Rotations need the
  3636. * rightmost path, everything else can go directly to the
  3637. * target leaf.
  3638. */
  3639. cpos = le32_to_cpu(insert_rec->e_cpos);
  3640. if (type->ins_appending == APPEND_NONE &&
  3641. type->ins_contig == CONTIG_NONE) {
  3642. rotate = 1;
  3643. cpos = UINT_MAX;
  3644. }
  3645. ret = ocfs2_find_path(et->et_ci, right_path, cpos);
  3646. if (ret) {
  3647. mlog_errno(ret);
  3648. goto out;
  3649. }
  3650. /*
  3651. * Rotations and appends need special treatment - they modify
  3652. * parts of the tree's above them.
  3653. *
  3654. * Both might pass back a path immediate to the left of the
  3655. * one being inserted to. This will be cause
  3656. * ocfs2_insert_path() to modify the rightmost records of
  3657. * left_path to account for an edge insert.
  3658. *
  3659. * XXX: When modifying this code, keep in mind that an insert
  3660. * can wind up skipping both of these two special cases...
  3661. */
  3662. if (rotate) {
  3663. ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
  3664. le32_to_cpu(insert_rec->e_cpos),
  3665. right_path, &left_path);
  3666. if (ret) {
  3667. mlog_errno(ret);
  3668. goto out;
  3669. }
  3670. /*
  3671. * ocfs2_rotate_tree_right() might have extended the
  3672. * transaction without re-journaling our tree root.
  3673. */
  3674. ret = ocfs2_et_root_journal_access(handle, et,
  3675. OCFS2_JOURNAL_ACCESS_WRITE);
  3676. if (ret) {
  3677. mlog_errno(ret);
  3678. goto out;
  3679. }
  3680. } else if (type->ins_appending == APPEND_TAIL
  3681. && type->ins_contig != CONTIG_LEFT) {
  3682. ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
  3683. right_path, &left_path);
  3684. if (ret) {
  3685. mlog_errno(ret);
  3686. goto out;
  3687. }
  3688. }
  3689. ret = ocfs2_insert_path(handle, et, left_path, right_path,
  3690. insert_rec, type);
  3691. if (ret) {
  3692. mlog_errno(ret);
  3693. goto out;
  3694. }
  3695. out_update_clusters:
  3696. if (type->ins_split == SPLIT_NONE)
  3697. ocfs2_et_update_clusters(et,
  3698. le16_to_cpu(insert_rec->e_leaf_clusters));
  3699. ocfs2_journal_dirty(handle, et->et_root_bh);
  3700. out:
  3701. ocfs2_free_path(left_path);
  3702. ocfs2_free_path(right_path);
  3703. return ret;
  3704. }
  3705. static enum ocfs2_contig_type
  3706. ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
  3707. struct ocfs2_path *path,
  3708. struct ocfs2_extent_list *el, int index,
  3709. struct ocfs2_extent_rec *split_rec)
  3710. {
  3711. int status;
  3712. enum ocfs2_contig_type ret = CONTIG_NONE;
  3713. u32 left_cpos, right_cpos;
  3714. struct ocfs2_extent_rec *rec = NULL;
  3715. struct ocfs2_extent_list *new_el;
  3716. struct ocfs2_path *left_path = NULL, *right_path = NULL;
  3717. struct buffer_head *bh;
  3718. struct ocfs2_extent_block *eb;
  3719. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  3720. if (index > 0) {
  3721. rec = &el->l_recs[index - 1];
  3722. } else if (path->p_tree_depth > 0) {
  3723. status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  3724. if (status)
  3725. goto out;
  3726. if (left_cpos != 0) {
  3727. left_path = ocfs2_new_path_from_path(path);
  3728. if (!left_path)
  3729. goto out;
  3730. status = ocfs2_find_path(et->et_ci, left_path,
  3731. left_cpos);
  3732. if (status)
  3733. goto out;
  3734. new_el = path_leaf_el(left_path);
  3735. if (le16_to_cpu(new_el->l_next_free_rec) !=
  3736. le16_to_cpu(new_el->l_count)) {
  3737. bh = path_leaf_bh(left_path);
  3738. eb = (struct ocfs2_extent_block *)bh->b_data;
  3739. ocfs2_error(sb,
  3740. "Extent block #%llu has an "
  3741. "invalid l_next_free_rec of "
  3742. "%d. It should have "
  3743. "matched the l_count of %d",
  3744. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3745. le16_to_cpu(new_el->l_next_free_rec),
  3746. le16_to_cpu(new_el->l_count));
  3747. status = -EINVAL;
  3748. goto out;
  3749. }
  3750. rec = &new_el->l_recs[
  3751. le16_to_cpu(new_el->l_next_free_rec) - 1];
  3752. }
  3753. }
  3754. /*
  3755. * We're careful to check for an empty extent record here -
  3756. * the merge code will know what to do if it sees one.
  3757. */
  3758. if (rec) {
  3759. if (index == 1 && ocfs2_is_empty_extent(rec)) {
  3760. if (split_rec->e_cpos == el->l_recs[index].e_cpos)
  3761. ret = CONTIG_RIGHT;
  3762. } else {
  3763. ret = ocfs2_et_extent_contig(et, rec, split_rec);
  3764. }
  3765. }
  3766. rec = NULL;
  3767. if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
  3768. rec = &el->l_recs[index + 1];
  3769. else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
  3770. path->p_tree_depth > 0) {
  3771. status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
  3772. if (status)
  3773. goto out;
  3774. if (right_cpos == 0)
  3775. goto out;
  3776. right_path = ocfs2_new_path_from_path(path);
  3777. if (!right_path)
  3778. goto out;
  3779. status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
  3780. if (status)
  3781. goto out;
  3782. new_el = path_leaf_el(right_path);
  3783. rec = &new_el->l_recs[0];
  3784. if (ocfs2_is_empty_extent(rec)) {
  3785. if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
  3786. bh = path_leaf_bh(right_path);
  3787. eb = (struct ocfs2_extent_block *)bh->b_data;
  3788. ocfs2_error(sb,
  3789. "Extent block #%llu has an "
  3790. "invalid l_next_free_rec of %d",
  3791. (unsigned long long)le64_to_cpu(eb->h_blkno),
  3792. le16_to_cpu(new_el->l_next_free_rec));
  3793. status = -EINVAL;
  3794. goto out;
  3795. }
  3796. rec = &new_el->l_recs[1];
  3797. }
  3798. }
  3799. if (rec) {
  3800. enum ocfs2_contig_type contig_type;
  3801. contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
  3802. if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
  3803. ret = CONTIG_LEFTRIGHT;
  3804. else if (ret == CONTIG_NONE)
  3805. ret = contig_type;
  3806. }
  3807. out:
  3808. if (left_path)
  3809. ocfs2_free_path(left_path);
  3810. if (right_path)
  3811. ocfs2_free_path(right_path);
  3812. return ret;
  3813. }
  3814. static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
  3815. struct ocfs2_insert_type *insert,
  3816. struct ocfs2_extent_list *el,
  3817. struct ocfs2_extent_rec *insert_rec)
  3818. {
  3819. int i;
  3820. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  3821. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3822. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  3823. contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
  3824. insert_rec);
  3825. if (contig_type != CONTIG_NONE) {
  3826. insert->ins_contig_index = i;
  3827. break;
  3828. }
  3829. }
  3830. insert->ins_contig = contig_type;
  3831. if (insert->ins_contig != CONTIG_NONE) {
  3832. struct ocfs2_extent_rec *rec =
  3833. &el->l_recs[insert->ins_contig_index];
  3834. unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
  3835. le16_to_cpu(insert_rec->e_leaf_clusters);
  3836. /*
  3837. * Caller might want us to limit the size of extents, don't
  3838. * calculate contiguousness if we might exceed that limit.
  3839. */
  3840. if (et->et_max_leaf_clusters &&
  3841. (len > et->et_max_leaf_clusters))
  3842. insert->ins_contig = CONTIG_NONE;
  3843. }
  3844. }
  3845. /*
  3846. * This should only be called against the righmost leaf extent list.
  3847. *
  3848. * ocfs2_figure_appending_type() will figure out whether we'll have to
  3849. * insert at the tail of the rightmost leaf.
  3850. *
  3851. * This should also work against the root extent list for tree's with 0
  3852. * depth. If we consider the root extent list to be the rightmost leaf node
  3853. * then the logic here makes sense.
  3854. */
  3855. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  3856. struct ocfs2_extent_list *el,
  3857. struct ocfs2_extent_rec *insert_rec)
  3858. {
  3859. int i;
  3860. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  3861. struct ocfs2_extent_rec *rec;
  3862. insert->ins_appending = APPEND_NONE;
  3863. BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
  3864. if (!el->l_next_free_rec)
  3865. goto set_tail_append;
  3866. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  3867. /* Were all records empty? */
  3868. if (le16_to_cpu(el->l_next_free_rec) == 1)
  3869. goto set_tail_append;
  3870. }
  3871. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3872. rec = &el->l_recs[i];
  3873. if (cpos >=
  3874. (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
  3875. goto set_tail_append;
  3876. return;
  3877. set_tail_append:
  3878. insert->ins_appending = APPEND_TAIL;
  3879. }
  3880. /*
  3881. * Helper function called at the begining of an insert.
  3882. *
  3883. * This computes a few things that are commonly used in the process of
  3884. * inserting into the btree:
  3885. * - Whether the new extent is contiguous with an existing one.
  3886. * - The current tree depth.
  3887. * - Whether the insert is an appending one.
  3888. * - The total # of free records in the tree.
  3889. *
  3890. * All of the information is stored on the ocfs2_insert_type
  3891. * structure.
  3892. */
  3893. static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
  3894. struct buffer_head **last_eb_bh,
  3895. struct ocfs2_extent_rec *insert_rec,
  3896. int *free_records,
  3897. struct ocfs2_insert_type *insert)
  3898. {
  3899. int ret;
  3900. struct ocfs2_extent_block *eb;
  3901. struct ocfs2_extent_list *el;
  3902. struct ocfs2_path *path = NULL;
  3903. struct buffer_head *bh = NULL;
  3904. insert->ins_split = SPLIT_NONE;
  3905. el = et->et_root_el;
  3906. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  3907. if (el->l_tree_depth) {
  3908. /*
  3909. * If we have tree depth, we read in the
  3910. * rightmost extent block ahead of time as
  3911. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  3912. * may want it later.
  3913. */
  3914. ret = ocfs2_read_extent_block(et->et_ci,
  3915. ocfs2_et_get_last_eb_blk(et),
  3916. &bh);
  3917. if (ret) {
  3918. mlog_exit(ret);
  3919. goto out;
  3920. }
  3921. eb = (struct ocfs2_extent_block *) bh->b_data;
  3922. el = &eb->h_list;
  3923. }
  3924. /*
  3925. * Unless we have a contiguous insert, we'll need to know if
  3926. * there is room left in our allocation tree for another
  3927. * extent record.
  3928. *
  3929. * XXX: This test is simplistic, we can search for empty
  3930. * extent records too.
  3931. */
  3932. *free_records = le16_to_cpu(el->l_count) -
  3933. le16_to_cpu(el->l_next_free_rec);
  3934. if (!insert->ins_tree_depth) {
  3935. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  3936. ocfs2_figure_appending_type(insert, el, insert_rec);
  3937. return 0;
  3938. }
  3939. path = ocfs2_new_path_from_et(et);
  3940. if (!path) {
  3941. ret = -ENOMEM;
  3942. mlog_errno(ret);
  3943. goto out;
  3944. }
  3945. /*
  3946. * In the case that we're inserting past what the tree
  3947. * currently accounts for, ocfs2_find_path() will return for
  3948. * us the rightmost tree path. This is accounted for below in
  3949. * the appending code.
  3950. */
  3951. ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
  3952. if (ret) {
  3953. mlog_errno(ret);
  3954. goto out;
  3955. }
  3956. el = path_leaf_el(path);
  3957. /*
  3958. * Now that we have the path, there's two things we want to determine:
  3959. * 1) Contiguousness (also set contig_index if this is so)
  3960. *
  3961. * 2) Are we doing an append? We can trivially break this up
  3962. * into two types of appends: simple record append, or a
  3963. * rotate inside the tail leaf.
  3964. */
  3965. ocfs2_figure_contig_type(et, insert, el, insert_rec);
  3966. /*
  3967. * The insert code isn't quite ready to deal with all cases of
  3968. * left contiguousness. Specifically, if it's an insert into
  3969. * the 1st record in a leaf, it will require the adjustment of
  3970. * cluster count on the last record of the path directly to it's
  3971. * left. For now, just catch that case and fool the layers
  3972. * above us. This works just fine for tree_depth == 0, which
  3973. * is why we allow that above.
  3974. */
  3975. if (insert->ins_contig == CONTIG_LEFT &&
  3976. insert->ins_contig_index == 0)
  3977. insert->ins_contig = CONTIG_NONE;
  3978. /*
  3979. * Ok, so we can simply compare against last_eb to figure out
  3980. * whether the path doesn't exist. This will only happen in
  3981. * the case that we're doing a tail append, so maybe we can
  3982. * take advantage of that information somehow.
  3983. */
  3984. if (ocfs2_et_get_last_eb_blk(et) ==
  3985. path_leaf_bh(path)->b_blocknr) {
  3986. /*
  3987. * Ok, ocfs2_find_path() returned us the rightmost
  3988. * tree path. This might be an appending insert. There are
  3989. * two cases:
  3990. * 1) We're doing a true append at the tail:
  3991. * -This might even be off the end of the leaf
  3992. * 2) We're "appending" by rotating in the tail
  3993. */
  3994. ocfs2_figure_appending_type(insert, el, insert_rec);
  3995. }
  3996. out:
  3997. ocfs2_free_path(path);
  3998. if (ret == 0)
  3999. *last_eb_bh = bh;
  4000. else
  4001. brelse(bh);
  4002. return ret;
  4003. }
  4004. /*
  4005. * Insert an extent into a btree.
  4006. *
  4007. * The caller needs to update the owning btree's cluster count.
  4008. */
  4009. int ocfs2_insert_extent(handle_t *handle,
  4010. struct ocfs2_extent_tree *et,
  4011. u32 cpos,
  4012. u64 start_blk,
  4013. u32 new_clusters,
  4014. u8 flags,
  4015. struct ocfs2_alloc_context *meta_ac)
  4016. {
  4017. int status;
  4018. int uninitialized_var(free_records);
  4019. struct buffer_head *last_eb_bh = NULL;
  4020. struct ocfs2_insert_type insert = {0, };
  4021. struct ocfs2_extent_rec rec;
  4022. mlog(0, "add %u clusters at position %u to owner %llu\n",
  4023. new_clusters, cpos,
  4024. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  4025. memset(&rec, 0, sizeof(rec));
  4026. rec.e_cpos = cpu_to_le32(cpos);
  4027. rec.e_blkno = cpu_to_le64(start_blk);
  4028. rec.e_leaf_clusters = cpu_to_le16(new_clusters);
  4029. rec.e_flags = flags;
  4030. status = ocfs2_et_insert_check(et, &rec);
  4031. if (status) {
  4032. mlog_errno(status);
  4033. goto bail;
  4034. }
  4035. status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
  4036. &free_records, &insert);
  4037. if (status < 0) {
  4038. mlog_errno(status);
  4039. goto bail;
  4040. }
  4041. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  4042. "Insert.contig_index: %d, Insert.free_records: %d, "
  4043. "Insert.tree_depth: %d\n",
  4044. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  4045. free_records, insert.ins_tree_depth);
  4046. if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
  4047. status = ocfs2_grow_tree(handle, et,
  4048. &insert.ins_tree_depth, &last_eb_bh,
  4049. meta_ac);
  4050. if (status) {
  4051. mlog_errno(status);
  4052. goto bail;
  4053. }
  4054. }
  4055. /* Finally, we can add clusters. This might rotate the tree for us. */
  4056. status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
  4057. if (status < 0)
  4058. mlog_errno(status);
  4059. else
  4060. ocfs2_et_extent_map_insert(et, &rec);
  4061. bail:
  4062. brelse(last_eb_bh);
  4063. mlog_exit(status);
  4064. return status;
  4065. }
  4066. /*
  4067. * Allcate and add clusters into the extent b-tree.
  4068. * The new clusters(clusters_to_add) will be inserted at logical_offset.
  4069. * The extent b-tree's root is specified by et, and
  4070. * it is not limited to the file storage. Any extent tree can use this
  4071. * function if it implements the proper ocfs2_extent_tree.
  4072. */
  4073. int ocfs2_add_clusters_in_btree(handle_t *handle,
  4074. struct ocfs2_extent_tree *et,
  4075. u32 *logical_offset,
  4076. u32 clusters_to_add,
  4077. int mark_unwritten,
  4078. struct ocfs2_alloc_context *data_ac,
  4079. struct ocfs2_alloc_context *meta_ac,
  4080. enum ocfs2_alloc_restarted *reason_ret)
  4081. {
  4082. int status = 0;
  4083. int free_extents;
  4084. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  4085. u32 bit_off, num_bits;
  4086. u64 block;
  4087. u8 flags = 0;
  4088. struct ocfs2_super *osb =
  4089. OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
  4090. BUG_ON(!clusters_to_add);
  4091. if (mark_unwritten)
  4092. flags = OCFS2_EXT_UNWRITTEN;
  4093. free_extents = ocfs2_num_free_extents(osb, et);
  4094. if (free_extents < 0) {
  4095. status = free_extents;
  4096. mlog_errno(status);
  4097. goto leave;
  4098. }
  4099. /* there are two cases which could cause us to EAGAIN in the
  4100. * we-need-more-metadata case:
  4101. * 1) we haven't reserved *any*
  4102. * 2) we are so fragmented, we've needed to add metadata too
  4103. * many times. */
  4104. if (!free_extents && !meta_ac) {
  4105. mlog(0, "we haven't reserved any metadata!\n");
  4106. status = -EAGAIN;
  4107. reason = RESTART_META;
  4108. goto leave;
  4109. } else if ((!free_extents)
  4110. && (ocfs2_alloc_context_bits_left(meta_ac)
  4111. < ocfs2_extend_meta_needed(et->et_root_el))) {
  4112. mlog(0, "filesystem is really fragmented...\n");
  4113. status = -EAGAIN;
  4114. reason = RESTART_META;
  4115. goto leave;
  4116. }
  4117. status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  4118. clusters_to_add, &bit_off, &num_bits);
  4119. if (status < 0) {
  4120. if (status != -ENOSPC)
  4121. mlog_errno(status);
  4122. goto leave;
  4123. }
  4124. BUG_ON(num_bits > clusters_to_add);
  4125. /* reserve our write early -- insert_extent may update the tree root */
  4126. status = ocfs2_et_root_journal_access(handle, et,
  4127. OCFS2_JOURNAL_ACCESS_WRITE);
  4128. if (status < 0) {
  4129. mlog_errno(status);
  4130. goto leave;
  4131. }
  4132. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  4133. mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
  4134. num_bits, bit_off,
  4135. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
  4136. status = ocfs2_insert_extent(handle, et, *logical_offset, block,
  4137. num_bits, flags, meta_ac);
  4138. if (status < 0) {
  4139. mlog_errno(status);
  4140. goto leave;
  4141. }
  4142. ocfs2_journal_dirty(handle, et->et_root_bh);
  4143. clusters_to_add -= num_bits;
  4144. *logical_offset += num_bits;
  4145. if (clusters_to_add) {
  4146. mlog(0, "need to alloc once more, wanted = %u\n",
  4147. clusters_to_add);
  4148. status = -EAGAIN;
  4149. reason = RESTART_TRANS;
  4150. }
  4151. leave:
  4152. mlog_exit(status);
  4153. if (reason_ret)
  4154. *reason_ret = reason;
  4155. return status;
  4156. }
  4157. static void ocfs2_make_right_split_rec(struct super_block *sb,
  4158. struct ocfs2_extent_rec *split_rec,
  4159. u32 cpos,
  4160. struct ocfs2_extent_rec *rec)
  4161. {
  4162. u32 rec_cpos = le32_to_cpu(rec->e_cpos);
  4163. u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
  4164. memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4165. split_rec->e_cpos = cpu_to_le32(cpos);
  4166. split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
  4167. split_rec->e_blkno = rec->e_blkno;
  4168. le64_add_cpu(&split_rec->e_blkno,
  4169. ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
  4170. split_rec->e_flags = rec->e_flags;
  4171. }
  4172. static int ocfs2_split_and_insert(handle_t *handle,
  4173. struct ocfs2_extent_tree *et,
  4174. struct ocfs2_path *path,
  4175. struct buffer_head **last_eb_bh,
  4176. int split_index,
  4177. struct ocfs2_extent_rec *orig_split_rec,
  4178. struct ocfs2_alloc_context *meta_ac)
  4179. {
  4180. int ret = 0, depth;
  4181. unsigned int insert_range, rec_range, do_leftright = 0;
  4182. struct ocfs2_extent_rec tmprec;
  4183. struct ocfs2_extent_list *rightmost_el;
  4184. struct ocfs2_extent_rec rec;
  4185. struct ocfs2_extent_rec split_rec = *orig_split_rec;
  4186. struct ocfs2_insert_type insert;
  4187. struct ocfs2_extent_block *eb;
  4188. leftright:
  4189. /*
  4190. * Store a copy of the record on the stack - it might move
  4191. * around as the tree is manipulated below.
  4192. */
  4193. rec = path_leaf_el(path)->l_recs[split_index];
  4194. rightmost_el = et->et_root_el;
  4195. depth = le16_to_cpu(rightmost_el->l_tree_depth);
  4196. if (depth) {
  4197. BUG_ON(!(*last_eb_bh));
  4198. eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
  4199. rightmost_el = &eb->h_list;
  4200. }
  4201. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4202. le16_to_cpu(rightmost_el->l_count)) {
  4203. ret = ocfs2_grow_tree(handle, et,
  4204. &depth, last_eb_bh, meta_ac);
  4205. if (ret) {
  4206. mlog_errno(ret);
  4207. goto out;
  4208. }
  4209. }
  4210. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4211. insert.ins_appending = APPEND_NONE;
  4212. insert.ins_contig = CONTIG_NONE;
  4213. insert.ins_tree_depth = depth;
  4214. insert_range = le32_to_cpu(split_rec.e_cpos) +
  4215. le16_to_cpu(split_rec.e_leaf_clusters);
  4216. rec_range = le32_to_cpu(rec.e_cpos) +
  4217. le16_to_cpu(rec.e_leaf_clusters);
  4218. if (split_rec.e_cpos == rec.e_cpos) {
  4219. insert.ins_split = SPLIT_LEFT;
  4220. } else if (insert_range == rec_range) {
  4221. insert.ins_split = SPLIT_RIGHT;
  4222. } else {
  4223. /*
  4224. * Left/right split. We fake this as a right split
  4225. * first and then make a second pass as a left split.
  4226. */
  4227. insert.ins_split = SPLIT_RIGHT;
  4228. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4229. &tmprec, insert_range, &rec);
  4230. split_rec = tmprec;
  4231. BUG_ON(do_leftright);
  4232. do_leftright = 1;
  4233. }
  4234. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4235. if (ret) {
  4236. mlog_errno(ret);
  4237. goto out;
  4238. }
  4239. if (do_leftright == 1) {
  4240. u32 cpos;
  4241. struct ocfs2_extent_list *el;
  4242. do_leftright++;
  4243. split_rec = *orig_split_rec;
  4244. ocfs2_reinit_path(path, 1);
  4245. cpos = le32_to_cpu(split_rec.e_cpos);
  4246. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4247. if (ret) {
  4248. mlog_errno(ret);
  4249. goto out;
  4250. }
  4251. el = path_leaf_el(path);
  4252. split_index = ocfs2_search_extent_list(el, cpos);
  4253. goto leftright;
  4254. }
  4255. out:
  4256. return ret;
  4257. }
  4258. static int ocfs2_replace_extent_rec(handle_t *handle,
  4259. struct ocfs2_extent_tree *et,
  4260. struct ocfs2_path *path,
  4261. struct ocfs2_extent_list *el,
  4262. int split_index,
  4263. struct ocfs2_extent_rec *split_rec)
  4264. {
  4265. int ret;
  4266. ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
  4267. path_num_items(path) - 1);
  4268. if (ret) {
  4269. mlog_errno(ret);
  4270. goto out;
  4271. }
  4272. el->l_recs[split_index] = *split_rec;
  4273. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4274. out:
  4275. return ret;
  4276. }
  4277. /*
  4278. * Split part or all of the extent record at split_index in the leaf
  4279. * pointed to by path. Merge with the contiguous extent record if needed.
  4280. *
  4281. * Care is taken to handle contiguousness so as to not grow the tree.
  4282. *
  4283. * meta_ac is not strictly necessary - we only truly need it if growth
  4284. * of the tree is required. All other cases will degrade into a less
  4285. * optimal tree layout.
  4286. *
  4287. * last_eb_bh should be the rightmost leaf block for any extent
  4288. * btree. Since a split may grow the tree or a merge might shrink it,
  4289. * the caller cannot trust the contents of that buffer after this call.
  4290. *
  4291. * This code is optimized for readability - several passes might be
  4292. * made over certain portions of the tree. All of those blocks will
  4293. * have been brought into cache (and pinned via the journal), so the
  4294. * extra overhead is not expressed in terms of disk reads.
  4295. */
  4296. int ocfs2_split_extent(handle_t *handle,
  4297. struct ocfs2_extent_tree *et,
  4298. struct ocfs2_path *path,
  4299. int split_index,
  4300. struct ocfs2_extent_rec *split_rec,
  4301. struct ocfs2_alloc_context *meta_ac,
  4302. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4303. {
  4304. int ret = 0;
  4305. struct ocfs2_extent_list *el = path_leaf_el(path);
  4306. struct buffer_head *last_eb_bh = NULL;
  4307. struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
  4308. struct ocfs2_merge_ctxt ctxt;
  4309. struct ocfs2_extent_list *rightmost_el;
  4310. if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
  4311. ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
  4312. (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
  4313. ret = -EIO;
  4314. mlog_errno(ret);
  4315. goto out;
  4316. }
  4317. ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
  4318. split_index,
  4319. split_rec);
  4320. /*
  4321. * The core merge / split code wants to know how much room is
  4322. * left in this allocation tree, so we pass the
  4323. * rightmost extent list.
  4324. */
  4325. if (path->p_tree_depth) {
  4326. struct ocfs2_extent_block *eb;
  4327. ret = ocfs2_read_extent_block(et->et_ci,
  4328. ocfs2_et_get_last_eb_blk(et),
  4329. &last_eb_bh);
  4330. if (ret) {
  4331. mlog_exit(ret);
  4332. goto out;
  4333. }
  4334. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4335. rightmost_el = &eb->h_list;
  4336. } else
  4337. rightmost_el = path_root_el(path);
  4338. if (rec->e_cpos == split_rec->e_cpos &&
  4339. rec->e_leaf_clusters == split_rec->e_leaf_clusters)
  4340. ctxt.c_split_covers_rec = 1;
  4341. else
  4342. ctxt.c_split_covers_rec = 0;
  4343. ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
  4344. mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
  4345. split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
  4346. ctxt.c_split_covers_rec);
  4347. if (ctxt.c_contig_type == CONTIG_NONE) {
  4348. if (ctxt.c_split_covers_rec)
  4349. ret = ocfs2_replace_extent_rec(handle, et, path, el,
  4350. split_index, split_rec);
  4351. else
  4352. ret = ocfs2_split_and_insert(handle, et, path,
  4353. &last_eb_bh, split_index,
  4354. split_rec, meta_ac);
  4355. if (ret)
  4356. mlog_errno(ret);
  4357. } else {
  4358. ret = ocfs2_try_to_merge_extent(handle, et, path,
  4359. split_index, split_rec,
  4360. dealloc, &ctxt);
  4361. if (ret)
  4362. mlog_errno(ret);
  4363. }
  4364. out:
  4365. brelse(last_eb_bh);
  4366. return ret;
  4367. }
  4368. /*
  4369. * Change the flags of the already-existing extent at cpos for len clusters.
  4370. *
  4371. * new_flags: the flags we want to set.
  4372. * clear_flags: the flags we want to clear.
  4373. * phys: the new physical offset we want this new extent starts from.
  4374. *
  4375. * If the existing extent is larger than the request, initiate a
  4376. * split. An attempt will be made at merging with adjacent extents.
  4377. *
  4378. * The caller is responsible for passing down meta_ac if we'll need it.
  4379. */
  4380. int ocfs2_change_extent_flag(handle_t *handle,
  4381. struct ocfs2_extent_tree *et,
  4382. u32 cpos, u32 len, u32 phys,
  4383. struct ocfs2_alloc_context *meta_ac,
  4384. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4385. int new_flags, int clear_flags)
  4386. {
  4387. int ret, index;
  4388. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4389. u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
  4390. struct ocfs2_extent_rec split_rec;
  4391. struct ocfs2_path *left_path = NULL;
  4392. struct ocfs2_extent_list *el;
  4393. struct ocfs2_extent_rec *rec;
  4394. left_path = ocfs2_new_path_from_et(et);
  4395. if (!left_path) {
  4396. ret = -ENOMEM;
  4397. mlog_errno(ret);
  4398. goto out;
  4399. }
  4400. ret = ocfs2_find_path(et->et_ci, left_path, cpos);
  4401. if (ret) {
  4402. mlog_errno(ret);
  4403. goto out;
  4404. }
  4405. el = path_leaf_el(left_path);
  4406. index = ocfs2_search_extent_list(el, cpos);
  4407. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4408. ocfs2_error(sb,
  4409. "Owner %llu has an extent at cpos %u which can no "
  4410. "longer be found.\n",
  4411. (unsigned long long)
  4412. ocfs2_metadata_cache_owner(et->et_ci), cpos);
  4413. ret = -EROFS;
  4414. goto out;
  4415. }
  4416. ret = -EIO;
  4417. rec = &el->l_recs[index];
  4418. if (new_flags && (rec->e_flags & new_flags)) {
  4419. mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
  4420. "extent that already had them",
  4421. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4422. new_flags);
  4423. goto out;
  4424. }
  4425. if (clear_flags && !(rec->e_flags & clear_flags)) {
  4426. mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
  4427. "extent that didn't have them",
  4428. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4429. clear_flags);
  4430. goto out;
  4431. }
  4432. memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
  4433. split_rec.e_cpos = cpu_to_le32(cpos);
  4434. split_rec.e_leaf_clusters = cpu_to_le16(len);
  4435. split_rec.e_blkno = cpu_to_le64(start_blkno);
  4436. split_rec.e_flags = rec->e_flags;
  4437. if (new_flags)
  4438. split_rec.e_flags |= new_flags;
  4439. if (clear_flags)
  4440. split_rec.e_flags &= ~clear_flags;
  4441. ret = ocfs2_split_extent(handle, et, left_path,
  4442. index, &split_rec, meta_ac,
  4443. dealloc);
  4444. if (ret)
  4445. mlog_errno(ret);
  4446. out:
  4447. ocfs2_free_path(left_path);
  4448. return ret;
  4449. }
  4450. /*
  4451. * Mark the already-existing extent at cpos as written for len clusters.
  4452. * This removes the unwritten extent flag.
  4453. *
  4454. * If the existing extent is larger than the request, initiate a
  4455. * split. An attempt will be made at merging with adjacent extents.
  4456. *
  4457. * The caller is responsible for passing down meta_ac if we'll need it.
  4458. */
  4459. int ocfs2_mark_extent_written(struct inode *inode,
  4460. struct ocfs2_extent_tree *et,
  4461. handle_t *handle, u32 cpos, u32 len, u32 phys,
  4462. struct ocfs2_alloc_context *meta_ac,
  4463. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4464. {
  4465. int ret;
  4466. mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
  4467. inode->i_ino, cpos, len, phys);
  4468. if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
  4469. ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
  4470. "that are being written to, but the feature bit "
  4471. "is not set in the super block.",
  4472. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  4473. ret = -EROFS;
  4474. goto out;
  4475. }
  4476. /*
  4477. * XXX: This should be fixed up so that we just re-insert the
  4478. * next extent records.
  4479. */
  4480. ocfs2_et_extent_map_truncate(et, 0);
  4481. ret = ocfs2_change_extent_flag(handle, et, cpos,
  4482. len, phys, meta_ac, dealloc,
  4483. 0, OCFS2_EXT_UNWRITTEN);
  4484. if (ret)
  4485. mlog_errno(ret);
  4486. out:
  4487. return ret;
  4488. }
  4489. static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
  4490. struct ocfs2_path *path,
  4491. int index, u32 new_range,
  4492. struct ocfs2_alloc_context *meta_ac)
  4493. {
  4494. int ret, depth, credits = handle->h_buffer_credits;
  4495. struct buffer_head *last_eb_bh = NULL;
  4496. struct ocfs2_extent_block *eb;
  4497. struct ocfs2_extent_list *rightmost_el, *el;
  4498. struct ocfs2_extent_rec split_rec;
  4499. struct ocfs2_extent_rec *rec;
  4500. struct ocfs2_insert_type insert;
  4501. /*
  4502. * Setup the record to split before we grow the tree.
  4503. */
  4504. el = path_leaf_el(path);
  4505. rec = &el->l_recs[index];
  4506. ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
  4507. &split_rec, new_range, rec);
  4508. depth = path->p_tree_depth;
  4509. if (depth > 0) {
  4510. ret = ocfs2_read_extent_block(et->et_ci,
  4511. ocfs2_et_get_last_eb_blk(et),
  4512. &last_eb_bh);
  4513. if (ret < 0) {
  4514. mlog_errno(ret);
  4515. goto out;
  4516. }
  4517. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  4518. rightmost_el = &eb->h_list;
  4519. } else
  4520. rightmost_el = path_leaf_el(path);
  4521. credits += path->p_tree_depth +
  4522. ocfs2_extend_meta_needed(et->et_root_el);
  4523. ret = ocfs2_extend_trans(handle, credits);
  4524. if (ret) {
  4525. mlog_errno(ret);
  4526. goto out;
  4527. }
  4528. if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
  4529. le16_to_cpu(rightmost_el->l_count)) {
  4530. ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
  4531. meta_ac);
  4532. if (ret) {
  4533. mlog_errno(ret);
  4534. goto out;
  4535. }
  4536. }
  4537. memset(&insert, 0, sizeof(struct ocfs2_insert_type));
  4538. insert.ins_appending = APPEND_NONE;
  4539. insert.ins_contig = CONTIG_NONE;
  4540. insert.ins_split = SPLIT_RIGHT;
  4541. insert.ins_tree_depth = depth;
  4542. ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
  4543. if (ret)
  4544. mlog_errno(ret);
  4545. out:
  4546. brelse(last_eb_bh);
  4547. return ret;
  4548. }
  4549. static int ocfs2_truncate_rec(handle_t *handle,
  4550. struct ocfs2_extent_tree *et,
  4551. struct ocfs2_path *path, int index,
  4552. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4553. u32 cpos, u32 len)
  4554. {
  4555. int ret;
  4556. u32 left_cpos, rec_range, trunc_range;
  4557. int wants_rotate = 0, is_rightmost_tree_rec = 0;
  4558. struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
  4559. struct ocfs2_path *left_path = NULL;
  4560. struct ocfs2_extent_list *el = path_leaf_el(path);
  4561. struct ocfs2_extent_rec *rec;
  4562. struct ocfs2_extent_block *eb;
  4563. if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
  4564. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4565. if (ret) {
  4566. mlog_errno(ret);
  4567. goto out;
  4568. }
  4569. index--;
  4570. }
  4571. if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
  4572. path->p_tree_depth) {
  4573. /*
  4574. * Check whether this is the rightmost tree record. If
  4575. * we remove all of this record or part of its right
  4576. * edge then an update of the record lengths above it
  4577. * will be required.
  4578. */
  4579. eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
  4580. if (eb->h_next_leaf_blk == 0)
  4581. is_rightmost_tree_rec = 1;
  4582. }
  4583. rec = &el->l_recs[index];
  4584. if (index == 0 && path->p_tree_depth &&
  4585. le32_to_cpu(rec->e_cpos) == cpos) {
  4586. /*
  4587. * Changing the leftmost offset (via partial or whole
  4588. * record truncate) of an interior (or rightmost) path
  4589. * means we have to update the subtree that is formed
  4590. * by this leaf and the one to it's left.
  4591. *
  4592. * There are two cases we can skip:
  4593. * 1) Path is the leftmost one in our btree.
  4594. * 2) The leaf is rightmost and will be empty after
  4595. * we remove the extent record - the rotate code
  4596. * knows how to update the newly formed edge.
  4597. */
  4598. ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
  4599. if (ret) {
  4600. mlog_errno(ret);
  4601. goto out;
  4602. }
  4603. if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
  4604. left_path = ocfs2_new_path_from_path(path);
  4605. if (!left_path) {
  4606. ret = -ENOMEM;
  4607. mlog_errno(ret);
  4608. goto out;
  4609. }
  4610. ret = ocfs2_find_path(et->et_ci, left_path,
  4611. left_cpos);
  4612. if (ret) {
  4613. mlog_errno(ret);
  4614. goto out;
  4615. }
  4616. }
  4617. }
  4618. ret = ocfs2_extend_rotate_transaction(handle, 0,
  4619. handle->h_buffer_credits,
  4620. path);
  4621. if (ret) {
  4622. mlog_errno(ret);
  4623. goto out;
  4624. }
  4625. ret = ocfs2_journal_access_path(et->et_ci, handle, path);
  4626. if (ret) {
  4627. mlog_errno(ret);
  4628. goto out;
  4629. }
  4630. ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
  4631. if (ret) {
  4632. mlog_errno(ret);
  4633. goto out;
  4634. }
  4635. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4636. trunc_range = cpos + len;
  4637. if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
  4638. int next_free;
  4639. memset(rec, 0, sizeof(*rec));
  4640. ocfs2_cleanup_merge(el, index);
  4641. wants_rotate = 1;
  4642. next_free = le16_to_cpu(el->l_next_free_rec);
  4643. if (is_rightmost_tree_rec && next_free > 1) {
  4644. /*
  4645. * We skip the edge update if this path will
  4646. * be deleted by the rotate code.
  4647. */
  4648. rec = &el->l_recs[next_free - 1];
  4649. ocfs2_adjust_rightmost_records(handle, et, path,
  4650. rec);
  4651. }
  4652. } else if (le32_to_cpu(rec->e_cpos) == cpos) {
  4653. /* Remove leftmost portion of the record. */
  4654. le32_add_cpu(&rec->e_cpos, len);
  4655. le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
  4656. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4657. } else if (rec_range == trunc_range) {
  4658. /* Remove rightmost portion of the record */
  4659. le16_add_cpu(&rec->e_leaf_clusters, -len);
  4660. if (is_rightmost_tree_rec)
  4661. ocfs2_adjust_rightmost_records(handle, et, path, rec);
  4662. } else {
  4663. /* Caller should have trapped this. */
  4664. mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
  4665. "(%u, %u)\n",
  4666. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4667. le32_to_cpu(rec->e_cpos),
  4668. le16_to_cpu(rec->e_leaf_clusters), cpos, len);
  4669. BUG();
  4670. }
  4671. if (left_path) {
  4672. int subtree_index;
  4673. subtree_index = ocfs2_find_subtree_root(et, left_path, path);
  4674. ocfs2_complete_edge_insert(handle, left_path, path,
  4675. subtree_index);
  4676. }
  4677. ocfs2_journal_dirty(handle, path_leaf_bh(path));
  4678. ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
  4679. if (ret) {
  4680. mlog_errno(ret);
  4681. goto out;
  4682. }
  4683. out:
  4684. ocfs2_free_path(left_path);
  4685. return ret;
  4686. }
  4687. int ocfs2_remove_extent(handle_t *handle,
  4688. struct ocfs2_extent_tree *et,
  4689. u32 cpos, u32 len,
  4690. struct ocfs2_alloc_context *meta_ac,
  4691. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4692. {
  4693. int ret, index;
  4694. u32 rec_range, trunc_range;
  4695. struct ocfs2_extent_rec *rec;
  4696. struct ocfs2_extent_list *el;
  4697. struct ocfs2_path *path = NULL;
  4698. /*
  4699. * XXX: Why are we truncating to 0 instead of wherever this
  4700. * affects us?
  4701. */
  4702. ocfs2_et_extent_map_truncate(et, 0);
  4703. path = ocfs2_new_path_from_et(et);
  4704. if (!path) {
  4705. ret = -ENOMEM;
  4706. mlog_errno(ret);
  4707. goto out;
  4708. }
  4709. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4710. if (ret) {
  4711. mlog_errno(ret);
  4712. goto out;
  4713. }
  4714. el = path_leaf_el(path);
  4715. index = ocfs2_search_extent_list(el, cpos);
  4716. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4717. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4718. "Owner %llu has an extent at cpos %u which can no "
  4719. "longer be found.\n",
  4720. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4721. cpos);
  4722. ret = -EROFS;
  4723. goto out;
  4724. }
  4725. /*
  4726. * We have 3 cases of extent removal:
  4727. * 1) Range covers the entire extent rec
  4728. * 2) Range begins or ends on one edge of the extent rec
  4729. * 3) Range is in the middle of the extent rec (no shared edges)
  4730. *
  4731. * For case 1 we remove the extent rec and left rotate to
  4732. * fill the hole.
  4733. *
  4734. * For case 2 we just shrink the existing extent rec, with a
  4735. * tree update if the shrinking edge is also the edge of an
  4736. * extent block.
  4737. *
  4738. * For case 3 we do a right split to turn the extent rec into
  4739. * something case 2 can handle.
  4740. */
  4741. rec = &el->l_recs[index];
  4742. rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  4743. trunc_range = cpos + len;
  4744. BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
  4745. mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
  4746. "(cpos %u, len %u)\n",
  4747. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4748. cpos, len, index,
  4749. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
  4750. if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
  4751. ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
  4752. cpos, len);
  4753. if (ret) {
  4754. mlog_errno(ret);
  4755. goto out;
  4756. }
  4757. } else {
  4758. ret = ocfs2_split_tree(handle, et, path, index,
  4759. trunc_range, meta_ac);
  4760. if (ret) {
  4761. mlog_errno(ret);
  4762. goto out;
  4763. }
  4764. /*
  4765. * The split could have manipulated the tree enough to
  4766. * move the record location, so we have to look for it again.
  4767. */
  4768. ocfs2_reinit_path(path, 1);
  4769. ret = ocfs2_find_path(et->et_ci, path, cpos);
  4770. if (ret) {
  4771. mlog_errno(ret);
  4772. goto out;
  4773. }
  4774. el = path_leaf_el(path);
  4775. index = ocfs2_search_extent_list(el, cpos);
  4776. if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
  4777. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4778. "Owner %llu: split at cpos %u lost record.",
  4779. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4780. cpos);
  4781. ret = -EROFS;
  4782. goto out;
  4783. }
  4784. /*
  4785. * Double check our values here. If anything is fishy,
  4786. * it's easier to catch it at the top level.
  4787. */
  4788. rec = &el->l_recs[index];
  4789. rec_range = le32_to_cpu(rec->e_cpos) +
  4790. ocfs2_rec_clusters(el, rec);
  4791. if (rec_range != trunc_range) {
  4792. ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
  4793. "Owner %llu: error after split at cpos %u"
  4794. "trunc len %u, existing record is (%u,%u)",
  4795. (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
  4796. cpos, len, le32_to_cpu(rec->e_cpos),
  4797. ocfs2_rec_clusters(el, rec));
  4798. ret = -EROFS;
  4799. goto out;
  4800. }
  4801. ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
  4802. cpos, len);
  4803. if (ret) {
  4804. mlog_errno(ret);
  4805. goto out;
  4806. }
  4807. }
  4808. out:
  4809. ocfs2_free_path(path);
  4810. return ret;
  4811. }
  4812. int ocfs2_remove_btree_range(struct inode *inode,
  4813. struct ocfs2_extent_tree *et,
  4814. u32 cpos, u32 phys_cpos, u32 len,
  4815. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4816. {
  4817. int ret;
  4818. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  4819. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4820. struct inode *tl_inode = osb->osb_tl_inode;
  4821. handle_t *handle;
  4822. struct ocfs2_alloc_context *meta_ac = NULL;
  4823. ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
  4824. if (ret) {
  4825. mlog_errno(ret);
  4826. return ret;
  4827. }
  4828. mutex_lock(&tl_inode->i_mutex);
  4829. if (ocfs2_truncate_log_needs_flush(osb)) {
  4830. ret = __ocfs2_flush_truncate_log(osb);
  4831. if (ret < 0) {
  4832. mlog_errno(ret);
  4833. goto out;
  4834. }
  4835. }
  4836. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4837. if (IS_ERR(handle)) {
  4838. ret = PTR_ERR(handle);
  4839. mlog_errno(ret);
  4840. goto out;
  4841. }
  4842. ret = ocfs2_et_root_journal_access(handle, et,
  4843. OCFS2_JOURNAL_ACCESS_WRITE);
  4844. if (ret) {
  4845. mlog_errno(ret);
  4846. goto out;
  4847. }
  4848. dquot_free_space_nodirty(inode,
  4849. ocfs2_clusters_to_bytes(inode->i_sb, len));
  4850. ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
  4851. if (ret) {
  4852. mlog_errno(ret);
  4853. goto out_commit;
  4854. }
  4855. ocfs2_et_update_clusters(et, -len);
  4856. ocfs2_journal_dirty(handle, et->et_root_bh);
  4857. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  4858. if (ret)
  4859. mlog_errno(ret);
  4860. out_commit:
  4861. ocfs2_commit_trans(osb, handle);
  4862. out:
  4863. mutex_unlock(&tl_inode->i_mutex);
  4864. if (meta_ac)
  4865. ocfs2_free_alloc_context(meta_ac);
  4866. return ret;
  4867. }
  4868. int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  4869. {
  4870. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4871. struct ocfs2_dinode *di;
  4872. struct ocfs2_truncate_log *tl;
  4873. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4874. tl = &di->id2.i_dealloc;
  4875. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  4876. "slot %d, invalid truncate log parameters: used = "
  4877. "%u, count = %u\n", osb->slot_num,
  4878. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  4879. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  4880. }
  4881. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  4882. unsigned int new_start)
  4883. {
  4884. unsigned int tail_index;
  4885. unsigned int current_tail;
  4886. /* No records, nothing to coalesce */
  4887. if (!le16_to_cpu(tl->tl_used))
  4888. return 0;
  4889. tail_index = le16_to_cpu(tl->tl_used) - 1;
  4890. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  4891. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  4892. return current_tail == new_start;
  4893. }
  4894. int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  4895. handle_t *handle,
  4896. u64 start_blk,
  4897. unsigned int num_clusters)
  4898. {
  4899. int status, index;
  4900. unsigned int start_cluster, tl_count;
  4901. struct inode *tl_inode = osb->osb_tl_inode;
  4902. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4903. struct ocfs2_dinode *di;
  4904. struct ocfs2_truncate_log *tl;
  4905. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  4906. (unsigned long long)start_blk, num_clusters);
  4907. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  4908. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  4909. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4910. /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
  4911. * by the underlying call to ocfs2_read_inode_block(), so any
  4912. * corruption is a code bug */
  4913. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  4914. tl = &di->id2.i_dealloc;
  4915. tl_count = le16_to_cpu(tl->tl_count);
  4916. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  4917. tl_count == 0,
  4918. "Truncate record count on #%llu invalid "
  4919. "wanted %u, actual %u\n",
  4920. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  4921. ocfs2_truncate_recs_per_inode(osb->sb),
  4922. le16_to_cpu(tl->tl_count));
  4923. /* Caller should have known to flush before calling us. */
  4924. index = le16_to_cpu(tl->tl_used);
  4925. if (index >= tl_count) {
  4926. status = -ENOSPC;
  4927. mlog_errno(status);
  4928. goto bail;
  4929. }
  4930. status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
  4931. OCFS2_JOURNAL_ACCESS_WRITE);
  4932. if (status < 0) {
  4933. mlog_errno(status);
  4934. goto bail;
  4935. }
  4936. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  4937. "%llu (index = %d)\n", num_clusters, start_cluster,
  4938. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  4939. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  4940. /*
  4941. * Move index back to the record we are coalescing with.
  4942. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  4943. */
  4944. index--;
  4945. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  4946. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  4947. index, le32_to_cpu(tl->tl_recs[index].t_start),
  4948. num_clusters);
  4949. } else {
  4950. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  4951. tl->tl_used = cpu_to_le16(index + 1);
  4952. }
  4953. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  4954. ocfs2_journal_dirty(handle, tl_bh);
  4955. bail:
  4956. mlog_exit(status);
  4957. return status;
  4958. }
  4959. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  4960. handle_t *handle,
  4961. struct inode *data_alloc_inode,
  4962. struct buffer_head *data_alloc_bh)
  4963. {
  4964. int status = 0;
  4965. int i;
  4966. unsigned int num_clusters;
  4967. u64 start_blk;
  4968. struct ocfs2_truncate_rec rec;
  4969. struct ocfs2_dinode *di;
  4970. struct ocfs2_truncate_log *tl;
  4971. struct inode *tl_inode = osb->osb_tl_inode;
  4972. struct buffer_head *tl_bh = osb->osb_tl_bh;
  4973. mlog_entry_void();
  4974. di = (struct ocfs2_dinode *) tl_bh->b_data;
  4975. tl = &di->id2.i_dealloc;
  4976. i = le16_to_cpu(tl->tl_used) - 1;
  4977. while (i >= 0) {
  4978. /* Caller has given us at least enough credits to
  4979. * update the truncate log dinode */
  4980. status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
  4981. OCFS2_JOURNAL_ACCESS_WRITE);
  4982. if (status < 0) {
  4983. mlog_errno(status);
  4984. goto bail;
  4985. }
  4986. tl->tl_used = cpu_to_le16(i);
  4987. ocfs2_journal_dirty(handle, tl_bh);
  4988. /* TODO: Perhaps we can calculate the bulk of the
  4989. * credits up front rather than extending like
  4990. * this. */
  4991. status = ocfs2_extend_trans(handle,
  4992. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  4993. if (status < 0) {
  4994. mlog_errno(status);
  4995. goto bail;
  4996. }
  4997. rec = tl->tl_recs[i];
  4998. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  4999. le32_to_cpu(rec.t_start));
  5000. num_clusters = le32_to_cpu(rec.t_clusters);
  5001. /* if start_blk is not set, we ignore the record as
  5002. * invalid. */
  5003. if (start_blk) {
  5004. mlog(0, "free record %d, start = %u, clusters = %u\n",
  5005. i, le32_to_cpu(rec.t_start), num_clusters);
  5006. status = ocfs2_free_clusters(handle, data_alloc_inode,
  5007. data_alloc_bh, start_blk,
  5008. num_clusters);
  5009. if (status < 0) {
  5010. mlog_errno(status);
  5011. goto bail;
  5012. }
  5013. }
  5014. i--;
  5015. }
  5016. bail:
  5017. mlog_exit(status);
  5018. return status;
  5019. }
  5020. /* Expects you to already be holding tl_inode->i_mutex */
  5021. int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  5022. {
  5023. int status;
  5024. unsigned int num_to_flush;
  5025. handle_t *handle;
  5026. struct inode *tl_inode = osb->osb_tl_inode;
  5027. struct inode *data_alloc_inode = NULL;
  5028. struct buffer_head *tl_bh = osb->osb_tl_bh;
  5029. struct buffer_head *data_alloc_bh = NULL;
  5030. struct ocfs2_dinode *di;
  5031. struct ocfs2_truncate_log *tl;
  5032. mlog_entry_void();
  5033. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  5034. di = (struct ocfs2_dinode *) tl_bh->b_data;
  5035. /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
  5036. * by the underlying call to ocfs2_read_inode_block(), so any
  5037. * corruption is a code bug */
  5038. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  5039. tl = &di->id2.i_dealloc;
  5040. num_to_flush = le16_to_cpu(tl->tl_used);
  5041. mlog(0, "Flush %u records from truncate log #%llu\n",
  5042. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  5043. if (!num_to_flush) {
  5044. status = 0;
  5045. goto out;
  5046. }
  5047. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  5048. GLOBAL_BITMAP_SYSTEM_INODE,
  5049. OCFS2_INVALID_SLOT);
  5050. if (!data_alloc_inode) {
  5051. status = -EINVAL;
  5052. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  5053. goto out;
  5054. }
  5055. mutex_lock(&data_alloc_inode->i_mutex);
  5056. status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
  5057. if (status < 0) {
  5058. mlog_errno(status);
  5059. goto out_mutex;
  5060. }
  5061. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5062. if (IS_ERR(handle)) {
  5063. status = PTR_ERR(handle);
  5064. mlog_errno(status);
  5065. goto out_unlock;
  5066. }
  5067. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  5068. data_alloc_bh);
  5069. if (status < 0)
  5070. mlog_errno(status);
  5071. ocfs2_commit_trans(osb, handle);
  5072. out_unlock:
  5073. brelse(data_alloc_bh);
  5074. ocfs2_inode_unlock(data_alloc_inode, 1);
  5075. out_mutex:
  5076. mutex_unlock(&data_alloc_inode->i_mutex);
  5077. iput(data_alloc_inode);
  5078. out:
  5079. mlog_exit(status);
  5080. return status;
  5081. }
  5082. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  5083. {
  5084. int status;
  5085. struct inode *tl_inode = osb->osb_tl_inode;
  5086. mutex_lock(&tl_inode->i_mutex);
  5087. status = __ocfs2_flush_truncate_log(osb);
  5088. mutex_unlock(&tl_inode->i_mutex);
  5089. return status;
  5090. }
  5091. static void ocfs2_truncate_log_worker(struct work_struct *work)
  5092. {
  5093. int status;
  5094. struct ocfs2_super *osb =
  5095. container_of(work, struct ocfs2_super,
  5096. osb_truncate_log_wq.work);
  5097. mlog_entry_void();
  5098. status = ocfs2_flush_truncate_log(osb);
  5099. if (status < 0)
  5100. mlog_errno(status);
  5101. else
  5102. ocfs2_init_steal_slots(osb);
  5103. mlog_exit(status);
  5104. }
  5105. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  5106. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  5107. int cancel)
  5108. {
  5109. if (osb->osb_tl_inode) {
  5110. /* We want to push off log flushes while truncates are
  5111. * still running. */
  5112. if (cancel)
  5113. cancel_delayed_work(&osb->osb_truncate_log_wq);
  5114. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  5115. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  5116. }
  5117. }
  5118. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  5119. int slot_num,
  5120. struct inode **tl_inode,
  5121. struct buffer_head **tl_bh)
  5122. {
  5123. int status;
  5124. struct inode *inode = NULL;
  5125. struct buffer_head *bh = NULL;
  5126. inode = ocfs2_get_system_file_inode(osb,
  5127. TRUNCATE_LOG_SYSTEM_INODE,
  5128. slot_num);
  5129. if (!inode) {
  5130. status = -EINVAL;
  5131. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  5132. goto bail;
  5133. }
  5134. status = ocfs2_read_inode_block(inode, &bh);
  5135. if (status < 0) {
  5136. iput(inode);
  5137. mlog_errno(status);
  5138. goto bail;
  5139. }
  5140. *tl_inode = inode;
  5141. *tl_bh = bh;
  5142. bail:
  5143. mlog_exit(status);
  5144. return status;
  5145. }
  5146. /* called during the 1st stage of node recovery. we stamp a clean
  5147. * truncate log and pass back a copy for processing later. if the
  5148. * truncate log does not require processing, a *tl_copy is set to
  5149. * NULL. */
  5150. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  5151. int slot_num,
  5152. struct ocfs2_dinode **tl_copy)
  5153. {
  5154. int status;
  5155. struct inode *tl_inode = NULL;
  5156. struct buffer_head *tl_bh = NULL;
  5157. struct ocfs2_dinode *di;
  5158. struct ocfs2_truncate_log *tl;
  5159. *tl_copy = NULL;
  5160. mlog(0, "recover truncate log from slot %d\n", slot_num);
  5161. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  5162. if (status < 0) {
  5163. mlog_errno(status);
  5164. goto bail;
  5165. }
  5166. di = (struct ocfs2_dinode *) tl_bh->b_data;
  5167. /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
  5168. * validated by the underlying call to ocfs2_read_inode_block(),
  5169. * so any corruption is a code bug */
  5170. BUG_ON(!OCFS2_IS_VALID_DINODE(di));
  5171. tl = &di->id2.i_dealloc;
  5172. if (le16_to_cpu(tl->tl_used)) {
  5173. mlog(0, "We'll have %u logs to recover\n",
  5174. le16_to_cpu(tl->tl_used));
  5175. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  5176. if (!(*tl_copy)) {
  5177. status = -ENOMEM;
  5178. mlog_errno(status);
  5179. goto bail;
  5180. }
  5181. /* Assuming the write-out below goes well, this copy
  5182. * will be passed back to recovery for processing. */
  5183. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  5184. /* All we need to do to clear the truncate log is set
  5185. * tl_used. */
  5186. tl->tl_used = 0;
  5187. ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
  5188. status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
  5189. if (status < 0) {
  5190. mlog_errno(status);
  5191. goto bail;
  5192. }
  5193. }
  5194. bail:
  5195. if (tl_inode)
  5196. iput(tl_inode);
  5197. brelse(tl_bh);
  5198. if (status < 0 && (*tl_copy)) {
  5199. kfree(*tl_copy);
  5200. *tl_copy = NULL;
  5201. }
  5202. mlog_exit(status);
  5203. return status;
  5204. }
  5205. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  5206. struct ocfs2_dinode *tl_copy)
  5207. {
  5208. int status = 0;
  5209. int i;
  5210. unsigned int clusters, num_recs, start_cluster;
  5211. u64 start_blk;
  5212. handle_t *handle;
  5213. struct inode *tl_inode = osb->osb_tl_inode;
  5214. struct ocfs2_truncate_log *tl;
  5215. mlog_entry_void();
  5216. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  5217. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  5218. return -EINVAL;
  5219. }
  5220. tl = &tl_copy->id2.i_dealloc;
  5221. num_recs = le16_to_cpu(tl->tl_used);
  5222. mlog(0, "cleanup %u records from %llu\n", num_recs,
  5223. (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
  5224. mutex_lock(&tl_inode->i_mutex);
  5225. for(i = 0; i < num_recs; i++) {
  5226. if (ocfs2_truncate_log_needs_flush(osb)) {
  5227. status = __ocfs2_flush_truncate_log(osb);
  5228. if (status < 0) {
  5229. mlog_errno(status);
  5230. goto bail_up;
  5231. }
  5232. }
  5233. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5234. if (IS_ERR(handle)) {
  5235. status = PTR_ERR(handle);
  5236. mlog_errno(status);
  5237. goto bail_up;
  5238. }
  5239. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  5240. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  5241. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  5242. status = ocfs2_truncate_log_append(osb, handle,
  5243. start_blk, clusters);
  5244. ocfs2_commit_trans(osb, handle);
  5245. if (status < 0) {
  5246. mlog_errno(status);
  5247. goto bail_up;
  5248. }
  5249. }
  5250. bail_up:
  5251. mutex_unlock(&tl_inode->i_mutex);
  5252. mlog_exit(status);
  5253. return status;
  5254. }
  5255. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  5256. {
  5257. int status;
  5258. struct inode *tl_inode = osb->osb_tl_inode;
  5259. mlog_entry_void();
  5260. if (tl_inode) {
  5261. cancel_delayed_work(&osb->osb_truncate_log_wq);
  5262. flush_workqueue(ocfs2_wq);
  5263. status = ocfs2_flush_truncate_log(osb);
  5264. if (status < 0)
  5265. mlog_errno(status);
  5266. brelse(osb->osb_tl_bh);
  5267. iput(osb->osb_tl_inode);
  5268. }
  5269. mlog_exit_void();
  5270. }
  5271. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  5272. {
  5273. int status;
  5274. struct inode *tl_inode = NULL;
  5275. struct buffer_head *tl_bh = NULL;
  5276. mlog_entry_void();
  5277. status = ocfs2_get_truncate_log_info(osb,
  5278. osb->slot_num,
  5279. &tl_inode,
  5280. &tl_bh);
  5281. if (status < 0)
  5282. mlog_errno(status);
  5283. /* ocfs2_truncate_log_shutdown keys on the existence of
  5284. * osb->osb_tl_inode so we don't set any of the osb variables
  5285. * until we're sure all is well. */
  5286. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  5287. ocfs2_truncate_log_worker);
  5288. osb->osb_tl_bh = tl_bh;
  5289. osb->osb_tl_inode = tl_inode;
  5290. mlog_exit(status);
  5291. return status;
  5292. }
  5293. /*
  5294. * Delayed de-allocation of suballocator blocks.
  5295. *
  5296. * Some sets of block de-allocations might involve multiple suballocator inodes.
  5297. *
  5298. * The locking for this can get extremely complicated, especially when
  5299. * the suballocator inodes to delete from aren't known until deep
  5300. * within an unrelated codepath.
  5301. *
  5302. * ocfs2_extent_block structures are a good example of this - an inode
  5303. * btree could have been grown by any number of nodes each allocating
  5304. * out of their own suballoc inode.
  5305. *
  5306. * These structures allow the delay of block de-allocation until a
  5307. * later time, when locking of multiple cluster inodes won't cause
  5308. * deadlock.
  5309. */
  5310. /*
  5311. * Describe a single bit freed from a suballocator. For the block
  5312. * suballocators, it represents one block. For the global cluster
  5313. * allocator, it represents some clusters and free_bit indicates
  5314. * clusters number.
  5315. */
  5316. struct ocfs2_cached_block_free {
  5317. struct ocfs2_cached_block_free *free_next;
  5318. u64 free_blk;
  5319. unsigned int free_bit;
  5320. };
  5321. struct ocfs2_per_slot_free_list {
  5322. struct ocfs2_per_slot_free_list *f_next_suballocator;
  5323. int f_inode_type;
  5324. int f_slot;
  5325. struct ocfs2_cached_block_free *f_first;
  5326. };
  5327. static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
  5328. int sysfile_type,
  5329. int slot,
  5330. struct ocfs2_cached_block_free *head)
  5331. {
  5332. int ret;
  5333. u64 bg_blkno;
  5334. handle_t *handle;
  5335. struct inode *inode;
  5336. struct buffer_head *di_bh = NULL;
  5337. struct ocfs2_cached_block_free *tmp;
  5338. inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
  5339. if (!inode) {
  5340. ret = -EINVAL;
  5341. mlog_errno(ret);
  5342. goto out;
  5343. }
  5344. mutex_lock(&inode->i_mutex);
  5345. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  5346. if (ret) {
  5347. mlog_errno(ret);
  5348. goto out_mutex;
  5349. }
  5350. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  5351. if (IS_ERR(handle)) {
  5352. ret = PTR_ERR(handle);
  5353. mlog_errno(ret);
  5354. goto out_unlock;
  5355. }
  5356. while (head) {
  5357. bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
  5358. head->free_bit);
  5359. mlog(0, "Free bit: (bit %u, blkno %llu)\n",
  5360. head->free_bit, (unsigned long long)head->free_blk);
  5361. ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
  5362. head->free_bit, bg_blkno, 1);
  5363. if (ret) {
  5364. mlog_errno(ret);
  5365. goto out_journal;
  5366. }
  5367. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  5368. if (ret) {
  5369. mlog_errno(ret);
  5370. goto out_journal;
  5371. }
  5372. tmp = head;
  5373. head = head->free_next;
  5374. kfree(tmp);
  5375. }
  5376. out_journal:
  5377. ocfs2_commit_trans(osb, handle);
  5378. out_unlock:
  5379. ocfs2_inode_unlock(inode, 1);
  5380. brelse(di_bh);
  5381. out_mutex:
  5382. mutex_unlock(&inode->i_mutex);
  5383. iput(inode);
  5384. out:
  5385. while(head) {
  5386. /* Premature exit may have left some dangling items. */
  5387. tmp = head;
  5388. head = head->free_next;
  5389. kfree(tmp);
  5390. }
  5391. return ret;
  5392. }
  5393. int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5394. u64 blkno, unsigned int bit)
  5395. {
  5396. int ret = 0;
  5397. struct ocfs2_cached_block_free *item;
  5398. item = kmalloc(sizeof(*item), GFP_NOFS);
  5399. if (item == NULL) {
  5400. ret = -ENOMEM;
  5401. mlog_errno(ret);
  5402. return ret;
  5403. }
  5404. mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
  5405. bit, (unsigned long long)blkno);
  5406. item->free_blk = blkno;
  5407. item->free_bit = bit;
  5408. item->free_next = ctxt->c_global_allocator;
  5409. ctxt->c_global_allocator = item;
  5410. return ret;
  5411. }
  5412. static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
  5413. struct ocfs2_cached_block_free *head)
  5414. {
  5415. struct ocfs2_cached_block_free *tmp;
  5416. struct inode *tl_inode = osb->osb_tl_inode;
  5417. handle_t *handle;
  5418. int ret = 0;
  5419. mutex_lock(&tl_inode->i_mutex);
  5420. while (head) {
  5421. if (ocfs2_truncate_log_needs_flush(osb)) {
  5422. ret = __ocfs2_flush_truncate_log(osb);
  5423. if (ret < 0) {
  5424. mlog_errno(ret);
  5425. break;
  5426. }
  5427. }
  5428. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  5429. if (IS_ERR(handle)) {
  5430. ret = PTR_ERR(handle);
  5431. mlog_errno(ret);
  5432. break;
  5433. }
  5434. ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
  5435. head->free_bit);
  5436. ocfs2_commit_trans(osb, handle);
  5437. tmp = head;
  5438. head = head->free_next;
  5439. kfree(tmp);
  5440. if (ret < 0) {
  5441. mlog_errno(ret);
  5442. break;
  5443. }
  5444. }
  5445. mutex_unlock(&tl_inode->i_mutex);
  5446. while (head) {
  5447. /* Premature exit may have left some dangling items. */
  5448. tmp = head;
  5449. head = head->free_next;
  5450. kfree(tmp);
  5451. }
  5452. return ret;
  5453. }
  5454. int ocfs2_run_deallocs(struct ocfs2_super *osb,
  5455. struct ocfs2_cached_dealloc_ctxt *ctxt)
  5456. {
  5457. int ret = 0, ret2;
  5458. struct ocfs2_per_slot_free_list *fl;
  5459. if (!ctxt)
  5460. return 0;
  5461. while (ctxt->c_first_suballocator) {
  5462. fl = ctxt->c_first_suballocator;
  5463. if (fl->f_first) {
  5464. mlog(0, "Free items: (type %u, slot %d)\n",
  5465. fl->f_inode_type, fl->f_slot);
  5466. ret2 = ocfs2_free_cached_blocks(osb,
  5467. fl->f_inode_type,
  5468. fl->f_slot,
  5469. fl->f_first);
  5470. if (ret2)
  5471. mlog_errno(ret2);
  5472. if (!ret)
  5473. ret = ret2;
  5474. }
  5475. ctxt->c_first_suballocator = fl->f_next_suballocator;
  5476. kfree(fl);
  5477. }
  5478. if (ctxt->c_global_allocator) {
  5479. ret2 = ocfs2_free_cached_clusters(osb,
  5480. ctxt->c_global_allocator);
  5481. if (ret2)
  5482. mlog_errno(ret2);
  5483. if (!ret)
  5484. ret = ret2;
  5485. ctxt->c_global_allocator = NULL;
  5486. }
  5487. return ret;
  5488. }
  5489. static struct ocfs2_per_slot_free_list *
  5490. ocfs2_find_per_slot_free_list(int type,
  5491. int slot,
  5492. struct ocfs2_cached_dealloc_ctxt *ctxt)
  5493. {
  5494. struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
  5495. while (fl) {
  5496. if (fl->f_inode_type == type && fl->f_slot == slot)
  5497. return fl;
  5498. fl = fl->f_next_suballocator;
  5499. }
  5500. fl = kmalloc(sizeof(*fl), GFP_NOFS);
  5501. if (fl) {
  5502. fl->f_inode_type = type;
  5503. fl->f_slot = slot;
  5504. fl->f_first = NULL;
  5505. fl->f_next_suballocator = ctxt->c_first_suballocator;
  5506. ctxt->c_first_suballocator = fl;
  5507. }
  5508. return fl;
  5509. }
  5510. int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5511. int type, int slot, u64 blkno,
  5512. unsigned int bit)
  5513. {
  5514. int ret;
  5515. struct ocfs2_per_slot_free_list *fl;
  5516. struct ocfs2_cached_block_free *item;
  5517. fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
  5518. if (fl == NULL) {
  5519. ret = -ENOMEM;
  5520. mlog_errno(ret);
  5521. goto out;
  5522. }
  5523. item = kmalloc(sizeof(*item), GFP_NOFS);
  5524. if (item == NULL) {
  5525. ret = -ENOMEM;
  5526. mlog_errno(ret);
  5527. goto out;
  5528. }
  5529. mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
  5530. type, slot, bit, (unsigned long long)blkno);
  5531. item->free_blk = blkno;
  5532. item->free_bit = bit;
  5533. item->free_next = fl->f_first;
  5534. fl->f_first = item;
  5535. ret = 0;
  5536. out:
  5537. return ret;
  5538. }
  5539. static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
  5540. struct ocfs2_extent_block *eb)
  5541. {
  5542. return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
  5543. le16_to_cpu(eb->h_suballoc_slot),
  5544. le64_to_cpu(eb->h_blkno),
  5545. le16_to_cpu(eb->h_suballoc_bit));
  5546. }
  5547. /* This function will figure out whether the currently last extent
  5548. * block will be deleted, and if it will, what the new last extent
  5549. * block will be so we can update his h_next_leaf_blk field, as well
  5550. * as the dinodes i_last_eb_blk */
  5551. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  5552. unsigned int clusters_to_del,
  5553. struct ocfs2_path *path,
  5554. struct buffer_head **new_last_eb)
  5555. {
  5556. int next_free, ret = 0;
  5557. u32 cpos;
  5558. struct ocfs2_extent_rec *rec;
  5559. struct ocfs2_extent_block *eb;
  5560. struct ocfs2_extent_list *el;
  5561. struct buffer_head *bh = NULL;
  5562. *new_last_eb = NULL;
  5563. /* we have no tree, so of course, no last_eb. */
  5564. if (!path->p_tree_depth)
  5565. goto out;
  5566. /* trunc to zero special case - this makes tree_depth = 0
  5567. * regardless of what it is. */
  5568. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  5569. goto out;
  5570. el = path_leaf_el(path);
  5571. BUG_ON(!el->l_next_free_rec);
  5572. /*
  5573. * Make sure that this extent list will actually be empty
  5574. * after we clear away the data. We can shortcut out if
  5575. * there's more than one non-empty extent in the
  5576. * list. Otherwise, a check of the remaining extent is
  5577. * necessary.
  5578. */
  5579. next_free = le16_to_cpu(el->l_next_free_rec);
  5580. rec = NULL;
  5581. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  5582. if (next_free > 2)
  5583. goto out;
  5584. /* We may have a valid extent in index 1, check it. */
  5585. if (next_free == 2)
  5586. rec = &el->l_recs[1];
  5587. /*
  5588. * Fall through - no more nonempty extents, so we want
  5589. * to delete this leaf.
  5590. */
  5591. } else {
  5592. if (next_free > 1)
  5593. goto out;
  5594. rec = &el->l_recs[0];
  5595. }
  5596. if (rec) {
  5597. /*
  5598. * Check it we'll only be trimming off the end of this
  5599. * cluster.
  5600. */
  5601. if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
  5602. goto out;
  5603. }
  5604. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  5605. if (ret) {
  5606. mlog_errno(ret);
  5607. goto out;
  5608. }
  5609. ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
  5610. if (ret) {
  5611. mlog_errno(ret);
  5612. goto out;
  5613. }
  5614. eb = (struct ocfs2_extent_block *) bh->b_data;
  5615. el = &eb->h_list;
  5616. /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
  5617. * Any corruption is a code bug. */
  5618. BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
  5619. *new_last_eb = bh;
  5620. get_bh(*new_last_eb);
  5621. mlog(0, "returning block %llu, (cpos: %u)\n",
  5622. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  5623. out:
  5624. brelse(bh);
  5625. return ret;
  5626. }
  5627. /*
  5628. * Trim some clusters off the rightmost edge of a tree. Only called
  5629. * during truncate.
  5630. *
  5631. * The caller needs to:
  5632. * - start journaling of each path component.
  5633. * - compute and fully set up any new last ext block
  5634. */
  5635. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  5636. handle_t *handle, struct ocfs2_truncate_context *tc,
  5637. u32 clusters_to_del, u64 *delete_start, u8 *flags)
  5638. {
  5639. int ret, i, index = path->p_tree_depth;
  5640. u32 new_edge = 0;
  5641. u64 deleted_eb = 0;
  5642. struct buffer_head *bh;
  5643. struct ocfs2_extent_list *el;
  5644. struct ocfs2_extent_rec *rec;
  5645. *delete_start = 0;
  5646. *flags = 0;
  5647. while (index >= 0) {
  5648. bh = path->p_node[index].bh;
  5649. el = path->p_node[index].el;
  5650. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  5651. index, (unsigned long long)bh->b_blocknr);
  5652. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  5653. if (index !=
  5654. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  5655. ocfs2_error(inode->i_sb,
  5656. "Inode %lu has invalid ext. block %llu",
  5657. inode->i_ino,
  5658. (unsigned long long)bh->b_blocknr);
  5659. ret = -EROFS;
  5660. goto out;
  5661. }
  5662. find_tail_record:
  5663. i = le16_to_cpu(el->l_next_free_rec) - 1;
  5664. rec = &el->l_recs[i];
  5665. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  5666. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  5667. ocfs2_rec_clusters(el, rec),
  5668. (unsigned long long)le64_to_cpu(rec->e_blkno),
  5669. le16_to_cpu(el->l_next_free_rec));
  5670. BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
  5671. if (le16_to_cpu(el->l_tree_depth) == 0) {
  5672. /*
  5673. * If the leaf block contains a single empty
  5674. * extent and no records, we can just remove
  5675. * the block.
  5676. */
  5677. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  5678. memset(rec, 0,
  5679. sizeof(struct ocfs2_extent_rec));
  5680. el->l_next_free_rec = cpu_to_le16(0);
  5681. goto delete;
  5682. }
  5683. /*
  5684. * Remove any empty extents by shifting things
  5685. * left. That should make life much easier on
  5686. * the code below. This condition is rare
  5687. * enough that we shouldn't see a performance
  5688. * hit.
  5689. */
  5690. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  5691. le16_add_cpu(&el->l_next_free_rec, -1);
  5692. for(i = 0;
  5693. i < le16_to_cpu(el->l_next_free_rec); i++)
  5694. el->l_recs[i] = el->l_recs[i + 1];
  5695. memset(&el->l_recs[i], 0,
  5696. sizeof(struct ocfs2_extent_rec));
  5697. /*
  5698. * We've modified our extent list. The
  5699. * simplest way to handle this change
  5700. * is to being the search from the
  5701. * start again.
  5702. */
  5703. goto find_tail_record;
  5704. }
  5705. le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
  5706. /*
  5707. * We'll use "new_edge" on our way back up the
  5708. * tree to know what our rightmost cpos is.
  5709. */
  5710. new_edge = le16_to_cpu(rec->e_leaf_clusters);
  5711. new_edge += le32_to_cpu(rec->e_cpos);
  5712. /*
  5713. * The caller will use this to delete data blocks.
  5714. */
  5715. *delete_start = le64_to_cpu(rec->e_blkno)
  5716. + ocfs2_clusters_to_blocks(inode->i_sb,
  5717. le16_to_cpu(rec->e_leaf_clusters));
  5718. *flags = rec->e_flags;
  5719. /*
  5720. * If it's now empty, remove this record.
  5721. */
  5722. if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
  5723. memset(rec, 0,
  5724. sizeof(struct ocfs2_extent_rec));
  5725. le16_add_cpu(&el->l_next_free_rec, -1);
  5726. }
  5727. } else {
  5728. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  5729. memset(rec, 0,
  5730. sizeof(struct ocfs2_extent_rec));
  5731. le16_add_cpu(&el->l_next_free_rec, -1);
  5732. goto delete;
  5733. }
  5734. /* Can this actually happen? */
  5735. if (le16_to_cpu(el->l_next_free_rec) == 0)
  5736. goto delete;
  5737. /*
  5738. * We never actually deleted any clusters
  5739. * because our leaf was empty. There's no
  5740. * reason to adjust the rightmost edge then.
  5741. */
  5742. if (new_edge == 0)
  5743. goto delete;
  5744. rec->e_int_clusters = cpu_to_le32(new_edge);
  5745. le32_add_cpu(&rec->e_int_clusters,
  5746. -le32_to_cpu(rec->e_cpos));
  5747. /*
  5748. * A deleted child record should have been
  5749. * caught above.
  5750. */
  5751. BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
  5752. }
  5753. delete:
  5754. ocfs2_journal_dirty(handle, bh);
  5755. mlog(0, "extent list container %llu, after: record %d: "
  5756. "(%u, %u, %llu), next = %u.\n",
  5757. (unsigned long long)bh->b_blocknr, i,
  5758. le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
  5759. (unsigned long long)le64_to_cpu(rec->e_blkno),
  5760. le16_to_cpu(el->l_next_free_rec));
  5761. /*
  5762. * We must be careful to only attempt delete of an
  5763. * extent block (and not the root inode block).
  5764. */
  5765. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  5766. struct ocfs2_extent_block *eb =
  5767. (struct ocfs2_extent_block *)bh->b_data;
  5768. /*
  5769. * Save this for use when processing the
  5770. * parent block.
  5771. */
  5772. deleted_eb = le64_to_cpu(eb->h_blkno);
  5773. mlog(0, "deleting this extent block.\n");
  5774. ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
  5775. BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
  5776. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  5777. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  5778. ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
  5779. /* An error here is not fatal. */
  5780. if (ret < 0)
  5781. mlog_errno(ret);
  5782. } else {
  5783. deleted_eb = 0;
  5784. }
  5785. index--;
  5786. }
  5787. ret = 0;
  5788. out:
  5789. return ret;
  5790. }
  5791. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  5792. unsigned int clusters_to_del,
  5793. struct inode *inode,
  5794. struct buffer_head *fe_bh,
  5795. handle_t *handle,
  5796. struct ocfs2_truncate_context *tc,
  5797. struct ocfs2_path *path,
  5798. struct ocfs2_alloc_context *meta_ac)
  5799. {
  5800. int status;
  5801. struct ocfs2_dinode *fe;
  5802. struct ocfs2_extent_block *last_eb = NULL;
  5803. struct ocfs2_extent_list *el;
  5804. struct buffer_head *last_eb_bh = NULL;
  5805. u64 delete_blk = 0;
  5806. u8 rec_flags;
  5807. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  5808. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  5809. path, &last_eb_bh);
  5810. if (status < 0) {
  5811. mlog_errno(status);
  5812. goto bail;
  5813. }
  5814. /*
  5815. * Each component will be touched, so we might as well journal
  5816. * here to avoid having to handle errors later.
  5817. */
  5818. status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
  5819. if (status < 0) {
  5820. mlog_errno(status);
  5821. goto bail;
  5822. }
  5823. if (last_eb_bh) {
  5824. status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
  5825. OCFS2_JOURNAL_ACCESS_WRITE);
  5826. if (status < 0) {
  5827. mlog_errno(status);
  5828. goto bail;
  5829. }
  5830. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  5831. }
  5832. el = &(fe->id2.i_list);
  5833. /*
  5834. * Lower levels depend on this never happening, but it's best
  5835. * to check it up here before changing the tree.
  5836. */
  5837. if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
  5838. ocfs2_error(inode->i_sb,
  5839. "Inode %lu has an empty extent record, depth %u\n",
  5840. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  5841. status = -EROFS;
  5842. goto bail;
  5843. }
  5844. dquot_free_space_nodirty(inode,
  5845. ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
  5846. spin_lock(&OCFS2_I(inode)->ip_lock);
  5847. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  5848. clusters_to_del;
  5849. spin_unlock(&OCFS2_I(inode)->ip_lock);
  5850. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  5851. inode->i_blocks = ocfs2_inode_sector_count(inode);
  5852. status = ocfs2_trim_tree(inode, path, handle, tc,
  5853. clusters_to_del, &delete_blk, &rec_flags);
  5854. if (status) {
  5855. mlog_errno(status);
  5856. goto bail;
  5857. }
  5858. if (le32_to_cpu(fe->i_clusters) == 0) {
  5859. /* trunc to zero is a special case. */
  5860. el->l_tree_depth = 0;
  5861. fe->i_last_eb_blk = 0;
  5862. } else if (last_eb)
  5863. fe->i_last_eb_blk = last_eb->h_blkno;
  5864. ocfs2_journal_dirty(handle, fe_bh);
  5865. if (last_eb) {
  5866. /* If there will be a new last extent block, then by
  5867. * definition, there cannot be any leaves to the right of
  5868. * him. */
  5869. last_eb->h_next_leaf_blk = 0;
  5870. ocfs2_journal_dirty(handle, last_eb_bh);
  5871. }
  5872. if (delete_blk) {
  5873. if (rec_flags & OCFS2_EXT_REFCOUNTED)
  5874. status = ocfs2_decrease_refcount(inode, handle,
  5875. ocfs2_blocks_to_clusters(osb->sb,
  5876. delete_blk),
  5877. clusters_to_del, meta_ac,
  5878. &tc->tc_dealloc, 1);
  5879. else
  5880. status = ocfs2_truncate_log_append(osb, handle,
  5881. delete_blk,
  5882. clusters_to_del);
  5883. if (status < 0) {
  5884. mlog_errno(status);
  5885. goto bail;
  5886. }
  5887. }
  5888. status = 0;
  5889. bail:
  5890. brelse(last_eb_bh);
  5891. mlog_exit(status);
  5892. return status;
  5893. }
  5894. static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
  5895. {
  5896. set_buffer_uptodate(bh);
  5897. mark_buffer_dirty(bh);
  5898. return 0;
  5899. }
  5900. void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
  5901. unsigned int from, unsigned int to,
  5902. struct page *page, int zero, u64 *phys)
  5903. {
  5904. int ret, partial = 0;
  5905. ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
  5906. if (ret)
  5907. mlog_errno(ret);
  5908. if (zero)
  5909. zero_user_segment(page, from, to);
  5910. /*
  5911. * Need to set the buffers we zero'd into uptodate
  5912. * here if they aren't - ocfs2_map_page_blocks()
  5913. * might've skipped some
  5914. */
  5915. ret = walk_page_buffers(handle, page_buffers(page),
  5916. from, to, &partial,
  5917. ocfs2_zero_func);
  5918. if (ret < 0)
  5919. mlog_errno(ret);
  5920. else if (ocfs2_should_order_data(inode)) {
  5921. ret = ocfs2_jbd2_file_inode(handle, inode);
  5922. if (ret < 0)
  5923. mlog_errno(ret);
  5924. }
  5925. if (!partial)
  5926. SetPageUptodate(page);
  5927. flush_dcache_page(page);
  5928. }
  5929. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
  5930. loff_t end, struct page **pages,
  5931. int numpages, u64 phys, handle_t *handle)
  5932. {
  5933. int i;
  5934. struct page *page;
  5935. unsigned int from, to = PAGE_CACHE_SIZE;
  5936. struct super_block *sb = inode->i_sb;
  5937. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  5938. if (numpages == 0)
  5939. goto out;
  5940. to = PAGE_CACHE_SIZE;
  5941. for(i = 0; i < numpages; i++) {
  5942. page = pages[i];
  5943. from = start & (PAGE_CACHE_SIZE - 1);
  5944. if ((end >> PAGE_CACHE_SHIFT) == page->index)
  5945. to = end & (PAGE_CACHE_SIZE - 1);
  5946. BUG_ON(from > PAGE_CACHE_SIZE);
  5947. BUG_ON(to > PAGE_CACHE_SIZE);
  5948. ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
  5949. &phys);
  5950. start = (page->index + 1) << PAGE_CACHE_SHIFT;
  5951. }
  5952. out:
  5953. if (pages)
  5954. ocfs2_unlock_and_free_pages(pages, numpages);
  5955. }
  5956. int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
  5957. struct page **pages, int *num)
  5958. {
  5959. int numpages, ret = 0;
  5960. struct address_space *mapping = inode->i_mapping;
  5961. unsigned long index;
  5962. loff_t last_page_bytes;
  5963. BUG_ON(start > end);
  5964. numpages = 0;
  5965. last_page_bytes = PAGE_ALIGN(end);
  5966. index = start >> PAGE_CACHE_SHIFT;
  5967. do {
  5968. pages[numpages] = grab_cache_page(mapping, index);
  5969. if (!pages[numpages]) {
  5970. ret = -ENOMEM;
  5971. mlog_errno(ret);
  5972. goto out;
  5973. }
  5974. numpages++;
  5975. index++;
  5976. } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
  5977. out:
  5978. if (ret != 0) {
  5979. if (pages)
  5980. ocfs2_unlock_and_free_pages(pages, numpages);
  5981. numpages = 0;
  5982. }
  5983. *num = numpages;
  5984. return ret;
  5985. }
  5986. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
  5987. struct page **pages, int *num)
  5988. {
  5989. struct super_block *sb = inode->i_sb;
  5990. BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
  5991. (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
  5992. return ocfs2_grab_pages(inode, start, end, pages, num);
  5993. }
  5994. /*
  5995. * Zero the area past i_size but still within an allocated
  5996. * cluster. This avoids exposing nonzero data on subsequent file
  5997. * extends.
  5998. *
  5999. * We need to call this before i_size is updated on the inode because
  6000. * otherwise block_write_full_page() will skip writeout of pages past
  6001. * i_size. The new_i_size parameter is passed for this reason.
  6002. */
  6003. int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
  6004. u64 range_start, u64 range_end)
  6005. {
  6006. int ret = 0, numpages;
  6007. struct page **pages = NULL;
  6008. u64 phys;
  6009. unsigned int ext_flags;
  6010. struct super_block *sb = inode->i_sb;
  6011. /*
  6012. * File systems which don't support sparse files zero on every
  6013. * extend.
  6014. */
  6015. if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
  6016. return 0;
  6017. pages = kcalloc(ocfs2_pages_per_cluster(sb),
  6018. sizeof(struct page *), GFP_NOFS);
  6019. if (pages == NULL) {
  6020. ret = -ENOMEM;
  6021. mlog_errno(ret);
  6022. goto out;
  6023. }
  6024. if (range_start == range_end)
  6025. goto out;
  6026. ret = ocfs2_extent_map_get_blocks(inode,
  6027. range_start >> sb->s_blocksize_bits,
  6028. &phys, NULL, &ext_flags);
  6029. if (ret) {
  6030. mlog_errno(ret);
  6031. goto out;
  6032. }
  6033. /*
  6034. * Tail is a hole, or is marked unwritten. In either case, we
  6035. * can count on read and write to return/push zero's.
  6036. */
  6037. if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
  6038. goto out;
  6039. ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
  6040. &numpages);
  6041. if (ret) {
  6042. mlog_errno(ret);
  6043. goto out;
  6044. }
  6045. ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
  6046. numpages, phys, handle);
  6047. /*
  6048. * Initiate writeout of the pages we zero'd here. We don't
  6049. * wait on them - the truncate_inode_pages() call later will
  6050. * do that for us.
  6051. */
  6052. ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
  6053. range_end - 1);
  6054. if (ret)
  6055. mlog_errno(ret);
  6056. out:
  6057. if (pages)
  6058. kfree(pages);
  6059. return ret;
  6060. }
  6061. static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
  6062. struct ocfs2_dinode *di)
  6063. {
  6064. unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
  6065. unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
  6066. if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
  6067. memset(&di->id2, 0, blocksize -
  6068. offsetof(struct ocfs2_dinode, id2) -
  6069. xattrsize);
  6070. else
  6071. memset(&di->id2, 0, blocksize -
  6072. offsetof(struct ocfs2_dinode, id2));
  6073. }
  6074. void ocfs2_dinode_new_extent_list(struct inode *inode,
  6075. struct ocfs2_dinode *di)
  6076. {
  6077. ocfs2_zero_dinode_id2_with_xattr(inode, di);
  6078. di->id2.i_list.l_tree_depth = 0;
  6079. di->id2.i_list.l_next_free_rec = 0;
  6080. di->id2.i_list.l_count = cpu_to_le16(
  6081. ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
  6082. }
  6083. void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
  6084. {
  6085. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  6086. struct ocfs2_inline_data *idata = &di->id2.i_data;
  6087. spin_lock(&oi->ip_lock);
  6088. oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
  6089. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  6090. spin_unlock(&oi->ip_lock);
  6091. /*
  6092. * We clear the entire i_data structure here so that all
  6093. * fields can be properly initialized.
  6094. */
  6095. ocfs2_zero_dinode_id2_with_xattr(inode, di);
  6096. idata->id_count = cpu_to_le16(
  6097. ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
  6098. }
  6099. int ocfs2_convert_inline_data_to_extents(struct inode *inode,
  6100. struct buffer_head *di_bh)
  6101. {
  6102. int ret, i, has_data, num_pages = 0;
  6103. handle_t *handle;
  6104. u64 uninitialized_var(block);
  6105. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  6106. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  6107. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  6108. struct ocfs2_alloc_context *data_ac = NULL;
  6109. struct page **pages = NULL;
  6110. loff_t end = osb->s_clustersize;
  6111. struct ocfs2_extent_tree et;
  6112. int did_quota = 0;
  6113. has_data = i_size_read(inode) ? 1 : 0;
  6114. if (has_data) {
  6115. pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
  6116. sizeof(struct page *), GFP_NOFS);
  6117. if (pages == NULL) {
  6118. ret = -ENOMEM;
  6119. mlog_errno(ret);
  6120. goto out;
  6121. }
  6122. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  6123. if (ret) {
  6124. mlog_errno(ret);
  6125. goto out;
  6126. }
  6127. }
  6128. handle = ocfs2_start_trans(osb,
  6129. ocfs2_inline_to_extents_credits(osb->sb));
  6130. if (IS_ERR(handle)) {
  6131. ret = PTR_ERR(handle);
  6132. mlog_errno(ret);
  6133. goto out_unlock;
  6134. }
  6135. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  6136. OCFS2_JOURNAL_ACCESS_WRITE);
  6137. if (ret) {
  6138. mlog_errno(ret);
  6139. goto out_commit;
  6140. }
  6141. if (has_data) {
  6142. u32 bit_off, num;
  6143. unsigned int page_end;
  6144. u64 phys;
  6145. ret = dquot_alloc_space_nodirty(inode,
  6146. ocfs2_clusters_to_bytes(osb->sb, 1));
  6147. if (ret)
  6148. goto out_commit;
  6149. did_quota = 1;
  6150. data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
  6151. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
  6152. &num);
  6153. if (ret) {
  6154. mlog_errno(ret);
  6155. goto out_commit;
  6156. }
  6157. /*
  6158. * Save two copies, one for insert, and one that can
  6159. * be changed by ocfs2_map_and_dirty_page() below.
  6160. */
  6161. block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  6162. /*
  6163. * Non sparse file systems zero on extend, so no need
  6164. * to do that now.
  6165. */
  6166. if (!ocfs2_sparse_alloc(osb) &&
  6167. PAGE_CACHE_SIZE < osb->s_clustersize)
  6168. end = PAGE_CACHE_SIZE;
  6169. ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
  6170. if (ret) {
  6171. mlog_errno(ret);
  6172. goto out_commit;
  6173. }
  6174. /*
  6175. * This should populate the 1st page for us and mark
  6176. * it up to date.
  6177. */
  6178. ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
  6179. if (ret) {
  6180. mlog_errno(ret);
  6181. goto out_commit;
  6182. }
  6183. page_end = PAGE_CACHE_SIZE;
  6184. if (PAGE_CACHE_SIZE > osb->s_clustersize)
  6185. page_end = osb->s_clustersize;
  6186. for (i = 0; i < num_pages; i++)
  6187. ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
  6188. pages[i], i > 0, &phys);
  6189. }
  6190. spin_lock(&oi->ip_lock);
  6191. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  6192. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  6193. spin_unlock(&oi->ip_lock);
  6194. ocfs2_dinode_new_extent_list(inode, di);
  6195. ocfs2_journal_dirty(handle, di_bh);
  6196. if (has_data) {
  6197. /*
  6198. * An error at this point should be extremely rare. If
  6199. * this proves to be false, we could always re-build
  6200. * the in-inode data from our pages.
  6201. */
  6202. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  6203. ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
  6204. if (ret) {
  6205. mlog_errno(ret);
  6206. goto out_commit;
  6207. }
  6208. inode->i_blocks = ocfs2_inode_sector_count(inode);
  6209. }
  6210. out_commit:
  6211. if (ret < 0 && did_quota)
  6212. dquot_free_space_nodirty(inode,
  6213. ocfs2_clusters_to_bytes(osb->sb, 1));
  6214. ocfs2_commit_trans(osb, handle);
  6215. out_unlock:
  6216. if (data_ac)
  6217. ocfs2_free_alloc_context(data_ac);
  6218. out:
  6219. if (pages) {
  6220. ocfs2_unlock_and_free_pages(pages, num_pages);
  6221. kfree(pages);
  6222. }
  6223. return ret;
  6224. }
  6225. /*
  6226. * It is expected, that by the time you call this function,
  6227. * inode->i_size and fe->i_size have been adjusted.
  6228. *
  6229. * WARNING: This will kfree the truncate context
  6230. */
  6231. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  6232. struct inode *inode,
  6233. struct buffer_head *fe_bh,
  6234. struct ocfs2_truncate_context *tc)
  6235. {
  6236. int status, i, credits, tl_sem = 0;
  6237. u32 clusters_to_del, new_highest_cpos, range;
  6238. u64 blkno = 0;
  6239. struct ocfs2_extent_list *el;
  6240. handle_t *handle = NULL;
  6241. struct inode *tl_inode = osb->osb_tl_inode;
  6242. struct ocfs2_path *path = NULL;
  6243. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  6244. struct ocfs2_alloc_context *meta_ac = NULL;
  6245. struct ocfs2_refcount_tree *ref_tree = NULL;
  6246. mlog_entry_void();
  6247. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  6248. i_size_read(inode));
  6249. path = ocfs2_new_path(fe_bh, &di->id2.i_list,
  6250. ocfs2_journal_access_di);
  6251. if (!path) {
  6252. status = -ENOMEM;
  6253. mlog_errno(status);
  6254. goto bail;
  6255. }
  6256. ocfs2_extent_map_trunc(inode, new_highest_cpos);
  6257. start:
  6258. /*
  6259. * Check that we still have allocation to delete.
  6260. */
  6261. if (OCFS2_I(inode)->ip_clusters == 0) {
  6262. status = 0;
  6263. goto bail;
  6264. }
  6265. credits = 0;
  6266. /*
  6267. * Truncate always works against the rightmost tree branch.
  6268. */
  6269. status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
  6270. if (status) {
  6271. mlog_errno(status);
  6272. goto bail;
  6273. }
  6274. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  6275. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  6276. /*
  6277. * By now, el will point to the extent list on the bottom most
  6278. * portion of this tree. Only the tail record is considered in
  6279. * each pass.
  6280. *
  6281. * We handle the following cases, in order:
  6282. * - empty extent: delete the remaining branch
  6283. * - remove the entire record
  6284. * - remove a partial record
  6285. * - no record needs to be removed (truncate has completed)
  6286. */
  6287. el = path_leaf_el(path);
  6288. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  6289. ocfs2_error(inode->i_sb,
  6290. "Inode %llu has empty extent block at %llu\n",
  6291. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  6292. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  6293. status = -EROFS;
  6294. goto bail;
  6295. }
  6296. i = le16_to_cpu(el->l_next_free_rec) - 1;
  6297. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  6298. ocfs2_rec_clusters(el, &el->l_recs[i]);
  6299. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  6300. clusters_to_del = 0;
  6301. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  6302. clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
  6303. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  6304. } else if (range > new_highest_cpos) {
  6305. clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
  6306. le32_to_cpu(el->l_recs[i].e_cpos)) -
  6307. new_highest_cpos;
  6308. blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
  6309. ocfs2_clusters_to_blocks(inode->i_sb,
  6310. ocfs2_rec_clusters(el, &el->l_recs[i]) -
  6311. clusters_to_del);
  6312. } else {
  6313. status = 0;
  6314. goto bail;
  6315. }
  6316. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  6317. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  6318. if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
  6319. BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
  6320. OCFS2_HAS_REFCOUNT_FL));
  6321. status = ocfs2_lock_refcount_tree(osb,
  6322. le64_to_cpu(di->i_refcount_loc),
  6323. 1, &ref_tree, NULL);
  6324. if (status) {
  6325. mlog_errno(status);
  6326. goto bail;
  6327. }
  6328. status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
  6329. blkno,
  6330. clusters_to_del,
  6331. &credits,
  6332. &meta_ac);
  6333. if (status < 0) {
  6334. mlog_errno(status);
  6335. goto bail;
  6336. }
  6337. }
  6338. mutex_lock(&tl_inode->i_mutex);
  6339. tl_sem = 1;
  6340. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  6341. * record is free for use. If there isn't any, we flush to get
  6342. * an empty truncate log. */
  6343. if (ocfs2_truncate_log_needs_flush(osb)) {
  6344. status = __ocfs2_flush_truncate_log(osb);
  6345. if (status < 0) {
  6346. mlog_errno(status);
  6347. goto bail;
  6348. }
  6349. }
  6350. credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  6351. (struct ocfs2_dinode *)fe_bh->b_data,
  6352. el);
  6353. handle = ocfs2_start_trans(osb, credits);
  6354. if (IS_ERR(handle)) {
  6355. status = PTR_ERR(handle);
  6356. handle = NULL;
  6357. mlog_errno(status);
  6358. goto bail;
  6359. }
  6360. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  6361. tc, path, meta_ac);
  6362. if (status < 0) {
  6363. mlog_errno(status);
  6364. goto bail;
  6365. }
  6366. mutex_unlock(&tl_inode->i_mutex);
  6367. tl_sem = 0;
  6368. ocfs2_commit_trans(osb, handle);
  6369. handle = NULL;
  6370. ocfs2_reinit_path(path, 1);
  6371. if (meta_ac) {
  6372. ocfs2_free_alloc_context(meta_ac);
  6373. meta_ac = NULL;
  6374. }
  6375. if (ref_tree) {
  6376. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  6377. ref_tree = NULL;
  6378. }
  6379. /*
  6380. * The check above will catch the case where we've truncated
  6381. * away all allocation.
  6382. */
  6383. goto start;
  6384. bail:
  6385. ocfs2_schedule_truncate_log_flush(osb, 1);
  6386. if (tl_sem)
  6387. mutex_unlock(&tl_inode->i_mutex);
  6388. if (handle)
  6389. ocfs2_commit_trans(osb, handle);
  6390. if (meta_ac)
  6391. ocfs2_free_alloc_context(meta_ac);
  6392. if (ref_tree)
  6393. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  6394. ocfs2_run_deallocs(osb, &tc->tc_dealloc);
  6395. ocfs2_free_path(path);
  6396. /* This will drop the ext_alloc cluster lock for us */
  6397. ocfs2_free_truncate_context(tc);
  6398. mlog_exit(status);
  6399. return status;
  6400. }
  6401. /*
  6402. * Expects the inode to already be locked.
  6403. */
  6404. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  6405. struct inode *inode,
  6406. struct buffer_head *fe_bh,
  6407. struct ocfs2_truncate_context **tc)
  6408. {
  6409. int status;
  6410. unsigned int new_i_clusters;
  6411. struct ocfs2_dinode *fe;
  6412. struct ocfs2_extent_block *eb;
  6413. struct buffer_head *last_eb_bh = NULL;
  6414. mlog_entry_void();
  6415. *tc = NULL;
  6416. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  6417. i_size_read(inode));
  6418. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  6419. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  6420. "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
  6421. (unsigned long long)le64_to_cpu(fe->i_size));
  6422. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  6423. if (!(*tc)) {
  6424. status = -ENOMEM;
  6425. mlog_errno(status);
  6426. goto bail;
  6427. }
  6428. ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
  6429. if (fe->id2.i_list.l_tree_depth) {
  6430. status = ocfs2_read_extent_block(INODE_CACHE(inode),
  6431. le64_to_cpu(fe->i_last_eb_blk),
  6432. &last_eb_bh);
  6433. if (status < 0) {
  6434. mlog_errno(status);
  6435. goto bail;
  6436. }
  6437. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  6438. }
  6439. (*tc)->tc_last_eb_bh = last_eb_bh;
  6440. status = 0;
  6441. bail:
  6442. if (status < 0) {
  6443. if (*tc)
  6444. ocfs2_free_truncate_context(*tc);
  6445. *tc = NULL;
  6446. }
  6447. mlog_exit_void();
  6448. return status;
  6449. }
  6450. /*
  6451. * 'start' is inclusive, 'end' is not.
  6452. */
  6453. int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
  6454. unsigned int start, unsigned int end, int trunc)
  6455. {
  6456. int ret;
  6457. unsigned int numbytes;
  6458. handle_t *handle;
  6459. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  6460. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  6461. struct ocfs2_inline_data *idata = &di->id2.i_data;
  6462. if (end > i_size_read(inode))
  6463. end = i_size_read(inode);
  6464. BUG_ON(start >= end);
  6465. if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  6466. !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
  6467. !ocfs2_supports_inline_data(osb)) {
  6468. ocfs2_error(inode->i_sb,
  6469. "Inline data flags for inode %llu don't agree! "
  6470. "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
  6471. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  6472. le16_to_cpu(di->i_dyn_features),
  6473. OCFS2_I(inode)->ip_dyn_features,
  6474. osb->s_feature_incompat);
  6475. ret = -EROFS;
  6476. goto out;
  6477. }
  6478. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  6479. if (IS_ERR(handle)) {
  6480. ret = PTR_ERR(handle);
  6481. mlog_errno(ret);
  6482. goto out;
  6483. }
  6484. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  6485. OCFS2_JOURNAL_ACCESS_WRITE);
  6486. if (ret) {
  6487. mlog_errno(ret);
  6488. goto out_commit;
  6489. }
  6490. numbytes = end - start;
  6491. memset(idata->id_data + start, 0, numbytes);
  6492. /*
  6493. * No need to worry about the data page here - it's been
  6494. * truncated already and inline data doesn't need it for
  6495. * pushing zero's to disk, so we'll let readpage pick it up
  6496. * later.
  6497. */
  6498. if (trunc) {
  6499. i_size_write(inode, start);
  6500. di->i_size = cpu_to_le64(start);
  6501. }
  6502. inode->i_blocks = ocfs2_inode_sector_count(inode);
  6503. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  6504. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  6505. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  6506. ocfs2_journal_dirty(handle, di_bh);
  6507. out_commit:
  6508. ocfs2_commit_trans(osb, handle);
  6509. out:
  6510. return ret;
  6511. }
  6512. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  6513. {
  6514. /*
  6515. * The caller is responsible for completing deallocation
  6516. * before freeing the context.
  6517. */
  6518. if (tc->tc_dealloc.c_first_suballocator != NULL)
  6519. mlog(ML_NOTICE,
  6520. "Truncate completion has non-empty dealloc context\n");
  6521. brelse(tc->tc_last_eb_bh);
  6522. kfree(tc);
  6523. }