alloc.c 197 KB

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