alloc.c 189 KB

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