alloc.c 168 KB

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