alloc.c 196 KB

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