alloc.c 191 KB

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