alloc.c 186 KB

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