alloc.c 191 KB

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