alloc.c 192 KB

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