alloc.c 196 KB

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