alloc.c 183 KB

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