alloc.c 181 KB

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