alloc.c 186 KB

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