alloc.c 190 KB

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