alloc.c 178 KB

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