alloc.c 184 KB

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